mainwp-child/class/class-mainwp-child-staging.php

443 lines
13 KiB
PHP
Raw Normal View History

2017-12-04 21:55:05 +01:00
<?php
/*
*
* Credits
*
* Plugin-Name: WP Staging
* Plugin URI: https://wordpress.org/plugins/wp-staging
* Author: WP-Staging
* Author URI: https://wp-staging.com
* Contributors: ReneHermi, ilgityildirim
*
* The code is used for the MainWP Staging Extension
* Extension URL: https://mainwp.com/extension/staging/
*
*/
2017-12-04 21:55:05 +01:00
class MainWP_Child_Staging {
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public static $instance = null;
public $is_plugin_installed = false;
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
static function Instance() {
2020-03-26 14:05:04 +00:00
if ( null === self::$instance ) {
self::$instance = new MainWP_Child_Staging();
2017-12-04 21:55:05 +01:00
}
2020-03-26 14:05:04 +00:00
return self::$instance;
2017-12-04 21:55:05 +01:00
}
2018-11-14 21:33:36 +07:00
public function __construct() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
2017-12-04 21:55:05 +01:00
if ( is_plugin_active( 'wp-staging/wp-staging.php' ) && defined('WPSTG_PLUGIN_DIR')) {
2019-05-06 22:42:18 +07:00
$this->is_plugin_installed = true;
} elseif ( is_plugin_active( 'wp-staging-pro/wp-staging-pro.php' ) ) {
2018-11-14 21:33:36 +07:00
$this->is_plugin_installed = true;
}
if (!$this->is_plugin_installed) {
2018-11-14 21:33:36 +07:00
return;
}
2018-11-14 21:33:36 +07:00
add_filter( 'mainwp-site-sync-others-data', array( $this, 'syncOthersData' ), 10, 2 );
2017-12-04 21:55:05 +01:00
}
2018-11-14 21:33:36 +07:00
public function init() {
if ( get_option( 'mainwp_wp_staging_ext_enabled' ) !== 'Y' ) {
2018-11-14 21:33:36 +07:00
return;
}
2018-11-14 21:33:36 +07:00
if (!$this->is_plugin_installed) {
2018-11-14 21:33:36 +07:00
return;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
if ( get_option( 'mainwp_wp_staging_hide_plugin' ) === 'hide' ) {
add_filter( 'all_plugins', array( $this, 'all_plugins' ) );
add_action( 'admin_menu', array( $this, 'remove_menu' ) );
add_filter( 'site_transient_update_plugins', array( &$this, 'remove_update_nag' ) );
add_filter( 'mainwp_child_hide_update_notice', array( &$this, 'hide_update_notice' ) );
2017-12-04 21:55:05 +01:00
}
}
2018-11-14 21:33:36 +07:00
public function syncOthersData( $information, $data = array() ) {
if ( isset( $data['syncWPStaging'] ) && $data['syncWPStaging'] ) {
2018-06-26 19:52:53 +02:00
try{
$information['syncWPStaging'] = $this->get_sync_data();
} catch(Exception $e) {
// do not exit
}
2018-11-14 21:33:36 +07:00
}
2018-06-26 19:52:53 +02:00
return $information;
}
// ok
2018-11-14 21:33:36 +07:00
public function get_sync_data() {
2017-12-04 21:55:05 +01:00
return $this->get_overview();
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public function action() {
if (!$this->is_plugin_installed) {
MainWP_Helper::write( array('error' => 'Please install WP Staging plugin on child website') );
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
if (!class_exists( 'WPStaging\WPStaging' )){
2020-03-26 14:05:04 +00:00
require_once WPSTG_PLUGIN_DIR . 'apps/Core/WPStaging.php';
2017-12-04 21:55:05 +01:00
}
\WPStaging\WPStaging::getInstance();
2018-11-14 21:33:36 +07:00
$information = array();
2017-12-04 21:55:05 +01:00
if (get_option( 'mainwp_wp_staging_ext_enabled' ) !== 'Y') {
2018-11-14 21:33:36 +07:00
MainWP_Helper::update_option( 'mainwp_wp_staging_ext_enabled', 'Y', 'yes' );
2017-12-04 21:55:05 +01:00
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
if ( isset( $_POST['mwp_action'] ) ) {
switch ( $_POST['mwp_action'] ) {
case 'set_showhide':
$information = $this->set_showhide();
2018-11-14 21:33:36 +07:00
break;
2017-12-04 21:55:05 +01:00
case 'save_settings':
$information = $this->save_settings();
2018-11-14 21:33:36 +07:00
break;
2017-12-04 21:55:05 +01:00
case 'get_overview':
$information = $this->get_overview();
break;
case 'get_scan':
$information = $this->get_scan();
break;
case 'check_disk_space':
$information = $this->ajaxCheckFreeSpace();
break;
case 'check_clone':
$information = $this->ajaxCheckCloneName();
break;
case 'start_clone':
$information = $this->ajaxStartClone();
break;
case 'clone_database':
$information = $this->ajaxCloneDatabase();
break;
case 'prepare_directories':
$information = $this->ajaxPrepareDirectories();
break;
case 'copy_files':
$information = $this->ajaxCopyFiles();
break;
case 'replace_data':
$information = $this->ajaxReplaceData();
break;
case 'clone_finish':
$information = $this->ajaxFinish();
2018-11-14 21:33:36 +07:00
break;
2017-12-04 21:55:05 +01:00
case 'delete_confirmation':
$information = $this->ajaxDeleteConfirmation();
break;
case 'delete_clone':
$information = $this->ajaxDeleteClone();
break;
case 'cancel_clone':
$information = $this->ajaxCancelClone();
break;
2018-04-17 20:33:10 +02:00
case 'staging_update':
$information = $this->ajaxUpdateProcess();
break;
case 'cancel_update':
$information = $this->ajaxCancelUpdate();
2018-11-14 21:33:36 +07:00
break;
2017-12-04 21:55:05 +01:00
}
}
MainWP_Helper::write( $information );
2018-11-14 21:33:36 +07:00
}
2017-12-04 21:55:05 +01:00
function set_showhide() {
$hide = isset( $_POST['showhide'] ) && ( 'hide' === $_POST['showhide'] ) ? 'hide' : '';
MainWP_Helper::update_option( 'mainwp_wp_staging_hide_plugin', $hide, 'yes' );
$information['result'] = 'SUCCESS';
return $information;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
function save_settings() {
2018-11-14 21:33:36 +07:00
$settings = $_POST['settings'];
2017-12-04 21:55:05 +01:00
$filters = array(
'queryLimit',
'fileLimit',
'batchSize',
'cpuLoad',
'delayRequests',
2017-12-04 21:55:05 +01:00
'disableAdminLogin',
2018-11-14 21:33:36 +07:00
'querySRLimit',
'maxFileSize',
//'wpSubDirectory', // removed
2017-12-04 21:55:05 +01:00
'debugMode',
'unInstallOnDelete',
2018-04-17 20:33:10 +02:00
'checkDirectorySize',
'optimizer',
2018-11-14 21:33:36 +07:00
//'loginSlug' // removed
2017-12-04 21:55:05 +01:00
);
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
$save_fields = array();
foreach($filters as $field) {
if (isset($settings[$field])) {
$save_fields[$field] = $settings[$field];
}
}
update_option('wpstg_settings', $save_fields );
return array('result' => 'success');
2018-11-14 21:33:36 +07:00
}
public function get_overview() {
2017-12-04 21:55:05 +01:00
$return = array(
2020-03-26 14:05:04 +00:00
'availableClones' => get_option( 'wpstg_existing_clones_beta', array()),
2017-12-04 21:55:05 +01:00
);
return $return;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public function get_scan() {
// Scan
$scan = new WPStaging\Backend\Modules\Jobs\Scan();
$scan->start();
// Get Options
$options = $scan->getOptions();
$return = array(
'options' => serialize($options),
'directoryListing' => $scan->directoryListing(),
2020-03-26 14:05:04 +00:00
'prefix' => WPStaging\WPStaging::getTablePrefix(),
2017-12-04 21:55:05 +01:00
);
return $return;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public function ajaxCheckCloneName() {
2020-03-26 14:05:04 +00:00
$cloneName = sanitize_key( $_POST['cloneID'] );
2017-12-04 21:55:05 +01:00
$cloneNameLength = strlen( $cloneName );
2020-03-26 14:05:04 +00:00
$clones = get_option( 'wpstg_existing_clones_beta', array() );
2017-12-04 21:55:05 +01:00
// Check clone name length
if( $cloneNameLength < 1 || $cloneNameLength > 16 ) {
echo array(
2020-03-26 14:05:04 +00:00
'status' => 'failed',
'message' => 'Clone name must be between 1 - 16 characters',
2017-12-04 21:55:05 +01:00
);
} elseif( array_key_exists( $cloneName, $clones ) ) {
return array(
2020-03-26 14:05:04 +00:00
'status' => 'failed',
'message' => 'Clone name is already in use, please choose an another clone name',
2017-12-04 21:55:05 +01:00
);
}
2020-03-26 14:05:04 +00:00
return array('status' => 'success');
2017-12-04 21:55:05 +01:00
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public function ajaxStartClone() {
2018-04-17 20:33:10 +02:00
$this->url = ''; // to fix warning
$cloning = new WPStaging\Backend\Modules\Jobs\Cloning();
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
if( !$cloning->save() ) {
return;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
ob_start();
2020-03-26 14:05:04 +00:00
require_once WPSTG_PLUGIN_DIR . 'apps/Backend/views/clone/ajax/start.php';
2017-12-04 21:55:05 +01:00
$result = ob_get_clean();
return $result;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public function ajaxCloneDatabase() {
$cloning = new WPStaging\Backend\Modules\Jobs\Cloning();
return $cloning->start();
}
/**
* Ajax Prepare Directories (get listing of files)
*/
public function ajaxPrepareDirectories() {
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
$cloning = new WPStaging\Backend\Modules\Jobs\Cloning();
return $cloning->start();
}
/**
* Ajax Clone Files
*/
public function ajaxCopyFiles() {
$cloning = new WPStaging\Backend\Modules\Jobs\Cloning();
return $cloning->start();
}
/**
* Ajax Replace Data
*/
public function ajaxReplaceData() {
$cloning = new WPStaging\Backend\Modules\Jobs\Cloning();
return $cloning->start();
}
/**
* Ajax Finish
*/
public function ajaxFinish() {
$cloning = new WPStaging\Backend\Modules\Jobs\Cloning();
$this->url = ''; // to fix warning
$return = $cloning->start();
2020-03-26 14:05:04 +00:00
$return->blogInfoName = get_bloginfo('name');
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
return $return;
}
/**
* Ajax Delete Confirmation
*/
public function ajaxDeleteConfirmation() {
$delete = new WPStaging\Backend\Modules\Jobs\Delete();
$delete->setData();
2018-11-14 21:33:36 +07:00
$clone = $delete->getClone();
2017-12-04 21:55:05 +01:00
$result = array(
'clone' => $clone,
'deleteTables' => $delete->getTables(),
);
2018-11-14 21:33:36 +07:00
return $result;
2017-12-04 21:55:05 +01:00
}
/**
* Delete clone
*/
public function ajaxDeleteClone() {
$delete = new WPStaging\Backend\Modules\Jobs\Delete();
return $delete->start();
}
/**
* Delete clone
*/
public function ajaxCancelClone() {
$cancel = new WPStaging\Backend\Modules\Jobs\Cancel();
return $cancel->start();
}
2018-11-14 21:33:36 +07:00
public function ajaxCancelUpdate() {
2018-04-17 20:33:10 +02:00
$cancel = new WPStaging\Backend\Modules\Jobs\CancelUpdate();
return $cancel->start();
}
public function ajaxUpdateProcess() {
2018-11-14 21:33:36 +07:00
$cloning = new WPStaging\Backend\Modules\Jobs\Updating();
2017-12-04 21:55:05 +01:00
2018-04-17 20:33:10 +02:00
if( !$cloning->save() ) {
return;
}
ob_start();
2020-03-26 14:05:04 +00:00
require_once WPSTG_PLUGIN_DIR . 'apps/Backend/views/clone/ajax/update.php';
2018-04-17 20:33:10 +02:00
$result = ob_get_clean();
2018-11-14 21:33:36 +07:00
return $result;
2018-04-17 20:33:10 +02:00
}
2018-11-14 21:33:36 +07:00
public function ajaxCheckFreeSpace() {
2017-12-04 21:55:05 +01:00
return $this->hasFreeDiskSpace();
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
// from wp-staging plugin
public function hasFreeDiskSpace() {
2020-03-26 14:05:04 +00:00
if( !function_exists( 'disk_free_space' ) ) {
2017-12-04 21:55:05 +01:00
return null;
}
$freeSpace = @disk_free_space( ABSPATH );
if( false === $freeSpace ) {
$data = array(
'freespace' => false,
2020-03-26 14:05:04 +00:00
'usedspace' => $this->formatSize($this->getDirectorySizeInclSubdirs(ABSPATH)),
2017-12-04 21:55:05 +01:00
);
2018-11-14 21:33:36 +07:00
return $data;
2017-12-04 21:55:05 +01:00
}
$data = array(
'freespace' => $this->formatSize($freeSpace),
2020-03-26 14:05:04 +00:00
'usedspace' => $this->formatSize($this->getDirectorySizeInclSubdirs(ABSPATH)),
2017-12-04 21:55:05 +01:00
);
2018-11-14 21:33:36 +07:00
return $data;
2017-12-04 21:55:05 +01:00
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
// from wp-staging plugin
function getDirectorySizeInclSubdirs( $dir ) {
$size = 0;
foreach ( glob( rtrim( $dir, '/' ) . '/*', GLOB_NOSORT ) as $each ) {
$size += is_file( $each ) ? filesize( $each ) : $this->getDirectorySizeInclSubdirs( $each );
}
return $size;
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
// from wp-staging plugin
2020-03-26 14:05:04 +00:00
public function formatSize( $bytes, $precision = 2) {
if ( (float) $bytes < 1)
2017-12-04 21:55:05 +01:00
{
return '';
}
2020-03-26 14:05:04 +00:00
$units = array('B', 'KB', 'MB', 'GB', 'TB');
2017-12-04 21:55:05 +01:00
2020-03-26 14:05:04 +00:00
$bytes = (float) $bytes;
2017-12-04 21:55:05 +01:00
$base = log($bytes) / log(1000); // 1024 would be for MiB KiB etc
$pow = pow(1000, $base - floor($base)); // Same rule for 1000
2020-03-26 14:05:04 +00:00
return round($pow, $precision) . ' ' . $units[ (int) floor($base)];
2017-12-04 21:55:05 +01:00
}
2018-11-14 21:33:36 +07:00
2017-12-04 21:55:05 +01:00
public function all_plugins( $plugins ) {
foreach ( $plugins as $key => $value ) {
$plugin_slug = basename( $key, '.php' );
if ( 'wp-staging' === $plugin_slug ) {
unset( $plugins[ $key ] );
}
}
return $plugins;
}
public function remove_menu() {
remove_menu_page( 'wpstg_clone' );
$pos = stripos( $_SERVER['REQUEST_URI'], 'admin.php?page=wpstg_clone' );
if ( false !== $pos ) {
wp_redirect( get_option( 'siteurl' ) . '/wp-admin/index.php' );
exit();
}
}
2018-11-14 21:33:36 +07:00
function hide_update_notice( $slugs ) {
$slugs[] = 'wp-staging/wp-staging.php';
return $slugs;
}
2017-12-04 21:55:05 +01:00
function remove_update_nag( $value ) {
if ( isset( $_POST['mainwpsignature'] ) ) {
return $value;
}
if (! MainWP_Helper::is_screen_with_update()) {
return $value;
}
2017-12-04 21:55:05 +01:00
if ( isset( $value->response['wp-staging/wp-staging.php'] ) ) {
unset( $value->response['wp-staging/wp-staging.php'] );
}
2019-05-06 22:42:18 +07:00
if ( isset( $value->response['wp-staging-pro/wp-staging-pro.php'] ) ) {
unset( $value->response['wp-staging-pro/wp-staging-pro.php'] );
}
2017-12-04 21:55:05 +01:00
return $value;
}
}