array( 'href' => array(), 'title' => array(), 'target' => array(), ), ); /** * Constructor. */ public function __construct() { add_filter( 'mwpal_extension_tabs', array( $this, 'add_tab' ), 10, 2 ); // For MainWP v4 or later. add_filter( 'mwpal_page_navigation', array( $this, 'add_tab_v4plus' ), 10, 1 ); add_action( 'mainwp_pageheader_extensions', array( $this, 'enqueue_styles' ), 20 ); add_action( 'mainwp_pagefooter_extensions', array( $this, 'enqueue_scripts' ), 20 ); if ( \version_compare( MWPAL_Extension\get_mainwp_version(), '4.0-beta', '<' ) ) { add_action( 'admin_notices', array( $this, 'display_admin_notices' ) ); } else { add_action( 'mainwp_after_header', array( $this, 'display_admin_notices' ) ); } } /** * Add tab to the extension tabs. * * @param array $extension_tabs - Extension tabs array. * @param array $extension_url - URL of the extension. * * @return array */ public function add_tab( $extension_tabs, $extension_url ) { $position = array_search( 'settings', $extension_tabs ); $result = array_splice( $extension_tabs, 0, $position - 1 ); $result[ self::$tab_id ] = array( 'name' => __( 'Child Sites Activity Log Settings', 'mwp-al-ext' ), 'link' => add_query_arg( 'tab', self::$tab_id, $extension_url ), 'render' => array( $this, 'render' ), 'save' => array( $this, 'save' ), ); $result += array_splice( $extension_tabs, $position - 1 ); return $result; } /** * Add tab to the extension tabs for MainWP v4 or later. * * @param array $mwpal_tabs - Extension tabs array. * * @return array */ public function add_tab_v4plus( $mwpal_tabs ) { $extension_url = add_query_arg( 'page', MWPAL_EXTENSION_NAME, admin_url( 'admin.php' ) ); $position = count( $mwpal_tabs ) - 1; $result = array_splice( $mwpal_tabs, 0, $position ); $result[] = array( 'title' => __( 'Child Sites Activity Log Settings', 'mwp-al-ext' ), 'href' => add_query_arg( 'tab', self::$tab_id, $extension_url ), 'active' => self::$tab_id === MWPAL_Extension\mwpal_extension()->extension_view->get_current_tab(), ); $result = array_merge( $result, array_splice( $mwpal_tabs, $position ) ); return $result; } /** * Renders the content of the tab. */ public function render() { $settings = MWPAL_Extension\mwpal_extension()->settings; $mwp_sites = $settings->get_mwp_child_sites(); $wsal_child_sites = $settings->get_wsal_child_sites(); $enforce_settings_on_subsites = $settings->get_enforce_settings_on_subsites(); $sites_with_enforced_settings = array(); if ( 'some' === $enforce_settings_on_subsites ) { $sites_with_enforced_settings = $settings->get_sites_with_enforced_settings(); } $enforced_settings = $settings->get_enforced_child_sites_settings(); $pruning_date_enabled = array_key_exists( 'pruning_enabled', $enforced_settings ) ? $enforced_settings['pruning_enabled'] : 'no'; $pruning_date = 12; $pruning_date_unit = 'months'; if ( 'yes' === $pruning_date_enabled ) { $pruning_date = array_key_exists( 'pruning_date', $enforced_settings ) ? $enforced_settings['pruning_date'] : 12; $pruning_date_unit = array_key_exists( 'pruning_unit', $enforced_settings ) ? $enforced_settings['pruning_unit'] : 'months'; } $selected_events = ( array_key_exists( 'disabled_events', $enforced_settings ) && ! empty( $enforced_settings['disabled_events'] ) ) ? array_map( 'intval', explode( ',', $enforced_settings['disabled_events'] ) ) : array(); $incognito_mode_enabled = array_key_exists( 'incognito_mode_enabled', $enforced_settings ) ? $enforced_settings['incognito_mode_enabled'] : 'no'; $login_page_notification_enabled = array_key_exists( 'login_notification_enabled', $enforced_settings ) ? $enforced_settings['login_notification_enabled'] : 'no'; // login page notification text $default_login_text = __( 'For security and auditing purposes, a record of all of your logged-in actions and changes within the WordPress dashboard will be recorded in an activity log with the WP Activity Log plugin. The audit log also includes the IP address where you accessed this site from.', 'mwp-al-text' ); $login_page_notification_text = array_key_exists( 'login_notification_text', $enforced_settings ) ? $enforced_settings['login_notification_text'] : $default_login_text; ?>



' . esc_html__( 'list of activity log event IDs', 'mwp-al-ext' ) . '' ); ?>




Note: ', 'mwp-al-text' ), $this->allowed_tags ) . esc_html__( 'The only HTML code allowed in the login page notification is for links ( < a href >< /a > ).', 'mwp-al-text' ); ?>


settings; $previous_where_to_enforce_settings = $settings->get_enforce_settings_on_subsites(); $previously_enforced_settings = $settings->get_enforced_child_sites_settings(); $previous_list_of_subsites = $settings->get_sites_with_enforced_settings(); // incognito setting. $incognito_enabled = filter_input( INPUT_POST, 'incognito-enabled', FILTER_SANITIZE_STRING ); $incognito_enabled = ( 'yes' === $incognito_enabled ) ? 'yes' : 'no'; $settings_to_enforce['incognito_mode_enabled'] = $incognito_enabled; // login page notification. $login_page_notification_enabled = filter_input( INPUT_POST, 'login-page-notification', FILTER_SANITIZE_STRING ); if ( $login_page_notification_enabled === 'yes' ) { $settings_to_enforce['login_notification_enabled'] = 'yes'; $settings_to_enforce['login_notification_text'] = wp_kses( trim( $_POST['login-page-notification-text'] ), $this->allowed_tags ); } else { $settings_to_enforce['login_notification_enabled'] = 'no'; } // save settings to be enforced. $settings->set_enforced_child_sites_settings( $settings_to_enforce ); // trigger event 7715. mwpal_extension()->alerts->trigger( 7715 ); // what sites should have enforced settings? $where_to_enforce_settings = filter_input( INPUT_POST, 'enforce_settings_on_subsites', FILTER_SANITIZE_STRING ); $settings->set_enforce_settings_on_subsites( $where_to_enforce_settings ); if ( $previous_where_to_enforce_settings != $where_to_enforce_settings ) { // fire event 7713. mwpal_extension()->alerts->trigger( 7713, array( 'old_status' => $previous_where_to_enforce_settings, 'new_status' => $where_to_enforce_settings, ) ); } // assume no child sites selection - this will clear the previous site selection that might be set (passing // an empty array will result in deleting the option from the database). $child_sites_ids = array(); if ( 'some' === $where_to_enforce_settings ) { $child_sites_to_enforce = filter_input( INPUT_POST, 'mwpal-wsal-child-sites', FILTER_SANITIZE_STRING ); if ( is_string( $child_sites_to_enforce ) && ! empty( $child_sites_to_enforce ) ) { $child_sites_ids = array_map( 'intval', explode( ',', $child_sites_to_enforce ) ); } } // save the child sites selection. $settings->set_sites_with_enforced_settings( $child_sites_ids ); // send notifications to the child sites if necessary. $this->maybe_notify_child_sites( $previous_where_to_enforce_settings, $where_to_enforce_settings, $previously_enforced_settings, $settings_to_enforce, $previous_list_of_subsites, $child_sites_ids ); } } private function maybe_notify_child_sites( $previous_where_to_enforce_settings, $where_to_enforce_settings, $previously_enforced_settings, $settings_to_enforce, $previous_list_of_subsites, $child_sites_to_enforce ) { if ( $previous_where_to_enforce_settings == $where_to_enforce_settings && 'none' === $where_to_enforce_settings ) { // no notifications necessary return; } $settings = MWPAL_Extension\mwpal_extension()->settings; // check if the settings actually changed. $enforced_settings_changed = ( ! empty( array_diff( $previously_enforced_settings, $settings_to_enforce ) ) ); $sites_to_update = array(); $sites_to_remove = array(); $all_wsal_sites = array_keys( $settings->get_wsal_child_sites() ); switch ( $previous_where_to_enforce_settings ) { case 'all': if ( 'none' == $where_to_enforce_settings ) { // tell all sites to delete enforced settings. $sites_to_remove = $all_wsal_sites; } elseif ( 'some' == $where_to_enforce_settings ) { $sites_to_remove = array_diff( $all_wsal_sites, $child_sites_to_enforce ); // log event 7714: removed/ $this->trigger_site_addition_or_removal( $sites_to_remove, false ); if ( $enforced_settings_changed ) { $sites_to_update = $child_sites_to_enforce; } } elseif ( 'all' == $where_to_enforce_settings && $enforced_settings_changed ) { $sites_to_update = $all_wsal_sites; } break; case 'none': if ( 'all' == $where_to_enforce_settings ) { // all sites need to receive the enforced settings. $sites_to_update = $all_wsal_sites; } elseif ( 'some' == $where_to_enforce_settings ) { $sites_to_update = $child_sites_to_enforce; // log event 7714: added. $this->trigger_site_addition_or_removal( $sites_to_remove, true ); } break; case 'some': // check if the list of sites changed if ( 'all' === $where_to_enforce_settings ) { $child_sites_to_enforce = $all_wsal_sites; } // sites removed. $sites_removed = array_diff( $previous_list_of_subsites, $child_sites_to_enforce ); if ( ! empty( $sites_removed ) ) { $sites_to_remove = $sites_removed; // log event 7714: removed. $this->trigger_site_addition_or_removal( $sites_removed, false ); } // sites added $sites_added = array_diff( $child_sites_to_enforce, $previous_list_of_subsites ); if ( ! empty( $sites_added ) ) { $sites_to_update = $sites_added; // log event 7714: added. $this->trigger_site_addition_or_removal( $sites_removed, true ); } if ( $enforced_settings_changed ) { // if the settings changed we notify all sites in the list, not just the added ones. $sites_to_update = $child_sites_to_enforce; } break; default: // nothing. } if ( ! empty( $sites_to_remove ) || ! empty( $sites_to_update ) ) { // log event 7716:started. MWPAL_Extension\mwpal_extension()->alerts->trigger( 7716 ); } if ( ! empty( $sites_to_remove ) ) { $removal_process = new MWPAL_Extension\Enforce_Settings_Removal_Process(); foreach ( $sites_to_remove as $site_id ) { $removal_process->push_to_queue( array( 'site_id' => $site_id, ) ); } $removal_process->save()->dispatch(); } if ( ! empty( $sites_to_update ) ) { $update_process = new MWPAL_Extension\Enforce_Settings_Update_Process(); foreach ( $sites_to_update as $site_id ) { $update_process->push_to_queue( array( 'site_id' => $site_id, 'settings' => $settings_to_enforce, ) ); } $update_process->save()->dispatch(); } } protected function trigger_site_addition_or_removal( $site_ids, $is_addition ) { if ( is_array( $site_ids ) && ! empty( $site_ids ) ) { foreach ( $site_ids as $site_id ) { $site = mwpal_extension()->settings->get_mwp_child_site_by_id( $site_id ); if ( $site != null ) { mwpal_extension()->alerts->trigger( 7714, array( 'friendly_name' => $site['name'], 'site_url' => $site['url'], 'EventType' => $is_addition ? 'added' : 'removed', ) ); } } } } /** * Enqueue styles. */ public function enqueue_styles() { // not needed } /** * Enqueue scripts. */ public function enqueue_scripts() { // Extension page check. if ( ! MWPAL_Extension\mwpal_extension()->settings->is_current_extension_page() ) { return; } // current view page check if ( self::$tab_id !== MWPAL_Extension\mwpal_extension()->extension_view->get_current_tab() ) { return; } $script_handle = 'mwpal-enforced-settings-script'; $relative_script_path = 'assets/js/dist/build.enforced-settings.js'; wp_register_script( $script_handle, trailingslashit( MWPAL_BASE_URL ) . $relative_script_path, array( 'jquery', 'mwpal-select2-js' ), filemtime( trailingslashit( MWPAL_BASE_DIR ) . $relative_script_path ), false ); wp_localize_script( $script_handle, 'mwpal_enforced_settings', array( 'events' => json_encode( MWPAL_Extension\Helpers\DataHelper::get_events_for_select2() ), 'selectEvents' => __( 'Select event code(s)', 'mwp-al-ext' ), ) ); wp_enqueue_script( $script_handle ); } /** * Display admin notices (if any). */ public function display_admin_notices() { if ( ! MWPAL_Extension\mwpal_extension()->settings->is_current_extension_page() ) { return; } // current view page check if ( self::$tab_id !== MWPAL_Extension\mwpal_extension()->extension_view->get_current_tab() ) { return; } // @todo show admin notice when the setting are saved } } new Enforce_Settings_View();