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

731 lines 23.4 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 * @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 }
731