PluginProbe
Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder / 1.5.1
Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder v1.5.1
3.6.0 3.5.0 trunk 1.0.10 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.6.1 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5 1.5.1 All 110 releases
buttonizer-multifunctional-button / freemius / includes / fs-core-functions.php

fs-core-functions.php in Buttonizer – Floating Menus, Sticky Buttons, & Popup Builder 1.5.1, at freemius/includes/fs-core-functions.php

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