khaihihi
This commit is contained in:
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die( '-1' );
|
||||
}
|
||||
|
||||
/**
|
||||
* WPBakery WPBakery Page Builder updater
|
||||
*
|
||||
* @package WPBakeryPageBuilder
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Vc updating manager.
|
||||
*/
|
||||
class Vc_Updater {
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $version_url = 'http://updates.wpbakery.com/';
|
||||
|
||||
/**
|
||||
* Proxy URL that returns real download link
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $download_link_url = 'http://support.wpbakery.com/updates/download-link';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $title = 'WPBakery Page Builder';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $auto_updater;
|
||||
|
||||
public function init() {
|
||||
add_filter( 'upgrader_pre_download', array(
|
||||
$this,
|
||||
'preUpgradeFilter',
|
||||
), 10, 4 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for manager updater.
|
||||
*
|
||||
* @param Vc_Updating_Manager $updater
|
||||
*/
|
||||
public function setUpdateManager( Vc_Updating_Manager $updater ) {
|
||||
$this->auto_updater = $updater;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for manager updater.
|
||||
*
|
||||
* @return Vc_Updating_Manager|bool
|
||||
*/
|
||||
public function updateManager() {
|
||||
return $this->auto_updater;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get url for version validation
|
||||
* @return string
|
||||
*/
|
||||
public function versionUrl() {
|
||||
return $this->version_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get unique, short-lived download link
|
||||
*
|
||||
* @return array|boolean JSON response or false if request failed
|
||||
*/
|
||||
public function getDownloadUrl() {
|
||||
$url = $this->getUrl();
|
||||
// FIX SSL SNI
|
||||
$filter_add = true;
|
||||
if ( function_exists( 'curl_version' ) ) {
|
||||
$version = curl_version();
|
||||
if ( version_compare( $version['version'], '7.18', '>=' ) ) {
|
||||
$filter_add = false;
|
||||
}
|
||||
}
|
||||
if ( $filter_add ) {
|
||||
add_filter( 'https_ssl_verify', '__return_false' );
|
||||
}
|
||||
$response = wp_remote_get( $url, array( 'timeout' => 30 ) );
|
||||
|
||||
if ( $filter_add ) {
|
||||
remove_filter( 'https_ssl_verify', '__return_false' );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return json_decode( $response['body'], true );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getUrl() {
|
||||
$host = esc_url( vc_license()->getSiteUrl() );
|
||||
$key = rawurlencode( vc_license()->getLicenseKey() );
|
||||
|
||||
$url = $this->download_link_url . '?product=vc&url=' . $host . '&key=' . $key . '&version=' . WPB_VC_VERSION;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|void
|
||||
*/
|
||||
public static function getUpdaterUrl() {
|
||||
return vc_is_network_plugin() ? network_admin_url( 'admin.php?page=vc-updater' ) : admin_url( 'admin.php?page=vc-updater' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get link to newest VC
|
||||
*
|
||||
* @param $reply
|
||||
* @param $package
|
||||
* @param WP_Upgrader $updater
|
||||
*
|
||||
* @return mixed|string|WP_Error
|
||||
*/
|
||||
public function preUpgradeFilter( $reply, $package, $updater ) {
|
||||
$condition1 = isset( $updater->skin->plugin ) && vc_plugin_name() === $updater->skin->plugin;
|
||||
$condition2 = isset( $updater->skin->plugin_info ) && $updater->skin->plugin_info['Name'] === $this->title;
|
||||
if ( ! $condition1 && ! $condition2 ) {
|
||||
return $reply;
|
||||
}
|
||||
|
||||
$res = $updater->fs_connect( array( WP_CONTENT_DIR ) );
|
||||
if ( ! $res ) {
|
||||
return new WP_Error( 'no_credentials', esc_html__( "Error! Can't connect to filesystem", 'js_composer' ) );
|
||||
}
|
||||
|
||||
if ( ! vc_license()->isActivated() ) {
|
||||
if ( vc_is_as_theme() && vc_get_param( 'action' ) !== 'update-selected' ) {
|
||||
return false;
|
||||
}
|
||||
$url = self::getUpdaterUrl();
|
||||
|
||||
return new WP_Error( 'no_credentials', sprintf( esc_html__( 'To receive automatic updates license activation is required. Please visit %sSettings%s to activate your WPBakery Page Builder.', 'js_composer' ), '<a href="' . esc_url( $url ) . '" target="_blank">', '</a>' ) . ' ' . sprintf( ' <a href="https://go.wpbakery.com/faq-update-in-theme" target="_blank">%s</a>', esc_html__( 'Got WPBakery Page Builder in theme?', 'js_composer' ) ) );
|
||||
}
|
||||
|
||||
$updater->strings['downloading_package_url'] = esc_html__( 'Getting download link...', 'js_composer' );
|
||||
$updater->skin->feedback( 'downloading_package_url' );
|
||||
|
||||
$response = $this->getDownloadUrl();
|
||||
|
||||
if ( ! $response ) {
|
||||
return new WP_Error( 'no_credentials', esc_html__( 'Download link could not be retrieved', 'js_composer' ) );
|
||||
}
|
||||
|
||||
if ( ! $response['status'] ) {
|
||||
return new WP_Error( 'no_credentials', $response['error'] );
|
||||
}
|
||||
|
||||
$updater->strings['downloading_package'] = esc_html__( 'Downloading package...', 'js_composer' );
|
||||
$updater->skin->feedback( 'downloading_package' );
|
||||
|
||||
$downloaded_archive = download_url( $response['url'] );
|
||||
if ( is_wp_error( $downloaded_archive ) ) {
|
||||
return $downloaded_archive;
|
||||
}
|
||||
|
||||
$plugin_directory_name = dirname( vc_plugin_name() );
|
||||
|
||||
// WP will use same name for plugin directory as archive name, so we have to rename it
|
||||
if ( basename( $downloaded_archive, '.zip' ) !== $plugin_directory_name ) {
|
||||
$new_archive_name = dirname( $downloaded_archive ) . '/' . $plugin_directory_name . time() . '.zip';
|
||||
if ( rename( $downloaded_archive, $new_archive_name ) ) {
|
||||
$downloaded_archive = $new_archive_name;
|
||||
}
|
||||
}
|
||||
|
||||
return $downloaded_archive;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
die( '-1' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage update messages and Plugins info for VC in default WordPress plugins list.
|
||||
*/
|
||||
class Vc_Updating_Manager {
|
||||
/**
|
||||
* The plugin current version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $current_version;
|
||||
|
||||
/**
|
||||
* The plugin remote update path
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $update_path;
|
||||
|
||||
/**
|
||||
* Plugin Slug (plugin_directory/plugin_file.php)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $plugin_slug;
|
||||
|
||||
/**
|
||||
* Plugin name (plugin_file)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $slug;
|
||||
/**
|
||||
* Link to download VC.
|
||||
* @var string
|
||||
*/
|
||||
protected $url = 'http://go.wpbakery.com/wpb-buy';
|
||||
|
||||
/**
|
||||
* Initialize a new instance of the WordPress Auto-Update class
|
||||
*
|
||||
* @param string $current_version
|
||||
* @param string $update_path
|
||||
* @param string $plugin_slug
|
||||
*/
|
||||
public function __construct( $current_version, $update_path, $plugin_slug ) {
|
||||
// Set the class public variables
|
||||
$this->current_version = $current_version;
|
||||
$this->update_path = $update_path;
|
||||
$this->plugin_slug = $plugin_slug;
|
||||
$t = explode( '/', $plugin_slug );
|
||||
$this->slug = str_replace( '.php', '', $t[1] );
|
||||
|
||||
// define the alternative API for updating checking
|
||||
add_filter( 'pre_set_site_transient_update_plugins', array(
|
||||
$this,
|
||||
'check_update',
|
||||
) );
|
||||
|
||||
// Define the alternative response for information checking
|
||||
add_filter( 'plugins_api', array(
|
||||
$this,
|
||||
'check_info',
|
||||
), 10, 3 );
|
||||
|
||||
add_action( 'in_plugin_update_message-' . vc_plugin_name(), array(
|
||||
$this,
|
||||
'addUpgradeMessageLink',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add our self-hosted autoupdate plugin to the filter transient
|
||||
*
|
||||
* @param $transient
|
||||
*
|
||||
* @return object $ transient
|
||||
*/
|
||||
public function check_update( $transient ) {
|
||||
// Extra check for 3rd plugins
|
||||
if ( isset( $transient->response[ $this->plugin_slug ] ) ) {
|
||||
return $transient;
|
||||
}
|
||||
// Get the remote version
|
||||
$remote_version = $this->getRemote_version();
|
||||
|
||||
// If a newer version is available, add the update
|
||||
if ( version_compare( $this->current_version, $remote_version, '<' ) ) {
|
||||
$obj = new stdClass();
|
||||
$obj->slug = $this->slug;
|
||||
$obj->new_version = $remote_version;
|
||||
$obj->plugin = $this->plugin_slug;
|
||||
$obj->url = '';
|
||||
$obj->package = vc_license()->isActivated();
|
||||
$obj->name = vc_updater()->title;
|
||||
$transient->response[ $this->plugin_slug ] = $obj;
|
||||
}
|
||||
|
||||
return $transient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add our self-hosted description to the filter
|
||||
*
|
||||
* @param bool $false
|
||||
* @param array $action
|
||||
* @param object $arg
|
||||
*
|
||||
* @return bool|object
|
||||
*/
|
||||
public function check_info( $false, $action, $arg ) {
|
||||
if ( isset( $arg->slug ) && $arg->slug === $this->slug ) {
|
||||
$information = $this->getRemote_information();
|
||||
$array_pattern = array(
|
||||
'/^([\*\s])*(\d\d\.\d\d\.\d\d\d\d[^\n]*)/m',
|
||||
'/^\n+|^[\t\s]*\n+/m',
|
||||
'/\n/',
|
||||
);
|
||||
$array_replace = array(
|
||||
'<h4>$2</h4>',
|
||||
'</div><div>',
|
||||
'</div><div>',
|
||||
);
|
||||
$information->name = vc_updater()->title;
|
||||
$information->sections = (array) $information->sections;
|
||||
$information->sections['changelog'] = '<div>' . preg_replace( $array_pattern, $array_replace, $information->sections['changelog'] ) . '</div>';
|
||||
|
||||
return $information;
|
||||
}
|
||||
|
||||
return $false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the remote version
|
||||
*
|
||||
* @return string $remote_version
|
||||
*/
|
||||
public function getRemote_version() {
|
||||
// FIX SSL SNI
|
||||
$filter_add = true;
|
||||
if ( function_exists( 'curl_version' ) ) {
|
||||
$version = curl_version();
|
||||
if ( version_compare( $version['version'], '7.18', '>=' ) ) {
|
||||
$filter_add = false;
|
||||
}
|
||||
}
|
||||
if ( $filter_add ) {
|
||||
add_filter( 'https_ssl_verify', '__return_false' );
|
||||
}
|
||||
$request = wp_remote_get( $this->update_path, array( 'timeout' => 30 ) );
|
||||
|
||||
if ( $filter_add ) {
|
||||
remove_filter( 'https_ssl_verify', '__return_false' );
|
||||
}
|
||||
if ( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
|
||||
return $request['body'];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information about the remote version
|
||||
*
|
||||
* @return bool|object
|
||||
*/
|
||||
public function getRemote_information() {
|
||||
// FIX SSL SNI
|
||||
$filter_add = true;
|
||||
if ( function_exists( 'curl_version' ) ) {
|
||||
$version = curl_version();
|
||||
if ( version_compare( $version['version'], '7.18', '>=' ) ) {
|
||||
$filter_add = false;
|
||||
}
|
||||
}
|
||||
if ( $filter_add ) {
|
||||
add_filter( 'https_ssl_verify', '__return_false' );
|
||||
}
|
||||
$request = wp_remote_get( $this->update_path . 'information.json', array( 'timeout' => 30 ) );
|
||||
|
||||
if ( $filter_add ) {
|
||||
remove_filter( 'https_ssl_verify', '__return_false' );
|
||||
}
|
||||
if ( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
|
||||
return json_decode( $request['body'] );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows message on Wp plugins page with a link for updating from envato.
|
||||
*/
|
||||
public function addUpgradeMessageLink() {
|
||||
$is_activated = vc_license()->isActivated();
|
||||
if ( ! $is_activated ) {
|
||||
$url = vc_updater()->getUpdaterUrl();
|
||||
|
||||
echo sprintf( ' ' . esc_html__( 'To receive automatic updates license activation is required. Please visit %ssettings%s to activate your WPBakery Page Builder.', 'js_composer' ), '<a href="' . esc_url( $url ) . '" target="_blank">', '</a>' ) . sprintf( ' <a href="https://go.wpbakery.com/faq-update-in-theme" target="_blank">%s</a>', esc_html__( 'Got WPBakery Page Builder in theme?', 'js_composer' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user