Created
September 17, 2015 02:08
-
-
Save dtbaker/a13f302c4bb646a49892 to your computer and use it in GitHub Desktop.
Visual Composer update script for including in 3rd party themes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Visual Composer Update script for 3rd party Themes | |
* Details here: http://dtbaker.net/web-development/visual-composer-plugin-updates-in-3rd-party-themes/ | |
* Author: dtbaker | |
* | |
*/ | |
// ************* CHANGE ME *************** change me to your server side visual composer update URL | |
define('_VC_DTBAKER_UPDATE_URL','http://dtbaker.net/files/envato/vc_update.php'); | |
$dtbaker_vc_updater = new dtbaker_vc_3rd_party_update(); | |
class dtbaker_vc_3rd_party_update{ | |
public $current_theme_purchase_code = ''; | |
public $purchase_code_url = ''; | |
public function __construct() | |
{ | |
// set the license code of the currently purchased theme | |
// we ask for this code on another page (not this script) and store it in options. | |
$this->current_theme_purchase_code = get_option('_envato_licence_freedom',''); | |
// this is the URL where we ask the user for thiir theme license code. changethis to your own page. | |
// the user is prompted to go here if they haven't entered their theme license code and try to update visual composer | |
$this->purchase_code_url = admin_url( 'themes.php?page=dtbaker.theme_options_envato.php' ); | |
// tell Visual Composer we're in theme mode, as per: https://wpbakery.atlassian.net/wiki/pages/viewpage.action?pageId=524297 | |
add_action( 'vc_before_init', array($this,'dtbaker_vcSetAsTheme') ); | |
// try to disable the "Welcome to Visual Composer" screen when the plugin is first activated on theme install with TGMP | |
add_filter( 'pre_transient__wc_activation_redirect', array($this, 'dtbaker_vc_disable_welcome') ); | |
add_filter( 'pre_transient__vc_page_welcome_redirect', array($this, 'dtbaker_vc_disable_welcome') ); | |
// run our init method which overwrites all the visual composer update stuff | |
add_action('init', array($this, 'dtbaker_freedom_vc_init')); | |
} | |
function dtbaker_vcSetAsTheme() { | |
vc_set_as_theme(); | |
} | |
function dtbaker_vc_disable_welcome() { | |
return 0; | |
} | |
public $vc_updater = false; | |
public $vc_auto_updater = false; | |
function dtbaker_freedom_vc_init() | |
{ | |
global $vc_manager; | |
if ($vc_manager && function_exists('vc_plugin_name')) { | |
$this->vc_updater = $vc_manager->updater(); | |
if ($this->vc_updater) { | |
$this->vc_auto_updater = $this->vc_updater->updateManager(); | |
if ($this->vc_auto_updater) { | |
// overwrite the "Download from CodeCanyon" link and offer VC through our own system. | |
remove_action('in_plugin_update_message-' . vc_plugin_name(), array($this->vc_auto_updater, 'addUpgradeMessageLink')); | |
// remove the filter that downloads the update ZIP from CodeCanyon | |
remove_filter('upgrader_pre_download', array($this->vc_updater, 'upgradeFilterFromEnvato'), 10); | |
// add our own "Update Plugin" message to the plugin page: | |
add_action('in_plugin_update_message-' . vc_plugin_name(), array($this, 'addUpgradeMessageLink')); | |
// add our own download plugin update method: | |
add_filter('upgrader_pre_download', array($this, 'upgradeFilterFromMyServer'), 10, 4); | |
} | |
} | |
} | |
} | |
public function addUpgradeMessageLink(){ | |
echo '<style type="text/css" media="all">tr#wpbakery-visual-composer + tr.plugin-update-tr a.thickbox + em { display: none; }</style>'; | |
// update.php?action=upgrade-plugin&plugin=testimonials-widget%2Ftestimonials-widget.php&_wpnonce=6178d48b6e | |
echo '<a href="' . wp_nonce_url( admin_url( 'update.php?action=upgrade-plugin&plugin=' . vc_plugin_name() ), 'upgrade-plugin_' . vc_plugin_name() ) . '">' . __( 'Update Visual Composer now.', 'js_composer' ) . '</a>'; | |
} | |
public function upgradeFilterFromMyServer($reply, $package, $updater){ | |
// hit up my server for an update to this plugin (my server just bounces through to Envato) | |
// this is copied from js_composer class-vc-updater.php upgradeFilterFromEnvato() | |
global $wp_filesystem; | |
if ( ( isset( $updater->skin->plugin ) && $updater->skin->plugin === vc_plugin_name() ) || | |
( isset( $updater->skin->plugin_info ) && $updater->skin->plugin_info['Name'] === $this->vc_updater->title ) | |
) { | |
$updater->strings['download_envato'] = __( 'Downloading package from Envato market...', 'js_composer' ); | |
$updater->skin->feedback( 'download_envato' ); | |
$package_filename = 'js_composer.zip'; | |
$res = $updater->fs_connect( array( WP_CONTENT_DIR ) ); | |
if ( ! $res ) { | |
return new WP_Error( 'no_credentials', __( "Error! Can't connect to filesystem", 'js_composer' ) ); | |
} | |
$purchase_code = $this->current_theme_purchase_code; | |
if ( !$purchase_code ) { | |
return new WP_Error( 'no_credentials', __( 'To receive automatic updates license activation is required. Please visit <a href="' . $this->purchase_code_url . '' . '" target="_blank">Theme Settings</a> to activate your purchase.', 'js_composer' ) ); | |
} | |
$json = wp_remote_get( _VC_DTBAKER_UPDATE_URL.'?purchase=' . rawurlencode( $purchase_code ) ); | |
$result = json_decode( $json['body'], true ); | |
if ( empty( $result['download_url'] ) ) { | |
return new WP_Error( 'no_credentials', __( 'Error! Envato API error. Please ensure your purchase code is correct and try again later.', 'js_composer' ) . ( isset( $result['error'] ) ? ': ' . $result['error'] : '.' ) ); | |
} | |
$download_file = download_url( $result['download_url'] ); | |
if ( is_wp_error( $download_file ) ) { | |
return $download_file; | |
} | |
// the existing post_update hook in VC will cleanup this directory and remove it after | |
$upgrade_folder = $wp_filesystem->wp_content_dir() . 'uploads/js_composer_envato_package/'; | |
if ( is_dir( $upgrade_folder ) ) { | |
$wp_filesystem->delete( $upgrade_folder ); | |
} | |
$wp_filesystem->mkdir($upgrade_folder); | |
$result = @copy( $download_file, $upgrade_folder . $package_filename); | |
@unlink($download_file); | |
if ( $result && is_file( $upgrade_folder . '/' . $package_filename ) && filesize( $upgrade_folder . '/' . $package_filename ) > 0) { | |
return $upgrade_folder . '/' . $package_filename; | |
} | |
return new WP_Error( 'no_credentials', __( 'Error on downloading package', 'js_composer' ) ); | |
} | |
return $reply; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment