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

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

793 lines 27.0 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 * @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
69 unset( $_COOKIE['wbcr-php-snippets-safe-mode'] );
70
71 setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/' );
72 setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/wp-admin' );
73
74 return true;
75 }
76
77 return false;
78 }
79
80 /**
81 * Should show a page about the plugin or not.
82 *
83 * @return bool
84 */
85 public static function is_need_show_about_page() {
86 $need_show_about = (int) get_option( WINP_Plugin::app()->getOptionName( 'what_new_210' ) );
87
88 $is_ajax = WINP_Helper::doing_ajax();
89 $is_cron = WINP_Helper::doing_cron();
90 $is_rest = WINP_Helper::doing_rest_api();
91
92 if ( $need_show_about && ! $is_ajax && ! $is_cron && ! $is_rest ) {
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 'post_type' => WINP_SNIPPETS_POST_TYPE,
110 'meta_query' => [
111 'relation' => 'AND',
112 [
113 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope',
114 'value' => 'shortcode'
115 ],
116 [
117 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_activate',
118 'value' => 1
119 ]
120 ],
121 'post_status' => 'publish',
122 'numberposts' => - 1
123 ] );
124
125 $result = [];
126
127 if ( ! empty( $snippets ) ) {
128 foreach ( (array) $snippets as $snippet ) {
129 $tag_names = [ 'id' ];
130 $snippet_type = WINP_Helper::get_snippet_type( $snippet->ID );
131
132 $available_tags = WINP_Helper::getMetaOption( $snippet->ID, 'snippet_tags' );
133 $available_tags = trim( rtrim( $available_tags ) );
134
135 if ( ! empty( $available_tags ) ) {
136 $available_tags = array_map( 'trim', explode( ',', $available_tags ) );
137 $available_tags = array_unique( $available_tags );
138 } else {
139 if ( $snippet_type !== 'text' && $snippet_type !== 'advert' ) {
140 $available_tags = [ 'id', 'title' ];
141 } else {
142 $available_tags = [ 'id' ];
143 }
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 } else if ( '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 null $post_id
181 *
182 * @return array|mixed|string
183 */
184 public static function get_snippet_type( $post_id = null ) {
185 global $post;
186
187 $_post = $post;
188
189 $snippet_type = WINP_Plugin::app()->request->get( 'winp_item', WINP_SNIPPET_TYPE_PHP, 'sanitize_key' );
190 $get_post = WINP_Plugin::app()->request->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
200 if ( ! empty( $_post ) && WINP_SNIPPETS_POST_TYPE === $_post->post_type ) {
201 $_snippet_type = get_post_meta( $_post->ID, WINP_Plugin::app()->getPrefix() . 'snippet_type', true );
202 $snippet_type = $_snippet_type ? $_snippet_type : $snippet_type;
203 }
204
205 return $snippet_type;
206 }
207
208 /**
209 * Checks if the current request is a WP REST API request.
210 *
211 * Case #1: After WP_REST_Request initialisation
212 * Case #2: Support "plain" permalink settings
213 * Case #3: URL Path begins with wp-json/ (your REST prefix)
214 * Also supports WP installations in subfolders
215 *
216 * @author matzeeable https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist
217 * @since 2.1.0
218 * @return boolean
219 */
220 public static function doing_rest_api() {
221 $prefix = rest_get_url_prefix();
222 $rest_route = WINP_Plugin::app()->request->get( 'rest_route', null );
223 if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
224 || ! is_null( $rest_route ) // (#2)
225 && strpos( trim( $rest_route, '\\/' ), $prefix, 0 ) === 0 ) {
226 return true;
227 }
228
229 // (#3)
230 $rest_url = wp_parse_url( site_url( $prefix ) );
231 $current_url = wp_parse_url( add_query_arg( [] ) );
232
233 return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
234 }
235
236 /**
237 * @return bool
238 * @since 2.1.0
239 */
240 public static function doing_ajax() {
241 if ( function_exists( 'wp_doing_ajax' ) ) {
242 return wp_doing_ajax();
243 }
244
245 return defined( 'DOING_AJAX' ) && DOING_AJAX;
246 }
247
248 /**
249 * @return bool
250 * @since 2.1.0
251 */
252 public static function doing_cron() {
253 if ( function_exists( 'wp_doing_cron' ) ) {
254 return wp_doing_cron();
255 }
256
257 return defined( 'DOING_CRON' ) && DOING_CRON;
258 }
259
260 /**
261 * In the new version of the plugin, we moved the code of the snippets
262 * from the meta data to the post table, to the post_content cell.
263 *
264 * If the migration was an error, we need to reliably get the
265 * snippet code if the post_content cell is empty.
266 *
267 * @param WP_Post $post
268 *
269 * @return string snippet code
270 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
271 * @since 2.2.1
272 *
273 */
274 public static function get_snippet_code( $post ) {
275 if ( empty( $post->post_content ) ) {
276 return WINP_Helper::getMetaOption( $post->ID, 'snippet_code' );
277 }
278
279 return $post->post_content;
280 }
281
282 /**
283 * Get meta option
284 *
285 * @param int $post_id
286 * @param string $option_name
287 * @param mixed $default
288 *
289 * @return mixed|array
290 */
291 public static function getMetaOption( $post_id, $option_name, $default = null ) {
292 $post_id = (int) $post_id;
293
294 if ( ! isset( self::$meta_options[ $post_id ] ) || empty( self::$meta_options[ $post_id ] ) ) {
295 $meta_vals = get_post_meta( $post_id, '', true );
296
297 if ( ! empty( $meta_vals ) && is_array( $meta_vals ) ) {
298 foreach ( $meta_vals as $name => $val ) {
299 self::$meta_options[ $post_id ][ $name ] = $val[0];
300 }
301 }
302 }
303
304 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;
305 }
306
307 /**
308 * Udpdate meta option
309 *
310 * @param int $post_id
311 * @param string $option_name
312 * @param mixed $option_value
313 *
314 * @return bool|int
315 */
316 public static function updateMetaOption( $post_id, $option_name, $option_value ) {
317 $post_id = (int) $post_id;
318
319 return update_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name, $option_value );
320 }
321
322 /**
323 * Remove meta option
324 *
325 * @param int $post_id
326 * @param string $option_name
327 *
328 * @return bool|int
329 */
330 public static function removeMetaOption( $post_id, $option_name ) {
331 $post_id = (int) $post_id;
332
333 return delete_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name );
334 }
335
336 /**
337 * Check capabilities for snippets post type.
338 *
339 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
340 * @since 2.2.0
341 */
342 public static function has_post_capabilities() {
343 $role = get_role( 'administrator' );
344
345 if ( ! $role ) {
346 return false;
347 }
348
349 return $role->has_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE );
350 }
351
352 /**
353 * Set capabilities for snippets post type.
354 *
355 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
356 * @since 2.2.0
357 */
358 public static function set_post_capabilities() {
359 $role = get_role( 'administrator' );
360
361 if ( ! $role ) {
362 return false;
363 }
364
365 $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE );
366 $role->add_cap( 'read_' . WINP_SNIPPETS_POST_TYPE );
367 $role->add_cap( 'delete_' . WINP_SNIPPETS_POST_TYPE );
368 $role->add_cap( 'edit_' . WINP_SNIPPETS_POST_TYPE . 's' );
369 $role->add_cap( 'edit_others_' . WINP_SNIPPETS_POST_TYPE . 's' );
370 $role->add_cap( 'publish_' . WINP_SNIPPETS_POST_TYPE . 's' );
371 $role->add_cap( 'read_private_' . WINP_SNIPPETS_POST_TYPE . 's' );
372
373 return true;
374 }
375
376 /**
377 * Create a demo snippets with examples of use
378 */
379 public static function create_demo_snippets() {
380
381 update_option( WINP_Plugin::app()->getOptionName( 'activate_by_default' ), 1 );
382 update_option( WINP_Plugin::app()->getOptionName( 'complete_uninstall' ), 0 );
383 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_theme' ), 'default' );
384 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_indent_with_tabs' ), 1 );
385 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_tab_size' ), 4 );
386 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_indent_unit' ), 4 );
387 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_wrap_lines' ), 1 );
388 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_line_numbers' ), 1 );
389 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_auto_close_brackets' ), 1 );
390 update_option( WINP_Plugin::app()->getOptionName( 'code_editor_highlight_selection_matches' ), 0 );
391
392 $posts = [
393 [
394 'post_title' => __( 'Simple php snippet: Disable emojis', 'insert-php' ),
395 'post_name' => 'simple-php-snippet',
396 'post_content' => self::get_simple_php_snippet(),
397 'meta' => [
398 'type' => WINP_SNIPPET_TYPE_PHP,
399 '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' ),
400 'tags' => [ 'php', 'disable features' ],
401 'priority' => 30
402 ]
403 ],
404 [
405 'post_title' => __( 'Simple text snippet: What is Lorem Ipsum?', 'insert-php' ),
406 'post_name' => 'simple-text-snippet',
407 'post_content' => self::get_simple_text_snippet(),
408 'meta' => [
409 'type' => WINP_SNIPPET_TYPE_TEXT,
410 'description' => __( 'This ordinary maintenance text. With this snippet, you can fill your pages with meaningless English text.', 'insert-php' ),
411 '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";}}',
412 'tags' => [ 'text', 'lorem ipsum' ],
413 'priority' => 20
414 ]
415 ],
416 [
417 'post_title' => __( 'Simple universal snippet: Google analytics tracking', 'insert-php' ),
418 'post_name' => 'simple-universal-snippet',
419 'post_content' => self::get_simple_universal_snippet(),
420 'meta' => [
421 'type' => WINP_SNIPPET_TYPE_UNIVERSAL,
422 '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.' ),
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:8:"base_web";}}}}s:4:"type";s:6:"showif";}}',
424 'tags' => [ 'universal', 'tracking' ],
425 'priority' => 10
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 'post_content' => $post['post_content'],
434 'post_title' => $post['post_title'],
435 'post_status' => 'publish',
436 'post_type' => WINP_SNIPPETS_POST_TYPE
437 ] );
438
439 if ( ! is_wp_error( $post_id ) ) {
440 if ( isset( $post['meta']['type'] ) ) {
441 WINP_Helper::updateMetaOption( $post_id, 'snippet_type', $post['meta']['type'] );
442
443 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_PHP ) {
444 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'evrywhere' );
445 }
446 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_TEXT ) {
447 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'shortcode' );
448 }
449 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_UNIVERSAL ) {
450 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'auto' );
451 WINP_Helper::updateMetaOption( $post_id, 'snippet_location', 'header' );
452 }
453 }
454
455 if ( isset( $post['meta']['description'] ) ) {
456 WINP_Helper::updateMetaOption( $post_id, 'snippet_description', $post['meta']['description'] );
457 }
458
459 if ( isset( $post['meta']['filters'] ) && is_serialized( $post['meta']['filters'] ) ) {
460 $unserialized_filters = unserialize( $post['meta']['filters'] );
461 WINP_Helper::updateMetaOption( $post_id, 'snippet_filters', $unserialized_filters );
462 WINP_Helper::updateMetaOption( $post_id, 'changed_filters', 1 );
463 }
464
465 if ( isset( $post['meta']['tags'] ) && ! empty( $post['meta']['tags'] ) ) {
466 if ( ! taxonomy_exists( WINP_SNIPPETS_TAXONOMY ) ) {
467 register_taxonomy( WINP_SNIPPETS_TAXONOMY, WINP_SNIPPETS_POST_TYPE, [] );
468 }
469
470 wp_set_post_terms( $post_id, $post['meta']['tags'], WINP_SNIPPETS_TAXONOMY, true );
471 }
472
473 if ( isset( $post['meta']['priority'] ) ) {
474 WINP_Helper::updateMetaOption( $post_id, 'snippet_priority', $post['meta']['priority'] );
475 }
476 }
477 }
478
479 update_option( WINP_Plugin::app()->getOptionName( 'demo_snippets_created' ), 1 );
480 }
481
482 /**
483 * Returns an example php snippet content
484 *
485 * @return string
486 */
487 protected static function get_simple_php_snippet() {
488 $output = "/**" . PHP_EOL;
489 $output .= "* Disable WP 4.2 emoji" . PHP_EOL;
490 $output .= "*/" . PHP_EOL;
491 $output .= "function ace_remove_emoji() {" . PHP_EOL;
492 $output .= "\tadd_filter( 'emoji_svg_url', '__return_false' );" . PHP_EOL;
493 $output .= "\tremove_action( 'admin_print_styles', 'print_emoji_styles' );" . PHP_EOL;
494 $output .= "\tremove_action( 'wp_head', 'print_emoji_detection_script', 7 );" . PHP_EOL;
495 $output .= "\tremove_action( 'admin_print_scripts', 'print_emoji_detection_script' );" . PHP_EOL;
496 $output .= "\tremove_action( 'wp_print_styles', 'print_emoji_styles' );" . PHP_EOL;
497 $output .= "\tremove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );" . PHP_EOL;
498 $output .= "\tremove_filter( 'the_content_feed', 'wp_staticize_emoji' );" . PHP_EOL;
499 $output .= "\tremove_filter( 'comment_text_rss', 'wp_staticize_emoji' );" . PHP_EOL;
500 $output .= "\t// filter to remove TinyMCE emojis" . PHP_EOL;
501 $output .= "\tadd_filter( 'tiny_mce_plugins', 'ace_disable_emoji_tinymce' );" . PHP_EOL;
502 $output .= "}" . PHP_EOL;
503 $output .= "add_action( 'init', 'ace_remove_emoji' );" . PHP_EOL;
504 $output .= "/**" . PHP_EOL;
505 $output .= "* Remove tinyMCE emoji" . PHP_EOL;
506 $output .= "*/" . PHP_EOL;
507 $output .= "function ace_disable_emoji_tinymce( \$plugins ) {" . PHP_EOL;
508 $output .= "\tunset( \$plugins['wpemoji'] );" . PHP_EOL;
509 $output .= "\treturn \$plugins;" . PHP_EOL;
510 $output .= "}" . PHP_EOL;
511
512 return $output;
513 }
514
515 /**
516 * Returns an example of the content of a text snippet.
517 *
518 * @return string
519 */
520 protected static function get_simple_text_snippet() {
521 $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.';
522 $output .= PHP_EOL . "{{SNIPPET_CONTENT}}" . PHP_EOL;
523
524 return $output;
525 }
526
527 /**
528 * Returns an example of the content of a universal snippet.
529 *
530 * @return string
531 */
532 protected static function get_simple_universal_snippet() {
533 $output = "<!-- Global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
534 $output .= "<script async src=\"https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID\"></script>" . PHP_EOL;
535 $output .= "<script>" . PHP_EOL;
536 $output .= "\twindow.dataLayer = window.dataLayer || [];" . PHP_EOL;
537 $output .= "\tfunction gtag(){dataLayer.push(arguments);}" . PHP_EOL;
538 $output .= "\tgtag('js', new Date());" . PHP_EOL;
539 $output .= "\tgtag('config', 'GA_TRACKING_ID');" . PHP_EOL;
540 $output .= "</script>" . PHP_EOL;
541 $output .= "<!-- End global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
542
543 return $output;
544 }
545
546 /**
547 * Wrapper for register shortcode
548 *
549 * @param $name
550 * @param $obj
551 */
552 public static function register_shortcode( $name, $obj ) {
553 Wbcr_FactoryShortcodes329::register( $name, $obj );
554 }
555
556 /**
557 * Wrapper for get factory class for container
558 *
559 * @return string
560 */
561 public static function get_factory_class() {
562 return "factory-bootstrap-433 factory-fontawesome-000";
563 }
564
565 /**
566 * Wrapper for Wbcr_FactoryForms430_OptionsValueProvider object
567 *
568 * @param $plugin
569 *
570 * @return Wbcr_FactoryForms430_OptionsValueProvider
571 */
572 public static function get_options_value_provider( $plugin ) {
573 return new Wbcr_FactoryForms430_OptionsValueProvider( $plugin );
574 }
575
576 /**
577 * Wrapper for get factory form object
578 *
579 * @param $options
580 * @param $plugin
581 *
582 * @return Wbcr_FactoryForms430_Form
583 */
584 public static function get_factory_form( $options, $plugin ) {
585 return new Wbcr_FactoryForms430_Form( $options, $plugin );
586 }
587
588 /**
589 * Wrapper for register factory metaboxes
590 *
591 * @param $class_name_or_object
592 * @param $post_type
593 * @param $plugin
594 */
595 public static function register_factory_metaboxes( $class_name_or_object, $post_type, $plugin ) {
596 Wbcr_FactoryMetaboxes409::registerFor( $class_name_or_object, $post_type, $plugin );
597 }
598
599 /**
600 * Render html for purchase button
601 */
602 public static function get_purchase_button( $utm_tracking_location = 'snippet-library-page' ) {
603 $price_url = WINP_Plugin::app()->get_support()->get_pricing_url( true, $utm_tracking_location );
604 $price = WINP_Plugin::app()->premium->get_price();
605 $price = empty( $price ) ? 19 : $price;
606 ?>
607 <p class="winp-purchase-button">
608 <a class="button" id="winp-library-buy-button" href="<?php echo esc_url( $price_url ) ?>" target="_blank">
609 <span><?php echo __( 'Purchase premium for', 'insert-php' ) . ' $' . esc_attr( $price ); ?></span>
610 </a>
611 </p>
612 <?php
613 }
614
615 /**
616 * Check if current user is admin or editor
617 * todo: удалить этот метод, потому что он дублирует функционал WINP_Plugin::app()->currentUserCan
618 *
619 * @return bool
620 */
621 public static function winp_check_user_admin() {
622 return current_user_can( 'manage_options' ) || current_user_can( 'administrator' );
623 }
624
625 /*
626 * Flushes as many page cache plugin's caches as possible.
627 *
628 * @return void
629 */
630 public static function flush_page_cache() {
631 if ( function_exists( 'wp_cache_clear_cache' ) ) {
632 if ( is_multisite() ) {
633 $blog_id = get_current_blog_id();
634 wp_cache_clear_cache( $blog_id );
635 } else {
636 wp_cache_clear_cache();
637 }
638 } else if ( has_action( 'cachify_flush_cache' ) ) {
639 do_action( 'cachify_flush_cache' );
640 } else if ( function_exists( 'w3tc_pgcache_flush' ) ) {
641 w3tc_pgcache_flush();
642 } else if ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) {
643 wp_fast_cache_bulk_delete_all();
644 } else if ( class_exists( 'WpFastestCache' ) ) {
645 $wpfc = new WpFastestCache();
646 $wpfc->deleteCache();
647 } else if ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) {
648 c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
649 } else if ( class_exists( 'zencache' ) ) {
650 zencache::clear();
651 } else if ( class_exists( 'comet_cache' ) ) {
652 comet_cache::clear();
653 } else if ( class_exists( 'WpeCommon' ) ) {
654 // WPEngine cache purge/flush methods to call by default
655 $wpe_methods = [
656 'purge_varnish_cache',
657 ];
658
659 // More agressive clear/flush/purge behind a filter
660 if ( apply_filters( 'wbcr/factory/flush_wpengine_aggressive', false ) ) {
661 $wpe_methods = array_merge( $wpe_methods, [ 'purge_memcached', 'clear_maxcdn_cache' ] );
662 }
663
664 // Filtering the entire list of WpeCommon methods to be called (for advanced usage + easier testing)
665 $wpe_methods = apply_filters( 'wbcr/factory/wpengine_methods', $wpe_methods );
666
667 foreach ( $wpe_methods as $wpe_method ) {
668 if ( method_exists( 'WpeCommon', $wpe_method ) ) {
669 WpeCommon::$wpe_method();
670 }
671 }
672 } else if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
673 sg_cachepress_purge_cache();
674 } else if ( file_exists( WP_CONTENT_DIR . '/wp-cache-config.php' ) && function_exists( 'prune_super_cache' ) ) {
675 // fallback for WP-Super-Cache
676 global $cache_path;
677 if ( is_multisite() ) {
678 $blog_id = get_current_blog_id();
679 prune_super_cache( get_supercache_dir( $blog_id ), true );
680 prune_super_cache( $cache_path . 'blogs/', true );
681 } else {
682 prune_super_cache( $cache_path . 'supercache/', true );
683 prune_super_cache( $cache_path, true );
684 }
685 }
686 }
687
688 /**
689 * @since 2.4.0
690 *
691 * @param $post WP_Post
692 *
693 * @return string
694 */
695 public static function get_where_use_text( $post ) {
696 $snippet_scope = self::getMetaOption( $post->ID, 'snippet_scope' );
697 $result = "";
698
699 if ( $snippet_scope == 'evrywhere' ) {
700 $result = __( 'Run everywhere', 'insert-php' );
701 } else if ( $snippet_scope == 'auto' ) {
702 $items = [
703 WINP_SNIPPET_AUTO_HEADER => __( 'Header', 'insert-php' ),
704 WINP_SNIPPET_AUTO_FOOTER => __( 'Footer', 'insert-php' ),
705 WINP_SNIPPET_AUTO_BEFORE_POST => __( 'Insert Before Post', 'insert-php' ),
706 WINP_SNIPPET_AUTO_BEFORE_CONTENT => __( 'Insert Before Content', 'insert-php' ),
707 WINP_SNIPPET_AUTO_BEFORE_PARAGRAPH => __( 'Insert Before Paragraph', 'insert-php' ),
708 WINP_SNIPPET_AUTO_AFTER_PARAGRAPH => __( 'Insert After Paragraph', 'insert-php' ),
709 WINP_SNIPPET_AUTO_AFTER_CONTENT => __( 'Insert After Content', 'insert-php' ),
710 WINP_SNIPPET_AUTO_AFTER_POST => __( 'Insert After Post', 'insert-php' ),
711 WINP_SNIPPET_AUTO_BEFORE_EXCERPT => __( 'Insert Before Excerpt', 'insert-php' ),
712 WINP_SNIPPET_AUTO_AFTER_EXCERPT => __( 'Insert After Excerpt', 'insert-php' ),
713 WINP_SNIPPET_AUTO_BETWEEN_POSTS => __( 'Between Posts', 'insert-php' ),
714 WINP_SNIPPET_AUTO_BEFORE_POSTS => __( 'Before post', 'insert-php' ),
715 WINP_SNIPPET_AUTO_AFTER_POSTS => __( 'After post', 'insert-php' ),
716 ];
717
718 $snippet_location = self::getMetaOption( $post->ID, 'snippet_location', '' );
719
720 switch ( $snippet_location ) {
721 case WINP_SNIPPET_AUTO_HEADER:
722 case WINP_SNIPPET_AUTO_FOOTER:
723 $text = __( 'Everywhere', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']';
724 break;
725
726 case WINP_SNIPPET_AUTO_BEFORE_POST:
727 case WINP_SNIPPET_AUTO_BEFORE_CONTENT:
728 case WINP_SNIPPET_AUTO_BEFORE_PARAGRAPH:
729 case WINP_SNIPPET_AUTO_AFTER_PARAGRAPH:
730 case WINP_SNIPPET_AUTO_AFTER_CONTENT:
731 case WINP_SNIPPET_AUTO_AFTER_POST:
732 $text = __( 'Posts, Pages, Custom post types', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']';
733 break;
734
735 case WINP_SNIPPET_AUTO_BEFORE_EXCERPT:
736 case WINP_SNIPPET_AUTO_AFTER_EXCERPT:
737 case WINP_SNIPPET_AUTO_BETWEEN_POSTS:
738 case WINP_SNIPPET_AUTO_BEFORE_POSTS:
739 case WINP_SNIPPET_AUTO_AFTER_POSTS:
740 $text = __( 'Categories, Archives, Tags, Taxonomies', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']';
741 break;
742
743 default:
744 $text = __( 'Everywhere', 'insert-php' );
745 }
746
747 $result = __( 'Automatic insertion', 'insert-php' ) . ': ' . esc_html( $text );
748 } else {
749 $result = self::get_shortcode_text( $post );
750 }
751
752 return $result;
753 }
754
755 /**
756 * @since 2.4.0
757 *
758 * @param $post WP_Post
759 *
760 * @return string
761 */
762 public static function get_shortcode_text( $post ) {
763 $snippet_type = self::get_snippet_type( $post->ID );
764 $snippet_type = ( $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? '' : $snippet_type . '_' );
765
766 return esc_html( apply_filters( 'wbcr/inp/viewtable/where_use', '[wbcr_' . $snippet_type . 'snippet id="' . $post->ID . '"]', $post->ID ) );
767 }
768
769 /**
770 * @since 2.4.0
771 *
772 * @param $post WP_Post
773 *
774 * @return int
775 */
776 public static function get_next_snippet_priority() {
777 global $wpdb;
778
779 $max_priority = $wpdb->get_var( "
780 SELECT MAX(CAST(meta_value AS UNSIGNED))
781 FROM {$wpdb->postmeta}
782 WHERE meta_key = '" . WINP_Plugin::app()->getPrefix() . "snippet_priority'" );
783
784 if ( is_null( $max_priority ) ) {
785 $max_priority = 0;
786 } else {
787 $max_priority = (int) $max_priority;
788 }
789
790 return $max_priority + 10;
791 }
792 }
793