array(
'name' => __( 'Activity Log', 'mwp-al-ext' ),
'link' => add_query_arg( 'tab', 'activity-log', $extension_url ),
'render' => array( $this, 'tab_activity_log' ),
'save' => array( $this, 'tab_activity_log_save' ),
),
'settings' => array(
'name' => __( 'Extension Settings', 'mwp-al-ext' ),
'link' => add_query_arg( 'tab', 'settings', $extension_url ),
'render' => array( $this, 'tab_settings' ),
'save' => array( $this, 'tab_settings_save' ),
),
);
/**
* Filter: `mwpal_extension_tabs`
*
* This filter is used to filter the tabs of WSAL settings page.
*
* Setting tabs structure:
* $mwpal_extension_tabs['unique-tab-id'] = array(
* 'name' => Name of the tab,
* 'link' => Link of the tab,
* 'render' => This function is used to render HTML elements in the tab,
* 'name' => This function is used to save the related setting of the tab,
* );
*
* @param array $mwpal_extension_tabs – Array of extension tabs.
*/
$this->mwpal_extension_tabs = apply_filters( 'mwpal_extension_tabs', $mwpal_extension_tabs );
// Get the current tab.
$current_tab = filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_STRING );
$this->current_tab = empty( $current_tab ) ? 'activity-log' : $current_tab;
}
/**
* Filter MainWP Dashboard Menu
*
* Modify MainWP Dashboard menu to include activity log's menu.
*
* @param array $mwp_sub_menu – MainWP Sub-Menu.
* @return array
*/
public function mwp_left_menu_sub( $mwp_sub_menu ) {
$activity_log_key = false;
$extensions_menu = isset( $mwp_sub_menu['Extensions'] ) ? $mwp_sub_menu['Extensions'] : false;
if ( $extensions_menu ) {
foreach ( $extensions_menu as $key => $submenu ) {
if ( MWPAL_EXTENSION_NAME === $submenu[1] ) {
$activity_log_key = $key;
break;
}
}
// Set the menu name.
$mwp_sub_menu['Extensions'][ $activity_log_key ][0] = __( 'Activity Log', 'mwp-al-ext' );
$sub_menu_before = array_slice( $mwp_sub_menu['mainwp_tab'], 0, 2 );
$sub_menu_after = array_splice( $mwp_sub_menu['mainwp_tab'], 2 );
$activity_log = $mwp_sub_menu['Extensions'][ $activity_log_key ];
$activity_log[3] = '';
$mwp_sub_menu['mainwp_tab'][] = $activity_log;
$mwp_sub_menu['mainwp_tab'] = array_merge( $mwp_sub_menu['mainwp_tab'], $sub_menu_after );
unset( $mwp_sub_menu['Extensions'][ $activity_log_key ] );
}
return $mwp_sub_menu;
}
/**
* Filter MainWP Dropdown Menus
*
* Modify mainwp dropdown menu to include activity log's
* dropdown menu.
*
* @param array $mwp_dropdown_menu – Dropdown menus of MainWP.
* @return array
*/
public function mwp_sub_menu_dropdown( $mwp_dropdown_menu ) {
$mwp_dropdown_menu[ MWPAL_EXTENSION_NAME ] = array(
array(
__( 'Extension Settings', 'mwp-al-ext' ),
'admin.php?page=' . MWPAL_EXTENSION_NAME . '&tab=settings',
'',
),
);
return $mwp_dropdown_menu;
}
/**
* Add Activity Log Settings Tab.
*
* @param string $current_page – Path of the extension.
*/
public function activitylog_settings_tab( $current_page ) {
$activity_log = basename( $current_page, '.php' );
if ( 'activity-log-mainwp' !== $activity_log ) {
return;
}
$settings_url_args = array(
'page' => MWPAL_EXTENSION_NAME,
'tab' => 'settings',
);
$settings_tab_url = add_query_arg( $settings_url_args, admin_url( 'admin.php' ) );
?>
current_tab ) {
// Select2 styles.
wp_enqueue_style(
'mwpal-select2-css',
trailingslashit( MWPAL_BASE_URL ) . 'assets/js/dist/select2/select2.css',
array(),
'3.5.1'
);
wp_enqueue_style(
'mwpal-select2-bootstrap-css',
trailingslashit( MWPAL_BASE_URL ) . 'assets/js/dist/select2/select2-bootstrap.css',
array(),
'3.5.1'
);
}
// View styles.
wp_enqueue_style(
'mwpal-view-styles',
trailingslashit( MWPAL_BASE_URL ) . 'assets/css/dist/styles.build.css',
array(),
filemtime( trailingslashit( MWPAL_BASE_DIR ) . 'assets/css/dist/styles.build.css' )
);
}
/**
* Enqueue Scripts in Footer.
*/
public function enqueue_scripts() {
// Confirm extension page.
global $pagenow;
// @codingStandardsIgnoreStart
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : false;
// @codingStandardsIgnoreEnd
if ( 'admin.php' !== $pagenow ) {
return;
} elseif ( MWPAL_EXTENSION_NAME !== $page ) {
return;
}
// Enqueue jQuery.
wp_enqueue_script( 'jquery' );
if ( 'activity-log' === $this->current_tab ) {
// Select2 script.
wp_enqueue_script(
'mwpal-select2-js',
trailingslashit( MWPAL_BASE_URL ) . 'assets/js/dist/select2/select2.min.js',
array( 'jquery' ),
'3.5.1',
true
);
}
wp_register_script(
'mwpal-view-script',
trailingslashit( MWPAL_BASE_URL ) . 'assets/js/dist/index.js',
array( 'jquery' ),
filemtime( trailingslashit( MWPAL_BASE_DIR ) . 'assets/js/dist/index.js' ),
false
);
// JS data.
$script_data = array(
'ajaxURL' => admin_url( 'admin-ajax.php' ),
'scriptNonce' => wp_create_nonce( 'mwp-activitylog-nonce' ),
'currentTab' => $this->current_tab,
'selectSites' => __( 'Select Child Site(s)', 'mwp-al-ext' ),
'refreshing' => __( 'Refreshing Child Sites...', 'mwp-al-ext' ),
'retrieving' => __( 'Retrieving Logs...', 'mwp-al-ext' ),
);
wp_localize_script( 'mwpal-view-script', 'scriptData', $script_data );
wp_enqueue_script( 'mwpal-view-script' );
}
/**
* Handle Audit Log Form Submission.
*/
public function handle_auditlog_form_submission() {
// Global WP page now variable.
global $pagenow;
// Only run the function on audit log custom page.
// @codingStandardsIgnoreStart
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : false; // Current page.
// @codingStandardsIgnoreEnd
if ( 'admin.php' !== $pagenow ) {
return;
} elseif ( MWPAL_EXTENSION_NAME !== $page ) { // Page is admin.php, now check auditlog page.
return; // Return if the current page is not auditlog's.
}
if ( isset( $_GET['_wpnonce'] ) && 'activity-log' === $this->current_tab ) {
// Verify nonce for security.
check_admin_referer( 'bulk-activity-logs' );
// Site id.
$site_id = isset( $_GET['mwpal-site-id'] ) ? sanitize_text_field( wp_unslash( $_GET['mwpal-site-id'] ) ) : false;
// Check for dashboard.
if ( 'dashboard' !== $site_id ) {
$site_id = (int) $site_id;
}
// Remove args array.
$remove_args = array(
'_wp_http_referer',
'_wpnonce',
);
if ( empty( $site_id ) ) {
$remove_args[] = 'mwpal-site-id';
}
wp_safe_redirect( remove_query_arg( $remove_args ) );
exit();
} elseif ( isset( $_POST['_wpnonce'] ) && isset( $_POST['submit'] ) && 'settings' === $this->current_tab ) {
// Verify nonce for security.
check_admin_referer( 'mwpal-settings-nonce' );
// Get form options.
$timezone = isset( $_POST['timezone'] ) ? sanitize_text_field( wp_unslash( $_POST['timezone'] ) ) : false;
$type_username = isset( $_POST['type_username'] ) ? sanitize_text_field( wp_unslash( $_POST['type_username'] ) ) : false;
$child_site_events = isset( $_POST['child-site-events'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['child-site-events'] ) ) : false;
$events_frequency = isset( $_POST['events-frequency'] ) ? (int) sanitize_text_field( wp_unslash( $_POST['events-frequency'] ) ) : false;
// @codingStandardsIgnoreStart
$columns = isset( $_POST['columns'] ) ? array_map( 'sanitize_text_field', $_POST['columns'] ) : false;
$wsal_child_sites = isset( $_POST['mwpal-wsal-child-sites'] ) ? sanitize_text_field( wp_unslash( $_POST['mwpal-wsal-child-sites'] ) ) : false;
// @codingStandardsIgnoreEnd
$this->activity_log->settings->set_timezone( $timezone );
$this->activity_log->settings->set_type_username( $type_username );
$this->activity_log->settings->set_child_site_events( $child_site_events );
$this->activity_log->settings->set_events_frequency( $events_frequency );
$this->activity_log->settings->set_columns( $columns );
$this->activity_log->settings->set_wsal_child_sites( ! empty( $wsal_child_sites ) ? explode( ',', $wsal_child_sites ) : false );
}
}
/**
* Render Header.
*/
public function header() {
// The "mainwp-pageheader-extensions" action is used to render the tabs on the Extensions screen.
// It's used together with mainwp-pagefooter-extensions and mainwp-getextensions.
do_action( 'mainwp-pageheader-extensions', $this->activity_log->get_child_file() );
}
/**
* Render Content.
*/
public function content() {
// Fetch all child-sites.
$this->mwp_child_sites = $this->activity_log->settings->get_mwp_child_sites(); // Get MainWP child sites.
$this->wsal_child_sites = $this->activity_log->settings->get_wsal_child_sites(); // Get child sites with WSAL installed.
if ( $this->activity_log->is_child_enabled() ) {
?>
current_tab ) && ! empty( $this->mwpal_extension_tabs[ $this->current_tab ]['render'] ) ) {
call_user_func( $this->mwpal_extension_tabs[ $this->current_tab ]['render'] );
} else {
call_user_func( $this->mwpal_extension_tabs['activity-log']['render'] );
}
?>
get_list_view()->prepare_items();
$site_id = $this->activity_log->settings->get_view_site_id();
// @codingStandardsIgnoreStart
$mwp_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : false; // Admin WSAL Page.
// @codingStandardsIgnoreEnd
?>
activity_log->get_child_file() );
}
/**
* Query events from all the child sites.
*
* @return void
*/
private function query_child_site_events() {
// Check if the WSAL child sites option exists.
$child_sites = $this->activity_log->settings->get_wsal_child_sites();
$server_ip = $this->activity_log->settings->get_server_ip(); // Get server IP.
if ( ! empty( $child_sites ) && is_array( $child_sites ) ) {
$sites_data = array();
$logged_retrieving = false; // Event 7711.
$logged_ready = false; // Event 7712.
foreach ( $child_sites as $site_id => $child_site ) {
// Get events count from native events DB.
$occ_query = new \WSAL\MainWPExtension\Models\OccurrenceQuery();
$occ_query->addCondition( 'site_id = %s ', $site_id ); // Set site id.
$occ_count = (int) $occ_query->getAdapter()->Count( $occ_query );
// If events are already present in the DB of a site, then no need to query from child site.
if ( 0 !== $occ_count ) {
continue;
}
if ( ! $logged_retrieving ) {
// Extension has started retrieving.
$this->activity_log->alerts->trigger( 7711, array(
'mainwp_dash' => true,
'Username' => 'System',
'ClientIP' => ! empty( $server_ip ) ? $server_ip : false,
) );
$logged_retrieving = true;
}
// Post data for child sites.
$post_data = array(
'action' => 'get_events',
'events_count' => $this->activity_log->settings->get_child_site_events(),
);
// Call to child sites to fetch WSAL events.
$sites_data[ $site_id ] = apply_filters(
'mainwp_fetchurlauthed',
$this->activity_log->get_child_file(),
$this->activity_log->get_child_key(),
$site_id,
'extra_excution',
$post_data
);
if ( ! $logged_ready && isset( $sites_data[ $site_id ]->events ) ) {
// Extension is ready after retrieving.
$this->activity_log->alerts->trigger( 7712, array(
'mainwp_dash' => true,
'Username' => 'System',
'ClientIP' => ! empty( $server_ip ) ? $server_ip : false,
) );
$logged_ready = true;
}
}
if ( ! empty( $sites_data ) && is_array( $sites_data ) ) {
// Get MainWP child sites.
$mwp_sites = $this->activity_log->settings->get_mwp_child_sites();
foreach ( $sites_data as $site_id => $site_events ) {
// If $site_events is array, then MainWP failed to fetch logs from the child site.
if ( ! empty( $site_events ) && is_array( $site_events ) ) {
// Search for the site data.
$key = array_search( $site_id, array_column( $mwp_sites, 'id' ), false );
if ( false !== $key && isset( $mwp_sites[ $key ] ) ) {
// Extension is unable to retrieve events.
$this->activity_log->alerts->trigger( 7710, array(
'friendly_name' => $mwp_sites[ $key ]['name'],
'site_url' => $mwp_sites[ $key ]['url'],
'site_id' => $mwp_sites[ $key ]['id'],
'mainwp_dash' => true,
'Username' => 'System',
'ClientIP' => ! empty( $server_ip ) ? $server_ip : false,
) );
}
} elseif ( empty( $site_events ) || ! isset( $site_events->events ) ) {
continue;
}
$this->activity_log->alerts->log_events( $site_events->events, $site_id );
}
}
}
}
/**
* Get Extension's List Table Instance.
*
* @return AuditLogListView
*/
private function get_list_view() {
if ( is_null( $this->list_view ) ) {
$this->list_view = new AuditLogListView( $this->activity_log );
}
return $this->list_view;
}
/**
* Set Per Page Events
*/
public function set_per_page_events() {
if ( ! current_user_can( 'manage_options' ) ) {
die( esc_html__( 'Access denied.', 'mwp-al-ext' ) );
}
// @codingStandardsIgnoreStart
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : false;
$per_page_events = isset( $_POST['count'] ) ? sanitize_text_field( wp_unslash( $_POST['count'] ) ) : false;
// @codingStandardsIgnoreEnd
if ( ! empty( $nonce ) && wp_verify_nonce( $nonce, 'mwp-activitylog-nonce' ) ) {
if ( empty( $per_page_events ) ) {
die( esc_html__( 'Count parameter expected.', 'mwp-al-ext' ) );
}
$this->activity_log->settings->set_view_per_page( (int) $per_page_events );
die();
}
die( esc_html__( 'Nonce verification failed.', 'mwp-al-ext' ) );
}
/**
* Events Metadata Viewer
*/
public function metadata_inspector() {
if ( ! current_user_can( 'manage_options' ) ) {
die( esc_html__( 'Access denied.', 'mwp-al-ext' ) );
}
// @codingStandardsIgnoreStart
$nonce = isset( $_GET['mwp_meta_nonc'] ) ? sanitize_text_field( wp_unslash( $_GET['mwp_meta_nonc'] ) ) : false;
$occurrence_id = isset( $_GET['occurrence_id'] ) ? (int) sanitize_text_field( wp_unslash( $_GET['occurrence_id'] ) ) : false;
// @codingStandardsIgnoreEnd
if ( empty( $occurrence_id ) ) {
die( esc_html__( 'Occurrence ID parameter expected.', 'mwp-al-ext' ) );
}
if ( ! empty( $nonce ) && wp_verify_nonce( $nonce, 'mwp-meta-display-' . $occurrence_id ) ) {
$occurrence = new \WSAL\MainWPExtension\Models\Occurrence();
$occurrence->Load( 'id = %d', array( $occurrence_id ) );
$event_meta = $occurrence->GetMetaArray();
unset( $event_meta['ReportText'] );
// Set Event_Ref class scripts and styles.
\WSAL\MainWPExtension\Event_Ref::config( 'stylePath', trailingslashit( MWPAL_BASE_DIR ) . 'assets/css/dist/wsal-ref.css' );
\WSAL\MainWPExtension\Event_Ref::config( 'scriptPath', trailingslashit( MWPAL_BASE_DIR ) . 'assets/js/dist/wsal-ref.js' );
echo '';
echo '';
echo '';
\WSAL\MainWPExtension\mwpal_r( $event_meta );
echo '';
die;
}
die( esc_html__( 'Nonce verification failed.', 'mwp-al-ext' ) );
}
/**
* Refresh WSAL Child Sites
*/
public function refresh_child_sites() {
if ( ! current_user_can( 'manage_options' ) ) {
die( esc_html__( 'Access denied.', 'mwp-al-ext' ) );
}
if ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'mwp-activitylog-nonce' ) ) {
$mwp_child_sites = $this->activity_log->settings->get_mwp_child_sites(); // Get MainWP child sites.
$wsal_child_sites = $this->activity_log->settings->get_option( 'wsal-child-sites', array() ); // Get activity log sites.
$disabled_sites = $this->activity_log->settings->get_option( 'disabled-wsal-sites', array() ); // Get disabled WSAL sites.
$wsal_site_ids = array_merge( array_keys( $wsal_child_sites ), array_keys( $disabled_sites ) ); // Merge arrays active & disabled WSAL child sites.
$mwp_site_ids = array_column( $mwp_child_sites, 'id' ); // Get MainWP child site ids.
$diff = array_diff( $mwp_site_ids, $wsal_site_ids ); // Compute the difference.
if ( ! empty( $diff ) ) {
foreach ( $diff as $index => $site_id ) {
// Post data for child site.
$post_data = array( 'action' => 'check_wsal' );
// Call to child sites to check if WSAL is installed on them or not.
$response = apply_filters(
'mainwp_fetchurlauthed',
$this->activity_log->get_child_file(),
$this->activity_log->get_child_key(),
$site_id,
'extra_excution',
$post_data
);
// Check if WSAL is installed on the child site.
if ( true === $response->wsal_installed ) {
$disabled_sites[ $site_id ] = $response;
}
}
// Update disabled sites.
$this->activity_log->settings->update_option( 'disabled-wsal-sites', $disabled_sites );
}
die();
}
die( esc_html__( 'Nonce verification failed.', 'mwp-al-ext' ) );
}
/**
* Update Active WSAL Sites.
*/
public function update_active_wsal_sites() {
if ( ! current_user_can( 'manage_options' ) ) {
echo wp_json_encode( array(
'success' => false,
'message' => esc_html__( 'Access denied.', 'mwp-al-ext' ),
) );
exit();
}
if ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'mwp-activitylog-nonce' ) ) {
// Get $_POST data.
$transfer_action = isset( $_POST['transferAction'] ) ? sanitize_text_field( wp_unslash( $_POST['transferAction'] ) ) : false;
$active_sites = isset( $_POST['activeSites'] ) ? sanitize_text_field( wp_unslash( $_POST['activeSites'] ) ) : false;
$active_sites = ! empty( $active_sites ) ? explode( ',', $active_sites ) : array();
$request_sites = isset( $_POST['requestSites'] ) ? sanitize_text_field( wp_unslash( $_POST['requestSites'] ) ) : false;
$request_sites = explode( ',', $request_sites );
if ( 'remove-sites' === $transfer_action && ! empty( $request_sites ) ) {
foreach ( $request_sites as $site ) {
$key = array_search( $site, $active_sites, true );
if ( false !== $key ) {
unset( $active_sites[ $key ] );
}
}
echo wp_json_encode( array(
'success' => true,
'activeSites' => implode( ',', $active_sites ),
) );
} elseif ( 'add-sites' === $transfer_action && ! empty( $request_sites ) ) {
foreach ( $request_sites as $site ) {
$key = array_search( $site, $active_sites, true );
if ( false === $key ) {
$active_sites[] = $site;
}
}
echo wp_json_encode( array(
'success' => true,
'activeSites' => implode( ',', $active_sites ),
) );
} else {
echo wp_json_encode( array(
'success' => false,
'message' => esc_html__( 'Invalid action.', 'mwp-al-ext' ),
) );
}
} else {
echo wp_json_encode( array(
'success' => false,
'message' => esc_html__( 'Access denied.', 'mwp-al-ext' ),
) );
}
exit();
}
/**
* Retrieve Events Manually.
*
* To retrieve fresh logs, just delete the events of
* the site and refresh the page.
*/
public function retrieve_events_manually() {
if ( ! current_user_can( 'manage_options' ) ) {
die( esc_html__( 'Access denied.', 'mwp-al-ext' ) );
}
if ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'mwp-activitylog-nonce' ) ) {
// Get MainWP sites.
$mwp_sites = $this->activity_log->settings->get_wsal_child_sites();
if ( ! empty( $mwp_sites ) ) {
$trigger_retrieving = true; // Event 7711.
$trigger_ready = true; // Event 7712.
$server_ip = $this->activity_log->settings->get_server_ip(); // Get server IP.
foreach ( $mwp_sites as $site_id => $site ) {
// Delete events by site id.
$this->activity_log->alerts->delete_site_events( $site_id );
// Fetch events by site id.
$sites_data[ $site_id ] = $this->activity_log->alerts->fetch_site_events( $site_id, $trigger_retrieving );
// Set $trigger_retrieving to false to avoid logging 7711 multiple times.
$trigger_retrieving = false;
if ( $trigger_ready && isset( $sites_data[ $site_id ]->events ) ) {
// Extension is ready after retrieving.
$this->activity_log->alerts->trigger( 7712, array(
'mainwp_dash' => true,
'Username' => 'System',
'ClientIP' => ! empty( $server_ip ) ? $server_ip : false,
) );
$trigger_ready = false;
}
}
// Set child site events.
$this->activity_log->alerts->set_site_events( $sites_data );
}
die();
}
die( esc_html__( 'Nonce verification failed.', 'mwp-al-ext' ) );
}
}