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

678 lines 23.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 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 }
678