-
Notifications
You must be signed in to change notification settings - Fork 0
/
dont-break-the-code.php
executable file
·145 lines (127 loc) · 3.87 KB
/
dont-break-the-code.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*
Plugin Name: Don't Break The Code
Plugin URI: http://www.strangerstudios.com/wp/dont-break-the-code/
Description: Disable the Visual editor on a per post basis.
Version: .3
Author: Stranger Studios (WordCamp Philly)
Author URI: http://www.strangerstudios.com
*/
/*
Runs on the edit post page in wp-admin. Set the cookie for the HTML tab.
*/
function dbtc_load_post()
{
$cookie_path = str_replace('/wp-admin/post.php','/',$_SERVER['PHP_SELF']);
setcookie('wp-settings-1','m5=3&editor=html&m9=o&m1=o',0,$cookie_path);
add_filter("format_to_edit", "dbtc_format_to_edit");
add_action("admin_footer", "dbtc_admin_footer", 10);
}
add_action("load-post.php", "dbtc_load_post");
function dbtc_format_to_edit($content)
{
if(isset($_GET['post']) && get_post_meta($_GET['post'], '_dbtc_disable_visual', true) != false){
$cookie_path = str_replace('/wp-admin/post.php','/',$_SERVER['PHP_SELF']);
setcookie('wp-settings-1','m5=3&editor=html&m9=o&m1=o',0,$cookie_path);
add_filter('admin_footer', 'dbtc_admin_footer_disabled', 15);
}
return $content;
}
/*
Javascript to select the HTML tab and disable the Visual tab.
*/
function dbtc_admin_footer()
{
?>
<script type="text/javascript">
function dbtc_disableVisualEditor()
{
switchEditors.go('content', 'html');
jQuery('#content-tmce').attr('onclick', '');
if(jQuery('#edButtonPreviewSpan').length)
{
jQuery('#edButtonPreviewSpan').css('text-decoration', 'line-through');
}
else
{
var edButtonPreviewHTML = jQuery('#content-tmce').html();
jQuery('#content-tmce').html('<span id="edButtonPreviewSpan" style="text-decoration:line-through">'+edButtonPreviewHTML+'</span>');
}
}
function dbtc_enableVisualEditor()
{
switchEditors.go('content', 'tinymce');
jQuery('#content-tmce').attr('onclick', "switchEditors.go('content', 'tinymce');");
//var edButtonPreviewHTML = jQuery('#content-tmce').html();
jQuery('#edButtonPreviewSpan').attr('style', '');
}
jQuery(document).ready(function(){
jQuery('#dbtc_disable_visual').click(function(){
//disable/enable the visual editor
if(jQuery('#dbtc_disable_visual').attr('checked'))
{
dbtc_disableVisualEditor();
var checked = '1';
}
else
{
dbtc_enableVisualEditor();
var checked = '';
}
//update post meta
data = 'action=dbtc_save&post=<?php echo $_GET["post"]; ?>&dbtc_disable_visual=' + checked;
jQuery.post(ajaxurl, data, function(response) {
//alert('Got this from the server: ' + response);
});
});
});
</script>
<?php
}
function dbtc_admin_footer_disabled()
{
?>
<script type="text/javascript">
jQuery(document).ready(function(){
dbtc_disableVisualEditor();
});
</script>
<?php
}
/*
Add checkbox to the screen settings.
*/
function dbtc_screen_settings($current, $screen)
{
//only for admins
if(!current_user_can("manage_options"))
return;
if(in_array($screen->id, array("post", "page")))
{
$checked = "";
if(isset($_GET['post']) && get_post_meta($_GET['post'], '_dbtc_disable_visual', true) != false) $checked = ' checked="checked" ';
$current .= "<h5>Don't Break The Code</h5>";
$current .= '<input type="checkbox" id="dbtc_disable_visual" name="dbtc_disable_visual" '.$checked.'/> ';
$current .= '<label for="dbtc_checkbox"> ';
$current .= __("Disable Visual Editor", 'dbtc_textbox' );
$current .= '</label> ';
}
return $current;
}
add_filter('screen_settings', 'dbtc_screen_settings', 10, 2);
/*
Ajax called when saving options
*/
function dbtc_save_options()
{
$dbtc_disable_visual = $_POST['dbtc_disable_visual'];
$post_id = $_POST['post'];
if($post_id && $dbtc_disable_visual !== NULL)
{
update_post_meta($post_id, "_dbtc_disable_visual", $dbtc_disable_visual);
}
//this is called via Ajax, so just exit here
exit;
}
add_action('wp_ajax_dbtc_save', 'dbtc_save_options');
?>