PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.1
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.1
2.7.7 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 All 28 releases
← All changes | includes/class.helpers.php +730 -677 2.2.42.7.1 View file →
@@ -1,677 +1,730 @@
1 -<?php
2 -/**
3 - * Helpers tools
4 - *
5 - * @author Webcraftic <wordpress.webraftic@gmail.com>
6 - * @copyright (c) 09.11.2017, Webcraftic
7 - * @version 1.0
8 - */
9 -
10 -// Exit if accessed directly
11 -if ( ! defined( 'ABSPATH' ) ) {
12 - exit;
13 -}
14 -
15 -class WINP_Helper {
16 -
17 - private static $meta_options = [];
18 -
19 - /**
20 - * @return bool
21 - */
22 - public static function is_safe_mode() {
23 - global $wbcr_inp_safe_mode;
24 -
25 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
26 - return false;
27 - }
28 -
29 - if ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) {
30 - return true;
31 - }
32 -
33 - return false;
34 - }
35 -
36 - /**
37 - * Enables safe mode, in which the php code will not be executed.
38 - */
39 - public static function enable_safe_mode() {
40 - global $wbcr_inp_safe_mode;
41 -
42 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
43 - return false;
44 - }
45 -
46 - if ( ( ! $wbcr_inp_safe_mode || ! isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
47 - $wbcr_inp_safe_mode = true;
48 - setcookie( "wbcr-php-snippets-safe-mode", 1, time() + 3600, '/' );
49 -
50 - return true;
51 - }
52 -
53 - return false;
54 - }
55 -
56 - /**
57 - * Disable safe mode, in which the php code will not be executed.
58 - */
59 - public static function disable_safe_mode() {
60 - global $wbcr_inp_safe_mode;
61 -
62 - if ( ! WINP_Plugin::app()->currentUserCan() ) {
63 - return false;
64 - }
65 -
66 - if ( ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
67 - $wbcr_inp_safe_mode = false;
68 - unset( $_COOKIE['wbcr-php-snippets-safe-mode'] );
69 - setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/' );
70 -
71 - return true;
72 - }
73 -
74 - return false;
75 - }
76 -
77 - /**
78 - * Should show a page about the plugin or not.
79 - *
80 - * @return bool
81 - */
82 - public static function is_need_show_about_page() {
83 - $need_show_about = (int) get_option( WINP_Plugin::app()->getOptionName( 'what_new_210' ) );
84 -
85 - $is_ajax = WINP_Helper::doing_ajax();
86 - $is_cron = WINP_Helper::doing_cron();
87 - $is_rest = WINP_Helper::doing_rest_api();
88 -
89 - if ( $need_show_about && ! $is_ajax && ! $is_cron && ! $is_rest ) {
90 - return true;
91 - }
92 -
93 - return false;
94 - }
95 -
96 - /**
97 - * Gets and verified available attributes for snippets shortcodes.
98 - *
99 - * @param bool $tinymce
100 - *
101 - * @return mixed|void
102 - */
103 - public static function get_shortcode_data( $tinymce = false ) {
104 -
105 - $snippets = get_posts( [
106 - 'post_type' => WINP_SNIPPETS_POST_TYPE,
107 - 'meta_query' => [
108 - 'relation' => 'AND',
109 - [
110 - 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope',
111 - 'value' => 'shortcode'
112 - ],
113 - [
114 - 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_activate',
115 - 'value' => 1
116 - ]
117 - ],
118 - 'post_status' => 'publish',
119 - 'numberposts' => - 1
120 - ] );
121 -
122 - $result = [];
123 -
124 - if ( ! empty( $snippets ) ) {
125 - foreach ( (array) $snippets as $snippet ) {
126 - $tag_names = [ 'id' ];
127 - $snippet_type = WINP_Helper::get_snippet_type( $snippet->ID );
128 -
129 - $available_tags = WINP_Helper::getMetaOption( $snippet->ID, 'snippet_tags' );
130 - $available_tags = trim( rtrim( $available_tags ) );
131 -
132 - if ( ! empty( $available_tags ) ) {
133 - $available_tags = array_map( 'trim', explode( ',', $available_tags ) );
134 - $available_tags = array_unique( $available_tags );
135 - } else {
136 - if ( $snippet_type !== 'text' ) {
137 - $available_tags = [ 'id', 'title' ];
138 - } else {
139 - $available_tags = [ 'id' ];
140 - }
141 - }
142 -
143 - $tags = [
144 - 'id' => $snippet->ID,
145 - 'type' => $snippet_type,
146 - 'name' => $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? 'wbcr_snippet' : 'wbcr_' . $snippet_type . '_snippet',
147 - 'title' => empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title
148 - ];
149 -
150 - if ( ! empty( $available_tags ) ) {
151 - foreach ( (array) $available_tags as $tag ) {
152 - if ( '' != $tag ) {
153 - if ( 'title' == $tag ) {
154 - $tags['title'] = empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title;
155 - $tag_names[] = 'title';
156 - } else if ( 'id' != $tag ) {
157 - $tag = preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '', $tag );
158 - $tags[ $tag ] = "";
159 - $tag_names[] = $tag;
160 - }
161 - }
162 - }
163 - }
164 -
165 - $tags['snippet_tags'] = $tag_names;
166 -
167 - $result[] = $tags;
168 - }
169 - }
170 -
171 - return apply_filters( 'wbcr/inp/helper/get_shortcode_data', $result, $tinymce );
172 - }
173 -
174 - /**
175 - * Get snippet type
176 - *
177 - * @param null $post_id
178 - *
179 - * @return array|mixed|string
180 - */
181 - public static function get_snippet_type( $post_id = null ) {
182 - global $post;
183 -
184 - $_post = $post;
185 -
186 - $snippet_type = WINP_Plugin::app()->request->get( 'winp_item', WINP_SNIPPET_TYPE_PHP, true );
187 - $get_post = WINP_Plugin::app()->request->get( 'post', '' );
188 -
189 - if ( empty( $post_id ) && ! empty( $get_post ) && ! is_array( $get_post ) ) {
190 - $post_id = esc_attr( $get_post );
191 - }
192 -
193 - if ( ! empty( $post_id ) ) {
194 - $_post = get_post( $post_id );
195 - }
196 -
197 - if ( ! empty( $_post ) && WINP_SNIPPETS_POST_TYPE === $_post->post_type ) {
198 - $_snippet_type = get_post_meta( $_post->ID, WINP_Plugin::app()->getPrefix() . 'snippet_type', true );
199 - $snippet_type = $_snippet_type ? $_snippet_type : $snippet_type;
200 - }
201 -
202 - return $snippet_type;
203 - }
204 -
205 - /**
206 - * Checks if the current request is a WP REST API request.
207 - *
208 - * Case #1: After WP_REST_Request initialisation
209 - * Case #2: Support "plain" permalink settings
210 - * Case #3: URL Path begins with wp-json/ (your REST prefix)
211 - * Also supports WP installations in subfolders
212 - *
213 - * @author matzeeable https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist
214 - * @since 2.1.0
215 - * @return boolean
216 - */
217 - public static function doing_rest_api() {
218 - $prefix = rest_get_url_prefix();
219 - $rest_route = WINP_Plugin::app()->request->get( 'rest_route', null );
220 - if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
221 - || ! is_null( $rest_route ) // (#2)
222 - && strpos( trim( $rest_route, '\\/' ), $prefix, 0 ) === 0 ) {
223 - return true;
224 - }
225 -
226 - // (#3)
227 - $rest_url = wp_parse_url( site_url( $prefix ) );
228 - $current_url = wp_parse_url( add_query_arg( [] ) );
229 -
230 - return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
231 - }
232 -
233 - /**
234 - * @since 2.1.0
235 - * @return bool
236 - */
237 - public static function doing_ajax() {
238 - if ( function_exists( 'wp_doing_ajax' ) ) {
239 - return wp_doing_ajax();
240 - }
241 -
242 - return defined( 'DOING_AJAX' ) && DOING_AJAX;
243 - }
244 -
245 - /**
246 - * @since 2.1.0
247 - * @return bool
248 - */
249 - public static function doing_cron() {
250 - if ( function_exists( 'wp_doing_cron' ) ) {
251 - return wp_doing_cron();
252 - }
253 -
254 - return defined( 'DOING_CRON' ) && DOING_CRON;
255 - }
256 -
257 - /**
258 - * In the new version of the plugin, we moved the code of the snippets
259 - * from the meta data to the post table, to the post_content cell.
260 - *
261 - * If the migration was an error, we need to reliably get the
262 - * snippet code if the post_content cell is empty.
263 - *
264 - * @author Alexander Kovalev <alex.kovalevv@gmail.com>
265 - * @since 2.2.1
266 - *
267 - * @param WP_Post $post
268 - *
269 - * @return string snippet code
270 - */
271 - public static function get_snippet_code( $post ) {
272 - if ( empty( $post->post_content ) ) {
273 - return WINP_Helper::getMetaOption( $post->ID, 'snippet_code' );
274 - }
275 -
276 - return $post->post_content;
277 - }
278 -
279 - /**
280 - * Get meta option
281 - *
282 - * @param int $post_id
283 - * @param string $option_name
284 - * @param mixed $default
285 - *
286 - * @return mixed|array
287 - */
288 - public static function getMetaOption( $post_id, $option_name, $default = null ) {
289 - $post_id = (int) $post_id;
290 -
291 - if ( ! isset( self::$meta_options[ $post_id ] ) || empty( self::$meta_options[ $post_id ] ) ) {
292 - $meta_vals = get_post_meta( $post_id, '', true );
293 -
294 - if ( ! empty( $meta_vals ) && is_array( $meta_vals ) ) {
295 - foreach ( $meta_vals as $name => $val ) {
296 - self::$meta_options[ $post_id ][ $name ] = $val[0];
297 - }
298 - }
299 - }
300 -
301 - 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;
302 - }
303 -
304 - /**
305 - * Udpdate meta option
306 - *
307 - * @param int $post_id
308 - * @param string $option_name
309 - * @param mixed $option_value
310 - *
311 - * @return bool|int
312 - */
313 - public static function updateMetaOption( $post_id, $option_name, $option_value ) {
314 - $post_id = (int) $post_id;
315 -
316 - return update_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name, $option_value );
317 - }
318 -
319 - /**
320 - * Remove meta option
321 - *
322 - * @param int $post_id
323 - * @param string $option_name
324 - *
325 - * @return bool|int
326 - */
327 - public static function removeMetaOption( $post_id, $option_name ) {
328 - $post_id = (int) $post_id;
329 -
330 - return delete_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name );
331 - }
332 -
333 - /**
334 - * Check capabilities for snippets post type.
335 - *
336 - * @author Alexander Kovalev <alex.kovalevv@gmail.com>
337 - * @since 2.2.0
338 - */
339 - public static function has_post_capabilities() {
340 - $role = get_role( 'administrator' );
341 -
342 - if ( ! $role ) {
343 - return false;
344 - }
345 -
346 - return $role->has_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE );
347 - }
348 -
349 - /**
350 - * Set capabilities for snippets post type.
351 - *
352 - * @author Alexander Kovalev <alex.kovalevv@gmail.com>
353 - * @since 2.2.0
354 - */
355 - public static function set_post_capabilities() {
356 - $role = get_role( 'administrator' );
357 -
358 - if ( ! $role ) {
359 - return false;
360 - }
361 -
362 - $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE );
363 - $role->add_cap( 'read_' . WINP_SNIPPETS_POST_TYPE );
364 - $role->add_cap( 'delete_' . WINP_SNIPPETS_POST_TYPE );
365 - $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE . 's' );
366 - $role->add_cap( 'edit_others_' . WINP_SNIPPETS_POST_TYPE . 's' );
367 - $role->add_cap( 'publish_' . WINP_SNIPPETS_POST_TYPE . 's' );
368 - $role->add_cap( 'read_private_' . WINP_SNIPPETS_POST_TYPE . 's' );
369 -
370 - return true;
371 - }
372 -
373 - /**
374 - * Create a demo snippets with examples of use
375 - */
376 - public static function create_demo_snippets() {
377 -
378 - update_option( WINP_Plugin::app()->getOptionName( 'activate_by_default' ), 1 );
379 - update_option( WINP_Plugin::app()->getOptionName( 'complete_uninstall' ), 0 );
380 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_theme' ), 'default' );
381 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_indent_with_tabs' ), 1 );
382 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_tab_size' ), 4 );
383 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_indent_unit' ), 4 );
384 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_wrap_lines' ), 1 );
385 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_line_numbers' ), 1 );
386 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_auto_close_brackets' ), 1 );
387 - update_option( WINP_Plugin::app()->getOptionName( 'code_editor_highlight_selection_matches' ), 0 );
388 -
389 - $posts = [
390 - [
391 - 'post_title' => __( 'Simple php snippet: Disable emojis', 'insert-php' ),
392 - 'post_name' => 'simple-php-snippet',
393 - 'post_content' => self::get_simple_php_snippet(),
394 - 'meta' => [
395 - 'type' => WINP_SNIPPET_TYPE_PHP,
396 - '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' ),
397 - 'tags' => [ 'php', 'disable features' ]
398 - ]
399 - ],
400 - [
401 - 'post_title' => __( 'Simple text snippet: What is Lorem Ipsum?', 'insert-php' ),
402 - 'post_name' => 'simple-text-snippet',
403 - 'post_content' => self::get_simple_text_snippet(),
404 - 'meta' => [
405 - 'type' => WINP_SNIPPET_TYPE_TEXT,
406 - 'description' => __( 'This ordinary maintenance text. With this snippet, you can fill your pages with meaningless English text.', 'insert-php' ),
407 - '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";}}',
408 - 'tags' => [ 'text', 'lorem ipsum' ]
409 - ]
410 - ],
411 - [
412 - 'post_title' => __( 'Simple universal snippet: Google analytics tracking', 'insert-php' ),
413 - 'post_name' => 'simple-universal-snippet',
414 - 'post_content' => self::get_simple_universal_snippet(),
415 - 'meta' => [
416 - 'type' => WINP_SNIPPET_TYPE_UNIVERSAL,
417 - '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.' ),
418 - '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";}}',
419 - 'tags' => [ 'universal', 'tracking' ]
420 - ]
421 - ]
422 - ];
423 -
424 - foreach ( $posts as $post ) {
425 - // '@' here is to hide unexpected output while plugin activation
426 - $post_id = @wp_insert_post( [
427 - 'post_content' => $post['post_content'],
428 - 'post_title' => $post['post_title'],
429 - 'post_status' => 'publish',
430 - 'post_type' => WINP_SNIPPETS_POST_TYPE
431 - ] );
432 -
433 - if ( ! is_wp_error( $post_id ) ) {
434 - if ( isset( $post['meta']['type'] ) ) {
435 - WINP_Helper::updateMetaOption( $post_id, 'snippet_type', $post['meta']['type'] );
436 -
437 - if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_PHP ) {
438 - WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'evrywhere' );
439 - }
440 - if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_TEXT ) {
441 - WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'shortcode' );
442 - }
443 - if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_UNIVERSAL ) {
444 - WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'auto' );
445 - WINP_Helper::updateMetaOption( $post_id, 'snippet_location', 'header' );
446 - }
447 - }
448 -
449 - if ( isset( $post['meta']['description'] ) ) {
450 - WINP_Helper::updateMetaOption( $post_id, 'snippet_description', $post['meta']['description'] );
451 - }
452 -
453 - if ( isset( $post['meta']['filters'] ) && is_serialized( $post['meta']['filters'] ) ) {
454 - $unserialized_filters = unserialize( $post['meta']['filters'] );
455 - WINP_Helper::updateMetaOption( $post_id, 'snippet_filters', $unserialized_filters );
456 - WINP_Helper::updateMetaOption( $post_id, 'changed_filters', 1 );
457 - }
458 -
459 - if ( isset( $post['meta']['tags'] ) && ! empty( $post['meta']['tags'] ) ) {
460 - if ( ! taxonomy_exists( WINP_SNIPPETS_TAXONOMY ) ) {
461 - register_taxonomy( WINP_SNIPPETS_TAXONOMY, WINP_SNIPPETS_POST_TYPE, [] );
462 - }
463 -
464 - wp_set_post_terms( $post_id, $post['meta']['tags'], WINP_SNIPPETS_TAXONOMY, true );
465 - }
466 - }
467 - }
468 -
469 - update_option( WINP_Plugin::app()->getOptionName( 'demo_snippets_created' ), 1 );
470 - }
471 -
472 - /**
473 - * Returns an example php snippet content
474 - *
475 - * @return string
476 - */
477 - protected static function get_simple_php_snippet() {
478 - $output = "/**" . PHP_EOL;
479 - $output .= "* Disable WP 4.2 emoji" . PHP_EOL;
480 - $output .= "*/" . PHP_EOL;
481 - $output .= "function ace_remove_emoji() {" . PHP_EOL;
482 - $output .= "\tadd_filter( 'emoji_svg_url', '__return_false' );" . PHP_EOL;
483 - $output .= "\tremove_action( 'admin_print_styles', 'print_emoji_styles' );" . PHP_EOL;
484 - $output .= "\tremove_action( 'wp_head', 'print_emoji_detection_script', 7 );" . PHP_EOL;
485 - $output .= "\tremove_action( 'admin_print_scripts', 'print_emoji_detection_script' );" . PHP_EOL;
486 - $output .= "\tremove_action( 'wp_print_styles', 'print_emoji_styles' );" . PHP_EOL;
487 - $output .= "\tremove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );" . PHP_EOL;
488 - $output .= "\tremove_filter( 'the_content_feed', 'wp_staticize_emoji' );" . PHP_EOL;
489 - $output .= "\tremove_filter( 'comment_text_rss', 'wp_staticize_emoji' );" . PHP_EOL;
490 - $output .= "\t// filter to remove TinyMCE emojis" . PHP_EOL;
491 - $output .= "\tadd_filter( 'tiny_mce_plugins', 'ace_disable_emoji_tinymce' );" . PHP_EOL;
492 - $output .= "}" . PHP_EOL;
493 - $output .= "add_action( 'init', 'ace_remove_emoji' );" . PHP_EOL;
494 - $output .= "/**" . PHP_EOL;
495 - $output .= "* Remove tinyMCE emoji" . PHP_EOL;
496 - $output .= "*/" . PHP_EOL;
497 - $output .= "function ace_disable_emoji_tinymce( \$plugins ) {" . PHP_EOL;
498 - $output .= "\tunset( \$plugins['wpemoji'] );" . PHP_EOL;
499 - $output .= "\treturn \$plugins;" . PHP_EOL;
500 - $output .= "}" . PHP_EOL;
501 -
502 - return $output;
503 - }
504 -
505 - /**
506 - * Returns an example of the content of a text snippet.
507 - *
508 - * @return string
509 - */
510 - protected static function get_simple_text_snippet() {
511 - $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.';
512 - $output .= PHP_EOL . "{{SNIPPET_CONTENT}}" . PHP_EOL;
513 -
514 - return $output;
515 - }
516 -
517 - /**
518 - * Returns an example of the content of a universal snippet.
519 - *
520 - * @return string
521 - */
522 - protected static function get_simple_universal_snippet() {
523 - $output = "<!-- Global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
524 - $output .= "<script async src=\"https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID\"></script>" . PHP_EOL;
525 - $output .= "<script>" . PHP_EOL;
526 - $output .= "\twindow.dataLayer = window.dataLayer || [];" . PHP_EOL;
527 - $output .= "\tfunction gtag(){dataLayer.push(arguments);}" . PHP_EOL;
528 - $output .= "\tgtag('js', new Date());" . PHP_EOL;
529 - $output .= "\tgtag('config', 'GA_TRACKING_ID');" . PHP_EOL;
530 - $output .= "</script>" . PHP_EOL;
531 - $output .= "<!-- End global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
532 -
533 - return $output;
534 - }
535 -
536 - /**
537 - * Wrapper for register shortcode
538 - *
539 - * @param $name
540 - * @param $obj
541 - */
542 - public static function register_shortcode( $name, $obj ) {
543 - Wbcr_FactoryShortcodes325::register( $name, $obj );
544 - }
545 -
546 - /**
547 - * Wrapper for get factory class for container
548 - *
549 - * @return string
550 - */
551 - public static function get_factory_class() {
552 - return "factory-bootstrap-413 factory-fontawesome-000";
553 - }
554 -
555 - /**
556 - * Wrapper for Wbcr_FactoryForms413_OptionsValueProvider object
557 - *
558 - * @param $plugin
559 - *
560 - * @return Wbcr_FactoryForms413_OptionsValueProvider
561 - */
562 - public static function get_options_value_provider( $plugin ) {
563 - return new Wbcr_FactoryForms413_OptionsValueProvider( $plugin );
564 - }
565 -
566 - /**
567 - * Wrapper for get factory form object
568 - *
569 - * @param $options
570 - * @param $plugin
571 - *
572 - * @return Wbcr_FactoryForms413_Form
573 - */
574 - public static function get_factory_form( $options, $plugin ) {
575 - return new Wbcr_FactoryForms413_Form( $options, $plugin );
576 - }
577 -
578 - /**
579 - * Wrapper for register factory metaboxes
580 - *
581 - * @param $class_name_or_object
582 - * @param $post_type
583 - * @param $plugin
584 - */
585 - public static function register_factory_metaboxes( $class_name_or_object, $post_type, $plugin ) {
586 - Wbcr_FactoryMetaboxes405::registerFor( $class_name_or_object, $post_type, $plugin );
587 - }
588 -
589 - /**
590 - * Render html for purchase button
591 - */
592 - public static function get_purchase_button() {
593 - ?>
594 - <p class="winp-purchase-button">
595 - <?php $price_url = WINP_Plugin::app()->get_support()->get_pricing_url( true, 'library_page' ); ?>
596 - <?php $price_url = empty( $price_url ) ? 'http://woody-ad-snippets.webcraftic.com/?utm_content=library_page&utm_source=wordpress.org' : $price_url; ?>
597 - <a class="button" id="winp-library-buy-button" href="<?php echo esc_url( $price_url ) ?>" target="_blank">
598 - <?php $price = WINP_Plugin::app()->premium->get_price(); ?>
599 - <?php $price = empty( $price ) ? 19 : $price; ?>
600 - <span><?php echo __( 'Purchase premium for', 'insert-php' ) . ' $' . esc_attr( $price ); ?></span>
601 - </a>
602 - </p>
603 - <?php
604 - }
605 -
606 - /**
607 - * Check if current user is admin or editor
608 - *
609 - * @return bool
610 - */
611 - public static function winp_check_user_admin() {
612 - return current_user_can( 'manage_options' ) || current_user_can( 'administrator' );
613 - }
614 -
615 - /*
616 - * Flushes as many page cache plugin's caches as possible.
617 - *
618 - * @return void
619 - */
620 - public static function flush_page_cache() {
621 - if ( function_exists( 'wp_cache_clear_cache' ) ) {
622 - if ( is_multisite() ) {
623 - $blog_id = get_current_blog_id();
624 - wp_cache_clear_cache( $blog_id );
625 - } else {
626 - wp_cache_clear_cache();
627 - }
628 - } else if ( has_action( 'cachify_flush_cache' ) ) {
629 - do_action( 'cachify_flush_cache' );
630 - } else if ( function_exists( 'w3tc_pgcache_flush' ) ) {
631 - w3tc_pgcache_flush();
632 - } else if ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) {
633 - wp_fast_cache_bulk_delete_all();
634 - } else if ( class_exists( 'WpFastestCache' ) ) {
635 - $wpfc = new WpFastestCache();
636 - $wpfc->deleteCache();
637 - } else if ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) {
638 - c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
639 - } else if ( class_exists( 'zencache' ) ) {
640 - zencache::clear();
641 - } else if ( class_exists( 'comet_cache' ) ) {
642 - comet_cache::clear();
643 - } else if ( class_exists( 'WpeCommon' ) ) {
644 - // WPEngine cache purge/flush methods to call by default
645 - $wpe_methods = [
646 - 'purge_varnish_cache',
647 - ];
648 -
649 - // More agressive clear/flush/purge behind a filter
650 - if ( apply_filters( 'wbcr/factory/flush_wpengine_aggressive', false ) ) {
651 - $wpe_methods = array_merge( $wpe_methods, [ 'purge_memcached', 'clear_maxcdn_cache' ] );
652 - }
653 -
654 - // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing)
655 - $wpe_methods = apply_filters( 'wbcr/factory/wpengine_methods', $wpe_methods );
656 -
657 - foreach ( $wpe_methods as $wpe_method ) {
658 - if ( method_exists( 'WpeCommon', $wpe_method ) ) {
659 - WpeCommon::$wpe_method();
660 - }
661 - }
662 - } else if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
663 - sg_cachepress_purge_cache();
664 - } else if ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) {
665 - // fallback for WP-Super-Cache
666 - global $cache_path;
667 - if ( is_multisite() ) {
668 - $blog_id = get_current_blog_id();
669 - prune_super_cache( get_supercache_dir( $blog_id ), true );
670 - prune_super_cache( $cache_path . 'blogs/', true );
671 - } else {
672 - prune_super_cache( $cache_path . 'supercache/', true );
673 - prune_super_cache( $cache_path, true );
674 - }
675 - }
676 - }
677 -}
1 +<?php
2 +/**
3 + * Helpers tools
4 + *
5 + * @package Woody_Code_Snippets
6 + */
7 +
8 +// Exit if accessed directly
9 +if ( ! defined( 'ABSPATH' ) ) {
10 + exit;
11 +}
12 +
13 +class WINP_Helper {
14 +
15 + private static $meta_options = [];
16 +
17 + /**
18 + * @return bool
19 + */
20 + public static function is_safe_mode() {
21 + global $wbcr_inp_safe_mode;
22 +
23 + if ( ! current_user_can( 'manage_options' ) ) {
24 + return false;
25 + }
26 +
27 + if ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) {
28 + return true;
29 + }
30 +
31 + return false;
32 + }
33 +
34 + /**
35 + * Enables safe mode, in which the php code will not be executed.
36 + */
37 + public static function enable_safe_mode() {
38 + global $wbcr_inp_safe_mode;
39 +
40 + if ( ! current_user_can( 'manage_options' ) ) {
41 + return false;
42 + }
43 +
44 + if ( ( ! $wbcr_inp_safe_mode || ! isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
45 + $wbcr_inp_safe_mode = true;
46 + setcookie( 'wbcr-php-snippets-safe-mode', 1, time() + 3600, '/' );
47 +
48 + return true;
49 + }
50 +
51 + return false;
52 + }
53 +
54 + /**
55 + * Disable safe mode, in which the php code will not be executed.
56 + */
57 + public static function disable_safe_mode() {
58 + global $wbcr_inp_safe_mode;
59 +
60 + if ( ! current_user_can( 'manage_options' ) ) {
61 + return false;
62 + }
63 +
64 + if ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) {
65 + $wbcr_inp_safe_mode = false;
66 +
67 + unset( $_COOKIE['wbcr-php-snippets-safe-mode'] );
68 +
69 + setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/' );
70 + setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/wp-admin' );
71 +
72 + return true;
73 + }
74 +
75 + return false;
76 + }
77 +
78 + /**
79 + * Gets and verified available attributes for snippets shortcodes.
80 + *
81 + * @param bool $tinymce
82 + *
83 + * @return mixed|void
84 + */
85 + public static function get_shortcode_data( $tinymce = false ) {
86 +
87 + $snippets = get_posts(
88 + [
89 + 'post_type' => WINP_SNIPPETS_POST_TYPE,
90 + 'meta_query' => [
91 + 'relation' => 'AND',
92 + [
93 + 'key' => 'wbcr_inp_snippet_scope',
94 + 'value' => 'shortcode',
95 + ],
96 + [
97 + 'key' => 'wbcr_inp_snippet_activate',
98 + 'value' => 1,
99 + ],
100 + ],
101 + 'post_status' => 'publish',
102 + 'numberposts' => - 1,
103 + ]
104 + );
105 +
106 + $result = [];
107 +
108 + if ( ! empty( $snippets ) ) {
109 + foreach ( (array) $snippets as $snippet ) {
110 + $tag_names = [ 'id' ];
111 + $snippet_type = self::get_snippet_type( $snippet->ID );
112 +
113 + $available_tags = self::getMetaOption( $snippet->ID, 'snippet_tags' );
114 + $available_tags = trim( rtrim( $available_tags ) );
115 +
116 + if ( ! empty( $available_tags ) ) {
117 + $available_tags = array_map( 'trim', explode( ',', $available_tags ) );
118 + $available_tags = array_unique( $available_tags );
119 + } elseif ( $snippet_type !== 'text' && $snippet_type !== 'advert' ) {
120 + $available_tags = [ 'id', 'title' ];
121 + } else {
122 + $available_tags = [ 'id' ];
123 + }
124 +
125 + $tags = [
126 + 'id' => $snippet->ID,
127 + 'type' => $snippet_type,
128 + 'name' => $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? 'wbcr_snippet' : 'wbcr_' . $snippet_type . '_snippet',
129 + 'title' => empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title,
130 + ];
131 +
132 + if ( ! empty( $available_tags ) ) {
133 + foreach ( (array) $available_tags as $tag ) {
134 + if ( '' != $tag ) {
135 + if ( 'title' == $tag ) {
136 + $tags['title'] = empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title;
137 + $tag_names[] = 'title';
138 + } elseif ( 'id' != $tag ) {
139 + $tag = preg_replace( '/[^a-zA-Z0-9_\x7f-\xff]/', '', $tag );
140 + $tags[ $tag ] = '';
141 + $tag_names[] = $tag;
142 + }
143 + }
144 + }
145 + }
146 +
147 + $tags['snippet_tags'] = $tag_names;
148 +
149 + $result[] = $tags;
150 + }
151 + }
152 +
153 + return apply_filters( 'wbcr/inp/helper/get_shortcode_data', $result, $tinymce );
154 + }
155 +
156 + /**
157 + * Get snippet type
158 + *
159 + * @param mixed $post_id Post ID.
160 + *
161 + * @return array|mixed|string
162 + */
163 + public static function get_snippet_type( $post_id = null ) {
164 + global $post;
165 +
166 + $_post = $post;
167 +
168 + $snippet_type = WINP_HTTP::get( 'winp_item', WINP_SNIPPET_TYPE_PHP, 'sanitize_key' );
169 + $get_post = WINP_HTTP::get( 'post', '' );
170 +
171 + if ( empty( $post_id ) && ! empty( $get_post ) && ! is_array( $get_post ) ) {
172 + $post_id = esc_attr( $get_post );
173 + }
174 +
175 + if ( ! empty( $post_id ) ) {
176 + $_post = get_post( $post_id );
177 + }
178 +
179 + if ( ! empty( $_post ) && WINP_SNIPPETS_POST_TYPE === $_post->post_type ) {
180 + $_snippet_type = get_post_meta( $_post->ID, 'wbcr_inp_snippet_type', true );
181 + $snippet_type = $_snippet_type ? $_snippet_type : $snippet_type;
182 + }
183 +
184 + return $snippet_type;
185 + }
186 +
187 + /**
188 + * Checks if the current request is a WP REST API request.
189 + *
190 + * Case #1: After WP_REST_Request initialisation
191 + * Case #2: Support "plain" permalink settings
192 + * Case #3: URL Path begins with wp-json/ (your REST prefix)
193 + * Also supports WP installations in subfolders
194 + *
195 + * @author matzeeable https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist
196 + * @since 2.1.0
197 + * @return boolean
198 + */
199 + public static function doing_rest_api() {
200 + $prefix = rest_get_url_prefix();
201 + $rest_route = WINP_HTTP::get( 'rest_route', null );
202 + if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
203 + || ! is_null( $rest_route ) // (#2)
204 + && strpos( trim( $rest_route, '\\/' ), $prefix, 0 ) === 0 ) {
205 + return true;
206 + }
207 +
208 + // (#3)
209 + $rest_url = wp_parse_url( site_url( $prefix ) );
210 + $current_url = wp_parse_url( esc_url( add_query_arg( [] ) ) );
211 +
212 + return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
213 + }
214 +
215 + /**
216 + * @return bool
217 + * @since 2.1.0
218 + */
219 + public static function doing_ajax() {
220 + if ( function_exists( 'wp_doing_ajax' ) ) {
221 + return wp_doing_ajax();
222 + }
223 +
224 + return defined( 'DOING_AJAX' ) && DOING_AJAX;
225 + }
226 +
227 + /**
228 + * @return bool
229 + * @since 2.1.0
230 + */
231 + public static function doing_cron() {
232 + if ( function_exists( 'wp_doing_cron' ) ) {
233 + return wp_doing_cron();
234 + }
235 +
236 + return defined( 'DOING_CRON' ) && DOING_CRON;
237 + }
238 +
239 + /**
240 + * In the new version of the plugin, we moved the code of the snippets
241 + * from the meta data to the post table, to the post_content cell.
242 + *
243 + * If the migration was an error, we need to reliably get the
244 + * snippet code if the post_content cell is empty.
245 + *
246 + * @param WP_Post $post
247 + *
248 + * @return string snippet code
249 + * @since 2.2.1
250 + */
251 + public static function get_snippet_code( $post ) {
252 + if ( empty( $post->post_content ) ) {
253 + return self::getMetaOption( $post->ID, 'snippet_code' );
254 + }
255 +
256 + return $post->post_content;
257 + }
258 +
259 + /**
260 + * Get meta option
261 + *
262 + * @param int $post_id
263 + * @param string $option_name
264 + * @param mixed $default
265 + *
266 + * @return mixed|array
267 + */
268 + public static function getMetaOption( $post_id, $option_name, $default = null ) {
269 + $post_id = (int) $post_id;
270 +
271 + if ( ! isset( self::$meta_options[ $post_id ] ) || empty( self::$meta_options[ $post_id ] ) ) {
272 + $meta_vals = get_post_meta( $post_id, '', true );
273 +
274 + if ( ! empty( $meta_vals ) && is_array( $meta_vals ) ) {
275 + foreach ( $meta_vals as $name => $val ) {
276 + self::$meta_options[ $post_id ][ $name ] = $val[0];
277 + }
278 + }
279 + }
280 +
281 + return isset( self::$meta_options[ $post_id ][ 'wbcr_inp_' . $option_name ] ) ? self::$meta_options[ $post_id ][ 'wbcr_inp_' . $option_name ] : $default;
282 + }
283 +
284 + /**
285 + * Udpdate meta option
286 + *
287 + * @param int $post_id
288 + * @param string $option_name
289 + * @param mixed $option_value
290 + *
291 + * @return bool|int
292 + */
293 + public static function updateMetaOption( $post_id, $option_name, $option_value ) {
294 + $post_id = (int) $post_id;
295 +
296 + return update_post_meta( $post_id, 'wbcr_inp_' . $option_name, $option_value );
297 + }
298 +
299 + /**
300 + * Check capabilities for snippets post type.
301 + *
302 + * @since 2.2.0
303 + */
304 + public static function has_post_capabilities() {
305 + $role = get_role( 'administrator' );
306 +
307 + if ( ! $role ) {
308 + return false;
309 + }
310 +
311 + return $role->has_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE );
312 + }
313 +
314 + /**
315 + * Set capabilities for snippets post type.
316 + *
317 + * @since 2.2.0
318 + */
319 + public static function set_post_capabilities() {
320 + $role = get_role( 'administrator' );
321 +
322 + if ( ! $role ) {
323 + return false;
324 + }
325 +
326 + $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE );
327 + $role->add_cap( 'read_' . WINP_SNIPPETS_POST_TYPE );
328 + $role->add_cap( 'delete_' . WINP_SNIPPETS_POST_TYPE );
329 + $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE . 's' );
330 + $role->add_cap( 'edit_others_' . WINP_SNIPPETS_POST_TYPE . 's' );
331 + $role->add_cap( 'publish_' . WINP_SNIPPETS_POST_TYPE . 's' );
332 + $role->add_cap( 'read_private_' . WINP_SNIPPETS_POST_TYPE . 's' );
333 +
334 + return true;
335 + }
336 +
337 + /**
338 + * Create a demo snippets with examples of use
339 + */
340 + public static function create_demo_snippets() {
341 +
342 + update_option( 'wbcr_inp_activate_by_default', 1 );
343 + update_option( 'wbcr_inp_complete_uninstall', 0 );
344 + update_option( 'wbcr_inp_code_editor_theme', 'default' );
345 + update_option( 'wbcr_inp_code_editor_indent_with_tabs', 1 );
346 + update_option( 'wbcr_inp_code_editor_tab_size', 4 );
347 + update_option( 'wbcr_inp_code_editor_indent_unit', 4 );
348 + update_option( 'wbcr_inp_code_editor_wrap_lines', 1 );
349 + update_option( 'wbcr_inp_code_editor_line_numbers', 1 );
350 + update_option( 'wbcr_inp_code_editor_auto_close_brackets', 1 );
351 + update_option( 'wbcr_inp_code_editor_highlight_selection_matches', 0 );
352 +
353 + $posts = [
354 + [
355 + 'post_title' => __( 'Simple universal snippet: Google analytics tracking', 'insert-php' ),
356 + 'post_name' => 'simple-universal-snippet',
357 + 'post_content' => self::get_simple_universal_snippet(),
358 + 'meta' => [
359 + 'type' => WINP_SNIPPET_TYPE_UNIVERSAL,
360 + '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.', 'insert-php' ),
361 + '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";}}',
362 + 'tags' => [ 'universal', 'tracking' ],
363 + 'priority' => 10,
364 + ],
365 + ],
366 + [
367 + 'post_title' => __( 'Simple text snippet: What is Lorem Ipsum?', 'insert-php' ),
368 + 'post_name' => 'simple-text-snippet',
369 + 'post_content' => self::get_simple_text_snippet(),
370 + 'meta' => [
371 + 'type' => WINP_SNIPPET_TYPE_TEXT,
372 + 'description' => __( 'This is ordinary maintenance text. With this snippet, you can fill your pages with meaningless English text.', 'insert-php' ),
373 + '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";}}',
374 + 'tags' => [ 'text', 'lorem ipsum' ],
375 + 'priority' => 20,
376 + ],
377 + ],
378 + [
379 + 'post_title' => __( 'Simple php snippet: Disable emojis', 'insert-php' ),
380 + 'post_name' => 'simple-php-snippet',
381 + 'post_content' => self::get_simple_php_snippet(),
382 + 'meta' => [
383 + 'type' => WINP_SNIPPET_TYPE_PHP,
384 + 'description' => __( 'Emojis are little icons used to express ideas or emotions. While these icons are useful, they may not be necessary for your WordPress site. Use this snippet to disable emojis on your site and make it faster.', 'insert-php' ),
385 + 'tags' => [ 'php', 'disable features' ],
386 + 'priority' => 30,
387 + ],
388 + ],
389 + [
390 + 'post_title' => __( 'Add Facebook Pixel to the Order success page', 'insert-php' ),
391 + 'post_name' => 'simple-uni-snippet-for-woocommerce',
392 + 'post_content' => self::get_woo_snippet(),
393 + 'meta' => [
394 + 'type' => WINP_SNIPPET_TYPE_UNIVERSAL,
395 + 'description' => __( 'Add Facebook Pixel to the Order success page.', 'insert-php' ),
396 + '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:16:"woo_checkout_pay";}}}}s:4:"type";s:6:"showif";}}',
397 + 'tags' => [ 'woocommerce' ],
398 + 'priority' => 40,
399 + ],
400 + ],
401 + ];
402 +
403 + foreach ( $posts as $post ) {
404 + // '@' here is to hide unexpected output while plugin activation
405 + $post_id = @wp_insert_post(
406 + [
407 + 'post_content' => $post['post_content'],
408 + 'post_title' => $post['post_title'],
409 + 'post_status' => 'publish',
410 + 'post_type' => WINP_SNIPPETS_POST_TYPE,
411 + ]
412 + );
413 +
414 + if ( ! is_wp_error( $post_id ) ) {
415 + if ( isset( $post['meta']['type'] ) ) {
416 + self::updateMetaOption( $post_id, 'snippet_type', $post['meta']['type'] );
417 +
418 + if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_PHP ) {
419 + self::updateMetaOption( $post_id, 'snippet_scope', 'evrywhere' );
420 + }
421 + if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_TEXT ) {
422 + self::updateMetaOption( $post_id, 'snippet_scope', 'shortcode' );
423 + }
424 + if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_UNIVERSAL ) {
425 + self::updateMetaOption( $post_id, 'snippet_scope', 'auto' );
426 + self::updateMetaOption( $post_id, 'snippet_location', 'header' );
427 + }
428 + }
429 +
430 + if ( isset( $post['meta']['description'] ) ) {
431 + self::updateMetaOption( $post_id, 'snippet_description', $post['meta']['description'] );
432 + }
433 +
434 + if ( isset( $post['meta']['filters'] ) && is_serialized( $post['meta']['filters'] ) ) {
435 + $unserialized_filters = unserialize( $post['meta']['filters'] );
436 + self::updateMetaOption( $post_id, 'snippet_filters', $unserialized_filters );
437 + self::updateMetaOption( $post_id, 'changed_filters', 1 );
438 + }
439 +
440 + if ( isset( $post['meta']['tags'] ) && ! empty( $post['meta']['tags'] ) ) {
441 + if ( ! taxonomy_exists( WINP_SNIPPETS_TAXONOMY ) ) {
442 + register_taxonomy( WINP_SNIPPETS_TAXONOMY, WINP_SNIPPETS_POST_TYPE, [] );
443 + }
444 +
445 + wp_set_post_terms( $post_id, $post['meta']['tags'], WINP_SNIPPETS_TAXONOMY, true );
446 + }
447 +
448 + if ( isset( $post['meta']['priority'] ) ) {
449 + self::updateMetaOption( $post_id, 'snippet_priority', $post['meta']['priority'] );
450 + }
451 + }
452 + }
453 +
454 + update_option( 'wbcr_inp_demo_snippets_created', 1 );
455 + }
456 +
457 + /**
458 + * Returns an example php snippet content
459 + *
460 + * @return string
461 + */
462 + protected static function get_simple_php_snippet() {
463 + $output = '/**' . PHP_EOL;
464 + $output .= '* Disable WP 4.2 emoji' . PHP_EOL;
465 + $output .= '*/' . PHP_EOL;
466 + $output .= 'function ace_remove_emoji() {' . PHP_EOL;
467 + $output .= "\tadd_filter( 'emoji_svg_url', '__return_false' );" . PHP_EOL;
468 + $output .= "\tremove_action( 'admin_print_styles', 'print_emoji_styles' );" . PHP_EOL;
469 + $output .= "\tremove_action( 'wp_head', 'print_emoji_detection_script', 7 );" . PHP_EOL;
470 + $output .= "\tremove_action( 'admin_print_scripts', 'print_emoji_detection_script' );" . PHP_EOL;
471 + $output .= "\tremove_action( 'wp_print_styles', 'print_emoji_styles' );" . PHP_EOL;
472 + $output .= "\tremove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );" . PHP_EOL;
473 + $output .= "\tremove_filter( 'the_content_feed', 'wp_staticize_emoji' );" . PHP_EOL;
474 + $output .= "\tremove_filter( 'comment_text_rss', 'wp_staticize_emoji' );" . PHP_EOL;
475 + $output .= "\t// filter to remove TinyMCE emojis" . PHP_EOL;
476 + $output .= "\tadd_filter( 'tiny_mce_plugins', 'ace_disable_emoji_tinymce' );" . PHP_EOL;
477 + $output .= '}' . PHP_EOL;
478 + $output .= "add_action( 'init', 'ace_remove_emoji' );" . PHP_EOL;
479 + $output .= '/**' . PHP_EOL;
480 + $output .= '* Remove tinyMCE emoji' . PHP_EOL;
481 + $output .= '*/' . PHP_EOL;
482 + $output .= 'function ace_disable_emoji_tinymce( $plugins ) {' . PHP_EOL;
483 + $output .= "\tunset( \$plugins['wpemoji'] );" . PHP_EOL;
484 + $output .= "\treturn \$plugins;" . PHP_EOL;
485 + $output .= '}' . PHP_EOL;
486 +
487 + return $output;
488 + }
489 +
490 + /**
491 + * Returns an example of the content of a text snippet.
492 + *
493 + * @return string
494 + */
495 + protected static function get_simple_text_snippet() {
496 + $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.';
497 + $output .= PHP_EOL . '{{SNIPPET_CONTENT}}' . PHP_EOL;
498 +
499 + return $output;
500 + }
501 +
502 + /**
503 + * Returns an example of the content of a universal snippet.
504 + *
505 + * @return string
506 + */
507 + protected static function get_simple_universal_snippet() {
508 + $output = '<!-- Global Site Tag (gtag.js) - Google Analytics -->' . PHP_EOL;
509 + $output .= '<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>' . PHP_EOL;
510 + $output .= '<script>' . PHP_EOL;
511 + $output .= "\twindow.dataLayer = window.dataLayer || [];" . PHP_EOL;
512 + $output .= "\tfunction gtag(){dataLayer.push(arguments);}" . PHP_EOL;
513 + $output .= "\tgtag('js', new Date());" . PHP_EOL;
514 + $output .= "\tgtag('config', 'GA_TRACKING_ID');" . PHP_EOL;
515 + $output .= '</script>' . PHP_EOL;
516 + $output .= '<!-- End global Site Tag (gtag.js) - Google Analytics -->' . PHP_EOL;
517 +
518 + return $output;
519 + }
520 +
521 + /**
522 + * Returns an example of the content of a woocommerce snippet.
523 + *
524 + * @return string
525 + */
526 + protected static function get_woo_snippet() {
527 + $output = <<<'SCRIPT'
528 +<script type="text/javascript">
529 + var pixel_id = ''; // Add you FB pixel ID!
530 + !function (f, b, e, v, n, t, s) {
531 + if (f.fbq) return; n = f.fbq = function () {
532 + n.callMethod ?
533 + n.callMethod.apply(n, arguments) : n.queue.push(arguments)
534 + }; if (!f._fbq) f._fbq = n;
535 + n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0;
536 + t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s)
537 + }(window,document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
538 +
539 + fbq('init', pixel_id);
540 + fbq('track', 'PageView');
541 +</script>
542 +SCRIPT;
543 +
544 + return $output;
545 + }
546 +
547 + /**
548 + * Wrapper for register shortcode
549 + *
550 + * @param $name
551 + * @param $obj
552 + */
553 + /**
554 + * Register a shortcode class
555 + *
556 + * @param string $name Shortcode class name.
557 + * @param object $obj Plugin object.
558 + *
559 + * @return void
560 + */
561 + public static function register_shortcode( $name, $obj ) {
562 + if ( class_exists( $name ) ) {
563 + new $name( $obj );
564 + }
565 + }
566 +
567 + /**
568 + * Render html for purchase button
569 + */
570 + public static function get_purchase_button( $utm_tracking_location = 'snippet-library-page' ) {
571 + $price_url = tsdk_utmify( WINP_UPGRADE, 'upsell_button', $utm_tracking_location );
572 + ?>
573 + <p class="winp-purchase-button">
574 + <a class="button" id="winp-library-buy-button" href="<?php echo esc_url( $price_url ); ?>" target="_blank">
575 + <span><?php _e( 'Upgrade to Pro', 'insert-php' ); ?></span>
576 + </a>
577 + </p>
578 + <?php
579 + }
580 +
581 + /**
582 + * Check if current user is admin or editor
583 + * todo: удалить этот метод, потому что он дублирует функционал WINP_Plugin::app()->current_user_car
584 + *
585 + * @return bool
586 + */
587 + public static function winp_check_user_admin() {
588 + return current_user_can( 'manage_options' ) || current_user_can( 'administrator' );
589 + }
590 +
591 + /*
592 + * Flushes as many page cache plugin's caches as possible.
593 + *
594 + * @return void
595 + */
596 + public static function flush_page_cache() {
597 + if ( function_exists( 'wp_cache_clear_cache' ) ) {
598 + if ( is_multisite() ) {
599 + $blog_id = get_current_blog_id();
600 + wp_cache_clear_cache( $blog_id );
601 + } else {
602 + wp_cache_clear_cache();
603 + }
604 + } elseif ( has_action( 'cachify_flush_cache' ) ) {
605 + do_action( 'cachify_flush_cache' );
606 + } elseif ( function_exists( 'w3tc_pgcache_flush' ) ) {
607 + w3tc_pgcache_flush();
608 + } elseif ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) {
609 + wp_fast_cache_bulk_delete_all();
610 + } elseif ( class_exists( 'WpFastestCache' ) ) {
611 + $wpfc = new WpFastestCache();
612 + $wpfc->deleteCache();
613 + } elseif ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) {
614 + c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
615 + } elseif ( class_exists( 'zencache' ) ) {
616 + zencache::clear();
617 + } elseif ( class_exists( 'comet_cache' ) ) {
618 + comet_cache::clear();
619 + } elseif ( class_exists( 'WpeCommon' ) ) {
620 + // WPEngine cache purge/flush methods to call by default
621 + $wpe_methods = [
622 + 'purge_varnish_cache',
623 + ];
624 +
625 + // More agressive clear/flush/purge behind a filter
626 + if ( apply_filters( 'wbcr/factory/flush_wpengine_aggressive', false ) ) {
627 + $wpe_methods = array_merge( $wpe_methods, [ 'purge_memcached', 'clear_maxcdn_cache' ] );
628 + }
629 +
630 + // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing)
631 + $wpe_methods = apply_filters( 'wbcr/factory/wpengine_methods', $wpe_methods );
632 +
633 + foreach ( $wpe_methods as $wpe_method ) {
634 + if ( method_exists( 'WpeCommon', $wpe_method ) ) {
635 + WpeCommon::$wpe_method();
636 + }
637 + }
638 + } elseif ( function_exists( 'sg_cachepress_purge_cache' ) ) {
639 + sg_cachepress_purge_cache();
640 + } elseif ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) {
641 + // fallback for WP-Super-Cache
642 + global $cache_path;
643 + if ( is_multisite() ) {
644 + $blog_id = get_current_blog_id();
645 + prune_super_cache( get_supercache_dir( $blog_id ), true );
646 + prune_super_cache( $cache_path . 'blogs/', true );
647 + } else {
648 + prune_super_cache( $cache_path . 'supercache/', true );
649 + prune_super_cache( $cache_path, true );
650 + }
651 + }
652 + }
653 +
654 + /**
655 + * @param $post WP_Post
656 + *
657 + * @return string
658 + * @since 2.4.0
659 + */
660 + public static function get_where_use_text( $post ) {
661 + global $winp_snippets_locations;
662 + $snippet_scope = self::getMetaOption( $post->ID, 'snippet_scope' );
663 + $result = '';
664 +
665 + if ( $snippet_scope == 'evrywhere' ) {
666 + $result = __( 'Run everywhere', 'insert-php' );
667 + } elseif ( $snippet_scope == 'auto' ) {
668 + $items = $winp_snippets_locations->getList();
669 +
670 + $snippet_location = self::getMetaOption( $post->ID, 'snippet_location', '' );
671 +
672 + if ( ! empty( $snippet_location ) ) {
673 + $text = $items[ $snippet_location ][0] ?? '';
674 + } else {
675 + $text = __( 'Everywhere', 'insert-php' );
676 + }
677 +
678 + $result = esc_html( $text );
679 + } else {
680 + $result = self::get_shortcode_text( $post );
681 + }
682 +
683 + return $result;
684 + }
685 +
686 + /**
687 + * @param $post WP_Post
688 + *
689 + * @return string
690 + */
691 + public static function get_shortcode_text( $post ) {
692 + $snippet_type = self::get_snippet_type( $post->ID );
693 + $snippet_type = ( $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? '' : $snippet_type . '_' );
694 +
695 + return esc_html( apply_filters( 'wbcr/inp/viewtable/where_use', '[wbcr_' . $snippet_type . 'snippet id="' . $post->ID . '"]', $post->ID ) );
696 + }
697 +
698 + /**
699 + * @param $post WP_Post
700 + *
701 + * @return int
702 + */
703 + public static function get_next_snippet_priority() {
704 + global $wpdb;
705 +
706 + $max_priority = $wpdb->get_var(
707 + "
708 + SELECT MAX(CAST(meta_value AS UNSIGNED))
709 + FROM {$wpdb->postmeta}
710 + WHERE meta_key = 'wbcr_inp_snippet_priority'"
711 + );
712 +
713 + if ( is_null( $max_priority ) ) {
714 + $max_priority = 0;
715 + } else {
716 + $max_priority = (int) $max_priority;
717 + }
718 +
719 + return $max_priority + 10;
720 + }
721 +
722 + /**
723 + * @return bool
724 + *
725 + * @since 2.4.0
726 + */
727 + public static function is_woo_active() {
728 + return is_plugin_active( 'woocommerce/woocommerce.php' );
729 + }
730 +}