Version Description
Download this release
Release Info
Developer | artstorm |
Plugin | Post Snippets |
Version | 2.2.2 |
Comparing to | |
See all releases |
Code changes from version 2.2.1 to 2.2.2
- lang/post-snippets.pot +1 -1
- lib/PostSnippets/WPEditor.php +319 -0
- post-snippets.php +2 -325
- readme.txt +9 -5
lang/post-snippets.pot
CHANGED
@@ -8,7 +8,7 @@ msgid ""
|
|
8 |
msgstr ""
|
9 |
"Project-Id-Version: PACKAGE VERSION\n"
|
10 |
"Report-Msgid-Bugs-To: \n"
|
11 |
-
"POT-Creation-Date: 2013-05-
|
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"
|
8 |
msgstr ""
|
9 |
"Project-Id-Version: PACKAGE VERSION\n"
|
10 |
"Report-Msgid-Bugs-To: \n"
|
11 |
+
"POT-Creation-Date: 2013-05-10 00:14+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"
|
lib/PostSnippets/WPEditor.php
ADDED
@@ -0,0 +1,319 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Post Snippets WP Editor.
|
4 |
+
*
|
5 |
+
* @author Johan Steen <artstorm at gmail dot com>
|
6 |
+
* @link http://johansteen.se/
|
7 |
+
*/
|
8 |
+
class PostSnippets_WPEditor
|
9 |
+
{
|
10 |
+
const TINYMCE_PLUGIN_NAME = 'post_snippets';
|
11 |
+
|
12 |
+
public function __construct()
|
13 |
+
{
|
14 |
+
|
15 |
+
// Add TinyMCE button
|
16 |
+
add_action('init', array(&$this, 'addTinymceButton'));
|
17 |
+
|
18 |
+
// Add Editor QuickTag button:
|
19 |
+
add_action(
|
20 |
+
'admin_print_footer_scripts',
|
21 |
+
array(&$this,'addQuicktagButton'),
|
22 |
+
100
|
23 |
+
);
|
24 |
+
|
25 |
+
add_action('admin_head', array(&$this,'jqueryUiDialog'));
|
26 |
+
add_action('admin_footer', array(&$this,'addJqueryUiDialog'));
|
27 |
+
|
28 |
+
// Adds the JS and HTML code in the header and footer for the jQuery
|
29 |
+
// insert UI dialog in the editor
|
30 |
+
add_action('admin_init', array(&$this,'enqueueAssets'));
|
31 |
+
}
|
32 |
+
|
33 |
+
|
34 |
+
// -------------------------------------------------------------------------
|
35 |
+
// WordPress Editor Buttons
|
36 |
+
// -------------------------------------------------------------------------
|
37 |
+
|
38 |
+
/**
|
39 |
+
* Add TinyMCE button.
|
40 |
+
*
|
41 |
+
* Adds filters to add custom buttons to the TinyMCE editor (Visual Editor)
|
42 |
+
* in WordPress.
|
43 |
+
*
|
44 |
+
* @since Post Snippets 1.8.7
|
45 |
+
*/
|
46 |
+
public function addTinymceButton()
|
47 |
+
{
|
48 |
+
// Don't bother doing this stuff if the current user lacks permissions
|
49 |
+
if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
|
50 |
+
return;
|
51 |
+
}
|
52 |
+
|
53 |
+
// Add only in Rich Editor mode
|
54 |
+
if (get_user_option('rich_editing') == 'true') {
|
55 |
+
add_filter(
|
56 |
+
'mce_external_plugins',
|
57 |
+
array(&$this, 'registerTinymcePlugin')
|
58 |
+
);
|
59 |
+
add_filter(
|
60 |
+
'mce_buttons',
|
61 |
+
array(&$this, 'registerTinymceButton')
|
62 |
+
);
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
/**
|
67 |
+
* Register TinyMCE button.
|
68 |
+
*
|
69 |
+
* Pushes the custom TinyMCE button into the array of with button names.
|
70 |
+
* 'separator' or '|' can be pushed to the array as well. See the link
|
71 |
+
* for all available TinyMCE controls.
|
72 |
+
*
|
73 |
+
* @see wp-includes/class-wp-editor.php
|
74 |
+
* @link http://www.tinymce.com/wiki.php/Buttons/controls
|
75 |
+
* @since Post Snippets 1.8.7
|
76 |
+
*
|
77 |
+
* @param array $buttons Filter supplied array of buttons to modify
|
78 |
+
* @return array The modified array with buttons
|
79 |
+
*/
|
80 |
+
public function registerTinymceButton($buttons)
|
81 |
+
{
|
82 |
+
array_push($buttons, 'separator', self::TINYMCE_PLUGIN_NAME);
|
83 |
+
return $buttons;
|
84 |
+
}
|
85 |
+
|
86 |
+
/**
|
87 |
+
* Register TinyMCE plugin.
|
88 |
+
*
|
89 |
+
* Adds the absolute URL for the TinyMCE plugin to the associative array of
|
90 |
+
* plugins. Array structure: 'plugin_name' => 'plugin_url'
|
91 |
+
*
|
92 |
+
* @see wp-includes/class-wp-editor.php
|
93 |
+
* @since Post Snippets 1.8.7
|
94 |
+
*
|
95 |
+
* @param array $plugins Filter supplied array of plugins to modify
|
96 |
+
* @return array The modified array with plugins
|
97 |
+
*/
|
98 |
+
public function registerTinymcePlugin($plugins)
|
99 |
+
{
|
100 |
+
// Load the TinyMCE plugin, editor_plugin.js, into the array
|
101 |
+
$plugins[self::TINYMCE_PLUGIN_NAME] =
|
102 |
+
plugins_url('/tinymce/editor_plugin.js?ver=1.9', PostSnippets::FILE);
|
103 |
+
|
104 |
+
return $plugins;
|
105 |
+
}
|
106 |
+
|
107 |
+
|
108 |
+
/**
|
109 |
+
* Adds a QuickTag button to the HTML editor.
|
110 |
+
*
|
111 |
+
* Compatible with WordPress 3.3 and newer.
|
112 |
+
*
|
113 |
+
* @see wp-includes/js/quicktags.dev.js -> qt.addButton()
|
114 |
+
* @since Post Snippets 1.8.6
|
115 |
+
*/
|
116 |
+
public function addQuicktagButton()
|
117 |
+
{
|
118 |
+
echo "\n<!-- START: Add QuickTag button for Post Snippets -->\n";
|
119 |
+
?>
|
120 |
+
<script type="text/javascript" charset="utf-8">
|
121 |
+
if(typeof QTags != 'undefined'){
|
122 |
+
QTags.addButton('post_snippets_id', 'Post Snippets', qt_post_snippets);
|
123 |
+
function qt_post_snippets() {
|
124 |
+
post_snippets_caller = 'html';
|
125 |
+
jQuery("#post-snippets-dialog").dialog("open");
|
126 |
+
}
|
127 |
+
}
|
128 |
+
</script>
|
129 |
+
<?php
|
130 |
+
echo "\n<!-- END: Add QuickTag button for Post Snippets -->\n";
|
131 |
+
}
|
132 |
+
|
133 |
+
|
134 |
+
// -------------------------------------------------------------------------
|
135 |
+
// JavaScript / jQuery handling for the post editor
|
136 |
+
// -------------------------------------------------------------------------
|
137 |
+
|
138 |
+
/**
|
139 |
+
* Enqueues the necessary scripts and styles for the plugins
|
140 |
+
*
|
141 |
+
* @since Post Snippets 1.7
|
142 |
+
*/
|
143 |
+
public function enqueueAssets()
|
144 |
+
{
|
145 |
+
wp_enqueue_script('jquery-ui-dialog');
|
146 |
+
wp_enqueue_script('jquery-ui-tabs');
|
147 |
+
wp_enqueue_style('wp-jquery-ui-dialog');
|
148 |
+
|
149 |
+
# Adds the CSS stylesheet for the jQuery UI dialog
|
150 |
+
$style_url = plugins_url('/assets/post-snippets.css', PostSnippets::FILE);
|
151 |
+
wp_register_style('post-snippets', $style_url, false, '2.0');
|
152 |
+
wp_enqueue_style('post-snippets');
|
153 |
+
}
|
154 |
+
|
155 |
+
/**
|
156 |
+
* jQuery control for the dialog and Javascript needed to insert snippets into the editor
|
157 |
+
*
|
158 |
+
* @since Post Snippets 1.7
|
159 |
+
*/
|
160 |
+
public function jqueryUiDialog()
|
161 |
+
{
|
162 |
+
echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
|
163 |
+
echo "<script type='text/javascript'>\n";
|
164 |
+
|
165 |
+
# Prepare the snippets and shortcodes into javascript variables
|
166 |
+
# so they can be inserted into the editor, and get the variables replaced
|
167 |
+
# with user defined strings.
|
168 |
+
$snippets = get_option(PostSnippets::OPTION_KEY, array());
|
169 |
+
foreach ($snippets as $key => $snippet) {
|
170 |
+
if ($snippet['shortcode']) {
|
171 |
+
# Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
|
172 |
+
# so {varnameX} can be replaced at runtime.
|
173 |
+
$var_arr = explode(",", $snippet['vars']);
|
174 |
+
$variables = '';
|
175 |
+
if (!empty($var_arr[0])) {
|
176 |
+
foreach ($var_arr as $var) {
|
177 |
+
// '[test2 yet="{yet}" mupp=per="{mupp=per}" content="{content}"]';
|
178 |
+
$var = $this->stripDefaultVal($var);
|
179 |
+
|
180 |
+
$variables .= ' ' . $var . '="{' . $var . '}"';
|
181 |
+
}
|
182 |
+
}
|
183 |
+
$shortcode = $snippet['title'] . $variables;
|
184 |
+
echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
|
185 |
+
} else {
|
186 |
+
// To use $snippet is probably not a good naming convention here.
|
187 |
+
// rename to js_snippet or something?
|
188 |
+
$snippet = $snippet['snippet'];
|
189 |
+
# Fixes for potential collisions:
|
190 |
+
/* Replace <> with char codes, otherwise </script> in a snippet will break it */
|
191 |
+
$snippet = str_replace('<', '\x3C', str_replace('>', '\x3E', $snippet));
|
192 |
+
/* Escape " with \" */
|
193 |
+
$snippet = str_replace('"', '\"', $snippet);
|
194 |
+
/* Remove CR and replace LF with \n to keep formatting */
|
195 |
+
$snippet = str_replace(chr(13), '', str_replace(chr(10), '\n', $snippet));
|
196 |
+
# Print out the variable containing the snippet
|
197 |
+
echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
|
198 |
+
}
|
199 |
+
}
|
200 |
+
?>
|
201 |
+
|
202 |
+
jQuery(document).ready(function($){
|
203 |
+
<?php
|
204 |
+
# Create js variables for all form fields
|
205 |
+
foreach ($snippets as $key => $snippet) {
|
206 |
+
$var_arr = explode(",", $snippet['vars']);
|
207 |
+
if (!empty($var_arr[0])) {
|
208 |
+
foreach ($var_arr as $key_2 => $var) {
|
209 |
+
$varname = "var_" . $key . "_" . $key_2;
|
210 |
+
echo "var {$varname} = $( \"#{$varname}\" );\n";
|
211 |
+
}
|
212 |
+
}
|
213 |
+
}
|
214 |
+
?>
|
215 |
+
|
216 |
+
var $tabs = $("#post-snippets-tabs").tabs();
|
217 |
+
|
218 |
+
$(function() {
|
219 |
+
$( "#post-snippets-dialog" ).dialog({
|
220 |
+
autoOpen: false,
|
221 |
+
modal: true,
|
222 |
+
dialogClass: 'wp-dialog',
|
223 |
+
buttons: {
|
224 |
+
Cancel: function() {
|
225 |
+
$( this ).dialog( "close" );
|
226 |
+
},
|
227 |
+
"Insert": function() {
|
228 |
+
$( this ).dialog( "close" );
|
229 |
+
var selected = $tabs.tabs('option', 'selected');
|
230 |
+
<?php
|
231 |
+
foreach ($snippets as $key => $snippet) {
|
232 |
+
?>
|
233 |
+
if (selected == <?php echo $key; ?>) {
|
234 |
+
insert_snippet = postsnippet_<?php echo $key; ?>;
|
235 |
+
<?php
|
236 |
+
$var_arr = explode(",", $snippet['vars']);
|
237 |
+
if (!empty($var_arr[0])) {
|
238 |
+
foreach ($var_arr as $key_2 => $var) {
|
239 |
+
$varname = "var_" . $key . "_" . $key_2; ?>
|
240 |
+
insert_snippet = insert_snippet.replace(/\{<?php
|
241 |
+
echo $this->stripDefaultVal($var);
|
242 |
+
?>\}/g, <?php echo $varname; ?>.val());
|
243 |
+
<?php
|
244 |
+
echo "\n";
|
245 |
+
}
|
246 |
+
}
|
247 |
+
?>
|
248 |
+
}
|
249 |
+
<?php
|
250 |
+
}
|
251 |
+
?>
|
252 |
+
|
253 |
+
// Decide what method to use to insert the snippet depending
|
254 |
+
// from what editor the window was opened from
|
255 |
+
if (post_snippets_caller == 'html') {
|
256 |
+
// HTML editor in WordPress 3.3 and greater
|
257 |
+
QTags.insertContent(insert_snippet);
|
258 |
+
} else if (post_snippets_caller == 'html_pre33') {
|
259 |
+
// HTML editor in WordPress below 3.3.
|
260 |
+
edInsertContent(post_snippets_canvas, insert_snippet);
|
261 |
+
} else {
|
262 |
+
// Visual Editor
|
263 |
+
post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
|
264 |
+
}
|
265 |
+
|
266 |
+
}
|
267 |
+
},
|
268 |
+
width: 500,
|
269 |
+
});
|
270 |
+
});
|
271 |
+
});
|
272 |
+
|
273 |
+
// Global variables to keep track on the canvas instance and from what editor
|
274 |
+
// that opened the Post Snippets popup.
|
275 |
+
var post_snippets_canvas;
|
276 |
+
var post_snippets_caller = '';
|
277 |
+
|
278 |
+
<?php
|
279 |
+
echo "</script>\n";
|
280 |
+
echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
|
281 |
+
}
|
282 |
+
|
283 |
+
/**
|
284 |
+
* Build jQuery UI Window.
|
285 |
+
*
|
286 |
+
* Creates the jQuery for Post Editor popup window, its snippet tabs and the
|
287 |
+
* form fields to enter variables.
|
288 |
+
*
|
289 |
+
* @since Post Snippets 1.7
|
290 |
+
*/
|
291 |
+
public function addJqueryUiDialog()
|
292 |
+
{
|
293 |
+
$data = array('snippets' => get_option(PostSnippets::OPTION_KEY, array()));
|
294 |
+
echo PostSnippets_View::render('jquery-ui-dialog', $data);
|
295 |
+
}
|
296 |
+
|
297 |
+
/**
|
298 |
+
* Strip Default Value.
|
299 |
+
*
|
300 |
+
* Checks if a variable string contains a default value, and if it does it
|
301 |
+
* will strip it away and return the string with only the variable name
|
302 |
+
* kept.
|
303 |
+
*
|
304 |
+
* @since Post Snippets 1.9.3
|
305 |
+
* @param string $variable The variable to check for default value
|
306 |
+
* @return string The variable without any default value
|
307 |
+
*/
|
308 |
+
public function stripDefaultVal($variable)
|
309 |
+
{
|
310 |
+
// Check if variable contains a default defintion
|
311 |
+
$def_pos = strpos($variable, '=');
|
312 |
+
|
313 |
+
if ($def_pos !== false) {
|
314 |
+
$split = str_split($variable, $def_pos);
|
315 |
+
$variable = $split[0];
|
316 |
+
}
|
317 |
+
return $variable;
|
318 |
+
}
|
319 |
+
}
|
post-snippets.php
CHANGED
@@ -5,7 +5,7 @@ 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 |
|
@@ -43,7 +43,6 @@ class PostSnippets
|
|
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 |
|
@@ -70,25 +69,9 @@ class PostSnippets
|
|
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 |
$this->createShortcodes();
|
77 |
-
|
78 |
-
// Adds the JS and HTML code in the header and footer for the jQuery
|
79 |
-
// insert UI dialog in the editor
|
80 |
-
add_action('admin_init', array(&$this,'enqueueAssets'));
|
81 |
-
add_action('admin_head', array(&$this,'jqueryUiDialog'));
|
82 |
-
add_action('admin_footer', array(&$this,'addJqueryUiDialog'));
|
83 |
-
|
84 |
-
// Add Editor QuickTag button:
|
85 |
-
add_action(
|
86 |
-
'admin_print_footer_scripts',
|
87 |
-
array(&$this,'addQuicktagButton'),
|
88 |
-
100
|
89 |
-
);
|
90 |
-
|
91 |
new PostSnippets_Admin;
|
|
|
92 |
}
|
93 |
|
94 |
/**
|
@@ -155,295 +138,6 @@ class PostSnippets
|
|
155 |
}
|
156 |
|
157 |
|
158 |
-
// -------------------------------------------------------------------------
|
159 |
-
// WordPress Editor Buttons
|
160 |
-
// -------------------------------------------------------------------------
|
161 |
-
|
162 |
-
/**
|
163 |
-
* Add TinyMCE button.
|
164 |
-
*
|
165 |
-
* Adds filters to add custom buttons to the TinyMCE editor (Visual Editor)
|
166 |
-
* in WordPress.
|
167 |
-
*
|
168 |
-
* @since Post Snippets 1.8.7
|
169 |
-
*/
|
170 |
-
public function addTinymceButton()
|
171 |
-
{
|
172 |
-
// Don't bother doing this stuff if the current user lacks permissions
|
173 |
-
if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
|
174 |
-
return;
|
175 |
-
}
|
176 |
-
|
177 |
-
// Add only in Rich Editor mode
|
178 |
-
if (get_user_option('rich_editing') == 'true') {
|
179 |
-
add_filter(
|
180 |
-
'mce_external_plugins',
|
181 |
-
array(&$this, 'registerTinymcePlugin')
|
182 |
-
);
|
183 |
-
add_filter(
|
184 |
-
'mce_buttons',
|
185 |
-
array(&$this, 'registerTinymceButton')
|
186 |
-
);
|
187 |
-
}
|
188 |
-
}
|
189 |
-
|
190 |
-
/**
|
191 |
-
* Register TinyMCE button.
|
192 |
-
*
|
193 |
-
* Pushes the custom TinyMCE button into the array of with button names.
|
194 |
-
* 'separator' or '|' can be pushed to the array as well. See the link
|
195 |
-
* for all available TinyMCE controls.
|
196 |
-
*
|
197 |
-
* @see wp-includes/class-wp-editor.php
|
198 |
-
* @link http://www.tinymce.com/wiki.php/Buttons/controls
|
199 |
-
* @since Post Snippets 1.8.7
|
200 |
-
*
|
201 |
-
* @param array $buttons Filter supplied array of buttons to modify
|
202 |
-
* @return array The modified array with buttons
|
203 |
-
*/
|
204 |
-
public function registerTinymceButton($buttons)
|
205 |
-
{
|
206 |
-
array_push($buttons, 'separator', self::TINYMCE_PLUGIN_NAME);
|
207 |
-
return $buttons;
|
208 |
-
}
|
209 |
-
|
210 |
-
/**
|
211 |
-
* Register TinyMCE plugin.
|
212 |
-
*
|
213 |
-
* Adds the absolute URL for the TinyMCE plugin to the associative array of
|
214 |
-
* plugins. Array structure: 'plugin_name' => 'plugin_url'
|
215 |
-
*
|
216 |
-
* @see wp-includes/class-wp-editor.php
|
217 |
-
* @since Post Snippets 1.8.7
|
218 |
-
*
|
219 |
-
* @param array $plugins Filter supplied array of plugins to modify
|
220 |
-
* @return array The modified array with plugins
|
221 |
-
*/
|
222 |
-
public function registerTinymcePlugin($plugins)
|
223 |
-
{
|
224 |
-
// Load the TinyMCE plugin, editor_plugin.js, into the array
|
225 |
-
$plugins[self::TINYMCE_PLUGIN_NAME] =
|
226 |
-
plugins_url('/tinymce/editor_plugin.js?ver=1.9', __FILE__);
|
227 |
-
|
228 |
-
return $plugins;
|
229 |
-
}
|
230 |
-
|
231 |
-
/**
|
232 |
-
* Adds a QuickTag button to the HTML editor.
|
233 |
-
*
|
234 |
-
* Compatible with WordPress 3.3 and newer.
|
235 |
-
*
|
236 |
-
* @see wp-includes/js/quicktags.dev.js -> qt.addButton()
|
237 |
-
* @since Post Snippets 1.8.6
|
238 |
-
*/
|
239 |
-
public function addQuicktagButton()
|
240 |
-
{
|
241 |
-
// Only run the function on post edit screens
|
242 |
-
if (function_exists('get_current_screen')) {
|
243 |
-
$screen = get_current_screen();
|
244 |
-
if ($screen->base != 'post') {
|
245 |
-
return;
|
246 |
-
}
|
247 |
-
}
|
248 |
-
|
249 |
-
echo "\n<!-- START: Add QuickTag button for Post Snippets -->\n";
|
250 |
-
?>
|
251 |
-
<script type="text/javascript" charset="utf-8">
|
252 |
-
QTags.addButton( 'post_snippets_id', 'Post Snippets', qt_post_snippets );
|
253 |
-
function qt_post_snippets() {
|
254 |
-
post_snippets_caller = 'html';
|
255 |
-
jQuery( "#post-snippets-dialog" ).dialog( "open" );
|
256 |
-
}
|
257 |
-
</script>
|
258 |
-
<?php
|
259 |
-
echo "\n<!-- END: Add QuickTag button for Post Snippets -->\n";
|
260 |
-
}
|
261 |
-
|
262 |
-
// -------------------------------------------------------------------------
|
263 |
-
// JavaScript / jQuery handling for the post editor
|
264 |
-
// -------------------------------------------------------------------------
|
265 |
-
|
266 |
-
/**
|
267 |
-
* jQuery control for the dialog and Javascript needed to insert snippets into the editor
|
268 |
-
*
|
269 |
-
* @since Post Snippets 1.7
|
270 |
-
*/
|
271 |
-
public function jqueryUiDialog()
|
272 |
-
{
|
273 |
-
// Only run the function on post edit screens
|
274 |
-
if (function_exists('get_current_screen')) {
|
275 |
-
$screen = get_current_screen();
|
276 |
-
if ($screen->base != 'post') {
|
277 |
-
return;
|
278 |
-
}
|
279 |
-
}
|
280 |
-
|
281 |
-
echo "\n<!-- START: Post Snippets jQuery UI and related functions -->\n";
|
282 |
-
echo "<script type='text/javascript'>\n";
|
283 |
-
|
284 |
-
# Prepare the snippets and shortcodes into javascript variables
|
285 |
-
# so they can be inserted into the editor, and get the variables replaced
|
286 |
-
# with user defined strings.
|
287 |
-
$snippets = get_option(self::OPTION_KEY, array());
|
288 |
-
foreach ($snippets as $key => $snippet) {
|
289 |
-
if ($snippet['shortcode']) {
|
290 |
-
# Build a long string of the variables, ie: varname1={varname1} varname2={varname2}
|
291 |
-
# so {varnameX} can be replaced at runtime.
|
292 |
-
$var_arr = explode(",", $snippet['vars']);
|
293 |
-
$variables = '';
|
294 |
-
if (!empty($var_arr[0])) {
|
295 |
-
foreach ($var_arr as $var) {
|
296 |
-
// '[test2 yet="{yet}" mupp=per="{mupp=per}" content="{content}"]';
|
297 |
-
$var = $this->stripDefaultVal($var);
|
298 |
-
|
299 |
-
$variables .= ' ' . $var . '="{' . $var . '}"';
|
300 |
-
}
|
301 |
-
}
|
302 |
-
$shortcode = $snippet['title'] . $variables;
|
303 |
-
echo "var postsnippet_{$key} = '[" . $shortcode . "]';\n";
|
304 |
-
} else {
|
305 |
-
// To use $snippet is probably not a good naming convention here.
|
306 |
-
// rename to js_snippet or something?
|
307 |
-
$snippet = $snippet['snippet'];
|
308 |
-
# Fixes for potential collisions:
|
309 |
-
/* Replace <> with char codes, otherwise </script> in a snippet will break it */
|
310 |
-
$snippet = str_replace('<', '\x3C', str_replace('>', '\x3E', $snippet));
|
311 |
-
/* Escape " with \" */
|
312 |
-
$snippet = str_replace('"', '\"', $snippet);
|
313 |
-
/* Remove CR and replace LF with \n to keep formatting */
|
314 |
-
$snippet = str_replace(chr(13), '', str_replace(chr(10), '\n', $snippet));
|
315 |
-
# Print out the variable containing the snippet
|
316 |
-
echo "var postsnippet_{$key} = \"" . $snippet . "\";\n";
|
317 |
-
}
|
318 |
-
}
|
319 |
-
?>
|
320 |
-
|
321 |
-
jQuery(document).ready(function($){
|
322 |
-
<?php
|
323 |
-
# Create js variables for all form fields
|
324 |
-
foreach ($snippets as $key => $snippet) {
|
325 |
-
$var_arr = explode(",", $snippet['vars']);
|
326 |
-
if (!empty($var_arr[0])) {
|
327 |
-
foreach ($var_arr as $key_2 => $var) {
|
328 |
-
$varname = "var_" . $key . "_" . $key_2;
|
329 |
-
echo "var {$varname} = $( \"#{$varname}\" );\n";
|
330 |
-
}
|
331 |
-
}
|
332 |
-
}
|
333 |
-
?>
|
334 |
-
|
335 |
-
var $tabs = $("#post-snippets-tabs").tabs();
|
336 |
-
|
337 |
-
$(function() {
|
338 |
-
$( "#post-snippets-dialog" ).dialog({
|
339 |
-
autoOpen: false,
|
340 |
-
modal: true,
|
341 |
-
dialogClass: 'wp-dialog',
|
342 |
-
buttons: {
|
343 |
-
Cancel: function() {
|
344 |
-
$( this ).dialog( "close" );
|
345 |
-
},
|
346 |
-
"Insert": function() {
|
347 |
-
$( this ).dialog( "close" );
|
348 |
-
var selected = $tabs.tabs('option', 'selected');
|
349 |
-
<?php
|
350 |
-
foreach ($snippets as $key => $snippet) {
|
351 |
-
?>
|
352 |
-
if (selected == <?php echo $key; ?>) {
|
353 |
-
insert_snippet = postsnippet_<?php echo $key; ?>;
|
354 |
-
<?php
|
355 |
-
$var_arr = explode(",", $snippet['vars']);
|
356 |
-
if (!empty($var_arr[0])) {
|
357 |
-
foreach ($var_arr as $key_2 => $var) {
|
358 |
-
$varname = "var_" . $key . "_" . $key_2; ?>
|
359 |
-
insert_snippet = insert_snippet.replace(/\{<?php
|
360 |
-
echo $this->stripDefaultVal($var);
|
361 |
-
?>\}/g, <?php echo $varname; ?>.val());
|
362 |
-
<?php
|
363 |
-
echo "\n";
|
364 |
-
}
|
365 |
-
}
|
366 |
-
?>
|
367 |
-
}
|
368 |
-
<?php
|
369 |
-
}
|
370 |
-
?>
|
371 |
-
|
372 |
-
// Decide what method to use to insert the snippet depending
|
373 |
-
// from what editor the window was opened from
|
374 |
-
if (post_snippets_caller == 'html') {
|
375 |
-
// HTML editor in WordPress 3.3 and greater
|
376 |
-
QTags.insertContent(insert_snippet);
|
377 |
-
} else if (post_snippets_caller == 'html_pre33') {
|
378 |
-
// HTML editor in WordPress below 3.3.
|
379 |
-
edInsertContent(post_snippets_canvas, insert_snippet);
|
380 |
-
} else {
|
381 |
-
// Visual Editor
|
382 |
-
post_snippets_canvas.execCommand('mceInsertContent', false, insert_snippet);
|
383 |
-
}
|
384 |
-
|
385 |
-
}
|
386 |
-
},
|
387 |
-
width: 500,
|
388 |
-
});
|
389 |
-
});
|
390 |
-
});
|
391 |
-
|
392 |
-
// Global variables to keep track on the canvas instance and from what editor
|
393 |
-
// that opened the Post Snippets popup.
|
394 |
-
var post_snippets_canvas;
|
395 |
-
var post_snippets_caller = '';
|
396 |
-
|
397 |
-
<?php
|
398 |
-
echo "</script>\n";
|
399 |
-
echo "\n<!-- END: Post Snippets jQuery UI and related functions -->\n";
|
400 |
-
}
|
401 |
-
|
402 |
-
/**
|
403 |
-
* Build jQuery UI Window.
|
404 |
-
*
|
405 |
-
* Creates the jQuery for Post Editor popup window, its snippet tabs and the
|
406 |
-
* form fields to enter variables.
|
407 |
-
*
|
408 |
-
* @since Post Snippets 1.7
|
409 |
-
*/
|
410 |
-
public function addJqueryUiDialog()
|
411 |
-
{
|
412 |
-
// Only run the function on post edit screens
|
413 |
-
if (function_exists('get_current_screen')) {
|
414 |
-
$screen = get_current_screen();
|
415 |
-
if ($screen->base != 'post') {
|
416 |
-
return;
|
417 |
-
}
|
418 |
-
}
|
419 |
-
|
420 |
-
$data = array('snippets' => get_option(self::OPTION_KEY, array()));
|
421 |
-
echo PostSnippets_View::render('jquery-ui-dialog', $data);
|
422 |
-
}
|
423 |
-
|
424 |
-
/**
|
425 |
-
* Strip Default Value.
|
426 |
-
*
|
427 |
-
* Checks if a variable string contains a default value, and if it does it
|
428 |
-
* will strip it away and return the string with only the variable name
|
429 |
-
* kept.
|
430 |
-
*
|
431 |
-
* @since Post Snippets 1.9.3
|
432 |
-
* @param string $variable The variable to check for default value
|
433 |
-
* @return string The variable without any default value
|
434 |
-
*/
|
435 |
-
public function stripDefaultVal($variable)
|
436 |
-
{
|
437 |
-
// Check if variable contains a default defintion
|
438 |
-
$def_pos = strpos($variable, '=');
|
439 |
-
|
440 |
-
if ($def_pos !== false) {
|
441 |
-
$split = str_split($variable, $def_pos);
|
442 |
-
$variable = $split[0];
|
443 |
-
}
|
444 |
-
return $variable;
|
445 |
-
}
|
446 |
-
|
447 |
// -------------------------------------------------------------------------
|
448 |
// Shortcode
|
449 |
// -------------------------------------------------------------------------
|
@@ -537,23 +231,6 @@ class PostSnippets
|
|
537 |
return addslashes($content);
|
538 |
}
|
539 |
|
540 |
-
/**
|
541 |
-
* Enqueues the necessary scripts and styles for the plugins
|
542 |
-
*
|
543 |
-
* @since Post Snippets 1.7
|
544 |
-
*/
|
545 |
-
public function enqueueAssets()
|
546 |
-
{
|
547 |
-
wp_enqueue_script('jquery-ui-dialog');
|
548 |
-
wp_enqueue_script('jquery-ui-tabs');
|
549 |
-
wp_enqueue_style('wp-jquery-ui-dialog');
|
550 |
-
|
551 |
-
# Adds the CSS stylesheet for the jQuery UI dialog
|
552 |
-
$style_url = plugins_url('/assets/post-snippets.css', __FILE__);
|
553 |
-
wp_register_style('post-snippets', $style_url, false, '2.0');
|
554 |
-
wp_enqueue_style('post-snippets');
|
555 |
-
}
|
556 |
-
|
557 |
// -------------------------------------------------------------------------
|
558 |
// Helpers
|
559 |
// -------------------------------------------------------------------------
|
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.2
|
9 |
License: GPLv2 or later
|
10 |
Text Domain: post-snippets
|
11 |
|
43 |
const MIN_WP_VERSION = '3.3';
|
44 |
const OPTION_KEY = 'post_snippets_options';
|
45 |
const USER_META_KEY = 'post_snippets';
|
|
|
46 |
const TEXT_DOMAIN = 'post-snippets';
|
47 |
const FILE = __FILE__;
|
48 |
|
69 |
add_action('init', array($this, 'textDomain'));
|
70 |
register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
|
71 |
|
|
|
|
|
|
|
72 |
$this->createShortcodes();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
new PostSnippets_Admin;
|
74 |
+
new PostSnippets_WPEditor;
|
75 |
}
|
76 |
|
77 |
/**
|
138 |
}
|
139 |
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
// -------------------------------------------------------------------------
|
142 |
// Shortcode
|
143 |
// -------------------------------------------------------------------------
|
231 |
return addslashes($content);
|
232 |
}
|
233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
// -------------------------------------------------------------------------
|
235 |
// Helpers
|
236 |
// -------------------------------------------------------------------------
|
readme.txt
CHANGED
@@ -4,7 +4,7 @@ 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 |
|
@@ -117,12 +117,16 @@ Contributions are appreciated and encouraged.
|
|
117 |
|
118 |
== Changelog ==
|
119 |
|
|
|
|
|
|
|
|
|
120 |
= Version 2.2.1 - 1 May 2013 =
|
121 |
* Added an option to give users with `edit_posts` capability access to the
|
122 |
-
Post Snippets Admin.
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
* Optimizes code for the admin section.
|
127 |
|
128 |
= Version 2.2 - 26 Apr 2013 =
|
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.2
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
117 |
|
118 |
== Changelog ==
|
119 |
|
120 |
+
= Version 2.2.2 - 10 May 2013 =
|
121 |
+
* The Post Snippets buttons now works everywhere there is a wp_editor present
|
122 |
+
and not only on post/page edit screens.
|
123 |
+
|
124 |
= Version 2.2.1 - 1 May 2013 =
|
125 |
* Added an option to give users with `edit_posts` capability access to the
|
126 |
+
Post Snippets Admin. Add
|
127 |
+
`define('POST_SNIPPETS_ALLOW_EDIT_POSTS', true);`
|
128 |
+
to wp-config.php to enable access for those users. Fixes
|
129 |
+
[issue #12](https://github.com/artstorm/post-snippets/issues/12).
|
130 |
* Optimizes code for the admin section.
|
131 |
|
132 |
= Version 2.2 - 26 Apr 2013 =
|