Post Snippets - Version 2.2

Version Description

Note that at least WordPress v3.3 are required for Post Snippets v2.2.

Download this release

Release Info

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

Code changes from version 2.1.1 to 2.2

lang/post-snippets.pot CHANGED
@@ -1,154 +1,141 @@
1
- # Copyright (C) 2012 Post Snippets
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"
11
- "PO-Revision-Date: 2012-MO-DA HO:MI+ZONE\n"
12
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
13
  "Language-Team: LANGUAGE <LL@li.org>\n"
 
 
 
14
 
15
- #: classes/help.php:41 classes/help.php:256
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
 
23
- #: classes/help.php:51 classes/help.php:260
24
- msgid "PHP"
 
25
  msgstr ""
26
 
27
- #: classes/help.php:56 classes/help.php:262
28
- msgid "Advanced"
 
29
  msgstr ""
30
 
31
- #: classes/help.php:70
32
- msgid "For more information:"
33
  msgstr ""
34
 
35
- #: classes/help.php:74
36
- msgid "Post Snippets Documentation"
37
  msgstr ""
38
 
39
- #: classes/help.php:78
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
 
47
- #: classes/help.php:94
48
- msgid ""
49
- "Give the snippet a title that helps you identify it in the post editor. This "
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
 
58
- #: classes/help.php:101
59
  msgid ""
60
- "A comma separated list of custom variables you can reference in your "
61
- "snippet. A variable can also be assigned a default value that will be used "
62
- "in the insert window by using the equal sign, variable=default."
63
- msgstr ""
64
-
65
- #: classes/help.php:104 classes/help.php:115 classes/help.php:230
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
 
74
- #: classes/help.php:112
75
- msgid ""
76
- "This is the block of text, HTML or PHP to insert in the post or as a "
77
- "shortcode. If you have entered predefined variables you can reference them "
78
- "from the snippet by enclosing them in {} brackets."
79
  msgstr ""
80
 
81
- #: classes/help.php:117
82
- msgid ""
83
- "To reference the variables in the example above, you would enter {url} and "
84
- "{name}. So if you enter this snippet:"
85
  msgstr ""
86
 
87
- #: classes/help.php:121
88
  msgid ""
89
- "You will get the option to replace url and name on insert if they are "
90
- "defined as variables."
91
  msgstr ""
92
 
93
- #: classes/help.php:125 classes/settings.php:270
94
- msgid "Description"
95
  msgstr ""
96
 
97
- #: classes/help.php:128
98
  msgid ""
99
- "An optional description for the Snippet. If filled out, the description will "
100
- "be displayed in the snippets insert window in the post editor."
101
  msgstr ""
102
 
103
- #: classes/help.php:141
104
  msgid ""
105
- "When enabling the shortcode checkbox, the snippet is no longer inserted "
106
- "directly but instead inserted as a shortcode. The obvious advantage of this "
107
- "is of course that you can insert a block of text or code in many places on "
108
- "the site, and update the content from one single place."
109
  msgstr ""
110
 
111
- #: classes/help.php:145
112
  msgid ""
113
- "The name to use the shortcode is the same as the title of the snippet "
114
- "(spaces are not allowed). When inserting a shortcode snippet, the shortcode "
115
- "and not the content will be inserted in the post."
116
  msgstr ""
117
 
118
- #: classes/help.php:148
119
  msgid ""
120
- "If you enclose the shortcode in your posts, you can access the enclosed "
121
- "content by using the variable {content} in your snippet. The {content} "
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
 
129
- #: classes/help.php:155
130
- msgid "See the dedicated help section for information about PHP shortcodes."
 
 
131
  msgstr ""
132
 
133
- #: classes/help.php:158
134
  msgid ""
135
- "Before the shortcode is outputted, it can optionally be formatted with %s, "
136
- "to transform quotes to smart quotes, apostrophes, dashes, ellipses, the "
137
- "trademark symbol, and the multiplication symbol."
138
  msgstr ""
139
 
140
- #: classes/help.php:171
141
  msgid ""
142
  "Snippets defined as shortcodes can optionally also be evaluated as PHP Code "
143
  "by enabling the PHP checkbox. PHP snippets is only available when treating "
144
  "the snippet as a shortcode."
145
  msgstr ""
146
 
147
- #: classes/help.php:174
148
  msgid "Example PHP Snippet"
149
  msgstr ""
150
 
151
- #: classes/help.php:183
152
  msgid ""
153
  "With a snippet defined like the one above, you can call it with its "
154
  "shortcode definition in a post. Let's pretend that the example snippet is "
@@ -156,131 +143,119 @@ msgid ""
156
  "like this from a post:"
157
  msgstr ""
158
 
159
- #: classes/help.php:189
160
  msgid ""
161
  "When the shortcode is executed the loop_me variable will be replaced with "
162
  "the string supplied in the shortcode and then the PHP code will be "
163
  "evaluated. (Outputting the string five times in this case. Wow!)"
164
  msgstr ""
165
 
166
- #: classes/help.php:192
167
  msgid ""
168
  "Note the evaluation order, any snippet variables will be replaced before the "
169
  "snippet is evaluated as PHP code. Also note that a PHP snippet don't need to "
170
  "be wrapped in &lt;?php #code; ?&gt;."
171
  msgstr ""
172
 
173
- #: classes/help.php:205
174
  msgid ""
175
- "You can retrieve a Post Snippet directly from PHP, in a theme for instance, "
176
- "by using the get_post_snippet() function."
177
- msgstr ""
178
-
179
- #: classes/help.php:209
180
- msgid "Usage"
181
  msgstr ""
182
 
183
- #: classes/help.php:217
184
- msgid "Parameters"
 
 
 
185
  msgstr ""
186
 
187
- #: classes/help.php:221
188
- msgid "(string) (required) The name of the snippet to retrieve."
 
 
 
189
  msgstr ""
190
 
191
- #: classes/help.php:226
192
- msgid ""
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. "
@@ -291,41 +266,57 @@ msgid ""
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
311
- msgid "Post Snippets"
312
  msgstr ""
313
 
314
- #. Plugin URI of the plugin/theme
315
- msgid "http://wpstorm.net/wordpress-plugins/post-snippets/"
 
 
 
 
316
  msgstr ""
317
 
318
- #. Description of the plugin/theme
319
  msgid ""
320
- "Build a library with snippets of HTML, PHP code or reoccurring text that you "
321
- "often use in your posts. Variables to replace parts of the snippet on insert "
322
- "can be used. The snippets can be inserted as-is or as shortcodes."
 
 
 
323
  msgstr ""
324
 
325
- #. Author of the plugin/theme
326
- msgid "Johan Steen"
 
 
 
 
 
327
  msgstr ""
328
 
329
- #. Author URI of the plugin/theme
330
- msgid "http://johansteen.se/"
 
 
 
 
331
  msgstr ""
1
+ # SOME DESCRIPTIVE TITLE.
2
+ # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3
+ # This file is distributed under the same license as the PACKAGE package.
4
+ # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5
+ #
6
+ #, fuzzy
7
  msgid ""
8
  msgstr ""
9
+ "Project-Id-Version: PACKAGE VERSION\n"
10
+ "Report-Msgid-Bugs-To: \n"
11
+ "POT-Creation-Date: 2013-04-26 10:36+0700\n"
12
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 
 
 
13
  "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14
  "Language-Team: LANGUAGE <LL@li.org>\n"
15
+ "MIME-Version: 1.0\n"
16
+ "Content-Type: text/plain; charset=CHARSET\n"
17
+ "Content-Transfer-Encoding: 8bit\n"
18
 
19
+ #: post-snippets.php:168
20
+ msgid "Settings"
21
  msgstr ""
22
 
23
+ #: views/admin_snippets.php:8 views/admin_snippets.php:17
24
+ #: views/help_basic.php:1
25
+ msgid "Title"
26
  msgstr ""
27
 
28
+ #: views/admin_snippets.php:9 views/admin_snippets.php:18
29
+ #: views/help_basic.php:6 lib/PostSnippets/Admin.php:316
30
+ msgid "Variables"
31
  msgstr ""
32
 
33
+ #: views/admin_snippets.php:10 views/admin_snippets.php:19
34
+ #: views/help_basic.php:16 lib/PostSnippets/Admin.php:335
35
+ msgid "Snippet"
36
  msgstr ""
37
 
38
+ #: views/admin_snippets.php:39 lib/PostSnippets/Help.php:40
39
+ msgid "Shortcode"
40
  msgstr ""
41
 
42
+ #: views/admin_snippets.php:46
43
+ msgid "PHP Code"
44
  msgstr ""
45
 
46
+ #: views/admin_snippets.php:58 views/help_basic.php:29
47
+ msgid "Description"
48
  msgstr ""
49
 
50
+ #: views/admin_snippets.php:70
51
+ msgid "Update Snippets"
52
  msgstr ""
53
 
54
+ #: views/admin_snippets.php:71
55
+ msgid "Add New Snippet"
 
 
56
  msgstr ""
57
 
58
+ #: views/admin_snippets.php:72
59
+ msgid "Delete Selected"
 
60
  msgstr ""
61
 
62
+ #: views/help_advanced.php:2
63
  msgid ""
64
+ "You can retrieve a Post Snippet directly from PHP, in a theme for instance, "
65
+ "by using the PostSnippets::getSnippet() method."
 
 
 
 
 
66
  msgstr ""
67
 
68
+ #: views/help_advanced.php:5
69
+ msgid "Usage"
 
70
  msgstr ""
71
 
72
+ #: views/help_advanced.php:11
73
+ msgid "Parameters"
 
 
 
74
  msgstr ""
75
 
76
+ #: views/help_advanced.php:14
77
+ msgid "(string) (required) The name of the snippet to retrieve."
 
 
78
  msgstr ""
79
 
80
+ #: views/help_advanced.php:17
81
  msgid ""
82
+ "(string) The variables to pass to the snippet, formatted as a query string."
 
83
  msgstr ""
84
 
85
+ #: views/help_advanced.php:20 views/help_basic.php:12 views/help_basic.php:21
86
+ msgid "Example"
87
  msgstr ""
88
 
89
+ #: views/help_basic.php:3
90
  msgid ""
91
+ "Give the snippet a title that helps you identify it in the post editor. This "
92
+ "also becomes the name of the shortcode if you enable that option"
93
  msgstr ""
94
 
95
+ #: views/help_basic.php:8
96
  msgid ""
97
+ "A comma separated list of custom variables you can reference in your "
98
+ "snippet. A variable can also be assigned a default value that will be used "
99
+ "in the insert window by using the equal sign, variable=default."
 
100
  msgstr ""
101
 
102
+ #: views/help_basic.php:18
103
  msgid ""
104
+ "This is the block of text, HTML or PHP to insert in the post or as a "
105
+ "shortcode. If you have entered predefined variables you can reference them "
106
+ "from the snippet by enclosing them in {} brackets."
107
  msgstr ""
108
 
109
+ #: views/help_basic.php:22
110
  msgid ""
111
+ "To reference the variables in the example above, you would enter {url} and "
112
+ "{name}. So if you enter this snippet:"
 
 
 
 
 
113
  msgstr ""
114
 
115
+ #: views/help_basic.php:26
116
+ msgid ""
117
+ "You will get the option to replace url and name on insert if they are "
118
+ "defined as variables."
119
  msgstr ""
120
 
121
+ #: views/help_basic.php:31
122
  msgid ""
123
+ "An optional description for the Snippet. If filled out, the description will "
124
+ "be displayed in the snippets insert window in the post editor."
 
125
  msgstr ""
126
 
127
+ #: views/help_php.php:2
128
  msgid ""
129
  "Snippets defined as shortcodes can optionally also be evaluated as PHP Code "
130
  "by enabling the PHP checkbox. PHP snippets is only available when treating "
131
  "the snippet as a shortcode."
132
  msgstr ""
133
 
134
+ #: views/help_php.php:5
135
  msgid "Example PHP Snippet"
136
  msgstr ""
137
 
138
+ #: views/help_php.php:13
139
  msgid ""
140
  "With a snippet defined like the one above, you can call it with its "
141
  "shortcode definition in a post. Let's pretend that the example snippet is "
143
  "like this from a post:"
144
  msgstr ""
145
 
146
+ #: views/help_php.php:19
147
  msgid ""
148
  "When the shortcode is executed the loop_me variable will be replaced with "
149
  "the string supplied in the shortcode and then the PHP code will be "
150
  "evaluated. (Outputting the string five times in this case. Wow!)"
151
  msgstr ""
152
 
153
+ #: views/help_php.php:23
154
  msgid ""
155
  "Note the evaluation order, any snippet variables will be replaced before the "
156
  "snippet is evaluated as PHP code. Also note that a PHP snippet don't need to "
157
  "be wrapped in &lt;?php #code; ?&gt;."
158
  msgstr ""
159
 
160
+ #: views/help_shortcode.php:2
161
  msgid ""
162
+ "When enabling the shortcode checkbox, the snippet is no longer inserted "
163
+ "directly but instead inserted as a shortcode. The obvious advantage of this "
164
+ "is of course that you can insert a block of text or code in many places on "
165
+ "the site, and update the content from one single place."
 
 
166
  msgstr ""
167
 
168
+ #: views/help_shortcode.php:6
169
+ msgid ""
170
+ "The name to use the shortcode is the same as the title of the snippet "
171
+ "(spaces are not allowed). When inserting a shortcode snippet, the shortcode "
172
+ "and not the content will be inserted in the post."
173
  msgstr ""
174
 
175
+ #: views/help_shortcode.php:10
176
+ msgid ""
177
+ "If you enclose the shortcode in your posts, you can access the enclosed "
178
+ "content by using the variable {content} in your snippet. The {content} "
179
+ "variable is reserved, so don't use it in the variables field."
180
  msgstr ""
181
 
182
+ #: views/help_shortcode.php:13 lib/PostSnippets/Admin.php:332
183
+ msgid "Options"
 
184
  msgstr ""
185
 
186
+ #: views/help_shortcode.php:15
187
+ msgid "See the dedicated help section for information about PHP shortcodes."
188
  msgstr ""
189
 
190
+ #: views/help_shortcode.php:18
191
+ #, php-format
192
  msgid ""
193
+ "Before the shortcode is outputted, it can optionally be formatted with %s, "
194
+ "to transform quotes to smart quotes, apostrophes, dashes, ellipses, the "
195
+ "trademark symbol, and the multiplication symbol."
196
  msgstr ""
197
 
198
+ #: views/help_sidebar.php:2
199
+ msgid "For more information:"
200
  msgstr ""
201
 
202
+ #: views/help_sidebar.php:6
203
+ msgid "Documentation"
204
  msgstr ""
205
 
206
+ #: views/help_sidebar.php:10
207
+ msgid "Support Forums"
 
208
  msgstr ""
209
 
210
+ #: views/help_sidebar.php:14
211
+ msgid "GitHub Contribution"
212
  msgstr ""
213
 
214
+ #: views/jquery-ui-dialog.php:47
215
+ msgid "This snippet is insert only, no variables defined."
216
  msgstr ""
217
 
218
+ #: lib/PostSnippets/Admin.php:47
219
  msgid "A snippet named Untitled has been added."
220
  msgstr ""
221
 
222
+ #: lib/PostSnippets/Admin.php:67
223
  msgid "Nothing selected to delete."
224
  msgstr ""
225
 
226
+ #: lib/PostSnippets/Admin.php:83
227
  msgid "Selected snippets have been deleted."
228
  msgstr ""
229
 
230
+ #: lib/PostSnippets/Admin.php:118
231
  msgid "Snippets have been updated."
232
  msgstr ""
233
 
234
+ #: lib/PostSnippets/Admin.php:216
235
  msgid "Manage Snippets"
236
  msgstr ""
237
 
238
+ #: lib/PostSnippets/Admin.php:216 lib/PostSnippets/Admin.php:259
239
  msgid "Import/Export"
240
  msgstr ""
241
 
242
+ #: lib/PostSnippets/Admin.php:224
243
  msgid "Use the help dropdown button for additional information."
244
  msgstr ""
245
 
246
+ #: lib/PostSnippets/Admin.php:260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  msgid "Export"
248
  msgstr ""
249
 
250
+ #: lib/PostSnippets/Admin.php:263
251
  msgid "Export your snippets for backup or to import them on another site."
252
  msgstr ""
253
 
254
+ #: lib/PostSnippets/Admin.php:265
255
  msgid "Export Snippets"
256
  msgstr ""
257
 
258
+ #: lib/PostSnippets/Admin.php:288
259
  msgid ""
260
  "This is an overview of all snippets defined for this site. These snippets "
261
  "are inserted into posts from the post editor using the Post Snippets button. "
266
  "recommended to keep this option disabled."
267
  msgstr ""
268
 
269
+ #: lib/PostSnippets/Admin.php:298
270
  msgid "Display rendered snippets"
271
  msgstr ""
272
 
273
+ #: lib/PostSnippets/Admin.php:299
274
  msgid "Update"
275
  msgstr ""
276
 
277
+ #: lib/PostSnippets/Help.php:33
278
+ msgid "Basic"
279
  msgstr ""
280
 
281
+ #: lib/PostSnippets/Help.php:48
282
+ msgid "PHP"
283
  msgstr ""
284
 
285
+ #: lib/PostSnippets/Help.php:56
286
+ msgid "Advanced"
287
  msgstr ""
288
 
289
+ #: lib/PostSnippets/ImportExport.php:38
290
+ msgid "Error: "
291
+ msgstr ""
292
+
293
+ #: lib/PostSnippets/ImportExport.php:61
294
+ msgid "Import"
295
  msgstr ""
296
 
297
+ #: lib/PostSnippets/ImportExport.php:67
298
  msgid ""
299
+ "Import snippets from a post-snippets-export.zip file. Importing overwrites "
300
+ "any existing snippets."
301
+ msgstr ""
302
+
303
+ #: lib/PostSnippets/ImportExport.php:74
304
+ msgid "Import Snippets"
305
  msgstr ""
306
 
307
+ #: lib/PostSnippets/ImportExport.php:113
308
+ msgid "Snippets successfully imported."
309
+ msgstr ""
310
+
311
+ #: lib/PostSnippets/ImportExport.php:118 lib/PostSnippets/ImportExport.php:127
312
+ #: lib/PostSnippets/ImportExport.php:133
313
+ msgid "Snippets could not be imported:"
314
  msgstr ""
315
 
316
+ #: lib/PostSnippets/ImportExport.php:120
317
+ msgid "Unzipping failed."
318
+ msgstr ""
319
+
320
+ #: lib/PostSnippets/ImportExport.php:135
321
+ msgid "Upload failed."
322
  msgstr ""
lib/PostSnippets/Admin.php CHANGED
@@ -5,430 +5,392 @@
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 PostSnippets_Admin
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( PostSnippets::optionDBKey() );
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( PostSnippets::optionDBKey(), $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( PostSnippets::optionDBKey() );
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( PostSnippets::optionDBKey(), $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( PostSnippets::optionDBKey() );
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
-
90
- if ( PostSnippets::canExecutePHP() )
91
- $new_snippets[$key]['php'] = isset($_POST[$key.'_php']) ? true : false;
92
- else
93
- $new_snippets[$key]['php'] = isset($snippets[$key]['php']) ? $snippets[$key]['php'] : false;
94
-
95
- $new_snippets[$key]['wptexturize'] = isset($_POST[$key.'_wptexturize']) ? true : false;
96
-
97
- $new_snippets[$key]['snippet'] = wp_specialchars_decode( trim(stripslashes($_POST[$key.'_snippet'])), ENT_NOQUOTES);
98
- $new_snippets[$key]['description'] = wp_specialchars_decode( trim(stripslashes($_POST[$key.'_description'])), ENT_NOQUOTES);
99
- }
100
- update_option( PostSnippets::optionDBKey(), $new_snippets );
101
- $this->message( __( 'Snippets have been updated.', 'post-snippets' ) );
102
- }
103
- }
104
- }
105
-
106
- /**
107
- * Update User Option.
108
- *
109
- * Sets the per user option for the read-only overview page.
110
- *
111
- * @since Post Snippets 1.9.7
112
- */
113
- private function set_user_options()
114
- {
115
- if ( isset($_POST['post_snippets_user_nonce']) && wp_verify_nonce( $_POST['post_snippets_user_nonce'], 'post_snippets_user_options') )
116
- {
117
- $id = get_current_user_id();
118
- $render = isset( $_POST['render'] ) ? true : false;
119
- update_user_meta( $id, PostSnippets::userMetaKey(), $render );
120
- }
121
- }
122
-
123
- /**
124
- * Get User Option.
125
- *
126
- * Gets the per user option for the read-only overview page.
127
- *
128
- * @since Post Snippets 1.9.7
129
- * @return boolean If overview should be rendered on output or not
130
- */
131
- private function get_user_options()
132
- {
133
- $id = get_current_user_id();
134
- $options = get_user_meta( $id, PostSnippets::userMetaKey(), true );
135
- return $options;
136
- }
137
-
138
-
139
- // -------------------------------------------------------------------------
140
- // HTML generation for option pages
141
- // -------------------------------------------------------------------------
142
-
143
- /**
144
- * Render the options page.
145
- *
146
- * @since Post Snippets 1.9.7
147
- * @param string $page Admin page to render. Default: options
148
- */
149
- public function render( $page )
150
- {
151
- switch ( $page ) {
152
- case 'options':
153
- $this->options_page();
154
- break;
155
-
156
- default:
157
- $this->overview_page();
158
- break;
159
- }
160
- }
161
-
162
- /**
163
- * Display Flashing Message.
164
- *
165
- * @param string $message Message to display to the user.
166
- */
167
- private function message( $message )
168
- {
169
- if ( $message )
170
- echo "<div class='updated'><p><strong>{$message}</strong></p></div>";
171
- }
172
-
173
- /**
174
- * Creates the snippets administration page.
175
- *
176
- * For users with manage_options capability (admin, super admin).
177
- *
178
- * @since Post Snippets 1.8.8
179
- */
180
- private function options_page()
181
- {
182
- // Handle Form Submits
183
- $this->add();
184
- $this->delete();
185
- $this->update();
186
-
187
- // Header
188
- echo '<div class="wrap">';
189
- echo '<h2>Post Snippets</h2>';
190
-
191
- // Tabs
192
- $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'snippets';
193
- $base_url = '?page=post-snippets/post-snippets.php&amp;tab=';
194
- $tabs = array( 'snippets' => __( 'Manage Snippets', 'post-snippets' ), 'tools' => __( 'Import/Export', 'post-snippets' ) );
195
- echo '<h2 class="nav-tab-wrapper">';
196
- foreach ( $tabs as $tab => $title ) {
197
- $active = ( $active_tab == $tab ) ? ' nav-tab-active' : '';
198
- echo "<a href='{$base_url}{$tab}' class='nav-tab {$active}'>{$title}</a>";
199
- }
200
- echo '</h2>';
201
- echo '<p class="description">';
202
- _e( 'Use the help dropdown button for additional information.', 'post-snippets' );
203
- echo '</p>';
204
-
205
- // Tab content
206
- if( $active_tab == 'snippets' )
207
- $this->tab_snippets();
208
- else
209
- $this->tab_tools();
210
-
211
- // Close it
212
- echo '</div>';
213
- }
214
-
215
- /**
216
- * Tab to Manage Snippets.
217
- *
218
- * @since Post Snippets 2.0
219
- */
220
- private function tab_snippets()
221
- {
222
- echo '<form method="post" action="">';
223
- wp_nonce_field( 'update_snippets', 'update_snippets_nonce' );
224
- ?>
225
-
226
- <table class="widefat fixed" cellspacing="0">
227
- <thead>
228
- <tr>
229
- <th scope="col" class="check-column"><input type="checkbox" /></th>
230
- <th scope="col" style="width: 180px;"><?php _e( 'Title', 'post-snippets' ) ?></th>
231
- <th scope="col" style="width: 180px;"><?php _e( 'Variables', 'post-snippets' ) ?></th>
232
- <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
233
- </tr>
234
- </thead>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
- <tfoot>
237
- <tr>
238
- <th scope="col" class="check-column"><input type="checkbox" /></th>
239
- <th scope="col"><?php _e( 'Title', 'post-snippets' ) ?></th>
240
- <th scope="col"><?php _e( 'Variables', 'post-snippets' ) ?></th>
241
- <th scope="col"><?php _e( 'Snippet', 'post-snippets' ) ?></th>
242
- </tr>
243
- </tfoot>
244
-
245
- <tbody>
246
- <?php
247
- $snippets = get_option( PostSnippets::optionDBKey() );
248
- if (!empty($snippets)) {
249
- foreach ($snippets as $key => $snippet) {
250
- ?>
251
- <tr class='recent'>
252
- <th scope='row' class='check-column'><input type='checkbox' name='checked[]' value='<?php echo $key; ?>' /></th>
253
- <td class='row-title'>
254
- <input type='text' name='<?php echo $key; ?>_title' value='<?php echo $snippet['title']; ?>' />
255
- </td>
256
- <td class='name'>
257
- <input type='text' name='<?php echo $key; ?>_vars' value='<?php echo $snippet['vars']; ?>' />
258
- <br/>
259
- <br/>
260
- <?php
261
- $this->checkbox(__('Shortcode', 'post-snippets'), $key.'_shortcode',
262
- $snippet['shortcode']);
263
-
264
- echo '<br/><strong>Shortcode Options:</strong><br/>';
265
-
266
- if ( PostSnippets::canExecutePHP() ) {
267
- $this->checkbox(__('PHP Code', 'post-snippets'), $key.'_php',
268
- $snippet['php']);
269
- }
270
-
271
- $wptexturize = isset( $snippet['wptexturize'] ) ? $snippet['wptexturize'] : false;
272
- $this->checkbox('wptexturize', $key.'_wptexturize', $wptexturize);
273
- ?>
274
- </td>
275
- <td class='desc'>
276
- <textarea name="<?php echo $key; ?>_snippet" class="large-text" style='width: 100%;' rows="5"><?php echo htmlspecialchars($snippet['snippet'], ENT_NOQUOTES); ?></textarea>
277
- <?php _e( 'Description', 'post-snippets' ) ?>:
278
- <input type='text' style='width: 100%;' name='<?php echo $key; ?>_description' value='<?php if (isset( $snippet['description'] ) ) echo esc_html($snippet['description']); ?>' /><br/>
279
- </td>
280
- </tr>
281
- <?php
282
- }
283
- }
284
- ?>
285
- </tbody>
286
- </table>
287
-
288
- <?php
289
- $this->submit( 'update-snippets', __('Update Snippets', 'post-snippets') );
290
- $this->submit( 'add-snippet', __('Add New Snippet', 'post-snippets'), 'button-secondary', false );
291
- $this->submit( 'delete-snippets', __('Delete Selected', 'post-snippets'), 'button-secondary', false );
292
- echo '</form>';
293
- }
294
-
295
- /**
296
- * Tab for Import/Export
297
- *
298
- * @since Post Snippets 2.0
299
- */
300
- private function tab_tools()
301
- {
302
- $ie = new PostSnippets_ImportExport();
303
-
304
- // Create header and export html form
305
- printf( "<h3>%s</h3>", __( 'Import/Export', 'post-snippets' ));
306
- printf( "<h4>%s</h4>", __( 'Export', 'post-snippets' ));
307
- echo '<form method="post">';
308
- echo '<p>';
309
- _e( 'Export your snippets for backup or to import them on another site.', 'post-snippets' );
310
- echo '</p>';
311
- printf("<input type='submit' class='button' name='postsnippets_export' value='%s' />", __( 'Export Snippets', 'post-snippets') );
312
- echo '</form>';
313
-
314
- // Export logic, and import html form and logic
315
- $ie->export_snippets();
316
- echo $ie->import_snippets();
317
- }
318
-
319
-
320
- /**
321
- * Creates a read-only overview page.
322
- *
323
- * For users with edit_posts capability but without manage_options
324
- * capability.
325
- *
326
- * @since Post Snippets 1.9.7
327
- */
328
- private function overview_page()
329
- {
330
- // Header
331
- echo '<div class="wrap">';
332
- echo '<h2>Post Snippets</h2>';
333
- echo '<p>';
334
- _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' );
335
- echo '</p>';
336
-
337
- // Form
338
- $this->set_user_options();
339
- $render = $this->get_user_options();
340
-
341
- echo '<form method="post" action="">';
342
- wp_nonce_field( 'post_snippets_user_options', 'post_snippets_user_nonce' );
343
-
344
- $this->checkbox(__('Display rendered snippets', 'post-snippets'), 'render', $render );
345
- $this->submit( 'update-post-snippets-user', __('Update', 'post-snippets') );
346
- echo '</form>';
347
-
348
- // Snippet List
349
- $snippets = get_option( PostSnippets::optionDBKey() );
350
- if (!empty($snippets)) {
351
- foreach ($snippets as $key => $snippet) {
352
-
353
- echo "<hr style='border: none;border-top:1px dashed #aaa; margin:24px 0;' />";
354
-
355
- echo "<h3>{$snippet['title']}";
356
- if ($snippet['description'])
357
- echo "<span class='description'> {$snippet['description']}</span>";
358
- echo "</h3>";
359
-
360
- if ($snippet['vars'])
361
- printf( "<strong>%s:</strong> {$snippet['vars']}<br/>", __('Variables', 'post-snippets') );
362
-
363
- // echo "<strong>Variables:</strong> {$snippet['vars']}<br/>";
364
-
365
- $options = array();
366
- if ($snippet['shortcode'])
367
- array_push($options, 'Shortcode');
368
- if ($snippet['php'])
369
- array_push($options, 'PHP');
370
- if ($snippet['wptexturize'])
371
- array_push($options, 'wptexturize');
372
- if ($options)
373
- printf ( "<strong>%s:</strong> %s<br/>", __('Options', 'post-snippets'), implode(', ', $options) );
374
-
375
- printf( "<br/><strong>%s:</strong><br/>", __('Snippet', 'post-snippets') );
376
- if ( $render ) {
377
- echo do_shortcode( $snippet['snippet'] );
378
- } else {
379
- echo "<code>";
380
- echo nl2br( htmlspecialchars($snippet['snippet'], ENT_NOQUOTES) );
381
- echo "</code>";
382
- }
383
- }
384
- }
385
-
386
- // Close
387
- echo '</div>';
388
- }
389
-
390
-
391
- // -------------------------------------------------------------------------
392
- // HTML and Form element methods
393
- // -------------------------------------------------------------------------
394
-
395
- /**
396
- * Checkbox.
397
- *
398
- * Renders the HTML for an input checkbox.
399
- *
400
- * @param string $label The label rendered to screen
401
- * @param string $name The unique name and id to identify the input
402
- * @param boolean $checked If the input is checked or not
403
- */
404
- private function checkbox( $label, $name, $checked )
405
- {
406
- echo "<label for=\"{$name}\">";
407
- printf( '<input type="checkbox" name="%1$s" id="%1$s" value="true"', $name );
408
- if ($checked)
409
- echo ' checked';
410
- echo ' />';
411
- echo " {$label}</label><br/>";
412
- }
413
-
414
- /**
415
- * Submit.
416
- *
417
- * Renders the HTML for a submit button.
418
- *
419
- * @since Post Snippets 1.9.7
420
- * @param string $name The name that identifies the button on submit
421
- * @param string $label The label rendered on the button
422
- * @param string $class Optional. Button class. Default: button-primary
423
- * @param boolean $wrap Optional. Wrap in a submit div. Default: true
424
- */
425
- private function submit( $name, $label, $class='button-primary', $wrap=true )
426
- {
427
- $btn = sprintf( '<input type="submit" name="%s" value="%s" class="%s" />', $name, $label, $class );
428
-
429
- if ($wrap)
430
- $btn = "<div class=\"submit\">{$btn}</div>";
431
-
432
- echo $btn;
433
- }
434
  }
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
+ * @author Johan Steen <artstorm at gmail dot com>
9
+ * @link http://johansteen.se/
 
10
  */
11
  class PostSnippets_Admin
12
  {
13
+ // -------------------------------------------------------------------------
14
+ // Handle form submissions
15
+ // -------------------------------------------------------------------------
16
+
17
+ /**
18
+ * Add New Snippet.
19
+ */
20
+ private function add()
21
+ {
22
+ if (isset($_POST['add-snippet'])
23
+ && isset($_POST['update_snippets_nonce'])
24
+ && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
25
+ ) {
26
+ $snippets = get_option(PostSnippets::OPTION_KEY);
27
+ if (empty($snippets)) {
28
+ $snippets = array();
29
+ }
30
+
31
+ array_push(
32
+ $snippets,
33
+ array(
34
+ 'title' => 'Untitled',
35
+ 'vars' => '',
36
+ 'description' => '',
37
+ 'shortcode' => false,
38
+ 'php' => false,
39
+ 'wptexturize' => false,
40
+ 'snippet' => ''
41
+ )
42
+ );
43
+
44
+ update_option(PostSnippets::OPTION_KEY, $snippets);
45
+ $this->message(
46
+ __(
47
+ 'A snippet named Untitled has been added.',
48
+ PostSnippets::TEXT_DOMAIN
49
+ )
50
+ );
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Delete Snippet/s.
56
+ */
57
+ private function delete()
58
+ {
59
+ if (isset($_POST['delete-snippets'])
60
+ && isset($_POST['update_snippets_nonce'])
61
+ && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
62
+ ) {
63
+ $snippets = get_option(PostSnippets::OPTION_KEY);
64
+
65
+ if (empty($snippets) || !isset($_POST['checked'])) {
66
+ $this->message(
67
+ __('Nothing selected to delete.', PostSnippets::TEXT_DOMAIN)
68
+ );
69
+ return;
70
+ }
71
+
72
+ $delete = $_POST['checked'];
73
+ $newsnippets = array();
74
+ foreach ($snippets as $key => $snippet) {
75
+ if (in_array($key, $delete) == false) {
76
+ array_push($newsnippets, $snippet);
77
+ }
78
+ }
79
+
80
+ update_option(PostSnippets::OPTION_KEY, $newsnippets);
81
+ $this->message(
82
+ __(
83
+ 'Selected snippets have been deleted.',
84
+ PostSnippets::TEXT_DOMAIN
85
+ )
86
+ );
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Update Snippet/s.
92
+ */
93
+ private function update()
94
+ {
95
+ if (isset($_POST['update-snippets'])
96
+ && isset($_POST['update_snippets_nonce'])
97
+ && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
98
+ ) {
99
+ $snippets = get_option(PostSnippets::OPTION_KEY);
100
+ if (!empty($snippets)) {
101
+ foreach ($snippets as $key => $value) {
102
+ $new_snippets[$key]['title'] = trim($_POST[$key.'_title']);
103
+ $new_snippets[$key]['vars'] = str_replace(' ', '', trim($_POST[$key.'_vars']));
104
+ $new_snippets[$key]['shortcode'] = isset($_POST[$key.'_shortcode']) ? true : false;
105
+
106
+ if (PostSnippets::canExecutePHP()) {
107
+ $new_snippets[$key]['php'] = isset($_POST[$key.'_php']) ? true : false;
108
+ } else {
109
+ $new_snippets[$key]['php'] = isset($snippets[$key]['php']) ? $snippets[$key]['php'] : false;
110
+ }
111
+
112
+ $new_snippets[$key]['wptexturize'] = isset($_POST[$key.'_wptexturize']) ? true : false;
113
+
114
+ $new_snippets[$key]['snippet'] = wp_specialchars_decode(trim(stripslashes($_POST[$key.'_snippet'])), ENT_NOQUOTES);
115
+ $new_snippets[$key]['description'] = wp_specialchars_decode(trim(stripslashes($_POST[$key.'_description'])), ENT_NOQUOTES);
116
+ }
117
+ update_option(PostSnippets::OPTION_KEY, $new_snippets);
118
+ $this->message(__('Snippets have been updated.', PostSnippets::TEXT_DOMAIN));
119
+ }
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Update User Option.
125
+ *
126
+ * Sets the per user option for the read-only overview page.
127
+ *
128
+ * @since Post Snippets 1.9.7
129
+ */
130
+ private function setUserOptions()
131
+ {
132
+ if (isset($_POST['post_snippets_user_nonce'])
133
+ && wp_verify_nonce($_POST['post_snippets_user_nonce'], 'post_snippets_user_options')
134
+ ) {
135
+ $id = get_current_user_id();
136
+ $render = isset( $_POST['render'] ) ? true : false;
137
+ update_user_meta($id, PostSnippets::USER_META_KEY, $render);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Get User Option.
143
+ *
144
+ * Gets the per user option for the read-only overview page.
145
+ *
146
+ * @since Post Snippets 1.9.7
147
+ * @return boolean If overview should be rendered on output or not
148
+ */
149
+ private function getUserOptions()
150
+ {
151
+ $id = get_current_user_id();
152
+ $options = get_user_meta($id, PostSnippets::USER_META_KEY, true);
153
+ return $options;
154
+ }
155
+
156
+
157
+ // -------------------------------------------------------------------------
158
+ // HTML generation for option pages
159
+ // -------------------------------------------------------------------------
160
+
161
+ /**
162
+ * Render the options page.
163
+ *
164
+ * @since Post Snippets 1.9.7
165
+ * @param string $page Admin page to render. Default: options
166
+ */
167
+ public function render($page)
168
+ {
169
+ switch ($page) {
170
+ case 'options':
171
+ $this->optionsPage();
172
+ break;
173
+ default:
174
+ $this->overviewPage();
175
+ break;
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Display Flashing Message.
181
+ *
182
+ * @param string $message Message to display to the user.
183
+ */
184
+ private function message($message)
185
+ {
186
+ if ($message) {
187
+ echo "<div class='updated'><p><strong>{$message}</strong></p></div>";
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Creates the snippets administration page.
193
+ *
194
+ * For users with manage_options capability (admin, super admin).
195
+ *
196
+ * @since Post Snippets 1.8.8
197
+ */
198
+ private function optionsPage()
199
+ {
200
+ // Handle Form Submits
201
+ $this->add();
202
+ $this->delete();
203
+ $this->update();
204
+
205
+ // Header
206
+ echo '
207
+ <!-- Create a header in the default WordPress \'wrap\' container -->
208
+ <div class="wrap">
209
+ <div id="icon-plugins" class="icon32"></div>
210
+ <h2>Post Snippets</h2>
211
+ ';
212
+
213
+ // Tabs
214
+ $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'snippets';
215
+ $base_url = '?page=post-snippets/post-snippets.php&amp;tab=';
216
+ $tabs = array('snippets' => __('Manage Snippets', PostSnippets::TEXT_DOMAIN), 'tools' => __('Import/Export', PostSnippets::TEXT_DOMAIN));
217
+ echo '<h2 class="nav-tab-wrapper">';
218
+ foreach ($tabs as $tab => $title) {
219
+ $active = ( $active_tab == $tab ) ? ' nav-tab-active' : '';
220
+ echo "<a href='{$base_url}{$tab}' class='nav-tab {$active}'>{$title}</a>";
221
+ }
222
+ echo '</h2>';
223
+ echo '<p class="description">';
224
+ _e('Use the help dropdown button for additional information.', PostSnippets::TEXT_DOMAIN);
225
+ echo '</p>';
226
+
227
+ // Tab content
228
+ if ($active_tab == 'snippets') {
229
+ $this->tabSnippets();
230
+ } else {
231
+ $this->tabTools();
232
+ }
233
+
234
+ // Close it
235
+ echo '</div>';
236
+ }
237
+
238
+ /**
239
+ * Tab to Manage Snippets.
240
+ *
241
+ * @since Post Snippets 2.0
242
+ */
243
+ private function tabSnippets()
244
+ {
245
+ $data = array();
246
+ echo PostSnippets_View::render('admin_snippets', $data);
247
+ }
248
+
249
+ /**
250
+ * Tab for Import/Export
251
+ *
252
+ * @since Post Snippets 2.0
253
+ */
254
+ private function tabTools()
255
+ {
256
+ $ie = new PostSnippets_ImportExport();
257
+
258
+ // Create header and export html form
259
+ printf("<h3>%s</h3>", __('Import/Export', PostSnippets::TEXT_DOMAIN));
260
+ printf("<h4>%s</h4>", __('Export', PostSnippets::TEXT_DOMAIN));
261
+ echo '<form method="post">';
262
+ echo '<p>';
263
+ _e('Export your snippets for backup or to import them on another site.', PostSnippets::TEXT_DOMAIN);
264
+ echo '</p>';
265
+ printf("<input type='submit' class='button' name='postsnippets_export' value='%s' />", __('Export Snippets', PostSnippets::TEXT_DOMAIN));
266
+ echo '</form>';
267
+
268
+ // Export logic, and import html form and logic
269
+ $ie->exportSnippets();
270
+ echo $ie->importSnippets();
271
+ }
272
+
273
+
274
+ /**
275
+ * Creates a read-only overview page.
276
+ *
277
+ * For users with edit_posts capability but without manage_options
278
+ * capability.
279
+ *
280
+ * @since Post Snippets 1.9.7
281
+ */
282
+ private function overviewPage()
283
+ {
284
+ // Header
285
+ echo '<div class="wrap">';
286
+ echo '<h2>Post Snippets</h2>';
287
+ echo '<p>';
288
+ _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.', PostSnippets::TEXT_DOMAIN);
289
+ echo '</p>';
290
+
291
+ // Form
292
+ $this->setUserOptions();
293
+ $render = $this->getUserOptions();
294
+
295
+ echo '<form method="post" action="">';
296
+ wp_nonce_field('post_snippets_user_options', 'post_snippets_user_nonce');
297
+
298
+ $this->checkbox(__('Display rendered snippets', PostSnippets::TEXT_DOMAIN), 'render', $render);
299
+ $this->submit('update-post-snippets-user', __('Update', PostSnippets::TEXT_DOMAIN));
300
+ echo '</form>';
301
+
302
+ // Snippet List
303
+ $snippets = get_option(PostSnippets::OPTION_KEY);
304
+ if (!empty($snippets)) {
305
+ foreach ($snippets as $key => $snippet) {
306
+
307
+ echo "<hr style='border: none;border-top:1px dashed #aaa; margin:24px 0;' />";
308
+
309
+ echo "<h3>{$snippet['title']}";
310
+ if ($snippet['description']) {
311
+ echo "<span class='description'> {$snippet['description']}</span>";
312
+ }
313
+ echo "</h3>";
314
+
315
+ if ($snippet['vars']) {
316
+ printf("<strong>%s:</strong> {$snippet['vars']}<br/>", __('Variables', PostSnippets::TEXT_DOMAIN));
317
+ }
318
+
319
+ // echo "<strong>Variables:</strong> {$snippet['vars']}<br/>";
320
+
321
+ $options = array();
322
+ if ($snippet['shortcode']) {
323
+ array_push($options, 'Shortcode');
324
+ }
325
+ if ($snippet['php']) {
326
+ array_push($options, 'PHP');
327
+ }
328
+ if ($snippet['wptexturize']) {
329
+ array_push($options, 'wptexturize');
330
+ }
331
+ if ($options) {
332
+ printf("<strong>%s:</strong> %s<br/>", __('Options', PostSnippets::TEXT_DOMAIN), implode(', ', $options));
333
+ }
334
+
335
+ printf("<br/><strong>%s:</strong><br/>", __('Snippet', PostSnippets::TEXT_DOMAIN));
336
+ if ($render) {
337
+ echo do_shortcode($snippet['snippet']);
338
+ } else {
339
+ echo "<code>";
340
+ echo nl2br(htmlspecialchars($snippet['snippet'], ENT_NOQUOTES));
341
+ echo "</code>";
342
+ }
343
+ }
344
+ }
345
+
346
+ // Close
347
+ echo '</div>';
348
+ }
349
+
350
+
351
+ // -------------------------------------------------------------------------
352
+ // HTML and Form element methods
353
+ // -------------------------------------------------------------------------
354
 
355
+ /**
356
+ * Checkbox.
357
+ *
358
+ * Renders the HTML for an input checkbox.
359
+ *
360
+ * @param string $label The label rendered to screen
361
+ * @param string $name The unique name and id to identify the input
362
+ * @param boolean $checked If the input is checked or not
363
+ */
364
+ public static function checkbox($label, $name, $checked)
365
+ {
366
+ echo "<label for=\"{$name}\">";
367
+ printf('<input type="checkbox" name="%1$s" id="%1$s" value="true"', $name);
368
+ if ($checked) {
369
+ echo ' checked';
370
+ }
371
+ echo ' />';
372
+ echo " {$label}</label><br/>";
373
+ }
374
+
375
+ /**
376
+ * Submit.
377
+ *
378
+ * Renders the HTML for a submit button.
379
+ *
380
+ * @since Post Snippets 1.9.7
381
+ * @param string $name The name that identifies the button on submit
382
+ * @param string $label The label rendered on the button
383
+ * @param string $class Optional. Button class. Default: button-primary
384
+ * @param boolean $wrap Optional. Wrap in a submit div. Default: true
385
+ */
386
+ public static function submit($name, $label, $class = 'button-primary', $wrap = true)
387
+ {
388
+ $btn = sprintf('<input type="submit" name="%s" value="%s" class="%s" />', $name, $label, $class);
389
+
390
+ if ($wrap) {
391
+ $btn = "<div class=\"submit\">{$btn}</div>";
392
+ }
393
+
394
+ echo $btn;
395
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
  }
lib/PostSnippets/Help.php CHANGED
@@ -1,270 +1,111 @@
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 PostSnippets_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
- // parent::__construct();
22
- // If WordPress is 3.3 or higher, use the new Help API, otherwise call
23
- // the old contextual help action.
24
- global $wp_version;
25
- if ( version_compare($wp_version, '3.3', '>=') ) {
26
- add_action( 'load-' . $option_page, array(&$this,'add_help_tabs') );
27
- } else {
28
- add_action( 'contextual_help', array(&$this,'add_help'), 10, 3 );
29
- }
30
- }
31
-
32
- /**
33
- * Setup the help tabs and sidebar.
34
- *
35
- * @since Post Snippets 1.8.9
36
- */
37
- public function add_help_tabs() {
38
- $screen = get_current_screen();
39
- $screen->set_help_sidebar( $this->help_sidebar() );
40
- $screen->add_help_tab( array(
41
- 'id' => 'basic-plugin-help',
42
- 'title' => __( 'Basic', 'post-snippets' ),
43
- 'content' => $this->help_basic()
44
- ) );
45
- $screen->add_help_tab( array(
46
- 'id' => 'shortcode-plugin-help',
47
- 'title' => __( 'Shortcode', 'post-snippets' ),
48
- 'content' => $this->help_shortcode()
49
- ) );
50
- if ( PostSnippets::canExecutePHP() ) {
51
- $screen->add_help_tab( array(
52
- 'id' => 'php-plugin-help',
53
- 'title' => __( 'PHP', 'post-snippets' ),
54
- 'content' => $this->help_php()
55
- ) );
56
- }
57
- $screen->add_help_tab( array(
58
- 'id' => 'advanced-plugin-help',
59
- 'title' => __( 'Advanced', 'post-snippets' ),
60
- 'content' => $this->help_advanced()
61
- ) );
62
- }
63
-
64
- /**
65
- * The right sidebar help text.
66
- *
67
- * @since Post Snippets 1.8.9
68
- * @return string The help text
69
- */
70
- public function help_sidebar()
71
- {
72
- return '<p><strong>'.
73
- __( 'For more information:', 'post-snippets' ).
74
- '</strong></p>
75
-
76
- <p><a href="http://wpstorm.net/wordpress-plugins/post-snippets/" target="_blank">'.
77
- __( 'Post Snippets Documentation', 'post-snippets' ).
78
- '</a></p>
79
-
80
- <p><a href="http://wordpress.org/tags/post-snippets?forum_id=10" target="_blank">'.
81
- __( 'Support Forums', 'post-snippets' ).
82
- '</a></p>';
83
- }
84
-
85
- /**
86
- * The basic help tab.
87
- *
88
- * @since Post Snippets 1.9.1
89
- * @return string The help text
90
- */
91
- public function help_basic()
92
- {
93
- return '<h2>'.
94
- __( 'Title', 'post-snippets' ).
95
- '</h2>
96
- <p>'.
97
- __( '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' ).
98
- '</p>
99
-
100
- <h2>'.
101
- __( 'Variables', 'post-snippets' ).
102
- '</h2>
103
- <p>'.
104
- __( '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' ).
105
- '</p>
106
- <p><strong>'.
107
- __( 'Example', 'post-snippets' ).
108
- '</strong><br/>
109
- <code>url,name,role=user,title</code></p>'.
110
-
111
- '<h2>'.
112
- __( 'Snippet', 'post-snippets' ).
113
- '</h2>
114
- <p>'.
115
- __('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' ).
116
- '</p>
117
- <p><strong>'.
118
- __( 'Example', 'post-snippets' ).
119
- '</strong><br/>'.
120
- __( 'To reference the variables in the example above, you would enter {url} and {name}. So if you enter this snippet:', 'post-snippets' ).
121
- '<br/>
122
- <code>This is the website of &lt;a href="{url}"&gt;{name}&lt;/a&gt;</code>
123
- <br/>'.
124
- __( 'You will get the option to replace url and name on insert if they are defined as variables.', 'post-snippets').
125
- '</p>
126
-
127
- <h2>'
128
- . __( 'Description', 'post-snippets' ).
129
- '</h2>
130
- <p>'.
131
- __( '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').
132
- '</p>';
133
- }
134
-
135
- /**
136
- * The shortcode help tab.
137
- *
138
- * @since Post Snippets 1.9.1
139
- * @return string The help text
140
- */
141
- public function help_shortcode()
142
- {
143
- return '<p>'.
144
- __( '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' ).
145
- '</p>
146
-
147
- <p>'.
148
- __( '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' ).
149
- '</p>
150
- <p>'.
151
- __( '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' ).
152
- '</p>
153
-
154
- <h2>'
155
- . __( 'Options', 'post-snippets' ).
156
- '</h2>
157
- <p><strong>PHP</strong><br/>'.
158
- __( 'See the dedicated help section for information about PHP shortcodes.', 'post-snippets' ).
159
- '</p>
160
- <p><strong>wptexturize</strong><br/>'.
161
- 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>' ).
162
- '</p>';
163
- }
164
-
165
- /**
166
- * The PHP help tab.
167
- *
168
- * @since Post Snippets 1.9.1
169
- * @return string The help text
170
- */
171
- public function help_php()
172
- {
173
- return '<p>'.
174
- __('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' ).
175
- '</p>
176
- <p><strong>'.
177
- __( 'Example PHP Snippet', 'post-snippets' ).
178
- '</strong><br/>
179
- <code>
180
- for ($i=1; $i<5; $i++) {<br/>
181
- echo "{loop_me}&lt;br/&gt;";<br/>
182
- };
183
- </code></p>
184
-
185
- <p>'.
186
- __( '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' ).
187
- '</p>
188
-
189
- <code>[phpcode loop_me="post snippet with PHP!"]</code>
190
-
191
- <p>'.
192
- __( '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' ).
193
- '</p>
194
- <p>'.
195
- __( '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' ).
196
- '</p>';
197
- }
198
-
199
- /**
200
- * The advanced help tab.
201
- *
202
- * @since Post Snippets 1.9.1
203
- * @return string The help text
204
- */
205
- public function help_advanced()
206
- {
207
- return '<p>'.
208
- __('You can retrieve a Post Snippet directly from PHP, in a theme for instance, by using the get_post_snippet() function.', 'post-snippets').
209
- '</p>
210
-
211
- <h2>'.
212
- __('Usage', 'post-snippets').
213
- '</h2>
214
- <p>'.
215
- '<code>
216
- &lt;?php $my_snippet = get_post_snippet( $snippet_name, $snippet_vars ); ?&gt;
217
- </code></p>
218
-
219
- <h2>'.
220
- __('Parameters', 'post-snippets').
221
- '</h2>
222
- <p>
223
- <code>$snippet_name</code><br/>'.
224
- __('(string) (required) The name of the snippet to retrieve.', 'post-snippets').
225
-
226
- '<br/><br/><code>'.
227
- '$snippet_vars
228
- </code><br/>'.
229
- __('(string) The variables to pass to the snippet, formatted as a query string.', 'post-snippets').
230
- '</p>
231
-
232
- <h2>'.
233
- __('Example', 'post-snippets').
234
- '</h2>
235
- <p><code>
236
- &lt;?php<br/>
237
- $my_snippet = get_post_snippet( \'internal-link\', \'title=Awesome&url=2011/02/awesome/\' );<br/>
238
- echo $my_snippet;<br/>
239
- ?&gt;
240
- </code></p>';
241
- }
242
-
243
-
244
- // -------------------------------------------------------------------------
245
- // For compability with WordPress before v3.3.
246
- // -------------------------------------------------------------------------
247
-
248
- /**
249
- * Contextual Help for WP < v3.3.
250
- *
251
- * Combines the help tabs above into one long help text for the help tab
252
- * when run on WordPress versions before v3.3.
253
- *
254
- * @since Post Snippets 1.7.1
255
- * @return string The Contextual Help
256
- */
257
- public function add_help($contextual_help, $screen_id, $screen) {
258
- if ( $screen->id == 'settings_page_post-snippets/post-snippets' ) {
259
- $contextual_help = '<h1>'.__( 'Basic', 'post-snippets' ).'</h1>';
260
- $contextual_help .= $this->help_basic();
261
- $contextual_help .= '<h1>'.__( 'Shortcode', 'post-snippets' ).'</h1>';
262
- $contextual_help .= $this->help_shortcode();
263
- $contextual_help .= '<h1>'.__( 'PHP', 'post-snippets' ).'</h1>';
264
- $contextual_help .= $this->help_php();
265
- $contextual_help .= '<h1>'.__( 'Advanced', 'post-snippets' ).'</h1>';
266
- $contextual_help .= $this->help_advanced();
267
- }
268
- return $contextual_help;
269
- }
270
  }
1
  <?php
2
  /**
3
+ * Handles the plugin help screen.
4
  *
5
+ * @author Johan Steen <artstorm at gmail dot com>
6
+ * @link http://johansteen.se/
 
 
 
7
  */
8
  class PostSnippets_Help
9
  {
10
+ /**
11
+ * Constructor.
12
+ *
13
+ * @since Post Snippets 1.8.9
14
+ * @param string The option page to load the help text on
15
+ */
16
+ public function __construct($optionPage)
17
+ {
18
+ add_action('load-'.$optionPage, array(&$this,'addHelpTabs'));
19
+ }
20
+
21
+ /**
22
+ * Setup the help tabs and sidebar.
23
+ *
24
+ * @since Post Snippets 1.8.9
25
+ */
26
+ public function addHelpTabs()
27
+ {
28
+ $screen = get_current_screen();
29
+ $screen->set_help_sidebar($this->helpSidebar());
30
+ $screen->add_help_tab(
31
+ array(
32
+ 'id' => 'basic-plugin-help',
33
+ 'title' => __('Basic', PostSnippets::TEXT_DOMAIN),
34
+ 'content' => $this->helpBasic()
35
+ )
36
+ );
37
+ $screen->add_help_tab(
38
+ array(
39
+ 'id' => 'shortcode-plugin-help',
40
+ 'title' => __('Shortcode', PostSnippets::TEXT_DOMAIN),
41
+ 'content' => $this->helpShortcode()
42
+ )
43
+ );
44
+ if (PostSnippets::canExecutePHP()) {
45
+ $screen->add_help_tab(
46
+ array(
47
+ 'id' => 'php-plugin-help',
48
+ 'title' => __('PHP', PostSnippets::TEXT_DOMAIN),
49
+ 'content' => $this->helpPhp()
50
+ )
51
+ );
52
+ }
53
+ $screen->add_help_tab(
54
+ array(
55
+ 'id' => 'advanced-plugin-help',
56
+ 'title' => __('Advanced', PostSnippets::TEXT_DOMAIN),
57
+ 'content' => $this->helpAdvanced()
58
+ )
59
+ );
60
+ }
61
+
62
+ /**
63
+ * The right sidebar help text.
64
+ *
65
+ * @return string The help text
66
+ */
67
+ public function helpSidebar()
68
+ {
69
+ return PostSnippets_View::render('help_sidebar');
70
+ }
71
+
72
+ /**
73
+ * The basic help tab.
74
+ *
75
+ * @return string The help text
76
+ */
77
+ public function helpBasic()
78
+ {
79
+ return PostSnippets_View::render('help_basic');
80
+ }
81
+
82
+ /**
83
+ * The shortcode help tab.
84
+ *
85
+ * @return string The help text
86
+ */
87
+ public function helpShortcode()
88
+ {
89
+ return PostSnippets_View::render('help_shortcode');
90
+ }
91
+
92
+ /**
93
+ * The PHP help tab.
94
+ *
95
+ * @return string The help text
96
+ */
97
+ public function helpPhp()
98
+ {
99
+ return PostSnippets_View::render('help_php');
100
+ }
101
+
102
+ /**
103
+ * The advanced help tab.
104
+ *
105
+ * @return string The help text
106
+ */
107
+ public function helpAdvanced()
108
+ {
109
+ return PostSnippets_View::render('help_advanced');
110
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  }
lib/PostSnippets/ImportExport.php CHANGED
@@ -4,133 +4,190 @@
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 PostSnippets_ImportExport
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( PostSnippets::optionDBKey() ));
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( PostSnippets::optionDBKey(), 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
  }
4
  *
5
  * Class to handle import and export of Snippets.
6
  *
7
+ * @author Johan Steen <artstorm at gmail dot com>
8
+ * @link http://johansteen.se/
 
9
  */
10
  class PostSnippets_ImportExport
11
  {
12
+ const FILE_CFG = 'post-snippets-export.cfg';
13
+ const FILE_ZIP = 'post-snippets-export.zip';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ private $downloadUrl;
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ /**
18
+ * Export Snippets.
19
+ *
20
+ * Check if an export file shall be created, or if a download url should be
21
+ * pushed to the footer. Also checks for old export files laying around and
22
+ * deletes them (for security).
23
+ *
24
+ * @return void
25
+ */
26
+ public function exportSnippets()
27
+ {
28
+ if (isset($_POST['postsnippets_export'])) {
29
+ $url = $this->createExportFile();
30
+ if ($url) {
31
+ $this->downloadUrl = $url;
32
+ add_action(
33
+ 'admin_footer',
34
+ array(&$this, 'psnippetsFooter'),
35
+ 10000
36
+ );
37
+ } else {
38
+ echo __('Error: ', PostSnippets::TEXT_DOMAIN).$url;
39
+ }
40
+ } else {
41
+ // Check if there is any old export files to delete
42
+ $dir = wp_upload_dir();
43
+ $upload_dir = $dir['basedir'] . '/';
44
+ chdir($upload_dir);
45
+ if (file_exists('./'.self::FILE_ZIP)) {
46
+ unlink('./'.self::FILE_ZIP);
47
+ }
48
+ }
49
+ }
50
 
51
+ /**
52
+ * Handles uploading of post snippets archive and import the snippets.
53
+ *
54
+ * @uses wp_handle_upload() in wp-admin/includes/file.php
55
+ * @return string HTML to handle the import
56
+ */
57
+ public function importSnippets()
58
+ {
59
+ $import =
60
+ '<br/><br/><strong>'.
61
+ __('Import', PostSnippets::TEXT_DOMAIN).
62
+ '</strong><br/>';
63
+ if (!isset($_FILES['postsnippets_import_file'])
64
+ || empty($_FILES['postsnippets_import_file'])
65
+ ) {
66
+ $import .=
67
+ '<p>'.__('Import snippets from a post-snippets-export.zip file. Importing overwrites any existing snippets.', PostSnippets::TEXT_DOMAIN).
68
+ '</p>';
69
+ $import .= '<form method="post" enctype="multipart/form-data">';
70
+ $import .= '<input type="file" name="postsnippets_import_file"/>';
71
+ $import .= '<input type="hidden" name="action" value="wp_handle_upload"/>';
72
+ $import .=
73
+ '<input type="submit" class="button" value="'.
74
+ __('Import Snippets', PostSnippets::TEXT_DOMAIN).'"/>';
75
+ $import .= '</form>';
76
+ } else {
77
+ $file = wp_handle_upload($_FILES['postsnippets_import_file']);
78
+
79
+ if (isset($file['file']) && !is_wp_error($file)) {
80
+ require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
81
+ $zip = new PclZip($file['file']);
82
+ $dir = wp_upload_dir();
83
+ $upload_dir = $dir['basedir'] . '/';
84
+ chdir($upload_dir);
85
+ $unzipped = $zip->extract();
86
 
87
+ if ($unzipped[0]['stored_filename'] == self::FILE_CFG
88
+ && $unzipped[0]['status'] == 'ok'
89
+ ) {
90
+ // Delete the uploaded archive
91
+ unlink($file['file']);
92
 
93
+ $snippets = file_get_contents(
94
+ $upload_dir.self::FILE_CFG
95
+ );
 
 
96
 
97
+ if ($snippets) {
98
+ $snippets = apply_filters(
99
+ 'post_snippets_import',
100
+ $snippets
101
+ );
102
+ update_option(
103
+ PostSnippets::OPTION_KEY,
104
+ unserialize($snippets)
105
+ );
106
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ // Delete the snippet file
109
+ unlink('./'.self::FILE_CFG);
 
110
 
111
+ $import .=
112
+ '<p><strong>'.
113
+ __('Snippets successfully imported.', PostSnippets::TEXT_DOMAIN).
114
+ '</strong></p>';
115
+ } else {
116
+ $import .=
117
+ '<p><strong>'.
118
+ __('Snippets could not be imported:', PostSnippets::TEXT_DOMAIN).
119
+ ' '.
120
+ __('Unzipping failed.', PostSnippets::TEXT_DOMAIN).
121
+ '</strong></p>';
122
+ }
123
+ } else {
124
+ if ($file['error'] || is_wp_error($file)) {
125
+ $import .=
126
+ '<p><strong>'.
127
+ __('Snippets could not be imported:', PostSnippets::TEXT_DOMAIN).
128
+ ' '.
129
+ $file['error'].'</strong></p>';
130
+ } else {
131
+ $import .=
132
+ '<p><strong>'.
133
+ __('Snippets could not be imported:', PostSnippets::TEXT_DOMAIN).
134
+ ' '.
135
+ __('Upload failed.', PostSnippets::TEXT_DOMAIN).
136
+ '</strong></p>';
137
+ }
138
+ }
139
+ }
140
+ return $import;
141
+ }
142
 
143
+ /**
144
+ * Create a zipped filed containing all Post Snippets, for export.
145
+ *
146
+ * @return string URL to the exported snippets
147
+ */
148
+ private function createExportFile()
149
+ {
150
+ $snippets = serialize(get_option(PostSnippets::OPTION_KEY));
151
+ $snippets = apply_filters('post_snippets_export', $snippets);
152
+ $dir = wp_upload_dir();
153
+ $upload_dir = $dir['basedir'] . '/';
154
+ $upload_url = $dir['baseurl'] . '/';
155
+
156
+ // Open a file stream and write the serialized options to it.
157
+ if (!$handle = fopen($upload_dir.'./'.self::FILE_CFG, 'w')) {
158
+ die();
159
+ }
160
+ if (!fwrite($handle, $snippets)) {
161
+ die();
162
+ }
163
+ fclose($handle);
164
 
165
+ // Create a zip archive
166
+ require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
167
+ chdir($upload_dir);
168
+ $zip = new PclZip('./'.self::FILE_ZIP);
169
+ $zipped = $zip->create('./'.self::FILE_CFG);
170
+
171
+ // Delete the snippet file
172
+ unlink('./'.self::FILE_CFG);
173
+
174
+ if (!$zipped) {
175
+ return false;
176
+ }
177
+
178
+ return $upload_url.'./'.self::FILE_ZIP;
179
+ }
180
+
181
+ /**
182
+ * Generates the javascript to trigger the download of the file.
183
+ *
184
+ * @return void
185
+ */
186
+ public function psnippetsFooter()
187
+ {
188
+ $export = '<script type="text/javascript">
189
+ document.location = \''.$this->downloadUrl.'\';
190
+ </script>';
191
+ echo $export;
192
+ }
193
  }
lib/PostSnippets/View.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * MVC like View Handling in WordPress.
4
+ *
5
+ * @author Johan Steen <artstorm at gmail dot com>
6
+ * @link http://johansteen.se/
7
+ */
8
+ class PostSnippets_View
9
+ {
10
+ /**
11
+ * Render a View.
12
+ *
13
+ * @param string $view View to render.
14
+ * @param array $data Data to be used within the view.
15
+ * @return string The processed view.
16
+ */
17
+ public static function render($view, $data = null)
18
+ {
19
+ // Handle data
20
+ ($data) ? extract($data) : null;
21
+
22
+ ob_start();
23
+ include(plugin_dir_path(__FILE__).'../../views/'.$view.'.php');
24
+ $view = ob_get_contents();
25
+ ob_end_clean();
26
+
27
+ return $view;
28
+ }
29
+ }
phpunit.xml DELETED
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <phpunit
3
- colors="true"
4
- stopOnFailure="false"
5
- bootstrap="unittest/bootstrap.php">
6
- backupGlobals="false"
7
- <testsuites>
8
- <testsuite name="complete">
9
- <directory>unittest</directory>
10
- </testsuite>
11
- </testsuites>
12
- </phpunit>
 
 
 
 
 
 
 
 
 
 
 
 
post-snippets.php CHANGED
@@ -1,11 +1,11 @@
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
  Author: Johan Steen
7
  Author URI: http://johansteen.se/
8
- Version: 2.1.1
9
  License: GPLv2 or later
10
  Text Domain: post-snippets
11
 
@@ -30,22 +30,22 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30
  spl_autoload_register('PostSnippets::autoload');
31
 
32
  /**
33
- * Init Singleton Class for Post Snippets.
34
  *
35
- * @package Post Snippets
36
  * @author Johan Steen <artstorm at gmail dot com>
 
37
  */
38
  class PostSnippets
39
  {
40
  private static $instance = false;
41
 
42
- const MIN_PHP_VERSION = '5.2.4';
43
- const MIN_WP_VERSION = '3.0';
44
- const OPTION_DB_KEY = 'post_snippets_options';
45
- const USER_META_KEY = 'post_snippets';
46
-
47
- // Constants
48
- const TINYMCE_PLUGIN_NAME = 'post_snippets';
49
 
50
  /**
51
  * Singleton class
@@ -59,12 +59,10 @@ class PostSnippets
59
  }
60
 
61
  /**
62
- * Constructor.
63
- * Initializes the plugin by setting localization, filters, and
64
- * administration functions.
65
  */
66
- private function __construct()
67
- {
68
  if (!$this->testHost()) {
69
  return;
70
  }
@@ -72,49 +70,40 @@ class PostSnippets
72
  add_action('init', array($this, 'textDomain'));
73
  register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
74
 
75
- // Add TinyMCE button
76
- add_action('init', array(&$this, 'add_tinymce_button') );
77
-
78
- // Settings link on plugins list
79
- add_filter( 'plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
80
- // Options Page
81
- add_action( 'admin_menu', array(&$this,'wp_admin') );
82
-
83
- $this->create_shortcodes();
84
-
85
- // Adds the JS and HTML code in the header and footer for the jQuery
86
- // insert UI dialog in the editor
87
- add_action( 'admin_init', array(&$this,'enqueue_assets') );
88
- add_action( 'admin_head', array(&$this,'jquery_ui_dialog') );
89
- add_action( 'admin_footer', array(&$this,'add_jquery_ui_dialog') );
90
-
91
- // Add Editor QuickTag button:
92
- // IF WordPress is 3.3 or higher, use the new refactored method to add
93
- // the quicktag button.
94
- // Start showing a deprecated message from version 1.9 of the plugin for
95
- // the old method. And remove it completely when the plugin hits 2.0.
96
- global $wp_version;
97
- if ( version_compare($wp_version, '3.3', '>=') ) {
98
- add_action( 'admin_print_footer_scripts',
99
- array(&$this,'add_quicktag_button'), 100 );
100
- } else {
101
- add_action( 'edit_form_advanced', array(&$this,'add_quicktag_button_pre33') );
102
- add_action( 'edit_page_form', array(&$this,'add_quicktag_button_pre33') );
103
- }
104
- }
105
 
106
  /**
107
  * PSR-0 compliant autoloader to load classes as needed.
108
  *
109
- * @since 2.1
110
- *
111
  * @param string $classname The name of the class
112
  * @return null Return early if the class name does not start with the
113
  * correct prefix
114
  */
115
  public static function autoload($className)
116
  {
117
- if ('PostSnippets' !== mb_substr($className, 0, 12)) {
118
  return;
119
  }
120
  $className = ltrim($className, '\\');
@@ -133,11 +122,11 @@ class PostSnippets
133
  }
134
 
135
  /**
136
- * Loads the text domain for translation
137
  */
138
  public function textDomain()
139
  {
140
- $domain = 'post-snippets';
141
  $locale = apply_filters('plugin_locale', get_locale(), $domain);
142
  load_textdomain(
143
  $domain,
@@ -155,621 +144,526 @@ class PostSnippets
155
  */
156
  public function uninstall()
157
  {
158
- // Delete all snippets
159
- delete_option('post_snippets_options');
160
-
161
- // Delete any per user settings
162
- global $wpdb;
163
- $wpdb->query(
164
- "
165
- DELETE FROM $wpdb->usermeta
166
- WHERE meta_key = 'post_snippets'
167
- "
168
- );
169
- }
170
-
171
- /**
172
- * Quick link to the Post Snippets Settings page from the Plugins page.
173
- *
174
- * @return Array with all the plugin's action links
175
- */
176
- function plugin_action_links( $links, $file ) {
177
- if ( $file == plugin_basename( dirname(__FILE__).'/post-snippets.php' ) ) {
178
- $links[] = '<a href="options-general.php?page=post-snippets/post-snippets.php">'.__('Settings', 'post-snippets').'</a>';
179
- }
180
- return $links;
181
- }
182
-
183
-
184
- /**
185
- * Enqueues the necessary scripts and styles for the plugins
186
- *
187
- * @since Post Snippets 1.7
188
- */
189
- function enqueue_assets() {
190
- wp_enqueue_script( 'jquery-ui-dialog' );
191
- wp_enqueue_script( 'jquery-ui-tabs' );
192
- wp_enqueue_style( 'wp-jquery-ui-dialog' );
193
-
194
- # Adds the CSS stylesheet for the jQuery UI dialog
195
- $style_url = plugins_url( '/assets/post-snippets.css', __FILE__ );
196
- wp_register_style( 'post-snippets', $style_url, false, '2.0' );
197
- wp_enqueue_style( 'post-snippets' );
198
- }
199
-
200
-
201
- // -------------------------------------------------------------------------
202
- // WordPress Editor Buttons
203
- // -------------------------------------------------------------------------
204
-
205
- /**
206
- * Add TinyMCE button.
207
- *
208
- * Adds filters to add custom buttons to the TinyMCE editor (Visual Editor)
209
- * in WordPress.
210
- *
211
- * @since Post Snippets 1.8.7
212
- */
213
- public function add_tinymce_button()
214
- {
215
- // Don't bother doing this stuff if the current user lacks permissions
216
- if ( !current_user_can('edit_posts') &&
217
- !current_user_can('edit_pages') )
218
- return;
219
-
220
- // Add only in Rich Editor mode
221
- if ( get_user_option('rich_editing') == 'true') {
222
- add_filter('mce_external_plugins',
223
- array(&$this, 'register_tinymce_plugin') );
224
- add_filter('mce_buttons',
225
- array(&$this, 'register_tinymce_button') );
226
- }
227
- }
228
-
229
- /**
230
- * Register TinyMCE button.
231
- *
232
- * Pushes the custom TinyMCE button into the array of with button names.
233
- * 'separator' or '|' can be pushed to the array as well. See the link
234
- * for all available TinyMCE controls.
235
- *
236
- * @see wp-includes/class-wp-editor.php
237
- * @link http://www.tinymce.com/wiki.php/Buttons/controls
238
- * @since Post Snippets 1.8.7
239
- *
240
- * @param array $buttons Filter supplied array of buttons to modify
241
- * @return array The modified array with buttons
242
- */
243
- public function register_tinymce_button( $buttons )
244
- {
245
- array_push( $buttons, 'separator', self::TINYMCE_PLUGIN_NAME );
246
- return $buttons;
247
- }
248
-
249
- /**
250
- * Register TinyMCE plugin.
251
- *
252
- * Adds the absolute URL for the TinyMCE plugin to the associative array of
253
- * plugins. Array structure: 'plugin_name' => 'plugin_url'
254
- *
255
- * @see wp-includes/class-wp-editor.php
256
- * @since Post Snippets 1.8.7
257
- *
258
- * @param array $plugins Filter supplied array of plugins to modify
259
- * @return array The modified array with plugins
260
- */
261
- public function register_tinymce_plugin( $plugins )
262
- {
263
- // Load the TinyMCE plugin, editor_plugin.js, into the array
264
- $plugins[self::TINYMCE_PLUGIN_NAME] =
265
- plugins_url('/tinymce/editor_plugin.js?ver=1.9', __FILE__);
266
-
267
- return $plugins;
268
- }
269
-
270
- /**
271
- * Adds a QuickTag button to the HTML editor.
272
- *
273
- * Compatible with WordPress 3.3 and newer.
274
- *
275
- * @see wp-includes/js/quicktags.dev.js -> qt.addButton()
276
- * @since Post Snippets 1.8.6
277
- */
278
- public function add_quicktag_button()
279
- {
280
- // Only run the function on post edit screens
281
- if ( function_exists( 'get_current_screen' ) ) {
282
- $screen = get_current_screen();
283
- if ($screen->base != 'post')
284
- return;
285
- }
286
-
287
- echo "\n<!-- START: Add QuickTag button for Post Snippets -->\n";
288
- ?>
289
- <script type="text/javascript" charset="utf-8">
290
- QTags.addButton( 'post_snippets_id', 'Post Snippets', qt_post_snippets );
291
- function qt_post_snippets() {
292
- post_snippets_caller = 'html';
293
- jQuery( "#post-snippets-dialog" ).dialog( "open" );
294
- }
295
- </script>
296
- <?php
297
- echo "\n<!-- END: Add QuickTag button for Post Snippets -->\n";
298
- }
299
-
300
-
301
- /**
302
- * Adds a QuickTag button to the HTML editor.
303
- *
304
- * Used when running on WordPress lower than version 3.3.
305
- *
306
- * @see wp-includes/js/quicktags.dev.js
307
- * @since Post Snippets 1.7
308
- * @deprecated Since 1.8.6
309
- */
310
- function add_quicktag_button_pre33() {
311
- // Only run the function on post edit screens
312
- if ( function_exists( 'get_current_screen' ) ) {
313
- $screen = get_current_screen();
314
- if ($screen->base != 'post')
315
- return;
316
- }
317
-
318
- echo "\n<!-- START: Post Snippets QuickTag button -->\n";
319
- ?>
320
- <script type="text/javascript" charset="utf-8">
321
- // <![CDATA[
322
- //edButton(id, display, tagStart, tagEnd, access, open)
323
- edbuttonlength = edButtons.length;
324
- edButtons[edbuttonlength++] = new edButton('ed_postsnippets', 'Post Snippets', '', '', '', -1);
325
- (function(){
326
- if (typeof jQuery === 'undefined') {
327
- return;
328
- }
329
- jQuery(document).ready(function(){
330
- jQuery("#ed_toolbar").append('<input type="button" value="Post Snippets" id="ed_postsnippets" class="ed_button" onclick="edOpenPostSnippets(edCanvas);" title="Post Snippets" />');
331
- });
332
- }());
333
- // ]]>
334
- </script>
335
- <?php
336
- echo "\n<!-- END: Post Snippets QuickTag button -->\n";
337
- }
338
-
339
-
340
- // -------------------------------------------------------------------------
341
- // JavaScript / jQuery handling for the post editor
342
- // -------------------------------------------------------------------------
343
-
344
- /**
345
- * jQuery control for the dialog and Javascript needed to insert snippets into the editor
346
- *
347
- * @since Post Snippets 1.7
348
- */
349
- public function jquery_ui_dialog()
350
- {
351
- // Only run the function on post edit screens
352
- if ( function_exists( 'get_current_screen' ) ) {
353
- $screen = get_current_screen();
354
- if ($screen->base != 'post')
355
- return;
356
- }
357
-
358
- echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
359
- echo "<script type='text/javascript'>\n";
360
-
361
- # Prepare the snippets and shortcodes into javascript variables
362
- # so they can be inserted into the editor, and get the variables replaced
363
- # with user defined strings.
364
- $snippets = get_option( self::OPTION_DB_KEY );
365
- foreach ($snippets as $key => $snippet) {
366
- if ($snippet['shortcode']) {
367
- # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
368
- # so {varnameX} can be replaced at runtime.
369
- $var_arr = explode(",",$snippet['vars']);
370
- $variables = '';
371
- if (!empty($var_arr[0])) {
372
- foreach ($var_arr as $var) {
373
- // '[test2 yet="{yet}" mupp=per="{mupp=per}" content="{content}"]';
374
- $var = $this->strip_default_val( $var );
375
-
376
- $variables .= ' ' . $var . '="{' . $var . '}"';
377
- }
378
- }
379
- $shortcode = $snippet['title'] . $variables;
380
- echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
381
- } else {
382
- // To use $snippet is probably not a good naming convention here.
383
- // rename to js_snippet or something?
384
- $snippet = $snippet['snippet'];
385
- # Fixes for potential collisions:
386
- /* Replace <> with char codes, otherwise </script> in a snippet will break it */
387
- $snippet = str_replace( '<', '\x3C', str_replace( '>', '\x3E', $snippet ) );
388
- /* Escape " with \" */
389
- $snippet = str_replace( '"', '\"', $snippet );
390
- /* Remove CR and replace LF with \n to keep formatting */
391
- $snippet = str_replace( chr(13), '', str_replace( chr(10), '\n', $snippet ) );
392
- # Print out the variable containing the snippet
393
- echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
394
- }
395
- }
396
- ?>
397
-
398
- jQuery(document).ready(function($){
399
- <?php
400
- # Create js variables for all form fields
401
- foreach ($snippets as $key => $snippet) {
402
- $var_arr = explode(",",$snippet['vars']);
403
- if (!empty($var_arr[0])) {
404
- foreach ($var_arr as $key_2 => $var) {
405
- $varname = "var_" . $key . "_" . $key_2;
406
- echo "var {$varname} = $( \"#{$varname}\" );\n";
407
- }
408
- }
409
- }
410
- ?>
411
-
412
- var $tabs = $("#post-snippets-tabs").tabs();
413
-
414
- $(function() {
415
- $( "#post-snippets-dialog" ).dialog({
416
- autoOpen: false,
417
- modal: true,
418
- dialogClass: 'wp-dialog',
419
- buttons: {
420
- Cancel: function() {
421
- $( this ).dialog( "close" );
422
- },
423
- "Insert": function() {
424
- $( this ).dialog( "close" );
425
- var selected = $tabs.tabs('option', 'selected');
426
- <?php
427
- foreach ($snippets as $key => $snippet) {
428
- ?>
429
- if (selected == <?php echo $key; ?>) {
430
- insert_snippet = postsnippet_<?php echo $key; ?>;
431
- <?php
432
- $var_arr = explode(",",$snippet['vars']);
433
- if (!empty($var_arr[0])) {
434
- foreach ($var_arr as $key_2 => $var) {
435
- $varname = "var_" . $key . "_" . $key_2; ?>
436
- insert_snippet = insert_snippet.replace(/\{<?php echo $this->strip_default_val( $var ); ?>\}/g, <?php echo $varname; ?>.val());
437
- <?php
438
- echo "\n";
439
- }
440
- }
441
- ?>
442
- }
443
- <?php
444
- }
445
- ?>
446
-
447
- // Decide what method to use to insert the snippet depending
448
- // from what editor the window was opened from
449
- if (post_snippets_caller == 'html') {
450
- // HTML editor in WordPress 3.3 and greater
451
- QTags.insertContent(insert_snippet);
452
- } else if (post_snippets_caller == 'html_pre33') {
453
- // HTML editor in WordPress below 3.3.
454
- edInsertContent(post_snippets_canvas, insert_snippet);
455
- } else {
456
- // Visual Editor
457
- post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
458
- }
459
-
460
- }
461
- },
462
- width: 500,
463
- });
464
- });
465
- });
466
-
467
- // Global variables to keep track on the canvas instance and from what editor
468
- // that opened the Post Snippets popup.
469
- var post_snippets_canvas;
470
- var post_snippets_caller = '';
471
 
472
- /**
473
- * Used in WordPress lower than version 3.3.
474
- * Not used anymore starting with WordPress version 3.3.
475
- * Called from: add_quicktag_button_pre33()
476
- */
477
- function edOpenPostSnippets(myField) {
478
- post_snippets_canvas = myField;
479
- post_snippets_caller = 'html_pre33';
480
- jQuery( "#post-snippets-dialog" ).dialog( "open" );
481
- };
482
- <?php
483
- echo "</script>\n";
484
- echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
485
- }
486
-
487
- /**
488
- * Build jQuery UI Window.
489
- *
490
- * Creates the jQuery for Post Editor popup window, its snippet tabs and the
491
- * form fields to enter variables.
492
- *
493
- * @since Post Snippets 1.7
494
- */
495
- public function add_jquery_ui_dialog()
496
- {
497
- // Only run the function on post edit screens
498
- if ( function_exists( 'get_current_screen' ) ) {
499
- $screen = get_current_screen();
500
- if ($screen->base != 'post')
501
- return;
502
- }
503
-
504
- echo "\n<!-- START: Post Snippets UI Dialog -->\n";
505
- // Setup the dialog divs
506
- echo "<div class=\"hidden\">\n";
507
- echo "\t<div id=\"post-snippets-dialog\" title=\"Post Snippets\">\n";
508
- // Init the tabs div
509
- echo "\t\t<div id=\"post-snippets-tabs\">\n";
510
- echo "\t\t\t<ul>\n";
511
-
512
- // Create a tab for each available snippet
513
- $snippets = get_option( self::OPTION_DB_KEY );
514
- foreach ($snippets as $key => $snippet) {
515
- echo "\t\t\t\t";
516
- echo "<li><a href=\"#ps-tabs-{$key}\">{$snippet['title']}</a></li>";
517
- echo "\n";
518
- }
519
- echo "\t\t\t</ul>\n";
520
-
521
- // Create a panel with form fields for each available snippet
522
- foreach ($snippets as $key => $snippet) {
523
- echo "\t\t\t<div id=\"ps-tabs-{$key}\">\n";
524
-
525
- // Print a snippet description is available
526
- if ( isset($snippet['description']) )
527
- echo "\t\t\t\t<p class=\"howto\">" . $snippet['description'] . "</p>\n";
528
-
529
- // Get all variables defined for the snippet and output them as
530
- // input fields
531
- $var_arr = explode(',', $snippet['vars']);
532
- if (!empty($var_arr[0])) {
533
- foreach ($var_arr as $key_2 => $var) {
534
- // Default value exists?
535
- $def_pos = strpos( $var, '=' );
536
- if ( $def_pos !== false ) {
537
- $split = explode( '=', $var );
538
- $var = $split[0];
539
- $def = $split[1];
540
- } else {
541
- $def = '';
542
- }
543
- echo "\t\t\t\t<label for=\"var_{$key}_{$key_2}\">{$var}:</label>\n";
544
- echo "\t\t\t\t<input type=\"text\" id=\"var_{$key}_{$key_2}\" name=\"var_{$key}_{$key_2}\" value=\"{$def}\" style=\"width: 190px\" />\n";
545
- echo "\t\t\t\t<br/>\n";
546
- }
547
- } else {
548
- // If no variables and no description available, output a text
549
- // to inform the user that it's an insert snippet only.
550
- if ( empty($snippet['description']) )
551
- echo "\t\t\t\t<p class=\"howto\">" . __('This snippet is insert only, no variables defined.', 'post-snippets') . "</p>\n";
552
- }
553
- echo "\t\t\t</div><!-- #ps-tabs-{$key} -->\n";
554
- }
555
- // Close the tabs and dialog divs
556
- echo "\t\t</div><!-- #post-snippets-tabs -->\n";
557
- echo "\t</div><!-- #post-snippets-dialog -->\n";
558
- echo "</div><!-- .hidden -->\n";
559
-
560
- echo "<!-- END: Post Snippets UI Dialog -->\n\n";
561
- }
562
-
563
- /**
564
- * Strip Default Value.
565
- *
566
- * Checks if a variable string contains a default value, and if it does it
567
- * will strip it away and return the string with only the variable name
568
- * kept.
569
- *
570
- * @since Post Snippets 1.9.3
571
- * @param string $variable The variable to check for default value
572
- * @return string The variable without any default value
573
- */
574
- public function strip_default_val( $variable )
575
- {
576
- // Check if variable contains a default defintion
577
- $def_pos = strpos( $variable, '=' );
578
-
579
- if ( $def_pos !== false ) {
580
- $split = str_split( $variable, $def_pos );
581
- $variable = $split[0];
582
- }
583
- return $variable;
584
- }
585
-
586
- // -------------------------------------------------------------------------
587
- // Shortcode
588
- // -------------------------------------------------------------------------
589
-
590
- /**
591
- * Create the functions for shortcodes dynamically and register them
592
- */
593
- function create_shortcodes() {
594
- $snippets = get_option( self::OPTION_DB_KEY );
595
- if (!empty($snippets)) {
596
- foreach ($snippets as $snippet) {
597
- // If shortcode is enabled for the snippet, and a snippet has been entered, register it as a shortcode.
598
- if ( $snippet['shortcode'] && !empty($snippet['snippet']) ) {
599
-
600
- $vars = explode(",",$snippet['vars']);
601
- $vars_str = "";
602
- foreach ($vars as $var) {
603
- $attribute = explode('=', $var);
604
- $default_value = (count($attribute) > 1) ? $attribute[1] : '';
605
- $vars_str .= "\"{$attribute[0]}\" => \"{$default_value}\",";
606
- }
607
-
608
- // Get the wptexturize setting
609
- $texturize = isset( $snippet["wptexturize"] ) ? $snippet["wptexturize"] : false;
610
-
611
- add_shortcode($snippet['title'], create_function('$atts,$content=null',
612
- '$shortcode_symbols = array('.$vars_str.');
613
- extract(shortcode_atts($shortcode_symbols, $atts));
614
-
615
- $attributes = compact( array_keys($shortcode_symbols) );
616
-
617
- // Add enclosed content if available to the attributes array
618
- if ( $content != null )
619
- $attributes["content"] = $content;
620
-
621
-
622
- $snippet = \''. addslashes($snippet["snippet"]) .'\';
623
- $snippet = str_replace("&", "&amp;", $snippet);
624
-
625
- foreach ($attributes as $key => $val) {
626
- $snippet = str_replace("{".$key."}", $val, $snippet);
627
- }
628
-
629
- // Handle PHP shortcodes
630
- $php = "'. $snippet["php"] .'";
631
- if ($php == true) {
632
- $snippet = PostSnippets::php_eval( $snippet );
633
- }
634
-
635
- // Strip escaping and execute nested shortcodes
636
- $snippet = do_shortcode(stripslashes($snippet));
637
-
638
- // WPTexturize the Snippet
639
- $texturize = "'. $texturize .'";
640
- if ($texturize == true) {
641
- $snippet = wptexturize( $snippet );
642
- }
643
-
644
- return $snippet;') );
645
- }
646
- }
647
- }
648
- }
649
-
650
- /**
651
- * Evaluate a snippet as PHP code.
652
- *
653
- * @since Post Snippets 1.9
654
- * @param string $content The snippet to evaluate
655
- * @return string The result of the evaluation
656
- */
657
- public static function php_eval( $content )
658
- {
659
- if ( !self::canExecutePHP() )
660
- return $content;
661
-
662
- $content = stripslashes($content);
663
-
664
- ob_start();
665
- eval ($content);
666
- $content = ob_get_clean();
667
-
668
- return addslashes( $content );
669
- }
670
-
671
-
672
- // -------------------------------------------------------------------------
673
- // Admin
674
- // -------------------------------------------------------------------------
675
-
676
- /**
677
- * The Admin Page.
678
- */
679
- function wp_admin() {
680
- if ( current_user_can('manage_options') ) {
681
- // If user can manage options, display the admin page
682
- $option_page = add_options_page( 'Post Snippets Options', 'Post Snippets', 'administrator', __FILE__, array(&$this, 'options_page') );
683
- if ( $option_page and class_exists('PostSnippets_Help') ) {
684
- $help = new PostSnippets_Help( $option_page );
685
- }
686
- } else {
687
- // If user can't manage options, but can edit posts, display the overview page
688
- $option_page = add_options_page( 'Post Snippets', 'Post Snippets', 'edit_posts', __FILE__, array(&$this, 'overview_page') );
689
- }
690
- }
691
-
692
- /**
693
- * The options Overview page.
694
- *
695
- * For users without manage_options cap but with edit_posts cap. A read-only
696
- * view.
697
- *
698
- * @since Post Snippets 1.9.7
699
- */
700
- public function overview_page()
701
- {
702
- $settings = new PostSnippets_Admin();
703
- $settings->render( 'overview' );
704
- }
705
-
706
- /**
707
- * The options Admin page.
708
- *
709
- * For users with manage_options capability.
710
- */
711
- public function options_page()
712
- {
713
- $settings = new PostSnippets_Admin();
714
- $settings->render( 'options' );
715
- }
716
-
717
-
718
- // -------------------------------------------------------------------------
719
- // Helpers
720
- // -------------------------------------------------------------------------
721
-
722
- /**
723
- * Allow snippets to be retrieved directly from PHP.
724
- *
725
- * @since Post Snippets 1.8.9.1
726
- *
727
- * @param string $snippet_name
728
- * The name of the snippet to retrieve
729
- * @param string $snippet_vars
730
- * The variables to pass to the snippet, formatted as a query string.
731
- * @return string
732
- * The Snippet
733
- */
734
- public static function getSnippet( $snippet_name, $snippet_vars = '' )
735
- {
736
- $snippets = get_option( self::OPTION_DB_KEY );
737
- for ($i = 0; $i < count($snippets); $i++) {
738
- if ($snippets[$i]['title'] == $snippet_name) {
739
- parse_str( htmlspecialchars_decode($snippet_vars), $snippet_output );
740
- $snippet = $snippets[$i]['snippet'];
741
- $var_arr = explode(",",$snippets[$i]['vars']);
742
-
743
- if ( !empty($var_arr[0]) ) {
744
- for ($j = 0; $j < count($var_arr); $j++) {
745
- $snippet = str_replace("{".$var_arr[$j]."}", $snippet_output[$var_arr[$j]], $snippet);
746
- }
747
- }
748
- }
749
- }
750
- return do_shortcode($snippet);
751
- }
752
-
753
- public static function optionDBKey()
754
- {
755
- return self::OPTION_DB_KEY;
756
- }
757
-
758
- public static function userMetaKey()
759
- {
760
- return self::USER_META_KEY;
761
- }
762
-
763
- /**
764
- * Allow other plugins to disable the PHP Code execution feature.
765
- *
766
- * @see http://wordpress.org/extend/plugins/post-snippets/faq/
767
- * @since 2.1
768
- */
769
- public static function canExecutePHP()
770
- {
771
- return apply_filters('post_snippets_php_execution_enabled', true);
772
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
773
 
774
 
775
  // -------------------------------------------------------------------------
@@ -784,14 +678,14 @@ function edOpenPostSnippets(myField) {
784
  // Check if PHP is too old
785
  if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<')) {
786
  // Display notice
787
- add_action( 'admin_notices', array(&$this, 'phpVersionError') );
788
  return false;
789
  }
790
 
791
  // Check if WordPress is too old
792
  global $wp_version;
793
  if (version_compare($wp_version, self::MIN_WP_VERSION, '<')) {
794
- add_action( 'admin_notices', array(&$this, 'wpVersionError') );
795
  return false;
796
  }
797
  return true;
@@ -804,9 +698,12 @@ function edOpenPostSnippets(myField) {
804
  {
805
  echo '<div class="error"><p><strong>';
806
  printf(
807
- 'Error: Post Snippets requires PHP version %1$s or greater.<br/>'.
808
  'Your installed PHP version: %2$s',
809
- self::MIN_PHP_VERSION, PHP_VERSION);
 
 
 
810
  echo '</strong></p></div>';
811
  }
812
 
@@ -817,10 +714,23 @@ function edOpenPostSnippets(myField) {
817
  {
818
  echo '<div class="error"><p><strong>';
819
  printf(
820
- 'Error: Post Snippets requires WordPress version %s or greater.',
821
- self::MIN_WP_VERSION );
 
 
822
  echo '</strong></p></div>';
823
  }
 
 
 
 
 
 
 
 
 
 
 
824
  }
825
 
826
  add_action('plugins_loaded', array('PostSnippets', 'getInstance'));
@@ -833,18 +743,18 @@ add_action('plugins_loaded', array('PostSnippets', 'getInstance'));
833
  * Allow snippets to be retrieved directly from PHP.
834
  * This function is a wrapper for Post_Snippets::get_snippet().
835
  *
836
- * @since Post Snippets 1.6
837
  * @deprecated Post Snippets 2.1
838
  *
839
- * @param string $snippet_name
840
- * The name of the snippet to retrieve
841
- * @param string $snippet_vars
842
- * The variables to pass to the snippet, formatted as a query string.
843
- * @return string
844
- * The Snippet
845
  */
846
  function get_post_snippet($snippet_name, $snippet_vars = '')
847
  {
848
- _deprecated_function(__FUNCTION__, '2.1', 'PostSnippets::getSnippet()');
849
- return PostSnippets::getSnippet($snippet_name, $snippet_vars);
850
  }
1
  <?php
2
  /*
3
  Plugin Name: Post Snippets
4
+ Plugin URI: http://johansteen.se/code/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
  Author: Johan Steen
7
  Author URI: http://johansteen.se/
8
+ Version: 2.2
9
  License: GPLv2 or later
10
  Text Domain: post-snippets
11
 
30
  spl_autoload_register('PostSnippets::autoload');
31
 
32
  /**
33
+ * Init Singleton Class.
34
  *
 
35
  * @author Johan Steen <artstorm at gmail dot com>
36
+ * @link http://johansteen.se/
37
  */
38
  class PostSnippets
39
  {
40
  private static $instance = false;
41
 
42
+ const MIN_PHP_VERSION = '5.2.4';
43
+ const MIN_WP_VERSION = '3.3';
44
+ const OPTION_KEY = 'post_snippets_options';
45
+ const USER_META_KEY = 'post_snippets';
46
+ const TINYMCE_PLUGIN_NAME = 'post_snippets';
47
+ const TEXT_DOMAIN = 'post-snippets';
48
+ const FILE = __FILE__;
49
 
50
  /**
51
  * Singleton class
59
  }
60
 
61
  /**
62
+ * Initializes the plugin.
 
 
63
  */
64
+ private function __construct()
65
+ {
66
  if (!$this->testHost()) {
67
  return;
68
  }
70
  add_action('init', array($this, 'textDomain'));
71
  register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
72
 
73
+ // Add TinyMCE button
74
+ add_action('init', array(&$this, 'addTinymceButton'));
75
+
76
+ // Settings link on plugins list
77
+ add_filter('plugin_action_links', array(&$this, 'actionLinks'), 10, 2);
78
+ // Options Page
79
+ add_action('admin_menu', array(&$this,'wpAdmin'));
80
+
81
+ $this->createShortcodes();
82
+
83
+ // Adds the JS and HTML code in the header and footer for the jQuery
84
+ // insert UI dialog in the editor
85
+ add_action('admin_init', array(&$this,'enqueueAssets'));
86
+ add_action('admin_head', array(&$this,'jqueryUiDialog'));
87
+ add_action('admin_footer', array(&$this,'addJqueryUiDialog'));
88
+
89
+ // Add Editor QuickTag button:
90
+ add_action(
91
+ 'admin_print_footer_scripts',
92
+ array(&$this,'addQuicktagButton'),
93
+ 100
94
+ );
95
+ }
 
 
 
 
 
 
 
96
 
97
  /**
98
  * PSR-0 compliant autoloader to load classes as needed.
99
  *
 
 
100
  * @param string $classname The name of the class
101
  * @return null Return early if the class name does not start with the
102
  * correct prefix
103
  */
104
  public static function autoload($className)
105
  {
106
+ if (__CLASS__ !== mb_substr($className, 0, strlen(__CLASS__))) {
107
  return;
108
  }
109
  $className = ltrim($className, '\\');
122
  }
123
 
124
  /**
125
+ * Loads the plugin text domain for translation
126
  */
127
  public function textDomain()
128
  {
129
+ $domain = self::TEXT_DOMAIN;
130
  $locale = apply_filters('plugin_locale', get_locale(), $domain);
131
  load_textdomain(
132
  $domain,
144
  */
145
  public function uninstall()
146
  {
147
+ // Delete all snippets
148
+ delete_option('post_snippets_options');
149
+
150
+ // Delete any per user settings
151
+ global $wpdb;
152
+ $wpdb->query(
153
+ "
154
+ DELETE FROM $wpdb->usermeta
155
+ WHERE meta_key = 'post_snippets'
156
+ "
157
+ );
158
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
+ /**
161
+ * Quick link to the Post Snippets Settings page from the Plugins page.
162
+ *
163
+ * @return Array with all the plugin's action links
164
+ */
165
+ public function actionLinks($links, $file)
166
+ {
167
+ if ($file == plugin_basename(dirname(__FILE__).'/post-snippets.php')) {
168
+ $links[] = '<a href="options-general.php?page=post-snippets/post-snippets.php">'.__('Settings', PostSnippets::TEXT_DOMAIN).'</a>';
169
+ }
170
+ return $links;
171
+ }
172
+
173
+
174
+ /**
175
+ * Enqueues the necessary scripts and styles for the plugins
176
+ *
177
+ * @since Post Snippets 1.7
178
+ */
179
+ public function enqueueAssets()
180
+ {
181
+ wp_enqueue_script('jquery-ui-dialog');
182
+ wp_enqueue_script('jquery-ui-tabs');
183
+ wp_enqueue_style('wp-jquery-ui-dialog');
184
+
185
+ # Adds the CSS stylesheet for the jQuery UI dialog
186
+ $style_url = plugins_url('/assets/post-snippets.css', __FILE__);
187
+ wp_register_style('post-snippets', $style_url, false, '2.0');
188
+ wp_enqueue_style('post-snippets');
189
+ }
190
+
191
+
192
+ // -------------------------------------------------------------------------
193
+ // WordPress Editor Buttons
194
+ // -------------------------------------------------------------------------
195
+
196
+ /**
197
+ * Add TinyMCE button.
198
+ *
199
+ * Adds filters to add custom buttons to the TinyMCE editor (Visual Editor)
200
+ * in WordPress.
201
+ *
202
+ * @since Post Snippets 1.8.7
203
+ */
204
+ public function addTinymceButton()
205
+ {
206
+ // Don't bother doing this stuff if the current user lacks permissions
207
+ if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
208
+ return;
209
+ }
210
+
211
+ // Add only in Rich Editor mode
212
+ if (get_user_option('rich_editing') == 'true') {
213
+ add_filter(
214
+ 'mce_external_plugins',
215
+ array(&$this, 'registerTinymcePlugin')
216
+ );
217
+ add_filter(
218
+ 'mce_buttons',
219
+ array(&$this, 'registerTinymceButton')
220
+ );
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Register TinyMCE button.
226
+ *
227
+ * Pushes the custom TinyMCE button into the array of with button names.
228
+ * 'separator' or '|' can be pushed to the array as well. See the link
229
+ * for all available TinyMCE controls.
230
+ *
231
+ * @see wp-includes/class-wp-editor.php
232
+ * @link http://www.tinymce.com/wiki.php/Buttons/controls
233
+ * @since Post Snippets 1.8.7
234
+ *
235
+ * @param array $buttons Filter supplied array of buttons to modify
236
+ * @return array The modified array with buttons
237
+ */
238
+ public function registerTinymceButton($buttons)
239
+ {
240
+ array_push($buttons, 'separator', self::TINYMCE_PLUGIN_NAME);
241
+ return $buttons;
242
+ }
243
+
244
+ /**
245
+ * Register TinyMCE plugin.
246
+ *
247
+ * Adds the absolute URL for the TinyMCE plugin to the associative array of
248
+ * plugins. Array structure: 'plugin_name' => 'plugin_url'
249
+ *
250
+ * @see wp-includes/class-wp-editor.php
251
+ * @since Post Snippets 1.8.7
252
+ *
253
+ * @param array $plugins Filter supplied array of plugins to modify
254
+ * @return array The modified array with plugins
255
+ */
256
+ public function registerTinymcePlugin($plugins)
257
+ {
258
+ // Load the TinyMCE plugin, editor_plugin.js, into the array
259
+ $plugins[self::TINYMCE_PLUGIN_NAME] =
260
+ plugins_url('/tinymce/editor_plugin.js?ver=1.9', __FILE__);
261
+
262
+ return $plugins;
263
+ }
264
+
265
+ /**
266
+ * Adds a QuickTag button to the HTML editor.
267
+ *
268
+ * Compatible with WordPress 3.3 and newer.
269
+ *
270
+ * @see wp-includes/js/quicktags.dev.js -> qt.addButton()
271
+ * @since Post Snippets 1.8.6
272
+ */
273
+ public function addQuicktagButton()
274
+ {
275
+ // Only run the function on post edit screens
276
+ if (function_exists('get_current_screen')) {
277
+ $screen = get_current_screen();
278
+ if ($screen->base != 'post') {
279
+ return;
280
+ }
281
+ }
282
+
283
+ echo "\n<!-- START: Add QuickTag button for Post Snippets -->\n";
284
+ ?>
285
+ <script type="text/javascript" charset="utf-8">
286
+ QTags.addButton( 'post_snippets_id', 'Post Snippets', qt_post_snippets );
287
+ function qt_post_snippets() {
288
+ post_snippets_caller = 'html';
289
+ jQuery( "#post-snippets-dialog" ).dialog( "open" );
290
+ }
291
+ </script>
292
+ <?php
293
+ echo "\n<!-- END: Add QuickTag button for Post Snippets -->\n";
294
+ }
295
+
296
+ // -------------------------------------------------------------------------
297
+ // JavaScript / jQuery handling for the post editor
298
+ // -------------------------------------------------------------------------
299
+
300
+ /**
301
+ * jQuery control for the dialog and Javascript needed to insert snippets into the editor
302
+ *
303
+ * @since Post Snippets 1.7
304
+ */
305
+ public function jqueryUiDialog()
306
+ {
307
+ // Only run the function on post edit screens
308
+ if (function_exists('get_current_screen')) {
309
+ $screen = get_current_screen();
310
+ if ($screen->base != 'post') {
311
+ return;
312
+ }
313
+ }
314
+
315
+ echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
316
+ echo "<script type='text/javascript'>\n";
317
+
318
+ # Prepare the snippets and shortcodes into javascript variables
319
+ # so they can be inserted into the editor, and get the variables replaced
320
+ # with user defined strings.
321
+ $snippets = get_option(self::OPTION_KEY, array());
322
+ foreach ($snippets as $key => $snippet) {
323
+ if ($snippet['shortcode']) {
324
+ # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
325
+ # so {varnameX} can be replaced at runtime.
326
+ $var_arr = explode(",", $snippet['vars']);
327
+ $variables = '';
328
+ if (!empty($var_arr[0])) {
329
+ foreach ($var_arr as $var) {
330
+ // '[test2 yet="{yet}" mupp=per="{mupp=per}" content="{content}"]';
331
+ $var = $this->stripDefaultVal($var);
332
+
333
+ $variables .= ' ' . $var . '="{' . $var . '}"';
334
+ }
335
+ }
336
+ $shortcode = $snippet['title'] . $variables;
337
+ echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
338
+ } else {
339
+ // To use $snippet is probably not a good naming convention here.
340
+ // rename to js_snippet or something?
341
+ $snippet = $snippet['snippet'];
342
+ # Fixes for potential collisions:
343
+ /* Replace <> with char codes, otherwise </script> in a snippet will break it */
344
+ $snippet = str_replace('<', '\x3C', str_replace('>', '\x3E', $snippet));
345
+ /* Escape " with \" */
346
+ $snippet = str_replace('"', '\"', $snippet);
347
+ /* Remove CR and replace LF with \n to keep formatting */
348
+ $snippet = str_replace(chr(13), '', str_replace(chr(10), '\n', $snippet));
349
+ # Print out the variable containing the snippet
350
+ echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
351
+ }
352
+ }
353
+ ?>
354
+
355
+ jQuery(document).ready(function($){
356
+ <?php
357
+ # Create js variables for all form fields
358
+ foreach ($snippets as $key => $snippet) {
359
+ $var_arr = explode(",", $snippet['vars']);
360
+ if (!empty($var_arr[0])) {
361
+ foreach ($var_arr as $key_2 => $var) {
362
+ $varname = "var_" . $key . "_" . $key_2;
363
+ echo "var {$varname} = $( \"#{$varname}\" );\n";
364
+ }
365
+ }
366
+ }
367
+ ?>
368
+
369
+ var $tabs = $("#post-snippets-tabs").tabs();
370
+
371
+ $(function() {
372
+ $( "#post-snippets-dialog" ).dialog({
373
+ autoOpen: false,
374
+ modal: true,
375
+ dialogClass: 'wp-dialog',
376
+ buttons: {
377
+ Cancel: function() {
378
+ $( this ).dialog( "close" );
379
+ },
380
+ "Insert": function() {
381
+ $( this ).dialog( "close" );
382
+ var selected = $tabs.tabs('option', 'selected');
383
+ <?php
384
+ foreach ($snippets as $key => $snippet) {
385
+ ?>
386
+ if (selected == <?php echo $key; ?>) {
387
+ insert_snippet = postsnippet_<?php echo $key; ?>;
388
+ <?php
389
+ $var_arr = explode(",", $snippet['vars']);
390
+ if (!empty($var_arr[0])) {
391
+ foreach ($var_arr as $key_2 => $var) {
392
+ $varname = "var_" . $key . "_" . $key_2; ?>
393
+ insert_snippet = insert_snippet.replace(/\{<?php
394
+ echo $this->stripDefaultVal($var);
395
+ ?>\}/g, <?php echo $varname; ?>.val());
396
+ <?php
397
+ echo "\n";
398
+ }
399
+ }
400
+ ?>
401
+ }
402
+ <?php
403
+ }
404
+ ?>
405
+
406
+ // Decide what method to use to insert the snippet depending
407
+ // from what editor the window was opened from
408
+ if (post_snippets_caller == 'html') {
409
+ // HTML editor in WordPress 3.3 and greater
410
+ QTags.insertContent(insert_snippet);
411
+ } else if (post_snippets_caller == 'html_pre33') {
412
+ // HTML editor in WordPress below 3.3.
413
+ edInsertContent(post_snippets_canvas, insert_snippet);
414
+ } else {
415
+ // Visual Editor
416
+ post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
417
+ }
418
+
419
+ }
420
+ },
421
+ width: 500,
422
+ });
423
+ });
424
+ });
425
+
426
+ // Global variables to keep track on the canvas instance and from what editor
427
+ // that opened the Post Snippets popup.
428
+ var post_snippets_canvas;
429
+ var post_snippets_caller = '';
430
+
431
+ <?php
432
+ echo "</script>\n";
433
+ echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
434
+ }
435
+
436
+ /**
437
+ * Build jQuery UI Window.
438
+ *
439
+ * Creates the jQuery for Post Editor popup window, its snippet tabs and the
440
+ * form fields to enter variables.
441
+ *
442
+ * @since Post Snippets 1.7
443
+ */
444
+ public function addJqueryUiDialog()
445
+ {
446
+ // Only run the function on post edit screens
447
+ if (function_exists('get_current_screen')) {
448
+ $screen = get_current_screen();
449
+ if ($screen->base != 'post') {
450
+ return;
451
+ }
452
+ }
453
+
454
+ $data = array('snippets' => get_option(self::OPTION_KEY, array()));
455
+ echo PostSnippets_View::render('jquery-ui-dialog', $data);
456
+ }
457
+
458
+ /**
459
+ * Strip Default Value.
460
+ *
461
+ * Checks if a variable string contains a default value, and if it does it
462
+ * will strip it away and return the string with only the variable name
463
+ * kept.
464
+ *
465
+ * @since Post Snippets 1.9.3
466
+ * @param string $variable The variable to check for default value
467
+ * @return string The variable without any default value
468
+ */
469
+ public function stripDefaultVal($variable)
470
+ {
471
+ // Check if variable contains a default defintion
472
+ $def_pos = strpos($variable, '=');
473
+
474
+ if ($def_pos !== false) {
475
+ $split = str_split($variable, $def_pos);
476
+ $variable = $split[0];
477
+ }
478
+ return $variable;
479
+ }
480
+
481
+ // -------------------------------------------------------------------------
482
+ // Shortcode
483
+ // -------------------------------------------------------------------------
484
+
485
+ /**
486
+ * Create the functions for shortcodes dynamically and register them
487
+ */
488
+ public function createShortcodes()
489
+ {
490
+ $snippets = get_option(self::OPTION_KEY);
491
+ if (!empty($snippets)) {
492
+ foreach ($snippets as $snippet) {
493
+ // If shortcode is enabled for the snippet, and a snippet has been entered, register it as a shortcode.
494
+ if ($snippet['shortcode'] && !empty($snippet['snippet'])) {
495
+
496
+ $vars = explode(",", $snippet['vars']);
497
+ $vars_str = "";
498
+ foreach ($vars as $var) {
499
+ $attribute = explode('=', $var);
500
+ $default_value = (count($attribute) > 1) ? $attribute[1] : '';
501
+ $vars_str .= "\"{$attribute[0]}\" => \"{$default_value}\",";
502
+ }
503
+
504
+ // Get the wptexturize setting
505
+ $texturize = isset( $snippet["wptexturize"] ) ? $snippet["wptexturize"] : false;
506
+
507
+ add_shortcode(
508
+ $snippet['title'],
509
+ create_function(
510
+ '$atts,$content=null',
511
+ '$shortcode_symbols = array('.$vars_str.');
512
+ extract(shortcode_atts($shortcode_symbols, $atts));
513
+
514
+ $attributes = compact( array_keys($shortcode_symbols) );
515
+
516
+ // Add enclosed content if available to the attributes array
517
+ if ( $content != null )
518
+ $attributes["content"] = $content;
519
+
520
+
521
+ $snippet = \''. addslashes($snippet["snippet"]) .'\';
522
+ // Disables auto conversion from & to &amp; as that should be done in snippet, not code (destroys php etc).
523
+ // $snippet = str_replace("&", "&amp;", $snippet);
524
+
525
+ foreach ($attributes as $key => $val) {
526
+ $snippet = str_replace("{".$key."}", $val, $snippet);
527
+ }
528
+
529
+ // Handle PHP shortcodes
530
+ $php = "'. $snippet["php"] .'";
531
+ if ($php == true) {
532
+ $snippet = PostSnippets::phpEval( $snippet );
533
+ }
534
+
535
+ // Strip escaping and execute nested shortcodes
536
+ $snippet = do_shortcode(stripslashes($snippet));
537
+
538
+ // WPTexturize the Snippet
539
+ $texturize = "'. $texturize .'";
540
+ if ($texturize == true) {
541
+ $snippet = wptexturize( $snippet );
542
+ }
543
+
544
+ return $snippet;'
545
+ )
546
+ );
547
+ }
548
+ }
549
+ }
550
+ }
551
+
552
+ /**
553
+ * Evaluate a snippet as PHP code.
554
+ *
555
+ * @since Post Snippets 1.9
556
+ * @param string $content The snippet to evaluate
557
+ * @return string The result of the evaluation
558
+ */
559
+ public static function phpEval($content)
560
+ {
561
+ if (!self::canExecutePHP()) {
562
+ return $content;
563
+ }
564
+
565
+ $content = stripslashes($content);
566
+
567
+ ob_start();
568
+ eval ($content);
569
+ $content = ob_get_clean();
570
+
571
+ return addslashes($content);
572
+ }
573
+
574
+
575
+ // -------------------------------------------------------------------------
576
+ // Admin
577
+ // -------------------------------------------------------------------------
578
+
579
+ /**
580
+ * The Admin Page.
581
+ */
582
+ public function wpAdmin()
583
+ {
584
+ if (current_user_can('manage_options')) {
585
+ // If user can manage options, display the admin page
586
+ $option_page = add_options_page('Post Snippets Options', 'Post Snippets', 'administrator', __FILE__, array(&$this, 'optionsPage'));
587
+ if ($option_page and class_exists('PostSnippets_Help')) {
588
+ $help = new PostSnippets_Help($option_page);
589
+ }
590
+ } else {
591
+ // If user can't manage options, but can edit posts, display the overview page
592
+ $option_page = add_options_page('Post Snippets', 'Post Snippets', 'edit_posts', __FILE__, array(&$this, 'overviewPage'));
593
+ }
594
+ }
595
+
596
+ /**
597
+ * The options Overview page.
598
+ *
599
+ * For users without manage_options cap but with edit_posts cap. A read-only
600
+ * view.
601
+ *
602
+ * @since Post Snippets 1.9.7
603
+ */
604
+ public function overviewPage()
605
+ {
606
+ $settings = new PostSnippets_Admin();
607
+ $settings->render('overview');
608
+ }
609
+
610
+ /**
611
+ * The options Admin page.
612
+ *
613
+ * For users with manage_options capability.
614
+ */
615
+ public function optionsPage()
616
+ {
617
+ $settings = new PostSnippets_Admin();
618
+ $settings->render('options');
619
+ }
620
+
621
+
622
+ // -------------------------------------------------------------------------
623
+ // Helpers
624
+ // -------------------------------------------------------------------------
625
+
626
+ /**
627
+ * Allow snippets to be retrieved directly from PHP.
628
+ *
629
+ * @since Post Snippets 1.8.9.1
630
+ *
631
+ * @param string $snippet_name
632
+ * The name of the snippet to retrieve
633
+ * @param string $snippet_vars
634
+ * The variables to pass to the snippet, formatted as a query string.
635
+ * @return string
636
+ * The Snippet
637
+ */
638
+ public static function getSnippet($snippet_name, $snippet_vars = '')
639
+ {
640
+ $snippets = get_option(self::OPTION_KEY, array());
641
+ for ($i = 0; $i < count($snippets); $i++) {
642
+ if ($snippets[$i]['title'] == $snippet_name) {
643
+ parse_str(htmlspecialchars_decode($snippet_vars), $snippet_output);
644
+ $snippet = $snippets[$i]['snippet'];
645
+ $var_arr = explode(",", $snippets[$i]['vars']);
646
+
647
+ if (!empty($var_arr[0])) {
648
+ for ($j = 0; $j < count($var_arr); $j++) {
649
+ $snippet = str_replace("{".$var_arr[$j]."}", $snippet_output[$var_arr[$j]], $snippet);
650
+ }
651
+ }
652
+ }
653
+ }
654
+ return do_shortcode($snippet);
655
+ }
656
+
657
+ /**
658
+ * Allow other plugins to disable the PHP Code execution feature.
659
+ *
660
+ * @see http://wordpress.org/extend/plugins/post-snippets/faq/
661
+ * @since 2.1
662
+ */
663
+ public static function canExecutePHP()
664
+ {
665
+ return apply_filters('post_snippets_php_execution_enabled', true);
666
+ }
667
 
668
 
669
  // -------------------------------------------------------------------------
678
  // Check if PHP is too old
679
  if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<')) {
680
  // Display notice
681
+ add_action('admin_notices', array(&$this, 'phpVersionError'));
682
  return false;
683
  }
684
 
685
  // Check if WordPress is too old
686
  global $wp_version;
687
  if (version_compare($wp_version, self::MIN_WP_VERSION, '<')) {
688
+ add_action('admin_notices', array(&$this, 'wpVersionError'));
689
  return false;
690
  }
691
  return true;
698
  {
699
  echo '<div class="error"><p><strong>';
700
  printf(
701
+ 'Error: %3$s requires PHP version %1$s or greater.<br/>'.
702
  'Your installed PHP version: %2$s',
703
+ self::MIN_PHP_VERSION,
704
+ PHP_VERSION,
705
+ $this->getPluginName()
706
+ );
707
  echo '</strong></p></div>';
708
  }
709
 
714
  {
715
  echo '<div class="error"><p><strong>';
716
  printf(
717
+ 'Error: %2$s requires WordPress version %1$s or greater.',
718
+ self::MIN_WP_VERSION,
719
+ $this->getPluginName()
720
+ );
721
  echo '</strong></p></div>';
722
  }
723
+
724
+ /**
725
+ * Get the name of this plugin.
726
+ *
727
+ * @return string The plugin name.
728
+ */
729
+ private function getPluginName()
730
+ {
731
+ $data = get_plugin_data(self::FILE);
732
+ return $data['Name'];
733
+ }
734
  }
735
 
736
  add_action('plugins_loaded', array('PostSnippets', 'getInstance'));
743
  * Allow snippets to be retrieved directly from PHP.
744
  * This function is a wrapper for Post_Snippets::get_snippet().
745
  *
746
+ * @since Post Snippets 1.6
747
  * @deprecated Post Snippets 2.1
748
  *
749
+ * @param string $snippet_name
750
+ * The name of the snippet to retrieve
751
+ * @param string $snippet_vars
752
+ * The variables to pass to the snippet, formatted as a query string.
753
+ * @return string
754
+ * The Snippet
755
  */
756
  function get_post_snippet($snippet_name, $snippet_vars = '')
757
  {
758
+ _deprecated_function(__FUNCTION__, '2.1', 'PostSnippets::getSnippet()');
759
+ return PostSnippets::getSnippet($snippet_name, $snippet_vars);
760
  }
readme.txt CHANGED
@@ -1,9 +1,10 @@
1
  === Post Snippets ===
2
  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.5.1
6
- Stable tag: 2.1.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -37,23 +38,20 @@ updating the snippet. PHP code is supported for snippets inserted as shortcodes.
37
 
38
  = Related Links =
39
 
40
- * [Documentation](http://wpstorm.net/wordpress-plugins/post-snippets/
41
- "Complete usage instructions")
42
- * [Support Forum](http://wordpress.org/tags/post-snippets?forum_id=10
43
  "Use this for support and feature requests")
44
- * [GitHub](https://github.com/artstorm/post-snippets
45
  "Develop and contribute your code or report bugs")
46
 
47
- See the [Changelog](http://wordpress.org/extend/plugins/post-snippets/changelog/)
48
- for what's new. Available [Translations](http://wpstorm.net/wordpress-plugins/post-snippets/#translations).
49
-
50
 
51
  == Installation ==
52
 
53
  = Requirements =
54
 
55
  * PHP version 5.2.4 or greater.
56
- * WordPress version 3.0 or greater.
57
 
58
  = Install =
59
 
@@ -119,6 +117,14 @@ Contributions are appreciated and encouraged.
119
 
120
  == Changelog ==
121
 
 
 
 
 
 
 
 
 
122
  = Version 2.1.1 - 23 Feb 2013 =
123
  * Fixes a bug that PHP snippets called an old class definition.
124
 
@@ -399,3 +405,6 @@ replaces the old function. `get_post_snippet()` will be removed in a future
399
  version.
400
 
401
  Most users are not affected by this change.
 
 
 
1
  === Post Snippets ===
2
  Contributors: artstorm
3
+ Donate link: http://johansteen.se/donate/
4
  Tags: post, admin, snippet, shortcode, html, custom, page, dynamic, editor, php, code
5
+ Requires at least: 3.3
6
  Tested up to: 3.5.1
7
+ Stable tag: 2.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
38
 
39
  = Related Links =
40
 
41
+ * [Documentation](http://johansteen.se/code/post-snippets/
42
+ "Usage instructions")
43
+ * [Support Forum](http://wordpress.org/support/plugin/post-snippets
44
  "Use this for support and feature requests")
45
+ * [GitHub](https://github.com/artstorm/post-snippets
46
  "Develop and contribute your code or report bugs")
47
 
 
 
 
48
 
49
  == Installation ==
50
 
51
  = Requirements =
52
 
53
  * PHP version 5.2.4 or greater.
54
+ * WordPress version 3.3 or greater.
55
 
56
  = Install =
57
 
117
 
118
  == Changelog ==
119
 
120
+ = Version 2.2 - 26 Apr 2013 =
121
+ * Bumps required WordPress version to v3.3.
122
+ * Fixes a problem with using some HTML entities in snippets.
123
+ * Removes the screenshots from the plugin archive, to make a smaller archive.
124
+ * Updates help text for `PostSnippets::getSnippet()`.
125
+ * Fixes a PHP warning that occurred when no post snippets exist.
126
+ * Makes the plugin PSR-2 compliant and introduces Travis CI for testing.
127
+
128
  = Version 2.1.1 - 23 Feb 2013 =
129
  * Fixes a bug that PHP snippets called an old class definition.
130
 
405
  version.
406
 
407
  Most users are not affected by this change.
408
+
409
+ = 2.2 =
410
+ Note that at least WordPress v3.3 are required for Post Snippets v2.2.
screenshot-1.jpg DELETED
Binary file
screenshot-2.jpg DELETED
Binary file
screenshot-3.jpg DELETED
Binary file
screenshot-4.jpg DELETED
Binary file
unittest/PostSnippetsTest.php DELETED
@@ -1,66 +0,0 @@
1
- <?php
2
- /**
3
- * Post Snippets Unit Tests.
4
- */
5
- class PostSnippetsTest extends WP_UnitTestCase {
6
-
7
- private $plugin = 'post-snippets';
8
-
9
- public function setUp()
10
- {
11
- parent::setUp();
12
-
13
- $snippets = array();
14
- array_push($snippets, array(
15
- 'title' => "TestTmp",
16
- 'vars' => "",
17
- 'description' => "",
18
- 'shortcode' => false,
19
- 'php' => false,
20
- 'snippet' => "A test snippet..."));
21
- update_option('post_snippets_options', $snippets);
22
- }
23
-
24
- // -------------------------------------------------------------------------
25
- // Tests
26
- // -------------------------------------------------------------------------
27
-
28
- public function testPluginInitialization()
29
- {
30
- $this->assertFalse( null == $this->plugin );
31
- }
32
-
33
- public function teast_Yo()
34
- {
35
- $this->assertTrue(true);
36
- }
37
-
38
- public function teast_Yos()
39
- {
40
- $this->assertTrue(true);
41
- }
42
-
43
- /**
44
- * @dataProvider provider
45
- */
46
- public function teast_data_inline($a, $b, $c)
47
- {
48
- // var_dump($c);
49
- }
50
- public function praovider()
51
- {
52
- return array(
53
- array(0, 0, 0),
54
- array(0, 1, 1),
55
- array(1, 0, 1),
56
- array(1, 1, 3)
57
- );
58
- }
59
-
60
- public function test_get_post_snippet()
61
- {
62
- $test = get_post_snippet('TestTmp');
63
- $this->assertTrue(is_string($test));
64
- $this->assertEquals($test, 'A test snippet...');
65
- }
66
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
unittest/bootstrap.php DELETED
@@ -1,8 +0,0 @@
1
- <?php
2
- $GLOBALS['wp_tests_options'] = array(
3
- 'active_plugins' => array( 'post-snippets/post-snippets.php' ),
4
- 'template' => 'twentyeleven',
5
- 'stylesheet' => 'twentyeleven',
6
- );
7
-
8
- require_once('D:/Dropbox/Code/_Tools/UniformServer/www/wordpress.dev/wordpress-tests/bootstrap.php');
 
 
 
 
 
 
 
 
views/admin_snippets.php ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <form method="post" action="">
2
+ <?php wp_nonce_field('update_snippets', 'update_snippets_nonce'); ?>
3
+
4
+ <table class="widefat fixed" cellspacing="0">
5
+ <thead>
6
+ <tr>
7
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
8
+ <th scope="col" style="width: 180px;"><?php _e('Title', PostSnippets::TEXT_DOMAIN); ?></th>
9
+ <th scope="col" style="width: 180px;"><?php _e('Variables', PostSnippets::TEXT_DOMAIN); ?></th>
10
+ <th scope="col"><?php _e('Snippet', PostSnippets::TEXT_DOMAIN); ?></th>
11
+ </tr>
12
+ </thead>
13
+
14
+ <tfoot>
15
+ <tr>
16
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
17
+ <th scope="col"><?php _e('Title', PostSnippets::TEXT_DOMAIN) ?></th>
18
+ <th scope="col"><?php _e('Variables', PostSnippets::TEXT_DOMAIN) ?></th>
19
+ <th scope="col"><?php _e('Snippet', PostSnippets::TEXT_DOMAIN) ?></th>
20
+ </tr>
21
+ </tfoot>
22
+
23
+ <tbody>
24
+ <?php
25
+ $snippets = get_option(PostSnippets::OPTION_KEY);
26
+ if (!empty($snippets)) {
27
+ foreach ($snippets as $key => $snippet) {
28
+ ?>
29
+ <tr class='recent'>
30
+ <th scope='row' class='check-column'><input type='checkbox' name='checked[]' value='<?php echo $key; ?>' /></th>
31
+ <td class='row-title'>
32
+ <input type='text' name='<?php echo $key; ?>_title' value='<?php echo $snippet['title']; ?>' />
33
+ </td>
34
+ <td class='name'>
35
+ <input type='text' name='<?php echo $key; ?>_vars' value='<?php echo $snippet['vars']; ?>' />
36
+ <br/>
37
+ <br/>
38
+ <?php
39
+ PostSnippets_Admin::checkbox(__('Shortcode', PostSnippets::TEXT_DOMAIN), $key.'_shortcode',
40
+ $snippet['shortcode']);
41
+
42
+ echo '<br/><strong>Shortcode Options:</strong><br/>';
43
+
44
+ if (PostSnippets::canExecutePHP()) {
45
+ PostSnippets_Admin::checkbox(
46
+ __('PHP Code', PostSnippets::TEXT_DOMAIN),
47
+ $key.'_php',
48
+ $snippet['php']
49
+ );
50
+ }
51
+
52
+ $wptexturize = isset( $snippet['wptexturize'] ) ? $snippet['wptexturize'] : false;
53
+ PostSnippets_Admin::checkbox('wptexturize', $key.'_wptexturize', $wptexturize);
54
+ ?>
55
+ </td>
56
+ <td class='desc'>
57
+ <textarea name="<?php echo $key; ?>_snippet" class="large-text" style='width: 100%;' rows="5"><?php echo htmlspecialchars($snippet['snippet'], ENT_NOQUOTES); ?></textarea>
58
+ <?php _e( 'Description', PostSnippets::TEXT_DOMAIN ) ?>:
59
+ <input type='text' style='width: 100%;' name='<?php echo $key; ?>_description' value='<?php if (isset( $snippet['description'] ) ) echo esc_html($snippet['description']); ?>' /><br/>
60
+ </td>
61
+ </tr>
62
+ <?php
63
+ }
64
+ }
65
+ ?>
66
+ </tbody>
67
+ </table>
68
+
69
+ <?php
70
+ PostSnippets_Admin::submit('update-snippets', __('Update Snippets', PostSnippets::TEXT_DOMAIN));
71
+ PostSnippets_Admin::submit('add-snippet', __('Add New Snippet', PostSnippets::TEXT_DOMAIN), 'button-secondary', false);
72
+ PostSnippets_Admin::submit('delete-snippets', __('Delete Selected', PostSnippets::TEXT_DOMAIN), 'button-secondary', false);
73
+ echo '</form>';
views/help_advanced.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <p>
2
+ <?php _e('You can retrieve a Post Snippet directly from PHP, in a theme for instance, by using the PostSnippets::getSnippet() method.', PostSnippets::TEXT_DOMAIN); ?>
3
+ </p>
4
+
5
+ <h2><?php _e('Usage', PostSnippets::TEXT_DOMAIN); ?></h2>
6
+ <p>
7
+ <code>
8
+ &lt;?php $my_snippet = PostSnippets::getSnippet( $snippet_name, $snippet_vars ); ?&gt;
9
+ </code></p>
10
+
11
+ <h2><?php _e('Parameters', PostSnippets::TEXT_DOMAIN); ?></h2>
12
+ <p>
13
+ <code>$snippet_name</code><br/>
14
+ <?php _e('(string) (required) The name of the snippet to retrieve.', PostSnippets::TEXT_DOMAIN); ?>
15
+ <br/><br/>
16
+ <code>$snippet_vars</code><br/>
17
+ <?php _e('(string) The variables to pass to the snippet, formatted as a query string.', PostSnippets::TEXT_DOMAIN); ?>
18
+ </p>
19
+
20
+ <h2><?php _e('Example', PostSnippets::TEXT_DOMAIN); ?></h2>
21
+ <p><code>
22
+ &lt;?php<br/>
23
+ $my_snippet = PostSnippets::getSnippet( 'internal-link', 'title=Awesome&amp;url=2011/02/awesome/' );<br/>
24
+ echo $my_snippet;<br/>
25
+ ?&gt;
26
+ </code></p>
views/help_basic.php ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <h2><?php _e('Title', PostSnippets::TEXT_DOMAIN); ?></h2>
2
+ <p>
3
+ <?php _e('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', PostSnippets::TEXT_DOMAIN); ?>
4
+ </p>
5
+
6
+ <h2><?php _e('Variables', PostSnippets::TEXT_DOMAIN); ?></h2>
7
+ <p>
8
+ <?php _e('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.', PostSnippets::TEXT_DOMAIN); ?>
9
+ </p>
10
+
11
+ <p>
12
+ <strong><?php _e('Example', PostSnippets::TEXT_DOMAIN); ?></strong><br/>
13
+ <code>url,name,role=user,title</code>
14
+ </p>
15
+
16
+ <h2><?php _e('Snippet', PostSnippets::TEXT_DOMAIN); ?></h2>
17
+ <p>
18
+ <?php _e('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.', PostSnippets::TEXT_DOMAIN); ?>
19
+ </p>
20
+
21
+ <p><strong><?php _e('Example', PostSnippets::TEXT_DOMAIN); ?></strong><br/>
22
+ <?php _e('To reference the variables in the example above, you would enter {url} and {name}. So if you enter this snippet:', PostSnippets::TEXT_DOMAIN); ?>
23
+ <br/>
24
+ <code>This is the website of &lt;a href="{url}"&gt;{name}&lt;/a&gt;</code>
25
+ <br/>
26
+ <?php _e('You will get the option to replace url and name on insert if they are defined as variables.', PostSnippets::TEXT_DOMAIN); ?>
27
+ </p>
28
+
29
+ <h2><?php _e('Description', PostSnippets::TEXT_DOMAIN); ?></h2>
30
+ <p>
31
+ <?php _e('An optional description for the Snippet. If filled out, the description will be displayed in the snippets insert window in the post editor.', PostSnippets::TEXT_DOMAIN); ?>
32
+ </p>
views/help_php.php ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <p>
2
+ <?php _e('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.', PostSnippets::TEXT_DOMAIN); ?>
3
+ </p>
4
+
5
+ <p><strong><?php _e('Example PHP Snippet', PostSnippets::TEXT_DOMAIN); ?></strong><br/>
6
+ <code>
7
+ for ($i=1; $i<5; $i++) {<br/>
8
+ echo "{loop_me}&lt;br/&gt;";<br/>
9
+ };
10
+ </code></p>
11
+
12
+ <p>
13
+ <?php _e('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:', PostSnippets::TEXT_DOMAIN); ?>
14
+ </p>
15
+
16
+ <code>[phpcode loop_me="post snippet with PHP!"]</code>
17
+
18
+ <p>
19
+ <?php _e('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!)', PostSnippets::TEXT_DOMAIN); ?>
20
+ </p>
21
+
22
+ <p>
23
+ <?php _e('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;.', PostSnippets::TEXT_DOMAIN); ?>
24
+ </p>
views/help_shortcode.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <p>
2
+ <?php _e('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.', PostSnippets::TEXT_DOMAIN); ?>
3
+ </p>
4
+
5
+ <p>
6
+ <?php _e('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.', PostSnippets::TEXT_DOMAIN); ?>
7
+ </p>
8
+
9
+ <p>
10
+ <?php _e('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.', PostSnippets::TEXT_DOMAIN); ?>
11
+ </p>
12
+
13
+ <h2><?php _e('Options', PostSnippets::TEXT_DOMAIN); ?></h2>
14
+ <p><strong>PHP</strong><br/>
15
+ <?php _e('See the dedicated help section for information about PHP shortcodes.', PostSnippets::TEXT_DOMAIN); ?>
16
+ </p>
17
+ <p><strong>wptexturize</strong><br/>
18
+ <?php printf(__('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.', PostSnippets::TEXT_DOMAIN), '<a href="http://codex.wordpress.org/Function_Reference/wptexturize">wptexturize</a>'); ?>
19
+ </p>
views/help_sidebar.php ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <p><strong>
2
+ <?php _e('For more information:', PostSnippets::TEXT_DOMAIN); ?>
3
+ </strong></p>
4
+
5
+ <p><a href="http://johansteen.se/code/post-snippets/" target="_blank"><?php
6
+ _e('Documentation', PostSnippets::TEXT_DOMAIN);
7
+ ?></a></p>
8
+
9
+ <p><a href="http://wordpress.org/support/plugin/post-snippets" target="_blank"><?php
10
+ _e('Support Forums', PostSnippets::TEXT_DOMAIN);
11
+ ?></a></p>
12
+
13
+ <p><a href="https://github.com/artstorm/post-snippets" target="_blank"><?php
14
+ _e('GitHub Contribution', PostSnippets::TEXT_DOMAIN);
15
+ ?></a></p>
views/jquery-ui-dialog.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- START: Post Snippets UI Dialog -->
2
+ <?php // Setup the dialog divs ?>
3
+ <div class="hidden">
4
+ <div id="post-snippets-dialog" title="Post Snippets">
5
+ <?php // Init the tabs div ?>
6
+ <div id="post-snippets-tabs">
7
+ <ul>
8
+ <?php
9
+ // Create a tab for each available snippet
10
+ foreach ($snippets as $key => $snippet) { ?>
11
+ <li><a href="#ps-tabs-<?php echo $key; ?>"><?php echo $snippet['title']; ?></a></li>
12
+ <?php } ?>
13
+ </ul>
14
+
15
+ <?php
16
+ // Create a panel with form fields for each available snippet
17
+ foreach ($snippets as $key => $snippet) { ?>
18
+ <div id="ps-tabs-<?php echo $key; ?>">
19
+ <?php
20
+ // Print a snippet description is available
21
+ if (isset($snippet['description'])) { ?>
22
+ <p class="howto"><?php echo $snippet['description']; ?></p>
23
+ <?php }
24
+
25
+ // Get all variables defined for the snippet and output them as
26
+ // input fields
27
+ $var_arr = explode(',', $snippet['vars']);
28
+ if (!empty($var_arr[0])) {
29
+ foreach ($var_arr as $key_2 => $var) {
30
+ // Default value exists?
31
+ $def_pos = strpos($var, '=');
32
+ if ($def_pos !== false) {
33
+ $split = explode('=', $var);
34
+ $var = $split[0];
35
+ $def = $split[1];
36
+ } else {
37
+ $def = '';
38
+ } ?>
39
+ <label for="var_<?php echo $key.'_'.$key_2; ?>"><?php echo $var; ?>:</label>
40
+ <input type="text" id="var_<?php echo $key.'_'.$key_2; ?>" name="var_<?php echo $key.'_'.$key_2; ?>" value="<?php echo $def; ?>" style="width: 190px" />
41
+ <br/>
42
+ <?php }
43
+ } else {
44
+ // If no variables and no description available, output a text
45
+ // to inform the user that it's an insert snippet only.
46
+ if (empty($snippet['description'])) { ?>
47
+ <p class="howto"><?php _e('This snippet is insert only, no variables defined.', PostSnippets::TEXT_DOMAIN); ?></p>
48
+ <?php }
49
+ } ?>
50
+ </div><!-- #ps-tabs-<?php echo $key; ?> -->
51
+ <?php }
52
+ // Close the tabs and dialog divs ?>
53
+ </div><!-- #post-snippets-tabs -->
54
+ </div><!-- #post-snippets-dialog -->
55
+ </div><!-- .hidden -->
56
+ <!-- END: Post Snippets UI Dialog -->