Post Snippets - Version 2.5.2

Version Description

Download this release

Release Info

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

Code changes from version 2.5.1 to 2.5.2

post-snippets.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: https://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: https://johansteen.se/
8
- Version: 2.5.1
9
  License: GPLv2 or later
10
  Text Domain: post-snippets
11
  Domain Path: /lang
@@ -78,23 +78,23 @@ class PostSnippets
78
  register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
79
  add_action('after_setup_theme', array(&$this, 'phpExecState'));
80
 
81
- new PostSnippets_Admin;
82
- new PostSnippets_WPEditor;
83
- new PostSnippets_Shortcode;
84
  }
85
 
86
  /**
87
  * PSR-0 compliant autoloader to load classes as needed.
88
  *
89
- * @param string $classname The name of the class
90
- * @return null Return early if the class name does not start with the
91
- * correct prefix
92
  */
93
  public static function autoload($className)
94
  {
95
  if (__CLASS__ !== mb_substr($className, 0, strlen(__CLASS__))) {
96
  return;
97
  }
 
98
  $className = ltrim($className, '\\');
99
  $fileName = '';
100
  $namespace = '';
@@ -104,10 +104,9 @@ class PostSnippets
104
  $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
105
  $fileName .= DIRECTORY_SEPARATOR;
106
  }
107
- $fileName .= str_replace('_', DIRECTORY_SEPARATOR, 'src_'.$className);
108
- $fileName .='.php';
109
 
110
- require $fileName;
111
  }
112
 
113
  /**
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: https://johansteen.se/
8
+ Version: 2.5.2
9
  License: GPLv2 or later
10
  Text Domain: post-snippets
11
  Domain Path: /lang
78
  register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
79
  add_action('after_setup_theme', array(&$this, 'phpExecState'));
80
 
81
+ new \PostSnippets\Admin;
82
+ new \PostSnippets\WPEditor;
83
+ new \PostSnippets\Shortcode;
84
  }
85
 
86
  /**
87
  * PSR-0 compliant autoloader to load classes as needed.
88
  *
89
+ * @param string $classname
90
+ * @return void
 
91
  */
92
  public static function autoload($className)
93
  {
94
  if (__CLASS__ !== mb_substr($className, 0, strlen(__CLASS__))) {
95
  return;
96
  }
97
+
98
  $className = ltrim($className, '\\');
99
  $fileName = '';
100
  $namespace = '';
104
  $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
105
  $fileName .= DIRECTORY_SEPARATOR;
106
  }
107
+ $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className);
 
108
 
109
+ require 'src'.DIRECTORY_SEPARATOR.$fileName.'.php';
110
  }
111
 
112
  /**
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: artstorm
3
  Donate link: https://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: 4.4
7
- Stable tag: 2.5.1
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -128,6 +128,11 @@ Contributions are appreciated and encouraged.
128
 
129
  == Changelog ==
130
 
 
 
 
 
 
131
  = Version 2.5.1 - 6 Feb 2016 =
132
  * Makes strings inside javascripts translatable.
133
  * Removes `{content}` from shortcode output if the shortcode is not enclosed.
3
  Donate link: https://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: 4.4.2
7
+ Stable tag: 2.5.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
128
 
129
  == Changelog ==
130
 
131
+ = Version 2.5.2 - 7 Feb 2016 =
132
+ * Fixes an issue than can occur when other plugins bootstrap WordPress Admin
133
+ and then includes `admin_head` but not `admin_footer`, like download monitor.
134
+ * Refactors parts of the code to prepare for a future snippet storage update.
135
+
136
  = Version 2.5.1 - 6 Feb 2016 =
137
  * Makes strings inside javascripts translatable.
138
  * Removes `{content}` from shortcode output if the shortcode is not enclosed.
src/PostSnippets/Admin.php CHANGED
@@ -1,4 +1,6 @@
1
  <?php
 
 
2
  /**
3
  * Post Snippets Settings.
4
  *
@@ -8,7 +10,7 @@
8
  * @author Johan Steen <artstorm at gmail dot com>
9
  * @link https://johansteen.se/
10
  */
11
- class PostSnippets_Admin
12
  {
13
  /**
14
  * Plugin settings.
@@ -53,16 +55,16 @@ class PostSnippets_Admin
53
  'Post Snippets Options',
54
  'Post Snippets',
55
  $capability,
56
- PostSnippets::FILE,
57
  array(&$this, 'optionsPage')
58
  );
59
- new PostSnippets_Help($optionPage);
60
  } else {
61
  $optionPage = add_options_page(
62
  'Post Snippets',
63
  'Post Snippets',
64
  'edit_posts',
65
- PostSnippets::FILE,
66
  array(&$this, 'overviewPage')
67
  );
68
  }
@@ -82,7 +84,7 @@ class PostSnippets_Admin
82
  {
83
  wp_register_script(
84
  'post-snippets',
85
- plugins_url('/assets/post-snippets.js', PostSnippets::FILE),
86
  array('jquery')
87
  );
88
 
@@ -128,7 +130,7 @@ class PostSnippets_Admin
128
  */
129
  public function actionLinks($links, $file)
130
  {
131
- $pluginFile = plugin_basename(dirname(PostSnippets::FILE));
132
  $pluginFile .= '/post-snippets.php';
133
 
134
  if ($file == $pluginFile) {
@@ -154,7 +156,7 @@ class PostSnippets_Admin
154
  && isset($_POST['update_snippets_nonce'])
155
  && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
156
  ) {
157
- $snippets = get_option(PostSnippets::OPTION_KEY);
158
  if (empty($snippets)) {
159
  $snippets = array();
160
  }
@@ -172,7 +174,7 @@ class PostSnippets_Admin
172
  )
173
  );
174
 
175
- update_option(PostSnippets::OPTION_KEY, $snippets);
176
  $this->message(
177
  __(
178
  'A snippet named Untitled has been added.',
@@ -191,7 +193,7 @@ class PostSnippets_Admin
191
  && isset($_POST['update_snippets_nonce'])
192
  && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
193
  ) {
194
- $snippets = get_option(PostSnippets::OPTION_KEY);
195
 
196
  if (empty($snippets) || !isset($_POST['checked'])) {
197
  $this->message(
@@ -208,7 +210,7 @@ class PostSnippets_Admin
208
  }
209
  }
210
 
211
- update_option(PostSnippets::OPTION_KEY, $newsnippets);
212
  $this->message(
213
  __(
214
  'Selected snippets have been deleted.',
@@ -227,7 +229,7 @@ class PostSnippets_Admin
227
  && isset($_POST['update_snippets_nonce'])
228
  && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
229
  ) {
230
- $snippets = get_option(PostSnippets::OPTION_KEY);
231
  if (!empty($snippets)) {
232
  foreach ($snippets as $key => $value) {
233
  $new_snippets[$key]['title'] = trim($_POST[$key.'_title']);
@@ -245,7 +247,7 @@ class PostSnippets_Admin
245
  $new_snippets[$key]['snippet'] = wp_specialchars_decode(trim(stripslashes($_POST[$key.'_snippet'])), ENT_NOQUOTES);
246
  $new_snippets[$key]['description'] = wp_specialchars_decode(trim(stripslashes($_POST[$key.'_description'])), ENT_NOQUOTES);
247
  }
248
- update_option(PostSnippets::OPTION_KEY, $new_snippets);
249
  $this->message(__('Snippets have been updated.', 'post-snippets'));
250
  }
251
  }
@@ -265,7 +267,7 @@ class PostSnippets_Admin
265
  ) {
266
  $id = get_current_user_id();
267
  $render = isset($_POST['render']) ? true : false;
268
- update_user_meta($id, PostSnippets::USER_META_KEY, $render);
269
  }
270
  }
271
 
@@ -280,7 +282,7 @@ class PostSnippets_Admin
280
  private function getUserOptions()
281
  {
282
  $id = get_current_user_id();
283
- $options = get_user_meta($id, PostSnippets::USER_META_KEY, true);
284
  return $options;
285
  }
286
 
@@ -362,7 +364,7 @@ class PostSnippets_Admin
362
  private function tabSnippets()
363
  {
364
  $data = array();
365
- echo PostSnippets_View::render('admin_snippets', $data);
366
  }
367
 
368
  /**
@@ -373,7 +375,7 @@ class PostSnippets_Admin
373
  private function tabOptions()
374
  {
375
  $data = array();
376
- echo PostSnippets_View::render('admin_options', $data);
377
  }
378
 
379
  /**
@@ -383,7 +385,7 @@ class PostSnippets_Admin
383
  */
384
  private function tabTools()
385
  {
386
- $ie = new PostSnippets_ImportExport();
387
 
388
  // Create header and export html form
389
  printf("<h3>%s</h3>", __('Import/Export', 'post-snippets'));
@@ -430,7 +432,7 @@ class PostSnippets_Admin
430
  echo '</form>';
431
 
432
  // Snippet List
433
- $snippets = get_option(PostSnippets::OPTION_KEY);
434
  if (!empty($snippets)) {
435
  foreach ($snippets as $key => $snippet) {
436
  echo "<hr style='border: none;border-top:1px dashed #aaa; margin:24px 0;' />";
@@ -487,11 +489,11 @@ class PostSnippets_Admin
487
  */
488
  protected function registerSettings()
489
  {
490
- $this->settings = get_option(PostSnippets::SETTINGS);
491
 
492
  register_setting(
493
- PostSnippets::SETTINGS,
494
- PostSnippets::SETTINGS
495
  );
496
 
497
  add_settings_section(
@@ -529,7 +531,7 @@ class PostSnippets_Admin
529
  false;
530
 
531
  echo "<input type='checkbox' id='{$args['id']}' ";
532
- echo "name='".PostSnippets::SETTINGS."[{$args['id']}]' value='1' ";
533
  if ($checked) {
534
  echo 'checked ';
535
  }
1
  <?php
2
+ namespace PostSnippets;
3
+
4
  /**
5
  * Post Snippets Settings.
6
  *
10
  * @author Johan Steen <artstorm at gmail dot com>
11
  * @link https://johansteen.se/
12
  */
13
+ class Admin
14
  {
15
  /**
16
  * Plugin settings.
55
  'Post Snippets Options',
56
  'Post Snippets',
57
  $capability,
58
+ \PostSnippets::FILE,
59
  array(&$this, 'optionsPage')
60
  );
61
+ new Help($optionPage);
62
  } else {
63
  $optionPage = add_options_page(
64
  'Post Snippets',
65
  'Post Snippets',
66
  'edit_posts',
67
+ \PostSnippets::FILE,
68
  array(&$this, 'overviewPage')
69
  );
70
  }
84
  {
85
  wp_register_script(
86
  'post-snippets',
87
+ plugins_url('/assets/post-snippets.js', \PostSnippets::FILE),
88
  array('jquery')
89
  );
90
 
130
  */
131
  public function actionLinks($links, $file)
132
  {
133
+ $pluginFile = plugin_basename(dirname(\PostSnippets::FILE));
134
  $pluginFile .= '/post-snippets.php';
135
 
136
  if ($file == $pluginFile) {
156
  && isset($_POST['update_snippets_nonce'])
157
  && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
158
  ) {
159
+ $snippets = get_option(\PostSnippets::OPTION_KEY);
160
  if (empty($snippets)) {
161
  $snippets = array();
162
  }
174
  )
175
  );
176
 
177
+ update_option(\PostSnippets::OPTION_KEY, $snippets);
178
  $this->message(
179
  __(
180
  'A snippet named Untitled has been added.',
193
  && isset($_POST['update_snippets_nonce'])
194
  && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
195
  ) {
196
+ $snippets = get_option(\PostSnippets::OPTION_KEY);
197
 
198
  if (empty($snippets) || !isset($_POST['checked'])) {
199
  $this->message(
210
  }
211
  }
212
 
213
+ update_option(\PostSnippets::OPTION_KEY, $newsnippets);
214
  $this->message(
215
  __(
216
  'Selected snippets have been deleted.',
229
  && isset($_POST['update_snippets_nonce'])
230
  && wp_verify_nonce($_POST['update_snippets_nonce'], 'update_snippets')
231
  ) {
232
+ $snippets = get_option(\PostSnippets::OPTION_KEY);
233
  if (!empty($snippets)) {
234
  foreach ($snippets as $key => $value) {
235
  $new_snippets[$key]['title'] = trim($_POST[$key.'_title']);
247
  $new_snippets[$key]['snippet'] = wp_specialchars_decode(trim(stripslashes($_POST[$key.'_snippet'])), ENT_NOQUOTES);
248
  $new_snippets[$key]['description'] = wp_specialchars_decode(trim(stripslashes($_POST[$key.'_description'])), ENT_NOQUOTES);
249
  }
250
+ update_option(\PostSnippets::OPTION_KEY, $new_snippets);
251
  $this->message(__('Snippets have been updated.', 'post-snippets'));
252
  }
253
  }
267
  ) {
268
  $id = get_current_user_id();
269
  $render = isset($_POST['render']) ? true : false;
270
+ update_user_meta($id, \PostSnippets::USER_META_KEY, $render);
271
  }
272
  }
273
 
282
  private function getUserOptions()
283
  {
284
  $id = get_current_user_id();
285
+ $options = get_user_meta($id, \PostSnippets::USER_META_KEY, true);
286
  return $options;
287
  }
288
 
364
  private function tabSnippets()
365
  {
366
  $data = array();
367
+ echo View::render('admin_snippets', $data);
368
  }
369
 
370
  /**
375
  private function tabOptions()
376
  {
377
  $data = array();
378
+ echo View::render('admin_options', $data);
379
  }
380
 
381
  /**
385
  */
386
  private function tabTools()
387
  {
388
+ $ie = new ImportExport();
389
 
390
  // Create header and export html form
391
  printf("<h3>%s</h3>", __('Import/Export', 'post-snippets'));
432
  echo '</form>';
433
 
434
  // Snippet List
435
+ $snippets = get_option(\PostSnippets::OPTION_KEY);
436
  if (!empty($snippets)) {
437
  foreach ($snippets as $key => $snippet) {
438
  echo "<hr style='border: none;border-top:1px dashed #aaa; margin:24px 0;' />";
489
  */
490
  protected function registerSettings()
491
  {
492
+ $this->settings = get_option(\PostSnippets::SETTINGS);
493
 
494
  register_setting(
495
+ \PostSnippets::SETTINGS,
496
+ \PostSnippets::SETTINGS
497
  );
498
 
499
  add_settings_section(
531
  false;
532
 
533
  echo "<input type='checkbox' id='{$args['id']}' ";
534
+ echo "name='".\PostSnippets::SETTINGS."[{$args['id']}]' value='1' ";
535
  if ($checked) {
536
  echo 'checked ';
537
  }
src/PostSnippets/Help.php CHANGED
@@ -1,11 +1,13 @@
1
  <?php
 
 
2
  /**
3
  * Handles the plugin help screen.
4
  *
5
  * @author Johan Steen <artstorm at gmail dot com>
6
  * @link https://johansteen.se/
7
  */
8
- class PostSnippets_Help
9
  {
10
  /**
11
  * Define actions.
@@ -109,6 +111,6 @@ class PostSnippets_Help
109
  */
110
  private function content($tab)
111
  {
112
- return PostSnippets_View::render($tab);
113
  }
114
  }
1
  <?php
2
+ namespace PostSnippets;
3
+
4
  /**
5
  * Handles the plugin help screen.
6
  *
7
  * @author Johan Steen <artstorm at gmail dot com>
8
  * @link https://johansteen.se/
9
  */
10
+ class Help
11
  {
12
  /**
13
  * Define actions.
111
  */
112
  private function content($tab)
113
  {
114
+ return View::render($tab);
115
  }
116
  }
src/PostSnippets/ImportExport.php CHANGED
@@ -1,4 +1,6 @@
1
  <?php
 
 
2
  /**
3
  * Post Snippets I/O.
4
  *
@@ -7,7 +9,7 @@
7
  * @author Johan Steen <artstorm at gmail dot com>
8
  * @link https://johansteen.se/
9
  */
10
- class PostSnippets_ImportExport
11
  {
12
  const FILE_CFG = 'post-snippets-export.cfg';
13
  const FILE_ZIP = 'post-snippets-export.zip';
@@ -100,7 +102,7 @@ class PostSnippets_ImportExport
100
  $snippets
101
  );
102
  update_option(
103
- PostSnippets::OPTION_KEY,
104
  unserialize($snippets)
105
  );
106
  }
@@ -147,7 +149,7 @@ class PostSnippets_ImportExport
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'] . '/';
1
  <?php
2
+ namespace PostSnippets;
3
+
4
  /**
5
  * Post Snippets I/O.
6
  *
9
  * @author Johan Steen <artstorm at gmail dot com>
10
  * @link https://johansteen.se/
11
  */
12
+ class ImportExport
13
  {
14
  const FILE_CFG = 'post-snippets-export.cfg';
15
  const FILE_ZIP = 'post-snippets-export.zip';
102
  $snippets
103
  );
104
  update_option(
105
+ \PostSnippets::OPTION_KEY,
106
  unserialize($snippets)
107
  );
108
  }
149
  */
150
  private function createExportFile()
151
  {
152
+ $snippets = serialize(get_option(\PostSnippets::OPTION_KEY));
153
  $snippets = apply_filters('post_snippets_export', $snippets);
154
  $dir = wp_upload_dir();
155
  $upload_dir = $dir['basedir'] . '/';
src/PostSnippets/Shortcode.php CHANGED
@@ -1,11 +1,13 @@
1
  <?php
 
 
2
  /**
3
  * Shortcode Handling.
4
  *
5
  * @author Johan Steen <artstorm at gmail dot com>
6
  * @link https://johansteen.se/
7
  */
8
- class PostSnippets_Shortcode
9
  {
10
  public function __construct()
11
  {
@@ -17,7 +19,7 @@ class PostSnippets_Shortcode
17
  */
18
  public function create()
19
  {
20
- $snippets = get_option(PostSnippets::OPTION_KEY);
21
  if (!empty($snippets)) {
22
  foreach ($snippets as $snippet) {
23
  // If shortcode is enabled for the snippet, and a snippet has been entered, register it as a shortcode.
@@ -66,7 +68,7 @@ class PostSnippets_Shortcode
66
  // Handle PHP shortcodes
67
  $php = "'. $snippet["php"] .'";
68
  if ($php == true) {
69
- $snippet = PostSnippets_Shortcode::phpEval( $snippet );
70
  }
71
 
72
  // Strip escaping and execute nested shortcodes
1
  <?php
2
+ namespace PostSnippets;
3
+
4
  /**
5
  * Shortcode Handling.
6
  *
7
  * @author Johan Steen <artstorm at gmail dot com>
8
  * @link https://johansteen.se/
9
  */
10
+ class Shortcode
11
  {
12
  public function __construct()
13
  {
19
  */
20
  public function create()
21
  {
22
+ $snippets = get_option(\PostSnippets::OPTION_KEY);
23
  if (!empty($snippets)) {
24
  foreach ($snippets as $snippet) {
25
  // If shortcode is enabled for the snippet, and a snippet has been entered, register it as a shortcode.
68
  // Handle PHP shortcodes
69
  $php = "'. $snippet["php"] .'";
70
  if ($php == true) {
71
+ $snippet = \PostSnippets\Shortcode::phpEval( $snippet );
72
  }
73
 
74
  // Strip escaping and execute nested shortcodes
src/PostSnippets/View.php CHANGED
@@ -1,11 +1,13 @@
1
  <?php
 
 
2
  /**
3
  * View Handling.
4
  *
5
  * @author Johan Steen <artstorm at gmail dot com>
6
  * @link https://johansteen.se/
7
  */
8
- class PostSnippets_View
9
  {
10
  /**
11
  * Render a View.
1
  <?php
2
+ namespace PostSnippets;
3
+
4
  /**
5
  * View Handling.
6
  *
7
  * @author Johan Steen <artstorm at gmail dot com>
8
  * @link https://johansteen.se/
9
  */
10
+ class View
11
  {
12
  /**
13
  * Render a View.
src/PostSnippets/WPEditor.php CHANGED
@@ -1,11 +1,13 @@
1
  <?php
 
 
2
  /**
3
  * Post Snippets WP Editor.
4
  *
5
  * @author Johan Steen <artstorm at gmail dot com>
6
  * @link https://johansteen.se/
7
  */
8
- class PostSnippets_WPEditor
9
  {
10
  const TINYMCE_PLUGIN_NAME = 'post_snippets';
11
 
@@ -106,7 +108,7 @@ class PostSnippets_WPEditor
106
 
107
  // Load the TinyMCE plugin, editor_plugin.js, into the array
108
  $plugins[self::TINYMCE_PLUGIN_NAME] =
109
- plugins_url('/tinymce/editor_plugin.js?ver=1.9', PostSnippets::FILE);
110
 
111
  return $plugins;
112
  }
@@ -158,7 +160,7 @@ class PostSnippets_WPEditor
158
  wp_enqueue_style('wp-jquery-ui-dialog');
159
 
160
  # Adds the CSS stylesheet for the jQuery UI dialog
161
- $style_url = plugins_url('/assets/post-snippets.css', PostSnippets::FILE);
162
  wp_register_style('post-snippets', $style_url, false, '2.0');
163
  wp_enqueue_style('post-snippets');
164
  }
@@ -174,17 +176,16 @@ class PostSnippets_WPEditor
174
  return;
175
  }
176
 
177
- echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
178
- echo "<script type='text/javascript'>\n";
179
-
180
  # Prepare the snippets and shortcodes into javascript variables
181
  # so they can be inserted into the editor, and get the variables replaced
182
  # with user defined strings.
183
- $snippets = get_option(PostSnippets::OPTION_KEY, array());
184
 
185
  //Let other plugins change the snippets array
186
  $snippets = apply_filters('post_snippets_snippets_list', $snippets);
187
 
 
 
188
  foreach ($snippets as $key => $snippet) {
189
  if ($snippet['shortcode']) {
190
  # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
@@ -199,7 +200,7 @@ class PostSnippets_WPEditor
199
  }
200
  }
201
  $shortcode = $snippet['title'] . $variables;
202
- echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
203
  } else {
204
  // To use $snippet is probably not a good naming convention here.
205
  // rename to js_snippet or something?
@@ -212,105 +213,18 @@ class PostSnippets_WPEditor
212
  /* Remove CR and replace LF with \n to keep formatting */
213
  $snippet = str_replace(chr(13), '', str_replace(chr(10), '\n', $snippet));
214
  # Print out the variable containing the snippet
215
- echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
216
  }
217
  }
218
  ?>
219
 
220
- jQuery(document).ready(function($){
221
  <?php
222
- # Create js variables for all form fields
223
- foreach ($snippets as $key => $snippet) {
224
- $var_arr = explode(",", $snippet['vars']);
225
- if (!empty($var_arr[0])) {
226
- foreach ($var_arr as $key_2 => $var) {
227
- $varname = "var_" . $key . "_" . $key_2;
228
- echo "var {$varname} = $( \"#{$varname}\" );\n";
229
- }
230
- }
231
- }
232
- ?>
233
-
234
- var tabs = $("#post-snippets-tabs").tabs();
235
-
236
- $(function() {
237
- $( "#post-snippets-dialog" ).dialog({
238
- autoOpen: false,
239
- modal: true,
240
- dialogClass: 'wp-dialog',
241
- buttons: {
242
- Cancel: function() {
243
- $( this ).dialog( "close" );
244
- },
245
- "Insert": function() {
246
- $(this).dialog("close");
247
- <?php
248
- global $wp_version;
249
- if (version_compare($wp_version, '3.5', '<')) {
250
- ?>
251
- var selected = tabs.tabs('option', 'selected');
252
- <?php
253
-
254
- } else {
255
- ?>
256
- var selected = tabs.tabs('option', 'active');
257
- <?php
258
-
259
- }
260
- ?>
261
- <?php
262
- foreach ($snippets as $key => $snippet) {
263
- ?>
264
- if (selected == <?php echo $key;
265
- ?>) {
266
- insert_snippet = postsnippet_<?php echo $key;
267
- ?>;
268
- <?php
269
- $var_arr = explode(",", $snippet['vars']);
270
- if (!empty($var_arr[0])) {
271
- foreach ($var_arr as $key_2 => $var) {
272
- $varname = "var_" . $key . "_" . $key_2;
273
- ?>
274
- insert_snippet = insert_snippet.replace(/\{<?php
275
- echo $this->stripDefaultVal($var);
276
- ?>\}/g, <?php echo $varname;
277
- ?>.val());
278
- <?php
279
- echo "\n";
280
- }
281
- }
282
- ?>
283
- }
284
- <?php
285
-
286
- }
287
- ?>
288
-
289
- // Decide what method to use to insert the snippet depending
290
- // from what editor the window was opened from
291
- if (post_snippets_caller == 'html') {
292
- // HTML editor in WordPress 3.3 and greater
293
- QTags.insertContent(insert_snippet);
294
- } else {
295
- // Visual Editor
296
- post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
297
- }
298
-
299
- }
300
- },
301
- width: 500,
302
- });
303
- });
304
- });
305
-
306
- // Global variables to keep track on the canvas instance and from what editor
307
- // that opened the Post Snippets popup.
308
- var post_snippets_canvas;
309
- var post_snippets_caller = '';
310
-
311
- <?php
312
- echo "</script>\n";
313
- echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
314
  }
315
 
316
  /**
@@ -327,13 +241,13 @@ class PostSnippets_WPEditor
327
  return;
328
  }
329
 
330
- $snippets = get_option(PostSnippets::OPTION_KEY, array());
331
 
332
  //Let other plugins change the snippets array
333
  $snippets = apply_filters('post_snippets_snippets_list', $snippets);
334
  $data = array('snippets' => $snippets);
335
 
336
- echo PostSnippets_View::render('jquery-ui-dialog', $data);
337
  }
338
 
339
  /**
@@ -343,7 +257,7 @@ class PostSnippets_WPEditor
343
  */
344
  protected function isEditingPost()
345
  {
346
- $settings = get_option(PostSnippets::SETTINGS);
347
  $exclude = isset($settings['exclude_from_custom_editors']) ?
348
  $settings['exclude_from_custom_editors'] :
349
  false;
1
  <?php
2
+ namespace PostSnippets;
3
+
4
  /**
5
  * Post Snippets WP Editor.
6
  *
7
  * @author Johan Steen <artstorm at gmail dot com>
8
  * @link https://johansteen.se/
9
  */
10
+ class WPEditor
11
  {
12
  const TINYMCE_PLUGIN_NAME = 'post_snippets';
13
 
108
 
109
  // Load the TinyMCE plugin, editor_plugin.js, into the array
110
  $plugins[self::TINYMCE_PLUGIN_NAME] =
111
+ plugins_url('/tinymce/editor_plugin.js?ver=1.9', \PostSnippets::FILE);
112
 
113
  return $plugins;
114
  }
160
  wp_enqueue_style('wp-jquery-ui-dialog');
161
 
162
  # Adds the CSS stylesheet for the jQuery UI dialog
163
+ $style_url = plugins_url('/assets/post-snippets.css', \PostSnippets::FILE);
164
  wp_register_style('post-snippets', $style_url, false, '2.0');
165
  wp_enqueue_style('post-snippets');
166
  }
176
  return;
177
  }
178
 
 
 
 
179
  # Prepare the snippets and shortcodes into javascript variables
180
  # so they can be inserted into the editor, and get the variables replaced
181
  # with user defined strings.
182
+ $snippets = get_option(\PostSnippets::OPTION_KEY, array());
183
 
184
  //Let other plugins change the snippets array
185
  $snippets = apply_filters('post_snippets_snippets_list', $snippets);
186
 
187
+ $snippetStack = array();
188
+
189
  foreach ($snippets as $key => $snippet) {
190
  if ($snippet['shortcode']) {
191
  # Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
200
  }
201
  }
202
  $shortcode = $snippet['title'] . $variables;
203
+ array_push($snippetStack, "var postsnippet_{$key} = '[" . $shortcode . "]';\n");
204
  } else {
205
  // To use $snippet is probably not a good naming convention here.
206
  // rename to js_snippet or something?
213
  /* Remove CR and replace LF with \n to keep formatting */
214
  $snippet = str_replace(chr(13), '', str_replace(chr(10), '\n', $snippet));
215
  # Print out the variable containing the snippet
216
+ array_push($snippetStack, "var postsnippet_{$key} = \"" . $snippet . "\";\n");
217
  }
218
  }
219
  ?>
220
 
 
221
  <?php
222
+ $data = array(
223
+ 'methods' => $this,
224
+ 'snippets' => $snippets,
225
+ 'snippetStack' => $snippetStack
226
+ );
227
+ echo View::render('jquery_ui_dialog_head', $data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
  }
229
 
230
  /**
241
  return;
242
  }
243
 
244
+ $snippets = get_option(\PostSnippets::OPTION_KEY, array());
245
 
246
  //Let other plugins change the snippets array
247
  $snippets = apply_filters('post_snippets_snippets_list', $snippets);
248
  $data = array('snippets' => $snippets);
249
 
250
+ echo View::render('jquery_ui_dialog_footer', $data);
251
  }
252
 
253
  /**
257
  */
258
  protected function isEditingPost()
259
  {
260
+ $settings = get_option(\PostSnippets::SETTINGS);
261
  $exclude = isset($settings['exclude_from_custom_editors']) ?
262
  $settings['exclude_from_custom_editors'] :
263
  false;
views/admin_options.php CHANGED
@@ -1,6 +1,6 @@
1
  <form method="post" action="options.php">
2
  <?php
3
- settings_fields(PostSnippets::SETTINGS);
4
  do_settings_sections('post-snippets');
5
  submit_button();
6
  ?>
1
  <form method="post" action="options.php">
2
  <?php
3
+ settings_fields(\PostSnippets::SETTINGS);
4
  do_settings_sections('post-snippets');
5
  submit_button();
6
  ?>
views/admin_snippets.php CHANGED
@@ -22,7 +22,7 @@
22
 
23
  <tbody>
24
  <?php
25
- $snippets = get_option(PostSnippets::OPTION_KEY);
26
  if (!empty($snippets)) {
27
  foreach ($snippets as $key => $snippet) {
28
  ?>
@@ -41,13 +41,13 @@ if (!empty($snippets)) {
41
  <br/>
42
  <br/>
43
  <?php
44
- PostSnippets_Admin::checkbox(__('Shortcode', 'post-snippets'), $key.'_shortcode',
45
  $snippet['shortcode']);
46
 
47
  echo '<br/><strong>Shortcode Options:</strong><br/>';
48
 
49
  if (!defined('POST_SNIPPETS_DISABLE_PHP')) {
50
- PostSnippets_Admin::checkbox(
51
  __('PHP Code', 'post-snippets'),
52
  $key.'_php',
53
  $snippet['php']
@@ -55,7 +55,7 @@ if (!empty($snippets)) {
55
  }
56
 
57
  $wptexturize = isset($snippet['wptexturize']) ? $snippet['wptexturize'] : false;
58
- PostSnippets_Admin::checkbox('wptexturize', $key.'_wptexturize', $wptexturize);
59
  ?>
60
  </td>
61
  <td class='desc'>
@@ -79,7 +79,7 @@ if (!empty($snippets)) {
79
  </table>
80
 
81
  <?php
82
- PostSnippets_Admin::submit('update-snippets', __('Update Snippets', 'post-snippets'));
83
- PostSnippets_Admin::submit('add-snippet', __('Add New Snippet', 'post-snippets'), 'button-secondary', false);
84
- PostSnippets_Admin::submit('delete-snippets', __('Delete Selected', 'post-snippets'), 'button-secondary', false);
85
  echo '</form>';
22
 
23
  <tbody>
24
  <?php
25
+ $snippets = get_option(\PostSnippets::OPTION_KEY);
26
  if (!empty($snippets)) {
27
  foreach ($snippets as $key => $snippet) {
28
  ?>
41
  <br/>
42
  <br/>
43
  <?php
44
+ \PostSnippets\Admin::checkbox(__('Shortcode', 'post-snippets'), $key.'_shortcode',
45
  $snippet['shortcode']);
46
 
47
  echo '<br/><strong>Shortcode Options:</strong><br/>';
48
 
49
  if (!defined('POST_SNIPPETS_DISABLE_PHP')) {
50
+ \PostSnippets\Admin::checkbox(
51
  __('PHP Code', 'post-snippets'),
52
  $key.'_php',
53
  $snippet['php']
55
  }
56
 
57
  $wptexturize = isset($snippet['wptexturize']) ? $snippet['wptexturize'] : false;
58
+ \PostSnippets\Admin::checkbox('wptexturize', $key.'_wptexturize', $wptexturize);
59
  ?>
60
  </td>
61
  <td class='desc'>
79
  </table>
80
 
81
  <?php
82
+ \PostSnippets\Admin::submit('update-snippets', __('Update Snippets', 'post-snippets'));
83
+ \PostSnippets\Admin::submit('add-snippet', __('Add New Snippet', 'post-snippets'), 'button-secondary', false);
84
+ \PostSnippets\Admin::submit('delete-snippets', __('Delete Selected', 'post-snippets'), 'button-secondary', false);
85
  echo '</form>';
views/help/php.php CHANGED
@@ -8,7 +8,7 @@
8
  <?php _e('Check this image for an example PHP snippet:', 'post-snippets'); ?>
9
  </p>
10
 
11
- <img src="<?php echo plugins_url('assets/img/help/php-snippet.jpg', PostSnippets::FILE); ?>" />
12
 
13
  <p>
14
  <?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:', 'post-snippets'); ?>
8
  <?php _e('Check this image for an example PHP snippet:', 'post-snippets'); ?>
9
  </p>
10
 
11
+ <img src="<?php echo plugins_url('assets/img/help/php-snippet.jpg', \PostSnippets::FILE); ?>" />
12
 
13
  <p>
14
  <?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:', 'post-snippets'); ?>
views/help/post.php CHANGED
@@ -1,12 +1,12 @@
1
  <h2><?php _e('The Post Editor', 'post-snippets'); ?></h2>
2
 
3
- <img src="<?php echo plugins_url('assets/img/help/post-snippets-editor.jpg', PostSnippets::FILE); ?>" />
4
 
5
  <p>
6
  <?php _e('With your snippets set up, you\'ll find a new button in your post editor (circled in the image above), which you can click to open the Post Snippet window to insert one of your snippets in your post.', 'post-snippets'); ?>
7
  </p>
8
 
9
- <img src="<?php echo plugins_url('assets/img/help/post-snippets-window.jpg', PostSnippets::FILE); ?>" />
10
 
11
  <p>
12
  <?php _e('When you click this button, you get a window with all your available snippets. Each snippet has its own tab in the Post Snippet window, so you can select which snippet to insert. If you have set up custom variables for the snippet, you can now enter the values for these variables before clicking insert and they will be inserted in the correct places in the snippet string.', 'post-snippets'); ?>
1
  <h2><?php _e('The Post Editor', 'post-snippets'); ?></h2>
2
 
3
+ <img src="<?php echo plugins_url('assets/img/help/post-snippets-editor.jpg', \PostSnippets::FILE); ?>" />
4
 
5
  <p>
6
  <?php _e('With your snippets set up, you\'ll find a new button in your post editor (circled in the image above), which you can click to open the Post Snippet window to insert one of your snippets in your post.', 'post-snippets'); ?>
7
  </p>
8
 
9
+ <img src="<?php echo plugins_url('assets/img/help/post-snippets-window.jpg', \PostSnippets::FILE); ?>" />
10
 
11
  <p>
12
  <?php _e('When you click this button, you get a window with all your available snippets. Each snippet has its own tab in the Post Snippet window, so you can select which snippet to insert. If you have set up custom variables for the snippet, you can now enter the values for these variables before clicking insert and they will be inserted in the correct places in the snippet string.', 'post-snippets'); ?>
views/{jquery-ui-dialog.php → jquery_ui_dialog_footer.php} RENAMED
File without changes
views/jquery_ui_dialog_head.php ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ <!-- START: Post Snippets jQuery UI and related functions -->
3
+ <script type='text/javascript'>
4
+ <?php
5
+ foreach ($snippetStack as $snippetVarString) {
6
+ echo $snippetVarString;
7
+ } ?>
8
+
9
+ jQuery(document).ready(function($){
10
+ <?php
11
+ # Create js variables for all form fields
12
+ foreach ($snippets as $key => $snippet) {
13
+ $var_arr = explode(",", $snippet['vars']);
14
+ if (!empty($var_arr[0])) {
15
+ foreach ($var_arr as $key_2 => $var) {
16
+ $varname = "var_" . $key . "_" . $key_2;
17
+ echo "var {$varname} = $( \"#{$varname}\" );\n";
18
+ }
19
+ }
20
+ }
21
+ ?>
22
+ // We do the check to see if post snippets dialog is in the footer.
23
+ // Some plugins, like 'download monitor' uses WordPress admin bootstrap and
24
+ // then bring in admin_head but not admin_footer. So to allow other plugins
25
+ // to do that 'hack', we bail out in thoise cases.
26
+ if ($("#post-snippets-tabs").length>0) {
27
+
28
+ var tabs = $("#post-snippets-tabs").tabs();
29
+
30
+ $(function() {
31
+ $("#post-snippets-dialog").dialog({
32
+ autoOpen: false,
33
+ modal: true,
34
+ dialogClass: 'wp-dialog',
35
+ buttons: {
36
+ Cancel: function() {
37
+ $(this).dialog("close");
38
+ },
39
+ "Insert": function() {
40
+ $(this).dialog("close");
41
+ <?php
42
+ global $wp_version;
43
+ if (version_compare($wp_version, '3.5', '<')) {
44
+ ?>
45
+ var selected = tabs.tabs('option', 'selected');
46
+ <?php
47
+
48
+ } else {
49
+ ?>
50
+ var selected = tabs.tabs('option', 'active');
51
+ <?php
52
+
53
+ }
54
+
55
+ foreach ($snippets as $key => $snippet) {
56
+ ?>
57
+ if (selected == <?php echo $key;
58
+ ?>) {
59
+ insert_snippet = postsnippet_<?php echo $key;
60
+ ?>;
61
+ <?php
62
+ $var_arr = explode(",", $snippet['vars']);
63
+ if (!empty($var_arr[0])) {
64
+ foreach ($var_arr as $key_2 => $var) {
65
+ $varname = "var_" . $key . "_" . $key_2;
66
+ ?>
67
+ insert_snippet = insert_snippet.replace(/\{<?php echo $methods->stripDefaultVal($var);
68
+ ?>\}/g, <?php echo $varname;
69
+ ?>.val());
70
+ <?php
71
+
72
+ }
73
+ }
74
+ ?>
75
+ }
76
+
77
+ <?php
78
+
79
+ }
80
+ ?>
81
+
82
+ // Decide what method to use to insert the snippet depending
83
+ // from what editor the window was opened from
84
+ if (post_snippets_caller == 'html') {
85
+ // HTML editor in WordPress 3.3 and greater
86
+ QTags.insertContent(insert_snippet);
87
+ } else {
88
+ // Visual Editor
89
+ post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
90
+ }
91
+ }
92
+ },
93
+ width: 500,
94
+ });
95
+ });
96
+ }
97
+ });
98
+
99
+ // Global variables to keep track on the canvas instance and from what editor
100
+ // that opened the Post Snippets popup.
101
+ var post_snippets_canvas;
102
+ var post_snippets_caller = '';
103
+ </script>
104
+ <!-- END: Post Snippets jQuery UI and related functions -->