Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update EDD_SL_Plugin_Updater to 1.8 #8170

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 72 additions & 41 deletions includes/EDD_SL_Plugin_Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Allows plugins to use their own update API.
*
* @author Easy Digital Downloads
* @version 1.6.19
* @version 1.8.0
*/
class EDD_SL_Plugin_Updater {

Expand Down Expand Up @@ -106,32 +106,50 @@ public function check_update( $_transient_data ) {
return $_transient_data;
}

$version_info = $this->get_cached_version_info();

if ( false === $version_info ) {
$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );

$this->set_version_info_cache( $version_info );

$current = $this->get_repo_api_data();
if ( false !== $current && is_object( $current ) && isset( $current->new_version ) ) {
if ( version_compare( $this->version, $current->new_version, '<' ) ) {
$_transient_data->response[ $this->name ] = $current;
} else {
// Populating the no_update information is required to support auto-updates in WordPress 5.5.
$_transient_data->no_update[ $this->name ] = $current;
}
}
$_transient_data->last_checked = time();
$_transient_data->checked[ $this->name ] = $this->version;

if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {

if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {

$_transient_data->response[ $this->name ] = $version_info;
return $_transient_data;
}

// Make sure the plugin property is set to the plugin's name/location. See issue 1463 on Software Licensing's GitHub repo.
$_transient_data->response[ $this->name ]->plugin = $this->name;
/**
* Get repo API data from store.
* Save to cache.
*
* @return \stdClass
*/
public function get_repo_api_data() {
$version_info = $this->get_cached_version_info();

if ( false === $version_info ) {
$version_info = $this->api_request(
'plugin_latest_version',
array(
'slug' => $this->slug,
'beta' => $this->beta,
)
);
if ( ! $version_info ) {
return false;
}

$_transient_data->last_checked = time();
$_transient_data->checked[ $this->name ] = $this->version;
// This is required for your plugin to support auto-updates in WordPress 5.5.
$version_info->plugin = $this->name;
$version_info->id = $this->name;

$this->set_version_info_cache( $version_info );
}

return $_transient_data;
return $version_info;
}

/**
Expand Down Expand Up @@ -167,7 +185,7 @@ public function show_update_notification( $file, $plugin ) {

if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {

$version_info = $this->get_cached_version_info();
$version_info = $this->get_repo_api_data();

if ( false === $version_info ) {
$version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug, 'beta' => $this->beta ) );
Expand All @@ -185,10 +203,6 @@ public function show_update_notification( $file, $plugin ) {
$version_info->icons = $this->convert_object_to_array( $version_info->icons );
}

if ( isset( $version_info->icons ) && ! is_array( $version_info->icons ) ) {
$version_info->icons = $this->convert_object_to_array( $version_info->icons );
}

if ( isset( $version_info->contributors ) && ! is_array( $version_info->contributors ) ) {
$version_info->contributors = $this->convert_object_to_array( $version_info->contributors );
}
Expand All @@ -201,12 +215,12 @@ public function show_update_notification( $file, $plugin ) {
}

if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {

$update_cache->response[ $this->name ] = $version_info;

} else {
$update_cache->no_update[ $this->name ] = $version_info;
}

$update_cache->last_checked = time();
$update_cache->last_checked = time();
$update_cache->checked[ $this->name ] = $this->version;

set_site_transient( 'update_plugins', $update_cache );
Expand Down Expand Up @@ -291,18 +305,16 @@ public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
)
);

$cache_key = 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) );

// Get the transient where we store the api request for this plugin for 24 hours
$edd_api_request_transient = $this->get_cached_version_info( $cache_key );
$edd_api_request_transient = $this->get_cached_version_info();

//If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
if ( empty( $edd_api_request_transient ) ) {

$api_response = $this->api_request( 'plugin_information', $to_send );

// Expires in 3 hours
$this->set_version_info_cache( $api_response, $cache_key );
$this->set_version_info_cache( $api_response );

if ( false !== $api_response ) {
$_data = $api_response;
Expand Down Expand Up @@ -413,16 +425,16 @@ private function api_request( $_action, $_data ) {
}

if ( false === $edd_plugin_url_available[ $store_hash ] ) {
return;
return false;
}

$data = array_merge( $this->api_data, $_data );

if ( $data['slug'] != $this->slug ) {
return;
return false;
}

if( $this->api_url == trailingslashit ( home_url() ) ) {
if ( $this->api_url == trailingslashit ( home_url() ) ) {
return false; // Don't allow a plugin to ping itself
}

Expand Down Expand Up @@ -458,7 +470,7 @@ private function api_request( $_action, $_data ) {
$request->icons = maybe_unserialize( $request->icons );
}

if( ! empty( $request->sections ) ) {
if ( ! empty( $request->sections ) ) {
foreach( $request->sections as $key => $section ) {
$request->$key = (array) $section;
}
Expand All @@ -467,6 +479,9 @@ private function api_request( $_action, $_data ) {
return $request;
}

/**
* If available, show the changelog for sites in a multisite install.
*/
public function show_changelog() {

global $edd_plugin_data;
Expand All @@ -488,9 +503,7 @@ public function show_changelog() {
}

$data = $edd_plugin_data[ $_REQUEST['slug'] ];
$beta = ! empty( $data['beta'] ) ? true : false;
$cache_key = md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $beta . '_version_info' );
$version_info = $this->get_cached_version_info( $cache_key );
$version_info = $this->get_cached_version_info();

if( false === $version_info ) {

Expand All @@ -511,7 +524,6 @@ public function show_changelog() {
$version_info = json_decode( wp_remote_retrieve_body( $request ) );
}


if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
$version_info->sections = maybe_unserialize( $version_info->sections );
} else {
Expand All @@ -524,17 +536,28 @@ public function show_changelog() {
}
}

$this->set_version_info_cache( $version_info, $cache_key );
$this->set_version_info_cache( $version_info );

// Delete the unneeded option
delete_option( md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $this->beta . '_version_info' ) );
}

if( ! empty( $version_info ) && isset( $version_info->sections['changelog'] ) ) {
echo '<div style="background:#fff;padding:10px;">' . $version_info->sections['changelog'] . '</div>';
if ( isset( $version_info->sections ) ) {
$sections = $this->convert_object_to_array( $version_info->sections );
if ( ! empty( $sections['changelog'] ) ) {
echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>';
}
}

exit;
}

/**
* Gets the plugin's cached version information from the database.
*
* @param string $cache_key
* @return boolean|string
*/
public function get_cached_version_info( $cache_key = '' ) {

if( empty( $cache_key ) ) {
Expand All @@ -557,6 +580,12 @@ public function get_cached_version_info( $cache_key = '' ) {

}

/**
* Adds the plugin version information to the database.
*
* @param string $value
* @param string $cache_key
*/
public function set_version_info_cache( $value = '', $cache_key = '' ) {

if( empty( $cache_key ) ) {
Expand All @@ -570,6 +599,8 @@ public function set_version_info_cache( $value = '', $cache_key = '' ) {

update_option( $cache_key, $data, 'no' );

// Delete the duplicate option
delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) );
}

/**
Expand Down
8 changes: 7 additions & 1 deletion includes/class-edd-license-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function hooks() {
//add_action( 'admin_init', array( $this, 'weekly_license_check' ) );

// Updater
add_action( 'admin_init', array( $this, 'auto_updater' ), 0 );
add_action( 'init', array( $this, 'auto_updater' ) );

// Display notices to admins
add_action( 'admin_notices', array( $this, 'notices' ) );
Expand All @@ -130,6 +130,12 @@ private function hooks() {
* @return void
*/
public function auto_updater() {

$doing_cron = defined( 'DOING_CRON' ) && DOING_CRON;
if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) {
return;
}

$betas = edd_get_option( 'enabled_betas', array() );

$args = array(
Expand Down