Repository URL to install this package:
<?php
/**
* @package Freemius
* @copyright Copyright (c) 2015, Freemius, Inc.
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3
* @since 1.0.4
*
* @link https://github.com/easydigitaldownloads/EDD-License-handler/blob/master/EDD_SL_Plugin_Updater.php
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class FS_Plugin_Updater {
/**
* @var Freemius
* @since 1.0.4
*/
private $_fs;
/**
* @var FS_Logger
* @since 1.0.4
*/
private $_logger;
/**
* @var object
* @since 1.1.8.1
*/
private $_update_details;
/**
* @var array
* @since 2.1.2
*/
private $_translation_updates;
private static $_upgrade_basename = null;
#--------------------------------------------------------------------------------
#region Singleton
#--------------------------------------------------------------------------------
/**
* @var FS_Plugin_Updater[]
* @since 2.0.0
*/
private static $_INSTANCES = array();
/**
* @param Freemius $freemius
*
* @return FS_Plugin_Updater
*/
static function instance( Freemius $freemius ) {
$key = $freemius->get_id();
if ( ! isset( self::$_INSTANCES[ $key ] ) ) {
self::$_INSTANCES[ $key ] = new self( $freemius );
}
return self::$_INSTANCES[ $key ];
}
#endregion
private function __construct( Freemius $freemius ) {
$this->_fs = $freemius;
$this->_logger = FS_Logger::get_logger( WP_FS__SLUG . '_' . $freemius->get_slug() . '_updater', WP_FS__DEBUG_SDK, WP_FS__ECHO_DEBUG_SDK );
$this->filters();
}
/**
* Initiate required filters.
*
* @author Vova Feldman (@svovaf)
* @since 1.0.4
*/
private function filters() {
// Override request for plugin information
add_filter( 'plugins_api', array( &$this, 'plugins_api_filter' ), 10, 3 );
$this->add_transient_filters();
/**
* If user has the premium plugin's code but do NOT have an active license,
* encourage him to upgrade by showing that there's a new release, but instead
* of showing an update link, show upgrade link to the pricing page.
*
* @since 1.1.6
*
*/
// WP 2.9+
add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
&$this,
'catch_plugin_update_row'
), 9 );
add_action( "after_plugin_row_{$this->_fs->get_plugin_basename()}", array(
&$this,
'edit_and_echo_plugin_update_row'
), 11, 2 );
if ( ! $this->_fs->has_any_active_valid_license() ) {
add_action( 'admin_head', array( &$this, 'catch_plugin_information_dialog_contents' ) );
}
if ( ! WP_FS__IS_PRODUCTION_MODE ) {
add_filter( 'http_request_host_is_external', array(
$this,
'http_request_host_is_external_filter'
), 10, 3 );
}
if ( $this->_fs->is_premium() ) {
if ( ! $this->is_correct_folder_name() ) {
add_filter( 'upgrader_post_install', array( &$this, '_maybe_update_folder_name' ), 10, 3 );
}
add_filter( 'upgrader_pre_install', array( 'FS_Plugin_Updater', '_store_basename_for_source_adjustment' ), 1, 2 );
add_filter( 'upgrader_source_selection', array( 'FS_Plugin_Updater', '_maybe_adjust_source_dir' ), 1, 3 );
if ( ! $this->_fs->has_any_active_valid_license() ) {
add_filter( 'wp_prepare_themes_for_js', array( &$this, 'change_theme_update_info_html' ), 10, 1 );
}
}
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.1.4
*/
function catch_plugin_information_dialog_contents() {
if (
'plugin-information' !== fs_request_get( 'tab', false ) ||
$this->_fs->get_slug() !== fs_request_get( 'plugin', false )
) {
return;
}
add_action( 'admin_footer', array( &$this, 'edit_and_echo_plugin_information_dialog_contents' ), 0, 1 );
ob_start();
}
/**
* @author Leo Fajardo (@leorw)
* @since 2.1.4
*
* @param string $hook_suffix
*/
function edit_and_echo_plugin_information_dialog_contents( $hook_suffix ) {
if (
'plugin-information' !== fs_request_get( 'tab', false ) ||
$this->_fs->get_slug() !== fs_request_get( 'plugin', false )
) {
return;
}
$license = $this->_fs->_get_license();
$subscription = ( is_object( $license ) && ! $license->is_lifetime() ) ?
$this->_fs->_get_subscription( $license->id ) :
null;
$contents = ob_get_clean();
$update_button_id_attribute_pos = strpos( $contents, 'id="plugin_update_from_iframe"' );
if ( false !== $update_button_id_attribute_pos ) {
$update_button_start_pos = strrpos(
substr( $contents, 0, $update_button_id_attribute_pos ),
'<a'
);
$update_button_end_pos = ( strpos( $contents, '</a>', $update_button_id_attribute_pos ) + strlen( '</a>' ) );
/**
* The part of the contents without the update button.
*
* @author Leo Fajardo (@leorw)
* @since 2.2.5
*/
$modified_contents = substr( $contents, 0, $update_button_start_pos );
$update_button = substr( $contents, $update_button_start_pos, ( $update_button_end_pos - $update_button_start_pos ) );
/**
* Replace the plugin information dialog's "Install Update Now" button's text and URL. If there's a license,
* the text will be "Renew license" and will link to the checkout page with the license's billing cycle
* and quota. If there's no license, the text will be "Buy license" and will link to the pricing page.
*/
$update_button = preg_replace(
'/(\<a.+)(id="plugin_update_from_iframe")(.+href=")([^\s]+)(".*\>)(.+)(\<\/a>)/is',
is_object( $license ) ?
sprintf(
'$1$3%s$5%s$7',
$this->_fs->checkout_url(
is_object( $subscription ) ?
( 1 == $subscription->billing_cycle ? WP_FS__PERIOD_MONTHLY : WP_FS__PERIOD_ANNUALLY ) :
WP_FS__PERIOD_LIFETIME,
false,
array( 'licenses' => $license->quota )
),
fs_text_inline( 'Renew license', 'renew-license', $this->_fs->get_slug() )
) :
sprintf(
'$1$3%s$5%s$7',
$this->_fs->pricing_url(),
fs_text_inline( 'Buy license', 'buy-license', $this->_fs->get_slug() )
),
$update_button
);
/**
* Append the modified button.
*
* @author Leo Fajardo (@leorw)
* @since 2.2.5
*/
$modified_contents .= $update_button;
/**
* Append the remaining part of the contents after the update button.
*
* @author Leo Fajardo (@leorw)
* @since 2.2.5
*/
$modified_contents .= substr( $contents, $update_button_end_pos );
$contents = $modified_contents;
}
echo $contents;
}
/**
* @author Vova Feldman (@svovaf)
* @since 2.0.0
*/
private function add_transient_filters() {
if (
$this->_fs->is_premium() &&
$this->_fs->is_registered() &&
! FS_Permission_Manager::instance( $this->_fs )->is_essentials_tracking_allowed()
) {
$this->_logger->log( 'Opted out sites cannot receive automatic software updates.' );
return;
}
add_filter( 'pre_set_site_transient_update_plugins', array(
&$this,
'pre_set_site_transient_update_plugins_filter'
) );
add_filter( 'pre_set_site_transient_update_themes', array(
&$this,
'pre_set_site_transient_update_plugins_filter'
) );
}
/**
* @author Vova Feldman (@svovaf)
* @since 2.0.0
*/
private function remove_transient_filters() {
remove_filter( 'pre_set_site_transient_update_plugins', array(
&$this,
'pre_set_site_transient_update_plugins_filter'
) );
remove_filter( 'pre_set_site_transient_update_themes', array(
&$this,
'pre_set_site_transient_update_plugins_filter'
) );
}
/**
* Capture plugin update row by turning output buffering.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.6
*/
function catch_plugin_update_row() {
ob_start();
}
/**
* Overrides default update message format with "renew your license" message.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.6
*
* @param string $file
* @param array $plugin_data
*/
function edit_and_echo_plugin_update_row( $file, $plugin_data ) {
$plugin_update_row = ob_get_clean();
$current = get_site_transient( 'update_plugins' );
if ( ! isset( $current->response[ $file ] ) ) {
echo $plugin_update_row;
return;
}
$r = $current->response[ $file ];
$has_beta_update = $this->_fs->has_beta_update();
if ( $this->_fs->has_any_active_valid_license() ) {
if ( $has_beta_update ) {
/**
* Turn the "new version" text into "new Beta version".
*
* Sample input:
* There is a new version of Awesome Plugin available. <a href="...>View version x.y.z details</a> or <a href="...>update now</a>.
* Output:
* There is a new Beta version of Awesome Plugin available. <a href="...>View version x.y.z details</a> or <a href="...>update now</a>.
*
* @author Leo Fajardo (@leorw)
* @since 2.3.0
*/
$plugin_update_row = preg_replace(
'/(\<div.+>)(.+)(\<a.+href="([^\s]+)"([^\<]+)\>.+\<a.+)(\<\/div\>)/is',
(
'$1' .
sprintf(
fs_text_inline( 'There is a %s of %s available.', 'new-version-available', $this->_fs->get_slug() ),
$has_beta_update ?
fs_text_inline( 'new Beta version', 'new-beta-version', $this->_fs->get_slug() ) :
fs_text_inline( 'new version', 'new-version', $this->_fs->get_slug() ),
$this->_fs->get_plugin_title()
) .
' ' .
'$3' .
'$6'
),
$plugin_update_row
);
}
} else {
/**
Loading ...