PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.2
Tiered Pricing Table for WooCommerce v2.2.2
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.2, at freemius/includes/fs-core-functions.php

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