Post Snippets - Version 1.4

Version Description

Download this release

Release Info

Developer artstorm
Plugin Icon 128x128 Post Snippets
Version 1.4
Comparing to
See all releases

Code changes from version 1.3.5 to 1.4

Files changed (3) hide show
  1. post-snippets.php +97 -31
  2. readme.txt +7 -2
  3. tinymce/window.php +30 -1
post-snippets.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Post Snippets
4
  Plugin URI: http://coding.cglounge.com/wordpress-plugins/post-snippets/
5
  Description: Stores snippets of HTML code or reoccurring text that you often use in your posts. You can use predefined variables to replace parts of the snippet on insert. All snippets are available in the post editor with a TinyMCE button or Quicktags.
6
- Version: 1.3.5
7
  Author: Johan Steen
8
  Author URI: http://coding.cglounge.com/
9
  Text Domain: post-snippets
@@ -59,6 +59,7 @@ class postSnippets {
59
  function init_hooks() {
60
  add_action('admin_menu', array(&$this,'wp_admin'));
61
  add_action('admin_footer', array(&$this,'quicktags'));
 
62
  }
63
 
64
  /**
@@ -70,6 +71,40 @@ class postSnippets {
70
  echo '<div class="updated fade"><p><strong>'.__('Post Snippets requires WordPress version 2.7 or later!', 'post-snippets').'</strong></p></div>';
71
  }
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  /**
74
  * Handling of QuickTags in the HTML editor
75
  *
@@ -88,40 +123,57 @@ class postSnippets {
88
  var postSnippetsNr, postSnippetsButton;
89
  ';
90
  for ($i = 0; $i < count($snippets); $i++) {
91
-
92
- // Make it js safe
93
- $theSnippet = str_replace('"','\"',str_replace(Chr(13), '', str_replace(Chr(10), '', $snippets[$i]['snippet'])));
94
- $var_arr = explode(",",$snippets[$i]['vars']);
95
- $theVariables = "";
96
- if (!empty($var_arr[0])) {
97
- for ($j = 0; $j < count($var_arr); $j++) {
98
- $theVariables = $theVariables . "'" . $var_arr[$j] . "'";
99
- if ( $j < (count($var_arr) -1) )
100
- $theVariables = $theVariables . ", ";
101
-
 
102
  }
103
- }
104
-
105
- echo '
106
- postSnippetsNr = edButtons.length;
107
- edButtons[postSnippetsNr] = new edButton(\'ed_ps'. $i . '\', \'' . $snippets[$i]['title'] . '\', \''.$snippets[$i]['snippet'].'\', \'\', \'\', -1);
108
- var postSnippetsButton = postSnippetsToolbar.lastChild;
109
-
110
- while (postSnippetsButton.nodeType != 1) {
111
- postSnippetsButton = postSnippetsButton.previousSibling;
112
  }
113
-
114
- postSnippetsButton = postSnippetsButton.cloneNode(true);
115
- postSnippetsToolbar.appendChild(postSnippetsButton);
116
- postSnippetsButton.value = \'' . $snippets[$i]['title'] . '\';
117
- postSnippetsButton.title = postSnippetsNr;
118
- var variables' . $i .' = new Array('.$theVariables.');
119
- postSnippetsButton.onclick = function () {edInsertSnippet(edCanvas, \''.$theSnippet.'\', variables' . $i .', parseInt(this.title));}
120
- postSnippetsButton.id = "ed_ps' . $i .'";
121
- ';
122
- }
 
 
 
 
 
 
 
 
 
123
  echo '
124
  }
 
 
 
 
 
 
 
 
 
 
125
  function edInsertSnippet(myField,theSnippet,theVariables) {
126
  var myValue;
127
  var insertString;
@@ -174,6 +226,8 @@ class postSnippets {
174
  array_push($snippets, array (
175
  'title' => "Untitled",
176
  'vars' => "",
 
 
177
  'snippet' => ""));
178
  update_option($this->plugin_options, $snippets);
179
  $this->admin_message( __( 'A snippet named Untitled has been added.', 'post-snippets' ) );
@@ -186,6 +240,8 @@ class postSnippets {
186
  for ($i=0; $i < count($snippets); $i++) {
187
  $snippets[$i]['title'] = trim($_POST[$i.'_title']);
188
  $snippets[$i]['vars'] = trim($_POST[$i.'_vars']);
 
 
189
  $snippets[$i]['snippet'] = trim(stripslashes($_POST[$i.'_snippet']));
190
  }
191
  update_option($this->plugin_options, $snippets);
@@ -230,6 +286,8 @@ class postSnippets {
230
  <th scope="col" style="width: 180px;"><?php _e( 'Title', 'post-snippets' ) ?></th>
231
  <th scope="col" style="width: 180px;"><?php _e( 'Variables', 'post-snippets' ) ?></th>
232
  <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
 
 
233
  </tr>
234
  </thead>
235
 
@@ -239,6 +297,8 @@ class postSnippets {
239
  <th scope="col"><?php _e( 'Title', 'post-snippets' ) ?></th>
240
  <th scope="col"><?php _e( 'Variables', 'post-snippets' ) ?></th>
241
  <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
 
 
242
  </tr>
243
  </tfoot>
244
 
@@ -252,6 +312,8 @@ class postSnippets {
252
  <td class='row-title'><input type='text' name='<?= $i ?>_title' value='<?= $snippets[$i]['title'] ?>' /></td>
253
  <td class='name'><input type='text' name='<?= $i ?>_vars' value='<?= $snippets[$i]['vars'] ?>' /></td>
254
  <td class='desc'><textarea name="<?= $i ?>_snippet" class="large-text" rows="3"><?= $snippets[$i]['snippet'] ?></textarea></td>
 
 
255
  </tr>
256
  <?php
257
  }
@@ -272,6 +334,10 @@ class postSnippets {
272
  <p><?php _e( '<strong>Variables</strong><br/>A comma separated list of custom variables you can reference in your snippet.<br/><br/>Example:<br/>url,name', 'post-snippets' ); ?></p>
273
 
274
  <p><?php _e( '<strong>Snippet</strong><br/>This is the block of text or HTML to insert in the post when you select the snippet from the insert button in the TinyMCE panel in the post editor. If you have entered predefined variables you can reference them from the snippet by enclosing them in {} brackets.<br/><br/>Example:<br/>To reference the variables in the example above, you would enter {url} and {name}.<br/><br/>So if you enter this snippet:<br/><i>This is the website of &lt;a href="{url}"&gt;{name}&lt;/a&gt;</i><br/>You will get the option to replace url and name on insert if they are defined as variables.', 'post-snippets' ); ?></p>
 
 
 
 
275
  </div>
276
  </div>
277
  </div>
3
  Plugin Name: Post Snippets
4
  Plugin URI: http://coding.cglounge.com/wordpress-plugins/post-snippets/
5
  Description: Stores snippets of HTML code or reoccurring text that you often use in your posts. You can use predefined variables to replace parts of the snippet on insert. All snippets are available in the post editor with a TinyMCE button or Quicktags.
6
+ Version: 1.4
7
  Author: Johan Steen
8
  Author URI: http://coding.cglounge.com/
9
  Text Domain: post-snippets
59
  function init_hooks() {
60
  add_action('admin_menu', array(&$this,'wp_admin'));
61
  add_action('admin_footer', array(&$this,'quicktags'));
62
+ $this->create_shortcodes();
63
  }
64
 
65
  /**
71
  echo '<div class="updated fade"><p><strong>'.__('Post Snippets requires WordPress version 2.7 or later!', 'post-snippets').'</strong></p></div>';
72
  }
73
 
74
+
75
+ /**
76
+ * Create the functions for shortcodes dynamically and register them
77
+ *
78
+ */
79
+ function create_shortcodes() {
80
+ $snippets = get_option($this->plugin_options);
81
+ if (!empty($snippets)) {
82
+ for ($i=0; $i < count($snippets); $i++) {
83
+ if ($snippets[$i]['shortcode'] == true) {
84
+ $vars = explode(",",$snippets[$i]['vars']);
85
+ $vars_str = '';
86
+ for ($j=0; $j < count($vars); $j++) {
87
+ $vars_str = $vars_str . '"'.$vars[$j].'" => "",';
88
+
89
+ }
90
+ add_shortcode($snippets[$i]['title'], create_function('$atts',
91
+ '$shortcode_symbols = array('.$vars_str.');
92
+ extract(shortcode_atts($shortcode_symbols, $atts));
93
+
94
+ $newArr = compact( array_keys($shortcode_symbols) );
95
+
96
+ $snippet = "'.$snippets[$i]["snippet"].'";
97
+
98
+ foreach ($newArr as $key => $val) {
99
+ $snippet = str_replace("{".$key."}", $val, $snippet);
100
+ }
101
+
102
+ return "{$snippet}";') );
103
+ }
104
+ }
105
+ }
106
+ }
107
+
108
  /**
109
  * Handling of QuickTags in the HTML editor
110
  *
123
  var postSnippetsNr, postSnippetsButton;
124
  ';
125
  for ($i = 0; $i < count($snippets); $i++) {
126
+ if ($snippets[$i]['quicktag']) {
127
+ // Make it js safe
128
+ $theSnippet = str_replace('"','\"',str_replace(Chr(13), '', str_replace(Chr(10), '', $snippets[$i]['snippet'])));
129
+ $var_arr = explode(",",$snippets[$i]['vars']);
130
+ $theVariables = "";
131
+ if (!empty($var_arr[0])) {
132
+ for ($j = 0; $j < count($var_arr); $j++) {
133
+ $theVariables = $theVariables . "'" . $var_arr[$j] . "'";
134
+ if ( $j < (count($var_arr) -1) )
135
+ $theVariables = $theVariables . ", ";
136
+
137
+ }
138
  }
139
+
140
+ if ($snippets[$i]['shortcode']) {
141
+ echo "var variables" . $i ." = new Array(".$theVariables.");";
142
+ echo "var insertString" . $i ." = createShortcode('".$snippets[$i]['title']."', variables".$i.");";
143
+ }else{
144
+ echo "var insertString" . $i ." = '" .$theSnippet. "';";
 
 
 
145
  }
146
+ echo '
147
+ postSnippetsNr = edButtons.length;
148
+ edButtons[postSnippetsNr] = new edButton(\'ed_ps'. $i . '\', \'' . $snippets[$i]['title'] . '\', \''.$snippets[$i]['snippet'].'\', \'\', \'\', -1);
149
+ var postSnippetsButton = postSnippetsToolbar.lastChild;
150
+
151
+ while (postSnippetsButton.nodeType != 1) {
152
+ postSnippetsButton = postSnippetsButton.previousSibling;
153
+ }
154
+
155
+ postSnippetsButton = postSnippetsButton.cloneNode(true);
156
+ postSnippetsToolbar.appendChild(postSnippetsButton);
157
+ postSnippetsButton.value = \'' . $snippets[$i]['title'] . '\';
158
+ postSnippetsButton.title = postSnippetsNr;
159
+ var variables' . $i .' = new Array('.$theVariables.');
160
+ postSnippetsButton.onclick = function () {edInsertSnippet(edCanvas, insertString' . $i .', variables' . $i .', parseInt(this.title));}
161
+ postSnippetsButton.id = "ed_ps' . $i .'";
162
+ ';
163
+ } // End if
164
+ } // Next
165
  echo '
166
  }
167
+ function createShortcode(shortcodeTag, shortcodeAtts) {
168
+ theSnippet = \'[\' + shortcodeTag;
169
+ for (x in shortcodeAtts)
170
+ {
171
+ theSnippet += \' \' + shortcodeAtts[x] + \'="{\' + shortcodeAtts[x] + \'}"\';
172
+ }
173
+ theSnippet += \']\';
174
+ return theSnippet;
175
+ }
176
+
177
  function edInsertSnippet(myField,theSnippet,theVariables) {
178
  var myValue;
179
  var insertString;
226
  array_push($snippets, array (
227
  'title' => "Untitled",
228
  'vars' => "",
229
+ 'shortcode' => false,
230
+ 'quicktag' => false,
231
  'snippet' => ""));
232
  update_option($this->plugin_options, $snippets);
233
  $this->admin_message( __( 'A snippet named Untitled has been added.', 'post-snippets' ) );
240
  for ($i=0; $i < count($snippets); $i++) {
241
  $snippets[$i]['title'] = trim($_POST[$i.'_title']);
242
  $snippets[$i]['vars'] = trim($_POST[$i.'_vars']);
243
+ $snippets[$i]['shortcode'] = $_POST[$i.'_shortcode'] == true ? true : false;
244
+ $snippets[$i]['quicktag'] = $_POST[$i.'_quicktag'] == true ? true : false;
245
  $snippets[$i]['snippet'] = trim(stripslashes($_POST[$i.'_snippet']));
246
  }
247
  update_option($this->plugin_options, $snippets);
286
  <th scope="col" style="width: 180px;"><?php _e( 'Title', 'post-snippets' ) ?></th>
287
  <th scope="col" style="width: 180px;"><?php _e( 'Variables', 'post-snippets' ) ?></th>
288
  <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
289
+ <th scope="col" style="width: 16px;"><?php _e( 'SC', 'post-snippets' ) ?></th>
290
+ <th scope="col" style="width: 16px;"><?php _e( 'QT', 'post-snippets' ) ?></th>
291
  </tr>
292
  </thead>
293
 
297
  <th scope="col"><?php _e( 'Title', 'post-snippets' ) ?></th>
298
  <th scope="col"><?php _e( 'Variables', 'post-snippets' ) ?></th>
299
  <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
300
+ <th scope="col"><?php _e( 'SC', 'post-snippets' ) ?></th>
301
+ <th scope="col"><?php _e( 'QT', 'post-snippets' ) ?></th>
302
  </tr>
303
  </tfoot>
304
 
312
  <td class='row-title'><input type='text' name='<?= $i ?>_title' value='<?= $snippets[$i]['title'] ?>' /></td>
313
  <td class='name'><input type='text' name='<?= $i ?>_vars' value='<?= $snippets[$i]['vars'] ?>' /></td>
314
  <td class='desc'><textarea name="<?= $i ?>_snippet" class="large-text" rows="3"><?= $snippets[$i]['snippet'] ?></textarea></td>
315
+ <td class='name'><input type='checkbox' name='<?= $i ?>_shortcode' value='true'<? if ($snippets[$i]['shortcode'] == true) { echo " checked"; }?> /></td>
316
+ <td class='name'><input type='checkbox' name='<?= $i ?>_quicktag' value='true'<? if ($snippets[$i]['quicktag'] == true) { echo " checked"; }?> /></td>
317
  </tr>
318
  <?php
319
  }
334
  <p><?php _e( '<strong>Variables</strong><br/>A comma separated list of custom variables you can reference in your snippet.<br/><br/>Example:<br/>url,name', 'post-snippets' ); ?></p>
335
 
336
  <p><?php _e( '<strong>Snippet</strong><br/>This is the block of text or HTML to insert in the post when you select the snippet from the insert button in the TinyMCE panel in the post editor. If you have entered predefined variables you can reference them from the snippet by enclosing them in {} brackets.<br/><br/>Example:<br/>To reference the variables in the example above, you would enter {url} and {name}.<br/><br/>So if you enter this snippet:<br/><i>This is the website of &lt;a href="{url}"&gt;{name}&lt;/a&gt;</i><br/>You will get the option to replace url and name on insert if they are defined as variables.', 'post-snippets' ); ?></p>
337
+
338
+ <p><?php _e( '<strong>SC</strong><br/>Treats the snippet as a shortcode. The name for the shortcode is the same as the title of the snippet (spaces not allowed) and will be used on insert.', 'post-snippets' ); ?></p>
339
+
340
+ <p><?php _e( '<strong>QT</strong><br/>Enables the snippet to be available as a quicktag in the HTML editor.', 'post-snippets' ); ?></p>
341
  </div>
342
  </div>
343
  </div>
readme.txt CHANGED
@@ -4,13 +4,13 @@ Donate link: http://coding.cglounge.com/wordpress-plugins/post-snippets/#pintwar
4
  Tags: post, admin, snippet, html, custom, page, dynamic, editor, quicktag
5
  Requires at least: 2.7
6
  Tested up to: 2.7.1
7
- Stable tag: 1.3.5
8
 
9
  Store snippets of HTML code or reoccurring text that you often use in your posts. Custom variables can be used.
10
 
11
  == Description ==
12
 
13
- This admin plugin stores snippets of HTML code or reoccurring text that you often use in your posts. You can use predefined variables to replace parts of the snippet on insert. All snippets are available in the post editor with a TinyMCE button.
14
 
15
  For complete usage instructions see: [Post Snippets](http://coding.cglounge.com/wordpress-plugins/post-snippets/ "Complete Usage Instructions for Post Snippets")
16
 
@@ -19,6 +19,7 @@ See the [Changelog](http://wordpress.org/extend/plugins/post-snippets/other_note
19
  Some features:
20
 
21
  * **Insert** All defined snippets is inserted from a button directly in the post editor.
 
22
  * **Buttons** The snippets are available in the viusal editor with a TinyMCE button and in the HTML editor with quicktag buttons.
23
  * **Admin** Easy to use administration panel where you can add, edit and remove snippets.
24
  * **Variables** Each snippet can have as many custom variables as you like, which can be used on insert.
@@ -46,6 +47,10 @@ Meditation.
46
 
47
  == Changelog ==
48
 
 
 
 
 
49
  = Version 1.3.5 - 9 Apr 2009 =
50
  * Fixed so the TinyMCE window adds a scrollbar if there is more variables for a snippet than fits in the window.
51
  * Fixed a bug that snippets didn't get inserted when using the visual editor in fullscreen mode.
4
  Tags: post, admin, snippet, html, custom, page, dynamic, editor, quicktag
5
  Requires at least: 2.7
6
  Tested up to: 2.7.1
7
+ Stable tag: 1.4
8
 
9
  Store snippets of HTML code or reoccurring text that you often use in your posts. Custom variables can be used.
10
 
11
  == Description ==
12
 
13
+ This admin plugin stores snippets of HTML code or reoccurring text that you often use in your posts. You can use predefined variables to replace parts of the snippet on insert. All snippets are available in the post editor with a TinyMCE button. The snippet can be inserted as defined, or as a shortcode to keep flexibility for updating the snippet.
14
 
15
  For complete usage instructions see: [Post Snippets](http://coding.cglounge.com/wordpress-plugins/post-snippets/ "Complete Usage Instructions for Post Snippets")
16
 
19
  Some features:
20
 
21
  * **Insert** All defined snippets is inserted from a button directly in the post editor.
22
+ * **Shortcodes** You can use this plugin to create your own shortcodes.
23
  * **Buttons** The snippets are available in the viusal editor with a TinyMCE button and in the HTML editor with quicktag buttons.
24
  * **Admin** Easy to use administration panel where you can add, edit and remove snippets.
25
  * **Variables** Each snippet can have as many custom variables as you like, which can be used on insert.
47
 
48
  == Changelog ==
49
 
50
+ = Version 1.4 - 10 Apr 2009 =
51
+ * Added a checkbox for Shortcodes (SC) in the admin panel. When checking this one a dynamic shortcode will be generated and inserted instead of the snippet, which allows snippets to be updated later on for all posts it's been inserted into when using this option.
52
+ * Added a checkbox for Quicktags (QT) in the admin panel, so Quicktags are optional. Speeds up loading of the post editor if you don't need the quicktag support, and only use the visual editor. Defaults to off.
53
+
54
  = Version 1.3.5 - 9 Apr 2009 =
55
  * Fixed so the TinyMCE window adds a scrollbar if there is more variables for a snippet than fits in the window.
56
  * Fixed a bug that snippets didn't get inserted when using the visual editor in fullscreen mode.
tinymce/window.php CHANGED
@@ -22,8 +22,21 @@ if ( !is_user_logged_in() || !current_user_can('edit_posts') )
22
  tinyMCEPopup.resizeToInnerSize();
23
  }
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  function insertSnippet() {
26
-
27
  var insertString;
28
 
29
  <?php
@@ -42,7 +55,23 @@ if ( !is_user_logged_in() || !current_user_can('edit_posts') )
42
  ?>
43
 
44
  if (panel<?= $i ?>.className.indexOf('current') != -1) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  insertString = "<?= $theString; ?>";
 
46
  <?php
47
  $var_arr = explode(",",$snippets[$i]['vars']);
48
  if (!empty($var_arr[0])) {
22
  tinyMCEPopup.resizeToInnerSize();
23
  }
24
 
25
+ function createSnippet(theSnippet) {
26
+ }
27
+
28
+ function createShortcode(shortcodeTag, shortcodeAtts) {
29
+ theSnippet = "[" + shortcodeTag;
30
+ for (x in shortcodeAtts)
31
+ {
32
+ theSnippet += ' ' + shortcodeAtts[x] + '="{' + shortcodeAtts[x] + '}"';
33
+ }
34
+ theSnippet += "]";
35
+ return theSnippet;
36
+ }
37
+
38
  function insertSnippet() {
39
+
40
  var insertString;
41
 
42
  <?php
55
  ?>
56
 
57
  if (panel<?= $i ?>.className.indexOf('current') != -1) {
58
+ <?php
59
+ if ($snippets[$i]['shortcode']) {
60
+ $var_arr = explode(",",$snippets[$i]['vars']);
61
+ $theVariables = "";
62
+ if (!empty($var_arr[0])) {
63
+ for ($j = 0; $j < count($var_arr); $j++) {
64
+ $theVariables = $theVariables . "'" . $var_arr[$j] . "'";
65
+ if ( $j < (count($var_arr) -1) )
66
+ $theVariables = $theVariables . ", ";
67
+
68
+ }
69
+ }
70
+ echo "var variables" . $i ." = new Array(".$theVariables.");"; ?>
71
+ insertString = createShortcode("<?= $snippets[$i]['title']; ?>", variables<?= $i; ?>);
72
+ <?php }else{ ?>
73
  insertString = "<?= $theString; ?>";
74
+ <?php } ?>
75
  <?php
76
  $var_arr = explode(",",$snippets[$i]['vars']);
77
  if (!empty($var_arr[0])) {