PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.5
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.5
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.7.5, at includes/class.helpers.php

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