Refactoring

This commit is contained in:
thanghv 2020-05-18 20:15:34 +07:00
parent 3a126a6e29
commit 737db8daa2
6 changed files with 245 additions and 224 deletions

View file

@ -146,9 +146,9 @@ class MainWP_Child_Branding {
'authoruri' => $settings['child_plugin_author_uri'], 'authoruri' => $settings['child_plugin_author_uri'],
'pluginuri' => isset( $settings['child_plugin_uri'] ) ? $settings['child_plugin_uri'] : '', 'pluginuri' => isset( $settings['child_plugin_uri'] ) ? $settings['child_plugin_uri'] : '',
); );
$current_settings['branding_header'] = $header;
$current_settings['preserve_branding'] = $settings['child_preserve_branding']; $current_settings['preserve_branding'] = $settings['child_preserve_branding'];
$current_settings['branding_header'] = $header;
$current_settings['support_email'] = $settings['child_support_email']; $current_settings['support_email'] = $settings['child_support_email'];
$current_settings['support_message'] = $settings['child_support_message']; $current_settings['support_message'] = $settings['child_support_message'];
$current_settings['remove_restore'] = $settings['child_remove_restore']; $current_settings['remove_restore'] = $settings['child_remove_restore'];
@ -162,10 +162,22 @@ class MainWP_Child_Branding {
$current_settings['email_message'] = $settings['child_send_email_message']; $current_settings['email_message'] = $settings['child_send_email_message'];
$current_settings['return_sender'] = $settings['child_message_return_sender']; $current_settings['return_sender'] = $settings['child_message_return_sender'];
$current_settings['submit_button_title'] = $settings['child_submit_button_title']; $current_settings['submit_button_title'] = $settings['child_submit_button_title'];
$current_settings['hide'] = $settings['child_plugin_hide'] ? 'T' : '';
$current_settings['show_support'] = ( $settings['child_show_support_button'] && ! empty( $settings['child_support_email'] ) ) ? 'T' : '';
$current_settings['disable_change'] = $settings['child_disable_change'] ? 'T' : '';
$current_settings['disable_switching_theme'] = $settings['child_disable_switching_theme'] ? 'T' : '';
if ( isset( $settings['child_disable_wp_branding'] ) && ( 'Y' === $settings['child_disable_wp_branding'] || 'N' === $settings['child_disable_wp_branding'] ) ) { if ( isset( $settings['child_disable_wp_branding'] ) && ( 'Y' === $settings['child_disable_wp_branding'] || 'N' === $settings['child_disable_wp_branding'] ) ) {
$current_settings['disable_wp_branding'] = $settings['child_disable_wp_branding']; $current_settings['disable_wp_branding'] = $settings['child_disable_wp_branding'];
} }
$current_settings['extra_settings'] = self::get_extra_settings( $current_extra_setting, $settings, $information );
MainWP_Helper::update_option( 'mainwp_child_branding_settings', $current_settings );
$information['result'] = 'SUCCESS';
return $information;
}
public static function get_extra_settings( $current_extra_setting, $settings, &$information ){
$extra_setting = array( $extra_setting = array(
'show_button_in' => $settings['child_show_support_button_in'], 'show_button_in' => $settings['child_show_support_button_in'],
@ -255,19 +267,7 @@ class MainWP_Child_Branding {
} elseif ( isset( $current_extra_setting['favico_image'] ) ) { } elseif ( isset( $current_extra_setting['favico_image'] ) ) {
$extra_setting['favico_image'] = $current_extra_setting['favico_image']; $extra_setting['favico_image'] = $current_extra_setting['favico_image'];
} }
return $extra_setting;
$current_settings['extra_settings'] = $extra_setting;
$current_settings['hide'] = $settings['child_plugin_hide'] ? 'T' : '';
$current_settings['show_support'] = ( $settings['child_show_support_button'] && ! empty( $settings['child_support_email'] ) ) ? 'T' : '';
$current_settings['disable_change'] = $settings['child_disable_change'] ? 'T' : '';
$current_settings['disable_switching_theme'] = $settings['child_disable_switching_theme'] ? 'T' : '';
MainWP_Helper::update_option( 'mainwp_child_branding_settings', $current_settings );
$information['result'] = 'SUCCESS';
return $information;
} }
public static function branding_upload_image( $img_url ) { public static function branding_upload_image( $img_url ) {

View file

@ -13,7 +13,7 @@
* Extension URL: https://mainwp.com/extension/ithemes-security/ * Extension URL: https://mainwp.com/extension/ithemes-security/
*/ */
// phpcs:disable PSR1.Classes.ClassDeclaration, WordPress.WP.AlternativeFunctions -- root namespace to use external code. // phpcs:disable -- third party credit code.
class MainWP_Child_IThemes_Security { class MainWP_Child_IThemes_Security {
public static $instance = null; public static $instance = null;
@ -1000,8 +1000,13 @@ class MainWP_Child_IThemes_Security {
$active = ''; $active = '';
} }
$results = $wpdb->get_results( 'SELECT * FROM `' . $wpdb->base_prefix . 'itsec_lockouts`' . $where . $type_statement . $active . ';', ARRAY_A ); // phpcs:ignore -- safe query. $output = array(); $results = $wpdb->get_results( 'SELECT * FROM `' . $wpdb->base_prefix . 'itsec_lockouts`' . $where . $type_statement . $active . ';', ARRAY_A ); // phpcs:ignore -- safe query. $output = array();
return $this->get_lockouts_int( $results, $type );
}
private function get_lockouts_int( $results, $type ){
if ( is_array( $results ) && count( $results ) > 0 ) { if ( is_array( $results ) && count( $results ) > 0 ) {
switch ( $type ) { switch ( $type ) {
case 'host': case 'host':
@ -1037,6 +1042,7 @@ class MainWP_Child_IThemes_Security {
} }
return $output; return $output;
} }
public function release_lockout() { public function release_lockout() {

View file

@ -428,10 +428,9 @@ class MainWP_Child_Server_Information {
<?php <?php
} }
public static function render_page() { public static function render_page(){
self::render_page_js(); self::render_page_js();
?> ?>
<style type="text/css"> <style type="text/css">
#mwp-server-information { #mwp-server-information {
display: none; display: none;

View file

@ -200,6 +200,47 @@ class MainWP_Client_Report {
public function get_stream() { public function get_stream() {
$sections = isset( $_POST['sections'] ) ? maybe_unserialize( base64_decode( $_POST['sections'] ) ) : array(); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( ! is_array( $sections ) ) {
$sections = array();
}
$other_tokens = isset( $_POST['other_tokens'] ) ? maybe_unserialize( base64_decode( $_POST['other_tokens'] ) ) : array(); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( ! is_array( $other_tokens ) ) {
$other_tokens = array();
}
unset( $_POST['sections'] );
unset( $_POST['other_tokens'] );
$args = $this->get_stream_get_params( $other_tokens, $sections );
$records = wp_mainwp_stream_get_instance()->db->query( $args );
if ( ! is_array( $records ) ) {
$records = array();
}
// fix invalid data, or skip records!
$skip_records = array();
// fix for incorrect posts created logs!
// query created posts from WP posts data to simulate records logging for created posts.
if ( isset( $_POST['direct_posts'] ) && ! empty( $_POST['direct_posts'] ) ) {
$this->fix_logs_posts_created( $records, $skip_records );
}
$other_tokens_data = $this->get_stream_others_tokens( $records, $other_tokens, $skip_records );
$sections_data = $this->get_stream_sections_data( $records, $sections, $skip_records );
$information = array(
'other_tokens_data' => $other_tokens_data,
'sections_data' => $sections_data,
);
return $information;
}
private function get_stream_get_params( $other_tokens, $sections ){
$allowed_params = array( $allowed_params = array(
'connector', 'connector',
'context', 'context',
@ -216,19 +257,6 @@ class MainWP_Client_Report {
'ip', 'ip',
); );
$sections = isset( $_POST['sections'] ) ? maybe_unserialize( base64_decode( $_POST['sections'] ) ) : array(); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( ! is_array( $sections ) ) {
$sections = array();
}
$other_tokens = isset( $_POST['other_tokens'] ) ? maybe_unserialize( base64_decode( $_POST['other_tokens'] ) ) : array(); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( ! is_array( $other_tokens ) ) {
$other_tokens = array();
}
unset( $_POST['sections'] );
unset( $_POST['other_tokens'] );
$args = array(); $args = array();
foreach ( $allowed_params as $param ) { foreach ( $allowed_params as $param ) {
$paramval = wp_mainwp_stream_filter_input( INPUT_POST, $param ); $paramval = wp_mainwp_stream_filter_input( INPUT_POST, $param );
@ -324,18 +352,35 @@ class MainWP_Client_Report {
$args['records_per_page'] = 9999; $args['records_per_page'] = 9999;
$records = wp_mainwp_stream_get_instance()->db->query( $args ); return $args;
if ( ! is_array( $records ) ) {
$records = array();
} }
// fix invalid data, or skip records! private function get_stream_others_tokens( $records, $other_tokens, $skip_records ){
$skip_records = array(); $other_tokens_data = array();
$parts = array( 'header', 'body', 'footer' );
foreach( $parts as $part ) {
if ( isset( $other_tokens[ $part ] ) && is_array( $other_tokens[ $part ] ) ) {
$other_tokens_data[ $part ] = $this->get_other_tokens_data( $records, $other_tokens[ $part ], $skip_records );
}
}
return $other_tokens_data;
}
// fix for incorrect posts created logs! private function get_stream_sections_data( $records, $sections, $skip_records ){
// query created posts from WP posts data to simulate records logging for created posts. $sections_data = array();
if ( isset( $_POST['direct_posts'] ) && ! empty( $_POST['direct_posts'] ) ) { $parts = array( 'header', 'body', 'footer' );
foreach( $parts as $part ) {
if ( isset( $sections[ $part ] ) && is_array( $sections[ $part ] ) && ! empty( $sections[ $part ] ) ) {
foreach ( $sections[ $part ]['section_token'] as $index => $sec ) {
$tokens = $sections[ $part ]['section_content_tokens'][ $index ];
$sections_data[ $part ][ $index ] = $this->get_section_loop_data( $records, $tokens, $sec, $skip_records );
}
}
}
return $sections_data;
}
private function fix_logs_posts_created( &$records, &$skip_records ){
$args = array( $args = array(
'post_type' => 'post', 'post_type' => 'post',
@ -389,47 +434,6 @@ class MainWP_Client_Report {
} }
} }
if ( isset( $other_tokens['header'] ) && is_array( $other_tokens['header'] ) ) {
$other_tokens_data['header'] = $this->get_other_tokens_data( $records, $other_tokens['header'], $skip_records );
}
if ( isset( $other_tokens['body'] ) && is_array( $other_tokens['body'] ) ) {
$other_tokens_data['body'] = $this->get_other_tokens_data( $records, $other_tokens['body'], $skip_records );
}
if ( isset( $other_tokens['footer'] ) && is_array( $other_tokens['footer'] ) ) {
$other_tokens_data['footer'] = $this->get_other_tokens_data( $records, $other_tokens['footer'], $skip_records );
}
$sections_data = array();
if ( isset( $sections['header'] ) && is_array( $sections['header'] ) && ! empty( $sections['header'] ) ) {
foreach ( $sections['header']['section_token'] as $index => $sec ) {
$tokens = $sections['header']['section_content_tokens'][ $index ];
$sections_data['header'][ $index ] = $this->get_section_loop_data( $records, $tokens, $sec, $skip_records );
}
}
if ( isset( $sections['body'] ) && is_array( $sections['body'] ) && ! empty( $sections['body'] ) ) {
foreach ( $sections['body']['section_token'] as $index => $sec ) {
$tokens = $sections['body']['section_content_tokens'][ $index ];
$sections_data['body'][ $index ] = $this->get_section_loop_data( $records, $tokens, $sec, $skip_records );
}
}
if ( isset( $sections['footer'] ) && is_array( $sections['footer'] ) && ! empty( $sections['footer'] ) ) {
foreach ( $sections['footer']['section_token'] as $index => $sec ) {
$tokens = $sections['footer']['section_content_tokens'][ $index ];
$sections_data['footer'][ $index ] = $this->get_section_loop_data( $records, $tokens, $sec, $skip_records );
}
}
$information = array(
'other_tokens_data' => $other_tokens_data,
'sections_data' => $sections_data,
);
return $information;
}
public function get_other_tokens_data( $records, $tokens, &$skip_records ) { public function get_other_tokens_data( $records, $tokens, &$skip_records ) {
$token_values = array(); $token_values = array();
@ -609,19 +613,6 @@ class MainWP_Client_Report {
public function get_section_loop_records( $records, $tokens, $connector, $context, $action, $skip_records ) { public function get_section_loop_records( $records, $tokens, $connector, $context, $action, $skip_records ) {
$maintenance_details = array(
'revisions' => __( 'Delete all post revisions', 'mainwp-child' ),
'revisions_max' => __( 'Delete all post revisions, except for the last:', 'mainwp-child' ),
'autodraft' => __( 'Delete all auto draft posts', 'mainwp-child' ),
'trashpost' => __( 'Delete trash posts', 'mainwp-child' ),
'spam' => __( 'Delete spam comments', 'mainwp-child' ),
'pending' => __( 'Delete pending comments', 'mainwp-child' ),
'trashcomment' => __( 'Delete trash comments', 'mainwp-child' ),
'tags' => __( 'Delete tags with 0 posts associated', 'mainwp-child' ),
'categories' => __( 'Delete categories with 0 posts associated', 'mainwp-child' ),
'optimize' => __( 'Optimize database tables', 'mainwp-child' ),
);
$loops = array(); $loops = array();
$loop_count = 0; $loop_count = 0;
foreach ( $records as $record ) { foreach ( $records as $record ) {
@ -905,6 +896,19 @@ class MainWP_Client_Report {
private function get_mainwp_maintenance_token_value( $record, $data ) { private function get_mainwp_maintenance_token_value( $record, $data ) {
$maintenance_details = array(
'revisions' => __( 'Delete all post revisions', 'mainwp-child' ),
'revisions_max' => __( 'Delete all post revisions, except for the last:', 'mainwp-child' ),
'autodraft' => __( 'Delete all auto draft posts', 'mainwp-child' ),
'trashpost' => __( 'Delete trash posts', 'mainwp-child' ),
'spam' => __( 'Delete spam comments', 'mainwp-child' ),
'pending' => __( 'Delete pending comments', 'mainwp-child' ),
'trashcomment' => __( 'Delete trash comments', 'mainwp-child' ),
'tags' => __( 'Delete tags with 0 posts associated', 'mainwp-child' ),
'categories' => __( 'Delete categories with 0 posts associated', 'mainwp-child' ),
'optimize' => __( 'Optimize database tables', 'mainwp-child' ),
);
$meta_value = $this->get_stream_meta_data( $record, $data ); $meta_value = $this->get_stream_meta_data( $record, $data );
$meta_value = explode( ',', $meta_value ); $meta_value = explode( ',', $meta_value );

View file

@ -1375,31 +1375,7 @@ class MainWP_Clone {
$file = ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] ); $file = ( isset( $_POST['f'] ) ? $_POST['f'] : $_POST['file'] );
$testFull = false; $testFull = false;
$file = $this->clone_backup_get_file( $file, $testFull );
if ( '' === $file ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$backupdir = $dirs[0];
$files = glob( $backupdir . 'download-*' );
$archiveFile = false;
foreach ( $files as $file ) {
if ( MainWP_Helper::is_archive( $file, 'download-' ) ) {
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
throw new \Exception( __( 'No download file found', 'mainwp-child' ) );
}
$file = $archiveFile;
} elseif ( file_exists( $file ) ) {
$testFull = true;
} else {
$file = ABSPATH . $file;
if ( ! file_exists( $file ) ) {
throw new \Exception( __( 'Backup file not found', 'mainwp-child' ) );
}
$testFull = true;
}
$cloneInstall = new MainWP_Clone_Install( $file ); $cloneInstall = new MainWP_Clone_Install( $file );
$cloneInstall->read_configuration_file(); $cloneInstall->read_configuration_file();
@ -1445,7 +1421,43 @@ class MainWP_Clone {
$cloneInstall->update_wp_config(); $cloneInstall->update_wp_config();
$cloneInstall->clean(); $cloneInstall->clean();
$output = $this->clone_backup_delete_files( $plugins, $themes );
} catch ( \Exception $e ) {
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
}
private function clone_backup_get_file( $file, &$testFull ){
if ( '' === $file ) {
$dirs = MainWP_Helper::get_mainwp_dir( 'backup', false );
$backupdir = $dirs[0];
$files = glob( $backupdir . 'download-*' );
$archiveFile = false;
foreach ( $files as $file ) {
if ( MainWP_Helper::is_archive( $file, 'download-' ) ) {
$archiveFile = $file;
break;
}
}
if ( false === $archiveFile ) {
throw new \Exception( __( 'No download file found', 'mainwp-child' ) );
}
$file = $archiveFile;
} elseif ( file_exists( $file ) ) {
$testFull = true;
} else {
$file = ABSPATH . $file;
if ( ! file_exists( $file ) ) {
throw new \Exception( __( 'Backup file not found', 'mainwp-child' ) );
}
$testFull = true;
}
return $file;
}
private function clone_backup_delete_files( $plugins, $themes ){
if ( false !== $plugins ) { if ( false !== $plugins ) {
$out = array(); $out = array();
if ( is_array( $plugins ) ) { if ( is_array( $plugins ) ) {
@ -1490,10 +1502,7 @@ class MainWP_Clone {
$output = array( 'result' => 'ok' ); $output = array( 'result' => 'ok' );
wp_logout(); wp_logout();
wp_set_current_user( 0 ); wp_set_current_user( 0 );
} catch ( \Exception $e ) { return $output;
$output = array( 'error' => $e->getMessage() );
}
die( wp_json_encode( $output ) );
} }
public static function permalink_changed( $action ) { public static function permalink_changed( $action ) {

View file

@ -100,7 +100,10 @@ class MainWP_Helper {
// Split by parenthesis, ignoring those inside content-quotes. // Split by parenthesis, ignoring those inside content-quotes.
$ordered[ $ordered_key ] = preg_split( '/([^\'"\{\}]*?[\'"].*?(?<!\\\)[\'"][^\'"\{\}]*?)[\{\}]|([^\'"\{\}]*?)[\{\}]/', trim( $ordered_value, " \r\n\t" ), -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE ); $ordered[ $ordered_key ] = preg_split( '/([^\'"\{\}]*?[\'"].*?(?<!\\\)[\'"][^\'"\{\}]*?)[\{\}]|([^\'"\{\}]*?)[\{\}]/', trim( $ordered_value, " \r\n\t" ), -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
} }
return self::parse_css_rebuild( $ordered );
}
public static function parse_css_rebuild( $ordered ) {
// Beginning to rebuild new slim CSS-Array. // Beginning to rebuild new slim CSS-Array.
foreach ( $ordered as $key => $val ) { foreach ( $ordered as $key => $val ) {
$new = array(); $new = array();