false, 'disable_post_revisions' => false, 'post_revisions_limit' => 10, 'disable_autosave' => false, 'disable_block_editor' => false, 'disable_file_editing' => false, // Security & API. 'disable_xmlrpc' => false, 'disable_rest_api_public' => false, 'disable_application_passwords' => false, 'disable_rest_output_link_wp_head' => false, 'disable_adjacent_posts_rel_link' => false, 'disable_rest_jsonp' => false, 'disable_rest_output_link_header' => false, 'remove_version_query_strings' => false, 'remove_x_pingback_header' => false, 'enable_security_headers' => false, 'disable_author_archives' => false, // Updates. 'disable_auto_updates_core' => false, 'disable_auto_updates_plugins' => false, 'disable_auto_updates_themes' => false, 'disable_update_notifications' => false, // Front-end / performance. 'disable_emojis' => false, 'disable_embeds' => false, 'disable_rss_feeds' => false, 'disable_self_pingbacks' => false, 'disable_wp_generator' => false, 'disable_wlw_manifest' => false, 'disable_rsd_link' => false, 'disable_shortlink' => false, // Media. 'disable_attachment_pages' => false, // Dequeue frontend assets. 'dequeue_wp_embed' => false, 'dequeue_wp_block_library' => false, 'dequeue_wp_block_library_theme' => false, 'dequeue_wc_block_style' => false, 'dequeue_jquery' => false, 'dequeue_jquery_migrate' => false, 'dequeue_global_styles' => false, 'dequeue_classic_styles' => false, 'dequeue_core_block_supports' => false, // Admin. 'disable_admin_bar_frontend' => false, 'hide_dashboard_events_news' => false, // Heartbeat control. 'heartbeat_control' => 'default', // default | disable | modify. 'heartbeat_frequency' => 60, // Seconds (15–300). 'heartbeat_admin_disable' => false, 'heartbeat_editor_disable' => false, 'heartbeat_frontend_disable' => false, // WP back-end extend. 'enable_last_login_column' => false, 'enable_registration_date_column' => false, 'enable_role_sorting' => false, ); } /** * Retrieve the stored WP Panel settings, merged with defaults. * * @return array * * @since 5.0.0 */ public static function get_settings(): array { if ( null === self::$panel_settings ) { $stored = \get_option( self::OPTION_NAME, array() ); self::$panel_settings = \wp_parse_args( \is_array( $stored ) ? $stored : array(), self::get_defaults() ); } return self::$panel_settings; } /** * Persist settings to the database. * * @param array $settings Sanitised settings array. * * @return void * * @since 5.0.0 */ private static function store_settings( array $settings ): void { // Use autoload=no to avoid loading on every request when the module is not active. if ( false === \get_option( self::OPTION_NAME, false ) ) { \add_option( self::OPTION_NAME, $settings, '', 'no' ); } else { \update_option( self::OPTION_NAME, $settings ); } self::$panel_settings = $settings; } // ------------------------------------------------------------------ // Apply settings at runtime // ------------------------------------------------------------------ /** * Hook into WordPress to enforce the stored panel settings. * * This method is called during plugin init and adds / removes the * necessary WordPress filters and actions. * * @return void * * @since 5.0.0 */ public static function apply_settings(): void { $s = self::get_settings(); // --- Content & editing ------------------------------------------- if ( ! empty( $s['disable_comments'] ) ) { \add_filter( 'comments_open', '__return_false', 20, 2 ); \add_filter( 'pings_open', '__return_false', 20, 2 ); \add_filter( 'comments_array', '__return_empty_array', 10, 2 ); \add_action( 'admin_menu', function () { \remove_menu_page( 'edit-comments.php' ); } ); \add_action( 'init', function () { \remove_post_type_support( 'post', 'comments' ); \remove_post_type_support( 'page', 'comments' ); \remove_post_type_support( 'post', 'trackbacks' ); \remove_post_type_support( 'page', 'trackbacks' ); }, 100 ); \add_action( 'wp_before_admin_bar_render', function () { global $wp_admin_bar; $wp_admin_bar->remove_menu( 'comments' ); } ); } if ( ! empty( $s['disable_post_revisions'] ) ) { if ( ! \defined( 'WP_POST_REVISIONS' ) ) { \define( 'WP_POST_REVISIONS', false ); } } elseif ( ! empty( $s['post_revisions_limit'] ) && is_numeric( $s['post_revisions_limit'] ) ) { if ( ! \defined( 'WP_POST_REVISIONS' ) ) { \define( 'WP_POST_REVISIONS', (int) $s['post_revisions_limit'] ); } } if ( ! empty( $s['disable_autosave'] ) ) { \add_action( 'admin_enqueue_scripts', function () { \wp_deregister_script( 'autosave' ); } ); } if ( ! empty( $s['disable_block_editor'] ) ) { \add_filter( 'use_block_editor_for_post', '__return_false', 10 ); \add_filter( 'use_block_editor_for_post_type', '__return_false', 10 ); } if ( ! empty( $s['disable_file_editing'] ) ) { if ( ! \defined( 'DISALLOW_FILE_EDIT' ) ) { \define( 'DISALLOW_FILE_EDIT', true ); } } // --- Security & API ---------------------------------------------- if ( ! empty( $s['disable_xmlrpc'] ) ) { \add_filter( 'xmlrpc_enabled', '__return_false' ); \add_filter( 'wp_headers', function ( $headers ) { unset( $headers['X-Pingback'] ); return $headers; } ); } if ( ! empty( $s['disable_rest_api_public'] ) ) { \add_filter( 'rest_authentication_errors', function ( $result ) { if ( true === $result || \is_wp_error( $result ) ) { return $result; } if ( ! \is_user_logged_in() ) { return new \WP_Error( 'rest_not_logged_in', \__( 'You are not currently logged in.', '0-day-analytics' ), array( 'status' => 401 ) ); } return $result; } ); } if ( ! empty( $s['disable_application_passwords'] ) ) { \add_filter( 'wp_is_application_passwords_available', '__return_false' ); } if ( ! empty( $s['disable_rest_output_link_wp_head'] ) ) { \remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); } if ( ! empty( $s['disable_adjacent_posts_rel_link'] ) ) { \remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); } if ( ! empty( $s['disable_rest_jsonp'] ) ) { \add_filter( 'rest_jsonp_enabled', '__return_false' ); } if ( ! empty( $s['disable_rest_output_link_header'] ) ) { \remove_action( 'template_redirect', 'rest_output_link_header', 11 ); } if ( ! empty( $s['remove_version_query_strings'] ) ) { \add_filter( 'style_loader_src', array( __CLASS__, 'strip_version_query' ), 9999 ); \add_filter( 'script_loader_src', array( __CLASS__, 'strip_version_query' ), 9999 ); } if ( ! empty( $s['remove_x_pingback_header'] ) ) { \add_filter( 'wp_headers', function ( $headers ) { unset( $headers['X-Pingback'] ); return $headers; } ); } if ( ! empty( $s['enable_security_headers'] ) ) { \add_action( 'send_headers', function () { if ( ! \headers_sent() ) { \header( 'X-Frame-Options: SAMEORIGIN' ); \header( 'X-Content-Type-Options: nosniff' ); \header( 'Referrer-Policy: no-referrer-when-downgrade' ); } } ); } if ( ! empty( $s['disable_author_archives'] ) ) { \add_action( 'template_redirect', function () { if ( \is_author() ) { \wp_redirect( \home_url(), 301 ); exit; } }, 0 ); } // --- Updates ----------------------------------------------------- if ( ! empty( $s['disable_auto_updates_core'] ) ) { \add_filter( 'auto_update_core', '__return_false' ); \add_filter( 'allow_major_auto_core_updates', '__return_false' ); \add_filter( 'allow_minor_auto_core_updates', '__return_false' ); } if ( ! empty( $s['disable_auto_updates_plugins'] ) ) { \add_filter( 'auto_update_plugin', '__return_false' ); } if ( ! empty( $s['disable_auto_updates_themes'] ) ) { \add_filter( 'auto_update_theme', '__return_false' ); } if ( ! empty( $s['disable_update_notifications'] ) ) { \add_action( 'admin_head', function () { \remove_action( 'admin_notices', 'update_nag', 3 ); }, 1 ); } // --- Front-end / performance ------------------------------------- if ( ! empty( $s['disable_emojis'] ) ) { \remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); \remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); \remove_action( 'wp_print_styles', 'print_emoji_styles' ); \remove_action( 'admin_print_styles', 'print_emoji_styles' ); \remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); \remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); \remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); \add_filter( 'tiny_mce_plugins', function ( $plugins ) { if ( \is_array( $plugins ) ) { return \array_diff( $plugins, array( 'wpemoji' ) ); } return array(); } ); \add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) { if ( 'dns-prefetch' === $relation_type ) { $urls = \array_values( \array_filter( $urls, function ( $url ) { return false === \strpos( (string) $url, 'https://s.w.org/images/core/emoji/' ); } ) ); } return $urls; }, 10, 2 ); } if ( ! empty( $s['disable_embeds'] ) ) { \remove_action( 'rest_api_init', 'wp_oembed_register_route' ); \remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); \remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); \remove_action( 'wp_head', 'wp_oembed_add_host_js' ); \add_filter( 'embed_oembed_discover', '__return_false' ); \add_filter( 'tiny_mce_plugins', function ( $plugins ) { return \array_diff( $plugins, array( 'wpembed' ) ); } ); \add_filter( 'wp_resource_hints', function ( $urls, $relation_type ) { if ( 'dns-prefetch' === $relation_type ) { $urls = \array_values( \array_filter( $urls, function ( $url ) { return false === \strpos( (string) $url, 'wp.com' ); } ) ); } return $urls; }, 10, 2 ); } if ( ! empty( $s['disable_rss_feeds'] ) ) { \add_action( 'do_feed', function () { \wp_die( \esc_html__( 'Feed templates are disabled in 0-day Analytics WP Panel module.', '0-day-analytics' ), '', array( 'response' => 404 ) ); }, 1 ); \add_action( 'do_feed_rdf', function () { \wp_die( \esc_html__( 'No RDF feed available - disabled in 0-day Analytics WP Panel module.', '0-day-analytics' ), '', array( 'response' => 404 ) ); }, 1 ); \add_action( 'do_feed_rss', function () { \wp_die( \esc_html__( 'No RSS feed available - disabled in 0-day Analytics WP Panel module.', '0-day-analytics' ), '', array( 'response' => 404 ) ); }, 1 ); \add_action( 'do_feed_rss2', function () { \wp_die( \esc_html__( 'No RSS2 feed available - disabled in 0-day Analytics WP Panel module.', '0-day-analytics' ), '', array( 'response' => 404 ) ); }, 1 ); \add_action( 'do_feed_atom', function () { \wp_die( \esc_html__( 'No Atom feed available - disabled in 0-day Analytics WP Panel module.', '0-day-analytics' ), '', array( 'response' => 404 ) ); }, 1 ); \remove_action( 'wp_head', 'feed_links_extra', 3 ); \remove_action( 'wp_head', 'feed_links', 2 ); } if ( ! empty( $s['disable_self_pingbacks'] ) ) { \add_action( 'pre_ping', function ( &$links ) { $home = \get_option( 'home' ); foreach ( $links as $l => $link ) { if ( 0 === \strpos( $link, $home ) ) { unset( $links[ $l ] ); } } } ); } if ( ! empty( $s['disable_wp_generator'] ) ) { \remove_action( 'wp_head', 'wp_generator' ); \add_filter( 'the_generator', '__return_empty_string' ); } if ( ! empty( $s['disable_wlw_manifest'] ) ) { \remove_action( 'wp_head', 'wlwmanifest_link' ); } if ( ! empty( $s['disable_rsd_link'] ) ) { \remove_action( 'wp_head', 'rsd_link' ); } if ( ! empty( $s['disable_shortlink'] ) ) { \remove_action( 'wp_head', 'wp_shortlink_wp_head' ); \remove_action( 'template_redirect', 'wp_shortlink_header', 11 ); } // --- Dequeue frontend assets ------------------------------------- if ( ! empty( $s['dequeue_wp_embed'] ) || ! empty( $s['dequeue_wp_block_library'] ) || ! empty( $s['dequeue_wp_block_library_theme'] ) || ! empty( $s['dequeue_wc_block_style'] ) || ! empty( $s['dequeue_jquery'] ) || ! empty( $s['dequeue_jquery_migrate'] ) || ! empty( $s['dequeue_global_styles'] ) || ! empty( $s['dequeue_classic_styles'] ) || ! empty( $s['dequeue_core_block_supports'] ) ) { \add_action( 'wp_enqueue_scripts', array( __CLASS__, 'dequeue_wp_assets' ), 100000 ); } // --- Media ------------------------------------------------------- if ( ! empty( $s['disable_attachment_pages'] ) ) { \add_action( 'template_redirect', function () { if ( \is_attachment() ) { global $post; if ( $post && $post->post_parent ) { \wp_safe_redirect( \get_permalink( $post->post_parent ), 301 ); exit; } \wp_safe_redirect( \home_url(), 301 ); exit; } } ); } // --- Admin ------------------------------------------------------- if ( ! empty( $s['disable_admin_bar_frontend'] ) ) { \add_filter( 'show_admin_bar', '__return_false' ); } if ( ! empty( $s['hide_dashboard_events_news'] ) ) { \add_action( 'wp_dashboard_setup', array( __CLASS__, 'hide_dashboard_events_news' ), 100 ); \add_action( 'wp_network_dashboard_setup', array( __CLASS__, 'hide_dashboard_events_news' ), 100 ); \add_action( 'wp_user_dashboard_setup', array( __CLASS__, 'hide_dashboard_events_news' ), 100 ); \add_action( 'wp_ajax_dashboard_widgets', array( __CLASS__, 'suppress_dashboard_news_ajax' ), 0 ); \add_action( 'wp_ajax_get_community_events', array( __CLASS__, 'suppress_community_events_ajax' ), 0 ); \add_filter( 'pre_http_request', array( __CLASS__, 'suppress_dashboard_http_request' ), 10, 3 ); \add_action( 'wp_feed_options', array( __CLASS__, 'disable_dashboard_feed_cache' ), 10, 2 ); } // --- Heartbeat control ------------------------------------------- if ( isset( $s['heartbeat_control'] ) && 'default' !== $s['heartbeat_control'] ) { if ( 'disable' === $s['heartbeat_control'] ) { \add_action( 'init', function () { \wp_deregister_script( 'heartbeat' ); }, 1 ); } elseif ( 'modify' === $s['heartbeat_control'] ) { $freq = ! empty( $s['heartbeat_frequency'] ) ? (int) $s['heartbeat_frequency'] : 60; \add_filter( 'heartbeat_settings', function ( $settings ) use ( $freq ) { $settings['interval'] = $freq; return $settings; } ); } } // Per-location heartbeat disable. if ( ! empty( $s['heartbeat_admin_disable'] ) ) { \add_action( 'admin_enqueue_scripts', function () { // Only on admin pages, not the post editor. global $pagenow; if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) { \wp_deregister_script( 'heartbeat' ); } }, 1 ); } if ( ! empty( $s['heartbeat_editor_disable'] ) ) { \add_action( 'admin_enqueue_scripts', function () { global $pagenow; if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) { \wp_deregister_script( 'heartbeat' ); } }, 1 ); } if ( ! empty( $s['heartbeat_frontend_disable'] ) ) { \add_action( 'wp_enqueue_scripts', function () { \wp_deregister_script( 'heartbeat' ); }, 1 ); } // --- WP back-end extend ------------------------------------------ // Track last login. if ( ! empty( $s['enable_last_login_column'] ) ) { \add_action( 'wp_login', function ( $user_login, $user ) { \update_user_meta( $user->ID, self::LAST_LOGIN_META, \time() ); }, 10, 2 ); \add_filter( 'manage_users_columns', array( __CLASS__, 'add_last_login_column' ) ); \add_filter( 'manage_users_custom_column', array( __CLASS__, 'render_last_login_column' ), 10, 3 ); \add_filter( 'manage_users_sortable_columns', array( __CLASS__, 'sortable_last_login_column' ) ); \add_action( 'pre_get_users', array( __CLASS__, 'sort_by_last_login' ) ); } if ( ! empty( $s['enable_role_sorting'] ) ) { \add_filter( 'manage_users_sortable_columns', array( __CLASS__, 'sortable_role_column' ) ); \add_filter( 'users_list_table_query_args', array( __CLASS__, 'mark_role_sort_query' ) ); \add_action( 'pre_user_query', array( __CLASS__, 'sort_by_role' ) ); } // Registration date column. if ( ! empty( $s['enable_registration_date_column'] ) ) { \add_filter( 'manage_users_columns', array( __CLASS__, 'add_registration_date_column' ) ); \add_filter( 'manage_users_custom_column', array( __CLASS__, 'render_registration_date_column' ), 10, 3 ); \add_filter( 'manage_users_sortable_columns', array( __CLASS__, 'sortable_registration_date_column' ) ); } } /** * Remove the WordPress Events and News widget and its event localization. * * Runs after widgets are registered on the site, network, or user dashboard. * Removes the widget from every supported placement and prevents core from * localizing event data or updating the user's saved event location. * * @return void * * @since 5.4.0 */ public static function hide_dashboard_events_news(): void { \remove_action( 'admin_print_scripts-index.php', 'wp_localize_community_events' ); $screen = \get_current_screen(); $screen_id = $screen && in_array( $screen->id, array( 'dashboard', 'dashboard-network', 'dashboard-user' ), true ) ? $screen->id : 'dashboard'; foreach ( array( 'normal', 'side', 'advanced' ) as $context ) { \remove_meta_box( 'dashboard_primary', $screen_id, $context ); } } /** * Stop AJAX refreshes of the WordPress news widget before core runs. * * Hooks into `wp_ajax_dashboard_widgets` at priority zero. Terminates the * request when the GET widget identifier is `dashboard_primary`; other * widget requests continue to their registered handlers. * * @return void * * @since 5.4.0 */ public static function suppress_dashboard_news_ajax(): void { if ( isset( $_GET['widget'] ) && 'dashboard_primary' === $_GET['widget'] ) { \wp_die(); } } /** * Reject community-events AJAX requests before core fetches or caches data. * * Hooks into `wp_ajax_get_community_events` at priority zero. Sends a JSON * error response and terminates the request before core can write event * transients or location metadata. * * @return void * * @since 5.4.0 */ public static function suppress_community_events_ajax(): void { \wp_send_json_error( array( 'message' => \__( 'WordPress events are disabled by WP Panel.', '0-day-analytics' ) ) ); } /** * Determine whether a URL matches a configured dashboard news feed. * * Checks the default news and Planet feeds, plus translated and filtered * feed URLs. Compares hosts without case sensitivity and paths without a * trailing slash; query strings must match exactly. Scheme and port are * not part of the comparison. * * @param string $url Absolute feed URL to check. * * @return bool Whether the URL matches a dashboard news feed. * * @since 5.4.0 */ private static function is_dashboard_news_feed( string $url ): bool { $feeds = array( 'https://wordpress.org/news/feed/', 'https://planet.wordpress.org/feed/', \apply_filters( 'dashboard_primary_feed', \__( 'https://wordpress.org/news/feed/' ) ), \apply_filters( 'dashboard_secondary_feed', \__( 'https://planet.wordpress.org/feed/' ) ), ); $parts = \wp_parse_url( $url ); if ( ! is_array( $parts ) || empty( $parts['host'] ) ) { return false; } foreach ( $feeds as $feed ) { if ( ! is_string( $feed ) || '' === $feed ) { continue; } $target = \wp_parse_url( $feed ); if ( is_array( $target ) && isset( $target['host'] ) && 0 === strcasecmp( $parts['host'], $target['host'] ) && rtrim( $parts['path'] ?? '/', '/' ) === rtrim( $target['path'] ?? '/', '/' ) && ( $parts['query'] ?? '' ) === ( $target['query'] ?? '' ) ) { return true; } } return false; } /** * Prevent HTTP requests to dashboard news feeds and the events API. * * Filters `pre_http_request`. Returns an error for a suppressed endpoint * and preserves the existing preemptive value for other requests. * * @param false|array|\WP_Error $preempt Existing preemptive HTTP response, or false to continue. * @param array $args Parsed HTTP request arguments. Unused by this callback. * @param string $url Requested URL. * * @return false|array|\WP_Error Error for a suppressed endpoint, otherwise the original value. * * @since 5.4.0 */ public static function suppress_dashboard_http_request( $preempt, $args, $url ) { $parts = \wp_parse_url( (string) $url ); $is_events = is_array( $parts ) && isset( $parts['host'] ) && 'api.wordpress.org' === strtolower( $parts['host'] ) && 0 === strpos( $parts['path'] ?? '', '/events/' ); if ( $is_events || self::is_dashboard_news_feed( (string) $url ) ) { return new \WP_Error( 'advan_dashboard_feed_disabled', \__( 'WordPress news and events are disabled by WP Panel.', '0-day-analytics' ) ); } return $preempt; } /** * Disable SimplePie caching for dashboard news feeds before initialization. * * Hooks into `wp_feed_options`. When a multi-feed request includes a * suppressed URL, caching is disabled for the entire feed instance. * Existing cached entries are not deleted. * * @param \SimplePie\SimplePie|\SimplePie $feed Feed instance supplied by WordPress. * @param string|string[] $urls Feed URL or list of feed URLs. * * @return void * * @since 5.4.0 */ public static function disable_dashboard_feed_cache( $feed, $urls ): void { foreach ( (array) $urls as $url ) { if ( is_string( $url ) && self::is_dashboard_news_feed( $url ) ) { $feed->enable_cache( false ); return; } } } /** * Determine whether the current screen supports site-specific role sorting. * * Requires an administration request, the site's Users screen, and the * `list_users` capability. Network administration screens are excluded. * * @return bool Whether role sorting is allowed on the current screen. * * @since 5.4.0 */ private static function is_site_users_screen(): bool { if ( ! \is_admin() || \is_network_admin() || ! \current_user_can( 'list_users' ) ) { return false; } $screen = \get_current_screen(); return $screen && 'users' === $screen->id; } /** * Register sorting for the existing Role column on the site's Users screen. * * Filters `manage_users_sortable_columns`. Preserves an existing Role * sorting definition supplied by another plugin. * * @param array $columns Existing sortable column definitions. * * @return array Sortable column definitions with role sorting added when applicable. * * @since 5.4.0 */ public static function sortable_role_column( array $columns ): array { if ( self::is_site_users_screen() && ! isset( $columns['role'] ) ) { $columns['role'] = 'advan_role'; } return $columns; } /** * Mark a site Users list-table query that requests sorting by role. * * Filters `users_list_table_query_args`. The marker lets the SQL callback * distinguish this list-table query from unrelated user queries. * * @param array $args Arguments passed to the list table's WP_User_Query instance. * * @return array Query arguments with the role-sort marker added when applicable. * * @since 5.4.0 */ public static function mark_role_sort_query( array $args ): array { if ( self::is_site_users_screen() && 'advan_role' === ( $args['orderby'] ?? '' ) ) { $args['advan_role_sort'] = true; } return $args; } /** * Sort a marked site Users query by its first alphabetic translated role. * * Hooks into `pre_user_query` and changes only the ORDER BY clause, before * pagination. Registered role names determine the order for multi-role * users. Users without a registered role sort first ascending and last * descending; username and user ID provide stable ordering within a role. * * A scalar subquery reads the site's first capabilities metadata row, * retaining users without metadata and avoiding additional joined rows. * Queries for other sites or network administration are excluded. * * @global \wpdb $wpdb WordPress database connection. * * @param \WP_User_Query $query Prepared user query whose ordering may be modified. * * @return void * * @since 5.4.0 */ public static function sort_by_role( $query ): void { if ( ! $query->get( 'advan_role_sort' ) || 'advan_role' !== $query->get( 'orderby' ) || ! self::is_site_users_screen() ) { return; } $blog_id = (int) $query->get( 'blog_id' ); if ( $blog_id !== (int) \get_current_blog_id() ) { return; } global $wpdb; $roles = array(); foreach ( \wp_roles()->get_names() as $slug => $name ) { $roles[ $slug ] = \translate_user_role( $name ); } \uksort( $roles, static function ( $a, $b ) use ( $roles ) { return \strnatcasecmp( $roles[ $a ], $roles[ $b ] ) ?: \strcmp( $a, $b ); } ); $cases = array(); foreach ( array_keys( $roles ) as $rank => $slug ) { // Include the serialized string length and terminator to avoid partial matches. $needle = 's:' . strlen( (string) $slug ) . ':"' . $slug . '";'; $cases[] = $wpdb->prepare( 'WHEN LOCATE(BINARY %s, BINARY advan_role_meta.meta_value) > 0 THEN %d', $needle, $rank + 1 ); } $rank_sql = $cases ? 'CASE ' . implode( ' ', $cases ) . ' ELSE 0 END' : '0'; $meta_key = $wpdb->prepare( '%s', $wpdb->get_blog_prefix( $blog_id ) . 'capabilities' ); $direction = 'DESC' === strtoupper( (string) $query->get( 'order' ) ) ? 'DESC' : 'ASC'; // No role sorts first in ASC. Ties use login and ID for stable pagination. $query->query_orderby = "ORDER BY COALESCE((SELECT {$rank_sql} FROM {$wpdb->usermeta} AS advan_role_meta WHERE advan_role_meta.user_id = {$wpdb->users}.ID AND advan_role_meta.meta_key = {$meta_key} ORDER BY advan_role_meta.umeta_id ASC LIMIT 1), 0) {$direction}, {$wpdb->users}.user_login {$direction}, {$wpdb->users}.ID {$direction}"; } // ------------------------------------------------------------------ // User list column helpers // ------------------------------------------------------------------ /** * Add "Last Login" column to the users list table. * * @param array $columns Existing columns. * * @return array * * @since 5.1.0 */ public static function add_last_login_column( array $columns ): array { $columns['advan_last_login'] = \__( 'Last Login', '0-day-analytics' ); return $columns; } /** * Render the "Last Login" column value. * * @param string $output Custom column output. * @param string $column_name Column identifier. * @param int $user_id User ID. * * @return string * * @since 5.1.0 */ public static function render_last_login_column( $output, $column_name, $user_id ) { if ( 'advan_last_login' !== $column_name ) { return $output; } $last_login = \get_user_meta( $user_id, self::LAST_LOGIN_META, true ); if ( empty( $last_login ) ) { return '' . \esc_html__( 'Never', '0-day-analytics' ) . ''; } $dt = \wp_date( \get_option( 'date_format' ) . ' ' . \get_option( 'time_format' ), (int) $last_login ); return \esc_html( $dt ); } /** * Make "Last Login" column sortable. * * @param array $columns Sortable columns. * * @return array * * @since 5.1.0 */ public static function sortable_last_login_column( array $columns ): array { $columns['advan_last_login'] = 'advan_last_login'; return $columns; } /** * Handle sorting by last login meta. * * @param \WP_User_Query $query User query. * * @return void * * @since 5.1.0 */ public static function sort_by_last_login( $query ): void { if ( ! \is_admin() ) { return; } if ( 'advan_last_login' === ( $query->get( 'orderby' ) ?? '' ) ) { $query->set( 'meta_key', self::LAST_LOGIN_META ); $query->set( 'orderby', 'meta_value_num' ); } } /** * Add "Registration Date" column to the users list table. * * @param array $columns Existing columns. * * @return array * * @since 5.1.0 */ public static function add_registration_date_column( array $columns ): array { $columns['advan_registered'] = \__( 'Registration Date', '0-day-analytics' ); return $columns; } /** * Render the "Registration Date" column value. * * @param string $output Custom column output. * @param string $column_name Column identifier. * @param int $user_id User ID. * * @return string * * @since 5.1.0 */ public static function render_registration_date_column( $output, $column_name, $user_id ) { if ( 'advan_registered' !== $column_name ) { return $output; } $user = \get_userdata( $user_id ); if ( ! $user ) { return '—'; } $registered = \strtotime( $user->user_registered ); $dt = \wp_date( \get_option( 'date_format' ) . ' ' . \get_option( 'time_format' ), $registered ); return \esc_html( $dt ); } /** * Make "Registration Date" column sortable. * * @param array $columns Sortable columns. * * @return array * * @since 5.1.0 */ public static function sortable_registration_date_column( array $columns ): array { $columns['advan_registered'] = 'registered'; return $columns; } /** * Handle sorting by registration date. * * @param \WP_User_Query $query User query. * * @return void * * @since 5.1.0 */ public static function sort_by_registration_date( $query ): void { if ( ! \is_admin() ) { return; } if ( 'registered' === ( $query->get( 'orderby' ) ?? '' ) ) { $query->set( 'orderby', 'registered' ); } } /** * Strip the version query string from asset URLs. * * @param string $src The asset URL. * * @return string * * @since 5.1.0 */ public static function strip_version_query( $src ) { if ( \strpos( (string) $src, 'ver=' ) ) { $src = \remove_query_arg( 'ver', $src ); } return $src; } // ------------------------------------------------------------------ // Save handler // ------------------------------------------------------------------ /** * Handle the form submission for saving WP Panel settings. * * @return void * * @since 5.0.0 */ public static function handle_save(): void { // Permission check. if ( ! \current_user_can( 'manage_options' ) ) { \wp_die( \esc_html__( 'Insufficient permissions.', '0-day-analytics' ), 403 ); } // Nonce verification. if ( ! isset( $_POST[ self::NONCE_FIELD ] ) || ! \wp_verify_nonce( \sanitize_text_field( \wp_unslash( $_POST[ self::NONCE_FIELD ] ) ), self::NONCE_ACTION ) ) { \wp_die( \esc_html__( 'Security check failed.', '0-day-analytics' ), 403 ); } $defaults = self::get_defaults(); $sanitised = array(); foreach ( $defaults as $key => $default ) { if ( 'post_revisions_limit' === $key ) { $sanitised[ $key ] = isset( $_POST[ $key ] ) ? \absint( $_POST[ $key ] ) : $default; // Clamp to reasonable range. if ( $sanitised[ $key ] < 1 ) { $sanitised[ $key ] = 1; } if ( $sanitised[ $key ] > 100 ) { $sanitised[ $key ] = 100; } continue; } if ( 'heartbeat_control' === $key ) { $allowed = array( 'default', 'disable', 'modify' ); $sanitised[ $key ] = isset( $_POST[ $key ] ) && \in_array( $_POST[ $key ], $allowed, true ) ? \sanitize_text_field( \wp_unslash( $_POST[ $key ] ) ) : 'default'; continue; } if ( 'heartbeat_frequency' === $key ) { $sanitised[ $key ] = isset( $_POST[ $key ] ) ? \absint( $_POST[ $key ] ) : $default; // Clamp to 15–300 range. if ( $sanitised[ $key ] < 15 ) { $sanitised[ $key ] = 15; } if ( $sanitised[ $key ] > 300 ) { $sanitised[ $key ] = 300; } continue; } // All other fields are boolean checkboxes. $sanitised[ $key ] = isset( $_POST[ $key ] ) ? filter_var( $_POST[ $key ], \FILTER_VALIDATE_BOOLEAN ) : false; } self::store_settings( $sanitised ); // Redirect back to the WP Panel page with a success flag. \wp_safe_redirect( \add_query_arg( array( 'page' => self::MENU_SLUG, 'updated' => '1', ), \admin_url( 'admin.php' ) ) ); exit; } // ------------------------------------------------------------------ // Admin page rendering // ------------------------------------------------------------------ /** * Render the WP Panel admin page. * * @return void * * @since 5.0.0 */ public static function render_admin_page(): void { if ( ! \current_user_can( 'manage_options' ) ) { \wp_die( \esc_html__( 'Insufficient permissions.', '0-day-analytics' ) ); } $settings = self::get_settings(); \wp_enqueue_style( 'advan-admin-style', \ADVAN_PLUGIN_ROOT_URL . 'css/admin/style.css', array(), \ADVAN_VERSION, 'all' ); ?>