Post Snippets - Version 1.7

Version Description

Download this release

Release Info

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

Code changes from version 1.5.4 to 1.7

Files changed (2) hide show
  1. post-snippets.php +313 -9
  2. readme.txt +9 -3
post-snippets.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Post Snippets
4
  Plugin URI: http://wpstorm.net/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.5.4
7
  Author: Johan Steen
8
  Author URI: http://wpstorm.net/
9
  Text Domain: post-snippets
@@ -43,11 +43,11 @@ class post_snippets {
43
 
44
  // Check installed Wordpress version.
45
  global $wp_version;
46
- if ( version_compare($wp_version, '2.7', '>=') ) {
 
 
47
  include_once (dirname (__FILE__)."/tinymce/tinymce.php");
48
  $this->init_hooks();
49
- } else {
50
- $this->version_warning();
51
  }
52
  }
53
 
@@ -57,19 +57,292 @@ class post_snippets {
57
  * @returns Nothing
58
  */
59
  function init_hooks() {
60
- add_action('admin_menu', array(&$this,'wp_admin'));
61
- add_action('admin_head', array(&$this,'quicktags'));
62
- // add_action('admin_footer', array(&$this,'quicktags'));
 
 
 
 
 
 
 
63
  $this->create_shortcodes();
 
 
 
 
 
 
 
 
 
 
64
  }
65
-
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  /**
67
  * Displays a warning when installed in an old Wordpress Version
68
  *
69
  * @returns Nothing
70
  */
71
  function version_warning() {
72
- echo '<div class="updated fade"><p><strong>'.__('Post Snippets requires WordPress version 2.7 or later!', 'post-snippets').'</strong></p></div>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  }
74
 
75
 
@@ -110,6 +383,7 @@ class post_snippets {
110
  /**
111
  * Handling of QuickTags in the HTML editor
112
  *
 
113
  */
114
  function quicktags() {
115
  $snippets = get_option($this->plugin_options);
@@ -409,4 +683,34 @@ JAVASCRIPT;
409
  }
410
 
411
  add_action( 'plugins_loaded', create_function( '', 'global $post_snippets; $post_snippets = new post_snippets();' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412
  ?>
3
  Plugin Name: Post Snippets
4
  Plugin URI: http://wpstorm.net/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.7
7
  Author: Johan Steen
8
  Author URI: http://wpstorm.net/
9
  Text Domain: post-snippets
43
 
44
  // Check installed Wordpress version.
45
  global $wp_version;
46
+ if ( version_compare($wp_version, '2.7', '<') ) {
47
+ add_action( 'admin_notices', array(&$this, 'version_warning') );
48
+ } else {
49
  include_once (dirname (__FILE__)."/tinymce/tinymce.php");
50
  $this->init_hooks();
 
 
51
  }
52
  }
53
 
57
  * @returns Nothing
58
  */
59
  function init_hooks() {
60
+ # Settings link on plugins list
61
+ add_filter( 'plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
62
+ # Options Page
63
+ add_action( 'admin_menu', array(&$this,'wp_admin') );
64
+
65
+ // This old methos is no longer used, DELETE when the new method is tried by users.
66
+ // Add Quicktags to header (used to be in footer!)
67
+ // add_action('admin_head', array(&$this,'quicktags'));
68
+ // add_action('admin_footer', array(&$this,'quicktags'));
69
+
70
  $this->create_shortcodes();
71
+
72
+ # Adds the JS and HTML code in the header and footer for the jQuery insert UI dialog in the editor
73
+ add_action( 'admin_init', array(&$this,'enqueue_scripts') );
74
+ add_action( 'admin_head', array(&$this,'jquery_ui_dialog_style') );
75
+ add_action( 'admin_head', array(&$this,'jquery_ui_dialog') );
76
+ add_action( 'admin_footer', array(&$this,'insert_ui_dialog') );
77
+
78
+ # Add Editor QuickTag button
79
+ add_action( 'edit_form_advanced', array(&$this,'add_quicktag_button'));
80
+ add_action( 'edit_page_form', array(&$this,'add_quicktag_button'));
81
  }
82
+
83
+
84
+ /**
85
+ * Quick link to the Post Snippets Settings page from the Plugins page.
86
+ *
87
+ * @returns Array with all the plugin's action links
88
+ */
89
+ function plugin_action_links( $links, $file ) {
90
+ if ( $file == plugin_basename( dirname(__FILE__).'/post-snippets.php' ) ) {
91
+ $links[] = '<a href="options-general.php?page=post-snippets/post-snippets.php">'.__('Settings', 'post-snippets').'</a>';
92
+ }
93
+ return $links;
94
+ }
95
+
96
  /**
97
  * Displays a warning when installed in an old Wordpress Version
98
  *
99
  * @returns Nothing
100
  */
101
  function version_warning() {
102
+ echo '<div class="updated fade"><p><strong>'.__('Post Snippets requires WordPress version 2.7 or higher.', 'post-snippets').'</strong></p></div>';
103
+ }
104
+
105
+ /**
106
+ * Enqueues the necessary scripts for the insert dialogue
107
+ *
108
+ * @since Post Snippets 1.7
109
+ *
110
+ * @returns Nothing
111
+ */
112
+ function enqueue_scripts() {
113
+ wp_enqueue_script( 'jquery-ui-dialog' );
114
+ wp_enqueue_script( 'jquery-ui-tabs' );
115
+ }
116
+
117
+
118
+ /**
119
+ * CSS Styling for the jQuert Dialog
120
+ *
121
+ * @since Post Snippets 1.7
122
+ *
123
+ * @returns Nothing
124
+ */
125
+ function jquery_ui_dialog_style() {
126
+ ?>
127
+ <style type="text/css">
128
+ #post-snippets-tabs {
129
+ padding: 15px 15px 3px;
130
+ background-color: #f1f1f1;
131
+ border-bottom: 1px solid #dfdfdf;
132
+ }
133
+ #post-snippets-tabs li {
134
+ display: inline;
135
+ }
136
+ #post-snippets-tabs a.current {
137
+ background-color: #fff;
138
+ border-color: #dfdfdf;
139
+ border-bottom-color: #fff;
140
+ color: #d54e21;
141
+ }
142
+ #post-snippets-tabs a {
143
+ color: #2583AD;
144
+ padding: 6px;
145
+ border-width: 1px 1px 0;
146
+ border-style: solid solid none;
147
+ border-color: #f1f1f1;
148
+ text-decoration: none;
149
+ }
150
+ #post-snippets-tabs a:hover {
151
+ color: #d54e21;
152
+ }
153
+ </style>
154
+ <?php
155
+ }
156
+
157
+ /**
158
+ * jQuery control for the dialog and Javascript needed to insert snippets into the editor
159
+ *
160
+ * @since Post Snippets 1.7
161
+ *
162
+ * @returns Nothing
163
+ */
164
+ function jquery_ui_dialog() {
165
+ echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
166
+ echo "<script>\n";
167
+
168
+ # Prepare the snippets and shortcodes into javascript variables
169
+ # so they can be inserted into the editor, and get the variables replaced
170
+ # with user defined strings.
171
+ $snippets = get_option($this->plugin_options);
172
+ for ($i = 0; $i < count($snippets); $i++) {
173
+ if ($snippets[$i]['shortcode']) {
174
+ # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
175
+ # so {varnameX} can be replaced at runtime.
176
+ $var_arr = explode(",",$snippets[$i]['vars']);
177
+ $variables = '';
178
+ if (!empty($var_arr[0])) {
179
+ for ($j = 0; $j < count($var_arr); $j++) {
180
+ $variables .= ' ' . $var_arr[$j] . '="{' . $var_arr[$j] . '}"';
181
+ }
182
+ }
183
+ $shortcode = $snippets[$i]['title'] . $variables;
184
+ echo "var postsnippet_{$i} = '[" . $shortcode . "]';\n";
185
+ } else {
186
+ $theSnippet = $snippets[$i]['snippet'];
187
+ $theSnippet = str_replace('"','\"',str_replace(chr(13), '', str_replace(chr(10), '%%LF%%', $theSnippet)));
188
+ echo "var postsnippet_{$i} = \"" . $theSnippet . "\";\n";
189
+ //echo "var postsnippet_{$i} = '" . esc_js( $snippets[$i]['snippet'] ) . "';\n";
190
+ }
191
+ }
192
+ ?>
193
+
194
+ jQuery(document).ready(function($){
195
+ <?php
196
+ # Create js variables for all form fields
197
+ for ($i = 0; $i < count($snippets); $i++) {
198
+ $var_arr = explode(",",$snippets[$i]['vars']);
199
+ if (!empty($var_arr[0])) {
200
+ for ($j = 0; $j < count($var_arr); $j++) {
201
+ $varname = "var_" . $i . "_" . $j;
202
+ echo "var {$varname} = $( \"#{$varname}\" );\n";
203
+ }
204
+ }
205
+ }
206
+ ?>
207
+
208
+ var $tabs = $("#post-snippets-tabs").tabs();
209
+
210
+ $(function() {
211
+ $( "#post-snippets-dialog" ).dialog({
212
+ autoOpen: false,
213
+ modal: true,
214
+ dialogClass: 'wp-dialog',
215
+ buttons: {
216
+ Cancel: function() {
217
+ $( this ).dialog( "close" );
218
+ },
219
+ "Insert": function() {
220
+ $( this ).dialog( "close" );
221
+ var selected = $tabs.tabs('option', 'selected');
222
+ <?php
223
+ for ($i = 0; $i < count($snippets); $i++) {
224
+ ?>
225
+ if (selected == <?php echo $i; ?>) {
226
+ insert_snippet = postsnippet_<?php echo $i; ?>;
227
+ <?php
228
+ $var_arr = explode(",",$snippets[$i]['vars']);
229
+ if (!empty($var_arr[0])) {
230
+ for ($j = 0; $j < count($var_arr); $j++) {
231
+ $varname = "var_" . $i . "_" . $j; ?>
232
+ insert_snippet = insert_snippet.replace(/\{<?php echo $var_arr[$j]; ?>\}/g, <?php echo $varname; ?>.val());
233
+ <?php
234
+ echo "\n";
235
+ }
236
+ }
237
+ ?>
238
+ }
239
+ <?php
240
+ }
241
+ ?>
242
+ edInsertContent(muppCanv, insert_snippet);
243
+ }
244
+ },
245
+ width: 500,
246
+ });
247
+ });
248
+ });
249
+
250
+
251
+ var muppCanv;
252
+
253
+
254
+ function edOpenPostSnippets(myField) {
255
+ muppCanv = myField;
256
+ jQuery( "#post-snippets-dialog" ).dialog( "open" );
257
+ };
258
+
259
+
260
+ <?php
261
+ echo "</script>\n";
262
+ echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
263
+ }
264
+
265
+
266
+
267
+ /**
268
+ * jQuery Insert Dialog for the editor
269
+ *
270
+ * @since Post Snippets 1.7
271
+ *
272
+ * @returns Nothing
273
+ */
274
+ function insert_ui_dialog() {
275
+ echo "\n<!-- START: Post Snippets UI Dialog -->\n";
276
+ ?>
277
+ <div class="hidden">
278
+ <div id="post-snippets-dialog" title="Post Snippets">
279
+
280
+ <div id="post-snippets-tabs">
281
+ <ul>
282
+ <?php
283
+ $snippets = get_option($this->plugin_options);
284
+ for ($i = 0; $i < count($snippets); $i++) { ?>
285
+ <li><a href="#ps-tabs-<?php echo $i; ?>"><?php echo $snippets[$i]['title']; ?></a></li>
286
+ <?php } ?>
287
+ </ul>
288
+
289
+ <?php
290
+ for ($i = 0; $i < count($snippets); $i++) { ?>
291
+ <div id="ps-tabs-<?php echo $i; ?>">
292
+ <h4><?php echo $snippets[$i]['title']; ?></h4>
293
+ <!--<p>Snippet Description</p>-->
294
+
295
+
296
+ <?php
297
+ $var_arr = explode(",",$snippets[$i]['vars']);
298
+ if (!empty($var_arr[0])) {
299
+ for ($j = 0; $j < count($var_arr); $j++) { ?>
300
+ <label for="var_<?php echo $i; ?>_<?php echo $j; ?>"><?php echo($var_arr[$j]);?>:</label>
301
+ <input type="text" id="var_<?php echo $i; ?>_<?php echo $j; ?>" name="var_<?php echo $i; ?>_<?php echo $j; ?>" style="width: 190px" />
302
+ <br/>
303
+
304
+ <?php } } ?>
305
+
306
+
307
+ </div>
308
+ <?php } ?>
309
+ </div>
310
+
311
+ </div>
312
+ </div>
313
+ <?
314
+ echo "\n<!-- END: Post Snippets UI Dialog -->\n";
315
+ }
316
+
317
+
318
+ /**
319
+ * Adds a QuickTag button to the HTML editor
320
+ *
321
+ * @see wp-includes/js/quicktags.dev.js
322
+ * @since Post Snippets 1.7
323
+ *
324
+ * @returns Nothing
325
+ */
326
+ function add_quicktag_button() {
327
+ echo "\n<!-- START: Post Snippets QuickTag button -->\n";
328
+ ?>
329
+ <script type="text/javascript" charset="utf-8">
330
+ // <![CDATA[
331
+ //edButton(id, display, tagStart, tagEnd, access, open)
332
+ edbuttonlength = edButtons.length;
333
+ edButtons[edbuttonlength++] = new edButton('ed_postsnippets', 'Post Snippets', '', '', '', -1);
334
+ (function(){
335
+ if (typeof jQuery === 'undefined') {
336
+ return;
337
+ }
338
+ jQuery(document).ready(function(){
339
+ jQuery("#ed_toolbar").append('<input type="button" value="Post Snippets" id="ed_postsnippets" class="ed_button" onclick="edOpenPostSnippets(edCanvas);" title="Post Snippets" />');
340
+ });
341
+ }());
342
+ // ]]>
343
+ </script>
344
+ <?php
345
+ echo "\n<!-- END: Post Snippets QuickTag button -->\n";
346
  }
347
 
348
 
383
  /**
384
  * Handling of QuickTags in the HTML editor
385
  *
386
+ * DELETE THIS PART WHEN THE NEW QUICKTAGS FUNCTION HAVE BEEN TRIED BY USERS.
387
  */
388
  function quicktags() {
389
  $snippets = get_option($this->plugin_options);
683
  }
684
 
685
  add_action( 'plugins_loaded', create_function( '', 'global $post_snippets; $post_snippets = new post_snippets();' ) );
686
+
687
+
688
+ /**
689
+ * Allow snippets to be retrieved directly from PHP
690
+ *
691
+ * @since Post Snippets 1.6
692
+ *
693
+ * @param string $snippet_name The name of the snippet to retrieve
694
+ * @param string $snippet_vars The variables to pass to the snippet, formatted as a query string.
695
+ * @returns string The Snippet
696
+ */
697
+ function get_post_snippet( $snippet_name, $snippet_vars ) {
698
+ global $post_snippets;
699
+ $snippets = get_option($post_snippets -> plugin_options);
700
+ for ($i = 0; $i < count($snippets); $i++) {
701
+ if ($snippets[$i]['title'] == $snippet_name) {
702
+ parse_str( htmlspecialchars_decode($snippet_vars), $snippet_output );
703
+ $snippet = $snippets[$i]['snippet'];
704
+ $var_arr = explode(",",$snippets[$i]['vars']);
705
+
706
+ if ( !empty($var_arr[0]) ) {
707
+ for ($j = 0; $j < count($var_arr); $j++) {
708
+ $snippet = str_replace("{".$var_arr[$j]."}", $snippet_output[$var_arr[$j]], $snippet);
709
+ }
710
+ }
711
+ }
712
+ }
713
+ return $snippet;
714
+ }
715
+
716
  ?>
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
- === Post Snippets ===
2
  Contributors: artstorm
3
  Donate link: http://wpstorm.net/wordpress-plugins/post-snippets/#donation
4
  Tags: post, admin, snippet, html, custom, page, dynamic, editor, quicktag
5
  Requires at least: 2.7
6
- Tested up to: 3.0.1
7
- Stable tag: 1.5.4
8
 
9
  Store snippets of HTML code or reoccurring text that you often use in your posts. Custom variables can be used.
10
 
@@ -52,6 +52,12 @@ Please visit [Post Snippets' Comments](http://wpstorm.net/wordpress-plugins/post
52
 
53
  == Changelog ==
54
 
 
 
 
 
 
 
55
  = Version 1.5.4 - 26 Jan 2011 =
56
  * Included Turkish translation by [Ersan Özdil](http://www.tml.web.tr/).
57
 
1
+ === Post Snippets ===
2
  Contributors: artstorm
3
  Donate link: http://wpstorm.net/wordpress-plugins/post-snippets/#donation
4
  Tags: post, admin, snippet, html, custom, page, dynamic, editor, quicktag
5
  Requires at least: 2.7
6
+ Tested up to: 3.1
7
+ Stable tag: 1.7
8
 
9
  Store snippets of HTML code or reoccurring text that you often use in your posts. Custom variables can be used.
10
 
52
 
53
  == Changelog ==
54
 
55
+ = Version 1.7 - 26 Feb 2011 =
56
+ * Complete rewrite of the QuickTags insert functionality. It now uses jQuery UI to display a similar tabbed window as the TinyMCE button does. There is now one 'Post Snippets' button in the HTML editor instead of a separate button for each snippet. As the QuickTags function is completely rewritten, and this is the initial release of the new method, please report if you encounter any problems with it.
57
+ * Fixed QuickTags compability with WordPress 3.1.
58
+ * Added a link to the Post Snippets Settings directly from the entry on the 'Plugins List' page.
59
+ * Added get_post_snippet() function to retrieve snippets directly from PHP.
60
+
61
  = Version 1.5.4 - 26 Jan 2011 =
62
  * Included Turkish translation by [Ersan Özdil](http://www.tml.web.tr/).
63