Post Snippets - Version 2.0

Version Description

Download this release

Release Info

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

Code changes from version 1.9.7 to 2.0

assets/post-snippets.css CHANGED
@@ -67,3 +67,9 @@
67
  #post-snippets-tabs .ui-tabs-hide {
68
  display: none !important;
69
  }
 
 
 
 
 
 
67
  #post-snippets-tabs .ui-tabs-hide {
68
  display: none !important;
69
  }
70
+
71
+
72
+ #post-snippets-dialog label {
73
+ width: 60px;
74
+ display: inline-block;
75
+ }
classes/help.php CHANGED
@@ -1,267 +1,267 @@
1
- <?php
2
- /**
3
- * Post Snippets Help.
4
- *
5
- * Class to handle the help texts and tabs on the settings screen.
6
- *
7
- * @package Post Snippets
8
- * @author Johan Steen <artstorm at gmail dot com>
9
- * @since Post Snippets 1.8.9
10
- */
11
- class Post_Snippets_Help
12
- {
13
- /**
14
- * Constructor.
15
- *
16
- * @since Post Snippets 1.8.9
17
- * @param string The option page to load the help text on
18
- */
19
- public function __construct( $option_page )
20
- {
21
- // If WordPress is 3.3 or higher, use the new Help API, otherwise call
22
- // the old contextual help action.
23
- global $wp_version;
24
- if ( version_compare($wp_version, '3.3', '>=') ) {
25
- add_action( 'load-' . $option_page, array(&$this,'add_help_tabs') );
26
- } else {
27
- add_action( 'contextual_help', array(&$this,'add_help'), 10, 3 );
28
- }
29
- }
30
-
31
- /**
32
- * Setup the help tabs and sidebar.
33
- *
34
- * @since Post Snippets 1.8.9
35
- */
36
- public function add_help_tabs() {
37
- $screen = get_current_screen();
38
- $screen->set_help_sidebar( $this->help_sidebar() );
39
- $screen->add_help_tab( array(
40
- 'id' => 'basic-plugin-help',
41
- 'title' => __( 'Basic', 'post-snippets' ),
42
- 'content' => $this->help_basic()
43
- ) );
44
- $screen->add_help_tab( array(
45
- 'id' => 'shortcode-plugin-help',
46
- 'title' => __( 'Shortcode', 'post-snippets' ),
47
- 'content' => $this->help_shortcode()
48
- ) );
49
- $screen->add_help_tab( array(
50
- 'id' => 'php-plugin-help',
51
- 'title' => __( 'PHP', 'post-snippets' ),
52
- 'content' => $this->help_php()
53
- ) );
54
- $screen->add_help_tab( array(
55
- 'id' => 'advanced-plugin-help',
56
- 'title' => __( 'Advanced', 'post-snippets' ),
57
- 'content' => $this->help_advanced()
58
- ) );
59
- }
60
-
61
- /**
62
- * The right sidebar help text.
63
- *
64
- * @since Post Snippets 1.8.9
65
- * @return string The help text
66
- */
67
- public function help_sidebar()
68
- {
69
- return '<p><strong>'.
70
- __( 'For more information:', 'post-snippets' ).
71
- '</strong></p>
72
-
73
- <p><a href="http://wpstorm.net/wordpress-plugins/post-snippets/" target="_blank">'.
74
- __( 'Post Snippets Documentation', 'post-snippets' ).
75
- '</a></p>
76
-
77
- <p><a href="http://wordpress.org/tags/post-snippets?forum_id=10" target="_blank">'.
78
- __( 'Support Forums', 'post-snippets' ).
79
- '</a></p>';
80
- }
81
-
82
- /**
83
- * The basic help tab.
84
- *
85
- * @since Post Snippets 1.9.1
86
- * @return string The help text
87
- */
88
- public function help_basic()
89
- {
90
- return '<h2>'.
91
- __( 'Title', 'post-snippets' ).
92
- '</h2>
93
- <p>'.
94
- __( 'Give the snippet a title that helps you identify it in the post editor. This also becomes the name of the shortcode if you enable that option', 'post-snippets' ).
95
- '</p>
96
-
97
- <h2>'.
98
- __( 'Variables', 'post-snippets' ).
99
- '</h2>
100
- <p>'.
101
- __( 'A comma separated list of custom variables you can reference in your snippet. A variable can also be assigned a default value that will be used in the insert window by using the equal sign, variable=default.', 'post-snippets' ).
102
- '</p>
103
- <p><strong>'.
104
- __( 'Example', 'post-snippets' ).
105
- '</strong><br/>
106
- <code>url,name,role=user,title</code></p>'.
107
-
108
- '<h2>'.
109
- __( 'Snippet', 'post-snippets' ).
110
- '</h2>
111
- <p>'.
112
- __('This is the block of text, HTML or PHP to insert in the post or as a shortcode. If you have entered predefined variables you can reference them from the snippet by enclosing them in {} brackets.', 'post-snippets' ).
113
- '</p>
114
- <p><strong>'.
115
- __( 'Example', 'post-snippets' ).
116
- '</strong><br/>'.
117
- __( 'To reference the variables in the example above, you would enter {url} and {name}. So if you enter this snippet:', 'post-snippets' ).
118
- '<br/>
119
- <code>This is the website of &lt;a href="{url}"&gt;{name}&lt;/a&gt;</code>
120
- <br/>'.
121
- __( 'You will get the option to replace url and name on insert if they are defined as variables.', 'post-snippets').
122
- '</p>
123
-
124
- <h2>'
125
- . __( 'Description', 'post-snippets' ).
126
- '</h2>
127
- <p>'.
128
- __( 'An optional description for the Snippet. If filled out, the description will be displayed in the snippets insert window in the post editor.', 'post-snippets').
129
- '</p>';
130
- }
131
-
132
- /**
133
- * The shortcode help tab.
134
- *
135
- * @since Post Snippets 1.9.1
136
- * @return string The help text
137
- */
138
- public function help_shortcode()
139
- {
140
- return '<p>'.
141
- __( 'When enabling the shortcode checkbox, the snippet is no longer inserted directly but instead inserted as a shortcode. The obvious advantage of this is of course that you can insert a block of text or code in many places on the site, and update the content from one single place.', 'post-snippets' ).
142
- '</p>
143
-
144
- <p>'.
145
- __( 'The name to use the shortcode is the same as the title of the snippet (spaces are not allowed). When inserting a shortcode snippet, the shortcode and not the content will be inserted in the post.', 'post-snippets' ).
146
- '</p>
147
- <p>'.
148
- __( 'If you enclose the shortcode in your posts, you can access the enclosed content by using the variable {content} in your snippet. The {content} variable is reserved, so don\'t use it in the variables field.', 'post-snippets' ).
149
- '</p>
150
-
151
- <h2>'
152
- . __( 'Options', 'post-snippets' ).
153
- '</h2>
154
- <p><strong>PHP</strong><br/>'.
155
- __( 'See the dedicated help section for information about PHP shortcodes.', 'post-snippets' ).
156
- '</p>
157
- <p><strong>wptexturize</strong><br/>'.
158
- sprintf(__( 'Before the shortcode is outputted, it can optionally be formatted with %s, to transform quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol.', 'post-snippets' ), '<a href="http://codex.wordpress.org/Function_Reference/wptexturize">wptexturize</a>' ).
159
- '</p>';
160
- }
161
-
162
- /**
163
- * The PHP help tab.
164
- *
165
- * @since Post Snippets 1.9.1
166
- * @return string The help text
167
- */
168
- public function help_php()
169
- {
170
- return '<p>'.
171
- __('Snippets defined as shortcodes can optionally also be evaluated as PHP Code by enabling the PHP checkbox. PHP snippets is only available when treating the snippet as a shortcode.', 'post-snippets' ).
172
- '</p>
173
- <p><strong>'.
174
- __( 'Example PHP Snippet', 'post-snippets' ).
175
- '</strong><br/>
176
- <code>
177
- for ($i=1; $i<5; $i++) {<br/>
178
- echo "{loop_me}&lt;br/&gt;";<br/>
179
- };
180
- </code></p>
181
-
182
- <p>'.
183
- __( 'With a snippet defined like the one above, you can call it with its shortcode definition in a post. Let\'s pretend that the example snippet is named phpcode and have one variable defined loop_me, then it would be called like this from a post:' , 'post-snippets' ).
184
- '</p>
185
-
186
- <code>[phpcode loop_me="post snippet with PHP!"]</code>
187
-
188
- <p>'.
189
- __( 'When the shortcode is executed the loop_me variable will be replaced with the string supplied in the shortcode and then the PHP code will be evaluated. (Outputting the string five times in this case. Wow!)', 'post-snippets' ).
190
- '</p>
191
- <p>'.
192
- __( 'Note the evaluation order, any snippet variables will be replaced before the snippet is evaluated as PHP code. Also note that a PHP snippet don\'t need to be wrapped in &lt;?php #code; ?&gt;.', 'post-snippets' ).
193
- '</p>';
194
- }
195
-
196
- /**
197
- * The advanced help tab.
198
- *
199
- * @since Post Snippets 1.9.1
200
- * @return string The help text
201
- */
202
- public function help_advanced()
203
- {
204
- return '<p>'.
205
- __('You can retrieve a Post Snippet directly from PHP, in a theme for instance, by using the get_post_snippet() function.', 'post-snippets').
206
- '</p>
207
-
208
- <h2>'.
209
- __('Usage', 'post-snippets').
210
- '</h2>
211
- <p>'.
212
- '<code>
213
- &lt;?php $my_snippet = get_post_snippet( $snippet_name, $snippet_vars ); ?&gt;
214
- </code></p>
215
-
216
- <h2>'.
217
- __('Parameters', 'post-snippets').
218
- '</h2>
219
- <p>
220
- <code>$snippet_name</code><br/>'.
221
- __('(string) (required) The name of the snippet to retrieve.', 'post-snippets').
222
-
223
- '<br/><br/><code>'.
224
- '$snippet_vars
225
- </code><br/>'.
226
- __('(string) The variables to pass to the snippet, formatted as a query string.', 'post-snippets').
227
- '</p>
228
-
229
- <h2>'.
230
- __('Example', 'post-snippets').
231
- '</h2>
232
- <p><code>
233
- &lt;?php<br/>
234
- $my_snippet = get_post_snippet( \'internal-link\', \'title=Awesome&url=2011/02/awesome/\' );<br/>
235
- echo $my_snippet;<br/>
236
- ?&gt;
237
- </code></p>';
238
- }
239
-
240
-
241
- // -------------------------------------------------------------------------
242
- // For compability with WordPress before v3.3.
243
- // -------------------------------------------------------------------------
244
-
245
- /**
246
- * Contextual Help for WP < v3.3.
247
- *
248
- * Combines the help tabs above into one long help text for the help tab
249
- * when run on WordPress versions before v3.3.
250
- *
251
- * @since Post Snippets 1.7.1
252
- * @return string The Contextual Help
253
- */
254
- public function add_help($contextual_help, $screen_id, $screen) {
255
- if ( $screen->id == 'settings_page_post-snippets/post-snippets' ) {
256
- $contextual_help = '<h1>'.__( 'Basic', 'post-snippets' ).'</h1>';
257
- $contextual_help .= $this->help_basic();
258
- $contextual_help .= '<h1>'.__( 'Shortcode', 'post-snippets' ).'</h1>';
259
- $contextual_help .= $this->help_shortcode();
260
- $contextual_help .= '<h1>'.__( 'PHP', 'post-snippets' ).'</h1>';
261
- $contextual_help .= $this->help_php();
262
- $contextual_help .= '<h1>'.__( 'Advanced', 'post-snippets' ).'</h1>';
263
- $contextual_help .= $this->help_advanced();
264
- }
265
- return $contextual_help;
266
- }
267
- }
1
+ <?php
2
+ /**
3
+ * Post Snippets Help.
4
+ *
5
+ * Class to handle the help texts and tabs on the settings screen.
6
+ *
7
+ * @package Post Snippets
8
+ * @author Johan Steen <artstorm at gmail dot com>
9
+ * @since Post Snippets 1.8.9
10
+ */
11
+ class Post_Snippets_Help extends Post_Snippets_Base
12
+ {
13
+ /**
14
+ * Constructor.
15
+ *
16
+ * @since Post Snippets 1.8.9
17
+ * @param string The option page to load the help text on
18
+ */
19
+ public function __construct( $option_page )
20
+ {
21
+ // If WordPress is 3.3 or higher, use the new Help API, otherwise call
22
+ // the old contextual help action.
23
+ global $wp_version;
24
+ if ( version_compare($wp_version, '3.3', '>=') ) {
25
+ add_action( 'load-' . $option_page, array(&$this,'add_help_tabs') );
26
+ } else {
27
+ add_action( 'contextual_help', array(&$this,'add_help'), 10, 3 );
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Setup the help tabs and sidebar.
33
+ *
34
+ * @since Post Snippets 1.8.9
35
+ */
36
+ public function add_help_tabs() {
37
+ $screen = get_current_screen();
38
+ $screen->set_help_sidebar( $this->help_sidebar() );
39
+ $screen->add_help_tab( array(
40
+ 'id' => 'basic-plugin-help',
41
+ 'title' => __( 'Basic', 'post-snippets' ),
42
+ 'content' => $this->help_basic()
43
+ ) );
44
+ $screen->add_help_tab( array(
45
+ 'id' => 'shortcode-plugin-help',
46
+ 'title' => __( 'Shortcode', 'post-snippets' ),
47
+ 'content' => $this->help_shortcode()
48
+ ) );
49
+ $screen->add_help_tab( array(
50
+ 'id' => 'php-plugin-help',
51
+ 'title' => __( 'PHP', 'post-snippets' ),
52
+ 'content' => $this->help_php()
53
+ ) );
54
+ $screen->add_help_tab( array(
55
+ 'id' => 'advanced-plugin-help',
56
+ 'title' => __( 'Advanced', 'post-snippets' ),
57
+ 'content' => $this->help_advanced()
58
+ ) );
59
+ }
60
+
61
+ /**
62
+ * The right sidebar help text.
63
+ *
64
+ * @since Post Snippets 1.8.9
65
+ * @return string The help text
66
+ */
67
+ public function help_sidebar()
68
+ {
69
+ return '<p><strong>'.
70
+ __( 'For more information:', 'post-snippets' ).
71
+ '</strong></p>
72
+
73
+ <p><a href="http://wpstorm.net/wordpress-plugins/post-snippets/" target="_blank">'.
74
+ __( 'Post Snippets Documentation', 'post-snippets' ).
75
+ '</a></p>
76
+
77
+ <p><a href="http://wordpress.org/tags/post-snippets?forum_id=10" target="_blank">'.
78
+ __( 'Support Forums', 'post-snippets' ).
79
+ '</a></p>';
80
+ }
81
+
82
+ /**
83
+ * The basic help tab.
84
+ *
85
+ * @since Post Snippets 1.9.1
86
+ * @return string The help text
87
+ */
88
+ public function help_basic()
89
+ {
90
+ return '<h2>'.
91
+ __( 'Title', 'post-snippets' ).
92
+ '</h2>
93
+ <p>'.
94
+ __( 'Give the snippet a title that helps you identify it in the post editor. This also becomes the name of the shortcode if you enable that option', 'post-snippets' ).
95
+ '</p>
96
+
97
+ <h2>'.
98
+ __( 'Variables', 'post-snippets' ).
99
+ '</h2>
100
+ <p>'.
101
+ __( 'A comma separated list of custom variables you can reference in your snippet. A variable can also be assigned a default value that will be used in the insert window by using the equal sign, variable=default.', 'post-snippets' ).
102
+ '</p>
103
+ <p><strong>'.
104
+ __( 'Example', 'post-snippets' ).
105
+ '</strong><br/>
106
+ <code>url,name,role=user,title</code></p>'.
107
+
108
+ '<h2>'.
109
+ __( 'Snippet', 'post-snippets' ).
110
+ '</h2>
111
+ <p>'.
112
+ __('This is the block of text, HTML or PHP to insert in the post or as a shortcode. If you have entered predefined variables you can reference them from the snippet by enclosing them in {} brackets.', 'post-snippets' ).
113
+ '</p>
114
+ <p><strong>'.
115
+ __( 'Example', 'post-snippets' ).
116
+ '</strong><br/>'.
117
+ __( 'To reference the variables in the example above, you would enter {url} and {name}. So if you enter this snippet:', 'post-snippets' ).
118
+ '<br/>
119
+ <code>This is the website of &lt;a href="{url}"&gt;{name}&lt;/a&gt;</code>
120
+ <br/>'.
121
+ __( 'You will get the option to replace url and name on insert if they are defined as variables.', 'post-snippets').
122
+ '</p>
123
+
124
+ <h2>'
125
+ . __( 'Description', 'post-snippets' ).
126
+ '</h2>
127
+ <p>'.
128
+ __( 'An optional description for the Snippet. If filled out, the description will be displayed in the snippets insert window in the post editor.', 'post-snippets').
129
+ '</p>';
130
+ }
131
+
132
+ /**
133
+ * The shortcode help tab.
134
+ *
135
+ * @since Post Snippets 1.9.1
136
+ * @return string The help text
137
+ */
138
+ public function help_shortcode()
139
+ {
140
+ return '<p>'.
141
+ __( 'When enabling the shortcode checkbox, the snippet is no longer inserted directly but instead inserted as a shortcode. The obvious advantage of this is of course that you can insert a block of text or code in many places on the site, and update the content from one single place.', 'post-snippets' ).
142
+ '</p>
143
+
144
+ <p>'.
145
+ __( 'The name to use the shortcode is the same as the title of the snippet (spaces are not allowed). When inserting a shortcode snippet, the shortcode and not the content will be inserted in the post.', 'post-snippets' ).
146
+ '</p>
147
+ <p>'.
148
+ __( 'If you enclose the shortcode in your posts, you can access the enclosed content by using the variable {content} in your snippet. The {content} variable is reserved, so don\'t use it in the variables field.', 'post-snippets' ).
149
+ '</p>
150
+
151
+ <h2>'
152
+ . __( 'Options', 'post-snippets' ).
153
+ '</h2>
154
+ <p><strong>PHP</strong><br/>'.
155
+ __( 'See the dedicated help section for information about PHP shortcodes.', 'post-snippets' ).
156
+ '</p>
157
+ <p><strong>wptexturize</strong><br/>'.
158
+ sprintf(__( 'Before the shortcode is outputted, it can optionally be formatted with %s, to transform quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol.', 'post-snippets' ), '<a href="http://codex.wordpress.org/Function_Reference/wptexturize">wptexturize</a>' ).
159
+ '</p>';
160
+ }
161
+
162
+ /**
163
+ * The PHP help tab.
164
+ *
165
+ * @since Post Snippets 1.9.1
166
+ * @return string The help text
167
+ */
168
+ public function help_php()
169
+ {
170
+ return '<p>'.
171
+ __('Snippets defined as shortcodes can optionally also be evaluated as PHP Code by enabling the PHP checkbox. PHP snippets is only available when treating the snippet as a shortcode.', 'post-snippets' ).
172
+ '</p>
173
+ <p><strong>'.
174
+ __( 'Example PHP Snippet', 'post-snippets' ).
175
+ '</strong><br/>
176
+ <code>
177
+ for ($i=1; $i<5; $i++) {<br/>
178
+ echo "{loop_me}&lt;br/&gt;";<br/>
179
+ };
180
+ </code></p>
181
+
182
+ <p>'.
183
+ __( 'With a snippet defined like the one above, you can call it with its shortcode definition in a post. Let\'s pretend that the example snippet is named phpcode and have one variable defined loop_me, then it would be called like this from a post:' , 'post-snippets' ).
184
+ '</p>
185
+
186
+ <code>[phpcode loop_me="post snippet with PHP!"]</code>
187
+
188
+ <p>'.
189
+ __( 'When the shortcode is executed the loop_me variable will be replaced with the string supplied in the shortcode and then the PHP code will be evaluated. (Outputting the string five times in this case. Wow!)', 'post-snippets' ).
190
+ '</p>
191
+ <p>'.
192
+ __( 'Note the evaluation order, any snippet variables will be replaced before the snippet is evaluated as PHP code. Also note that a PHP snippet don\'t need to be wrapped in &lt;?php #code; ?&gt;.', 'post-snippets' ).
193
+ '</p>';
194
+ }
195
+
196
+ /**
197
+ * The advanced help tab.
198
+ *
199
+ * @since Post Snippets 1.9.1
200
+ * @return string The help text
201
+ */
202
+ public function help_advanced()
203
+ {
204
+ return '<p>'.
205
+ __('You can retrieve a Post Snippet directly from PHP, in a theme for instance, by using the get_post_snippet() function.', 'post-snippets').
206
+ '</p>
207
+
208
+ <h2>'.
209
+ __('Usage', 'post-snippets').
210
+ '</h2>
211
+ <p>'.
212
+ '<code>
213
+ &lt;?php $my_snippet = get_post_snippet( $snippet_name, $snippet_vars ); ?&gt;
214
+ </code></p>
215
+
216
+ <h2>'.
217
+ __('Parameters', 'post-snippets').
218
+ '</h2>
219
+ <p>
220
+ <code>$snippet_name</code><br/>'.
221
+ __('(string) (required) The name of the snippet to retrieve.', 'post-snippets').
222
+
223
+ '<br/><br/><code>'.
224
+ '$snippet_vars
225
+ </code><br/>'.
226
+ __('(string) The variables to pass to the snippet, formatted as a query string.', 'post-snippets').
227
+ '</p>
228
+
229
+ <h2>'.
230
+ __('Example', 'post-snippets').
231
+ '</h2>
232
+ <p><code>
233
+ &lt;?php<br/>
234
+ $my_snippet = get_post_snippet( \'internal-link\', \'title=Awesome&url=2011/02/awesome/\' );<br/>
235
+ echo $my_snippet;<br/>
236
+ ?&gt;
237
+ </code></p>';
238
+ }
239
+
240
+
241
+ // -------------------------------------------------------------------------
242
+ // For compability with WordPress before v3.3.
243
+ // -------------------------------------------------------------------------
244
+
245
+ /**
246
+ * Contextual Help for WP < v3.3.
247
+ *
248
+ * Combines the help tabs above into one long help text for the help tab
249
+ * when run on WordPress versions before v3.3.
250
+ *
251
+ * @since Post Snippets 1.7.1
252
+ * @return string The Contextual Help
253
+ */
254
+ public function add_help($contextual_help, $screen_id, $screen) {
255
+ if ( $screen->id == 'settings_page_post-snippets/post-snippets' ) {
256
+ $contextual_help = '<h1>'.__( 'Basic', 'post-snippets' ).'</h1>';
257
+ $contextual_help .= $this->help_basic();
258
+ $contextual_help .= '<h1>'.__( 'Shortcode', 'post-snippets' ).'</h1>';
259
+ $contextual_help .= $this->help_shortcode();
260
+ $contextual_help .= '<h1>'.__( 'PHP', 'post-snippets' ).'</h1>';
261
+ $contextual_help .= $this->help_php();
262
+ $contextual_help .= '<h1>'.__( 'Advanced', 'post-snippets' ).'</h1>';
263
+ $contextual_help .= $this->help_advanced();
264
+ }
265
+ return $contextual_help;
266
+ }
267
+ }
classes/import-export.php ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Post Snippets I/O.
4
+ *
5
+ * Class to handle import and export of Snippets.
6
+ *
7
+ * @package Post Snippets
8
+ * @author Johan Steen <artstorm at gmail dot com>
9
+ * @since Post Snippets 2.0
10
+ */
11
+ class Post_Snippets_ImportExport extends Post_Snippets_Base
12
+ {
13
+ /**
14
+ * Export Snippets.
15
+ *
16
+ * Check if an export file shall be created, or if a download url should be
17
+ * pushed to the footer. Also checks for old export files laying around and
18
+ * deletes them (for security).
19
+ *
20
+ * @since Post Snippets 1.8
21
+ */
22
+ public function export_snippets() {
23
+ if ( isset($_POST['postsnippets_export']) ) {
24
+ $url = $this->create_export_file();
25
+ if ($url) {
26
+ define('PSURL', $url);
27
+ function psnippets_footer() {
28
+ $export = '<script type="text/javascript">
29
+ document.location = \''.PSURL.'\';
30
+ </script>';
31
+ echo $export;
32
+ }
33
+ add_action('admin_footer', 'psnippets_footer', 10000);
34
+
35
+ } else {
36
+ $export .= 'Error: '.$url;
37
+ }
38
+ } else {
39
+ // Check if there is any old export files to delete
40
+ $dir = wp_upload_dir();
41
+ $upload_dir = $dir['basedir'] . '/';
42
+ chdir($upload_dir);
43
+ if (file_exists ( './post-snippets-export.zip' ) )
44
+ unlink('./post-snippets-export.zip');
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Create a zipped filed containing all Post Snippets, for export.
50
+ *
51
+ * @since Post Snippets 1.8
52
+ * @return string URL to the exported snippets
53
+ */
54
+ private function create_export_file() {
55
+ $snippets = serialize(get_option( self::PLUGIN_OPTION_KEY ));
56
+ $snippets = apply_filters( 'post_snippets_export', $snippets );
57
+ $dir = wp_upload_dir();
58
+ $upload_dir = $dir['basedir'] . '/';
59
+ $upload_url = $dir['baseurl'] . '/';
60
+
61
+ // Open a file stream and write the serialized options to it.
62
+ if ( !$handle = fopen( $upload_dir.'post-snippets-export.cfg', 'w' ) )
63
+ die();
64
+ if ( !fwrite($handle, $snippets) )
65
+ die();
66
+ fclose($handle);
67
+
68
+ // Create a zip archive
69
+ require_once (ABSPATH . 'wp-admin/includes/class-pclzip.php');
70
+ chdir($upload_dir);
71
+ $zip = new PclZip('./post-snippets-export.zip');
72
+ $zipped = $zip->create('./post-snippets-export.cfg');
73
+
74
+ // Delete the snippet file
75
+ unlink('./post-snippets-export.cfg');
76
+
77
+ if (!$zipped)
78
+ return false;
79
+
80
+ return $upload_url.'post-snippets-export.zip';
81
+ }
82
+
83
+ /**
84
+ * Handles uploading of post snippets archive and import the snippets.
85
+ *
86
+ * @uses wp_handle_upload() in wp-admin/includes/file.php
87
+ * @since Post Snippets 1.8
88
+ * @return string HTML to handle the import
89
+ */
90
+ public function import_snippets() {
91
+ $import = '<br/><br/><strong>'.__( 'Import', 'post-snippets' ).'</strong><br/>';
92
+ if ( !isset($_FILES['postsnippets_import_file']) || empty($_FILES['postsnippets_import_file']) ) {
93
+ $import .= '<p>'.__( 'Import snippets from a post-snippets-export.zip file. Importing overwrites any existing snippets.', 'post-snippets' ).'</p>';
94
+ $import .= '<form method="post" enctype="multipart/form-data">';
95
+ $import .= '<input type="file" name="postsnippets_import_file"/>';
96
+ $import .= '<input type="hidden" name="action" value="wp_handle_upload"/>';
97
+ $import .= '<input type="submit" class="button" value="'.__( 'Import Snippets', 'post-snippets' ).'"/>';
98
+ $import .= '</form>';
99
+ } else {
100
+ $file = wp_handle_upload( $_FILES['postsnippets_import_file'] );
101
+
102
+ if ( isset( $file['file'] ) && !is_wp_error($file) ) {
103
+ require_once (ABSPATH . 'wp-admin/includes/class-pclzip.php');
104
+ $zip = new PclZip( $file['file'] );
105
+ $dir = wp_upload_dir();
106
+ $upload_dir = $dir['basedir'] . '/';
107
+ chdir($upload_dir);
108
+ $unzipped = $zip->extract();
109
+
110
+ if ( $unzipped[0]['stored_filename'] == 'post-snippets-export.cfg' && $unzipped[0]['status'] == 'ok') {
111
+ // Delete the uploaded archive
112
+ unlink($file['file']);
113
+
114
+ $snippets = file_get_contents( $upload_dir.'post-snippets-export.cfg' ); // Returns false on failure, else the contents
115
+ if ($snippets) {
116
+ $snippets = apply_filters( 'post_snippets_import', $snippets );
117
+ update_option( self::PLUGIN_OPTION_KEY, unserialize($snippets));
118
+ }
119
+
120
+ // Delete the snippet file
121
+ unlink('./post-snippets-export.cfg');
122
+
123
+ $import .= '<p><strong>'.__( 'Snippets successfully imported.').'</strong></p>';
124
+ } else {
125
+ $import .= '<p><strong>'.__( 'Snippets could not be imported:').' '.__('Unzipping failed.').'</strong></p>';
126
+ }
127
+ } else {
128
+ if ( $file['error'] || is_wp_error( $file ) )
129
+ $import .= '<p><strong>'.__( 'Snippets could not be imported:').' '.$file['error'].'</strong></p>';
130
+ else
131
+ $import .= '<p><strong>'.__( 'Snippets could not be imported:').' '.__('Upload failed.').'</strong></p>';
132
+ }
133
+ }
134
+ return $import;
135
+ }
136
+ }
classes/settings.php CHANGED
@@ -1,281 +1,427 @@
1
- <?php
2
- /**
3
- * Post Snippets Settings.
4
- *
5
- * Class that renders out the HTML for the settings screen and contains helpful
6
- * methods to simply the maintainance of the admin screen.
7
- *
8
- * @package Post Snippets
9
- * @author Johan Steen <artstorm at gmail dot com>
10
- * @since Post Snippets 1.8.8
11
- */
12
- class Post_Snippets_Settings
13
- {
14
- // Constants
15
- const PLUGIN_OPTION_KEY = 'post_snippets_options';
16
- const USER_OPTION_KEY = 'post_snippets';
17
-
18
-
19
- // -------------------------------------------------------------------------
20
- // Handle form I/O
21
- // -------------------------------------------------------------------------
22
-
23
- /**
24
- * Update User Option.
25
- *
26
- * Sets the per user option for the read-only overview page.
27
- *
28
- * @since Post Snippets 1.9.7
29
- */
30
- private function set_user_options()
31
- {
32
- if ( isset($_POST['post_snippets_user_nonce']) && wp_verify_nonce( $_POST['post_snippets_user_nonce'], 'post_snippets_user_options') )
33
- {
34
- $id = get_current_user_id();
35
- $render = isset( $_POST['render'] ) ? true : false;
36
- update_user_meta( $id, self::USER_OPTION_KEY, $render );
37
- }
38
- }
39
-
40
- /**
41
- * Get User Option.
42
- *
43
- * Gets the per user option for the read-only overview page.
44
- *
45
- * @since Post Snippets 1.9.7
46
- * @return boolean If overview should be rendered on output or not
47
- */
48
- private function get_user_options()
49
- {
50
- $id = get_current_user_id();
51
- $options = get_user_meta( $id, self::USER_OPTION_KEY, true );
52
- return $options;
53
- }
54
-
55
-
56
- // -------------------------------------------------------------------------
57
- // HTML generation for option pages
58
- // -------------------------------------------------------------------------
59
-
60
- /**
61
- * Render the options page.
62
- *
63
- * @since Post Snippets 1.9.7
64
- * @param string $page Admin page to render. Default: options
65
- */
66
- public function render( $page )
67
- {
68
- switch ( $page ) {
69
- case 'options':
70
- $this->options_page();
71
- break;
72
-
73
- default:
74
- $this->overview_page();
75
- break;
76
- }
77
- }
78
-
79
- /**
80
- * Creates the snippets administration page.
81
- *
82
- * For users with manage_options capability (admin, super admin).
83
- *
84
- * @since Post Snippets 1.8.8
85
- */
86
- private function options_page()
87
- {
88
- ?>
89
- <div class=wrap>
90
- <h2>Post Snippets</h2>
91
-
92
- <form method="post" action="">
93
- <?php wp_nonce_field('update-options'); ?>
94
-
95
- <div class="tablenav">
96
- <div class="alignleft actions">
97
- <input type="submit" name="add-snippet" value="<?php _e( 'Add New Snippet', 'post-snippets' ) ?>" class="button-secondary" />
98
- <input type="submit" name="delete-selected" value="<?php _e( 'Delete Selected', 'post-snippets' ) ?>" class="button-secondary" />
99
- <span class="description"><?php _e( '(Use the help dropdown button above for additional information.)', 'post-snippets' ); ?></span>
100
- </div>
101
- </div>
102
- <div class="clear"></div>
103
-
104
- <table class="widefat fixed" cellspacing="0">
105
- <thead>
106
- <tr>
107
- <th scope="col" class="check-column"><input type="checkbox" /></th>
108
- <th scope="col" style="width: 180px;"><?php _e( 'Title', 'post-snippets' ) ?></th>
109
- <th scope="col" style="width: 180px;"><?php _e( 'Variables', 'post-snippets' ) ?></th>
110
- <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
111
- </tr>
112
- </thead>
113
-
114
- <tfoot>
115
- <tr>
116
- <th scope="col" class="check-column"><input type="checkbox" /></th>
117
- <th scope="col"><?php _e( 'Title', 'post-snippets' ) ?></th>
118
- <th scope="col"><?php _e( 'Variables', 'post-snippets' ) ?></th>
119
- <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
120
- </tr>
121
- </tfoot>
122
-
123
- <tbody>
124
- <?php
125
- $snippets = get_option( self::PLUGIN_OPTION_KEY );
126
- if (!empty($snippets)) {
127
- foreach ($snippets as $key => $snippet) {
128
- ?>
129
- <tr class='recent'>
130
- <th scope='row' class='check-column'><input type='checkbox' name='checked[]' value='<?php echo $key; ?>' /></th>
131
- <td class='row-title'>
132
- <input type='text' name='<?php echo $key; ?>_title' value='<?php echo $snippet['title']; ?>' />
133
- </td>
134
- <td class='name'>
135
- <input type='text' name='<?php echo $key; ?>_vars' value='<?php echo $snippet['vars']; ?>' />
136
- <br/>
137
- <br/>
138
- <?php
139
- $this->checkbox(__('Shortcode', 'post-snippets'), $key.'_shortcode',
140
- $snippet['shortcode']);
141
-
142
- echo '<br/><strong>Shortcode Options:</strong><br/>';
143
-
144
- $this->checkbox(__('PHP Code', 'post-snippets'), $key.'_php',
145
- $snippet['php']);
146
-
147
- $wptexturize = isset( $snippet['wptexturize'] ) ? $snippet['wptexturize'] : false;
148
- $this->checkbox('wptexturize', $key.'_wptexturize', $wptexturize);
149
- ?>
150
- </td>
151
- <td class='desc'>
152
- <textarea name="<?php echo $key; ?>_snippet" class="large-text" style='width: 100%;' rows="5"><?php echo htmlspecialchars($snippet['snippet'], ENT_NOQUOTES); ?></textarea>
153
- <?php _e( 'Description', 'post-snippets' ) ?>:
154
- <input type='text' style='width: 100%;' name='<?php echo $key; ?>_description' value='<?php if (isset( $snippet['description'] ) ) echo esc_html($snippet['description']); ?>' /><br/>
155
- </td>
156
- </tr>
157
- <?php
158
- }
159
- }
160
- ?>
161
- </tbody>
162
- </table>
163
-
164
- <?php
165
- $this->submit( 'update-post-snippets', __('Update Snippets', 'post-snippets') );
166
- echo '</form>';
167
- // ---
168
-
169
- echo '</div>';
170
- }
171
-
172
- /**
173
- * Creates a read-only overview page.
174
- *
175
- * For users with edit_posts capability but without manage_options
176
- * capability.
177
- *
178
- * @since Post Snippets 1.9.7
179
- */
180
- private function overview_page()
181
- {
182
- // Header
183
- echo '<div class=wrap>';
184
- echo '<h2>Post Snippets</h2>';
185
- echo '<p>';
186
- _e( 'This is an overview of all snippets defined for this site. These snippets are inserted into posts from the post editor using the Post Snippets button. You can choose to see the snippets here as-is or as they are actually rendered on the website. Enabling rendered snippets for this overview might look strange if the snippet have dependencies on variables, CSS or other parameters only available on the frontend. If that is the case it is recommended to keep this option disabled.', 'post-snippets' );
187
- echo '</p>';
188
-
189
- // Form
190
- $this->set_user_options();
191
- $render = $this->get_user_options();
192
-
193
- echo '<form method="post" action="">';
194
- wp_nonce_field( 'post_snippets_user_options', 'post_snippets_user_nonce' );
195
-
196
- $this->checkbox(__('Display rendered snippets', 'post-snippets'), 'render', $render );
197
- $this->submit( 'update-post-snippets-user', __('Update', 'post-snippets') );
198
- echo '</form>';
199
-
200
- // Snippet List
201
- $snippets = get_option( self::PLUGIN_OPTION_KEY );
202
- if (!empty($snippets)) {
203
- foreach ($snippets as $key => $snippet) {
204
-
205
- echo "<hr style='border: none;border-top:1px dashed #aaa; margin:24px 0;' />";
206
-
207
- echo "<h3>{$snippet['title']}";
208
- if ($snippet['description'])
209
- echo "<span class='description'> {$snippet['description']}</span>";
210
- echo "</h3>";
211
-
212
- if ($snippet['vars'])
213
- printf( "<strong>%s:</strong> {$snippet['vars']}<br/>", __('Variables', 'post-snippets') );
214
-
215
- // echo "<strong>Variables:</strong> {$snippet['vars']}<br/>";
216
-
217
- $options = array();
218
- if ($snippet['shortcode'])
219
- array_push($options, 'Shortcode');
220
- if ($snippet['php'])
221
- array_push($options, 'PHP');
222
- if ($snippet['wptexturize'])
223
- array_push($options, 'wptexturize');
224
- if ($options)
225
- printf ( "<strong>%s:</strong> %s<br/>", __('Options', 'post-snippets'), implode(', ', $options) );
226
-
227
- printf( "<br/><strong>%s:</strong><br/>", __('Snippet', 'post-snippets') );
228
- if ( $render ) {
229
- echo do_shortcode( $snippet['snippet'] );
230
- } else {
231
- echo "<code>";
232
- echo nl2br( htmlspecialchars($snippet['snippet'], ENT_NOQUOTES) );
233
- echo "</code>";
234
- }
235
- }
236
- }
237
-
238
- // Close
239
- echo '</div>';
240
- }
241
-
242
-
243
- // -------------------------------------------------------------------------
244
- // HTML and Form element methods
245
- // -------------------------------------------------------------------------
246
-
247
- /**
248
- * Checkbox.
249
- *
250
- * Renders the HTML for an input checkbox.
251
- *
252
- * @param string $label The label rendered to screen
253
- * @param string $name The unique name and id to identify the input
254
- * @param boolean $checked If the input is checked or not
255
- */
256
- private function checkbox( $label, $name, $checked )
257
- {
258
- echo "<label for=\"{$name}\">";
259
- printf( '<input type="checkbox" name="%1$s" id="%1$s" value="true"', $name );
260
- if ($checked)
261
- echo ' checked';
262
- echo ' />';
263
- echo " {$label}</label><br/>";
264
- }
265
-
266
- /**
267
- * Submit.
268
- *
269
- * Renders the HTML for a submit button.
270
- *
271
- * @since Post Snippets 1.9.7
272
- * @param string $name The name that identifies the button on submit
273
- * @param string $label The label rendered on the button
274
- */
275
- private function submit( $name, $label )
276
- {
277
- echo '<div class="submit">';
278
- printf( '<input type="submit" name="%s" value="%s" class="button-primary" />', $name, $label );
279
- echo '</div>';
280
- }
281
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Post Snippets Settings.
4
+ *
5
+ * Class that renders out the HTML for the settings screen and contains helpful
6
+ * methods to simply the maintainance of the admin screen.
7
+ *
8
+ * @package Post Snippets
9
+ * @author Johan Steen <artstorm at gmail dot com>
10
+ * @since Post Snippets 1.8.8
11
+ */
12
+ class Post_Snippets_Settings extends Post_Snippets_Base
13
+ {
14
+ // -------------------------------------------------------------------------
15
+ // Handle form submissions
16
+ // -------------------------------------------------------------------------
17
+
18
+ /**
19
+ * Add New Snippet.
20
+ */
21
+ private function add()
22
+ {
23
+ if (isset( $_POST['add-snippet'] )
24
+ && isset( $_POST['update_snippets_nonce'])
25
+ && wp_verify_nonce( $_POST['update_snippets_nonce'], 'update_snippets') )
26
+ {
27
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
28
+ if (empty($snippets)) { $snippets = array(); }
29
+
30
+ array_push($snippets, array (
31
+ 'title' => 'Untitled',
32
+ 'vars' => '',
33
+ 'description' => '',
34
+ 'shortcode' => false,
35
+ 'php' => false,
36
+ 'wptexturize' => false,
37
+ 'snippet' => '')
38
+ );
39
+
40
+ update_option( self::PLUGIN_OPTION_KEY, $snippets );
41
+ $this->message( __( 'A snippet named Untitled has been added.', 'post-snippets' ) );
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Delete Snippet/s.
47
+ */
48
+ private function delete()
49
+ {
50
+ if (isset( $_POST['delete-snippets'] )
51
+ && isset( $_POST['update_snippets_nonce'])
52
+ && wp_verify_nonce( $_POST['update_snippets_nonce'], 'update_snippets') )
53
+ {
54
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
55
+
56
+ if ( empty($snippets) || !isset($_POST['checked']) ) {
57
+ $this->message( __( 'Nothing selected to delete.', 'post-snippets' ) );
58
+ return;
59
+ }
60
+
61
+ $delete = $_POST['checked'];
62
+ $newsnippets = array();
63
+ foreach ($snippets as $key => $snippet) {
64
+ if (in_array($key,$delete) == false) {
65
+ array_push($newsnippets,$snippet);
66
+ }
67
+ }
68
+
69
+ update_option( self::PLUGIN_OPTION_KEY, $newsnippets );
70
+ $this->message( __( 'Selected snippets have been deleted.', 'post-snippets' ) );
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Update Snippet/s.
76
+ */
77
+ private function update()
78
+ {
79
+ if (isset( $_POST['update-snippets'] )
80
+ && isset( $_POST['update_snippets_nonce'])
81
+ && wp_verify_nonce( $_POST['update_snippets_nonce'], 'update_snippets') )
82
+ {
83
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
84
+ if (!empty($snippets)) {
85
+ foreach ($snippets as $key => $value) {
86
+ $new_snippets[$key]['title'] = trim($_POST[$key.'_title']);
87
+ $new_snippets[$key]['vars'] = str_replace(' ', '', trim($_POST[$key.'_vars']) );
88
+ $new_snippets[$key]['shortcode'] = isset($_POST[$key.'_shortcode']) ? true : false;
89
+ $new_snippets[$key]['php'] = isset($_POST[$key.'_php']) ? true : false;
90
+ $new_snippets[$key]['wptexturize'] = isset($_POST[$key.'_wptexturize']) ? true : false;
91
+
92
+ $new_snippets[$key]['snippet'] = wp_specialchars_decode( trim(stripslashes($_POST[$key.'_snippet'])), ENT_NOQUOTES);
93
+ $new_snippets[$key]['description'] = wp_specialchars_decode( trim(stripslashes($_POST[$key.'_description'])), ENT_NOQUOTES);
94
+ }
95
+ update_option( self::PLUGIN_OPTION_KEY, $new_snippets );
96
+ $this->message( __( 'Snippets have been updated.', 'post-snippets' ) );
97
+ }
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Update User Option.
103
+ *
104
+ * Sets the per user option for the read-only overview page.
105
+ *
106
+ * @since Post Snippets 1.9.7
107
+ */
108
+ private function set_user_options()
109
+ {
110
+ if ( isset($_POST['post_snippets_user_nonce']) && wp_verify_nonce( $_POST['post_snippets_user_nonce'], 'post_snippets_user_options') )
111
+ {
112
+ $id = get_current_user_id();
113
+ $render = isset( $_POST['render'] ) ? true : false;
114
+ update_user_meta( $id, self::USER_OPTION_KEY, $render );
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Get User Option.
120
+ *
121
+ * Gets the per user option for the read-only overview page.
122
+ *
123
+ * @since Post Snippets 1.9.7
124
+ * @return boolean If overview should be rendered on output or not
125
+ */
126
+ private function get_user_options()
127
+ {
128
+ $id = get_current_user_id();
129
+ $options = get_user_meta( $id, self::USER_OPTION_KEY, true );
130
+ return $options;
131
+ }
132
+
133
+
134
+ // -------------------------------------------------------------------------
135
+ // HTML generation for option pages
136
+ // -------------------------------------------------------------------------
137
+
138
+ /**
139
+ * Render the options page.
140
+ *
141
+ * @since Post Snippets 1.9.7
142
+ * @param string $page Admin page to render. Default: options
143
+ */
144
+ public function render( $page )
145
+ {
146
+ switch ( $page ) {
147
+ case 'options':
148
+ $this->options_page();
149
+ break;
150
+
151
+ default:
152
+ $this->overview_page();
153
+ break;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Display Flashing Message.
159
+ *
160
+ * @param string $message Message to display to the user.
161
+ */
162
+ private function message( $message )
163
+ {
164
+ if ( $message )
165
+ echo "<div class='updated'><p><strong>{$message}</strong></p></div>";
166
+ }
167
+
168
+ /**
169
+ * Creates the snippets administration page.
170
+ *
171
+ * For users with manage_options capability (admin, super admin).
172
+ *
173
+ * @since Post Snippets 1.8.8
174
+ */
175
+ private function options_page()
176
+ {
177
+ // Handle Form Submits
178
+ $this->add();
179
+ $this->delete();
180
+ $this->update();
181
+
182
+ // Header
183
+ echo '<div class="wrap">';
184
+ echo '<h2>Post Snippets</h2>';
185
+
186
+ // Tabs
187
+ $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'snippets';
188
+ $base_url = '?page=post-snippets/post-snippets.php&amp;tab=';
189
+ $tabs = array( 'snippets' => __( 'Manage Snippets', 'post-snippets' ), 'tools' => __( 'Import/Export', 'post-snippets' ) );
190
+ echo '<h2 class="nav-tab-wrapper">';
191
+ foreach ( $tabs as $tab => $title ) {
192
+ $active = ( $active_tab == $tab ) ? ' nav-tab-active' : '';
193
+ echo "<a href='{$base_url}{$tab}' class='nav-tab {$active}'>{$title}</a>";
194
+ }
195
+ echo '</h2>';
196
+ echo '<p class="description">';
197
+ _e( 'Use the help dropdown button for additional information.', 'post-snippets' );
198
+ echo '</p>';
199
+
200
+ // Tab content
201
+ if( $active_tab == 'snippets' )
202
+ $this->tab_snippets();
203
+ else
204
+ $this->tab_tools();
205
+
206
+ // Close it
207
+ echo '</div>';
208
+ }
209
+
210
+ /**
211
+ * Tab to Manage Snippets.
212
+ *
213
+ * @since Post Snippets 2.0
214
+ */
215
+ private function tab_snippets()
216
+ {
217
+ echo '<form method="post" action="">';
218
+ wp_nonce_field( 'update_snippets', 'update_snippets_nonce' );
219
+ ?>
220
+
221
+ <table class="widefat fixed" cellspacing="0">
222
+ <thead>
223
+ <tr>
224
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
225
+ <th scope="col" style="width: 180px;"><?php _e( 'Title', 'post-snippets' ) ?></th>
226
+ <th scope="col" style="width: 180px;"><?php _e( 'Variables', 'post-snippets' ) ?></th>
227
+ <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
228
+ </tr>
229
+ </thead>
230
+
231
+ <tfoot>
232
+ <tr>
233
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
234
+ <th scope="col"><?php _e( 'Title', 'post-snippets' ) ?></th>
235
+ <th scope="col"><?php _e( 'Variables', 'post-snippets' ) ?></th>
236
+ <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
237
+ </tr>
238
+ </tfoot>
239
+
240
+ <tbody>
241
+ <?php
242
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
243
+ if (!empty($snippets)) {
244
+ foreach ($snippets as $key => $snippet) {
245
+ ?>
246
+ <tr class='recent'>
247
+ <th scope='row' class='check-column'><input type='checkbox' name='checked[]' value='<?php echo $key; ?>' /></th>
248
+ <td class='row-title'>
249
+ <input type='text' name='<?php echo $key; ?>_title' value='<?php echo $snippet['title']; ?>' />
250
+ </td>
251
+ <td class='name'>
252
+ <input type='text' name='<?php echo $key; ?>_vars' value='<?php echo $snippet['vars']; ?>' />
253
+ <br/>
254
+ <br/>
255
+ <?php
256
+ $this->checkbox(__('Shortcode', 'post-snippets'), $key.'_shortcode',
257
+ $snippet['shortcode']);
258
+
259
+ echo '<br/><strong>Shortcode Options:</strong><br/>';
260
+
261
+ $this->checkbox(__('PHP Code', 'post-snippets'), $key.'_php',
262
+ $snippet['php']);
263
+
264
+ $wptexturize = isset( $snippet['wptexturize'] ) ? $snippet['wptexturize'] : false;
265
+ $this->checkbox('wptexturize', $key.'_wptexturize', $wptexturize);
266
+ ?>
267
+ </td>
268
+ <td class='desc'>
269
+ <textarea name="<?php echo $key; ?>_snippet" class="large-text" style='width: 100%;' rows="5"><?php echo htmlspecialchars($snippet['snippet'], ENT_NOQUOTES); ?></textarea>
270
+ <?php _e( 'Description', 'post-snippets' ) ?>:
271
+ <input type='text' style='width: 100%;' name='<?php echo $key; ?>_description' value='<?php if (isset( $snippet['description'] ) ) echo esc_html($snippet['description']); ?>' /><br/>
272
+ </td>
273
+ </tr>
274
+ <?php
275
+ }
276
+ }
277
+ ?>
278
+ </tbody>
279
+ </table>
280
+
281
+ <?php
282
+ $this->submit( 'update-snippets', __('Update Snippets', 'post-snippets') );
283
+ $this->submit( 'add-snippet', __('Add New Snippet', 'post-snippets'), 'button-secondary', false );
284
+ $this->submit( 'delete-snippets', __('Delete Selected', 'post-snippets'), 'button-secondary', false );
285
+ echo '</form>';
286
+ }
287
+
288
+ /**
289
+ * Tab for Import/Export
290
+ *
291
+ * @since Post Snippets 2.0
292
+ */
293
+ private function tab_tools()
294
+ {
295
+ $ie = new Post_Snippets_ImportExport();
296
+
297
+ // Create header and export html form
298
+ printf( "<h3>%s</h3>", __( 'Import/Export', 'post-snippets' ));
299
+ printf( "<h4>%s</h4>", __( 'Export', 'post-snippets' ));
300
+ echo '<form method="post">';
301
+ echo '<p>';
302
+ _e( 'Export your snippets for backup or to import them on another site.', 'post-snippets' );
303
+ echo '</p>';
304
+ printf("<input type='submit' class='button' name='postsnippets_export' value='%s' />", __( 'Export Snippets', 'post-snippets') );
305
+ echo '</form>';
306
+
307
+ // Export logic, and import html form and logic
308
+ $ie->export_snippets();
309
+ echo $ie->import_snippets();
310
+ }
311
+
312
+
313
+ /**
314
+ * Creates a read-only overview page.
315
+ *
316
+ * For users with edit_posts capability but without manage_options
317
+ * capability.
318
+ *
319
+ * @since Post Snippets 1.9.7
320
+ */
321
+ private function overview_page()
322
+ {
323
+ // Header
324
+ echo '<div class="wrap">';
325
+ echo '<h2>Post Snippets</h2>';
326
+ echo '<p>';
327
+ _e( 'This is an overview of all snippets defined for this site. These snippets are inserted into posts from the post editor using the Post Snippets button. You can choose to see the snippets here as-is or as they are actually rendered on the website. Enabling rendered snippets for this overview might look strange if the snippet have dependencies on variables, CSS or other parameters only available on the frontend. If that is the case it is recommended to keep this option disabled.', 'post-snippets' );
328
+ echo '</p>';
329
+
330
+ // Form
331
+ $this->set_user_options();
332
+ $render = $this->get_user_options();
333
+
334
+ echo '<form method="post" action="">';
335
+ wp_nonce_field( 'post_snippets_user_options', 'post_snippets_user_nonce' );
336
+
337
+ $this->checkbox(__('Display rendered snippets', 'post-snippets'), 'render', $render );
338
+ $this->submit( 'update-post-snippets-user', __('Update', 'post-snippets') );
339
+ echo '</form>';
340
+
341
+ // Snippet List
342
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
343
+ if (!empty($snippets)) {
344
+ foreach ($snippets as $key => $snippet) {
345
+
346
+ echo "<hr style='border: none;border-top:1px dashed #aaa; margin:24px 0;' />";
347
+
348
+ echo "<h3>{$snippet['title']}";
349
+ if ($snippet['description'])
350
+ echo "<span class='description'> {$snippet['description']}</span>";
351
+ echo "</h3>";
352
+
353
+ if ($snippet['vars'])
354
+ printf( "<strong>%s:</strong> {$snippet['vars']}<br/>", __('Variables', 'post-snippets') );
355
+
356
+ // echo "<strong>Variables:</strong> {$snippet['vars']}<br/>";
357
+
358
+ $options = array();
359
+ if ($snippet['shortcode'])
360
+ array_push($options, 'Shortcode');
361
+ if ($snippet['php'])
362
+ array_push($options, 'PHP');
363
+ if ($snippet['wptexturize'])
364
+ array_push($options, 'wptexturize');
365
+ if ($options)
366
+ printf ( "<strong>%s:</strong> %s<br/>", __('Options', 'post-snippets'), implode(', ', $options) );
367
+
368
+ printf( "<br/><strong>%s:</strong><br/>", __('Snippet', 'post-snippets') );
369
+ if ( $render ) {
370
+ echo do_shortcode( $snippet['snippet'] );
371
+ } else {
372
+ echo "<code>";
373
+ echo nl2br( htmlspecialchars($snippet['snippet'], ENT_NOQUOTES) );
374
+ echo "</code>";
375
+ }
376
+ }
377
+ }
378
+
379
+ // Close
380
+ echo '</div>';
381
+ }
382
+
383
+
384
+ // -------------------------------------------------------------------------
385
+ // HTML and Form element methods
386
+ // -------------------------------------------------------------------------
387
+
388
+ /**
389
+ * Checkbox.
390
+ *
391
+ * Renders the HTML for an input checkbox.
392
+ *
393
+ * @param string $label The label rendered to screen
394
+ * @param string $name The unique name and id to identify the input
395
+ * @param boolean $checked If the input is checked or not
396
+ */
397
+ private function checkbox( $label, $name, $checked )
398
+ {
399
+ echo "<label for=\"{$name}\">";
400
+ printf( '<input type="checkbox" name="%1$s" id="%1$s" value="true"', $name );
401
+ if ($checked)
402
+ echo ' checked';
403
+ echo ' />';
404
+ echo " {$label}</label><br/>";
405
+ }
406
+
407
+ /**
408
+ * Submit.
409
+ *
410
+ * Renders the HTML for a submit button.
411
+ *
412
+ * @since Post Snippets 1.9.7
413
+ * @param string $name The name that identifies the button on submit
414
+ * @param string $label The label rendered on the button
415
+ * @param string $class Optional. Button class. Default: button-primary
416
+ * @param boolean $wrap Optional. Wrap in a submit div. Default: true
417
+ */
418
+ private function submit( $name, $label, $class='button-primary', $wrap=true )
419
+ {
420
+ $btn = sprintf( '<input type="submit" name="%s" value="%s" class="%s" />', $name, $label, $class );
421
+
422
+ if ($wrap)
423
+ $btn = "<div class=\"submit\">{$btn}</div>";
424
+
425
+ echo $btn;
426
+ }
427
+ }
languages/post-snippets.pot CHANGED
@@ -2,9 +2,9 @@
2
  # This file is distributed under the same license as the Post Snippets package.
3
  msgid ""
4
  msgstr ""
5
- "Project-Id-Version: Post Snippets 1.9.6\n"
6
  "Report-Msgid-Bugs-To: http://wordpress.org/tag/post-snippets\n"
7
- "POT-Creation-Date: 2012-03-21 09:28:13+00:00\n"
8
  "MIME-Version: 1.0\n"
9
  "Content-Type: text/plain; charset=UTF-8\n"
10
  "Content-Transfer-Encoding: 8bit\n"
@@ -16,7 +16,7 @@ msgstr ""
16
  msgid "Basic"
17
  msgstr ""
18
 
19
- #: classes/help.php:46 classes/help.php:258 classes/settings.php:146
20
  msgid "Shortcode"
21
  msgstr ""
22
 
@@ -40,7 +40,7 @@ msgstr ""
40
  msgid "Support Forums"
41
  msgstr ""
42
 
43
- #: classes/help.php:91 classes/settings.php:114 classes/settings.php:123
44
  msgid "Title"
45
  msgstr ""
46
 
@@ -50,8 +50,8 @@ msgid ""
50
  "also becomes the name of the shortcode if you enable that option"
51
  msgstr ""
52
 
53
- #: classes/help.php:98 classes/settings.php:115 classes/settings.php:124
54
- #: classes/settings.php:219
55
  msgid "Variables"
56
  msgstr ""
57
 
@@ -66,8 +66,8 @@ msgstr ""
66
  msgid "Example"
67
  msgstr ""
68
 
69
- #: classes/help.php:109 classes/settings.php:116 classes/settings.php:125
70
- #: classes/settings.php:233
71
  msgid "Snippet"
72
  msgstr ""
73
 
@@ -90,7 +90,7 @@ msgid ""
90
  "defined as variables."
91
  msgstr ""
92
 
93
- #: classes/help.php:125 classes/settings.php:160
94
  msgid "Description"
95
  msgstr ""
96
 
@@ -122,7 +122,7 @@ msgid ""
122
  "variable is reserved, so don't use it in the variables field."
123
  msgstr ""
124
 
125
- #: classes/help.php:152 classes/settings.php:231
126
  msgid "Options"
127
  msgstr ""
128
 
@@ -193,109 +193,118 @@ msgid ""
193
  "(string) The variables to pass to the snippet, formatted as a query string."
194
  msgstr ""
195
 
196
- #: classes/settings.php:103
197
- msgid "Add New Snippet"
198
- msgstr ""
199
-
200
- #: classes/settings.php:104
201
- msgid "Delete Selected"
202
  msgstr ""
203
 
204
- #: classes/settings.php:105
205
- msgid "(Use the help dropdown button above for additional information.)"
 
 
206
  msgstr ""
207
 
208
- #: classes/settings.php:151
209
- msgid "PHP Code"
210
  msgstr ""
211
 
212
- #: classes/settings.php:172
213
- msgid "Update Snippets"
214
  msgstr ""
215
 
216
- #: classes/settings.php:193
217
- msgid ""
218
- "This is an overview of all snippets defined for this site. These snippets "
219
- "are inserted into posts from the post editor using the Post Snippets button. "
220
- "You can choose to see the snippets here as-is or as they are actually "
221
- "rendered on the website. Enabling rendered snippets for this overview might "
222
- "look strange if the snippet have dependencies on variables, CSS or other "
223
- "parameters only available on the frontend. If that is the case it is "
224
- "recommended to keep this option disabled."
225
  msgstr ""
226
 
227
- #: classes/settings.php:202
228
- msgid "Display rendered snippets"
229
  msgstr ""
230
 
231
- #: classes/settings.php:203
232
- msgid "Update"
233
  msgstr ""
234
 
235
- #: post-snippets.php:85
236
- msgid "Settings"
237
  msgstr ""
238
 
239
- #: post-snippets.php:458
240
- msgid "This snippet is insert only, no variables defined."
241
  msgstr ""
242
 
243
- #: post-snippets.php:626
244
- msgid "A snippet named Untitled has been added."
245
  msgstr ""
246
 
247
- #: post-snippets.php:644 post-snippets.php:797
248
  msgid "Snippets have been updated."
249
  msgstr ""
250
 
251
- #: post-snippets.php:660
252
- msgid "Selected snippets have been deleted."
253
  msgstr ""
254
 
255
- #: post-snippets.php:674
256
  msgid "Import/Export"
257
  msgstr ""
258
 
259
- #: post-snippets.php:675
260
- msgid "Export"
261
  msgstr ""
262
 
263
- #: post-snippets.php:677
264
- msgid "Export your snippets for backup or to import them on another site."
265
  msgstr ""
266
 
267
- #: post-snippets.php:678
268
- msgid "Export Snippets"
269
  msgstr ""
270
 
271
- #: post-snippets.php:765
272
- msgid "Import"
273
  msgstr ""
274
 
275
- #: post-snippets.php:767
276
- msgid ""
277
- "Import snippets from a post-snippets-export.zip file. Importing overwrites "
278
- "any existing snippets."
279
  msgstr ""
280
 
281
- #: post-snippets.php:771
282
- msgid "Import Snippets"
283
  msgstr ""
284
 
285
- #: post-snippets.php:799
286
- msgid "Snippets successfully imported."
287
  msgstr ""
288
 
289
- #: post-snippets.php:801 post-snippets.php:805 post-snippets.php:807
290
- msgid "Snippets could not be imported:"
291
  msgstr ""
292
 
293
- #: post-snippets.php:801
294
- msgid "Unzipping failed."
 
 
 
 
 
 
 
295
  msgstr ""
296
 
297
- #: post-snippets.php:807
298
- msgid "Upload failed."
 
 
 
 
 
 
 
 
 
 
 
 
299
  msgstr ""
300
 
301
  #. Plugin Name of the plugin/theme
2
  # This file is distributed under the same license as the Post Snippets package.
3
  msgid ""
4
  msgstr ""
5
+ "Project-Id-Version: Post Snippets 1.9.7\n"
6
  "Report-Msgid-Bugs-To: http://wordpress.org/tag/post-snippets\n"
7
+ "POT-Creation-Date: 2012-03-28 21:41:26+00:00\n"
8
  "MIME-Version: 1.0\n"
9
  "Content-Type: text/plain; charset=UTF-8\n"
10
  "Content-Transfer-Encoding: 8bit\n"
16
  msgid "Basic"
17
  msgstr ""
18
 
19
+ #: classes/help.php:46 classes/help.php:258 classes/settings.php:256
20
  msgid "Shortcode"
21
  msgstr ""
22
 
40
  msgid "Support Forums"
41
  msgstr ""
42
 
43
+ #: classes/help.php:91 classes/settings.php:225 classes/settings.php:234
44
  msgid "Title"
45
  msgstr ""
46
 
50
  "also becomes the name of the shortcode if you enable that option"
51
  msgstr ""
52
 
53
+ #: classes/help.php:98 classes/settings.php:226 classes/settings.php:235
54
+ #: classes/settings.php:354
55
  msgid "Variables"
56
  msgstr ""
57
 
66
  msgid "Example"
67
  msgstr ""
68
 
69
+ #: classes/help.php:109 classes/settings.php:227 classes/settings.php:236
70
+ #: classes/settings.php:368
71
  msgid "Snippet"
72
  msgstr ""
73
 
90
  "defined as variables."
91
  msgstr ""
92
 
93
+ #: classes/help.php:125 classes/settings.php:270
94
  msgid "Description"
95
  msgstr ""
96
 
122
  "variable is reserved, so don't use it in the variables field."
123
  msgstr ""
124
 
125
+ #: classes/help.php:152 classes/settings.php:366
126
  msgid "Options"
127
  msgstr ""
128
 
193
  "(string) The variables to pass to the snippet, formatted as a query string."
194
  msgstr ""
195
 
196
+ #: classes/import-export.php:91
197
+ msgid "Import"
 
 
 
 
198
  msgstr ""
199
 
200
+ #: classes/import-export.php:93
201
+ msgid ""
202
+ "Import snippets from a post-snippets-export.zip file. Importing overwrites "
203
+ "any existing snippets."
204
  msgstr ""
205
 
206
+ #: classes/import-export.php:97
207
+ msgid "Import Snippets"
208
  msgstr ""
209
 
210
+ #: classes/import-export.php:123
211
+ msgid "Snippets successfully imported."
212
  msgstr ""
213
 
214
+ #: classes/import-export.php:125 classes/import-export.php:129
215
+ #: classes/import-export.php:131
216
+ msgid "Snippets could not be imported:"
 
 
 
 
 
 
217
  msgstr ""
218
 
219
+ #: classes/import-export.php:125
220
+ msgid "Unzipping failed."
221
  msgstr ""
222
 
223
+ #: classes/import-export.php:131
224
+ msgid "Upload failed."
225
  msgstr ""
226
 
227
+ #: classes/settings.php:41
228
+ msgid "A snippet named Untitled has been added."
229
  msgstr ""
230
 
231
+ #: classes/settings.php:57
232
+ msgid "Nothing selected to delete."
233
  msgstr ""
234
 
235
+ #: classes/settings.php:70
236
+ msgid "Selected snippets have been deleted."
237
  msgstr ""
238
 
239
+ #: classes/settings.php:96
240
  msgid "Snippets have been updated."
241
  msgstr ""
242
 
243
+ #: classes/settings.php:189
244
+ msgid "Manage Snippets"
245
  msgstr ""
246
 
247
+ #: classes/settings.php:189 classes/settings.php:298
248
  msgid "Import/Export"
249
  msgstr ""
250
 
251
+ #: classes/settings.php:197
252
+ msgid "Use the help dropdown button for additional information."
253
  msgstr ""
254
 
255
+ #: classes/settings.php:261
256
+ msgid "PHP Code"
257
  msgstr ""
258
 
259
+ #: classes/settings.php:282
260
+ msgid "Update Snippets"
261
  msgstr ""
262
 
263
+ #: classes/settings.php:283
264
+ msgid "Add New Snippet"
265
  msgstr ""
266
 
267
+ #: classes/settings.php:284
268
+ msgid "Delete Selected"
 
 
269
  msgstr ""
270
 
271
+ #: classes/settings.php:299
272
+ msgid "Export"
273
  msgstr ""
274
 
275
+ #: classes/settings.php:302
276
+ msgid "Export your snippets for backup or to import them on another site."
277
  msgstr ""
278
 
279
+ #: classes/settings.php:304
280
+ msgid "Export Snippets"
281
  msgstr ""
282
 
283
+ #: classes/settings.php:327
284
+ msgid ""
285
+ "This is an overview of all snippets defined for this site. These snippets "
286
+ "are inserted into posts from the post editor using the Post Snippets button. "
287
+ "You can choose to see the snippets here as-is or as they are actually "
288
+ "rendered on the website. Enabling rendered snippets for this overview might "
289
+ "look strange if the snippet have dependencies on variables, CSS or other "
290
+ "parameters only available on the frontend. If that is the case it is "
291
+ "recommended to keep this option disabled."
292
  msgstr ""
293
 
294
+ #: classes/settings.php:337
295
+ msgid "Display rendered snippets"
296
+ msgstr ""
297
+
298
+ #: classes/settings.php:338
299
+ msgid "Update"
300
+ msgstr ""
301
+
302
+ #: post-snippets.php:99
303
+ msgid "Settings"
304
+ msgstr ""
305
+
306
+ #: post-snippets.php:472
307
+ msgid "This snippet is insert only, no variables defined."
308
  msgstr ""
309
 
310
  #. Plugin Name of the plugin/theme
post-snippets.php CHANGED
@@ -1,984 +1,808 @@
1
- <?php
2
- /*
3
- Plugin Name: Post Snippets
4
- Plugin URI: http://wpstorm.net/wordpress-plugins/post-snippets/
5
- Description: Build a library with snippets of HTML, PHP code or reoccurring text that you often use in your posts. Variables to replace parts of the snippet on insert can be used. The snippets can be inserted as-is or as shortcodes.
6
- Version: 1.9.7
7
- Author: Johan Steen
8
- Author URI: http://johansteen.se/
9
- Text Domain: post-snippets
10
-
11
- Copyright 2009-2012 Johan Steen (email : artstorm [at] gmail [dot] com)
12
-
13
- This program is free software; you can redistribute it and/or modify
14
- it under the terms of the GNU General Public License as published by
15
- the Free Software Foundation; either version 2 of the License, or
16
- (at your option) any later version.
17
-
18
- This program is distributed in the hope that it will be useful,
19
- but WITHOUT ANY WARRANTY; without even the implied warranty of
20
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
- GNU General Public License for more details.
22
-
23
- You should have received a copy of the GNU General Public License
24
- along with this program; if not, write to the Free Software
25
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
- */
27
-
28
- class Post_Snippets {
29
- private $tinymce_plugin_name = 'post_snippets';
30
- private $plugin_options = 'post_snippets_options';
31
-
32
- // -------------------------------------------------------------------------
33
-
34
- public function __construct() {
35
- // Define the domain and path for translations
36
- $rel_path = dirname(plugin_basename($this->get_File())).'/languages/';
37
- load_plugin_textdomain( 'post-snippets', false, $rel_path );
38
-
39
- $this->init_hooks();
40
- }
41
-
42
- /**
43
- * Initializes the hooks for the plugin
44
- */
45
- function init_hooks() {
46
-
47
- // Add TinyMCE button
48
- add_action('init', array(&$this, 'add_tinymce_button') );
49
-
50
- # Settings link on plugins list
51
- add_filter( 'plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
52
- # Options Page
53
- add_action( 'admin_menu', array(&$this,'wp_admin') );
54
-
55
- $this->create_shortcodes();
56
-
57
- # Adds the JS and HTML code in the header and footer for the jQuery insert UI dialog in the editor
58
- add_action( 'admin_init', array(&$this,'enqueue_assets') );
59
- add_action( 'admin_head', array(&$this,'jquery_ui_dialog') );
60
- add_action( 'admin_footer', array(&$this,'add_jquery_ui_dialog') );
61
-
62
- # Add Editor QuickTag button
63
- // IF WordPress is 3.3 or higher, use the new refactored method to add
64
- // the quicktag button.
65
- // Start showing a deprecated message from version 1.9 of the plugin for
66
- // the old method. And remove it completely when the plugin hits 2.0.
67
- global $wp_version;
68
- if ( version_compare($wp_version, '3.3', '>=') ) {
69
- add_action( 'admin_print_footer_scripts',
70
- array(&$this,'add_quicktag_button'), 100 );
71
- } else {
72
- add_action( 'edit_form_advanced', array(&$this,'add_quicktag_button_pre33') );
73
- add_action( 'edit_page_form', array(&$this,'add_quicktag_button_pre33') );
74
- }
75
- }
76
-
77
-
78
- /**
79
- * Quick link to the Post Snippets Settings page from the Plugins page.
80
- *
81
- * @return Array with all the plugin's action links
82
- */
83
- function plugin_action_links( $links, $file ) {
84
- if ( $file == plugin_basename( dirname($this->get_FILE()).'/post-snippets.php' ) ) {
85
- $links[] = '<a href="options-general.php?page=post-snippets/post-snippets.php">'.__('Settings', 'post-snippets').'</a>';
86
- }
87
- return $links;
88
- }
89
-
90
-
91
- /**
92
- * Enqueues the necessary scripts and styles for the plugins
93
- *
94
- * @since Post Snippets 1.7
95
- */
96
- function enqueue_assets() {
97
- wp_enqueue_script( 'jquery-ui-dialog' );
98
- wp_enqueue_script( 'jquery-ui-tabs' );
99
- wp_enqueue_style( 'wp-jquery-ui-dialog' );
100
-
101
- # Adds the CSS stylesheet for the jQuery UI dialog
102
- $style_url = plugins_url( '/assets/post-snippets.css', $this->get_FILE() );
103
- wp_register_style( 'post-snippets', $style_url, false, '2.0' );
104
- wp_enqueue_style( 'post-snippets' );
105
- }
106
-
107
-
108
- // -------------------------------------------------------------------------
109
- // WordPress Editor Buttons
110
- // -------------------------------------------------------------------------
111
-
112
- /**
113
- * Add TinyMCE button.
114
- *
115
- * Adds filters to add custom buttons to the TinyMCE editor (Visual Editor)
116
- * in WordPress.
117
- *
118
- * @since Post Snippets 1.8.7
119
- */
120
- public function add_tinymce_button()
121
- {
122
- // Don't bother doing this stuff if the current user lacks permissions
123
- if ( !current_user_can('edit_posts') &&
124
- !current_user_can('edit_pages') )
125
- return;
126
-
127
- // Add only in Rich Editor mode
128
- if ( get_user_option('rich_editing') == 'true') {
129
- add_filter('mce_external_plugins',
130
- array(&$this, 'register_tinymce_plugin') );
131
- add_filter('mce_buttons',
132
- array(&$this, 'register_tinymce_button') );
133
- }
134
- }
135
-
136
- /**
137
- * Register TinyMCE button.
138
- *
139
- * Pushes the custom TinyMCE button into the array of with button names.
140
- * 'separator' or '|' can be pushed to the array as well. See the link
141
- * for all available TinyMCE controls.
142
- *
143
- * @see wp-includes/class-wp-editor.php
144
- * @link http://www.tinymce.com/wiki.php/Buttons/controls
145
- * @since Post Snippets 1.8.7
146
- *
147
- * @param array $buttons Filter supplied array of buttons to modify
148
- * @return array The modified array with buttons
149
- */
150
- public function register_tinymce_button( $buttons )
151
- {
152
- array_push( $buttons, 'separator', $this->tinymce_plugin_name );
153
- return $buttons;
154
- }
155
-
156
- /**
157
- * Register TinyMCE plugin.
158
- *
159
- * Adds the absolute URL for the TinyMCE plugin to the associative array of
160
- * plugins. Array structure: 'plugin_name' => 'plugin_url'
161
- *
162
- * @see wp-includes/class-wp-editor.php
163
- * @since Post Snippets 1.8.7
164
- *
165
- * @param array $plugins Filter supplied array of plugins to modify
166
- * @return array The modified array with plugins
167
- */
168
- public function register_tinymce_plugin( $plugins )
169
- {
170
- // Load the TinyMCE plugin, editor_plugin.js, into the array
171
- $plugins[$this->tinymce_plugin_name] =
172
- plugins_url('/tinymce/editor_plugin.js?ver=1.9', $this->get_FILE());
173
-
174
- return $plugins;
175
- }
176
-
177
- /**
178
- * Adds a QuickTag button to the HTML editor.
179
- *
180
- * Compatible with WordPress 3.3 and newer.
181
- *
182
- * @see wp-includes/js/quicktags.dev.js -> qt.addButton()
183
- * @since Post Snippets 1.8.6
184
- */
185
- public function add_quicktag_button()
186
- {
187
- // Only run the function on post edit screens
188
- if ( function_exists( 'get_current_screen' ) ) {
189
- $screen = get_current_screen();
190
- if ($screen->base != 'post')
191
- return;
192
- }
193
-
194
- echo "\n<!-- START: Add QuickTag button for Post Snippets -->\n";
195
- ?>
196
- <script type="text/javascript" charset="utf-8">
197
- QTags.addButton( 'post_snippets_id', 'Post Snippets', qt_post_snippets );
198
- function qt_post_snippets() {
199
- post_snippets_caller = 'html';
200
- jQuery( "#post-snippets-dialog" ).dialog( "open" );
201
- }
202
- </script>
203
- <?php
204
- echo "\n<!-- END: Add QuickTag button for Post Snippets -->\n";
205
- }
206
-
207
-
208
- /**
209
- * Adds a QuickTag button to the HTML editor.
210
- *
211
- * Used when running on WordPress lower than version 3.3.
212
- *
213
- * @see wp-includes/js/quicktags.dev.js
214
- * @since Post Snippets 1.7
215
- * @deprecated Since 1.8.6
216
- */
217
- function add_quicktag_button_pre33() {
218
- // Only run the function on post edit screens
219
- if ( function_exists( 'get_current_screen' ) ) {
220
- $screen = get_current_screen();
221
- if ($screen->base != 'post')
222
- return;
223
- }
224
-
225
- echo "\n<!-- START: Post Snippets QuickTag button -->\n";
226
- ?>
227
- <script type="text/javascript" charset="utf-8">
228
- // <![CDATA[
229
- //edButton(id, display, tagStart, tagEnd, access, open)
230
- edbuttonlength = edButtons.length;
231
- edButtons[edbuttonlength++] = new edButton('ed_postsnippets', 'Post Snippets', '', '', '', -1);
232
- (function(){
233
- if (typeof jQuery === 'undefined') {
234
- return;
235
- }
236
- jQuery(document).ready(function(){
237
- jQuery("#ed_toolbar").append('<input type="button" value="Post Snippets" id="ed_postsnippets" class="ed_button" onclick="edOpenPostSnippets(edCanvas);" title="Post Snippets" />');
238
- });
239
- }());
240
- // ]]>
241
- </script>
242
- <?php
243
- echo "\n<!-- END: Post Snippets QuickTag button -->\n";
244
- }
245
-
246
-
247
- // -------------------------------------------------------------------------
248
- // JavaScript / jQuery handling for the post editor
249
- // -------------------------------------------------------------------------
250
-
251
- /**
252
- * jQuery control for the dialog and Javascript needed to insert snippets into the editor
253
- *
254
- * @since Post Snippets 1.7
255
- */
256
- public function jquery_ui_dialog()
257
- {
258
- // Only run the function on post edit screens
259
- if ( function_exists( 'get_current_screen' ) ) {
260
- $screen = get_current_screen();
261
- if ($screen->base != 'post')
262
- return;
263
- }
264
-
265
- echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
266
- echo "<script type='text/javascript'>\n";
267
-
268
- # Prepare the snippets and shortcodes into javascript variables
269
- # so they can be inserted into the editor, and get the variables replaced
270
- # with user defined strings.
271
- $snippets = get_option($this->plugin_options);
272
- foreach ($snippets as $key => $snippet) {
273
- if ($snippet['shortcode']) {
274
- # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
275
- # so {varnameX} can be replaced at runtime.
276
- $var_arr = explode(",",$snippet['vars']);
277
- $variables = '';
278
- if (!empty($var_arr[0])) {
279
- foreach ($var_arr as $var) {
280
- // '[test2 yet="{yet}" mupp=per="{mupp=per}" content="{content}"]';
281
- $var = $this->strip_default_val( $var );
282
-
283
- $variables .= ' ' . $var . '="{' . $var . '}"';
284
- }
285
- }
286
- $shortcode = $snippet['title'] . $variables;
287
- echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
288
- } else {
289
- // To use $snippet is probably not a good naming convention here.
290
- // rename to js_snippet or something?
291
- $snippet = $snippet['snippet'];
292
- # Fixes for potential collisions:
293
- /* Replace <> with char codes, otherwise </script> in a snippet will break it */
294
- $snippet = str_replace( '<', '\x3C', str_replace( '>', '\x3E', $snippet ) );
295
- /* Escape " with \" */
296
- $snippet = str_replace( '"', '\"', $snippet );
297
- /* Remove CR and replace LF with \n to keep formatting */
298
- $snippet = str_replace( chr(13), '', str_replace( chr(10), '\n', $snippet ) );
299
- # Print out the variable containing the snippet
300
- echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
301
- }
302
- }
303
- ?>
304
-
305
- jQuery(document).ready(function($){
306
- <?php
307
- # Create js variables for all form fields
308
- foreach ($snippets as $key => $snippet) {
309
- $var_arr = explode(",",$snippet['vars']);
310
- if (!empty($var_arr[0])) {
311
- foreach ($var_arr as $key_2 => $var) {
312
- $varname = "var_" . $key . "_" . $key_2;
313
- echo "var {$varname} = $( \"#{$varname}\" );\n";
314
- }
315
- }
316
- }
317
- ?>
318
-
319
- var $tabs = $("#post-snippets-tabs").tabs();
320
-
321
- $(function() {
322
- $( "#post-snippets-dialog" ).dialog({
323
- autoOpen: false,
324
- modal: true,
325
- dialogClass: 'wp-dialog',
326
- buttons: {
327
- Cancel: function() {
328
- $( this ).dialog( "close" );
329
- },
330
- "Insert": function() {
331
- $( this ).dialog( "close" );
332
- var selected = $tabs.tabs('option', 'selected');
333
- <?php
334
- foreach ($snippets as $key => $snippet) {
335
- ?>
336
- if (selected == <?php echo $key; ?>) {
337
- insert_snippet = postsnippet_<?php echo $key; ?>;
338
- <?php
339
- $var_arr = explode(",",$snippet['vars']);
340
- if (!empty($var_arr[0])) {
341
- foreach ($var_arr as $key_2 => $var) {
342
- $varname = "var_" . $key . "_" . $key_2; ?>
343
- insert_snippet = insert_snippet.replace(/\{<?php echo $this->strip_default_val( $var ); ?>\}/g, <?php echo $varname; ?>.val());
344
- <?php
345
- echo "\n";
346
- }
347
- }
348
- ?>
349
- }
350
- <?php
351
- }
352
- ?>
353
-
354
- // Decide what method to use to insert the snippet depending
355
- // from what editor the window was opened from
356
- if (post_snippets_caller == 'html') {
357
- // HTML editor in WordPress 3.3 and greater
358
- QTags.insertContent(insert_snippet);
359
- } else if (post_snippets_caller == 'html_pre33') {
360
- // HTML editor in WordPress below 3.3.
361
- edInsertContent(post_snippets_canvas, insert_snippet);
362
- } else {
363
- // Visual Editor
364
- post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
365
- }
366
-
367
- }
368
- },
369
- width: 500,
370
- });
371
- });
372
- });
373
-
374
- // Global variables to keep track on the canvas instance and from what editor
375
- // that opened the Post Snippets popup.
376
- var post_snippets_canvas;
377
- var post_snippets_caller = '';
378
-
379
- /**
380
- * Used in WordPress lower than version 3.3.
381
- * Not used anymore starting with WordPress version 3.3.
382
- * Called from: add_quicktag_button_pre33()
383
- */
384
- function edOpenPostSnippets(myField) {
385
- post_snippets_canvas = myField;
386
- post_snippets_caller = 'html_pre33';
387
- jQuery( "#post-snippets-dialog" ).dialog( "open" );
388
- };
389
- <?php
390
- echo "</script>\n";
391
- echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
392
- }
393
-
394
- /**
395
- * Build jQuery UI Window.
396
- *
397
- * Creates the jQuery for Post Editor popup window, its snippet tabs and the
398
- * form fields to enter variables.
399
- *
400
- * @since Post Snippets 1.7
401
- */
402
- public function add_jquery_ui_dialog()
403
- {
404
- // Only run the function on post edit screens
405
- if ( function_exists( 'get_current_screen' ) ) {
406
- $screen = get_current_screen();
407
- if ($screen->base != 'post')
408
- return;
409
- }
410
-
411
- echo "\n<!-- START: Post Snippets UI Dialog -->\n";
412
- // Setup the dialog divs
413
- echo "<div class=\"hidden\">\n";
414
- echo "\t<div id=\"post-snippets-dialog\" title=\"Post Snippets\">\n";
415
- // Init the tabs div
416
- echo "\t\t<div id=\"post-snippets-tabs\">\n";
417
- echo "\t\t\t<ul>\n";
418
-
419
- // Create a tab for each available snippet
420
- $snippets = get_option($this->plugin_options);
421
- foreach ($snippets as $key => $snippet) {
422
- echo "\t\t\t\t";
423
- echo "<li><a href=\"#ps-tabs-{$key}\">{$snippet['title']}</a></li>";
424
- echo "\n";
425
- }
426
- echo "\t\t\t</ul>\n";
427
-
428
- // Create a panel with form fields for each available snippet
429
- foreach ($snippets as $key => $snippet) {
430
- echo "\t\t\t<div id=\"ps-tabs-{$key}\">\n";
431
-
432
- // Print a snippet description is available
433
- if ( isset($snippet['description']) )
434
- echo "\t\t\t\t<p class=\"howto\">" . $snippet['description'] . "</p>\n";
435
-
436
- // Get all variables defined for the snippet and output them as
437
- // input fields
438
- $var_arr = explode(',', $snippet['vars']);
439
- if (!empty($var_arr[0])) {
440
- foreach ($var_arr as $key_2 => $var) {
441
- // Default value exists?
442
- $def_pos = strpos( $var, '=' );
443
- if ( $def_pos !== false ) {
444
- $split = explode( '=', $var );
445
- $var = $split[0];
446
- $def = $split[1];
447
- } else {
448
- $def = '';
449
- }
450
- echo "\t\t\t\t<label for=\"var_{$key}_{$key_2}\">{$var}:</label>\n";
451
- echo "\t\t\t\t<input type=\"text\" id=\"var_{$key}_{$key_2}\" name=\"var_{$key}_{$key_2}\" value=\"{$def}\" style=\"width: 190px\" />\n";
452
- echo "\t\t\t\t<br/>\n";
453
- }
454
- } else {
455
- // If no variables and no description available, output a text
456
- // to inform the user that it's an insert snippet only.
457
- if ( empty($snippet['description']) )
458
- echo "\t\t\t\t<p class=\"howto\">" . __('This snippet is insert only, no variables defined.', 'post-snippets') . "</p>\n";
459
- }
460
- echo "\t\t\t</div><!-- #ps-tabs-{$key} -->\n";
461
- }
462
- // Close the tabs and dialog divs
463
- echo "\t\t</div><!-- #post-snippets-tabs -->\n";
464
- echo "\t</div><!-- #post-snippets-dialog -->\n";
465
- echo "</div><!-- .hidden -->\n";
466
-
467
- echo "<!-- END: Post Snippets UI Dialog -->\n\n";
468
- }
469
-
470
- /**
471
- * Strip Default Value.
472
- *
473
- * Checks if a variable string contains a default value, and if it does it
474
- * will strip it away and return the string with only the variable name
475
- * kept.
476
- *
477
- * @since Post Snippets 1.9.3
478
- * @param string $variable The variable to check for default value
479
- * @return string The variable without any default value
480
- */
481
- public function strip_default_val( $variable )
482
- {
483
- // Check if variable contains a default defintion
484
- $def_pos = strpos( $variable, '=' );
485
-
486
- if ( $def_pos !== false ) {
487
- $split = str_split( $variable, $def_pos );
488
- $variable = $split[0];
489
- }
490
- return $variable;
491
- }
492
-
493
- // -------------------------------------------------------------------------
494
- // XXXXXX
495
- // -------------------------------------------------------------------------
496
-
497
- /**
498
- * Create the functions for shortcodes dynamically and register them
499
- */
500
- function create_shortcodes() {
501
- $snippets = get_option($this->plugin_options);
502
- if (!empty($snippets)) {
503
- foreach ($snippets as $snippet) {
504
- // If shortcode is enabled for the snippet, and a snippet has been entered, register it as a shortcode.
505
- if ( $snippet['shortcode'] && !empty($snippet['snippet']) ) {
506
-
507
- $vars = explode(",",$snippet['vars']);
508
- $vars_str = '';
509
- foreach ($vars as $var) {
510
- $vars_str = $vars_str . '"'.$var.'" => "",';
511
- }
512
-
513
- // Get the wptexturize setting
514
- $texturize = isset( $snippet["wptexturize"] ) ? $snippet["wptexturize"] : false;
515
-
516
- add_shortcode($snippet['title'], create_function('$atts,$content=null',
517
- '$shortcode_symbols = array('.$vars_str.');
518
- extract(shortcode_atts($shortcode_symbols, $atts));
519
-
520
- $attributes = compact( array_keys($shortcode_symbols) );
521
-
522
- // Add enclosed content if available to the attributes array
523
- if ( $content != null )
524
- $attributes["content"] = $content;
525
-
526
-
527
- $snippet = \''. addslashes($snippet["snippet"]) .'\';
528
- $snippet = str_replace("&", "&amp;", $snippet);
529
-
530
- foreach ($attributes as $key => $val) {
531
- $snippet = str_replace("{".$key."}", $val, $snippet);
532
- }
533
-
534
- // Handle PHP shortcodes
535
- $php = "'. $snippet["php"] .'";
536
- if ($php == true) {
537
- $snippet = Post_Snippets::php_eval( $snippet );
538
- }
539
-
540
- // Strip escaping and execute nested shortcodes
541
- $snippet = do_shortcode(stripslashes($snippet));
542
-
543
- // WPTexturize the Snippet
544
- $texturize = "'. $texturize .'";
545
- if ($texturize == true) {
546
- $snippet = wptexturize( $snippet );
547
- }
548
-
549
- return $snippet;') );
550
- }
551
- }
552
- }
553
- }
554
-
555
- /**
556
- * Evaluate a snippet as PHP code.
557
- *
558
- * @since Post Snippets 1.9
559
- * @param string $content The snippet to evaluate
560
- * @return string The result of the evaluation
561
- */
562
- public static function php_eval( $content )
563
- {
564
- $content = stripslashes($content);
565
-
566
- ob_start();
567
- eval ($content);
568
- $content = ob_get_clean();
569
-
570
- return addslashes( $content );
571
- }
572
-
573
- /**
574
- * The Admin Page and all it's functions
575
- */
576
- function wp_admin() {
577
- if ( current_user_can('manage_options') ) {
578
- // If user can manage options, display the admin page
579
- $option_page = add_options_page( 'Post Snippets Options', 'Post Snippets', 'administrator', $this->get_FILE(), array(&$this, 'options_page') );
580
- if ( $option_page and class_exists('Post_Snippets_Help') ) {
581
- $help = new Post_Snippets_Help( $option_page );
582
- }
583
- } else {
584
- // If user can't manage options, but can edit posts, display the overview page
585
- $option_page = add_options_page( 'Post Snippets', 'Post Snippets', 'edit_posts', $this->get_FILE(), array(&$this, 'overview_page') );
586
- }
587
- }
588
-
589
- function admin_message($message) {
590
- if ( $message ) {
591
- ?>
592
- <div class="updated"><p><strong><?php echo $message; ?></strong></p></div>
593
- <?php
594
- }
595
- }
596
-
597
- /**
598
- * The options Overview page.
599
- *
600
- * For users without manage_options cap but with edit_posts cap. A read-only
601
- * view.
602
- *
603
- * @since Post Snippets 1.9.7
604
- */
605
- public function overview_page() {
606
- $settings = new Post_Snippets_Settings();
607
- $settings->render( 'overview' );
608
- }
609
-
610
-
611
- function options_page() {
612
- // Add a new Snippet
613
- if (isset($_POST['add-snippet'])) {
614
- $snippets = get_option($this->plugin_options);
615
- if (empty($snippets)) { $snippets = array(); }
616
- array_push($snippets, array (
617
- 'title' => "Untitled",
618
- 'vars' => "",
619
- 'description' => "",
620
- 'shortcode' => false,
621
- 'php' => false,
622
- 'wptexturize' => false,
623
- 'snippet' => ""));
624
- update_option($this->plugin_options, $snippets);
625
- $this->admin_message( __( 'A snippet named Untitled has been added.', 'post-snippets' ) );
626
- }
627
-
628
- // Update Snippets
629
- if (isset($_POST['update-post-snippets'])) {
630
- $snippets = get_option($this->plugin_options);
631
- if (!empty($snippets)) {
632
- foreach ($snippets as $key => $value) {
633
- $new_snippets[$key]['title'] = trim($_POST[$key.'_title']);
634
- $new_snippets[$key]['vars'] = str_replace(' ', '', trim($_POST[$key.'_vars']) );
635
- $new_snippets[$key]['shortcode'] = isset($_POST[$key.'_shortcode']) ? true : false;
636
- $new_snippets[$key]['php'] = isset($_POST[$key.'_php']) ? true : false;
637
- $new_snippets[$key]['wptexturize'] = isset($_POST[$key.'_wptexturize']) ? true : false;
638
-
639
- $new_snippets[$key]['snippet'] = wp_specialchars_decode( trim(stripslashes($_POST[$key.'_snippet'])), ENT_NOQUOTES);
640
- $new_snippets[$key]['description'] = wp_specialchars_decode( trim(stripslashes($_POST[$key.'_description'])), ENT_NOQUOTES);
641
- }
642
- update_option($this->plugin_options, $new_snippets);
643
- $this->admin_message( __( 'Snippets have been updated.', 'post-snippets' ) );
644
- }
645
- }
646
-
647
- // Delete Snippets
648
- if (isset($_POST['delete-selected'])) {
649
- $snippets = get_option($this->plugin_options);
650
- if (!empty($snippets)) {
651
- $delete = $_POST['checked'];
652
- $newsnippets = array();
653
- foreach ($snippets as $key => $snippet) {
654
- if (in_array($key,$delete) == false) {
655
- array_push($newsnippets,$snippet);
656
- }
657
- }
658
- update_option($this->plugin_options, $newsnippets);
659
- $this->admin_message( __( 'Selected snippets have been deleted.', 'post-snippets' ) );
660
- }
661
- }
662
-
663
- // Handle import of snippets (Run before the option page is outputted, in case any snippets have been imported, so they are displayed).
664
- $import = $this->import_snippets();
665
-
666
-
667
- // Render the settings screen
668
- $settings = new Post_Snippets_Settings();
669
- $settings->render( 'options' );
670
-
671
- ?>
672
- <h3><?php _e( 'Import/Export', 'post-snippets' ); ?></h3>
673
- <strong><?php _e( 'Export', 'post-snippets' ); ?></strong><br/>
674
- <form method="post">
675
- <p><?php _e( 'Export your snippets for backup or to import them on another site.', 'post-snippets' ); ?></p>
676
- <input type="submit" class="button" name="postsnippets_export" value="<?php _e( 'Export Snippets', 'post-snippets');?>"/>
677
- </form>
678
- <?php
679
- $this->export_snippets();
680
- echo $import;
681
- }
682
-
683
-
684
- /**
685
- * Check if an export file shall be created, or if a download url should be pushed to the footer.
686
- * Also checks for old export files laying around and deletes them (for security).
687
- *
688
- * @since Post Snippets 1.8
689
- *
690
- * @return string URL to the exported snippets
691
- */
692
- function export_snippets() {
693
- if ( isset($_POST['postsnippets_export']) ) {
694
- $url = $this->create_export_file();
695
- if ($url) {
696
- define('PSURL', $url);
697
- function psnippets_footer() {
698
- $export .= '<script type="text/javascript">
699
- document.location = \''.PSURL.'\';
700
- </script>';
701
- echo $export;
702
- }
703
- add_action('admin_footer', 'psnippets_footer', 10000);
704
-
705
- } else {
706
- $export .= 'Error: '.$url;
707
- }
708
- } else {
709
- // Check if there is any old export files to delete
710
- $dir = wp_upload_dir();
711
- $upload_dir = $dir['basedir'] . '/';
712
- chdir($upload_dir);
713
- if (file_exists ( './post-snippets-export.zip' ) )
714
- unlink('./post-snippets-export.zip');
715
- }
716
- }
717
-
718
- /**
719
- * Create a zipped filed containing all Post Snippets, for export.
720
- *
721
- * @since Post Snippets 1.8
722
- *
723
- * @return string URL to the exported snippets
724
- */
725
- function create_export_file() {
726
- $snippets = serialize(get_option($this->plugin_options));
727
- $snippets = apply_filters( 'post_snippets_export', $snippets );
728
- $dir = wp_upload_dir();
729
- $upload_dir = $dir['basedir'] . '/';
730
- $upload_url = $dir['baseurl'] . '/';
731
-
732
- // Open a file stream and write the serialized options to it.
733
- if ( !$handle = fopen( $upload_dir.'post-snippets-export.cfg', 'w' ) )
734
- die();
735
- if ( !fwrite($handle, $snippets) )
736
- die();
737
- fclose($handle);
738
-
739
- // Create a zip archive
740
- require_once (ABSPATH . 'wp-admin/includes/class-pclzip.php');
741
- chdir($upload_dir);
742
- $zip = new PclZip('./post-snippets-export.zip');
743
- $zipped = $zip->create('./post-snippets-export.cfg');
744
-
745
- // Delete the snippet file
746
- unlink('./post-snippets-export.cfg');
747
-
748
- if (!$zipped)
749
- return false;
750
-
751
- return $upload_url.'post-snippets-export.zip';
752
- }
753
-
754
- /**
755
- * Handles uploading of post snippets archive and import the snippets.
756
- *
757
- * @uses wp_handle_upload() in wp-admin/includes/file.php
758
- * @since Post Snippets 1.8
759
- *
760
- * @return string HTML to handle the import
761
- */
762
- function import_snippets() {
763
- $import = '<br/><br/><strong>'.__( 'Import', 'post-snippets' ).'</strong><br/>';
764
- if ( !isset($_FILES['postsnippets_import_file']) || empty($_FILES['postsnippets_import_file']) ) {
765
- $import .= '<p>'.__( 'Import snippets from a post-snippets-export.zip file. Importing overwrites any existing snippets.', 'post-snippets' ).'</p>';
766
- $import .= '<form method="post" enctype="multipart/form-data">';
767
- $import .= '<input type="file" name="postsnippets_import_file"/>';
768
- $import .= '<input type="hidden" name="action" value="wp_handle_upload"/>';
769
- $import .= '<input type="submit" class="button" value="'.__( 'Import Snippets', 'post-snippets' ).'"/>';
770
- $import .= '</form>';
771
- } else {
772
- $file = wp_handle_upload( $_FILES['postsnippets_import_file'] );
773
-
774
- if ( isset( $file['file'] ) && !is_wp_error($file) ) {
775
- require_once (ABSPATH . 'wp-admin/includes/class-pclzip.php');
776
- $zip = new PclZip( $file['file'] );
777
- $dir = wp_upload_dir();
778
- $upload_dir = $dir['basedir'] . '/';
779
- chdir($upload_dir);
780
- $unzipped = $zip->extract();
781
-
782
- if ( $unzipped[0]['stored_filename'] == 'post-snippets-export.cfg' && $unzipped[0]['status'] == 'ok') {
783
- // Delete the uploaded archive
784
- unlink($file['file']);
785
-
786
- $snippets = file_get_contents( $upload_dir.'post-snippets-export.cfg' ); // Returns false on failure, else the contents
787
- if ($snippets) {
788
- $snippets = apply_filters( 'post_snippets_import', $snippets );
789
- update_option($this->plugin_options, unserialize($snippets));
790
- }
791
-
792
- // Delete the snippet file
793
- unlink('./post-snippets-export.cfg');
794
-
795
- $this->admin_message( __( 'Snippets have been updated.', 'post-snippets' ) );
796
-
797
- $import .= '<p><strong>'.__( 'Snippets successfully imported.').'</strong></p>';
798
- } else {
799
- $import .= '<p><strong>'.__( 'Snippets could not be imported:').' '.__('Unzipping failed.').'</strong></p>';
800
- }
801
- } else {
802
- if ( $file['error'] || is_wp_error( $file ) )
803
- $import .= '<p><strong>'.__( 'Snippets could not be imported:').' '.$file['error'].'</strong></p>';
804
- else
805
- $import .= '<p><strong>'.__( 'Snippets could not be imported:').' '.__('Upload failed.').'</strong></p>';
806
- }
807
- }
808
- return $import;
809
- }
810
-
811
- // -------------------------------------------------------------------------
812
- // Helpers
813
- // -------------------------------------------------------------------------
814
-
815
- /**
816
- * Get __FILE__ with no symlinks.
817
- *
818
- * For development purposes mainly. Returns __FILE__ without resolved
819
- * symlinks to be used when __FILE__ is needed while resolving symlinks
820
- * breaks WP functionaly, so the actual WordPress path is returned instead.
821
- * This makes it possible for all WordPress versions to point to the same
822
- * plugin folder for faster testing of the plugin in different WordPress
823
- * versions.
824
- *
825
- * @since Post Snippets 1.9
826
- * @return The __FILE__ constant without resolved symlinks.
827
- */
828
- private function get_FILE()
829
- {
830
- $dev_path = 'E:\Code\WordPress';
831
- $result = strpos( __FILE__, $dev_path );
832
-
833
- if ( $result === false ) {
834
- return __FILE__;
835
- } else {
836
- return str_replace($dev_path, WP_PLUGIN_DIR, __FILE__);
837
- }
838
- }
839
-
840
- /**
841
- * Allow snippets to be retrieved directly from PHP.
842
- *
843
- * @since Post Snippets 1.8.9.1
844
- *
845
- * @param string $snippet_name
846
- * The name of the snippet to retrieve
847
- * @param string $snippet_vars
848
- * The variables to pass to the snippet, formatted as a query string.
849
- * @return string
850
- * The Snippet
851
- */
852
- public function get_snippet( $snippet_name, $snippet_vars = '' )
853
- {
854
- $snippets = get_option($this->plugin_options);
855
- for ($i = 0; $i < count($snippets); $i++) {
856
- if ($snippets[$i]['title'] == $snippet_name) {
857
- parse_str( htmlspecialchars_decode($snippet_vars), $snippet_output );
858
- $snippet = $snippets[$i]['snippet'];
859
- $var_arr = explode(",",$snippets[$i]['vars']);
860
-
861
- if ( !empty($var_arr[0]) ) {
862
- for ($j = 0; $j < count($var_arr); $j++) {
863
- $snippet = str_replace("{".$var_arr[$j]."}", $snippet_output[$var_arr[$j]], $snippet);
864
- }
865
- }
866
- }
867
- }
868
- return $snippet;
869
- }
870
- }
871
-
872
-
873
- // -----------------------------------------------------------------------------
874
- // Start the plugin
875
- // -----------------------------------------------------------------------------
876
-
877
- // Check the host environment
878
- $test_post_snippets_host = new Post_Snippets_Host_Environment();
879
-
880
- // If environment is up to date, start the plugin
881
- if($test_post_snippets_host->passed) {
882
- // Load external classes
883
- if (is_admin()) {
884
- require plugin_dir_path(__FILE__).'classes/settings.php';
885
- require plugin_dir_path(__FILE__).'classes/help.php';
886
- }
887
-
888
- add_action(
889
- 'plugins_loaded',
890
- create_function(
891
- '',
892
- 'global $post_snippets; $post_snippets = new Post_Snippets();'
893
- )
894
- );
895
- }
896
-
897
-
898
- /**
899
- * Post Snippets Host Environment.
900
- *
901
- * Checks that the host environment fulfils the requirements of Post Snippets.
902
- * This class is designed to work with PHP versions below 5, to make sure it's
903
- * always executed.
904
- *
905
- * - PHP Version 5.2.4 is on par with the requirements for WordPress 3.3.
906
- *
907
- * @since Post Snippets 1.8.8
908
- */
909
- class Post_Snippets_Host_Environment
910
- {
911
- // Minimum versions required
912
- var $MIN_PHP_VERSION = '5.2.4';
913
- var $MIN_WP_VERSION = '3.0';
914
- var $passed = true;
915
-
916
- /**
917
- * Constructor.
918
- *
919
- * Checks PHP and WordPress versions. If any check failes, a system notice
920
- * is added and $passed is set to fail, which can be checked before trying
921
- * to create the main class.
922
- */
923
- function Post_Snippets_Host_Environment()
924
- {
925
- // Check if PHP is too old
926
- if (version_compare(PHP_VERSION, $this->MIN_PHP_VERSION, '<')) {
927
- // Display notice
928
- add_action( 'admin_notices', array(&$this, 'php_version_error') );
929
- $this->passed = false;
930
- }
931
-
932
- // Check if WordPress is too old
933
- global $wp_version;
934
- if ( version_compare($wp_version, $this->MIN_WP_VERSION, '<') ) {
935
- add_action( 'admin_notices', array(&$this, 'wp_version_error') );
936
- $this->passed = false;
937
- }
938
- }
939
-
940
- /**
941
- * Displays a warning when installed on an old PHP version.
942
- */
943
- function php_version_error() {
944
- echo '<div class="error"><p><strong>';
945
- printf(
946
- 'Error: Post Snippets requires PHP version %1$s or greater.<br/>'.
947
- 'Your installed PHP version: %2$s',
948
- $this->MIN_PHP_VERSION, PHP_VERSION);
949
- echo '</strong></p></div>';
950
- }
951
-
952
- /**
953
- * Displays a warning when installed in an old Wordpress version.
954
- */
955
- function wp_version_error() {
956
- echo '<div class="error"><p><strong>';
957
- printf(
958
- 'Error: Post Snippets requires WordPress version %s or greater.',
959
- $this->MIN_WP_VERSION );
960
- echo '</strong></p></div>';
961
- }
962
- }
963
-
964
- // -----------------------------------------------------------------------------
965
- // Helper functions
966
- // -----------------------------------------------------------------------------
967
-
968
- /**
969
- * Allow snippets to be retrieved directly from PHP.
970
- * This function is a wrapper for Post_Snippets::get_snippet().
971
- *
972
- * @since Post Snippets 1.6
973
- *
974
- * @param string $snippet_name
975
- * The name of the snippet to retrieve
976
- * @param string $snippet_vars
977
- * The variables to pass to the snippet, formatted as a query string.
978
- * @return string
979
- * The Snippet
980
- */
981
- function get_post_snippet( $snippet_name, $snippet_vars = '' ) {
982
- global $post_snippets;
983
- return $post_snippets->get_snippet( $snippet_name, $snippet_vars );
984
- }
1
+ <?php
2
+ /*
3
+ Plugin Name: Post Snippets
4
+ Plugin URI: http://wpstorm.net/wordpress-plugins/post-snippets/
5
+ Description: Build a library with snippets of HTML, PHP code or reoccurring text that you often use in your posts. Variables to replace parts of the snippet on insert can be used. The snippets can be inserted as-is or as shortcodes.
6
+ Version: 2.0
7
+ Author: Johan Steen
8
+ Author URI: http://johansteen.se/
9
+ Text Domain: post-snippets
10
+
11
+ Copyright 2009-2012 Johan Steen (email : artstorm [at] gmail [dot] com)
12
+
13
+ This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License as published by
15
+ the Free Software Foundation; either version 2 of the License, or
16
+ (at your option) any later version.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ */
27
+
28
+ /**
29
+ * Base Class.
30
+ */
31
+ class Post_Snippets_Base {
32
+ // Constants
33
+ const PLUGIN_OPTION_KEY = 'post_snippets_options';
34
+ const USER_OPTION_KEY = 'post_snippets';
35
+ }
36
+
37
+ /**
38
+ * Plugin Main Class.
39
+ */
40
+ class Post_Snippets extends Post_Snippets_Base
41
+ {
42
+ // Constants
43
+ const TINYMCE_PLUGIN_NAME = 'post_snippets';
44
+
45
+ // -------------------------------------------------------------------------
46
+
47
+ public function __construct() {
48
+ // Define the domain and path for translations
49
+ $rel_path = dirname(plugin_basename($this->get_File())).'/languages/';
50
+ load_plugin_textdomain( 'post-snippets', false, $rel_path );
51
+
52
+ $this->init_hooks();
53
+ }
54
+
55
+ /**
56
+ * Initializes the hooks for the plugin
57
+ */
58
+ function init_hooks() {
59
+
60
+ // Add TinyMCE button
61
+ add_action('init', array(&$this, 'add_tinymce_button') );
62
+
63
+ // Settings link on plugins list
64
+ add_filter( 'plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
65
+ // Options Page
66
+ add_action( 'admin_menu', array(&$this,'wp_admin') );
67
+
68
+ $this->create_shortcodes();
69
+
70
+ // Adds the JS and HTML code in the header and footer for the jQuery
71
+ // insert UI dialog in the editor
72
+ add_action( 'admin_init', array(&$this,'enqueue_assets') );
73
+ add_action( 'admin_head', array(&$this,'jquery_ui_dialog') );
74
+ add_action( 'admin_footer', array(&$this,'add_jquery_ui_dialog') );
75
+
76
+ // Add Editor QuickTag button:
77
+ // IF WordPress is 3.3 or higher, use the new refactored method to add
78
+ // the quicktag button.
79
+ // Start showing a deprecated message from version 1.9 of the plugin for
80
+ // the old method. And remove it completely when the plugin hits 2.0.
81
+ global $wp_version;
82
+ if ( version_compare($wp_version, '3.3', '>=') ) {
83
+ add_action( 'admin_print_footer_scripts',
84
+ array(&$this,'add_quicktag_button'), 100 );
85
+ } else {
86
+ add_action( 'edit_form_advanced', array(&$this,'add_quicktag_button_pre33') );
87
+ add_action( 'edit_page_form', array(&$this,'add_quicktag_button_pre33') );
88
+ }
89
+ }
90
+
91
+
92
+ /**
93
+ * Quick link to the Post Snippets Settings page from the Plugins page.
94
+ *
95
+ * @return Array with all the plugin's action links
96
+ */
97
+ function plugin_action_links( $links, $file ) {
98
+ if ( $file == plugin_basename( dirname($this->get_FILE()).'/post-snippets.php' ) ) {
99
+ $links[] = '<a href="options-general.php?page=post-snippets/post-snippets.php">'.__('Settings', 'post-snippets').'</a>';
100
+ }
101
+ return $links;
102
+ }
103
+
104
+
105
+ /**
106
+ * Enqueues the necessary scripts and styles for the plugins
107
+ *
108
+ * @since Post Snippets 1.7
109
+ */
110
+ function enqueue_assets() {
111
+ wp_enqueue_script( 'jquery-ui-dialog' );
112
+ wp_enqueue_script( 'jquery-ui-tabs' );
113
+ wp_enqueue_style( 'wp-jquery-ui-dialog' );
114
+
115
+ # Adds the CSS stylesheet for the jQuery UI dialog
116
+ $style_url = plugins_url( '/assets/post-snippets.css', $this->get_FILE() );
117
+ wp_register_style( 'post-snippets', $style_url, false, '2.0' );
118
+ wp_enqueue_style( 'post-snippets' );
119
+ }
120
+
121
+
122
+ // -------------------------------------------------------------------------
123
+ // WordPress Editor Buttons
124
+ // -------------------------------------------------------------------------
125
+
126
+ /**
127
+ * Add TinyMCE button.
128
+ *
129
+ * Adds filters to add custom buttons to the TinyMCE editor (Visual Editor)
130
+ * in WordPress.
131
+ *
132
+ * @since Post Snippets 1.8.7
133
+ */
134
+ public function add_tinymce_button()
135
+ {
136
+ // Don't bother doing this stuff if the current user lacks permissions
137
+ if ( !current_user_can('edit_posts') &&
138
+ !current_user_can('edit_pages') )
139
+ return;
140
+
141
+ // Add only in Rich Editor mode
142
+ if ( get_user_option('rich_editing') == 'true') {
143
+ add_filter('mce_external_plugins',
144
+ array(&$this, 'register_tinymce_plugin') );
145
+ add_filter('mce_buttons',
146
+ array(&$this, 'register_tinymce_button') );
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Register TinyMCE button.
152
+ *
153
+ * Pushes the custom TinyMCE button into the array of with button names.
154
+ * 'separator' or '|' can be pushed to the array as well. See the link
155
+ * for all available TinyMCE controls.
156
+ *
157
+ * @see wp-includes/class-wp-editor.php
158
+ * @link http://www.tinymce.com/wiki.php/Buttons/controls
159
+ * @since Post Snippets 1.8.7
160
+ *
161
+ * @param array $buttons Filter supplied array of buttons to modify
162
+ * @return array The modified array with buttons
163
+ */
164
+ public function register_tinymce_button( $buttons )
165
+ {
166
+ array_push( $buttons, 'separator', self::TINYMCE_PLUGIN_NAME );
167
+ return $buttons;
168
+ }
169
+
170
+ /**
171
+ * Register TinyMCE plugin.
172
+ *
173
+ * Adds the absolute URL for the TinyMCE plugin to the associative array of
174
+ * plugins. Array structure: 'plugin_name' => 'plugin_url'
175
+ *
176
+ * @see wp-includes/class-wp-editor.php
177
+ * @since Post Snippets 1.8.7
178
+ *
179
+ * @param array $plugins Filter supplied array of plugins to modify
180
+ * @return array The modified array with plugins
181
+ */
182
+ public function register_tinymce_plugin( $plugins )
183
+ {
184
+ // Load the TinyMCE plugin, editor_plugin.js, into the array
185
+ $plugins[self::TINYMCE_PLUGIN_NAME] =
186
+ plugins_url('/tinymce/editor_plugin.js?ver=1.9', $this->get_FILE());
187
+
188
+ return $plugins;
189
+ }
190
+
191
+ /**
192
+ * Adds a QuickTag button to the HTML editor.
193
+ *
194
+ * Compatible with WordPress 3.3 and newer.
195
+ *
196
+ * @see wp-includes/js/quicktags.dev.js -> qt.addButton()
197
+ * @since Post Snippets 1.8.6
198
+ */
199
+ public function add_quicktag_button()
200
+ {
201
+ // Only run the function on post edit screens
202
+ if ( function_exists( 'get_current_screen' ) ) {
203
+ $screen = get_current_screen();
204
+ if ($screen->base != 'post')
205
+ return;
206
+ }
207
+
208
+ echo "\n<!-- START: Add QuickTag button for Post Snippets -->\n";
209
+ ?>
210
+ <script type="text/javascript" charset="utf-8">
211
+ QTags.addButton( 'post_snippets_id', 'Post Snippets', qt_post_snippets );
212
+ function qt_post_snippets() {
213
+ post_snippets_caller = 'html';
214
+ jQuery( "#post-snippets-dialog" ).dialog( "open" );
215
+ }
216
+ </script>
217
+ <?php
218
+ echo "\n<!-- END: Add QuickTag button for Post Snippets -->\n";
219
+ }
220
+
221
+
222
+ /**
223
+ * Adds a QuickTag button to the HTML editor.
224
+ *
225
+ * Used when running on WordPress lower than version 3.3.
226
+ *
227
+ * @see wp-includes/js/quicktags.dev.js
228
+ * @since Post Snippets 1.7
229
+ * @deprecated Since 1.8.6
230
+ */
231
+ function add_quicktag_button_pre33() {
232
+ // Only run the function on post edit screens
233
+ if ( function_exists( 'get_current_screen' ) ) {
234
+ $screen = get_current_screen();
235
+ if ($screen->base != 'post')
236
+ return;
237
+ }
238
+
239
+ echo "\n<!-- START: Post Snippets QuickTag button -->\n";
240
+ ?>
241
+ <script type="text/javascript" charset="utf-8">
242
+ // <![CDATA[
243
+ //edButton(id, display, tagStart, tagEnd, access, open)
244
+ edbuttonlength = edButtons.length;
245
+ edButtons[edbuttonlength++] = new edButton('ed_postsnippets', 'Post Snippets', '', '', '', -1);
246
+ (function(){
247
+ if (typeof jQuery === 'undefined') {
248
+ return;
249
+ }
250
+ jQuery(document).ready(function(){
251
+ jQuery("#ed_toolbar").append('<input type="button" value="Post Snippets" id="ed_postsnippets" class="ed_button" onclick="edOpenPostSnippets(edCanvas);" title="Post Snippets" />');
252
+ });
253
+ }());
254
+ // ]]>
255
+ </script>
256
+ <?php
257
+ echo "\n<!-- END: Post Snippets QuickTag button -->\n";
258
+ }
259
+
260
+
261
+ // -------------------------------------------------------------------------
262
+ // JavaScript / jQuery handling for the post editor
263
+ // -------------------------------------------------------------------------
264
+
265
+ /**
266
+ * jQuery control for the dialog and Javascript needed to insert snippets into the editor
267
+ *
268
+ * @since Post Snippets 1.7
269
+ */
270
+ public function jquery_ui_dialog()
271
+ {
272
+ // Only run the function on post edit screens
273
+ if ( function_exists( 'get_current_screen' ) ) {
274
+ $screen = get_current_screen();
275
+ if ($screen->base != 'post')
276
+ return;
277
+ }
278
+
279
+ echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
280
+ echo "<script type='text/javascript'>\n";
281
+
282
+ # Prepare the snippets and shortcodes into javascript variables
283
+ # so they can be inserted into the editor, and get the variables replaced
284
+ # with user defined strings.
285
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
286
+ foreach ($snippets as $key => $snippet) {
287
+ if ($snippet['shortcode']) {
288
+ # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
289
+ # so {varnameX} can be replaced at runtime.
290
+ $var_arr = explode(",",$snippet['vars']);
291
+ $variables = '';
292
+ if (!empty($var_arr[0])) {
293
+ foreach ($var_arr as $var) {
294
+ // '[test2 yet="{yet}" mupp=per="{mupp=per}" content="{content}"]';
295
+ $var = $this->strip_default_val( $var );
296
+
297
+ $variables .= ' ' . $var . '="{' . $var . '}"';
298
+ }
299
+ }
300
+ $shortcode = $snippet['title'] . $variables;
301
+ echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
302
+ } else {
303
+ // To use $snippet is probably not a good naming convention here.
304
+ // rename to js_snippet or something?
305
+ $snippet = $snippet['snippet'];
306
+ # Fixes for potential collisions:
307
+ /* Replace <> with char codes, otherwise </script> in a snippet will break it */
308
+ $snippet = str_replace( '<', '\x3C', str_replace( '>', '\x3E', $snippet ) );
309
+ /* Escape " with \" */
310
+ $snippet = str_replace( '"', '\"', $snippet );
311
+ /* Remove CR and replace LF with \n to keep formatting */
312
+ $snippet = str_replace( chr(13), '', str_replace( chr(10), '\n', $snippet ) );
313
+ # Print out the variable containing the snippet
314
+ echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
315
+ }
316
+ }
317
+ ?>
318
+
319
+ jQuery(document).ready(function($){
320
+ <?php
321
+ # Create js variables for all form fields
322
+ foreach ($snippets as $key => $snippet) {
323
+ $var_arr = explode(",",$snippet['vars']);
324
+ if (!empty($var_arr[0])) {
325
+ foreach ($var_arr as $key_2 => $var) {
326
+ $varname = "var_" . $key . "_" . $key_2;
327
+ echo "var {$varname} = $( \"#{$varname}\" );\n";
328
+ }
329
+ }
330
+ }
331
+ ?>
332
+
333
+ var $tabs = $("#post-snippets-tabs").tabs();
334
+
335
+ $(function() {
336
+ $( "#post-snippets-dialog" ).dialog({
337
+ autoOpen: false,
338
+ modal: true,
339
+ dialogClass: 'wp-dialog',
340
+ buttons: {
341
+ Cancel: function() {
342
+ $( this ).dialog( "close" );
343
+ },
344
+ "Insert": function() {
345
+ $( this ).dialog( "close" );
346
+ var selected = $tabs.tabs('option', 'selected');
347
+ <?php
348
+ foreach ($snippets as $key => $snippet) {
349
+ ?>
350
+ if (selected == <?php echo $key; ?>) {
351
+ insert_snippet = postsnippet_<?php echo $key; ?>;
352
+ <?php
353
+ $var_arr = explode(",",$snippet['vars']);
354
+ if (!empty($var_arr[0])) {
355
+ foreach ($var_arr as $key_2 => $var) {
356
+ $varname = "var_" . $key . "_" . $key_2; ?>
357
+ insert_snippet = insert_snippet.replace(/\{<?php echo $this->strip_default_val( $var ); ?>\}/g, <?php echo $varname; ?>.val());
358
+ <?php
359
+ echo "\n";
360
+ }
361
+ }
362
+ ?>
363
+ }
364
+ <?php
365
+ }
366
+ ?>
367
+
368
+ // Decide what method to use to insert the snippet depending
369
+ // from what editor the window was opened from
370
+ if (post_snippets_caller == 'html') {
371
+ // HTML editor in WordPress 3.3 and greater
372
+ QTags.insertContent(insert_snippet);
373
+ } else if (post_snippets_caller == 'html_pre33') {
374
+ // HTML editor in WordPress below 3.3.
375
+ edInsertContent(post_snippets_canvas, insert_snippet);
376
+ } else {
377
+ // Visual Editor
378
+ post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
379
+ }
380
+
381
+ }
382
+ },
383
+ width: 500,
384
+ });
385
+ });
386
+ });
387
+
388
+ // Global variables to keep track on the canvas instance and from what editor
389
+ // that opened the Post Snippets popup.
390
+ var post_snippets_canvas;
391
+ var post_snippets_caller = '';
392
+
393
+ /**
394
+ * Used in WordPress lower than version 3.3.
395
+ * Not used anymore starting with WordPress version 3.3.
396
+ * Called from: add_quicktag_button_pre33()
397
+ */
398
+ function edOpenPostSnippets(myField) {
399
+ post_snippets_canvas = myField;
400
+ post_snippets_caller = 'html_pre33';
401
+ jQuery( "#post-snippets-dialog" ).dialog( "open" );
402
+ };
403
+ <?php
404
+ echo "</script>\n";
405
+ echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
406
+ }
407
+
408
+ /**
409
+ * Build jQuery UI Window.
410
+ *
411
+ * Creates the jQuery for Post Editor popup window, its snippet tabs and the
412
+ * form fields to enter variables.
413
+ *
414
+ * @since Post Snippets 1.7
415
+ */
416
+ public function add_jquery_ui_dialog()
417
+ {
418
+ // Only run the function on post edit screens
419
+ if ( function_exists( 'get_current_screen' ) ) {
420
+ $screen = get_current_screen();
421
+ if ($screen->base != 'post')
422
+ return;
423
+ }
424
+
425
+ echo "\n<!-- START: Post Snippets UI Dialog -->\n";
426
+ // Setup the dialog divs
427
+ echo "<div class=\"hidden\">\n";
428
+ echo "\t<div id=\"post-snippets-dialog\" title=\"Post Snippets\">\n";
429
+ // Init the tabs div
430
+ echo "\t\t<div id=\"post-snippets-tabs\">\n";
431
+ echo "\t\t\t<ul>\n";
432
+
433
+ // Create a tab for each available snippet
434
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
435
+ foreach ($snippets as $key => $snippet) {
436
+ echo "\t\t\t\t";
437
+ echo "<li><a href=\"#ps-tabs-{$key}\">{$snippet['title']}</a></li>";
438
+ echo "\n";
439
+ }
440
+ echo "\t\t\t</ul>\n";
441
+
442
+ // Create a panel with form fields for each available snippet
443
+ foreach ($snippets as $key => $snippet) {
444
+ echo "\t\t\t<div id=\"ps-tabs-{$key}\">\n";
445
+
446
+ // Print a snippet description is available
447
+ if ( isset($snippet['description']) )
448
+ echo "\t\t\t\t<p class=\"howto\">" . $snippet['description'] . "</p>\n";
449
+
450
+ // Get all variables defined for the snippet and output them as
451
+ // input fields
452
+ $var_arr = explode(',', $snippet['vars']);
453
+ if (!empty($var_arr[0])) {
454
+ foreach ($var_arr as $key_2 => $var) {
455
+ // Default value exists?
456
+ $def_pos = strpos( $var, '=' );
457
+ if ( $def_pos !== false ) {
458
+ $split = explode( '=', $var );
459
+ $var = $split[0];
460
+ $def = $split[1];
461
+ } else {
462
+ $def = '';
463
+ }
464
+ echo "\t\t\t\t<label for=\"var_{$key}_{$key_2}\">{$var}:</label>\n";
465
+ echo "\t\t\t\t<input type=\"text\" id=\"var_{$key}_{$key_2}\" name=\"var_{$key}_{$key_2}\" value=\"{$def}\" style=\"width: 190px\" />\n";
466
+ echo "\t\t\t\t<br/>\n";
467
+ }
468
+ } else {
469
+ // If no variables and no description available, output a text
470
+ // to inform the user that it's an insert snippet only.
471
+ if ( empty($snippet['description']) )
472
+ echo "\t\t\t\t<p class=\"howto\">" . __('This snippet is insert only, no variables defined.', 'post-snippets') . "</p>\n";
473
+ }
474
+ echo "\t\t\t</div><!-- #ps-tabs-{$key} -->\n";
475
+ }
476
+ // Close the tabs and dialog divs
477
+ echo "\t\t</div><!-- #post-snippets-tabs -->\n";
478
+ echo "\t</div><!-- #post-snippets-dialog -->\n";
479
+ echo "</div><!-- .hidden -->\n";
480
+
481
+ echo "<!-- END: Post Snippets UI Dialog -->\n\n";
482
+ }
483
+
484
+ /**
485
+ * Strip Default Value.
486
+ *
487
+ * Checks if a variable string contains a default value, and if it does it
488
+ * will strip it away and return the string with only the variable name
489
+ * kept.
490
+ *
491
+ * @since Post Snippets 1.9.3
492
+ * @param string $variable The variable to check for default value
493
+ * @return string The variable without any default value
494
+ */
495
+ public function strip_default_val( $variable )
496
+ {
497
+ // Check if variable contains a default defintion
498
+ $def_pos = strpos( $variable, '=' );
499
+
500
+ if ( $def_pos !== false ) {
501
+ $split = str_split( $variable, $def_pos );
502
+ $variable = $split[0];
503
+ }
504
+ return $variable;
505
+ }
506
+
507
+ // -------------------------------------------------------------------------
508
+ // Shortcode
509
+ // -------------------------------------------------------------------------
510
+
511
+ /**
512
+ * Create the functions for shortcodes dynamically and register them
513
+ */
514
+ function create_shortcodes() {
515
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
516
+ if (!empty($snippets)) {
517
+ foreach ($snippets as $snippet) {
518
+ // If shortcode is enabled for the snippet, and a snippet has been entered, register it as a shortcode.
519
+ if ( $snippet['shortcode'] && !empty($snippet['snippet']) ) {
520
+
521
+ $vars = explode(",",$snippet['vars']);
522
+ $vars_str = '';
523
+ foreach ($vars as $var) {
524
+ $vars_str = $vars_str . '"'.$var.'" => "",';
525
+ }
526
+
527
+ // Get the wptexturize setting
528
+ $texturize = isset( $snippet["wptexturize"] ) ? $snippet["wptexturize"] : false;
529
+
530
+ add_shortcode($snippet['title'], create_function('$atts,$content=null',
531
+ '$shortcode_symbols = array('.$vars_str.');
532
+ extract(shortcode_atts($shortcode_symbols, $atts));
533
+
534
+ $attributes = compact( array_keys($shortcode_symbols) );
535
+
536
+ // Add enclosed content if available to the attributes array
537
+ if ( $content != null )
538
+ $attributes["content"] = $content;
539
+
540
+
541
+ $snippet = \''. addslashes($snippet["snippet"]) .'\';
542
+ $snippet = str_replace("&", "&amp;", $snippet);
543
+
544
+ foreach ($attributes as $key => $val) {
545
+ $snippet = str_replace("{".$key."}", $val, $snippet);
546
+ }
547
+
548
+ // Handle PHP shortcodes
549
+ $php = "'. $snippet["php"] .'";
550
+ if ($php == true) {
551
+ $snippet = Post_Snippets::php_eval( $snippet );
552
+ }
553
+
554
+ // Strip escaping and execute nested shortcodes
555
+ $snippet = do_shortcode(stripslashes($snippet));
556
+
557
+ // WPTexturize the Snippet
558
+ $texturize = "'. $texturize .'";
559
+ if ($texturize == true) {
560
+ $snippet = wptexturize( $snippet );
561
+ }
562
+
563
+ return $snippet;') );
564
+ }
565
+ }
566
+ }
567
+ }
568
+
569
+ /**
570
+ * Evaluate a snippet as PHP code.
571
+ *
572
+ * @since Post Snippets 1.9
573
+ * @param string $content The snippet to evaluate
574
+ * @return string The result of the evaluation
575
+ */
576
+ public static function php_eval( $content )
577
+ {
578
+ $content = stripslashes($content);
579
+
580
+ ob_start();
581
+ eval ($content);
582
+ $content = ob_get_clean();
583
+
584
+ return addslashes( $content );
585
+ }
586
+
587
+
588
+ // -------------------------------------------------------------------------
589
+ // Admin
590
+ // -------------------------------------------------------------------------
591
+
592
+ /**
593
+ * The Admin Page.
594
+ */
595
+ function wp_admin() {
596
+ if ( current_user_can('manage_options') ) {
597
+ // If user can manage options, display the admin page
598
+ $option_page = add_options_page( 'Post Snippets Options', 'Post Snippets', 'administrator', $this->get_FILE(), array(&$this, 'options_page') );
599
+ if ( $option_page and class_exists('Post_Snippets_Help') ) {
600
+ $help = new Post_Snippets_Help( $option_page );
601
+ }
602
+ } else {
603
+ // If user can't manage options, but can edit posts, display the overview page
604
+ $option_page = add_options_page( 'Post Snippets', 'Post Snippets', 'edit_posts', $this->get_FILE(), array(&$this, 'overview_page') );
605
+ }
606
+ }
607
+
608
+ /**
609
+ * The options Overview page.
610
+ *
611
+ * For users without manage_options cap but with edit_posts cap. A read-only
612
+ * view.
613
+ *
614
+ * @since Post Snippets 1.9.7
615
+ */
616
+ public function overview_page()
617
+ {
618
+ $settings = new Post_Snippets_Settings();
619
+ $settings->render( 'overview' );
620
+ }
621
+
622
+ /**
623
+ * The options Admin page.
624
+ *
625
+ * For users with manage_options capability.
626
+ */
627
+ public function options_page()
628
+ {
629
+ $settings = new Post_Snippets_Settings();
630
+ $settings->render( 'options' );
631
+ }
632
+
633
+
634
+ // -------------------------------------------------------------------------
635
+ // Helpers
636
+ // -------------------------------------------------------------------------
637
+
638
+ /**
639
+ * Get __FILE__ with no symlinks.
640
+ *
641
+ * For development purposes mainly. Returns __FILE__ without resolved
642
+ * symlinks to be used when __FILE__ is needed while resolving symlinks
643
+ * breaks WP functionaly, so the actual WordPress path is returned instead.
644
+ * This makes it possible for all WordPress versions to point to the same
645
+ * plugin folder for faster testing of the plugin in different WordPress
646
+ * versions.
647
+ *
648
+ * @since Post Snippets 1.9
649
+ * @return The __FILE__ constant without resolved symlinks.
650
+ */
651
+ private function get_FILE()
652
+ {
653
+ $dev_path = 'E:\Code\WordPress';
654
+ $result = strpos( __FILE__, $dev_path );
655
+
656
+ if ( $result === false ) {
657
+ return __FILE__;
658
+ } else {
659
+ return str_replace($dev_path, WP_PLUGIN_DIR, __FILE__);
660
+ }
661
+ }
662
+
663
+ /**
664
+ * Allow snippets to be retrieved directly from PHP.
665
+ *
666
+ * @since Post Snippets 1.8.9.1
667
+ *
668
+ * @param string $snippet_name
669
+ * The name of the snippet to retrieve
670
+ * @param string $snippet_vars
671
+ * The variables to pass to the snippet, formatted as a query string.
672
+ * @return string
673
+ * The Snippet
674
+ */
675
+ public function get_snippet( $snippet_name, $snippet_vars = '' )
676
+ {
677
+ $snippets = get_option( self::PLUGIN_OPTION_KEY );
678
+ for ($i = 0; $i < count($snippets); $i++) {
679
+ if ($snippets[$i]['title'] == $snippet_name) {
680
+ parse_str( htmlspecialchars_decode($snippet_vars), $snippet_output );
681
+ $snippet = $snippets[$i]['snippet'];
682
+ $var_arr = explode(",",$snippets[$i]['vars']);
683
+
684
+ if ( !empty($var_arr[0]) ) {
685
+ for ($j = 0; $j < count($var_arr); $j++) {
686
+ $snippet = str_replace("{".$var_arr[$j]."}", $snippet_output[$var_arr[$j]], $snippet);
687
+ }
688
+ }
689
+ }
690
+ }
691
+ return $snippet;
692
+ }
693
+ }
694
+
695
+
696
+ // -----------------------------------------------------------------------------
697
+ // Start the plugin
698
+ // -----------------------------------------------------------------------------
699
+
700
+ // Check the host environment
701
+ $test_post_snippets_host = new Post_Snippets_Host_Environment();
702
+
703
+ // If environment is up to date, start the plugin
704
+ if($test_post_snippets_host->passed) {
705
+ // Load external classes
706
+ if (is_admin()) {
707
+ require plugin_dir_path(__FILE__).'classes/settings.php';
708
+ require plugin_dir_path(__FILE__).'classes/help.php';
709
+ require plugin_dir_path(__FILE__).'classes/import-export.php';
710
+ }
711
+
712
+ add_action(
713
+ 'plugins_loaded',
714
+ create_function(
715
+ '',
716
+ 'global $post_snippets; $post_snippets = new Post_Snippets();'
717
+ )
718
+ );
719
+ }
720
+
721
+
722
+ /**
723
+ * Post Snippets Host Environment.
724
+ *
725
+ * Checks that the host environment fulfils the requirements of Post Snippets.
726
+ * This class is designed to work with PHP versions below 5, to make sure it's
727
+ * always executed.
728
+ *
729
+ * - PHP Version 5.2.4 is on par with the requirements for WordPress 3.3.
730
+ *
731
+ * @since Post Snippets 1.8.8
732
+ */
733
+ class Post_Snippets_Host_Environment
734
+ {
735
+ // Minimum versions required
736
+ var $MIN_PHP_VERSION = '5.2.4';
737
+ var $MIN_WP_VERSION = '3.0';
738
+ var $passed = true;
739
+
740
+ /**
741
+ * Constructor.
742
+ *
743
+ * Checks PHP and WordPress versions. If any check failes, a system notice
744
+ * is added and $passed is set to fail, which can be checked before trying
745
+ * to create the main class.
746
+ */
747
+ function Post_Snippets_Host_Environment()
748
+ {
749
+ // Check if PHP is too old
750
+ if (version_compare(PHP_VERSION, $this->MIN_PHP_VERSION, '<')) {
751
+ // Display notice
752
+ add_action( 'admin_notices', array(&$this, 'php_version_error') );
753
+ $this->passed = false;
754
+ }
755
+
756
+ // Check if WordPress is too old
757
+ global $wp_version;
758
+ if ( version_compare($wp_version, $this->MIN_WP_VERSION, '<') ) {
759
+ add_action( 'admin_notices', array(&$this, 'wp_version_error') );
760
+ $this->passed = false;
761
+ }
762
+ }
763
+
764
+ /**
765
+ * Displays a warning when installed on an old PHP version.
766
+ */
767
+ function php_version_error() {
768
+ echo '<div class="error"><p><strong>';
769
+ printf(
770
+ 'Error: Post Snippets requires PHP version %1$s or greater.<br/>'.
771
+ 'Your installed PHP version: %2$s',
772
+ $this->MIN_PHP_VERSION, PHP_VERSION);
773
+ echo '</strong></p></div>';
774
+ }
775
+
776
+ /**
777
+ * Displays a warning when installed in an old Wordpress version.
778
+ */
779
+ function wp_version_error() {
780
+ echo '<div class="error"><p><strong>';
781
+ printf(
782
+ 'Error: Post Snippets requires WordPress version %s or greater.',
783
+ $this->MIN_WP_VERSION );
784
+ echo '</strong></p></div>';
785
+ }
786
+ }
787
+
788
+ // -----------------------------------------------------------------------------
789
+ // Helper functions
790
+ // -----------------------------------------------------------------------------
791
+
792
+ /**
793
+ * Allow snippets to be retrieved directly from PHP.
794
+ * This function is a wrapper for Post_Snippets::get_snippet().
795
+ *
796
+ * @since Post Snippets 1.6
797
+ *
798
+ * @param string $snippet_name
799
+ * The name of the snippet to retrieve
800
+ * @param string $snippet_vars
801
+ * The variables to pass to the snippet, formatted as a query string.
802
+ * @return string
803
+ * The Snippet
804
+ */
805
+ function get_post_snippet( $snippet_name, $snippet_vars = '' ) {
806
+ global $post_snippets;
807
+ return $post_snippets->get_snippet( $snippet_name, $snippet_vars );
808
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: artstorm
3
  Tags: post, admin, snippet, shortcode, html, custom, page, dynamic, editor, php, code
4
  Requires at least: 3.0
5
  Tested up to: 3.3.1
6
- Stable tag: 1.9.7
7
 
8
  Keep a snippet library of text, HTML or PHP code to be used in posts. Variables
9
  can be set for more flexibility. Inserts directly or as shortcodes.
@@ -86,19 +86,28 @@ for questions, answers, support and feature requests.
86
 
87
  == Screenshots ==
88
 
89
- 1. The Admin page where you set up new snippets.
90
  2. The TinyMCE button for Post Snippets.
91
- 3. The Post Snippet Insert Window.
 
 
92
 
93
  == Changelog ==
94
 
 
 
 
 
 
 
 
95
  = Version 1.9.7 - 22 Mar 2012 =
96
  * Updated the styling for the snippet insert window in the post editor. This
97
  fixes the visual glitch with tabs spanning multiple rows.
98
  * Users without `manage_options` but with `edit_posts` capability (authors,
99
- contributors, editors)can now see a read-only list of available snippets and
100
  related info.
101
- * Users with read-only access can toggle if they want to see their snippets
102
  overview as rendered output or as-is.
103
  * Enabled `label for=` with checkboxes.
104
  * Included Romanian translation by Web Hosting Geeks.
3
  Tags: post, admin, snippet, shortcode, html, custom, page, dynamic, editor, php, code
4
  Requires at least: 3.0
5
  Tested up to: 3.3.1
6
+ Stable tag: 2.0
7
 
8
  Keep a snippet library of text, HTML or PHP code to be used in posts. Variables
9
  can be set for more flexibility. Inserts directly or as shortcodes.
86
 
87
  == Screenshots ==
88
 
89
+ 1. The Admin page where you create and manage snippets.
90
  2. The TinyMCE button for Post Snippets.
91
+ 3. The Post Snippet insert dialog.
92
+ 4. Help system available on the admin page.
93
+
94
 
95
  == Changelog ==
96
 
97
+ = Version 2.0 - 29 Mar 2012 =
98
+ * Added tabs to the Admin page, and moved Import/Export to a separate tab.
99
+ * Pressing enter in a text field now defaults to Update and not Add.
100
+ * Fixed debug notice message displayed when deleting without any snippets
101
+ selected.
102
+ * Various code refactoring and optimizations.
103
+
104
  = Version 1.9.7 - 22 Mar 2012 =
105
  * Updated the styling for the snippet insert window in the post editor. This
106
  fixes the visual glitch with tabs spanning multiple rows.
107
  * Users without `manage_options` but with `edit_posts` capability (authors,
108
+ contributors, editors) can now see a read-only list of available snippets and
109
  related info.
110
+ * Users with read-only access can toggle if they want to see their snippets
111
  overview as rendered output or as-is.
112
  * Enabled `label for=` with checkboxes.
113
  * Included Romanian translation by Web Hosting Geeks.
screenshot-1.jpg CHANGED
Binary file
screenshot-3.jpg CHANGED
Binary file
screenshot-4.jpg ADDED
Binary file
uninstall.php CHANGED
@@ -1,8 +1,8 @@
1
  <?php
2
  /**
3
- * Uninstall.
4
- * Clean up the WP DB by deleting the options created by the plugin.
5
- */
6
  function post_snippets_uninstall()
7
  {
8
  // Delete all snippets
1
  <?php
2
  /**
3
+ * Uninstall.
4
+ * Clean up the WP DB by deleting the options created by the plugin.
5
+ */
6
  function post_snippets_uninstall()
7
  {
8
  // Delete all snippets