Commit 478a58ef authored by John James Jacoby's avatar John James Jacoby

Tweaks to updater methods.

parent d5f71967
......@@ -23,12 +23,10 @@ class Flox_Licensed_Product {
protected $remote_api_url = null;
protected $slug = null;
protected $name = null;
protected $updater = null;
protected $version = null;
protected $author = null;
protected $download_id = null;
protected $renew_url = null;
protected $response_key = null;
/**
* Initialize the class
......@@ -40,8 +38,6 @@ class Flox_Licensed_Product {
'remote_api_url' => '',
'slug' => '',
'name' => '',
'updater' => '',
'license' => '',
'version' => '',
'author' => '',
'download_id' => '',
......@@ -51,8 +47,7 @@ class Flox_Licensed_Product {
// Set config arguments
$this->remote_api_url = esc_url_raw( $r['remote_api_url'] );
$this->slug = sanitize_key( $r['slug'] );
$this->name = $r['name'];
$this->updater = $r['updater'];
$this->name = urlencode( $r['name'] );
$this->version = $r['version'];
$this->author = $r['author'];
$this->download_id = $r['download_id'];
......@@ -190,7 +185,7 @@ class Flox_Licensed_Product {
* @param array $api_params to be used for wp_remote_get
* @return array $response decoded JSON response
*/
public function get_api_response( $api_params = array() ) {
protected function get_api_response( $api_params = array() ) {
// Call the custom API.
$response = wp_remote_post( $this->remote_api_url, array(
......@@ -216,7 +211,7 @@ class Flox_Licensed_Product {
$license_data = $this->get_api_response( array(
'edd_action' => 'activate_license',
'license' => $this->get_license(),
'item_name' => urlencode( $this->name )
'item_name' => $this->name
) );
// $response->license will be either "active" or "inactive"
......@@ -234,7 +229,7 @@ class Flox_Licensed_Product {
$license_data = $this->get_api_response( array(
'edd_action' => 'deactivate_license',
'license' => $this->get_license(),
'item_name' => urlencode( $this->name )
'item_name' => $this->name
) );
// $license_data->license will be either "deactivated" or "failed"
......@@ -244,46 +239,36 @@ class Flox_Licensed_Product {
}
/**
* Constructs a renewal link
* Checks if a license action was submitted.
*/
public function get_renewal_link() {
public function license_action() {
// Use renewal link if passed in config
if ( ! empty( $this->renew_url ) ) {
return $this->renew_url;
}
// Maybe activate
if ( $this->check_admin_referer( 'license_activate' ) ) {
$this->activate_license();
// If download_id was passed in the config, a renewal link can be constructed
$license = $this->get_license();
if ( ! empty( $this->download_id ) && ! empty( $license ) ) {
return add_query_arg( array(
'edd_license_key' => $license,
'download_id' => $this->download_id
), $this->remote_api_url . '/checkout/' );
// Maybe deactivate
} elseif ( $this->check_admin_referer( 'license_deactivate' ) ) {
$this->deactivate_license();
}
// Otherwise return the remote_api_url
return $this->remote_api_url;
}
/**
* Checks if a license action was submitted.
* Check action & referrer
*
* @param string $action
*
* @return boolean
*/
public function license_action() {
protected function check_admin_referer( $action = '' ) {
// Activate
if ( isset( $_POST[ $this->slug . '_license_activate' ] ) ) {
if ( check_admin_referer( $this->transient_nonce_key(), $this->transient_nonce_key() ) ) {
$this->activate_license();
}
// Bail if no action
if ( ! isset( $_POST[ $this->slug . '_' . $action ] ) ) {
return false;
}
// Deactivate
if ( isset( $_POST[ $this->slug . '_license_deactivate'] ) ) {
if ( check_admin_referer( $this->transient_nonce_key(), $this->transient_nonce_key() ) ) {
$this->deactivate_license();
}
}
// Check nonce
return check_admin_referer( $this->transient_nonce_key(), $this->transient_nonce_key() );
}
/**
......@@ -291,32 +276,30 @@ class Flox_Licensed_Product {
*
* @return string $message License status message.
*/
public function check_license() {
protected function check_license() {
$license_data = $this->get_api_response( array(
'edd_action' => 'check_license',
'license' => $this->get_license(),
'item_name' => urlencode( $this->name ),
'item_name' => $this->name,
'url' => home_url()
) );
// Bail if response doesn't include license data
if ( ! isset( $license_data->license ) ) {
return __( 'The status of this sicense is unknown.', 'flox' );
if ( empty( $license_data->license ) ) {
return __( 'The status of this license is unknown.', 'flox' );
}
// We need to update the license status at the same time the message
// is updated
if ( ! empty( $license_data->license ) ) {
$this->update_license_status( $license_data->license );
}
// Update the license status
$this->update_license_status( $license_data->license );
// Get expire date
$expires = false;
$renew_link = '';
if ( isset( $license_data->expires ) && ( 'lifetime' !== $license_data->expires ) ) {
if ( ! empty( $license_data->expires ) && ( 'lifetime' !== $license_data->expires ) ) {
$expires = date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires ) );
$renew_link = '<a href="' . esc_url( $this->get_renewal_link() ) . '" target="_blank">' . esc_html__( 'Renew?', 'flox' ) . '</a>';
} else {
$expires = false;
$renew_link = '';
}
// Empty messages array
......@@ -395,7 +378,7 @@ class Flox_Licensed_Product {
*
* @return boolean
*/
public function check_for_update() {
protected function check_for_update() {
// Look for API response
$update_data = $this->get_update_status();
......@@ -424,8 +407,10 @@ class Flox_Licensed_Product {
return false;
}
// Maybe unserialize sections
$update_data->sections = maybe_unserialize( $update_data->sections );
// Look again in 12 hours
$this->set_update_transient( $update_data, strtotime( '+12 hours' ) );
}
......@@ -433,7 +418,13 @@ class Flox_Licensed_Product {
return (array) $update_data;
}
protected function update_available( $update_data = false ) {
/**
* Is an update to this product available?
*
* @param mixed $update_data
* @return bool
*/
protected function is_update_available( $update_data = false ) {
if ( false === $update_data ) {
$update_data = $this->check_for_update();
}
......@@ -441,6 +432,29 @@ class Flox_Licensed_Product {
return (bool) version_compare( $this->version, $update_data->new_version, '<' );
}
/**
* Constructs a renewal link
*/
protected function get_renewal_link() {
// Use renewal link if passed in config
if ( ! empty( $this->renew_url ) ) {
return $this->renew_url;
}
// If download_id was passed in the config, construct a renewal link
$license = $this->get_license();
if ( ! empty( $this->download_id ) && ! empty( $license ) ) {
return add_query_arg( array(
'edd_license_key' => $license,
'download_id' => $this->download_id
), $this->remote_api_url . '/checkout/' );
}
// Otherwise return the remote_api_url
return $this->remote_api_url;
}
/** Option Keys ***********************************************************/
protected function license() {
......@@ -483,6 +497,29 @@ class Flox_Licensed_Product {
/** Option Updaters *******************************************************/
/**
* Public method used by public actions for updating transient data
*
* @param mixed $value
* @return mixed
*/
public function maybe_update_transient( $value = false ) {
$update_data = $this->check_for_update();
if ( is_object( $update_data ) ) {
$value->response[ $this->slug ] = $update_data;
}
return $value;
}
/**
* Public method used by public actions for deleting transient data
*/
public function delete_update_transient() {
delete_transient( $this->transient_response_key() );
}
/**
* Set of delete the updater transient
*
......@@ -493,7 +530,7 @@ class Flox_Licensed_Product {
*/
protected function set_update_transient( $data = false, $duration = DAY_IN_SECONDS ) {
if ( false === $data ) {
delete_transient( $this->transient_response_key() );
$this->delete_update_transient();
} else {
set_transient( $this->transient_response_key(), $data, $duration );
}
......
......@@ -40,15 +40,6 @@ class Flox_Plugin_Updater extends Flox_Licensed_Product {
$this->slug = basename( $args['plugin_file'], '.php' );
$this->name = plugin_basename( $args['plugin_file'] );
// Filters
add_filter( 'site_transient_update_plugins', array( $this, 'plugin_update_transient' ) );
add_filter( 'delete_site_transient_update_plugins', array( $this, 'delete_plugin_update_transient' ) );
// Actions
add_action( 'load-update-core.php', array( $this, 'delete_plugin_update_transient' ) );
add_action( 'load-plugins.php', array( $this, 'delete_plugin_update_transient' ) );
add_action( 'load-plugins.php', array( $this, 'load_plugins_screen' ) );
// Set up hooks.
$this->init();
}
......@@ -56,15 +47,21 @@ class Flox_Plugin_Updater extends Flox_Licensed_Product {
/**
* Set up WordPress filters to hook into WP's update process.
*/
public function init() {
private function init() {
// Actions
add_action( 'load-update-core.php', array( $this, 'delete_update_transient' ) );
add_action( 'load-plugins.php', array( $this, 'delete_update_transient' ) );
add_action( 'load-plugins.php', array( $this, 'load_plugins_screen' ) );
add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ) );
add_action( 'admin_init', array( $this, 'show_changelog' ) );
// Filters
//add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) );
add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
add_filter( 'site_transient_update_plugins', array( $this, 'plugin_update_transient' ) );
add_filter( 'delete_site_transient_update_plugins', array( $this, 'delete_update_transient' ) );
// Actions
add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ) );
add_action( 'admin_init', array( $this, 'show_changelog' ) );
// Filters
add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
}
/**
......@@ -323,11 +320,7 @@ class Flox_Plugin_Updater extends Flox_Licensed_Product {
* @return array
*/
public function plugin_update_transient( $value = false ) {
$update_data = $this->check_for_update();
if ( false !== $update_data ) {
$value->response[ $this->slug ] = $update_data;
}
return $value;
return $this->maybe_update_transient( $value );
}
/**
......
......@@ -47,13 +47,13 @@ class Flox_Theme_Updater extends Flox_Licensed_Product {
private function init() {
// Actions
add_action( 'load-update-core.php', array( $this, 'delete_theme_update_transient' ) );
add_action( 'load-themes.php', array( $this, 'delete_theme_update_transient' ) );
add_action( 'load-themes.php', array( $this, 'load_themes_screen' ) );
add_action( 'load-update-core.php', array( $this, 'delete_update_transient' ) );
add_action( 'load-themes.php', array( $this, 'delete_update_transient' ) );
add_action( 'load-themes.php', array( $this, 'load_themes_screen' ) );
// Filters
add_filter( 'site_transient_update_themes', array( $this, 'theme_update_transient' ) );
add_filter( 'delete_site_transient_update_themes', array( $this, 'delete_theme_update_transient' ) );
add_filter( 'site_transient_update_themes', array( $this, 'maybe_update_transient' ) );
add_filter( 'delete_site_transient_update_themes', array( $this, 'delete_update_transient' ) );
// Disable wporg requests
add_filter( 'http_request_args', array( $this, 'disable_wporg_request' ), 5, 2 );
......@@ -87,7 +87,7 @@ class Flox_Theme_Updater extends Flox_Licensed_Product {
}
// Bail if no new version
if ( ! $this->update_available( $api_response ) ) {
if ( ! $this->is_update_available( $api_response ) ) {
return;
}
......@@ -150,11 +150,7 @@ class Flox_Theme_Updater extends Flox_Licensed_Product {
* @return array
*/
public function theme_update_transient( $value = false ) {
$update_data = $this->check_for_update();
if ( false !== $update_data ) {
$value->response[ $this->slug ] = $update_data;
}
return $value;
return $this->maybe_update_transient( $value );
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment