is_on() ) { $fs_core_logger->info( 'content_dir = ' . $wp_content_dir ); $fs_core_logger->info( 'asset_abs_path = ' . $asset_abs_path ); $fs_core_logger->info( 'asset_rel_path = ' . $asset_rel_path ); $fs_core_logger->info( 'asset_url = ' . $asset_url ); } return $asset_url; } function fs_enqueue_local_style( $handle, $path, $deps = array(), $ver = false, $media = 'all' ) { global $fs_core_logger; if ( $fs_core_logger->is_on() ) { $fs_core_logger->info( 'handle = ' . $handle . '; path = ' . $path . ';' ); } wp_enqueue_style( $handle, fs_asset_url( WP_FS__DIR_CSS . '/' . trim( $path, '/' ) ), $deps, $ver, $media ); } function fs_enqueue_local_script( $handle, $path, $deps = array(), $ver = false, $in_footer = 'all' ) { global $fs_core_logger; if ( $fs_core_logger->is_on() ) { $fs_core_logger->info( 'handle = ' . $handle . '; path = ' . $path . ';' ); $fs_core_logger->info( 'plugin_basename = ' . plugins_url( WP_FS__DIR_JS . trim( $path, '/' ) ) ); $fs_core_logger->info( 'plugins_url = ' . plugins_url( plugin_basename( WP_FS__DIR_JS . '/' . trim( $path, '/' ) ) ) ); } wp_enqueue_script( $handle, fs_asset_url( WP_FS__DIR_JS . '/' . trim( $path, '/' ) ), $deps, $ver, $in_footer ); } function fs_img_url( $path, $img_dir = WP_FS__DIR_IMG ) { return ( fs_asset_url( $img_dir . '/' . trim( $path, '/' ) ) ); } /* Request handlers. --------------------------------------------------------------------------------------------*/ /** * @param string $key * @param mixed $def * * @return mixed */ function fs_request_get( $key, $def = false ) { return isset( $_REQUEST[ $key ] ) ? $_REQUEST[ $key ] : $def; } function fs_request_has( $key ) { return isset( $_REQUEST[ $key ] ); } function fs_request_get_bool( $key, $def = false ) { if ( ! isset( $_REQUEST[ $key ] ) ) { return $def; } if ( 1 == $_REQUEST[ $key ] || 'true' === strtolower( $_REQUEST[ $key ] ) ) { return true; } if ( 0 == $_REQUEST[ $key ] || 'false' === strtolower( $_REQUEST[ $key ] ) ) { return false; } return $def; } function fs_request_is_post() { return ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) ); } function fs_request_is_get() { return ( 'get' === strtolower( $_SERVER['REQUEST_METHOD'] ) ); } function fs_get_action( $action_key = 'action' ) { if ( ! empty( $_REQUEST[ $action_key ] ) ) { return strtolower( $_REQUEST[ $action_key ] ); } if ( 'action' == $action_key ) { $action_key = 'fs_action'; if ( ! empty( $_REQUEST[ $action_key ] ) ) { return strtolower( $_REQUEST[ $action_key ] ); } } return false; } function fs_request_is_action( $action, $action_key = 'action' ) { return ( strtolower( $action ) === fs_get_action( $action_key ) ); } /** * @author Vova Feldman (@svovaf) * @since 1.0.0 * * @since 1.2.1.5 Allow nonce verification. * * @param string $action * @param string $action_key * @param string $nonce_key * * @return bool */ function fs_request_is_action_secure( $action, $action_key = 'action', $nonce_key = 'nonce' ) { if ( strtolower( $action ) !== fs_get_action( $action_key ) ) { return false; } $nonce = ! empty( $_REQUEST[ $nonce_key ] ) ? $_REQUEST[ $nonce_key ] : ''; if ( empty( $nonce ) || ( false === wp_verify_nonce( $nonce, $action ) ) ) { return false; } return true; } function fs_is_plugin_page( $menu_slug ) { return ( is_admin() && isset( $_REQUEST['page'] ) && $_REQUEST['page'] === $menu_slug ); } /* Core UI. --------------------------------------------------------------------------------------------*/ /** * @param number $module_id * @param string $page * @param string $action * @param string $title * @param array $params * @param bool $is_primary * @param string|bool $icon_class Optional class for an icon (since 1.1.7). * @param string|bool $confirmation Optional confirmation message before submit (since 1.1.7). * @param string $method Since 1.1.7 * * @uses fs_ui_get_action_button() */ function fs_ui_action_button( $module_id, $page, $action, $title, $params = array(), $is_primary = true, $icon_class = false, $confirmation = false, $method = 'GET' ) { echo fs_ui_get_action_button( $module_id, $page, $action, $title, $params, $is_primary, $icon_class, $confirmation, $method ); } /** * @author Vova Feldman (@svovaf) * @since 1.1.7 * * @param number $module_id * @param string $page * @param string $action * @param string $title * @param array $params * @param bool $is_primary * @param string|bool $icon_class Optional class for an icon. * @param string|bool $confirmation Optional confirmation message before submit. * @param string $method * * @return string */ function fs_ui_get_action_button( $module_id, $page, $action, $title, $params = array(), $is_primary = true, $icon_class = false, $confirmation = false, $method = 'GET' ) { // Prepend icon (if set). $title = ( is_string( $icon_class ) ? ' ' : '' ) . $title; if ( is_string( $confirmation ) ) { return sprintf( '
', freemius( $module_id )->_get_admin_page_url( $page, $params ), $method, $action, wp_nonce_field( $action, '_wpnonce', true, false ), 'button' . ( $is_primary ? ' button-primary' : '' ), $confirmation, $title ); } else if ( 'GET' !== strtoupper( $method ) ) { return sprintf( '', freemius( $module_id )->_get_admin_page_url( $page, $params ), $method, $action, wp_nonce_field( $action, '_wpnonce', true, false ), 'button' . ( $is_primary ? ' button-primary' : '' ), $title ); } else { return sprintf( '%s', wp_nonce_url( freemius( $module_id )->_get_admin_page_url( $page, array_merge( $params, array( 'fs_action' => $action ) ) ), $action ), 'button' . ( $is_primary ? ' button-primary' : '' ), $title ); } } function fs_ui_action_link( $module_id, $page, $action, $title, $params = array() ) { ?> $value ) { $lower_param = strtolower( $parameter ); // Skip ignore params. if ( in_array( $lower_param, $ignore_params ) || ( false !== $params_prefix && fs_starts_with( $lower_param, $params_prefix ) ) ) { continue; } if ( is_array( $value ) ) { // If two or more parameters share the same name, they are sorted by their value // Ref: Spec: 9.1.1 (1) natsort( $value ); foreach ( $value as $duplicate_value ) { $pairs[] = $lower_param . '=' . $duplicate_value; } } else { $pairs[] = $lower_param . '=' . $value; } } if ( 0 === count( $pairs ) ) { return ''; } return implode( "&", $pairs ); } } if ( ! function_exists( 'fs_urlencode_rfc3986' ) ) { /** * @author Vova Feldman (@svovaf) * @since 1.1.3 * * @param string|string[] $input * * @return array|mixed|string */ function fs_urlencode_rfc3986( $input ) { if ( is_array( $input ) ) { return array_map( 'fs_urlencode_rfc3986', $input ); } else if ( is_scalar( $input ) ) { return str_replace( '+', ' ', str_replace( '%7E', '~', rawurlencode( $input ) ) ); } return ''; } } #endregion Url Canonization ------------------------------------------------------------------ /** * @author Vova Feldman (@svovaf) * * @since 1.2.2 Changed to usage of WP_Filesystem_Direct. * * @param string $from URL * @param string $to File path. */ function fs_download_image( $from, $to ) { $dir = dirname( $to ); if ( 'direct' !== get_filesystem_method( array(), $dir ) ) { return; } $fs = new WP_Filesystem_Direct( '' ); $tmpfile = download_url( $from ); $fs->copy( $tmpfile, $to ); $fs->delete( $tmpfile ); } /* General Utilities --------------------------------------------------------------------------------------------*/ /** * Sorts an array by the value of the priority key. * * @author Daniel Iser (@danieliser) * @since 1.1.7 * * @param $a * @param $b * * @return int */ function fs_sort_by_priority( $a, $b ) { // If b has a priority and a does not, b wins. if ( ! isset( $a['priority'] ) && isset( $b['priority'] ) ) { return 1; } // If b has a priority and a does not, b wins. elseif ( isset( $a['priority'] ) && ! isset( $b['priority'] ) ) { return - 1; } // If neither has a priority or both priorities are equal its a tie. elseif ( ( ! isset( $a['priority'] ) && ! isset( $b['priority'] ) ) || $a['priority'] === $b['priority'] ) { return 0; } // If both have priority return the winner. return ( $a['priority'] < $b['priority'] ) ? - 1 : 1; } /** * VERY IMPORTANT ---------------------------------------------- * * @todo IMPORTANT - After merging to main branch rename _efs() to fs_echo() and __fs() to fs_translate(). Otherwise, if a there's a plugin that runs version < 1.2.2 some of the translation in the plugin dialog will not be translated correctly. * * VERY IMPORTANT ---------------------------------------------- */ if ( ! function_exists( '__fs' ) ) { global $fs_text_overrides; if ( ! isset( $fs_text_overrides ) ) { $fs_text_overrides = array(); } /** * Retrieve a translated text by key. * * @author Vova Feldman (@svovaf) * @since 1.1.4 * * @param string $key * @param string $slug * * @return string * * @global $fs_text , $fs_text_overrides */ function __fs( $key, $slug = 'freemius' ) { global $fs_text, $fs_module_info_text, $fs_text_overrides; if ( isset( $fs_text_overrides[ $slug ] ) ) { if ( isset( $fs_text_overrides[ $slug ][ $key ] ) ) { return $fs_text_overrides[ $slug ][ $key ]; } $lower_key = strtolower( $key ); if ( isset( $fs_text_overrides[ $slug ][ $lower_key ] ) ) { return $fs_text_overrides[ $slug ][ $lower_key ]; } } if ( ! isset( $fs_text ) ) { $dir = defined( 'WP_FS__DIR_INCLUDES' ) ? WP_FS__DIR_INCLUDES : dirname( __FILE__ ); require_once $dir . '/i18n.php'; } if ( isset( $fs_text[ $key ] ) ) { return $fs_text[ $key ]; } if ( isset( $fs_module_info_text[ $key ] ) ) { return $fs_module_info_text[ $key ]; } return $key; } /** * Display a translated text by key. * * @author Vova Feldman (@svovaf) * @since 1.1.4 * * @param string $key * @param string $slug */ function _efs( $key, $slug = 'freemius' ) { echo __fs( $key, $slug ); } } if ( ! function_exists( 'fs_override_i18n' ) ) { /** * Override default i18n text phrases. * * @author Vova Feldman (@svovaf) * @since 1.1.6 * * @param string[] $key_value * @param string $slug * * @global $fs_text_overrides */ function fs_override_i18n( array $key_value, $slug = 'freemius' ) { global $fs_text_overrides; if ( ! isset( $fs_text_overrides[ $slug ] ) ) { $fs_text_overrides[ $slug ] = array(); } foreach ( $key_value as $key => $value ) { $fs_text_overrides[ $slug ][ $key ] = $value; } } } #-------------------------------------------------------------------------------- #region Localization #-------------------------------------------------------------------------------- /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug * * @return string */ function fs_esc_attr($key, $slug) { return esc_attr( __fs( $key, $slug ) ); } /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug */ function fs_esc_attr_echo($key, $slug) { echo esc_attr( __fs( $key, $slug ) ); } /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug * * @return string */ function fs_esc_js($key, $slug) { return esc_js( __fs( $key, $slug ) ); } /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug */ function fs_esc_js_echo($key, $slug) { echo esc_js( __fs( $key, $slug ) ); } /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug */ function fs_json_encode_echo($key, $slug) { echo json_encode( __fs( $key, $slug ) ); } /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug * * @return string */ function fs_esc_html($key, $slug) { return esc_html( __fs( $key, $slug ) ); } /** * @author Vova Feldman * @since 1.2.1.6 * * @param string $key * @param string $slug */ function fs_esc_html_echo($key, $slug) { echo esc_html( __fs( $key, $slug ) ); } #endregion