PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.1.91
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.1.91
2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 2.7.1 All 27 releases
insert-php / includes / class.helpers.php

class.helpers.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.1.91, at includes/class.helpers.php

465 lines 16.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Helpers tools
5 * @author Webcraftic <wordpress.webraftic@gmail.com>
6 * @copyright (c) 09.11.2017, Webcraftic
7 * @version 1.0
8 */
9 class WINP_Helper {
10
11 private static $meta_options = array();
12
13 /**
14 * @return bool
15 */
16 public static function is_safe_mode() {
17 global $wbcr_inp_safe_mode;
18
19 if ( ! WINP_Plugin::app()->currentUserCan() ) {
20 return false;
21 }
22
23 if ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) {
24 return true;
25 }
26
27 return false;
28 }
29
30 /**
31 * Enables safe mode, in which the php code will not be executed.
32 */
33 public static function enable_safe_mode() {
34 global $wbcr_inp_safe_mode;
35
36 if ( ! WINP_Plugin::app()->currentUserCan() ) {
37 return false;
38 }
39
40 if ( ( ! $wbcr_inp_safe_mode || ! isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
41 $wbcr_inp_safe_mode = true;
42 setcookie( "wbcr-php-snippets-safe-mode", 1, time() + 3600, '/' );
43
44 return true;
45 }
46
47 return false;
48 }
49
50 /**
51 * Disable safe mode, in which the php code will not be executed.
52 */
53 public static function disable_safe_mode() {
54 global $wbcr_inp_safe_mode;
55
56 if ( ! WINP_Plugin::app()->currentUserCan() ) {
57 return;
58 }
59
60 if ( ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
61 $wbcr_inp_safe_mode = false;
62 unset( $_COOKIE['wbcr-php-snippets-safe-mode'] );
63 setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/' );
64
65 wp_safe_redirect( remove_query_arg( array( 'wbcr-php-snippets-disable-safe-mode' ) ) );
66 die();
67 }
68 }
69
70 /**
71 * Should show a page about the plugin or not.
72 *
73 * @return bool
74 */
75 public static function is_need_show_about_page() {
76 $need_show_about = (int)get_option(WINP_Plugin::app()->getOptionName( 'what_new_210' ));
77
78 $is_ajax = WINP_Helper::doing_ajax();
79 $is_cron = WINP_Helper::doing_cron();
80 $is_rest = WINP_Helper::doing_rest_api();
81
82 if ( $need_show_about && ! $is_ajax && ! $is_cron && ! $is_rest ) {
83 return true;
84 }
85
86 return false;
87 }
88
89 /**
90 * Gets and verified available attributes for snippets shortcodes.
91 *
92 * @return array
93 */
94 public static function get_shortcode_data() {
95
96 $snippets = get_posts( array(
97 'post_type' => WINP_SNIPPETS_POST_TYPE,
98 'meta_query' => array(
99 'relation' => 'AND',
100 array(
101 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope',
102 'value' => 'shortcode'
103 ),
104 array(
105 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_activate',
106 'value' => 1
107 )
108 ),
109 'post_status' => 'publish',
110 'numberposts' => - 1
111 ) );
112
113 $result = array();
114
115 if ( ! empty( $snippets ) ) {
116 foreach ( (array) $snippets as $snippet ) {
117 $tag_names = array( 'id' );
118 $snippet_type = WINP_Helper::get_snippet_type( $snippet->ID );
119
120 $available_tags = WINP_Helper::getMetaOption( $snippet->ID, 'snippet_tags' );
121 $available_tags = trim( rtrim( $available_tags ) );
122
123 if ( ! empty( $available_tags ) ) {
124 $available_tags = array_map( 'trim', explode( ',', $available_tags ) );
125 $available_tags = array_unique( $available_tags );
126 } else {
127 if ( $snippet_type !== 'text' ) {
128 $available_tags = array( 'id', 'title' );
129 } else {
130 $available_tags = array( 'id' );
131 }
132 }
133
134 $tags = array(
135 'id' => $snippet->ID,
136 'type' => $snippet_type,
137 'title' => empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title
138 );
139
140 if ( ! empty( $available_tags ) ) {
141 foreach ( (array) $available_tags as $tag ) {
142 if ( '' != $tag ) {
143 if ( 'title' == $tag ) {
144 $tags['title'] = empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title;
145 $tag_names[] = 'title';
146 } else if ( 'id' != $tag ) {
147 $tag = preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '', $tag );
148 $tags[ $tag ] = "";
149 $tag_names[] = $tag;
150 }
151 }
152 }
153 }
154
155 $tags['snippet_tags'] = $tag_names;
156
157 $result[] = $tags;
158 }
159 }
160
161 return $result;
162 }
163
164 /**
165 * Get snippet type
166 *
167 * @param null $post_id
168 *
169 * @return array|mixed|string
170 */
171 public static function get_snippet_type( $post_id = null ) {
172 global $post;
173
174 $_post = $post;
175
176 $snippet_type = isset( $_GET['winp_item'] ) ? sanitize_text_field( $_GET['winp_item'] ) : WINP_SNIPPET_TYPE_PHP;
177
178 if ( empty( $post_id ) && isset( $_GET['post'] ) && ! is_array( $_GET['post'] ) ) {
179 $post_id = esc_attr( $_GET['post'] );
180 }
181
182 if ( ! empty( $post_id ) ) {
183 $_post = get_post( $post_id );
184 }
185
186 if ( ! empty( $_post ) && $_post->post_type === WINP_SNIPPETS_POST_TYPE ) {
187 $_snippet_type = get_post_meta( $_post->ID, WINP_Plugin::app()->getPrefix() . 'snippet_type', true );
188 $snippet_type = $_snippet_type ? $_snippet_type : $snippet_type;
189 }
190
191 return $snippet_type;
192 }
193
194 /**
195 * Checks if the current request is a WP REST API request.
196 *
197 * Case #1: After WP_REST_Request initialisation
198 * Case #2: Support "plain" permalink settings
199 * Case #3: URL Path begins with wp-json/ (your REST prefix)
200 * Also supports WP installations in subfolders
201 *
202 * @since 2.1.0
203 * @return boolean
204 * @author matzeeable https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist
205 */
206 public static function doing_rest_api() {
207 $prefix = rest_get_url_prefix();
208 if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
209 || isset( $_GET['rest_route'] ) // (#2)
210 && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0 ) {
211 return true;
212 }
213
214 // (#3)
215 $rest_url = wp_parse_url( site_url( $prefix ) );
216 $current_url = wp_parse_url( add_query_arg( array() ) );
217
218 return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
219 }
220
221 /**
222 * @since 2.1.0
223 * @return bool
224 */
225 public static function doing_ajax() {
226 if ( function_exists( 'wp_doing_ajax' ) ) {
227 return wp_doing_ajax();
228 }
229
230 return defined( 'DOING_AJAX' ) && DOING_AJAX;
231 }
232
233 /**
234 * @since 2.1.0
235 * @return bool
236 */
237 public static function doing_cron() {
238 if ( function_exists( 'wp_doing_cron' ) ) {
239 return wp_doing_cron();
240 }
241
242 return defined( 'DOING_CRON' ) && DOING_CRON;
243 }
244
245 /**
246 * Get meta option
247 *
248 * @param int $post_id
249 * @param string $option_name
250 * @param mixed $default
251 *
252 * @return mixed|array
253 */
254 public static function getMetaOption( $post_id, $option_name, $default = null ) {
255 $post_id = (int) $post_id;
256
257 if ( ! isset( self::$meta_options[ $post_id ] ) || empty( self::$meta_options[ $post_id ] ) ) {
258 $meta_vals = get_post_meta( $post_id, '', true );
259
260 foreach ( $meta_vals as $name => $val ) {
261 self::$meta_options[ $post_id ][ $name ] = $val[0];
262 }
263 }
264
265 return isset( self::$meta_options[ $post_id ][ WINP_Plugin::app()->getPrefix() . $option_name ] ) ? self::$meta_options[ $post_id ][ WINP_Plugin::app()->getPrefix() . $option_name ] : $default;
266 }
267
268 /**
269 * Udpdate meta option
270 *
271 * @param int $post_id
272 * @param string $option_name
273 * @param mixed $option_value
274 *
275 * @return bool|int
276 */
277 public static function updateMetaOption( $post_id, $option_name, $option_value ) {
278 $post_id = (int) $post_id;
279
280 return update_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name, $option_value );
281 }
282
283 /**
284 * Remove meta option
285 *
286 * @param int $post_id
287 * @param string $option_name
288 *
289 * @return bool|int
290 */
291 public static function removeMetaOption( $post_id, $option_name ) {
292 $post_id = (int) $post_id;
293
294 return delete_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name );
295 }
296
297 /**
298 * Create a demo snippets with examples of use
299 */
300 public static function create_demo_snippets() {
301
302 WINP_Plugin::app()->updatePopulateOption( 'activate_by_default', 1 );
303 WINP_Plugin::app()->updatePopulateOption( 'complete_uninstall', 0 );
304 WINP_Plugin::app()->updatePopulateOption( 'code_editor_theme', 'default' );
305 WINP_Plugin::app()->updatePopulateOption( 'code_editor_indent_with_tabs', 1 );
306 WINP_Plugin::app()->updatePopulateOption( 'code_editor_tab_size', 4 );
307 WINP_Plugin::app()->updatePopulateOption( 'code_editor_indent_unit', 4 );
308 WINP_Plugin::app()->updatePopulateOption( 'code_editor_wrap_lines', 1 );
309 WINP_Plugin::app()->updatePopulateOption( 'code_editor_line_numbers', 1 );
310 WINP_Plugin::app()->updatePopulateOption( 'code_editor_auto_close_brackets', 1 );
311 WINP_Plugin::app()->updatePopulateOption( 'code_editor_highlight_selection_matches', 0 );
312
313 $posts = array(
314 array(
315 'post_title' => __( 'Simple php snippet: Disable emojis', 'insert-php' ),
316 'post_name' => 'simple-php-snippet',
317 'post_content' => '',
318 'meta' => array(
319 'type' => WINP_SNIPPET_TYPE_PHP,
320 'code' => self::get_simple_php_snippet(),
321 'description' => __( 'Emojis are little icons used to express ideas or emotions. While these icons are fun and all, are they really necessary for your WordPress site? This snippet to disable emojis on your site to make it faster.', 'insert-php' ),
322 'tags' => array( 'php', 'disable features' )
323 )
324 ),
325 array(
326 'post_title' => __( 'Simple text snippet: What is Lorem Ipsum?', 'insert-php' ),
327 'post_name' => 'simple-text-snippet',
328 'post_content' => self::get_simple_text_snippet(),
329 'meta' => array(
330 'type' => WINP_SNIPPET_TYPE_TEXT,
331 'description' => __( 'This ordinary maintenance text. With this snippet, you can fill your pages with meaningless English text.', 'insert-php' ),
332 'filters' => 'a:1:{i:0;O:8:"stdClass":2:{s:10:"conditions";a:2:{i:0;O:8:"stdClass":2:{s:4:"type";s:5:"scope";s:10:"conditions";a:1:{i:0;O:8:"stdClass":4:{s:5:"param";s:18:"location-some-page";s:8:"operator";s:6:"equals";s:4:"type";s:6:"select";s:5:"value";s:9:"base_sing";}}}i:1;O:8:"stdClass":2:{s:4:"type";s:5:"scope";s:10:"conditions";a:2:{i:0;O:8:"stdClass":4:{s:5:"param";s:18:"location-post-type";s:8:"operator";s:6:"equals";s:4:"type";s:6:"select";s:5:"value";s:4:"post";}i:1;O:8:"stdClass":4:{s:5:"param";s:18:"location-post-type";s:8:"operator";s:6:"equals";s:4:"type";s:6:"select";s:5:"value";s:4:"page";}}}}s:4:"type";s:6:"showif";}}',
333 'tags' => array( 'text', 'lorem ipsum' )
334 )
335 ),
336 array(
337 'post_title' => __( 'Simple universal snippet: Google analytics tracking', 'insert-php' ),
338 'post_name' => 'simple-universal-snippet',
339 'post_content' => '',
340 'meta' => array(
341 'type' => WINP_SNIPPET_TYPE_UNIVERSAL,
342 'description' => __( 'Google analytics tracking code will be added to all pages before the &lt;/head&gt; tag. Please remember to set the Tracking ID before activating the snippet.' ),
343 'code' => self::get_simple_universal_snippet(),
344 'filters' => 'a:1:{i:0;O:8:"stdClass":2:{s:10:"conditions";a:1:{i:0;O:8:"stdClass":2:{s:4:"type";s:5:"scope";s:10:"conditions";a:1:{i:0;O:8:"stdClass":4:{s:5:"param";s:18:"location-some-page";s:8:"operator";s:6:"equals";s:4:"type";s:6:"select";s:5:"value";s:8:"base_web";}}}}s:4:"type";s:6:"showif";}}',
345 'tags' => array( 'universal', 'tracking' )
346 )
347 )
348 );
349
350 foreach ( $posts as $post ) {
351 // '@' here is to hide unexpected output while plugin activation
352 $post_id = @wp_insert_post( array(
353 'post_content' => $post['post_content'],
354 'post_title' => $post['post_title'],
355 'post_status' => 'publish',
356 'post_type' => WINP_SNIPPETS_POST_TYPE
357 ) );
358
359 if ( ! is_wp_error( $post_id ) ) {
360 if ( isset( $post['meta']['code'] ) ) {
361 WINP_Helper::updateMetaOption( $post_id, 'snippet_code', $post['meta']['code'] );
362 }
363
364 if ( isset( $post['meta']['type'] ) ) {
365 WINP_Helper::updateMetaOption( $post_id, 'snippet_type', $post['meta']['type'] );
366
367 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_PHP ) {
368 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'evrywhere' );
369 }
370 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_TEXT ) {
371 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'shortcode' );
372 }
373 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_UNIVERSAL ) {
374 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'auto' );
375 WINP_Helper::updateMetaOption( $post_id, 'snippet_location', 'header' );
376 }
377 }
378
379 if ( isset( $post['meta']['description'] ) ) {
380 WINP_Helper::updateMetaOption( $post_id, 'snippet_description', $post['meta']['description'] );
381 }
382
383 if ( isset( $post['meta']['filters'] ) && is_serialized( $post['meta']['filters'] ) ) {
384 $unserialized_filters = unserialize( $post['meta']['filters'] );
385 WINP_Helper::updateMetaOption( $post_id, 'snippet_filters', $unserialized_filters );
386 WINP_Helper::updateMetaOption( $post_id, 'changed_filters', 1 );
387 }
388
389 if ( isset( $post['meta']['tags'] ) && ! empty( $post['meta']['tags'] ) ) {
390 if ( ! taxonomy_exists( WINP_SNIPPETS_TAXONOMY ) ) {
391 register_taxonomy( WINP_SNIPPETS_TAXONOMY, WINP_SNIPPETS_POST_TYPE, array() );
392 }
393
394 wp_set_post_terms( $post_id, $post['meta']['tags'], WINP_SNIPPETS_TAXONOMY, true );
395 }
396 }
397 }
398 WINP_Plugin::app()->updatePopulateOption( 'demo_snippets_created', 1 );
399 }
400
401 /**
402 * Returns an example php snippet content
403 *
404 * @return string
405 */
406 protected static function get_simple_php_snippet() {
407 $output = "/**" . PHP_EOL;
408 $output .= "* Disable WP 4.2 emoji" . PHP_EOL;
409 $output .= "*/" . PHP_EOL;
410 $output .= "function ace_remove_emoji() {" . PHP_EOL;
411 $output .= "\tadd_filter( 'emoji_svg_url', '__return_false' );" . PHP_EOL;
412 $output .= "\tremove_action( 'admin_print_styles', 'print_emoji_styles' );" . PHP_EOL;
413 $output .= "\tremove_action( 'wp_head', 'print_emoji_detection_script', 7 );" . PHP_EOL;
414 $output .= "\tremove_action( 'admin_print_scripts', 'print_emoji_detection_script' );" . PHP_EOL;
415 $output .= "\tremove_action( 'wp_print_styles', 'print_emoji_styles' );" . PHP_EOL;
416 $output .= "\tremove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );" . PHP_EOL;
417 $output .= "\tremove_filter( 'the_content_feed', 'wp_staticize_emoji' );" . PHP_EOL;
418 $output .= "\tremove_filter( 'comment_text_rss', 'wp_staticize_emoji' );" . PHP_EOL;
419 $output .= "\t// filter to remove TinyMCE emojis" . PHP_EOL;
420 $output .= "\tadd_filter( 'tiny_mce_plugins', 'ace_disable_emoji_tinymce' );" . PHP_EOL;
421 $output .= "}" . PHP_EOL;
422 $output .= "add_action( 'init', 'ace_remove_emoji' );" . PHP_EOL;
423 $output .= "/**" . PHP_EOL;
424 $output .= "* Remove tinyMCE emoji" . PHP_EOL;
425 $output .= "*/" . PHP_EOL;
426 $output .= "function ace_disable_emoji_tinymce( \$plugins ) {" . PHP_EOL;
427 $output .= "\tunset( \$plugins['wpemoji'] );" . PHP_EOL;
428 $output .= "\treturn \$plugins;" . PHP_EOL;
429 $output .= "}" . PHP_EOL;
430
431 return $output;
432 }
433
434 /**
435 * Returns an example of the content of a text snippet.
436 *
437 * @return string
438 */
439 protected static function get_simple_text_snippet() {
440 $output = '<h3>What is Lorem Ipsum?</h3>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.';
441 $output .= PHP_EOL . "{{SNIPPET_CONTENT}}" . PHP_EOL;
442
443 return $output;
444 }
445
446 /**
447 * Returns an example of the content of a universal snippet.
448 *
449 * @return string
450 */
451 protected static function get_simple_universal_snippet() {
452 $output = "<!-- Global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
453 $output .= "<script async src=\"https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID\"></script>" . PHP_EOL;
454 $output .= "<script>" . PHP_EOL;
455 $output .= "\twindow.dataLayer = window.dataLayer || [];" . PHP_EOL;
456 $output .= "\tfunction gtag(){dataLayer.push(arguments);}" . PHP_EOL;
457 $output .= "\tgtag('js', new Date());" . PHP_EOL;
458 $output .= "\tgtag('config', 'GA_TRACKING_ID');" . PHP_EOL;
459 $output .= "</script>" . PHP_EOL;
460 $output .= "<!-- End global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
461
462 return $output;
463 }
464
465 }