load_filters(); $this->init_hooks(); } /** * Load filters. */ private function load_filters() { foreach ( glob( dirname( __FILE__ ) . '/filters/*.php' ) as $file ) { $this->add_from_file( $file ); } } /** * Initialize search filter hooks. */ private function init_hooks() { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_search_assets' ), 10, 1 ); add_action( 'mwpal_auditlog_before_view', array( $this, 'render_before_auditlog_view' ), 10, 1 ); add_action( 'admin_print_footer_scripts', array( $this, 'print_footer_js' ) ); add_filter( 'mwpal_auditlog_query', array( $this, 'modify_auditlog_query' ), 10, 1 ); } /** * Add new filter from file inside autoloader path. * * @param string $file - Path to file. */ private function add_from_file( $file ) { $this->add_from_class( $this->get_class_name( $file ) ); } /** * Add new filter given class name. * * @param string $class - Class name. */ private function add_from_class( $class ) { if ( $class ) { $this->filters[] = new $class(); if ( empty( $this->widgets ) ) { $this->widgets = null; } } } /** * Get class from file. * * @param string $file - File path. * @return string|bool */ private function get_class_name( $file ) { $filename = basename( $file, '.php' ); if ( false !== strpos( $filename, 'class-filter-' ) ) { $class = str_replace( 'class-', '', $filename ); $class = str_replace( '-', ' ', $class ); $class = ucwords( $class ); $class = str_replace( ' ', '_', $class ); $class = '\WSAL\MainWPExtension\Extensions\Search\\' . $class; return $class; } return false; } /** * Get filters. * * @return array */ public function get_filters() { return $this->filters; } /** * Gets widgets grouped in arrays with widget class as key. * * @return array */ public function get_widgets() { if ( null === $this->widgets ) { $this->widgets = array(); foreach ( $this->filters as $filter ) { foreach ( $filter->get_widgets() as $widget ) { $class = get_class( $widget ); if ( ! isset( $this->widgets[ $class ] ) ) { $this->widgets[ $class ] = array(); } $this->widgets[ $class ][] = $widget; } } } return $this->widgets; } /** * Find widget given filter and widget name. * * @param string $filter_name - Filter name. * @param string $widget_name - Widget name. * @return Abstract_Widget|null */ public function find_widget( $filter_name, $widget_name ) { foreach ( $this->filters as $filter ) { if ( $filter->get_safe_name() === $filter_name ) { foreach ( $filter->get_widgets() as $widget ) { if ( $widget->get_safe_name() === $widget_name ) { return $widget; } } } } return null; } /** * Find a filter given a supported prefix. * * @param string $prefix - Filter prefix. * @return Abstract_Filter|null */ public function find_filter_by_prefix( $prefix ) { foreach ( $this->filters as $filter ) { if ( in_array( $prefix, $filter->get_prefixes(), true ) ) { return $filter; } } return null; } /** * Enqueue search assets. * * @param string $hook_suffix - Admin page hook. */ public function enqueue_search_assets( $hook_suffix ) { $extension_hook = str_replace( 'mainwp_page_', '', $hook_suffix ); $current_tab = MWPAL_Extension\mwpal_extension()->extension_view->get_current_tab(); if ( MWPAL_EXTENSION_NAME === $extension_hook && 'activity-log' === $current_tab ) { // Datapicker styles. wp_enqueue_style( 'mwpal-daterangepicker-styles', MWPAL_BASE_URL . 'assets/js/dist/search/daterangepicker.css', array(), '3.1.0' ); // Search styles. wp_enqueue_style( 'mwpal-search-styles', MWPAL_SEARCH_URL . 'assets/css/search-styles.css', array(), filemtime( MWPAL_SEARCH_DIR . 'assets/css/search-styles.css' ) ); wp_enqueue_script( 'jquery-ui-dialog' ); wp_enqueue_script( 'mwpal-moment-script', MWPAL_BASE_URL . 'assets/js/dist/search/moment.min.js', array(), '2.22.1', true ); wp_enqueue_script( 'mwpal-daterangepicker-script', MWPAL_BASE_URL . 'assets/js/dist/search/daterangepicker.js', array( 'jquery' ), '3.1.0', true ); wp_register_script( 'mwpal-search-script', MWPAL_BASE_URL . 'assets/js/dist/search/build.search.js', array( 'jquery', 'jquery-ui-dialog', 'mwpal-moment-script', 'mwpal-daterangepicker-script' ), filemtime( MWPAL_BASE_DIR . 'assets/js/dist/search/build.search.js' ), true ); wp_localize_script( 'mwpal-search-script', 'searchScriptData', array( 'remove' => __( 'Remove', 'mwp-al-ext' ), 'filtersPopupTitle' => __( 'Search Filters', 'mwp-al-ext' ), 'dateFormat' => get_date_format(), 'extensionName' => MWPAL_EXTENSION_NAME, 'security' => wp_create_nonce( 'search-script-nonce' ), 'adminAjax' => admin_url( 'admin-ajax.php' ), 'filterBtnOpen' => __( 'Close Filters', 'mwp-al-ext' ), 'filterBtnClose' => __( 'Filter View', 'mwp-al-ext' ), 'filterChangeMsg' => sprintf( /* translators: both placeholders are html formatting strings for itallics */ __( 'Click the %1$sSearch%2$s or %1$sClear Search Results%2$s button to apply the new filters or reset all filters.', 'mwp-al-ext' ), '', '' ), ) ); wp_enqueue_script( 'mwpal-search-script' ); } } /** * Display search filters. * * @param MWPAL_Extension\Views\AuditLogView $audit_log_view Audit log view. * @param bool $search_available True is search is available for selected site. * @param string $site_name Site name. */ public function render_search_filters( $audit_log_view, $search_available, $site_name ) { if ( ! MWPAL_Extension\mwpal_extension()->settings->is_current_extension_page() ) { return; } if ( ! $search_available ) { return; } // Get current tab. $view_class = MWPAL_Extension\mwpal_extension()->extension_view; $current_tab = $view_class->get_current_tab(); if ( 'activity-log' !== $current_tab ) { return; } ?>
search_box_extended( $search_button_label, 'mwpal-search-box', ! $search_available, $site_name ); $search_filters = ( isset( $_REQUEST['filters'] ) && is_array( $_REQUEST['filters'] ) ) ? array_map( 'sanitize_text_field', $_REQUEST['filters'] ) : false; // phpcs:ignore if ( ! empty( $search_filters ) && is_array( $search_filters ) ) : ?>
$strings ) { // Add username filter widget. $filter = $this->find_filter_by_prefix( $handle ); // If username filter is found, then add to container. if ( $filter ) { ?>
Render(); ?>

get_allowed_html_tags() ); ?>

settings->is_current_extension_page() ) { return; } foreach ( $this->get_widgets() as $widgets ) { foreach ( $widgets as $widget ) { $widget->footer_js(); } } } /** * Filter the query. * * @param OccurrenceQuery $query - Audit log query. * @return OccurrenceQuery */ public function modify_auditlog_query( $query ) { // @codingStandardsIgnoreStart $search_term = ( isset( $_REQUEST['s'] ) && ! empty( $_REQUEST['s'] ) ) ? trim( sanitize_text_field( $_REQUEST['s'] ) ) : false; $search_filters = ( isset( $_REQUEST['filters'] ) && is_array( $_REQUEST['filters'] ) ) ? array_map( 'sanitize_text_field', $_REQUEST['filters'] ) : false; // @codingStandardsIgnoreEnd // Handle text search. if ( $search_term ) { // Handle free text search. $query->addSearchCondition( $search_term ); } // else { // fixes #4 (@see WP_List_Table::search_box). // $_REQUEST['s'] = ' '; // } // Handle filter search. $filters_arr = array(); if ( ! empty( $search_filters ) && is_array( $search_filters ) ) { foreach ( $search_filters as $filter ) { $filter = explode( ':', $filter, 2 ); if ( isset( $filter[1] ) ) { // Group the filter by type. $filters_arr[ $filter[0] ][] = $filter[1]; } } foreach ( $filters_arr as $prefix => $value ) { $the_filter = $this->find_filter_by_prefix( $prefix ); $the_filter->modify_query( $query, $prefix, $value ); } } return $query; } /** * Ajax handler to dismiss live search notice. */ public function dismiss_live_search_notice() { check_admin_referer( 'search-script-nonce', 'security' ); MWPAL_Extension\mwpal_extension()->settings->update_option( 'dismiss-live-search-notice', true ); echo true; die(); } public function get_allowed_html_tags() { return array( 'a' => array( 'href' => array(), 'title' => array(), 'target' => array(), ), 'br' => array(), 'code' => array(), 'em' => array(), 'strong' => array(), 'p' => array( 'class' => array(), ), ); } /** * @param MWPAL_Extension\Views\AuditLogView $audit_log_view Audit log view. */ public function render_before_auditlog_view( $audit_log_view ) { $search_available = true; $site_name = ''; if ( isset( $_REQUEST['mwpal-site-id'] ) && is_numeric( $_REQUEST['mwpal-site-id'] ) ) { $wsal_child_sites = MWPAL_Extension\mwpal_extension()->settings->get_mwp_child_sites(); $site_names = array_column( $wsal_child_sites, 'name', 'id' ); $current_site_id = (int) $_REQUEST['mwpal-site-id']; // Figure out if the search button should be disabled. $search_available = false; $mwp_sites = MWPAL_Extension\mwpal_extension()->settings->get_wsal_child_sites(); if ( is_array( $mwp_sites ) && array_key_exists( $current_site_id, $mwp_sites ) && is_array( $mwp_sites[ $current_site_id ] ) && true === $mwp_sites[ $current_site_id ]['wsal_installed'] && true === $mwp_sites[ $current_site_id ]['is_premium'] ) { $search_available = true; } $site_name = $site_names[ $current_site_id ]; } $this->display_search_field( $audit_log_view, $search_available, $site_name ); $this->render_search_filters( $audit_log_view, $search_available, $site_name ); } }