Restore UM templates after theme update

This commit is contained in:
denisbaranov
2019-04-10 15:39:05 +03:00
parent a3fb633b5c
commit 863f00ffee
2 changed files with 207 additions and 0 deletions
@@ -0,0 +1,193 @@
<?php
namespace um\admin\core;
// Exit if accessed directly.
if ( !defined( 'ABSPATH' ) ) {
exit;
}
if ( !class_exists( 'um\admin\core\Admin_Theme_Updater' ) ) {
/**
* Class Admin_Theme_Updater
* @package um\admin\core
*/
class Admin_Theme_Updater {
/**
* When enable this functionality?
* @var array
*/
private $actions = array(
'do-theme-upgrade',
'update-selected-themes',
'update-theme',
);
/**
* Restored themes
* @var array
*/
private $restored = array();
/**
* Saved themes
* @var array
*/
private $saved = array();
/**
* Constructor.
*/
public function __construct() {
if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], $this->actions ) ) {
add_filter( 'upgrader_package_options', array( $this, 'upgrader_package_options' ), 40 );
add_action( 'upgrader_process_complete', array( $this, 'upgrader_process_complete' ), 40, 2 );
}
}
/**
* Copy directory
* @param string $src
* @param string $dest
*/
public static function recurse_copy( $src, $dest ) {
if ( !is_dir( $dest ) ) {
@mkdir( $dest, 0777, true );
}
$dir = opendir( $src );
while ( false !== ( $file = readdir( $dir )) ) {
if ( ( $file != '.' ) && ( $file != '..' ) ) {
if ( is_dir( $src . DIRECTORY_SEPARATOR . $file ) ) {
self::recurse_copy( $src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file );
}
else {
copy( $src . DIRECTORY_SEPARATOR . $file, $dest . DIRECTORY_SEPARATOR . $file );
}
}
}
closedir( $dir );
}
/**
* Restore UM templates to theme directory
* @param type $name
* @return type
*/
public function restore_templates( $name = '' ) {
$theme = wp_get_theme();
if ( $name && $name !== $theme->template ) {
return;
}
if ( isset( $this->restored[$theme->get( 'Name' )] ) ) {
return;
}
if ( empty( $this->saved[$theme->get( 'Name' )] ) ) {
return;
}
$old_version = get_option( 'theme_version ' . $theme->get( 'Name' ) );
$version = $theme->get( 'Version' );
if ( $old_version === $version ) {
return;
}
$temp_dir = UM()->uploader()->get_core_temp_dir() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $theme->template;
if ( !is_dir( $temp_dir ) ) {
return;
}
$um_dir = $theme->get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'ultimate-member';
@mkdir( $um_dir, 0777, true );
$src = realpath( $temp_dir );
$dest = realpath( $um_dir );
if ( $src && $dest ) {
self::recurse_copy( $src, $dest );
UM()->files()->remove_dir( $src );
}
else {
error_log( "UM Error. Can not restore theme templates." );
}
update_option( 'theme_version ' . $theme->get( 'Name' ), $theme->get( 'Version' ) );
$this->restored[$theme->get( 'Name' )] = $theme->get( 'Version' );
}
/**
* Save UM templates to temp directory
* @param type $name
* @return type
*/
public function save_templates( $name = '' ) {
$theme = wp_get_theme();
if ( $name && $name !== $theme->template ) {
return;
}
if ( isset( $this->restored[$theme->get( 'Name' )] ) ) {
return;
}
if ( isset( $this->saved[$theme->get( 'Name' )] ) ) {
return;
}
$um_dir = $theme->get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'ultimate-member';
if ( !is_dir( $um_dir ) ) {
return;
}
$temp_dir = UM()->uploader()->get_core_temp_dir() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $theme->template;
@mkdir( $temp_dir, 0777, true );
$src = realpath( $um_dir );
$dest = realpath( $temp_dir );
if ( $src && $dest ) {
self::recurse_copy( $src, $dest );
}
else {
error_log( "UM Error. Can not save theme templates." );
}
update_option( 'theme_version ' . $theme->get( 'Name' ), $theme->get( 'Version' ) );
$this->saved[$theme->get( 'Name' )] = $theme->get( 'Version' );
}
/**
* Filter: upgrader_package_options
*
* @param array $options
* @return array
*/
public function upgrader_package_options( $options ) {
if ( isset( $options['hook_extra'] ) && isset( $options['hook_extra']['theme'] ) ) {
$this->save_templates( $options['hook_extra']['theme'] );
}
return $options;
}
/**
* Action: upgrader_process_complete
*
* @param WP_Upgrader $WP_Upgrader
* @param array $options
*/
public function upgrader_process_complete( $WP_Upgrader, $options ) {
if ( isset( $options['themes'] ) && is_array( $options['themes'] ) ) {
foreach ( $options['themes'] as $theme ) {
$this->restore_templates( $theme );
}
}
}
}
}
+14
View File
@@ -509,6 +509,7 @@ if ( ! class_exists( 'UM' ) ) {
$this->columns();
$this->notices();
$this->admin_navmenu();
$this->theme_updater();
} elseif ( $this->is_request( 'admin' ) ) {
$this->admin();
$this->admin_menu();
@@ -523,6 +524,7 @@ if ( ! class_exists( 'UM' ) ) {
$this->plugin_updater();
$this->admin_gdpr();
$this->admin_navmenu();
$this->theme_updater();
} elseif ( $this->is_request( 'frontend' ) ) {
$this->enqueue();
$this->account();
@@ -645,6 +647,18 @@ if ( ! class_exists( 'UM' ) ) {
}
/**
* @since 2.0.45
* @return um\admin\core\Admin_Theme_Updater()
*/
function theme_updater() {
if ( empty( $this->classes['theme_updater'] ) ) {
$this->classes['theme_updater'] = new um\admin\core\Admin_Theme_Updater();
}
return $this->classes['theme_updater'];
}
/**
* @since 2.0
*/