PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.2.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.2.7
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
← All changes | includes/class.helpers.php +366 -151 2.1.912.2.7 View file →
@@ -1,73 +1,83 @@
1 1 <?php
2 -
3 2 /**
4 3 * Helpers tools
5 - * @author Webcraftic <wordpress.webraftic@gmail.com>
4 + *
5 + * @author Webcraftic <wordpress.webraftic@gmail.com>
6 6 * @copyright (c) 09.11.2017, Webcraftic
7 - * @version 1.0
7 + * @version 1.0
8 8 */
9 +
10 +// Exit if accessed directly
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit;
13 +}
14 +
9 15 class WINP_Helper {
10 -
11 - private static $meta_options = array();
12 -
16 +
17 + private static $meta_options = [];
18 +
13 19 /**
14 20 * @return bool
15 21 */
16 22 public static function is_safe_mode() {
17 23 global $wbcr_inp_safe_mode;
18 -
24 +
19 25 if ( ! WINP_Plugin::app()->currentUserCan() ) {
20 26 return false;
21 27 }
22 -
28 +
23 29 if ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) {
24 30 return true;
25 31 }
26 -
32 +
27 33 return false;
28 34 }
29 -
35 +
30 36 /**
31 37 * Enables safe mode, in which the php code will not be executed.
32 38 */
33 39 public static function enable_safe_mode() {
34 40 global $wbcr_inp_safe_mode;
35 -
41 +
36 42 if ( ! WINP_Plugin::app()->currentUserCan() ) {
37 43 return false;
38 44 }
39 -
45 +
40 46 if ( ( ! $wbcr_inp_safe_mode || ! isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
41 47 $wbcr_inp_safe_mode = true;
42 48 setcookie( "wbcr-php-snippets-safe-mode", 1, time() + 3600, '/' );
43 -
49 +
44 50 return true;
45 51 }
46 -
52 +
47 53 return false;
48 54 }
49 -
55 +
50 56 /**
51 57 * Disable safe mode, in which the php code will not be executed.
52 58 */
53 59 public static function disable_safe_mode() {
54 60 global $wbcr_inp_safe_mode;
55 -
61 +
56 62 if ( ! WINP_Plugin::app()->currentUserCan() ) {
57 - return;
63 + return false;
58 64 }
59 -
60 - if ( ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) ) {
65 +
66 + if ( $wbcr_inp_safe_mode || isset( $_COOKIE['wbcr-php-snippets-safe-mode'] ) ) {
61 67 $wbcr_inp_safe_mode = false;
68 +
62 69 unset( $_COOKIE['wbcr-php-snippets-safe-mode'] );
70 +
63 71 setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/' );
64 -
65 - wp_safe_redirect( remove_query_arg( array( 'wbcr-php-snippets-disable-safe-mode' ) ) );
66 - die();
72 + setcookie( 'wbcr-php-snippets-safe-mode', null, - 1, '/wp-admin' );
73 +
74 + return true;
67 75 }
76 +
77 + return false;
68 78 }
69 -
79 +
70 80 /**
71 81 * Should show a page about the plugin or not.
72 82 *
73 83 * @return bool
@@ -72,72 +82,75 @@
72 82 *
73 83 * @return bool
74 84 */
75 85 public static function is_need_show_about_page() {
76 - $need_show_about = (int)get_option(WINP_Plugin::app()->getOptionName( 'what_new_210' ));
77 -
86 + $need_show_about = (int) get_option( WINP_Plugin::app()->getOptionName( 'what_new_210' ) );
87 +
78 88 $is_ajax = WINP_Helper::doing_ajax();
79 89 $is_cron = WINP_Helper::doing_cron();
80 90 $is_rest = WINP_Helper::doing_rest_api();
81 -
91 +
82 92 if ( $need_show_about && ! $is_ajax && ! $is_cron && ! $is_rest ) {
83 93 return true;
84 94 }
85 -
95 +
86 96 return false;
87 97 }
88 -
98 +
89 99 /**
90 100 * Gets and verified available attributes for snippets shortcodes.
91 101 *
92 - * @return array
102 + * @param bool $tinymce
103 + *
104 + * @return mixed|void
93 105 */
94 - public static function get_shortcode_data() {
95 -
96 - $snippets = get_posts( array(
106 + public static function get_shortcode_data( $tinymce = false ) {
107 +
108 + $snippets = get_posts( [
97 109 'post_type' => WINP_SNIPPETS_POST_TYPE,
98 - 'meta_query' => array(
110 + 'meta_query' => [
99 111 'relation' => 'AND',
100 - array(
112 + [
101 113 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_scope',
102 114 'value' => 'shortcode'
103 - ),
104 - array(
115 + ],
116 + [
105 117 'key' => WINP_Plugin::app()->getPrefix() . 'snippet_activate',
106 118 'value' => 1
107 - )
108 - ),
119 + ]
120 + ],
109 121 'post_status' => 'publish',
110 122 'numberposts' => - 1
111 - ) );
112 -
113 - $result = array();
114 -
123 + ] );
124 +
125 + $result = [];
126 +
115 127 if ( ! empty( $snippets ) ) {
116 128 foreach ( (array) $snippets as $snippet ) {
117 - $tag_names = array( 'id' );
129 + $tag_names = [ 'id' ];
118 130 $snippet_type = WINP_Helper::get_snippet_type( $snippet->ID );
119 -
131 +
120 132 $available_tags = WINP_Helper::getMetaOption( $snippet->ID, 'snippet_tags' );
121 133 $available_tags = trim( rtrim( $available_tags ) );
122 -
134 +
123 135 if ( ! empty( $available_tags ) ) {
124 136 $available_tags = array_map( 'trim', explode( ',', $available_tags ) );
125 137 $available_tags = array_unique( $available_tags );
126 138 } else {
127 139 if ( $snippet_type !== 'text' ) {
128 - $available_tags = array( 'id', 'title' );
140 + $available_tags = [ 'id', 'title' ];
129 141 } else {
130 - $available_tags = array( 'id' );
142 + $available_tags = [ 'id' ];
131 143 }
132 144 }
133 -
134 - $tags = array(
145 +
146 + $tags = [
135 147 'id' => $snippet->ID,
136 148 'type' => $snippet_type,
149 + 'name' => $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? 'wbcr_snippet' : 'wbcr_' . $snippet_type . '_snippet',
137 150 'title' => empty( $snippet->post_title ) ? '(no titled, ID=' . $snippet->ID . ')' : $snippet->post_title
138 - );
139 -
151 + ];
152 +
140 153 if ( ! empty( $available_tags ) ) {
141 154 foreach ( (array) $available_tags as $tag ) {
142 155 if ( '' != $tag ) {
143 156 if ( 'title' == $tag ) {
@@ -150,18 +163,18 @@
150 163 }
151 164 }
152 165 }
153 166 }
154 -
167 +
155 168 $tags['snippet_tags'] = $tag_names;
156 -
169 +
157 170 $result[] = $tags;
158 171 }
159 172 }
160 -
161 - return $result;
173 +
174 + return apply_filters( 'wbcr/inp/helper/get_shortcode_data', $result, $tinymce );
162 175 }
163 -
176 +
164 177 /**
165 178 * Get snippet type
166 179 *
167 180 * @param null $post_id
@@ -169,29 +182,30 @@
169 182 * @return array|mixed|string
170 183 */
171 184 public static function get_snippet_type( $post_id = null ) {
172 185 global $post;
173 -
186 +
174 187 $_post = $post;
175 -
176 - $snippet_type = isset( $_GET['winp_item'] ) ? sanitize_text_field( $_GET['winp_item'] ) : WINP_SNIPPET_TYPE_PHP;
177 -
178 - if ( empty( $post_id ) && isset( $_GET['post'] ) && ! is_array( $_GET['post'] ) ) {
179 - $post_id = esc_attr( $_GET['post'] );
188 +
189 + $snippet_type = WINP_Plugin::app()->request->get( 'winp_item', WINP_SNIPPET_TYPE_PHP, true );
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 );
180 194 }
181 -
195 +
182 196 if ( ! empty( $post_id ) ) {
183 197 $_post = get_post( $post_id );
184 198 }
185 -
186 - if ( ! empty( $_post ) && $_post->post_type === WINP_SNIPPETS_POST_TYPE ) {
199 +
200 + if ( ! empty( $_post ) && WINP_SNIPPETS_POST_TYPE === $_post->post_type ) {
187 201 $_snippet_type = get_post_meta( $_post->ID, WINP_Plugin::app()->getPrefix() . 'snippet_type', true );
188 202 $snippet_type = $_snippet_type ? $_snippet_type : $snippet_type;
189 203 }
190 -
204 +
191 205 return $snippet_type;
192 206 }
193 -
207 +
194 208 /**
195 209 * Checks if the current request is a WP REST API request.
196 210 *
197 211 * Case #1: After WP_REST_Request initialisation
@@ -198,27 +212,28 @@
198 212 * Case #2: Support "plain" permalink settings
199 213 * Case #3: URL Path begins with wp-json/ (your REST prefix)
200 214 * Also supports WP installations in subfolders
201 215 *
202 - * @since 2.1.0
216 + * @author matzeeable https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist
217 + * @since 2.1.0
203 218 * @return boolean
204 - * @author matzeeable https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist
205 219 */
206 220 public static function doing_rest_api() {
207 - $prefix = rest_get_url_prefix();
221 + $prefix = rest_get_url_prefix();
222 + $rest_route = WINP_Plugin::app()->request->get( 'rest_route', null );
208 223 if ( defined( 'REST_REQUEST' ) && REST_REQUEST // (#1)
209 - || isset( $_GET['rest_route'] ) // (#2)
210 - && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix, 0 ) === 0 ) {
224 + || ! is_null( $rest_route ) // (#2)
225 + && strpos( trim( $rest_route, '\\/' ), $prefix, 0 ) === 0 ) {
211 226 return true;
212 227 }
213 -
228 +
214 229 // (#3)
215 230 $rest_url = wp_parse_url( site_url( $prefix ) );
216 - $current_url = wp_parse_url( add_query_arg( array() ) );
217 -
231 + $current_url = wp_parse_url( add_query_arg( [] ) );
232 +
218 233 return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
219 234 }
220 -
235 +
221 236 /**
222 237 * @since 2.1.0
223 238 * @return bool
224 239 */
@@ -225,12 +240,12 @@
225 240 public static function doing_ajax() {
226 241 if ( function_exists( 'wp_doing_ajax' ) ) {
227 242 return wp_doing_ajax();
228 243 }
229 -
244 +
230 245 return defined( 'DOING_AJAX' ) && DOING_AJAX;
231 246 }
232 -
247 +
233 248 /**
234 249 * @since 2.1.0
235 250 * @return bool
236 251 */
@@ -237,54 +252,78 @@
237 252 public static function doing_cron() {
238 253 if ( function_exists( 'wp_doing_cron' ) ) {
239 254 return wp_doing_cron();
240 255 }
241 -
256 +
242 257 return defined( 'DOING_CRON' ) && DOING_CRON;
243 258 }
244 -
259 +
245 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 + * @author Alexander Kovalev <alex.kovalevv@gmail.com>
268 + * @since 2.2.1
269 + *
270 + * @param WP_Post $post
271 + *
272 + * @return string snippet code
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 + /**
246 283 * Get meta option
247 284 *
248 - * @param int $post_id
285 + * @param int $post_id
249 286 * @param string $option_name
250 - * @param mixed $default
287 + * @param mixed $default
251 288 *
252 289 * @return mixed|array
253 290 */
254 291 public static function getMetaOption( $post_id, $option_name, $default = null ) {
255 292 $post_id = (int) $post_id;
256 -
293 +
257 294 if ( ! isset( self::$meta_options[ $post_id ] ) || empty( self::$meta_options[ $post_id ] ) ) {
258 295 $meta_vals = get_post_meta( $post_id, '', true );
259 -
260 - foreach ( $meta_vals as $name => $val ) {
261 - self::$meta_options[ $post_id ][ $name ] = $val[0];
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 + }
262 301 }
263 302 }
264 -
303 +
265 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;
266 305 }
267 -
306 +
268 307 /**
269 308 * Udpdate meta option
270 309 *
271 - * @param int $post_id
310 + * @param int $post_id
272 311 * @param string $option_name
273 - * @param mixed $option_value
312 + * @param mixed $option_value
274 313 *
275 314 * @return bool|int
276 315 */
277 316 public static function updateMetaOption( $post_id, $option_name, $option_value ) {
278 317 $post_id = (int) $post_id;
279 -
318 +
280 319 return update_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name, $option_value );
281 320 }
282 -
321 +
283 322 /**
284 323 * Remove meta option
285 324 *
286 - * @param int $post_id
325 + * @param int $post_id
287 326 * @param string $option_name
288 327 *
289 328 * @return bool|int
290 329 */
@@ -289,82 +328,116 @@
289 328 * @return bool|int
290 329 */
291 330 public static function removeMetaOption( $post_id, $option_name ) {
292 331 $post_id = (int) $post_id;
293 -
332 +
294 333 return delete_post_meta( $post_id, WINP_Plugin::app()->getPrefix() . $option_name );
295 334 }
296 -
335 +
297 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 + /**
298 377 * Create a demo snippets with examples of use
299 378 */
300 379 public static function create_demo_snippets() {
301 -
302 - WINP_Plugin::app()->updatePopulateOption( 'activate_by_default', 1 );
303 - WINP_Plugin::app()->updatePopulateOption( 'complete_uninstall', 0 );
304 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_theme', 'default' );
305 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_indent_with_tabs', 1 );
306 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_tab_size', 4 );
307 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_indent_unit', 4 );
308 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_wrap_lines', 1 );
309 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_line_numbers', 1 );
310 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_auto_close_brackets', 1 );
311 - WINP_Plugin::app()->updatePopulateOption( 'code_editor_highlight_selection_matches', 0 );
312 -
313 - $posts = array(
314 - array(
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 + [
315 394 'post_title' => __( 'Simple php snippet: Disable emojis', 'insert-php' ),
316 395 'post_name' => 'simple-php-snippet',
317 - 'post_content' => '',
318 - 'meta' => array(
396 + 'post_content' => self::get_simple_php_snippet(),
397 + 'meta' => [
319 398 'type' => WINP_SNIPPET_TYPE_PHP,
320 - 'code' => self::get_simple_php_snippet(),
321 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' ),
322 - 'tags' => array( 'php', 'disable features' )
323 - )
324 - ),
325 - array(
400 + 'tags' => [ 'php', 'disable features' ]
401 + ]
402 + ],
403 + [
326 404 'post_title' => __( 'Simple text snippet: What is Lorem Ipsum?', 'insert-php' ),
327 405 'post_name' => 'simple-text-snippet',
328 406 'post_content' => self::get_simple_text_snippet(),
329 - 'meta' => array(
407 + 'meta' => [
330 408 'type' => WINP_SNIPPET_TYPE_TEXT,
331 409 'description' => __( 'This ordinary maintenance text. With this snippet, you can fill your pages with meaningless English text.', 'insert-php' ),
332 410 '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";}}',
333 - 'tags' => array( 'text', 'lorem ipsum' )
334 - )
335 - ),
336 - array(
411 + 'tags' => [ 'text', 'lorem ipsum' ]
412 + ]
413 + ],
414 + [
337 415 'post_title' => __( 'Simple universal snippet: Google analytics tracking', 'insert-php' ),
338 416 'post_name' => 'simple-universal-snippet',
339 - 'post_content' => '',
340 - 'meta' => array(
417 + 'post_content' => self::get_simple_universal_snippet(),
418 + 'meta' => [
341 419 'type' => WINP_SNIPPET_TYPE_UNIVERSAL,
342 420 '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.' ),
343 - 'code' => self::get_simple_universal_snippet(),
344 421 '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";}}',
345 - 'tags' => array( 'universal', 'tracking' )
346 - )
347 - )
348 - );
349 -
422 + 'tags' => [ 'universal', 'tracking' ]
423 + ]
424 + ]
425 + ];
426 +
350 427 foreach ( $posts as $post ) {
351 428 // '@' here is to hide unexpected output while plugin activation
352 - $post_id = @wp_insert_post( array(
429 + $post_id = @wp_insert_post( [
353 430 'post_content' => $post['post_content'],
354 431 'post_title' => $post['post_title'],
355 432 'post_status' => 'publish',
356 433 'post_type' => WINP_SNIPPETS_POST_TYPE
357 - ) );
358 -
434 + ] );
435 +
359 436 if ( ! is_wp_error( $post_id ) ) {
360 - if ( isset( $post['meta']['code'] ) ) {
361 - WINP_Helper::updateMetaOption( $post_id, 'snippet_code', $post['meta']['code'] );
362 - }
363 -
364 437 if ( isset( $post['meta']['type'] ) ) {
365 438 WINP_Helper::updateMetaOption( $post_id, 'snippet_type', $post['meta']['type'] );
366 -
439 +
367 440 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_PHP ) {
368 441 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'evrywhere' );
369 442 }
370 443 if ( $post['meta']['type'] == WINP_SNIPPET_TYPE_TEXT ) {
@@ -374,31 +447,32 @@
374 447 WINP_Helper::updateMetaOption( $post_id, 'snippet_scope', 'auto' );
375 448 WINP_Helper::updateMetaOption( $post_id, 'snippet_location', 'header' );
376 449 }
377 450 }
378 -
451 +
379 452 if ( isset( $post['meta']['description'] ) ) {
380 453 WINP_Helper::updateMetaOption( $post_id, 'snippet_description', $post['meta']['description'] );
381 454 }
382 -
455 +
383 456 if ( isset( $post['meta']['filters'] ) && is_serialized( $post['meta']['filters'] ) ) {
384 457 $unserialized_filters = unserialize( $post['meta']['filters'] );
385 458 WINP_Helper::updateMetaOption( $post_id, 'snippet_filters', $unserialized_filters );
386 459 WINP_Helper::updateMetaOption( $post_id, 'changed_filters', 1 );
387 460 }
388 -
461 +
389 462 if ( isset( $post['meta']['tags'] ) && ! empty( $post['meta']['tags'] ) ) {
390 463 if ( ! taxonomy_exists( WINP_SNIPPETS_TAXONOMY ) ) {
391 - register_taxonomy( WINP_SNIPPETS_TAXONOMY, WINP_SNIPPETS_POST_TYPE, array() );
464 + register_taxonomy( WINP_SNIPPETS_TAXONOMY, WINP_SNIPPETS_POST_TYPE, [] );
392 465 }
393 -
466 +
394 467 wp_set_post_terms( $post_id, $post['meta']['tags'], WINP_SNIPPETS_TAXONOMY, true );
395 468 }
396 469 }
397 470 }
398 - WINP_Plugin::app()->updatePopulateOption( 'demo_snippets_created', 1 );
471 +
472 + update_option( WINP_Plugin::app()->getOptionName( 'demo_snippets_created' ), 1 );
399 473 }
400 -
474 +
401 475 /**
402 476 * Returns an example php snippet content
403 477 *
404 478 * @return string
@@ -426,12 +500,12 @@
426 500 $output .= "function ace_disable_emoji_tinymce( \$plugins ) {" . PHP_EOL;
427 501 $output .= "\tunset( \$plugins['wpemoji'] );" . PHP_EOL;
428 502 $output .= "\treturn \$plugins;" . PHP_EOL;
429 503 $output .= "}" . PHP_EOL;
430 -
504 +
431 505 return $output;
432 506 }
433 -
507 +
434 508 /**
435 509 * Returns an example of the content of a text snippet.
436 510 *
437 511 * @return string
@@ -438,12 +512,12 @@
438 512 */
439 513 protected static function get_simple_text_snippet() {
440 514 $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.';
441 515 $output .= PHP_EOL . "{{SNIPPET_CONTENT}}" . PHP_EOL;
442 -
516 +
443 517 return $output;
444 518 }
445 -
519 +
446 520 /**
447 521 * Returns an example of the content of a universal snippet.
448 522 *
449 523 * @return string
@@ -457,9 +531,150 @@
457 531 $output .= "\tgtag('js', new Date());" . PHP_EOL;
458 532 $output .= "\tgtag('config', 'GA_TRACKING_ID');" . PHP_EOL;
459 533 $output .= "</script>" . PHP_EOL;
460 534 $output .= "<!-- End global Site Tag (gtag.js) - Google Analytics -->" . PHP_EOL;
461 -
535 +
462 536 return $output;
463 537 }
464 -
465 -}
538 +
539 + /**
540 + * Wrapper for register shortcode
541 + *
542 + * @param $name
543 + * @param $obj
544 + */
545 + public static function register_shortcode( $name, $obj ) {
546 + Wbcr_FactoryShortcodes329::register( $name, $obj );
547 + }
548 +
549 + /**
550 + * Wrapper for get factory class for container
551 + *
552 + * @return string
553 + */
554 + public static function get_factory_class() {
555 + return "factory-bootstrap-420 factory-fontawesome-000";
556 + }
557 +
558 + /**
559 + * Wrapper for Wbcr_FactoryForms417_OptionsValueProvider object
560 + *
561 + * @param $plugin
562 + *
563 + * @return Wbcr_FactoryForms417_OptionsValueProvider
564 + */
565 + public static function get_options_value_provider( $plugin ) {
566 + return new Wbcr_FactoryForms417_OptionsValueProvider( $plugin );
567 + }
568 +
569 + /**
570 + * Wrapper for get factory form object
571 + *
572 + * @param $options
573 + * @param $plugin
574 + *
575 + * @return Wbcr_FactoryForms417_Form
576 + */
577 + public static function get_factory_form( $options, $plugin ) {
578 + return new Wbcr_FactoryForms417_Form( $options, $plugin );
579 + }
580 +
581 + /**
582 + * Wrapper for register factory metaboxes
583 + *
584 + * @param $class_name_or_object
585 + * @param $post_type
586 + * @param $plugin
587 + */
588 + public static function register_factory_metaboxes( $class_name_or_object, $post_type, $plugin ) {
589 + Wbcr_FactoryMetaboxes409::registerFor( $class_name_or_object, $post_type, $plugin );
590 + }
591 +
592 + /**
593 + * Render html for purchase button
594 + */
595 + public static function get_purchase_button( $utm_tracking_location = 'snippet-library-page' ) {
596 + $price_url = WINP_Plugin::app()->get_support()->get_pricing_url( true, $utm_tracking_location );
597 + $price = WINP_Plugin::app()->premium->get_price();
598 + $price = empty( $price ) ? 19 : $price;
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 echo __( 'Purchase premium for', 'insert-php' ) . ' $' . esc_attr( $price ); ?></span>
603 + </a>
604 + </p>
605 + <?php
606 + }
607 +
608 + /**
609 + * Check if current user is admin or editor
610 + * todo: удалить этот метод, потому что он дублирует функционал WINP_Plugin::app()->currentUserCan
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 + } else if ( has_action( 'cachify_flush_cache' ) ) {
632 + do_action( 'cachify_flush_cache' );
633 + } else if ( function_exists( 'w3tc_pgcache_flush' ) ) {
634 + w3tc_pgcache_flush();
635 + } else if ( function_exists( 'wp_fast_cache_bulk_delete_all' ) ) {
636 + wp_fast_cache_bulk_delete_all();
637 + } else if ( class_exists( 'WpFastestCache' ) ) {
638 + $wpfc = new WpFastestCache();
639 + $wpfc->deleteCache();
640 + } else if ( class_exists( 'c_ws_plugin__qcache_purging_routines' ) ) {
641 + c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
642 + } else if ( class_exists( 'zencache' ) ) {
643 + zencache::clear();
644 + } else if ( class_exists( 'comet_cache' ) ) {
645 + comet_cache::clear();
646 + } else if ( 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 + } else if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
666 + sg_cachepress_purge_cache();
667 + } else if ( 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 +}