/** * Find the plugin main file path based on any give file inside the plugin's folder. * * @author Vova Feldman (@svovaf) * @since 1.1.7.1 * * @param string $file Absolute path to a file inside a plugin's folder. * * @return string */ function fs_find_direct_caller_plugin_file($file) { /** * All the code below will be executed once on activation. * If the user changes the main plugin's file name, the file_exists() * will catch it. */ if (!function_exists('get_plugins')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $all_plugins = get_plugins(); $file_real_path = fs_normalize_path(realpath($file)); // Get active plugin's main files real full names (might be symlinks). foreach ($all_plugins as $relative_path => &$data) { if (0 === strpos($file_real_path, fs_normalize_path(dirname(realpath(WP_PLUGIN_DIR . '/' . $relative_path))))) { return $relative_path; } } return null; }
/** * Get add-on basename. * * @author Vova Feldman (@svovaf) * @since 1.0.6 * * @param string $slug * * @return string */ function get_addon_basename($slug) { if ($this->is_addon_activated($slug)) { self::instance($slug)->get_plugin_basename(); } $premium_basename = $slug . '-premium/' . $slug . '.php'; if (file_exists(fs_normalize_path(WP_PLUGIN_DIR . '/' . $premium_basename))) { return $premium_basename; } $free_basename = $slug . '/' . $slug . '.php'; return $free_basename; }
if (class_exists('Freemius')) { if (fs_redirect($_SERVER['REQUEST_URI'])) { exit; } } } } } } } if (class_exists('Freemius')) { // SDK was already loaded. return; } if (version_compare($this_sdk_version, $fs_active_plugins->newest->version, '<')) { $newest_sdk_starter = fs_normalize_path(WP_PLUGIN_DIR . '/' . $fs_active_plugins->newest->sdk_path . '/start.php'); if (file_exists($newest_sdk_starter)) { // Reorder plugins to load plugin with newest SDK first. fs_newest_sdk_plugin_first(); // There's a newer SDK version, load it instead of the current one! require_once $newest_sdk_starter; return; } } #endregion SDK Selection Logic -------------------------------------------------------------------- #region Hooks & Filters Collection -------------------------------------------------------------------- /** * Freemius hooks (actions & filters) tags structure: * * fs_{filter/action_name}_{plugin_slug} *
* This code will only be executed once during the testing * of the plugin in a local environment. The plugin icon file WILL * already exist in the assets folder when the plugin is deployed to * the repository. */ $suffixes = array('-128x128.png', '-128x128.jpg', '-256x256.png', '-256x256.jpg', '.svg'); $base_url = 'https://plugins.svn.wordpress.org/' . $slug . '/assets/icon'; foreach ($suffixes as $s) { $headers = get_headers($base_url . $s); if (strpos($headers[0], '200')) { $local_path = fs_normalize_path(WP_FS__DIR_IMG . '/icon.' . substr($s, strpos($s, '.') + 1)); fs_download_image($base_url . $s, $local_path); $icon_found = true; break; } } } if (!$icon_found) { // No icons found, fallback to default icon. copy(fs_normalize_path(WP_FS__DIR_IMG . '/plugin-icon.png'), $local_path); } $icons = array($local_path); } $relative_url = fs_img_url(substr($icons[0], strlen(fs_normalize_path(WP_FS__DIR_IMG)))); ?> <div class="fs-plugin-icon"> <img src="<?php echo $relative_url; ?> "/> </div>
/** * Determines if add-on installed. * * NOTE: This is a heuristic and only works if the folder/file named as the slug. * * @author Vova Feldman (@svovaf) * @since 1.0.6 * * @param string $slug * * @return bool */ function is_addon_installed($slug) { return file_exists(fs_normalize_path(WP_PLUGIN_DIR . '/' . $this->get_addon_basename($slug))); }
$headers = get_headers($base_url . $s); if (strpos($headers[0], '200')) { $local_path = fs_normalize_path($img_dir . '/' . $slug . '.' . substr($s, strpos($s, '.') + 1)); fs_download_image($base_url . $s, $local_path); $icon_found = true; break; } } } if (!$icon_found) { // No icons found, fallback to default icon. if ($have_write_permissions) { // If have write permissions, copy default icon. copy(fs_normalize_path($img_dir . '/plugin-icon.png'), $local_path); } else { // If doesn't have write permissions, use default icon path. $local_path = fs_normalize_path($img_dir . '/plugin-icon.png'); } } $icons = array($local_path); } } $icon_dir = dirname($icons[0]); $relative_url = fs_img_url(substr($icons[0], strlen($icon_dir)), $icon_dir); ?> <div class="fs-plugin-icon"> <img src="<?php echo $relative_url; ?> "/> </div>
/** * Go over all Freemius SDKs in the system and find and "remember" * the newest SDK which is associated with an active plugin. * * @author Vova Feldman (@svovaf) * @since 1.1.6 * * @global $fs_active_plugins */ function fs_fallback_to_newest_active_sdk() { global $fs_active_plugins; $newest_sdk_data = null; $newest_sdk_path = null; foreach ($fs_active_plugins->plugins as $sdk_relative_path => $data) { if (is_null($newest_sdk_data) || version_compare($data->version, $newest_sdk_data->version, '>')) { // If plugin inactive or SDK starter file doesn't exist, remove SDK reference. if (!is_plugin_active($data->plugin_path) || !file_exists(fs_normalize_path(WP_PLUGIN_DIR . '/' . $sdk_relative_path . '/start.php'))) { unset($fs_active_plugins->plugins[$sdk_relative_path]); // No need to store the data since it will be stored in fs_update_sdk_newest_version() // or explicitly with update_option(). } else { $newest_sdk_data = $data; $newest_sdk_path = $sdk_relative_path; } } } if (is_null($newest_sdk_data)) { // Couldn't find any SDK reference. $fs_active_plugins = new stdClass(); update_option('fs_active_plugins', $fs_active_plugins); } else { fs_update_sdk_newest_version($newest_sdk_path, $newest_sdk_data->plugin_path); } }
function fs_download_image($from, $to) { $ch = curl_init($from); $fp = fopen(fs_normalize_path($to), 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); }
* This code will only be executed once during the testing * of the plugin in a local environment. The plugin icon file WILL * already exist in the assets folder when the plugin is deployed to * the repository. */ $suffixes = array('-128x128.png', '-128x128.jpg', '-256x256.png', '-256x256.jpg', '.svg'); $base_url = 'https://plugins.svn.wordpress.org/' . $slug . '/assets/icon'; foreach ($suffixes as $s) { $headers = get_headers($base_url . $s); if (strpos($headers[0], '200')) { $local_path = fs_normalize_path($img_dir . '/icon.' . substr($s, strpos($s, '.') + 1)); fs_download_image($base_url . $s, $local_path); $icon_found = true; break; } } } if (!$icon_found) { // No icons found, fallback to default icon. copy(fs_normalize_path($img_dir . '/plugin-icon.png'), $local_path); } $icons = array($local_path); } $relative_url = fs_img_url(substr($icons[0], strlen(fs_normalize_path($img_dir))), $img_dir); ?> <div class="fs-plugin-icon"> <img src="<?php echo $relative_url; ?> "/> </div>