Raw HTML - Version 1.3

Version Description

  • Added a new panel to the "Screen Options" box that lest you auto-enable specific RawHTML settings (e.g. "Disable automatic paragraphs") for all new or updated posts.
Download this release

Release Info

Developer whiteshadow
Plugin Icon wp plugin Raw HTML
Version 1.3
Comparing to
See all releases

Code changes from version 1.2.5 to 1.3

raw_html.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Raw HTML capability
4
  Plugin URI: http://w-shadow.com/blog/2007/12/13/raw-html-in-wordpress/
5
  Description: Lets you enter raw HTML in your posts. You can also enable/disable smart quotes and other automatic formatting on a per-post basis.
6
- Version: 1.2.5
7
  Author: Janis Elsts
8
  Author URI: http://w-shadow.com/blog/
9
  */
@@ -13,6 +13,8 @@ Created by Janis Elsts (email : whiteshadow@w-shadow.com)
13
  It's LGPL.
14
  */
15
 
 
 
16
  /**********************************************
17
  Filter inline blocks of raw HTML
18
  ***********************************************/
@@ -168,13 +170,19 @@ function rawhtml_meta_box(){
168
  'disable_wpautop' => 'Disable automatic paragraphs',
169
  'disable_convert_chars' => 'Disable convert_chars',
170
  'disable_convert_smilies' => 'Disable smilies',
171
- );
 
172
  foreach($fields as $field => $legend){
 
 
 
 
 
 
173
  ?>
174
  <label for="rawhtml_<?php echo $field; ?>">
175
  <input type="checkbox" name="rawhtml_<?php echo $field; ?>" id="rawhtml_<?php echo $field; ?>" <?php
176
- if (get_post_meta($post->ID, $field, true) == '1')
177
- echo ' checked="checked"';
178
  ?>/>
179
  <?php echo $legend; ?>
180
  </label>
@@ -188,6 +196,10 @@ function rawhtml_save_postdata( $post_id ){
188
  // verify this came from the our screen and with proper authorization,
189
  // because save_post can be triggered at other times
190
 
 
 
 
 
191
  if ( !wp_verify_nonce( $_POST['rawhtml_nonce'], plugin_basename(__FILE__) )) {
192
  return $post_id;
193
  }
@@ -206,10 +218,128 @@ function rawhtml_save_postdata( $post_id ){
206
  if ( !empty($_POST['rawhtml_'.$field]) ){
207
  update_post_meta($post_id, $field, '1');
208
  } else {
209
- delete_post_meta($post_id, $field);
210
  };
211
  }
212
 
213
  return true;
214
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  ?>
3
  Plugin Name: Raw HTML capability
4
  Plugin URI: http://w-shadow.com/blog/2007/12/13/raw-html-in-wordpress/
5
  Description: Lets you enter raw HTML in your posts. You can also enable/disable smart quotes and other automatic formatting on a per-post basis.
6
+ Version: 1.3
7
  Author: Janis Elsts
8
  Author URI: http://w-shadow.com/blog/
9
  */
13
  It's LGPL.
14
  */
15
 
16
+ include 'screen-options/screen-options.php';
17
+
18
  /**********************************************
19
  Filter inline blocks of raw HTML
20
  ***********************************************/
170
  'disable_wpautop' => 'Disable automatic paragraphs',
171
  'disable_convert_chars' => 'Disable convert_chars',
172
  'disable_convert_smilies' => 'Disable smilies',
173
+ );
174
+ $defaults = rawhtml_get_default_settings();
175
  foreach($fields as $field => $legend){
176
+ $current_setting = get_post_meta($post->ID, $field, true);
177
+ if ( $current_setting == '' ){
178
+ $current_setting = $defaults[$field];
179
+ } else {
180
+ $current_setting = (bool)intval($current_setting);
181
+ }
182
  ?>
183
  <label for="rawhtml_<?php echo $field; ?>">
184
  <input type="checkbox" name="rawhtml_<?php echo $field; ?>" id="rawhtml_<?php echo $field; ?>" <?php
185
+ if ($current_setting) echo ' checked="checked"';
 
186
  ?>/>
187
  <?php echo $legend; ?>
188
  </label>
196
  // verify this came from the our screen and with proper authorization,
197
  // because save_post can be triggered at other times
198
 
199
+ if ( !isset($_POST['rawhtml_nonce']) ){
200
+ return $post_id;
201
+ }
202
+
203
  if ( !wp_verify_nonce( $_POST['rawhtml_nonce'], plugin_basename(__FILE__) )) {
204
  return $post_id;
205
  }
218
  if ( !empty($_POST['rawhtml_'.$field]) ){
219
  update_post_meta($post_id, $field, '1');
220
  } else {
221
+ update_post_meta($post_id, $field, '0');
222
  };
223
  }
224
 
225
  return true;
226
  }
227
+
228
+ //Add our panel to the "Screen Options" box
229
+ add_screen_options_panel(
230
+ 'rawhtml-default-settings',
231
+ 'Raw HTML defaults',
232
+ 'rawhtml_default_settings_panel',
233
+ array('post', 'page'),
234
+ 'rawhtml_save_new_defaults',
235
+ true
236
+ );
237
+
238
+ /**
239
+ * Retrieve the default settings for our post/page meta box.
240
+ * Settings are saved in user meta.
241
+ *
242
+ * @return array
243
+ */
244
+ function rawhtml_get_default_settings(){
245
+ //By default, all tweaks are disabled
246
+ $defaults = array(
247
+ 'disable_wptexturize' => false,
248
+ 'disable_wpautop' => false,
249
+ 'disable_convert_chars' => false,
250
+ 'disable_convert_smilies' => false,
251
+ );
252
+
253
+ if ( !function_exists('wp_get_current_user') || !function_exists('get_user_meta') ){
254
+ return $defaults;
255
+ }
256
+
257
+ //Get current defaults, if any
258
+ $user = wp_get_current_user();
259
+ $user_defaults = get_user_meta($user->ID, 'rawhtml_defaults', true);
260
+ if ( is_array($user_defaults) ){
261
+ $defaults = array_merge($defaults, $user_defaults);
262
+ }
263
+
264
+ return $defaults;
265
+ }
266
+
267
+ /**
268
+ * Update default settings for our post/page meta box.
269
+ *
270
+ * @param array $new_defaults
271
+ * @return bool True on success, false on failure.
272
+ */
273
+ function rawhtml_set_default_settings($new_defaults){
274
+ if ( !function_exists('wp_get_current_user') || !function_exists('update_user_meta') ){
275
+ return false;
276
+ }
277
+
278
+ //Get current defaults, if any
279
+ $user = wp_get_current_user();
280
+ if ( isset($user) && $user && isset($user->ID) ){
281
+ return update_user_meta($user->ID, 'rawhtml_defaults', $new_defaults);
282
+ } else {
283
+ return false;
284
+ }
285
+ }
286
+
287
+ /**
288
+ * Generate the "Raw HTML defaults" panel for Screen Options.
289
+ *
290
+ * @return string
291
+ */
292
+ function rawhtml_default_settings_panel(){
293
+ $defaults = rawhtml_get_default_settings();
294
+
295
+ //Output checkboxes
296
+ $fields = array(
297
+ 'disable_wptexturize' => 'Disable wptexturize',
298
+ 'disable_wpautop' => 'Disable automatic paragraphs',
299
+ 'disable_convert_chars' => 'Disable convert_chars',
300
+ 'disable_convert_smilies' => 'Disable smilies',
301
+ );
302
+
303
+ $output = '';
304
+ foreach($fields as $field => $legend){
305
+ $esc_field = esc_attr($field);
306
+ $output .= sprintf(
307
+ '<label for="rawhtml_default-%s" style="line-height: 20px;">
308
+ <input type="checkbox" name="rawhtml_default-%s" id="rawhtml_default-%s"%s>
309
+ %s
310
+ </label><br>',
311
+ $esc_field,
312
+ $esc_field,
313
+ $esc_field,
314
+ ($defaults[$field]?' checked="checked"':''),
315
+ $legend
316
+ );
317
+ }
318
+
319
+ return $output;
320
+ }
321
+
322
+ /**
323
+ * Process the "Raw HTML defaults" form fields and save new settings
324
+ *
325
+ * @param array $params
326
+ * @return void
327
+ */
328
+ function rawhtml_save_new_defaults($params){
329
+ //Get current defaults
330
+ $defaults = rawhtml_get_default_settings();
331
+
332
+ //Read new values from the submitted form
333
+ foreach($defaults as $field => $old_value){
334
+ if ( isset($params['rawhtml_default-'.$field]) && ($params['rawhtml_default-'.$field] == 'on') ){
335
+ $defaults[$field] = true;
336
+ } else {
337
+ $defaults[$field] = false;
338
+ }
339
+ }
340
+
341
+ //Store the new defaults
342
+ rawhtml_set_default_settings($defaults);
343
+ }
344
+
345
  ?>
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Raw HTML ===
2
  Contributors: whiteshadow
3
  Tags: posts, formatting, javascript, html, css, code
4
- Requires at least: 2.2
5
  Tested up to: 3.0
6
- Stable tag: 1.2.5
7
 
8
  Lets you use raw HTML or any other code in your posts. You can also disable smart quotes and other automatic formatting on a per-post basis.
9
 
@@ -11,7 +11,7 @@ Lets you use raw HTML or any other code in your posts. You can also disable smar
11
 
12
  This plugin lets you use raw HTML or any other code in your posts. One way to use it is to wrap a part of your post in special tags (below) to prevent WordPress from converting newlines to HTML paragraphs, escaping apostrophes and so on. This is very useful if you need to add a CSS block or JavaScript to your post.
13
 
14
- RawHTML will also add some new checkboxes to the "Edit" screen that will let you disable certain WP filters on a per-post basis. This way you can :
15
 
16
  * Disable wptexturize (this filter creates smart quotes and other typographic characters).
17
  * Disable automatic paragraph creation.
@@ -48,8 +48,12 @@ To install the plugin follow these steps :
48
 
49
  == Changelog ==
50
 
 
 
 
51
  = 1.2.5 =
52
  * Fixed a conflict with the Intypo plugin
 
53
 
54
  = 1.2.4 =
55
  * Added an autogenerated changelog.
1
  === Raw HTML ===
2
  Contributors: whiteshadow
3
  Tags: posts, formatting, javascript, html, css, code
4
+ Requires at least: 2.6
5
  Tested up to: 3.0
6
+ Stable tag: 1.3
7
 
8
  Lets you use raw HTML or any other code in your posts. You can also disable smart quotes and other automatic formatting on a per-post basis.
9
 
11
 
12
  This plugin lets you use raw HTML or any other code in your posts. One way to use it is to wrap a part of your post in special tags (below) to prevent WordPress from converting newlines to HTML paragraphs, escaping apostrophes and so on. This is very useful if you need to add a CSS block or JavaScript to your post.
13
 
14
+ RawHTML will also add some new checkboxes to the "Edit" screen that let you disable certain WP filters on a per-post basis. This way you can :
15
 
16
  * Disable wptexturize (this filter creates smart quotes and other typographic characters).
17
  * Disable automatic paragraph creation.
48
 
49
  == Changelog ==
50
 
51
+ = 1.3 =
52
+ * Added a new panel to the "Screen Options" box that lest you auto-enable specific RawHTML settings (e.g. "Disable automatic paragraphs") for all new or updated posts.
53
+
54
  = 1.2.5 =
55
  * Fixed a conflict with the Intypo plugin
56
+ * Fixed example code formatting in readme.txt
57
 
58
  = 1.2.4 =
59
  * Added an autogenerated changelog.
screen-options/screen-options.js ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(function($){
2
+ function performAutosave(){
3
+ var panel = $(this).parents('div.custom-options-panel');
4
+ var params = panel.find('input, select, textarea').serialize();
5
+ params = params + '&action=save_settings-' + panel.attr('id');
6
+ $.post(
7
+ 'admin-ajax.php',
8
+ params
9
+ );
10
+ }
11
+
12
+ $('#screen-options-wrap div.requires-autosave').find('input, select, textarea').change(performAutosave);
13
+ });
screen-options/screen-options.php ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ if ( !class_exists('wsScreenOptions10') ):
4
+
5
+ /**
6
+ * Class for adding new panels to the "Screen Options" box.
7
+ *
8
+ * Do not access this class directly. Instead, use the add_screen_options_panel() function.
9
+ *
10
+ * @author Janis Elsts
11
+ * @copyright 2010
12
+ * @version 1.0
13
+ * @access public
14
+ */
15
+ class wsScreenOptions10 {
16
+ var $registered_panels; //List of custom "Screen Options" panels
17
+ var $page_panels; //Index of panels registered for each page ($page => array of panel ids).
18
+
19
+ /**
20
+ * Class constructor
21
+ *
22
+ * @return void
23
+ */
24
+ function wsScreenOptions10(){
25
+ $this->registered_panels = array();
26
+ $this->page_panels = array();
27
+
28
+ add_filter('screen_settings', array(&$this, 'append_screen_settings'), 10, 2);
29
+ add_action('admin_print_scripts', array(&$this, 'add_autosave_script'));
30
+ }
31
+
32
+ /**
33
+ * Add a new settings panel to the "Screen Options" box.
34
+ *
35
+ * @param string $id String to use in the 'id' attribute of the settings panel. Should be unique.
36
+ * @param string $title Title of the settings panel. Set to an empty string to omit title.
37
+ * @param callback $callback Function that fills the panel with the desired content. Should return its output.
38
+ * @param string|array $page The page(s) on which to show the panel (similar to add_meta_box()).
39
+ * @param callback $save_callback Optional. Function that saves the settings.
40
+ * @param bool $autosave Optional. If se, settings will be automatically saved (via AJAX) when the value of any input element in the panel changes. Defaults to false.
41
+ * @return void
42
+ */
43
+ function add_screen_options_panel($id, $title, $callback, $page, $save_callback = null, $autosave = false){
44
+ if ( !is_array($page) ){
45
+ $page = array($page);
46
+ }
47
+ //Convert page hooks/slugs to screen IDs
48
+ $page = array_map(array(&$this, 'page_to_screen_id'), $page);
49
+ $page = array_unique($page);
50
+
51
+ $new_panel = array(
52
+ 'title' => $title,
53
+ 'callback' => $callback,
54
+ 'page' => $page,
55
+ 'save_callback' => $save_callback,
56
+ 'autosave' => $autosave,
57
+ );
58
+
59
+ if ( $save_callback ){
60
+ add_action('wp_ajax_save_settings-' . $id, array(&$this, 'ajax_save_callback'));
61
+ }
62
+
63
+ foreach($page as $page_id){
64
+ if ( !isset($this->page_panels[$page_id]) ){
65
+ $this->page_panels[$page_id] = array();
66
+ }
67
+ $this->page_panels[$page_id][] = $id;
68
+ }
69
+
70
+ $this->registered_panels[$id] = $new_panel;
71
+ }
72
+
73
+ /**
74
+ * Convert a page hook name to a screen ID.
75
+ *
76
+ * @uses convert_to_screen()
77
+ * @access private
78
+ *
79
+ * @param string $page
80
+ * @return string
81
+ */
82
+ function page_to_screen_id($page){
83
+ if ( function_exists('convert_to_screen') ){
84
+ $screen = convert_to_screen($page);
85
+ if ( isset($screen->id) ){
86
+ return $screen->id;
87
+ } else {
88
+ return '';
89
+ }
90
+ } else {
91
+ return str_replace( array('.php', '-new', '-add' ), '', $page);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Append custom panel HTML to the "Screen Options" box of the current page.
97
+ * Callback for the 'screen_settings' filter (available in WP 3.0 and up).
98
+ *
99
+ * @access private
100
+ *
101
+ * @param string $current
102
+ * @param string $screen Screen object (undocumented).
103
+ * @return string The HTML code to append to "Screen Options"
104
+ */
105
+ function append_screen_settings($current, $screen){
106
+ global $hook_suffix;
107
+
108
+ //Sanity check
109
+ if ( !isset($screen->id) ) {
110
+ return $current;
111
+ }
112
+
113
+ //Are there any panels that want to appear on this page?
114
+ if ( !isset($this->page_panels[$screen->id]) || empty($this->page_panels[$screen->id]) ){
115
+ return $current;
116
+ }
117
+ $panels = $this->page_panels[$screen->id];
118
+
119
+ //Append all panels registered for this screen
120
+ foreach($panels as $panel_id){
121
+ $panel = $this->registered_panels[$panel_id];
122
+
123
+ //Add panel title
124
+ if ( !empty($panel['title']) ){
125
+ $current .= "\n<h5>".$panel['title']."</h5>\n";
126
+ }
127
+ //Generate panel contents
128
+ if ( is_callable($panel['callback']) ){
129
+ $contents = call_user_func($panel['callback']);
130
+ $classes = array(
131
+ 'metabox-prefs',
132
+ 'custom-options-panel',
133
+ );
134
+ if ( $panel['autosave'] ){
135
+ $classes[] = 'requires-autosave';
136
+ }
137
+
138
+ $contents = sprintf(
139
+ '<div id="%s" class="%s"><input type="hidden" name="_wpnonce-%s" value="%s" />%s</div>',
140
+ esc_attr($panel_id),
141
+ implode(' ',$classes),
142
+ esc_attr($panel_id),
143
+ wp_create_nonce('save_settings-'.$panel_id),
144
+ $contents
145
+ );
146
+
147
+ $current .= $contents;
148
+ }
149
+ }
150
+
151
+ return $current;
152
+ }
153
+
154
+ /**
155
+ * AJAX callback for the "Screen Options" autosave.
156
+ *
157
+ * @access private
158
+ * @return void
159
+ */
160
+ function ajax_save_callback(){
161
+ if ( empty($_POST['action']) ){
162
+ die('0');
163
+ }
164
+
165
+ //The 'action' argument is in the form "save_settings-panel_id"
166
+ $id = end(explode('-', $_POST['action'], 2));
167
+
168
+ //Basic security check.
169
+ check_ajax_referer('save_settings-' . $id, '_wpnonce-' . $id);
170
+
171
+ //Hand the request to the registered callback, if any
172
+ if ( !isset($this->registered_panels[$id]) ){
173
+ exit('0');
174
+ }
175
+ $panel = $this->registered_panels[$id];
176
+ if ( is_callable($panel['save_callback']) ){
177
+ call_user_func($panel['save_callback'], $_POST);
178
+ die('1');
179
+ } else {
180
+ die('0');
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Add/enqueue supporting JavaScript for the autosave function of custom "Screen Options" panels.
186
+ *
187
+ * Checks if the current page is supposed to contain any autosave-enabled
188
+ * panels and adds the script only if that's the case.
189
+ *
190
+ * @return void
191
+ */
192
+ function add_autosave_script(){
193
+ //Get the page id/hook/slug/whatever.
194
+ global $hook_suffix;
195
+ $page = $this->page_to_screen_id($hook_suffix);
196
+
197
+ //Check if we have some panels with autosave registered for this page.
198
+ if ( !isset($this->page_panels[$page]) || empty($this->page_panels[$page]) ){
199
+ return;
200
+ }
201
+ $panels = $this->page_panels[$page];
202
+ $got_autosave = false;
203
+ foreach($panels as $panel_id){
204
+ if ( $this->registered_panels[$panel_id]['autosave'] ){
205
+ $got_autosave = true;
206
+ break;
207
+ }
208
+ }
209
+
210
+ if ( $got_autosave ){
211
+ //Enqueue the script itself
212
+ $url = plugins_url('screen-options.js', __FILE__);
213
+ wp_enqueue_script('screen-options-custom-autosave', $url, array('jquery'));
214
+ }
215
+ }
216
+ }
217
+
218
+ //All versions of the class are stored in a global array
219
+ //and only the latest version is actually used.
220
+ global $ws_screen_options_versions;
221
+ if ( !isset($ws_screen_options_versions) ){
222
+ $ws_screen_options_versions = array();
223
+ }
224
+ $ws_screen_options_versions['1.0'] = 'wsScreenOptions10';
225
+
226
+ endif;
227
+
228
+ if ( !function_exists('add_screen_options_panel') ){
229
+
230
+ /**
231
+ * Add a new settings panel to the "Screen Options" box.
232
+ *
233
+ * @see wsScreenOptions10::add_screen_options_panel()
234
+ *
235
+ * @param string $id String to use in the 'id' attribute of the settings panel. Should be unique.
236
+ * @param string $title Title of the settings panel. Set to an empty string to omit title.
237
+ * @param callback $callback Function that fills the panel with the desired content. Should return its output.
238
+ * @param string|array $page The page(s) on which to show the panel (similar to add_meta_box()).
239
+ * @param callback $save_callback Optional. Function that saves the settings contained in the panel.
240
+ * @param bool $autosave Optional. If se, settings will be automatically saved (via AJAX) when the value of any input element in the panel changes. Defaults to false.
241
+ * @return void
242
+ */
243
+ function add_screen_options_panel($id, $title, $callback, $page, $save_callback = null, $autosave = false){
244
+ global $ws_screen_options_versions;
245
+
246
+ static $instance = null;
247
+ if ( is_null($instance) ){
248
+ //Instantiate the latest version of the wsScreenOptions class
249
+ uksort($ws_screen_options_versions, 'version_compare');
250
+ $className = end($ws_screen_options_versions);
251
+ $instance = new $className;
252
+ }
253
+
254
+ return $instance->add_screen_options_panel($id, $title, $callback, $page, $save_callback, $autosave);
255
+ }
256
+
257
+ }
258
+
259
+ ?>