PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.3
Tiered Pricing Table for WooCommerce v2.2.3
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / freemius / includes / fs-core-functions.php

fs-core-functions.php in Tiered Pricing Table for WooCommerce 2.2.3, at freemius/includes/fs-core-functions.php

1,351 lines 45.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Freemius
4 * @copyright Copyright (c) 2015, Freemius, Inc.
5 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
6 * @since 1.0.3
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 if ( ! function_exists( 'fs_dummy' ) ) {
14 function fs_dummy() {
15 }
16 }
17
18 /* Url.
19 --------------------------------------------------------------------------------------------*/
20 if ( ! function_exists( 'fs_get_url_daily_cache_killer' ) ) {
21 function fs_get_url_daily_cache_killer() {
22 return date( '\YY\Mm\Dd' );
23 }
24 }
25
26 /* Templates / Views.
27 --------------------------------------------------------------------------------------------*/
28 if ( ! function_exists( 'fs_get_template_path' ) ) {
29 function fs_get_template_path( $path ) {
30 return WP_FS__DIR_TEMPLATES . '/' . trim( $path, '/' );
31 }
32
33 function fs_include_template( $path, &$params = null ) {
34 $VARS = &$params;
35 include fs_get_template_path( $path );
36 }
37
38 function fs_include_once_template( $path, &$params = null ) {
39 $VARS = &$params;
40 include_once fs_get_template_path( $path );
41 }
42
43 function fs_require_template( $path, &$params = null ) {
44 $VARS = &$params;
45 require fs_get_template_path( $path );
46 }
47
48 function fs_require_once_template( $path, &$params = null ) {
49 $VARS = &$params;
50 require_once fs_get_template_path( $path );
51 }
52
53 function fs_get_template( $path, &$params = null ) {
54 ob_start();
55
56 $VARS = &$params;
57 require fs_get_template_path( $path );
58
59 return ob_get_clean();
60 }
61 }
62
63 /* Scripts and styles including.
64 --------------------------------------------------------------------------------------------*/
65
66 /**
67 * Generates an absolute URL to the given path. This function ensures that the URL will be correct whether the asset
68 * is inside a plugin's folder or a theme's folder.
69 *
70 * Examples:
71 * 1. "themes" folder
72 * Path: C:/xampp/htdocs/fswp/wp-content/themes/twentytwelve/freemius/assets/css/admin/common.css
73 * URL: http://fswp:8080/wp-content/themes/twentytwelve/freemius/assets/css/admin/common.css
74 *
75 * 2. "plugins" folder
76 * Path: C:/xampp/htdocs/fswp/wp-content/plugins/rating-widget-premium/freemius/assets/css/admin/common.css
77 * URL: http://fswp:8080/wp-content/plugins/rating-widget-premium/freemius/assets/css/admin/common.css
78 *
79 * @author Leo Fajardo (@leorw)
80 * @since 1.2.2
81 *
82 * @param string $asset_abs_path Asset's absolute path.
83 *
84 * @return string Asset's URL.
85 */
86 function fs_asset_url( $asset_abs_path ) {
87 $wp_content_dir = fs_normalize_path( WP_CONTENT_DIR );
88 $asset_abs_path = fs_normalize_path( $asset_abs_path );
89
90 if ( 0 === strpos( $asset_abs_path, $wp_content_dir ) ) {
91 // Handle both theme and plugin assets located in the standard directories.
92 $asset_rel_path = str_replace( $wp_content_dir, '', $asset_abs_path );
93 $asset_url = content_url( fs_normalize_path( $asset_rel_path ) );
94 } else {
95 $wp_plugins_dir = fs_normalize_path( WP_PLUGIN_DIR );
96 if ( 0 === strpos( $asset_abs_path, $wp_plugins_dir ) ) {
97 // Try to handle plugin assets that may be located in a non-standard plugins directory.
98 $asset_rel_path = str_replace( $wp_plugins_dir, '', $asset_abs_path );
99 $asset_url = plugins_url( fs_normalize_path( $asset_rel_path ) );
100 } else {
101 // Try to handle theme assets that may be located in a non-standard themes directory.
102 $active_theme_stylesheet = get_stylesheet();
103 $wp_themes_dir = fs_normalize_path( trailingslashit( get_theme_root( $active_theme_stylesheet ) ) );
104 $asset_rel_path = str_replace( $wp_themes_dir, '', fs_normalize_path( $asset_abs_path ) );
105 $asset_url = trailingslashit( get_theme_root_uri( $active_theme_stylesheet ) ) . fs_normalize_path( $asset_rel_path );
106 }
107 }
108
109 return $asset_url;
110 }
111
112 function fs_enqueue_local_style( $handle, $path, $deps = array(), $ver = false, $media = 'all' ) {
113 wp_enqueue_style( $handle, fs_asset_url( WP_FS__DIR_CSS . '/' . trim( $path, '/' ) ), $deps, $ver, $media );
114 }
115
116 function fs_enqueue_local_script( $handle, $path, $deps = array(), $ver = false, $in_footer = 'all' ) {
117 wp_enqueue_script( $handle, fs_asset_url( WP_FS__DIR_JS . '/' . trim( $path, '/' ) ), $deps, $ver, $in_footer );
118 }
119
120 function fs_img_url( $path, $img_dir = WP_FS__DIR_IMG ) {
121 return ( fs_asset_url( $img_dir . '/' . trim( $path, '/' ) ) );
122 }
123
124 #--------------------------------------------------------------------------------
125 #region Request handlers.
126 #--------------------------------------------------------------------------------
127
128 if ( ! function_exists( 'fs_request_get' ) ) {
129 /**
130 * A helper method to fetch GET/POST user input with an optional default value when the input is not set.
131 * @author Vova Feldman (@svovaf)
132 *
133 * @param string $key
134 * @param mixed $def
135 * @param string|bool $type Since 1.2.1.7 - when set to 'get' will look for the value passed via querystring, when
136 * set to 'post' will look for the value passed via the POST request's body, otherwise,
137 * will check if the parameter was passed in any of the two.
138 *
139 * @return mixed
140 */
141 function fs_request_get( $key, $def = false, $type = false ) {
142 if ( is_string( $type ) ) {
143 $type = strtolower( $type );
144 }
145
146 /**
147 * Note to WordPress.org Reviewers:
148 * This is a helper method to fetch GET/POST user input with an optional default value when the input is not set. The actual sanitization is done in the scope of the function's usage.
149 */
150 switch ( $type ) {
151 case 'post':
152 $value = isset( $_POST[ $key ] ) ? $_POST[ $key ] : $def;
153 break;
154 case 'get':
155 $value = isset( $_GET[ $key ] ) ? $_GET[ $key ] : $def;
156 break;
157 default:
158 $value = isset( $_REQUEST[ $key ] ) ? $_REQUEST[ $key ] : $def;
159 break;
160 }
161
162 return $value;
163 }
164 }
165
166 if ( ! function_exists( 'fs_request_has' ) ) {
167 function fs_request_has( $key ) {
168 return isset( $_REQUEST[ $key ] );
169 }
170 }
171
172 if ( ! function_exists( 'fs_request_get_bool' ) ) {
173 /**
174 * A helper method to fetch GET/POST user boolean input with an optional default value when the input is not set.
175 *
176 * @author Vova Feldman (@svovaf)
177 *
178 * @param string $key
179 * @param bool $def
180 *
181 * @return bool|mixed
182 */
183 function fs_request_get_bool( $key, $def = false ) {
184 $val = fs_request_get( $key, null );
185
186 if ( is_null( $val ) ) {
187 return $def;
188 }
189
190 if ( is_bool( $val ) ) {
191 return $val;
192 } else if ( is_numeric( $val ) ) {
193 if ( 1 == $val ) {
194 return true;
195 } else if ( 0 == $val ) {
196 return false;
197 }
198 } else if ( is_string( $val ) ) {
199 $val = strtolower( $val );
200
201 if ( 'true' === $val ) {
202 return true;
203 } else if ( 'false' === $val ) {
204 return false;
205 }
206 }
207
208 return $def;
209 }
210 }
211
212 if ( ! function_exists( 'fs_request_is_post' ) ) {
213 function fs_request_is_post() {
214 return ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) );
215 }
216 }
217
218 if ( ! function_exists( 'fs_request_is_get' ) ) {
219 function fs_request_is_get() {
220 return ( 'get' === strtolower( $_SERVER['REQUEST_METHOD'] ) );
221 }
222 }
223
224 if ( ! function_exists( 'fs_get_action' ) ) {
225 function fs_get_action( $action_key = 'action' ) {
226 if ( ! empty( $_REQUEST[ $action_key ] ) && is_string( $_REQUEST[ $action_key ] ) ) {
227 return strtolower( $_REQUEST[ $action_key ] );
228 }
229
230 if ( 'action' == $action_key ) {
231 $action_key = 'fs_action';
232
233 if ( ! empty( $_REQUEST[ $action_key ] ) && is_string( $_REQUEST[ $action_key ] ) ) {
234 return strtolower( $_REQUEST[ $action_key ] );
235 }
236 }
237
238 return false;
239 }
240 }
241
242 if ( ! function_exists( 'fs_request_is_action' ) ) {
243 function fs_request_is_action( $action, $action_key = 'action' ) {
244 return ( strtolower( $action ) === fs_get_action( $action_key ) );
245 }
246 }
247
248 if ( ! function_exists( 'fs_request_is_action_secure' ) ) {
249 /**
250 * @author Vova Feldman (@svovaf)
251 * @since 1.0.0
252 *
253 * @since 1.2.1.5 Allow nonce verification.
254 *
255 * @param string $action
256 * @param string $action_key
257 * @param string $nonce_key
258 *
259 * @return bool
260 */
261 function fs_request_is_action_secure(
262 $action,
263 $action_key = 'action',
264 $nonce_key = 'nonce'
265 ) {
266 if ( strtolower( $action ) !== fs_get_action( $action_key ) ) {
267 return false;
268 }
269
270 $nonce = ! empty( $_REQUEST[ $nonce_key ] ) ?
271 $_REQUEST[ $nonce_key ] :
272 '';
273
274 if ( empty( $nonce ) ||
275 ( false === wp_verify_nonce( $nonce, $action ) )
276 ) {
277 return false;
278 }
279
280 return true;
281 }
282 }
283
284 #endregion
285
286 if ( ! function_exists( 'fs_is_plugin_page' ) ) {
287 function fs_is_plugin_page( $page_slug ) {
288 return ( is_admin() && $page_slug === fs_request_get( 'page' ) );
289 }
290 }
291
292 if ( ! function_exists( 'fs_get_raw_referer' ) ) {
293 /**
294 * Retrieves unvalidated referer from '_wp_http_referer' or HTTP referer.
295 *
296 * Do not use for redirects, use {@see wp_get_referer()} instead.
297 *
298 * @since 1.2.3
299 *
300 * @return string|false Referer URL on success, false on failure.
301 */
302 function fs_get_raw_referer() {
303 if ( function_exists( 'wp_get_raw_referer' ) ) {
304 return wp_get_raw_referer();
305 }
306 if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
307 return wp_unslash( $_REQUEST['_wp_http_referer'] );
308 } else if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
309 return wp_unslash( $_SERVER['HTTP_REFERER'] );
310 }
311
312 return false;
313 }
314 }
315
316 /* Core UI.
317 --------------------------------------------------------------------------------------------*/
318 /**
319 * @param number $module_id
320 * @param string $page
321 * @param string $action
322 * @param string $title
323 * @param string $button_class
324 * @param array $params
325 * @param bool $is_primary
326 * @param bool $is_small
327 * @param string|bool $icon_class Optional class for an icon (since 1.1.7).
328 * @param string|bool $confirmation Optional confirmation message before submit (since 1.1.7).
329 * @param string $method Since 1.1.7
330 *
331 * @uses fs_ui_get_action_button()
332 */
333 function fs_ui_action_button(
334 $module_id,
335 $page,
336 $action,
337 $title,
338 $button_class = '',
339 $params = array(),
340 $is_primary = true,
341 $is_small = false,
342 $icon_class = false,
343 $confirmation = false,
344 $method = 'GET'
345 ) {
346 echo fs_ui_get_action_button(
347 $module_id,
348 $page,
349 $action,
350 $title,
351 $button_class,
352 $params,
353 $is_primary,
354 $is_small,
355 $icon_class,
356 $confirmation,
357 $method
358 );
359 }
360
361 /**
362 * @author Vova Feldman (@svovaf)
363 * @since 1.1.7
364 *
365 * @param number $module_id
366 * @param string $page
367 * @param string $action
368 * @param string $title
369 * @param string $button_class
370 * @param array $params
371 * @param bool $is_primary
372 * @param bool $is_small
373 * @param string|bool $icon_class Optional class for an icon.
374 * @param string|bool $confirmation Optional confirmation message before submit.
375 * @param string $method
376 *
377 * @return string
378 */
379 function fs_ui_get_action_button(
380 $module_id,
381 $page,
382 $action,
383 $title,
384 $button_class = '',
385 $params = array(),
386 $is_primary = true,
387 $is_small = false,
388 $icon_class = false,
389 $confirmation = false,
390 $method = 'GET'
391 ) {
392 // Prepend icon (if set).
393 $title = ( is_string( $icon_class ) ? '<i class="' . $icon_class . '"></i> ' : '' ) . $title;
394
395 if ( is_string( $confirmation ) ) {
396 return sprintf( '<form action="%s" method="%s"><input type="hidden" name="fs_action" value="%s">%s<a href="#" class="%s" onclick="if (confirm(\'%s\')) this.parentNode.submit(); return false;">%s</a></form>',
397 freemius( $module_id )->_get_admin_page_url( $page, $params ),
398 $method,
399 $action,
400 wp_nonce_field( $action, '_wpnonce', true, false ),
401 'button' . ( ! empty( $button_class ) ? ' ' . $button_class : '' ) . ( $is_primary ? ' button-primary' : '' ) . ( $is_small ? ' button-small' : '' ),
402 $confirmation,
403 $title
404 );
405 } else if ( 'GET' !== strtoupper( $method ) ) {
406 return sprintf( '<form action="%s" method="%s"><input type="hidden" name="fs_action" value="%s">%s<a href="#" class="%s" onclick="this.parentNode.submit(); return false;">%s</a></form>',
407 freemius( $module_id )->_get_admin_page_url( $page, $params ),
408 $method,
409 $action,
410 wp_nonce_field( $action, '_wpnonce', true, false ),
411 'button' . ( ! empty( $button_class ) ? ' ' . $button_class : '' ) . ( $is_primary ? ' button-primary' : '' ) . ( $is_small ? ' button-small' : '' ),
412 $title
413 );
414 } else {
415 return sprintf( '<a href="%s" class="%s">%s</a></form>',
416 wp_nonce_url( freemius( $module_id )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ),
417 'button' . ( ! empty( $button_class ) ? ' ' . $button_class : '' ) . ( $is_primary ? ' button-primary' : '' ) . ( $is_small ? ' button-small' : '' ),
418 $title
419 );
420 }
421 }
422
423 function fs_ui_action_link( $module_id, $page, $action, $title, $params = array() ) {
424 ?><a class=""
425 href="<?php echo wp_nonce_url( freemius( $module_id )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ) ?>"><?php echo $title ?></a><?php
426 }
427
428 /*function fs_error_handler($errno, $errstr, $errfile, $errline)
429 {
430 if (false === strpos($errfile, 'freemius/'))
431 {
432 // @todo Dump Freemius errors to local log.
433 }
434
435 // switch ($errno) {
436 // case E_USER_ERROR:
437 // break;
438 // case E_WARNING:
439 // case E_USER_WARNING:
440 // break;
441 // case E_NOTICE:
442 // case E_USER_NOTICE:
443 // break;
444 // default:
445 // break;
446 // }
447 }
448
449 set_error_handler('fs_error_handler');*/
450
451 if ( ! function_exists( 'fs_nonce_url' ) ) {
452 /**
453 * Retrieve URL with nonce added to URL query.
454 *
455 * Originally was using `wp_nonce_url()` but the new version
456 * changed the return value to escaped URL, that's not the expected
457 * behaviour.
458 *
459 * @author Vova Feldman (@svovaf)
460 * @since ~1.1.3
461 *
462 * @param string $actionurl URL to add nonce action.
463 * @param int|string $action Optional. Nonce action name. Default -1.
464 * @param string $name Optional. Nonce name. Default '_wpnonce'.
465 *
466 * @return string Escaped URL with nonce action added.
467 */
468 function fs_nonce_url( $actionurl, $action = - 1, $name = '_wpnonce' ) {
469 return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
470 }
471 }
472
473 if ( ! function_exists( 'fs_starts_with' ) ) {
474 /**
475 * Check if string starts with.
476 *
477 * @author Vova Feldman (@svovaf)
478 * @since 1.1.3
479 *
480 * @param string $haystack
481 * @param string $needle
482 *
483 * @return bool
484 */
485 function fs_starts_with( $haystack, $needle ) {
486 $length = strlen( $needle );
487
488 return ( substr( $haystack, 0, $length ) === $needle );
489 }
490 }
491
492 if ( ! function_exists( 'fs_ends_with' ) ) {
493 /**
494 * Check if string ends with.
495 *
496 * @author Vova Feldman (@svovaf)
497 * @since 2.0.0
498 *
499 * @param string $haystack
500 * @param string $needle
501 *
502 * @return bool
503 */
504 function fs_ends_with( $haystack, $needle ) {
505 $length = strlen( $needle );
506 $start = $length * - 1; // negative
507
508 return ( substr( $haystack, $start ) === $needle );
509 }
510 }
511
512 if ( ! function_exists( 'fs_strip_url_protocol' ) ) {
513 function fs_strip_url_protocol( $url ) {
514 if ( ! fs_starts_with( $url, 'http' ) ) {
515 return $url;
516 }
517
518 $protocol_pos = strpos( $url, '://' );
519
520 if ( $protocol_pos > 5 ) {
521 return $url;
522 }
523
524 return substr( $url, $protocol_pos + 3 );
525 }
526 }
527
528 #region Url Canonization ------------------------------------------------------------------
529
530 if ( ! function_exists( 'fs_canonize_url' ) ) {
531 /**
532 * @author Vova Feldman (@svovaf)
533 * @since 1.1.3
534 *
535 * @param string $url
536 * @param bool $omit_host
537 * @param array $ignore_params
538 *
539 * @return string
540 */
541 function fs_canonize_url( $url, $omit_host = false, $ignore_params = array() ) {
542 $parsed_url = parse_url( strtolower( $url ) );
543
544 // if ( ! isset( $parsed_url['host'] ) ) {
545 // return $url;
546 // }
547
548 $canonical = ( ( $omit_host || ! isset( $parsed_url['host'] ) ) ? '' : $parsed_url['host'] ) . $parsed_url['path'];
549
550 if ( isset( $parsed_url['query'] ) ) {
551 parse_str( $parsed_url['query'], $queryString );
552 $canonical .= '?' . fs_canonize_query_string( $queryString, $ignore_params );
553 }
554
555 return $canonical;
556 }
557 }
558
559 if ( ! function_exists( 'fs_canonize_query_string' ) ) {
560 /**
561 * @author Vova Feldman (@svovaf)
562 * @since 1.1.3
563 *
564 * @param array $params
565 * @param array $ignore_params
566 * @param bool $params_prefix
567 *
568 * @return string
569 */
570 function fs_canonize_query_string( array $params, array &$ignore_params, $params_prefix = false ) {
571 if ( ! is_array( $params ) || 0 === count( $params ) ) {
572 return '';
573 }
574
575 // Url encode both keys and values
576 $keys = fs_urlencode_rfc3986( array_keys( $params ) );
577 $values = fs_urlencode_rfc3986( array_values( $params ) );
578 $params = array_combine( $keys, $values );
579
580 // Parameters are sorted by name, using lexicographical byte value ordering.
581 // Ref: Spec: 9.1.1 (1)
582 uksort( $params, 'strcmp' );
583
584 $pairs = array();
585 foreach ( $params as $parameter => $value ) {
586 $lower_param = strtolower( $parameter );
587
588 // Skip ignore params.
589 if ( in_array( $lower_param, $ignore_params ) ||
590 ( false !== $params_prefix && fs_starts_with( $lower_param, $params_prefix ) )
591 ) {
592 continue;
593 }
594
595 if ( is_array( $value ) ) {
596 // If two or more parameters share the same name, they are sorted by their value
597 // Ref: Spec: 9.1.1 (1)
598 natsort( $value );
599 foreach ( $value as $duplicate_value ) {
600 $pairs[] = $lower_param . '=' . $duplicate_value;
601 }
602 } else {
603 $pairs[] = $lower_param . '=' . $value;
604 }
605 }
606
607 if ( 0 === count( $pairs ) ) {
608 return '';
609 }
610
611 return implode( "&", $pairs );
612 }
613 }
614
615 if ( ! function_exists( 'fs_urlencode_rfc3986' ) ) {
616 /**
617 * @author Vova Feldman (@svovaf)
618 * @since 1.1.3
619 *
620 * @param string|string[] $input
621 *
622 * @return array|mixed|string
623 */
624 function fs_urlencode_rfc3986( $input ) {
625 if ( is_array( $input ) ) {
626 return array_map( 'fs_urlencode_rfc3986', $input );
627 } else if ( is_scalar( $input ) ) {
628 return str_replace( '+', ' ', str_replace( '%7E', '~', rawurlencode( $input ) ) );
629 }
630
631 return '';
632 }
633 }
634
635 #endregion Url Canonization ------------------------------------------------------------------
636
637 /**
638 * @author Vova Feldman (@svovaf)
639 *
640 * @since 1.2.2 Changed to usage of WP_Filesystem_Direct.
641 *
642 * @param string $from URL
643 * @param string $to File path.
644 *
645 * @return bool Is successfully downloaded.
646 */
647 function fs_download_image( $from, $to ) {
648 $dir = dirname( $to );
649
650 if ( 'direct' !== get_filesystem_method( array(), $dir ) ) {
651 return false;
652 }
653
654 if ( ! class_exists( 'WP_Filesystem_Direct' ) ) {
655 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
656 require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
657 }
658
659 $fs = new WP_Filesystem_Direct( '' );
660 $tmpfile = download_url( $from );
661
662 if ( $tmpfile instanceof WP_Error ) {
663 // Issue downloading the file.
664 return false;
665 }
666
667 $fs->copy( $tmpfile, $to );
668 $fs->delete( $tmpfile );
669
670 return true;
671 }
672
673 /* General Utilities
674 --------------------------------------------------------------------------------------------*/
675
676 /**
677 * Sorts an array by the value of the priority key.
678 *
679 * @author Daniel Iser (@danieliser)
680 * @since 1.1.7
681 *
682 * @param $a
683 * @param $b
684 *
685 * @return int
686 */
687 function fs_sort_by_priority( $a, $b ) {
688
689 // If b has a priority and a does not, b wins.
690 if ( ! isset( $a['priority'] ) && isset( $b['priority'] ) ) {
691 return 1;
692 } // If b has a priority and a does not, b wins.
693 elseif ( isset( $a['priority'] ) && ! isset( $b['priority'] ) ) {
694 return - 1;
695 } // If neither has a priority or both priorities are equal its a tie.
696 elseif ( ( ! isset( $a['priority'] ) && ! isset( $b['priority'] ) ) || $a['priority'] === $b['priority'] ) {
697 return 0;
698 }
699
700 // If both have priority return the winner.
701 return ( $a['priority'] < $b['priority'] ) ? - 1 : 1;
702 }
703
704 #--------------------------------------------------------------------------------
705 #region Localization
706 #--------------------------------------------------------------------------------
707
708 if ( ! function_exists( 'fs_text' ) ) {
709 /**
710 * Retrieve a translated text by key.
711 *
712 * @author Vova Feldman (@svovaf)
713 * @since 1.2.1.7
714 *
715 * @param string $key
716 * @param string $slug
717 *
718 * @return string
719 *
720 * @global $fs_text , $fs_text_overrides
721 */
722 function fs_text( $key, $slug = 'freemius' ) {
723 global $fs_text,
724 $fs_module_info_text,
725 $fs_text_overrides;
726
727 if ( isset( $fs_text_overrides[ $slug ] ) ) {
728 if ( isset( $fs_text_overrides[ $slug ][ $key ] ) ) {
729 return $fs_text_overrides[ $slug ][ $key ];
730 }
731
732 $lower_key = strtolower( $key );
733 if ( isset( $fs_text_overrides[ $slug ][ $lower_key ] ) ) {
734 return $fs_text_overrides[ $slug ][ $lower_key ];
735 }
736 }
737
738 if ( ! isset( $fs_text ) ) {
739 $dir = defined( 'WP_FS__DIR_INCLUDES' ) ?
740 WP_FS__DIR_INCLUDES :
741 dirname( __FILE__ );
742
743 require_once $dir . '/i18n.php';
744 }
745
746 if ( isset( $fs_text[ $key ] ) ) {
747 return $fs_text[ $key ];
748 }
749
750 if ( isset( $fs_module_info_text[ $key ] ) ) {
751 return $fs_module_info_text[ $key ];
752 }
753
754 return $key;
755 }
756
757 #region Private
758
759 /**
760 * Retrieve an inline translated text by key with a context.
761 *
762 * @author Vova Feldman (@svovaf)
763 * @since 1.2.3
764 *
765 * @param string $text Translatable string.
766 * @param string $context Context information for the translators.
767 * @param string $key String key for overrides.
768 * @param string $slug Module slug for overrides.
769 *
770 * @return string
771 *
772 * @global $fs_text_overrides
773 */
774 function _fs_text_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
775 list( $text, $text_domain ) = fs_text_and_domain( $text, $key, $slug );
776
777 // Avoid misleading Theme Check warning.
778 $fn = 'translate_with_gettext_context';
779
780 return $fn( $text, $context, $text_domain );
781 }
782
783 #endregion
784
785 /**
786 * Retrieve an inline translated text by key with a context.
787 *
788 * @author Vova Feldman (@svovaf)
789 * @since 1.2.3
790 *
791 * @param string $text Translatable string.
792 * @param string $context Context information for the translators.
793 * @param string $key String key for overrides.
794 * @param string $slug Module slug for overrides.
795 *
796 * @return string
797 *
798 * @global $fs_text_overrides
799 */
800 function fs_text_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
801 return _fs_text_x_inline( $text, $context, $key, $slug );
802 }
803
804 /**
805 * Output a translated text by key.
806 *
807 * @author Vova Feldman (@svovaf)
808 * @since 1.2.1.7
809 *
810 * @param string $key
811 * @param string $slug
812 */
813 function fs_echo( $key, $slug = 'freemius' ) {
814 echo fs_text( $key, $slug );
815 }
816
817 /**
818 * Output an inline translated text.
819 *
820 * @author Vova Feldman (@svovaf)
821 * @since 1.2.3
822 *
823 * @param string $text Translatable string.
824 * @param string $key String key for overrides.
825 * @param string $slug Module slug for overrides.
826 */
827 function fs_echo_inline( $text, $key = '', $slug = 'freemius' ) {
828 echo _fs_text_inline( $text, $key, $slug );
829 }
830
831 /**
832 * Output an inline translated text with a context.
833 *
834 * @author Vova Feldman (@svovaf)
835 * @since 1.2.3
836 *
837 * @param string $text Translatable string.
838 * @param string $context Context information for the translators.
839 * @param string $key String key for overrides.
840 * @param string $slug Module slug for overrides.
841 */
842 function fs_echo_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
843 echo _fs_text_x_inline( $text, $context, $key, $slug );
844 }
845 }
846
847 if ( ! function_exists( 'fs_text_override' ) ) {
848 /**
849 * Get a translatable text override if exists, or `false`.
850 *
851 * @author Vova Feldman (@svovaf)
852 * @since 1.2.1.7
853 *
854 * @param string $text Translatable string.
855 * @param string $key String key for overrides.
856 * @param string $slug Module slug for overrides.
857 *
858 * @return string|false
859 */
860 function fs_text_override( $text, $key, $slug ) {
861 global $fs_text_overrides;
862
863 /**
864 * Check if string is overridden.
865 */
866 if ( ! isset( $fs_text_overrides[ $slug ] ) ) {
867 return false;
868 }
869
870 if ( empty( $key ) ) {
871 $key = strtolower( str_replace( ' ', '-', $text ) );
872 }
873
874 if ( isset( $fs_text_overrides[ $slug ][ $key ] ) ) {
875 return $fs_text_overrides[ $slug ][ $key ];
876 }
877
878 $lower_key = strtolower( $key );
879 if ( isset( $fs_text_overrides[ $slug ][ $lower_key ] ) ) {
880 return $fs_text_overrides[ $slug ][ $lower_key ];
881 }
882
883 return false;
884 }
885 }
886
887 if ( ! function_exists( 'fs_text_and_domain' ) ) {
888 /**
889 * Get a translatable text and its text domain.
890 *
891 * When the text is overridden by the module, returns the overridden text and the text domain of the module. Otherwise, returns the original text and 'freemius' as the text domain.
892 *
893 * @author Vova Feldman (@svovaf)
894 * @since 1.2.1.7
895 *
896 * @param string $text Translatable string.
897 * @param string $key String key for overrides.
898 * @param string $slug Module slug for overrides.
899 *
900 * @return string[]
901 */
902 function fs_text_and_domain( $text, $key, $slug ) {
903 $override = fs_text_override( $text, $key, $slug );
904
905 if ( false === $override ) {
906 // No override, use FS text domain.
907 $text_domain = 'freemius';
908 } else {
909 // Found an override.
910 $text = $override;
911 // Use the module's text domain.
912 $text_domain = $slug;
913 }
914
915 return array( $text, $text_domain );
916 }
917 }
918
919 if ( ! function_exists( '_fs_text_inline' ) ) {
920 /**
921 * Retrieve an inline translated text by key.
922 *
923 * @author Vova Feldman (@svovaf)
924 * @since 1.2.3
925 *
926 * @param string $text Translatable string.
927 * @param string $key String key for overrides.
928 * @param string $slug Module slug for overrides.
929 *
930 * @return string
931 *
932 * @global $fs_text_overrides
933 */
934 function _fs_text_inline( $text, $key = '', $slug = 'freemius' ) {
935 list( $text, $text_domain ) = fs_text_and_domain( $text, $key, $slug );
936
937 // Avoid misleading Theme Check warning.
938 $fn = 'translate';
939
940 return $fn( $text, $text_domain );
941 }
942 }
943
944 if ( ! function_exists( 'fs_text_inline' ) ) {
945 /**
946 * Retrieve an inline translated text by key.
947 *
948 * @author Vova Feldman (@svovaf)
949 * @since 1.2.3
950 *
951 * @param string $text Translatable string.
952 * @param string $key String key for overrides.
953 * @param string $slug Module slug for overrides.
954 *
955 * @return string
956 *
957 * @global $fs_text_overrides
958 */
959 function fs_text_inline( $text, $key = '', $slug = 'freemius' ) {
960 return _fs_text_inline( $text, $key, $slug );
961 }
962 }
963
964 if ( ! function_exists( 'fs_esc_attr' ) ) {
965 /**
966 * @author Vova Feldman
967 * @since 1.2.1.6
968 *
969 * @param string $key
970 * @param string $slug
971 *
972 * @return string
973 */
974 function fs_esc_attr( $key, $slug ) {
975 return esc_attr( fs_text( $key, $slug ) );
976 }
977 }
978
979 if ( ! function_exists( 'fs_esc_attr_inline' ) ) {
980 /**
981 * @author Vova Feldman (@svovaf)
982 * @since 1.2.3
983 *
984 * @param string $text Translatable string.
985 * @param string $key String key for overrides.
986 * @param string $slug Module slug for overrides.
987 *
988 * @return string
989 */
990 function fs_esc_attr_inline( $text, $key = '', $slug = 'freemius' ) {
991 return esc_attr( _fs_text_inline( $text, $key, $slug ) );
992 }
993 }
994
995 if ( ! function_exists( 'fs_esc_attr_x_inline' ) ) {
996 /**
997 * @author Vova Feldman (@svovaf)
998 * @since 1.2.3
999 *
1000 * @param string $text Translatable string.
1001 * @param string $context Context information for the translators.
1002 * @param string $key String key for overrides.
1003 * @param string $slug Module slug for overrides.
1004 *
1005 * @return string
1006 */
1007 function fs_esc_attr_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
1008 return esc_attr( _fs_text_x_inline( $text, $context, $key, $slug ) );
1009 }
1010 }
1011
1012 if ( ! function_exists( 'fs_esc_attr_echo' ) ) {
1013 /**
1014 * @author Vova Feldman
1015 * @since 1.2.1.6
1016 *
1017 * @param string $key
1018 * @param string $slug
1019 */
1020 function fs_esc_attr_echo( $key, $slug ) {
1021 echo esc_attr( fs_text( $key, $slug ) );
1022 }
1023 }
1024
1025 if ( ! function_exists( 'fs_esc_attr_echo_inline' ) ) {
1026 /**
1027 * @author Vova Feldman (@svovaf)
1028 * @since 1.2.3
1029 *
1030 * @param string $text Translatable string.
1031 * @param string $key String key for overrides.
1032 * @param string $slug Module slug for overrides.
1033 */
1034 function fs_esc_attr_echo_inline( $text, $key = '', $slug = 'freemius' ) {
1035 echo esc_attr( _fs_text_inline( $text, $key, $slug ) );
1036 }
1037 }
1038
1039 if ( ! function_exists( 'fs_esc_js' ) ) {
1040 /**
1041 * @author Vova Feldman
1042 * @since 1.2.1.6
1043 *
1044 * @param string $key
1045 * @param string $slug
1046 *
1047 * @return string
1048 */
1049 function fs_esc_js( $key, $slug ) {
1050 return esc_js( fs_text( $key, $slug ) );
1051 }
1052 }
1053
1054 if ( ! function_exists( 'fs_esc_js_inline' ) ) {
1055 /**
1056 * @author Vova Feldman (@svovaf)
1057 * @since 1.2.3
1058 *
1059 * @param string $text Translatable string.
1060 * @param string $key String key for overrides.
1061 * @param string $slug Module slug for overrides.
1062 *
1063 * @return string
1064 */
1065 function fs_esc_js_inline( $text, $key = '', $slug = 'freemius' ) {
1066 return esc_js( _fs_text_inline( $text, $key, $slug ) );
1067 }
1068 }
1069
1070 if ( ! function_exists( 'fs_esc_js_x_inline' ) ) {
1071 /**
1072 * @author Vova Feldman (@svovaf)
1073 * @since 1.2.3
1074 *
1075 * @param string $text Translatable string.
1076 * @param string $context Context information for the translators.
1077 * @param string $key String key for overrides.
1078 * @param string $slug Module slug for overrides.
1079 *
1080 * @return string
1081 */
1082 function fs_esc_js_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
1083 return esc_js( _fs_text_x_inline( $text, $context, $key, $slug ) );
1084 }
1085 }
1086
1087 if ( ! function_exists( 'fs_esc_js_echo_x_inline' ) ) {
1088 /**
1089 * @author Vova Feldman (@svovaf)
1090 * @since 1.2.3
1091 *
1092 * @param string $text Translatable string.
1093 * @param string $context Context information for the translators.
1094 * @param string $key String key for overrides.
1095 * @param string $slug Module slug for overrides.
1096 *
1097 * @return string
1098 */
1099 function fs_esc_js_echo_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
1100 echo esc_js( _fs_text_x_inline( $text, $context, $key, $slug ) );
1101 }
1102 }
1103
1104 if ( ! function_exists( 'fs_esc_js_echo' ) ) {
1105 /**
1106 * @author Vova Feldman
1107 * @since 1.2.1.6
1108 *
1109 * @param string $key
1110 * @param string $slug
1111 */
1112 function fs_esc_js_echo( $key, $slug ) {
1113 echo esc_js( fs_text( $key, $slug ) );
1114 }
1115 }
1116
1117 if ( ! function_exists( 'fs_esc_js_echo_inline' ) ) {
1118 /**
1119 * @author Vova Feldman (@svovaf)
1120 * @since 1.2.3
1121 *
1122 * @param string $text Translatable string.
1123 * @param string $key String key for overrides.
1124 * @param string $slug Module slug for overrides.
1125 */
1126 function fs_esc_js_echo_inline( $text, $key = '', $slug = 'freemius' ) {
1127 echo esc_js( _fs_text_inline( $text, $key, $slug ) );
1128 }
1129 }
1130
1131 if ( ! function_exists( 'fs_json_encode_echo' ) ) {
1132 /**
1133 * @author Vova Feldman
1134 * @since 1.2.1.6
1135 *
1136 * @param string $key
1137 * @param string $slug
1138 */
1139 function fs_json_encode_echo( $key, $slug ) {
1140 echo json_encode( fs_text( $key, $slug ) );
1141 }
1142 }
1143
1144 if ( ! function_exists( 'fs_json_encode_echo_inline' ) ) {
1145 /**
1146 * @author Vova Feldman (@svovaf)
1147 * @since 1.2.3
1148 *
1149 * @param string $text Translatable string.
1150 * @param string $key String key for overrides.
1151 * @param string $slug Module slug for overrides.
1152 */
1153 function fs_json_encode_echo_inline( $text, $key = '', $slug = 'freemius' ) {
1154 echo json_encode( _fs_text_inline( $text, $key, $slug ) );
1155 }
1156 }
1157
1158 if ( ! function_exists( 'fs_esc_html' ) ) {
1159 /**
1160 * @author Vova Feldman
1161 * @since 1.2.1.6
1162 *
1163 * @param string $key
1164 * @param string $slug
1165 *
1166 * @return string
1167 */
1168 function fs_esc_html( $key, $slug ) {
1169 return esc_html( fs_text( $key, $slug ) );
1170 }
1171 }
1172
1173 if ( ! function_exists( 'fs_esc_html_inline' ) ) {
1174 /**
1175 * @author Vova Feldman (@svovaf)
1176 * @since 1.2.3
1177 *
1178 * @param string $text Translatable string.
1179 * @param string $key String key for overrides.
1180 * @param string $slug Module slug for overrides.
1181 *
1182 * @return string
1183 */
1184 function fs_esc_html_inline( $text, $key = '', $slug = 'freemius' ) {
1185 return esc_html( _fs_text_inline( $text, $key, $slug ) );
1186 }
1187 }
1188
1189 if ( ! function_exists( 'fs_esc_html_x_inline' ) ) {
1190 /**
1191 * @author Vova Feldman (@svovaf)
1192 * @since 1.2.3
1193 *
1194 * @param string $text Translatable string.
1195 * @param string $context Context information for the translators.
1196 * @param string $key String key for overrides.
1197 * @param string $slug Module slug for overrides.
1198 *
1199 * @return string
1200 */
1201 function fs_esc_html_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
1202 return esc_html( _fs_text_x_inline( $text, $context, $key, $slug ) );
1203 }
1204 }
1205
1206 if ( ! function_exists( 'fs_esc_html_echo_x_inline' ) ) {
1207 /**
1208 * @author Vova Feldman (@svovaf)
1209 * @since 1.2.3
1210 *
1211 * @param string $text Translatable string.
1212 * @param string $context Context information for the translators.
1213 * @param string $key String key for overrides.
1214 * @param string $slug Module slug for overrides.
1215 */
1216 function fs_esc_html_echo_x_inline( $text, $context, $key = '', $slug = 'freemius' ) {
1217 echo esc_html( _fs_text_x_inline( $text, $context, $key, $slug ) );
1218 }
1219 }
1220
1221 if ( ! function_exists( 'fs_esc_html_echo' ) ) {
1222 /**
1223 * @author Vova Feldman
1224 * @since 1.2.1.6
1225 *
1226 * @param string $key
1227 * @param string $slug
1228 */
1229 function fs_esc_html_echo( $key, $slug ) {
1230 echo esc_html( fs_text( $key, $slug ) );
1231 }
1232 }
1233
1234 if ( ! function_exists( 'fs_esc_html_echo_inline' ) ) {
1235 /**
1236 * @author Vova Feldman (@svovaf)
1237 * @since 1.2.3
1238 *
1239 * @param string $text Translatable string.
1240 * @param string $key String key for overrides.
1241 * @param string $slug Module slug for overrides.
1242 */
1243 function fs_esc_html_echo_inline( $text, $key = '', $slug = 'freemius' ) {
1244 echo esc_html( _fs_text_inline( $text, $key, $slug ) );
1245 }
1246 }
1247
1248 if ( ! function_exists( 'fs_override_i18n' ) ) {
1249 /**
1250 * Override default i18n text phrases.
1251 *
1252 * @author Vova Feldman (@svovaf)
1253 * @since 1.1.6
1254 *
1255 * @param array[string]string $key_value
1256 * @param string $slug
1257 *
1258 * @global $fs_text_overrides
1259 */
1260 function fs_override_i18n( array $key_value, $slug = 'freemius' ) {
1261 global $fs_text_overrides;
1262
1263 if ( ! isset( $fs_text_overrides[ $slug ] ) ) {
1264 $fs_text_overrides[ $slug ] = array();
1265 }
1266
1267 foreach ( $key_value as $key => $value ) {
1268 $fs_text_overrides[ $slug ][ $key ] = $value;
1269 }
1270 }
1271 }
1272
1273 #endregion
1274
1275 #--------------------------------------------------------------------------------
1276 #region Multisite Network
1277 #--------------------------------------------------------------------------------
1278
1279 if ( ! function_exists( 'fs_is_plugin_uninstall' ) ) {
1280 /**
1281 * @author Vova Feldman (@svovaf)
1282 * @since 2.0.0
1283 */
1284 function fs_is_plugin_uninstall() {
1285 return (
1286 defined( 'WP_UNINSTALL_PLUGIN' ) ||
1287 ( 0 < did_action( 'update_option_uninstall_plugins' ) )
1288 );
1289 }
1290 }
1291
1292 if ( ! function_exists( 'fs_is_network_admin' ) ) {
1293 /**
1294 * Unlike is_network_admin(), this one will also work properly when
1295 * the context execution is WP AJAX handler, and during plugin
1296 * uninstall.
1297 *
1298 * @author Vova Feldman (@svovaf)
1299 * @since 2.0.0
1300 */
1301 function fs_is_network_admin() {
1302 return (
1303 WP_FS__IS_NETWORK_ADMIN ||
1304 ( is_multisite() && fs_is_plugin_uninstall() )
1305 );
1306 }
1307 }
1308
1309 if ( ! function_exists( 'fs_is_blog_admin' ) ) {
1310 /**
1311 * Unlike is_blog_admin(), this one will also work properly when
1312 * the context execution is WP AJAX handler, and during plugin
1313 * uninstall.
1314 *
1315 * @author Vova Feldman (@svovaf)
1316 * @since 2.0.0
1317 */
1318 function fs_is_blog_admin() {
1319 return (
1320 WP_FS__IS_BLOG_ADMIN ||
1321 ( ! is_multisite() && fs_is_plugin_uninstall() )
1322 );
1323 }
1324 }
1325
1326 #endregion
1327
1328 if ( ! function_exists( 'fs_apply_filter' ) ) {
1329 /**
1330 * Apply filter for specific plugin.
1331 *
1332 * @author Vova Feldman (@svovaf)
1333 * @since 1.0.9
1334 *
1335 * @param string $module_unique_affix Module's unique affix.
1336 * @param string $tag The name of the filter hook.
1337 * @param mixed $value The value on which the filters hooked to `$tag` are applied on.
1338 *
1339 * @return mixed The filtered value after all hooked functions are applied to it.
1340 *
1341 * @uses apply_filters()
1342 */
1343 function fs_apply_filter( $module_unique_affix, $tag, $value ) {
1344 $args = func_get_args();
1345
1346 return call_user_func_array( 'apply_filters', array_merge(
1347 array( "fs_{$tag}_{$module_unique_affix}" ),
1348 array_slice( $args, 2 ) )
1349 );
1350 }
1351 }