2015-10-15 22:52:37 +10:00
< ? php
2020-05-05 20:13:38 +07:00
namespace MainWP\Child ;
2015-10-15 22:52:37 +10:00
class MainWP_Client_Report {
2020-03-17 18:40:10 +00:00
public static $instance = null ;
2020-05-06 20:22:11 +07:00
public static function instance () {
2020-03-17 18:40:10 +00:00
if ( null === self :: $instance ) {
2020-05-07 01:03:56 +07:00
self :: $instance = new self ();
2015-10-15 22:52:37 +10:00
}
2020-03-17 18:40:10 +00:00
return self :: $instance ;
2015-10-15 22:52:37 +10:00
}
2020-03-17 18:40:10 +00:00
public function __construct () {
add_filter ( 'wp_mainwp_stream_current_agent' , array ( $this , 'current_agent' ), 10 , 1 );
2019-12-09 22:02:19 +07:00
}
2020-03-17 18:40:10 +00:00
2018-06-26 19:52:53 +02:00
public function init () {
2020-05-07 19:34:36 +07:00
add_filter ( 'mainwp_site_sync_others_data' , array ( $this , 'sync_others_data' ), 10 , 2 );
2017-08-24 20:41:12 +02:00
add_action ( 'mainwp_child_log' , array ( 'MainWP_Client_Report' , 'do_reports_log' ) );
2015-10-15 22:52:37 +10:00
}
2020-03-17 18:40:10 +00:00
public function current_agent ( $agent ) {
if ( isset ( $_POST [ 'function' ] ) && isset ( $_POST [ 'mainwpsignature' ] ) ) {
$agent = '' ;
}
return $agent ;
2019-12-09 22:02:19 +07:00
}
2020-03-17 18:40:10 +00:00
2020-05-06 00:47:59 +07:00
public function sync_others_data ( $information , $data = array () ) {
2020-03-17 18:40:10 +00:00
if ( isset ( $data [ 'syncClientReportData' ] ) && $data [ 'syncClientReportData' ] ) {
$creport_sync_data = array ();
2020-04-15 15:23:18 +00:00
$firsttime = get_option ( 'mainwp_creport_first_time_activated' );
2020-04-15 17:23:10 +02:00
if ( false !== $firsttime ) {
2020-03-17 18:40:10 +00:00
$creport_sync_data [ 'firsttime_activated' ] = $firsttime ;
}
if ( ! empty ( $creport_sync_data ) ) {
$information [ 'syncClientReportData' ] = $creport_sync_data ;
}
}
2018-06-26 19:52:53 +02:00
return $information ;
}
2020-03-17 18:40:10 +00:00
public static function do_reports_log ( $ext = '' ) {
switch ( $ext ) {
2017-08-24 20:41:12 +02:00
case 'backupbuddy' :
2020-05-07 19:34:36 +07:00
\MainWP_Child_Back_Up_Buddy :: instance () -> do_reports_log ( $ext );
2017-08-24 20:41:12 +02:00
break ;
case 'backupwordpress' :
2020-05-07 23:22:05 +07:00
\MainWP_Child_Back_Up_WordPress :: instance () -> do_reports_log ( $ext );
2017-08-24 20:41:12 +02:00
break ;
case 'backwpup' :
2020-05-07 19:34:36 +07:00
\MainWP_Child_Back_WP_Up :: instance () -> do_reports_log ( $ext );
2017-08-24 20:41:12 +02:00
break ;
case 'wordfence' :
2020-05-07 23:22:05 +07:00
\MainWP_Child_Wordfence :: instance () -> do_reports_log ( $ext );
2017-08-24 20:41:12 +02:00
break ;
2020-03-17 18:40:10 +00:00
case 'wptimecapsule' :
2020-05-07 23:22:05 +07:00
\MainWP_Child_Timecapsule :: instance () -> do_reports_log ( $ext );
2018-09-27 19:52:32 +02:00
break ;
2017-08-24 20:41:12 +02:00
}
}
2017-01-14 13:16:52 +01:00
2015-10-15 22:52:37 +10:00
public function action () {
2020-03-17 18:40:10 +00:00
$information = array ();
2017-01-14 13:16:52 +01:00
2020-03-17 18:40:10 +00:00
if ( ! function_exists ( 'wp_mainwp_stream_get_instance' ) ) {
2020-04-15 17:23:10 +02:00
$information [ 'error' ] = __ ( 'No MainWP Child Reports plugin installed.' , 'mainwp-child' );
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2015-10-15 22:52:37 +10:00
}
if ( isset ( $_POST [ 'mwp_action' ] ) ) {
switch ( $_POST [ 'mwp_action' ] ) {
case 'save_sucuri_stream' :
$information = $this -> save_sucuri_stream ();
break ;
case 'save_backup_stream' :
$information = $this -> save_backup_stream ();
break ;
case 'get_stream' :
$information = $this -> get_stream ();
break ;
case 'set_showhide' :
$information = $this -> set_showhide ();
break ;
}
}
2020-05-07 19:34:36 +07:00
mainwp_child_helper () -> write ( $information );
2015-10-15 22:52:37 +10:00
}
2020-03-17 18:40:10 +00:00
public function save_sucuri_stream () {
2020-04-15 17:23:10 +02:00
$scan_data = isset ( $_POST [ 'scan_data' ] ) ? $_POST [ 'scan_data' ] : '' ;
do_action ( 'mainwp_reports_sucuri_scan' , $_POST [ 'result' ], $_POST [ 'scan_status' ], $scan_data , isset ( $_POST [ 'scan_time' ] ) ? $_POST [ 'scan_time' ] : 0 );
2015-10-15 22:52:37 +10:00
return true ;
}
public function save_backup_stream () {
do_action ( 'mainwp_backup' , $_POST [ 'destination' ], $_POST [ 'message' ], $_POST [ 'size' ], $_POST [ 'status' ], $_POST [ 'type' ] );
return true ;
}
2020-03-17 18:40:10 +00:00
2019-12-09 22:02:19 +07:00
public function is_backup_action ( $action ) {
2020-03-17 18:40:10 +00:00
if ( in_array ( $action , array ( 'mainwp_backup' , 'backupbuddy_backup' , 'backupwordpress_backup' , 'backwpup_backup' , 'updraftplus_backup' , 'wptimecapsule_backup' , 'wpvivid_backup' ) ) ) {
2019-12-09 22:02:19 +07:00
return true ;
2020-03-17 18:40:10 +00:00
}
return false ;
2019-12-09 22:02:19 +07:00
}
2020-03-17 18:40:10 +00:00
public function get_compatible_context ( $context ) {
2020-04-15 17:23:10 +02:00
// convert context name of tokens to context name saved in child report.
// some context are not difference.
2020-03-17 18:40:10 +00:00
$mapping_contexts = array (
2020-04-15 17:23:10 +02:00
'comment' => 'comments' , // actual context values: post, page.
2020-03-17 18:40:10 +00:00
'plugin' => 'plugins' ,
'users' => 'profiles' ,
'user' => 'profiles' ,
'session' => 'sessions' ,
'setting' => 'settings' ,
'theme' => 'themes' ,
'posts' => 'post' ,
'pages' => 'page' ,
'widgets' => 'widgets' ,
'widget' => 'widgets' ,
'menu' => 'menus' ,
'backups' => 'backups' ,
'backup' => 'backups' ,
'sucuri' => 'sucuri_scan' ,
2019-12-09 22:02:19 +07:00
'maintenance' => 'mainwp_maintenance' ,
2020-03-17 18:40:10 +00:00
'wordfence' => 'wordfence_scan' ,
'backups' => 'backups' ,
'backup' => 'backups' ,
'media' => 'media' ,
2019-12-09 22:02:19 +07:00
);
2020-03-17 18:40:10 +00:00
$context = isset ( $mapping_contexts [ $context ] ) ? $mapping_contexts [ $context ] : $context ;
2020-04-15 17:23:10 +02:00
return strtolower ( $context );
2019-12-09 22:02:19 +07:00
}
2020-03-17 18:40:10 +00:00
2019-12-09 22:02:19 +07:00
public function get_connector_by_compatible_context ( $context ) {
2020-03-17 18:40:10 +00:00
$connector = '' ;
2020-05-15 16:54:45 +00:00
2020-05-15 23:53:53 +07:00
$mapping_connectors = array (
2020-05-15 16:54:45 +00:00
'plugins' => 'installer' ,
'themes' => 'installer' ,
'WordPress' => 'installer' ,
'profiles' => 'users' ,
'comments' => 'comments' ,
'settings' => 'settings' ,
'post' => 'posts' ,
'page' => 'posts' ,
'widgets' => 'widgets' ,
'menus' => 'menus' ,
'backups' => 'mainwp_backups' ,
'sucuri_scan' => 'mainwp_sucuri' ,
'mainwp_maintenance' => 'mainwp_maintenance' ,
'wordfence_scan' => 'mainwp_wordfence' ,
'media' => 'media' ,
2020-05-15 23:53:53 +07:00
);
2020-05-15 16:54:45 +00:00
if ( isset ( $mapping_connectors [ $context ] ) ) {
2020-05-15 23:53:53 +07:00
$connector = $mapping_connectors [ $context ];
2020-05-15 16:54:45 +00:00
}
2020-03-17 18:40:10 +00:00
return $connector ;
2019-12-09 22:02:19 +07:00
}
2015-10-15 22:52:37 +10:00
2019-12-09 22:02:19 +07:00
public function get_compatible_action ( $action , $context = '' ) {
2020-03-17 18:40:10 +00:00
2019-12-09 22:02:19 +07:00
$mapping_actions = array (
'restored' => 'untrashed' ,
2020-03-17 18:40:10 +00:00
'spam' => 'spammed' ,
);
2020-04-15 17:23:10 +02:00
if ( isset ( $mapping_actions [ $action ] ) ) {
2019-12-09 22:02:19 +07:00
return $mapping_actions [ $action ];
2020-03-17 18:40:10 +00:00
}
2020-04-15 17:23:10 +02:00
if ( 'mainwp_maintenance' == $context ) {
if ( 'process' == $action ) {
2019-12-09 22:02:19 +07:00
$action = 'maintenance' ;
2020-03-17 18:40:10 +00:00
}
2020-04-15 17:23:10 +02:00
} elseif ( 'sucuri_scan' == $context ) {
if ( 'checks' == $action ) {
2019-12-09 22:02:19 +07:00
$action = 'sucuri_scan' ;
2020-03-17 18:40:10 +00:00
}
2020-04-15 17:23:10 +02:00
} elseif ( 'wordfence_scan' == $context ) {
if ( 'scan' == $action ) {
2019-12-09 22:02:19 +07:00
$action = 'wordfence_scan' ;
2020-03-17 18:40:10 +00:00
}
}
2019-12-09 22:02:19 +07:00
return $action ;
}
2020-03-17 18:40:10 +00:00
2015-10-15 22:52:37 +10:00
public function get_stream () {
2020-05-15 16:54:45 +00:00
2020-05-18 20:15:34 +07:00
$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' ] );
2020-05-18 13:16:52 +00:00
$args = $this -> get_stream_get_params ( $other_tokens , $sections );
2020-05-18 20:15:34 +07:00
$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' ] ) ) {
2020-05-18 13:16:52 +00:00
$this -> fix_logs_posts_created ( $records , $skip_records );
2020-05-18 20:15:34 +07:00
}
2020-05-18 13:16:52 +00:00
2020-05-18 20:15:34 +07:00
$other_tokens_data = $this -> get_stream_others_tokens ( $records , $other_tokens , $skip_records );
2020-05-18 13:16:52 +00:00
$sections_data = $this -> get_stream_sections_data ( $records , $sections , $skip_records );
2020-05-18 20:15:34 +07:00
$information = array (
'other_tokens_data' => $other_tokens_data ,
'sections_data' => $sections_data ,
);
return $information ;
}
2020-05-18 13:16:52 +00:00
private function get_stream_get_params ( $other_tokens , $sections ) {
2015-10-15 22:52:37 +10:00
$allowed_params = array (
'connector' ,
'context' ,
'action' ,
'author' ,
'author_role' ,
'object_id' ,
'search' ,
'date' ,
'date_from' ,
'date_to' ,
'record__in' ,
'blog_id' ,
'ip' ,
);
2020-05-18 13:16:52 +00:00
2015-10-15 22:52:37 +10:00
$args = array ();
foreach ( $allowed_params as $param ) {
2019-12-09 22:02:19 +07:00
$paramval = wp_mainwp_stream_filter_input ( INPUT_POST , $param );
2015-10-15 22:52:37 +10:00
if ( $paramval || '0' === $paramval ) {
$args [ $param ] = $paramval ;
}
}
foreach ( $args as $arg => $val ) {
if ( ! in_array ( $arg , $allowed_params ) ) {
unset ( $args [ $arg ] );
}
}
2020-05-19 01:15:36 +07:00
$exclude_connector_posts = $this -> get_stream_get_not_in_params ( $sections , $other_tokens );
2020-03-17 18:40:10 +00:00
if ( $exclude_connector_posts ) {
$args [ 'connector__not_in' ] = array ( 'posts' );
}
2015-12-05 17:38:52 +01:00
2015-10-15 22:52:37 +10:00
$args [ 'action__not_in' ] = array ( 'login' );
2019-12-09 22:02:19 +07:00
$args [ 'with-meta' ] = 1 ;
2020-03-17 18:40:10 +00:00
2017-08-24 20:41:12 +02:00
if ( isset ( $args [ 'date_from' ] ) ) {
2020-05-07 19:34:36 +07:00
$args [ 'date_from' ] = date ( 'Y-m-d' , $args [ 'date_from' ] ); // phpcs:ignore -- local time.
2017-08-24 20:41:12 +02:00
}
2015-10-15 22:52:37 +10:00
2017-08-24 20:41:12 +02:00
if ( isset ( $args [ 'date_to' ] ) ) {
2020-05-07 19:34:36 +07:00
$args [ 'date_to' ] = date ( 'Y-m-d' , $args [ 'date_to' ] ); // phpcs:ignore -- local time.
2017-08-24 20:41:12 +02:00
}
2015-10-15 22:52:37 +10:00
2020-05-06 20:22:11 +07:00
if ( MainWP_Child_Branding :: instance () -> is_branding () ) {
2017-08-24 20:41:12 +02:00
$args [ 'hide_child_reports' ] = 1 ;
}
2016-11-24 20:46:47 +01:00
2015-10-15 22:52:37 +10:00
$args [ 'records_per_page' ] = 9999 ;
2020-05-18 13:16:52 +00:00
2020-05-18 20:15:34 +07:00
return $args ;
}
2020-05-18 13:16:52 +00:00
2020-05-19 01:15:36 +07:00
private function get_stream_get_not_in_params ( $sections , $other_tokens ){
$exclude_connector_posts = true ;
$parts = array ( 'header' , 'body' , 'footer' );
foreach ( $parts as $part ) {
if ( isset ( $sections [ $part ] ) && isset ( $sections [ $part ][ 'section_token' ] ) && is_array ( $sections [ $part ][ 'section_token' ] ) ) {
foreach ( $sections [ $part ][ 'section_token' ] as $sec ) {
if ( false !== strpos ( $sec , '[section.posts' ) || false !== strpos ( $sec , '[section.pages' ) ) {
$exclude_connector_posts = false ;
break ;
}
}
}
if ( ! $exclude_connector_posts )
break ;
}
if ( $exclude_connector_posts ) {
foreach ( $parts as $part ) {
if ( isset ( $other_tokens [ $part ] ) && is_array ( $other_tokens [ $part ] ) ) {
foreach ( $other_tokens [ $part ] as $sec ) {
if ( false !== strpos ( $sec , '[post.' ) || false !== strpos ( $sec , '[page.' ) ) {
$exclude_connector_posts = false ;
break ;
}
}
}
if ( ! $exclude_connector_posts )
break ;
}
}
return $exclude_connector_posts ;
}
2020-05-18 13:16:52 +00:00
private function get_stream_others_tokens ( $records , $other_tokens , $skip_records ) {
2020-05-18 20:15:34 +07:00
$other_tokens_data = array ();
2020-05-18 13:16:52 +00:00
$parts = array ( 'header' , 'body' , 'footer' );
foreach ( $parts as $part ) {
2020-05-18 20:15:34 +07:00
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 );
2020-05-18 13:16:52 +00:00
}
2020-05-18 20:15:34 +07:00
}
return $other_tokens_data ;
}
2020-05-18 13:16:52 +00:00
private function get_stream_sections_data ( $records , $sections , $skip_records ) {
2020-05-18 20:15:34 +07:00
$sections_data = array ();
2020-05-18 13:16:52 +00:00
$parts = array ( 'header' , 'body' , 'footer' );
foreach ( $parts as $part ) {
2020-05-18 20:15:34 +07:00
if ( isset ( $sections [ $part ] ) && is_array ( $sections [ $part ] ) && ! empty ( $sections [ $part ] ) ) {
foreach ( $sections [ $part ][ 'section_token' ] as $index => $sec ) {
2020-05-18 13:16:52 +00:00
$tokens = $sections [ $part ][ 'section_content_tokens' ][ $index ];
2020-05-18 20:15:34 +07:00
$sections_data [ $part ][ $index ] = $this -> get_section_loop_data ( $records , $tokens , $sec , $skip_records );
}
}
2015-10-15 22:52:37 +10:00
}
2020-05-18 20:15:34 +07:00
return $sections_data ;
}
2020-03-27 13:05:05 +00:00
2020-05-18 13:16:52 +00:00
private function fix_logs_posts_created ( & $records , & $skip_records ) {
2020-05-18 20:15:34 +07:00
$args = array (
'post_type' => 'post' ,
'post_status' => 'publish' ,
'date_query' => array (
'column' => 'post_date' ,
'after' => $args [ 'date_from' ],
'before' => $args [ 'date_to' ],
),
);
2020-05-07 12:38:30 +00:00
2020-05-18 20:15:34 +07:00
$result = new \WP_Query ( $args );
$records_created_posts = $result -> posts ;
2020-03-27 13:05:05 +00:00
2020-05-18 20:15:34 +07:00
if ( $records_created_posts ) {
2020-03-27 13:05:05 +00:00
2020-05-18 20:15:34 +07:00
$count_records = count ( $records );
for ( $i = 0 ; $i < $count_records ; $i ++ ) {
$record = $records [ $i ];
if ( 'posts' == $record -> connector && 'post' == $record -> context && 'created' == $record -> action ) {
if ( ! in_array ( $record -> ID , $skip_records ) ) {
$skip_records [] = $record -> ID ; // so avoid this created logging, will use logging query from posts data.
2020-03-27 19:18:06 +07:00
}
}
}
2015-10-15 22:52:37 +10:00
2020-05-18 20:15:34 +07:00
$post_authors = array ();
2015-10-15 22:52:37 +10:00
2020-05-18 20:15:34 +07:00
foreach ( $records_created_posts as $_post ) {
$au_id = $_post -> post_author ;
if ( ! isset ( $post_authors [ $au_id ] ) ) {
$au = get_user_by ( 'id' , $au_id );
$post_authors [ $au_id ] = $au -> display_name ;
}
$au_name = $post_authors [ $au_id ];
2015-10-15 22:52:37 +10:00
2020-05-18 20:15:34 +07:00
// simulate logging created posts record.
$stdObj = new \stdClass ();
$stdObj -> ID = 0 ; // simulate ID value.
$stdObj -> connector = 'posts' ;
$stdObj -> context = 'post' ;
$stdObj -> action = 'created' ;
$stdObj -> created = $_post -> post_date ;
$stdObj -> meta = array (
'post_title' => array ( $_post -> post_title ),
'user_meta' => array ( $au_name ),
);
2015-10-15 22:52:37 +10:00
2020-05-18 20:15:34 +07:00
$records [] = $stdObj ;
2015-10-15 22:52:37 +10:00
}
}
}
2020-05-18 13:16:52 +00:00
2020-04-15 17:23:10 +02:00
public function get_other_tokens_data ( $records , $tokens , & $skip_records ) {
2015-10-15 22:52:37 +10:00
$token_values = array ();
if ( ! is_array ( $tokens ) ) {
$tokens = array ();
}
2020-03-17 18:40:10 +00:00
$backups_created_time_to_fix = array ();
2020-05-19 01:15:36 +07:00
2015-10-15 22:52:37 +10:00
foreach ( $tokens as $token ) {
2020-03-17 18:40:10 +00:00
if ( isset ( $token_values [ $token ] ) ) {
2019-09-06 23:24:30 +07:00
continue ;
2020-03-17 18:40:10 +00:00
}
2015-10-15 22:52:37 +10:00
$str_tmp = str_replace ( array ( '[' , ']' ), '' , $token );
$array_tmp = explode ( '.' , $str_tmp );
if ( is_array ( $array_tmp ) ) {
2020-04-15 17:23:10 +02:00
$context = '' ;
$action = '' ;
$data = '' ;
2015-10-15 22:52:37 +10:00
if ( 2 === count ( $array_tmp ) ) {
list ( $context , $data ) = $array_tmp ;
2020-03-17 18:40:10 +00:00
} elseif ( 3 === count ( $array_tmp ) ) {
2015-10-15 22:52:37 +10:00
list ( $context , $action , $data ) = $array_tmp ;
}
2020-03-17 18:40:10 +00:00
$context = $this -> get_compatible_context ( $context );
2020-04-15 17:23:10 +02:00
// to compatible with new version of child report.
// to check condition for grabbing report data.
2019-12-09 22:02:19 +07:00
$connector = $this -> get_connector_by_compatible_context ( $context );
$action = $this -> get_compatible_action ( $action , $context );
2020-04-15 17:23:10 +02:00
// custom values.
if ( 'profiles' == $context ) {
if ( 'created' == $action || 'deleted' == $action ) {
$context = 'users' ; // see class-connector-user.php.
2019-12-09 22:02:19 +07:00
}
2015-10-15 22:52:37 +10:00
}
switch ( $data ) {
2020-05-19 01:15:36 +07:00
case 'count' :
$token_values [ $token ] = $this -> get_other_tokens_count ( $records , $connector , $context , $action , $skip_records , $backups_created_time_to_fix );
break ;
}
}
}
2020-03-17 18:40:10 +00:00
2020-05-19 01:15:36 +07:00
return $token_values ;
}
2020-05-06 00:47:59 +07:00
2020-05-19 01:15:36 +07:00
private function get_other_tokens_count ( $records , $connector , $context , $action , & $skip_records , & $backups_created_time_to_fix ){
$count = 0 ;
foreach ( $records as $record ) {
// check connector.
if ( 'editor' == $record -> connector ) {
if ( ! in_array ( $context , array ( 'plugins' , 'themes' ) ) || 'updated' !== $action ) {
continue ;
}
} elseif ( $connector !== $record -> connector ) {
continue ;
}
2020-05-05 17:50:07 +00:00
2020-05-19 01:15:36 +07:00
$valid_context = false ;
// check context.
if ( 'comments' == $context ) { // multi values.
$comment_contexts = array ( 'post' , 'page' );
if ( ! in_array ( $record -> context , $comment_contexts ) ) {
continue ;
}
$valid_context = true ;
} elseif ( 'post' === $context && 'created' === $action ) {
if ( in_array ( $record -> ID , $skip_records ) ) {
continue ;
}
$valid_context = true ;
} elseif ( 'menus' == $context ) {
$valid_context = true ; // ok, pass, don't check context.
} elseif ( 'editor' == $record -> connector ) {
$valid_context = true ; // ok, pass, checked above.
} elseif ( 'media' == $connector && 'media' == $record -> connector ) {
$valid_context = true ; // ok, pass, do not check context.
} elseif ( 'widgets' == $connector && 'widgets' == $record -> connector ) {
$valid_context = true ; // ok, pass, don't check context.
}
if ( ! $valid_context || strtolower ( $record -> context ) !== $context ) {
continue ;
}
// custom action value.
if ( 'widgets' == $connector ) {
if ( 'deleted' == $action ) {
$action = 'removed' ; // action saved in database.
}
}
2020-03-17 18:40:10 +00:00
2020-05-19 01:15:36 +07:00
// check action.
if ( 'backups' === $context ) {
if ( ! $this -> is_backup_action ( $record -> action ) ) {
continue ;
}
$created = strtotime ( $record -> created );
if ( in_array ( $created , $backups_created_time_to_fix ) ) {
if ( ! in_array ( $record -> ID , $skip_records ) ) {
$skip_records [] = $record -> ID ;
}
continue ;
} else {
$backups_created_time_to_fix [] = $created ;
}
} else {
if ( $action !== $record -> action ) {
continue ;
}
if ( 'updated' === $action && ( 'post' === $context || 'page' === $context ) ) {
$new_status = $this -> get_stream_meta_data ( $record , 'new_status' );
if ( 'draft' === $new_status ) {
continue ;
}
} elseif ( 'updated' === $action && ( 'themes' === $context || 'plugins' === $context ) ) {
$name = $this -> get_stream_meta_data ( $record , 'name' );
if ( empty ( $name ) ) {
if ( ! in_array ( $record -> ID , $skip_records ) ) {
$skip_records [] = $record -> ID ;
}
continue ;
} else {
$old_version = $this -> get_stream_meta_data ( $record , 'old_version' );
$version = $this -> get_stream_meta_data ( $record , 'version' );
if ( version_compare ( $version , $old_version , '<=' ) ) {
if ( ! in_array ( $record -> ID , $skip_records ) ) {
$skip_records [] = $record -> ID ;
2015-10-15 22:52:37 +10:00
}
2020-05-19 01:15:36 +07:00
continue ;
2015-10-15 22:52:37 +10:00
}
2020-05-19 01:15:36 +07:00
}
2015-10-15 22:52:37 +10:00
}
}
2020-05-19 01:15:36 +07:00
$count ++ ;
2015-10-15 22:52:37 +10:00
}
2020-05-19 01:15:36 +07:00
return $count ;
2015-10-15 22:52:37 +10:00
}
2020-05-19 01:15:36 +07:00
2020-04-15 17:23:10 +02:00
public function get_section_loop_data ( $records , $tokens , $section , $skip_records = array () ) {
2015-10-15 22:52:37 +10:00
2020-05-12 18:19:20 +00:00
$context = '' ;
$action = '' ;
2015-10-15 22:52:37 +10:00
$str_tmp = str_replace ( array ( '[' , ']' ), '' , $section );
$array_tmp = explode ( '.' , $str_tmp );
if ( is_array ( $array_tmp ) ) {
if ( 2 === count ( $array_tmp ) ) {
list ( $str1 , $context ) = $array_tmp ;
2020-03-17 18:40:10 +00:00
} elseif ( 3 === count ( $array_tmp ) ) {
2015-10-15 22:52:37 +10:00
list ( $str1 , $context , $action ) = $array_tmp ;
}
}
2020-03-17 18:40:10 +00:00
2020-04-15 17:23:10 +02:00
// get db $context value by mapping.
2020-03-17 18:40:10 +00:00
$context = $this -> get_compatible_context ( $context );
2020-04-15 17:23:10 +02:00
// to compatible with new version of child report.
// to check condition for grabbing report data.
2019-12-09 22:02:19 +07:00
$connector = $this -> get_connector_by_compatible_context ( $context );
2020-03-17 18:40:10 +00:00
2019-12-09 22:02:19 +07:00
$action = $this -> get_compatible_action ( $action , $context );
2020-03-17 18:40:10 +00:00
2020-04-15 17:23:10 +02:00
if ( 'profiles' == $context ) {
if ( 'created' == $action || 'deleted' == $action ) {
$context = 'users' ; // see class-connector-user.php.
2019-12-09 22:02:19 +07:00
}
}
2020-05-12 18:19:20 +00:00
return $this -> get_section_loop_records ( $records , $tokens , $connector , $context , $action , $skip_records );
2020-05-13 01:18:02 +07:00
}
2020-03-17 18:40:10 +00:00
2020-05-12 18:19:20 +00:00
public function get_section_loop_records ( $records , $tokens , $connector , $context , $action , $skip_records ) {
$loops = array ();
$loop_count = 0 ;
2020-03-17 18:40:10 +00:00
foreach ( $records as $record ) {
2020-05-05 13:19:34 +00:00
2020-04-15 17:23:10 +02:00
if ( in_array ( $record -> ID , $skip_records ) ) {
2020-01-17 19:42:57 +07:00
continue ;
2017-08-24 20:41:12 +02:00
}
2020-03-17 18:40:10 +00:00
2020-04-15 17:23:10 +02:00
if ( 'editor' == $record -> connector ) {
if ( ! in_array ( $context , array ( 'plugins' , 'themes' ) ) || 'updated' !== $action ) {
2020-03-17 18:40:10 +00:00
continue ;
}
} elseif ( $connector !== $record -> connector ) {
2019-12-09 22:02:19 +07:00
continue ;
}
2020-03-17 18:40:10 +00:00
2020-05-05 20:13:38 +07:00
$valid_context = false ;
2020-05-05 13:19:34 +00:00
2020-04-15 17:23:10 +02:00
if ( 'comments' == $context ) {
2020-03-17 18:40:10 +00:00
$comment_contexts = array ( 'post' , 'page' );
2019-12-09 22:02:19 +07:00
if ( ! in_array ( $record -> context , $comment_contexts ) ) {
2015-10-15 22:52:37 +10:00
continue ;
2020-03-17 18:40:10 +00:00
}
2020-05-05 13:19:34 +00:00
$valid_context = true ;
2020-04-15 17:23:10 +02:00
} elseif ( 'menus' == $context ) {
2020-05-05 20:13:38 +07:00
$valid_context = true ; // ok, pass, don't check context.
2020-04-15 17:23:10 +02:00
} elseif ( 'editor' == $record -> connector ) {
2020-05-05 20:13:38 +07:00
$valid_context = true ; // ok, pass, checked above.
2020-04-15 17:23:10 +02:00
} elseif ( 'media' == $connector && 'media' == $record -> connector ) {
2020-05-05 20:13:38 +07:00
$valid_context = true ; // ok, pass, do not check context.
2020-04-15 17:23:10 +02:00
} elseif ( 'widgets' == $connector && 'widgets' == $record -> connector ) {
2020-05-05 13:19:34 +00:00
$valid_context = true ; // ok, pass, don't check context.
}
if ( ! $valid_context || strtolower ( $record -> context ) !== $context ) {
2019-12-09 22:02:19 +07:00
continue ;
2020-03-17 18:40:10 +00:00
}
2020-04-15 17:23:10 +02:00
// custom action value!
if ( 'widgets' == $connector ) {
if ( 'deleted' == $action ) {
$action = 'removed' ; // action saved in database!
2020-03-17 18:40:10 +00:00
}
2019-12-09 22:02:19 +07:00
}
2020-03-17 18:40:10 +00:00
2020-04-15 17:23:10 +02:00
if ( 'backups' == $context ) {
if ( ! $this -> is_backup_action ( $record -> action ) ) {
2015-10-15 22:52:37 +10:00
continue ;
}
2020-03-17 18:40:10 +00:00
} elseif ( $action !== $record -> action ) {
2019-12-09 22:02:19 +07:00
continue ;
}
2015-10-15 22:52:37 +10:00
2019-12-09 22:02:19 +07:00
if ( 'updated' === $action && ( 'post' === $context || 'page' === $context ) ) {
$new_status = $this -> get_stream_meta_data ( $record , 'new_status' );
2020-04-15 17:23:10 +02:00
if ( 'draft' === $new_status ) { // avoid auto save post!
2015-10-15 22:52:37 +10:00
continue ;
}
2020-05-19 01:15:36 +07:00
}
$token_values = $this -> get_section_loop_token_values ( $record , $context , $tokens );
if ( ! empty ( $token_values ) ) {
$loops [ $loop_count ] = $token_values ;
$loop_count ++ ;
2020-03-17 18:40:10 +00:00
}
2020-05-19 01:15:36 +07:00
}
return $loops ;
}
2020-03-17 18:40:10 +00:00
2020-05-19 01:15:36 +07:00
private function get_section_loop_token_values ( $record , $context , $tokens ){
$token_values = array ();
foreach ( $tokens as $token ) {
$data = '' ;
$token_name = str_replace ( array ( '[' , ']' ), '' , $token );
$array_tmp = explode ( '.' , $token_name );
if ( 'user.name' === $token_name ) {
$data = 'display_name' ;
} else {
if ( 1 === count ( $array_tmp ) ) {
list ( $data ) = $array_tmp ;
} elseif ( 2 === count ( $array_tmp ) ) {
list ( $str1 , $data ) = $array_tmp ;
} elseif ( 3 === count ( $array_tmp ) ) {
list ( $str1 , $str2 , $data ) = $array_tmp ;
2015-10-15 22:52:37 +10:00
}
2020-05-19 01:15:36 +07:00
if ( 'version' === $data ) {
if ( 'old' === $str2 ) {
$data = 'old_version' ;
} elseif ( 'current' === $str2 && 'WordPress' === $str1 ) {
$data = 'new_version' ;
}
2015-10-15 22:52:37 +10:00
}
2020-05-19 01:15:36 +07:00
}
2015-10-15 22:52:37 +10:00
2020-05-19 01:15:36 +07:00
if ( 'role' === $data ) {
$data = 'roles' ;
}
2020-05-12 18:19:20 +00:00
2020-05-19 01:15:36 +07:00
$tok_value = $this -> get_section_loop_token_value ( $record , $data , $context , $token );
2020-03-17 18:40:10 +00:00
2020-05-19 01:15:36 +07:00
$token_values [ $token ] = $tok_value ;
2020-03-17 18:40:10 +00:00
2020-05-19 01:15:36 +07:00
if ( empty ( $tok_value ) ) {
$msg = 'MainWP Child Report:: skip empty value :: token :: ' . $token . ' :: record :: ' . print_r ( $record , true ); // phpcs:ignore -- debug mode only.
MainWP_Helper :: log_debug ( $msg );
2020-05-13 01:18:02 +07:00
}
2020-05-19 01:15:36 +07:00
}
return $token_values ;
2020-05-13 01:18:02 +07:00
}
2020-05-19 01:15:36 +07:00
2020-05-13 01:18:02 +07:00
public function get_section_loop_token_value ( $record , $data , $context , $token ) {
$tok_value = '' ;
switch ( $data ) {
case 'ID' :
$tok_value = $record -> ID ;
break ;
case 'date' :
$tok_value = MainWP_Helper :: format_date ( MainWP_Helper :: get_timestamp ( strtotime ( $record -> created ) ) );
break ;
case 'time' :
$tok_value = MainWP_Helper :: format_time ( MainWP_Helper :: get_timestamp ( strtotime ( $record -> created ) ) );
break ;
case 'area' :
$data = 'sidebar_name' ;
$tok_value = $this -> get_stream_meta_data ( $record , $data );
break ;
case 'name' :
case 'version' :
case 'old_version' :
case 'new_version' :
case 'display_name' :
case 'roles' :
if ( 'name' == $data ) {
if ( 'profiles' == $context ) {
$data = 'display_name' ;
}
}
2020-05-19 01:15:36 +07:00
$tok_value = $this -> get_stream_meta_data ( $record , $data );
2020-05-13 01:18:02 +07:00
break ;
case 'title' :
if ( 'comments' === $context ) {
$tok_value = $record -> summary ;
} else {
if ( 'page' === $context || 'post' === $context ) {
$data = 'post_title' ;
} elseif ( 'menus' === $record -> connector ) {
$data = 'name' ;
}
$tok_value = $this -> get_stream_meta_data ( $record , $data );
}
break ;
2020-05-19 01:15:36 +07:00
case 'author' :
$tok_value = $this -> get_author_data_token_value ( $record , $connector , $context , $data );;
2020-05-13 01:18:02 +07:00
break ;
case 'status' :
case 'webtrust' :
2020-05-19 01:15:36 +07:00
$value = '' ;
2020-05-13 01:18:02 +07:00
if ( 'sucuri_scan' === $context ) {
2020-05-19 01:15:36 +07:00
$value = $this -> get_sucuri_scan_token_value ( $record , $data );
2020-05-13 01:18:02 +07:00
}
2020-05-19 01:15:36 +07:00
$tok_value = $value ;
2020-05-13 01:18:02 +07:00
break ;
case 'details' :
case 'result' :
2020-05-19 01:15:36 +07:00
$tok_value = $this -> get_result_data_token_value ( $record , $context , $data );
2020-05-13 01:18:02 +07:00
break ;
case 'type' :
if ( 'backups' === $context ) {
$tok_value = $this -> get_stream_meta_data ( $record , $data );
} else {
$tok_value = $token ;
}
break ;
default :
$tok_value = 'N/A' ;
break ;
2020-04-15 17:23:10 +02:00
}
2020-05-13 01:18:02 +07:00
return $tok_value ;
2015-10-15 22:52:37 +10:00
}
2020-04-15 17:23:10 +02:00
public function get_stream_meta_data ( $record , $data ) {
2015-10-15 22:52:37 +10:00
if ( empty ( $record ) ) {
return '' ;
}
2017-08-24 20:41:12 +02:00
$meta_key = $data ;
2015-10-15 22:52:37 +10:00
$value = '' ;
if ( isset ( $record -> meta ) ) {
$meta = $record -> meta ;
2020-05-04 23:02:02 +07:00
if ( isset ( $meta [ $meta_key ] ) ) {
2015-10-15 22:52:37 +10:00
$value = $meta [ $meta_key ];
2020-04-15 17:23:10 +02:00
$value = ( 'user_meta' == $meta_key && isset ( $value [ 1 ] ) ) ? $value [ 1 ] : current ( $value );
2020-05-04 23:02:02 +07:00
2020-03-17 18:40:10 +00:00
if ( 'author_meta' === $meta_key ) {
$value = maybe_unserialize ( $value );
2020-04-15 17:23:10 +02:00
if ( is_array ( $value ) ) {
2019-12-13 20:27:05 +07:00
$value = $value [ 'display_name' ];
2020-04-15 17:23:10 +02:00
// fix empty author value!
if ( empty ( $value ) ) {
if ( isset ( $value [ 'agent' ] ) && ! empty ( $value [ 'agent' ] ) ) {
2019-12-13 20:27:05 +07:00
$value = $value [ 'agent' ];
}
}
}
2020-04-15 17:23:10 +02:00
if ( ! is_string ( $value ) ) {
2020-03-17 18:40:10 +00:00
$value = '' ;
}
2019-12-13 20:27:05 +07:00
}
2020-03-17 18:40:10 +00:00
}
2015-10-15 22:52:37 +10:00
}
return $value ;
}
2020-05-19 01:15:36 +07:00
private function get_author_data_token_value ( $record , $connector , $context , $data ){
if ( 'comment' == $connector ) {
$data = 'user_name' ;
} else {
$data = 'user_meta' ;
}
$value = $this -> get_stream_meta_data ( $record , $data );
if ( empty ( $value ) && 'comments' === $context ) {
$value = __ ( 'Guest' , 'mainwp-child' );
}
// check compatibility with old meta data.
if ( empty ( $value ) ) {
$value = $this -> get_stream_meta_data ( $record , 'author_meta' );
}
return $value ;
}
private function get_result_data_token_value ( $record , $context , $data ){
if ( 'mainwp_maintenance' === $context && 'details' == $data ) {
$tok_value = $this -> get_mainwp_maintenance_token_value ( $record , $data );
} elseif ( 'wordfence_scan' === $context || 'mainwp_maintenance' === $context ) {
$meta_value = $this -> get_stream_meta_data ( $record , $data );
if ( 'wordfence_scan' === $context && 'result' == $data ) {
// SUM_FINAL:Scan complete. You have xxx new issues to fix. See below.
// SUM_FINAL:Scan complete. Congratulations, no new problems found.
if ( stripos ( $meta_value , 'Congratulations' ) ) {
$meta_value = 'No issues detected' ;
} elseif ( stripos ( $meta_value , 'You have' ) ) {
$meta_value = 'Issues Detected' ;
} else {
$meta_value = '' ;
}
}
$tok_value = $meta_value ;
}
return $tok_value ;
}
2020-05-15 23:53:53 +07:00
private function get_sucuri_scan_token_value ( $record , $data ) {
2020-05-19 01:15:36 +07:00
$tok_value = '' ;
$scan_data = $this -> get_stream_meta_data ( $record , 'scan_data' );
2020-05-15 23:53:53 +07:00
if ( ! empty ( $scan_data ) ) {
$scan_data = maybe_unserialize ( base64_decode ( $scan_data ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode function is used for begin reasons.
if ( is_array ( $scan_data ) ) {
$blacklisted = $scan_data [ 'blacklisted' ];
$malware_exists = $scan_data [ 'malware_exists' ];
$status = array ();
if ( $blacklisted ) {
$status [] = __ ( 'Site Blacklisted' , 'mainwp-child' ); }
if ( $malware_exists ) {
$status [] = __ ( 'Site With Warnings' , 'mainwp-child' ); }
if ( 'status' == $data ) {
$tok_value = count ( $status ) > 0 ? implode ( ', ' , $status ) : __ ( 'Verified Clear' , 'mainwp-child' );
} elseif ( 'webtrust' == $data ) {
$tok_value = $blacklisted ? __ ( 'Site Blacklisted' , 'mainwp-child' ) : __ ( 'Trusted' , 'mainwp-child' );
}
}
} else {
$tok_value = $this -> get_stream_meta_data ( $record , $data );
}
return $tok_value ;
}
2020-05-15 16:54:45 +00:00
2020-05-15 23:53:53 +07:00
private function get_mainwp_maintenance_token_value ( $record , $data ) {
2020-05-18 13:16:52 +00:00
2020-05-18 20:15:34 +07:00
$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' ),
);
2020-05-18 13:16:52 +00:00
2020-05-15 23:53:53 +07:00
$meta_value = $this -> get_stream_meta_data ( $record , $data );
$meta_value = explode ( ',' , $meta_value );
$details = array ();
if ( is_array ( $meta_value ) ) {
foreach ( $meta_value as $mt ) {
if ( isset ( $maintenance_details [ $mt ] ) ) {
if ( 'revisions_max' == $mt ) {
$max_revisions = $this -> get_stream_meta_data ( $record , 'revisions' );
$dtl = $maintenance_details [ 'revisions_max' ] . ' ' . $max_revisions ;
} else {
$dtl = $maintenance_details [ $mt ];
}
$details [] = $dtl ;
}
}
}
$tok_value = implode ( ', ' , $details );
return $tok_value ;
}
2020-04-15 17:29:26 +02:00
public function set_showhide () {
2020-03-17 18:40:10 +00:00
$hide = isset ( $_POST [ 'showhide' ] ) && ( 'hide' === $_POST [ 'showhide' ] ) ? 'hide' : '' ;
2020-05-06 20:22:11 +07:00
MainWP_Child_Branding :: instance () -> save_branding_options ( 'hide_child_reports' , $hide );
2015-10-15 22:52:37 +10:00
$information [ 'result' ] = 'SUCCESS' ;
2020-03-17 18:40:10 +00:00
return $information ;
2015-10-15 22:52:37 +10:00
}
public function creport_init () {
2020-05-06 20:22:11 +07:00
$branding_opts = MainWP_Child_Branding :: instance () -> get_branding_options ();
2020-03-17 18:40:10 +00:00
$hide_nag = false ;
2020-04-15 17:23:10 +02:00
if ( isset ( $branding_opts [ 'hide_child_reports' ] ) && 'hide' == $branding_opts [ 'hide_child_reports' ] ) {
2020-03-17 18:40:10 +00:00
add_filter ( 'all_plugins' , array ( $this , 'creport_branding_plugin' ) );
add_action ( 'admin_menu' , array ( $this , 'creport_remove_menu' ) );
$hide_nag = true ;
}
if ( ! $hide_nag ) {
2020-04-15 17:23:10 +02:00
// check child branding settings!
2020-05-06 20:22:11 +07:00
if ( MainWP_Child_Branding :: instance () -> is_branding () ) {
2020-03-17 18:40:10 +00:00
$hide_nag = true ;
}
}
if ( $hide_nag ) {
add_filter ( 'site_transient_update_plugins' , array ( & $this , 'remove_update_nag' ) );
add_filter ( 'mainwp_child_hide_update_notice' , array ( & $this , 'hide_update_notice' ) );
}
2015-10-15 22:52:37 +10:00
}
2020-04-15 17:23:10 +02:00
public function hide_update_notice ( $slugs ) {
2020-03-17 18:40:10 +00:00
$slugs [] = 'mainwp-child-reports/mainwp-child-reports.php' ;
return $slugs ;
}
2018-12-19 17:01:08 +07:00
2020-04-15 17:23:10 +02:00
public function remove_update_nag ( $value ) {
2020-03-17 18:40:10 +00:00
if ( isset ( $_POST [ 'mainwpsignature' ] ) ) {
2015-10-15 22:52:37 +10:00
return $value ;
}
2018-12-19 17:01:08 +07:00
2020-03-17 18:40:10 +00:00
if ( ! MainWP_Helper :: is_screen_with_update () ) {
return $value ;
}
2018-12-19 17:01:08 +07:00
2015-10-15 22:52:37 +10:00
if ( isset ( $value -> response [ 'mainwp-child-reports/mainwp-child-reports.php' ] ) ) {
unset ( $value -> response [ 'mainwp-child-reports/mainwp-child-reports.php' ] );
}
return $value ;
}
public function creport_branding_plugin ( $plugins ) {
foreach ( $plugins as $key => $value ) {
$plugin_slug = basename ( $key , '.php' );
2017-01-14 13:16:52 +01:00
if ( 'mainwp-child-reports' === $plugin_slug ) {
2015-10-15 22:52:37 +10:00
unset ( $plugins [ $key ] );
}
}
return $plugins ;
}
public function creport_remove_menu () {
remove_menu_page ( 'mainwp_wp_stream' );
}
}