Corral things together

This commit is contained in:
claudiulodro 2017-07-13 11:00:39 -07:00
parent b355e016cd
commit 45ba7fcbbb
3 changed files with 142 additions and 83 deletions

View File

@ -20,13 +20,53 @@ class WC_Admin_Plugin_Updates {
const VERSION_REQUIRED_HEADER = 'WC requires at least';
const VERSION_TESTED_HEADER = 'WC tested up to';
protected $relative_upgrade_type = ''; // One of: 'unknown', 'major', 'minor', 'patch'.
protected $upgrade_notice = '';
protected $new_version = '';
protected $untested_extensions = array();
/**
* Constructor.
*/
public function __construct() {
add_filter( 'extra_plugin_headers', array( $this, 'enable_wc_plugin_headers' ) );
add_action( 'in_plugin_update_message-woocommerce/woocommerce.php', array( $this, 'output_untested_plugin_warnings' ), 11, 2 );
add_action( 'admin_footer', array( $this, 'js' ) );
add_action( 'in_plugin_update_message-woocommerce/woocommerce.php', array( $this, 'in_plugin_update_message' ), 10, 2 );
}
/**
* Read in WooCommerce headers when reading plugin headers.
*
* @param array $headers
* @return array $headers
*/
public function enable_wc_plugin_headers( $headers ) {
$headers['WCRequires'] = self::VERSION_REQUIRED_HEADER;
$headers['WCTested'] = self::VERSION_TESTED_HEADER;
return $headers;
}
/**
* Show plugin changes. Code adapted from W3 Total Cache.
*
* @param array $args
*/
public function in_plugin_update_message( $args, $response ) {
$this->new_version = $response->new_version;
$this->upgrade_notice = $this->get_upgrade_notice( $response->new_version );
$this->relative_upgrade_type = $this->get_upgrade_type( $args['Version'], $response->new_version );
$this->untested_plugins = $this->get_untested_plugins( $response->new_version );
if ( ! empty( $this->untested_plugins ) && ( 'major' == $this->relative_upgrade_type || 'minor' == $this->relative_upgrade_type ) ) {
$this->upgrade_notice .= $this->get_extensions_inline_warning();
}
if ( ! empty( $this->untested_plugins ) && 'major' === $this->relative_upgrade_type ) {
$this->upgrade_notice .= $this->get_extensions_modal_warning();
add_action( 'admin_footer', array( $this, 'js' ) );
}
echo apply_filters( 'woocommerce_in_plugin_update_message', wp_kses_post( $this->upgrade_notice ) );
}
public function js() {
@ -46,31 +86,112 @@ class WC_Admin_Plugin_Updates {
<?php
}
/**
* Read in WooCommerce headers when reading plugin headers.
*
* @param array $headers
* @return array $headers
*/
public function enable_wc_plugin_headers( $headers ) {
$headers['WCRequires'] = self::VERSION_REQUIRED_HEADER;
$headers['WCTested'] = self::VERSION_TESTED_HEADER;
return $headers;
/*
|--------------------------------------------------------------------------
| Message Helpers
|--------------------------------------------------------------------------
|
| Methods for getting messages.
*/
protected function get_extensions_inline_warning() {
$plugins = $this->untested_plugins;
$new_version = $this->new_version;
ob_start();
include( 'views/html-notice-untested-extensions-inline.php' );
return ob_get_clean();
}
protected function get_extensions_modal_warning() {
$plugins = $this->untested_plugins;
$new_version = $this->new_version;
//ob_start();
}
protected function get_upgrade_notice( $version ) {
$transient_name = 'wc_upgrade_notice_' . $version;
//if ( false === ( $upgrade_notice = get_transient( $transient_name ) ) ) {
//$response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/woocommerce/trunk/readme.txt' );
$response = wp_safe_remote_get( 'http://local.wordpress.dev/wp-content/plugins/woocommerce/readme.txt' );
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
$upgrade_notice = $this->parse_update_notice( $response['body'], $version );
// set_transient( $transient_name, $upgrade_notice, 1/*DAY_IN_SECONDS*/ );
}
//}
return $upgrade_notice;
}
/**
* Output a warning message if plugins exist with a tested version lower than the WooCommerce version.
* Parse update notice from readme file.
*
* @param array $data
* @param stdObject $response
* @param string $content
* @param string $new_version
* @return string
*/
public function output_untested_plugin_warnings( $data, $response ) {
$plugins = $this->get_untested_plugins( $response->new_version );
if ( empty( $plugins ) ) {
return;
private function parse_update_notice( $content, $new_version ) {
// Output Upgrade Notice.
$matches = null;
$regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( $new_version ) . '\s*=|$)~Uis';
$upgrade_notice = '';
if ( preg_match( $regexp, $content, $matches ) ) {
$notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) );
// Convert the full version strings to minor versions.
$notice_version_parts = explode( '.', trim( $matches[1] ) );
$current_version_parts = explode( '.', WC_VERSION );
if ( 3 !== sizeof( $notice_version_parts ) ) {
return;
}
$notice_version = $notice_version_parts[0] . '.' . $notice_version_parts[1];
$current_version = $current_version_parts[0] . '.' . $current_version_parts[1];
// Check the latest stable version and ignore trunk.
if ( version_compare( $current_version, $notice_version, '<' ) ) {
$upgrade_notice .= '</p><p class="wc_plugin_upgrade_notice">';
foreach ( $notices as $index => $line ) {
$upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line );
}
}
}
include( 'views/html-notice-untested-extensions.php' );
return wp_kses_post( $upgrade_notice );
}
/*
|--------------------------------------------------------------------------
| Data Helpers
|--------------------------------------------------------------------------
|
| Methods for getting & manipulating data.
*/
protected function get_upgrade_type( $current_version, $upgrade_version ) {
$current_parts = explode( '.', $current_version );
$upgrade_parts = explode( '.', $upgrade_version );
if ( 3 !== count( $current_parts ) || 3 !== count( $upgrade_parts ) ) {
return 'unknown';
}
if ( (int) $current_parts[0] < (int) $upgrade_parts[0] ) {
return 'major';
}
if ( (int) $current_parts[1] < (int) $upgrade_parts[1] ) {
return 'minor';
}
if ( (int) $current_parts[2] < (int) $upgrade_parts[2] ) {
return 'patch';
}
return 'unknown';
}
/**

View File

@ -15,7 +15,7 @@ if ( ! defined( 'ABSPATH' ) ) {
/* translators: %s: version number */
printf(
__( 'Heads up! The following plugin(s) are not listed compatible with WooCommerce %s yet. If you upgrade without upgrading these extensions first, you may experience issues:', 'woocommerce' ),
wc_clean( $response->new_version )
wc_clean( $new_version )
);
?>
</strong><br />

View File

@ -100,7 +100,6 @@ class WC_Install {
add_action( 'init', array( __CLASS__, 'check_version' ), 5 );
add_action( 'init', array( __CLASS__, 'init_background_updater' ), 5 );
add_action( 'admin_init', array( __CLASS__, 'install_actions' ) );
add_action( 'in_plugin_update_message-woocommerce/woocommerce.php', array( __CLASS__, 'in_plugin_update_message' ) );
add_filter( 'plugin_action_links_' . WC_PLUGIN_BASENAME, array( __CLASS__, 'plugin_action_links' ) );
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
add_filter( 'wpmu_drop_tables', array( __CLASS__, 'wpmu_drop_tables' ) );
@ -834,67 +833,6 @@ CREATE TABLE {$wpdb->prefix}woocommerce_termmeta (
}
}
/**
* Show plugin changes. Code adapted from W3 Total Cache.
*
* @param array $args
*/
public static function in_plugin_update_message( $args ) {
$transient_name = 'wc_upgrade_notice_' . $args['Version'];
if ( false === ( $upgrade_notice = get_transient( $transient_name ) ) ) {
$response = wp_safe_remote_get( 'https://plugins.svn.wordpress.org/woocommerce/trunk/readme.txt' );
if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
$upgrade_notice = self::parse_update_notice( $response['body'], $args['new_version'] );
set_transient( $transient_name, $upgrade_notice, DAY_IN_SECONDS );
}
}
echo apply_filters( 'woocommerce_in_plugin_update_message', wp_kses_post( $upgrade_notice ) );
}
/**
* Parse update notice from readme file.
*
* @param string $content
* @param string $new_version
* @return string
*/
private static function parse_update_notice( $content, $new_version ) {
// Output Upgrade Notice.
$matches = null;
$regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote( WC_VERSION ) . '\s*=|$)~Uis';
$upgrade_notice = '';
if ( preg_match( $regexp, $content, $matches ) ) {
$notices = (array) preg_split( '~[\r\n]+~', trim( $matches[2] ) );
// Convert the full version strings to minor versions.
$notice_version_parts = explode( '.', trim( $matches[1] ) );
$current_version_parts = explode( '.', WC_VERSION );
if ( 3 !== sizeof( $notice_version_parts ) ) {
return;
}
$notice_version = $notice_version_parts[0] . '.' . $notice_version_parts[1];
$current_version = $current_version_parts[0] . '.' . $current_version_parts[1];
// Check the latest stable version and ignore trunk.
if ( version_compare( $current_version, $notice_version, '<' ) ) {
$upgrade_notice .= '</p><p class="wc_plugin_upgrade_notice">';
foreach ( $notices as $index => $line ) {
$upgrade_notice .= preg_replace( '~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line );
}
}
}
return wp_kses_post( $upgrade_notice );
}
/**
* Show action links on the plugin screen.
*