Refactoring

This commit is contained in:
thanghv 2020-05-20 16:51:25 +07:00
parent aa6ebc7421
commit d1e94b4bc5
6 changed files with 181 additions and 160 deletions

View file

@ -63,6 +63,20 @@ class MainWP_Child_Install {
}
}
} elseif ( 'delete' === $action ) {
$this->delete_plugins( $plugins );
} else {
$information['status'] = 'FAIL';
}
if ( ! isset( $information['status'] ) ) {
$information['status'] = 'SUCCESS';
}
$information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false );
mainwp_child_helper()->write( $information );
}
private function delete_plugins( $plugins ) {
global $mainWPChild;
include_once ABSPATH . '/wp-admin/includes/plugin.php';
if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
include_once ABSPATH . '/wp-admin/includes/screen.php';
@ -99,15 +113,7 @@ class MainWP_Child_Install {
}
}
}
} else {
$information['status'] = 'FAIL';
}
if ( ! isset( $information['status'] ) ) {
$information['status'] = 'SUCCESS';
}
$information['sync'] = MainWP_Child_Stats::get_instance()->get_site_stats( array(), false );
mainwp_child_helper()->write( $information );
}
public function theme_action() {
@ -227,27 +233,8 @@ class MainWP_Child_Install {
if ( is_wp_error( $result ) ) {
if ( true == $ssl_verify && strpos( $url, 'https://' ) === 0 ) {
add_filter( 'http_request_args', array( MainWP_Helper::get_class_name(), 'no_ssl_filter_function' ), 99, 2 );
$ssl_verify = false;
$result = $installer->run(
array(
'package' => $url,
'destination' => ( 'plugin' === $_POST['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes' ),
'clear_destination' => ( isset( $_POST['overwrite'] ) && $_POST['overwrite'] ),
'clear_working' => true,
'hook_extra' => array(),
)
);
}
if ( is_wp_error( $result ) ) {
$err_code = $result->get_error_code();
if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) {
$error = $result->get_error_data();
MainWP_Helper::error( $error, $err_code );
} else {
MainWP_Helper::error( implode( ', ', $error ), $err_code );
}
$result = $this->try_install_one_more( $url, $installer );
}
}
@ -263,7 +250,6 @@ class MainWP_Child_Install {
if ( 'plugin' === $_POST['type'] ) {
$path = $result['destination'];
$fileName = '';
$rslt = null;
wp_cache_set( 'plugins', array(), 'plugins' );
foreach ( $result['source_files'] as $srcFile ) {
if ( is_dir( $path . $srcFile ) ) {
@ -305,4 +291,26 @@ class MainWP_Child_Install {
mainwp_child_helper()->write( $information );
}
private function try_install_one_more( $url, $installer ) {
add_filter( 'http_request_args', array( MainWP_Helper::get_class_name(), 'no_ssl_filter_function' ), 99, 2 );
$result = $installer->run(
array(
'package' => $url,
'destination' => ( 'plugin' === $_POST['type'] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes' ),
'clear_destination' => ( isset( $_POST['overwrite'] ) && $_POST['overwrite'] ),
'clear_working' => true,
'hook_extra' => array(),
)
);
if ( is_wp_error( $result ) ) {
$err_code = $result->get_error_code();
if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) {
$error = $result->get_error_data();
MainWP_Helper::error( $error, $err_code );
} else {
MainWP_Helper::error( implode( ', ', $error ), $err_code );
}
}
return $result;
}
}

View file

@ -214,7 +214,7 @@ class MainWP_Child_Users {
}
public function edit_user( $user_id, $data ) {
public function edit_user( $user_id, $data ) { // phpcs:ignore -- ignore complex method notice, see detail at: function edit_user() in the wp/wp-admin/includes/user.php.
$wp_roles = wp_roles();
$user = new \stdClass();

View file

@ -238,37 +238,22 @@ class MainWP_Connect {
}
}
}
if ( isset( $_REQUEST['fdl'] ) ) {
if ( stristr( $_REQUEST['fdl'], '..' ) ) {
return;
}
MainWP_Utility::instance()->upload_file( $_REQUEST['fdl'], isset( $_REQUEST['foffset'] ) ? $_REQUEST['foffset'] : 0 );
exit;
}
$where = isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : '';
if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) {
$file = '';
if ( isset( $_POST['f'] ) ) {
$file = $_POST['f'];
} elseif ( isset( $_POST['file'] ) ) {
$file = $_POST['file'];
}
$where = 'admin.php?page=mainwp_child_tab&tab=restore-clone';
if ( '' === session_id() ) {
session_start();
}
$_SESSION['file'] = $file;
$_SESSION['size'] = $_POST['size'];
}
// to support open not wp-admin url.
$open_location = isset( $_REQUEST['open_location'] ) ? $_REQUEST['open_location'] : '';
if ( ! empty( $open_location ) ) {
$open_location = base64_decode( $open_location ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( isset( $_REQUEST['open_location'] ) ) {
$open_location = base64_decode( $_REQUEST['open_location'] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
$this->open_location_redirect( $open_location );
}
$this->where_redirect();
}
private function open_location_redirect( $open_location ) {
$_vars = MainWP_Helper::parse_query( $open_location );
$_path = wp_parse_url( $open_location, PHP_URL_PATH );
if ( isset( $_vars['_mwpNoneName'] ) && isset( $_vars['_mwpNoneValue'] ) ) {
@ -290,8 +275,24 @@ class MainWP_Connect {
exit();
}
wp_safe_redirect( admin_url( $where ) );
private function where_redirect() {
$where = isset( $_REQUEST['where'] ) ? $_REQUEST['where'] : '';
if ( isset( $_POST['f'] ) || isset( $_POST['file'] ) ) {
$file = '';
if ( isset( $_POST['f'] ) ) {
$file = $_POST['f'];
} elseif ( isset( $_POST['file'] ) ) {
$file = $_POST['file'];
}
$where = 'admin.php?page=mainwp_child_tab&tab=restore-clone';
if ( '' === session_id() ) {
session_start();
}
$_SESSION['file'] = $file;
$_SESSION['size'] = $_POST['size'];
}
wp_safe_redirect( admin_url( $where ) );
exit();
}

View file

@ -299,30 +299,18 @@ class MainWP_Custom_Post_Type {
private function insert_postmeta( $post_id, $data, $check_image_existed, $is_woocomerce ) {
foreach ( $data['postmeta'] as $key ) {
if ( isset( $key['meta_key'] ) && isset( $key['meta_value'] ) ) {
$meta_value = $key['meta_value'];
if ( $is_woocomerce ) {
if ( '_sku' == $key['meta_key'] ) {
if ( ! wc_product_has_unique_sku( $post_id, $key['meta_value'] ) ) {
if ( ! wc_product_has_unique_sku( $post_id, $meta_value ) ) {
return array( 'error' => __( 'Product SKU must be unique', $this->plugin_translate ) );
}
}
if ( '_product_image_gallery' == $key['meta_key'] ) {
$product_image_gallery = array();
if ( isset( $data['extras']['woocommerce']['product_images'] ) ) {
foreach ( $data['extras']['woocommerce']['product_images'] as $product_image ) {
try {
$upload_featured_image = MainWP_Utility::upload_image( $product_image, array(), $check_image_existed );
if ( null !== $upload_featured_image ) {
$product_image_gallery[] = $upload_featured_image['id'];
} else {
return array( 'error' => __( 'Cannot add product image', $this->plugin_translate ) );
}
} catch ( \Exception $e ) {
continue;
}
}
$key['meta_value'] = implode( $product_image_gallery, ',' );
$ret = $this->upload_postmeta_image( $data['extras']['woocommerce']['product_images'], $meta_value, $check_image_existed );
if ( true !== $ret )
return $ret;
} else {
continue;
}
@ -335,7 +323,7 @@ class MainWP_Custom_Post_Type {
$upload_featured_image = MainWP_Utility::upload_image( $data['extras']['featured_image'], array(), $check_image_existed );
if ( null !== $upload_featured_image ) {
$key['meta_value'] = $upload_featured_image['id'];
$meta_value = $upload_featured_image['id'];
} else {
return array( 'error' => __( 'Cannot add featured image', $this->plugin_translate ) );
}
@ -347,7 +335,7 @@ class MainWP_Custom_Post_Type {
}
}
$meta_value = maybe_unserialize( $key['meta_value'] );
$meta_value = maybe_unserialize( $meta_value );
if ( add_post_meta( $post_id, $key['meta_key'], $meta_value ) === false ) {
return array( 'error' => __( 'Error when adding post meta', $this->plugin_translate ) . ' `' . esc_html( $key['meta_key'] ) . '`' );
}
@ -355,4 +343,23 @@ class MainWP_Custom_Post_Type {
}
return true;
}
private function upload_postmeta_image( $product_images, &$meta_value, $check_image_existed ){
$product_image_gallery = array();
foreach ( $product_images as $product_image ) {
try {
$upload_featured_image = MainWP_Utility::upload_image( $product_image, array(), $check_image_existed );
if ( null !== $upload_featured_image ) {
$product_image_gallery[] = $upload_featured_image['id'];
} else {
return array( 'error' => __( 'Cannot add product image', $this->plugin_translate ) );
}
} catch ( \Exception $e ) {
continue;
}
}
$meta_value = implode( $product_image_gallery, ',' );
return true;
}
}

View file

@ -105,6 +105,7 @@ class MainWP_Pages {
// if preserve branding and do not hide menus.
if ( ( ! $remove_all_child_menu && 'T' !== $is_hide ) || $cancelled_branding ) {
$branding_header = isset( $branding_opts['branding_header'] ) ? $branding_opts['branding_header'] : array();
if ( ( is_array( $branding_header ) && ! empty( $branding_header['name'] ) ) && ! $cancelled_branding ) {
self::$brandingTitle = stripslashes( $branding_header['name'] );
@ -114,6 +115,11 @@ class MainWP_Pages {
$child_menu_title = 'MainWP Child';
$child_page_title = 'MainWPSettings';
}
$this->init_pages( $branding_header );
}
}
private function init_pages( $child_menu_title ) {
$settingsPage = add_submenu_page( 'options-general.php', $child_menu_title, $child_menu_title, 'manage_options', 'mainwp_child_tab', array( &$this, 'render_pages' ) );
@ -171,7 +177,6 @@ class MainWP_Pages {
}
}
}
}
public function plugin_row_meta( $plugin_meta, $plugin_file ) {
global $mainWPChild;

View file

@ -146,7 +146,7 @@ class Tar_Archiver {
if ( $this->archive ) {
$nodes = glob( ABSPATH . '*' );
if ( ! $includeCoreFiles ) {
$this->include_core_files( &$nodes );
$this->include_core_files( $nodes );
}
$db_files = $this->backup->create_backup_db( dirname( $filepath ) . DIRECTORY_SEPARATOR . 'dbBackup', false, $this );