PumpAudit screen and the dashboard widget. * * @package PumpAudit_WP */ namespace PumpAudit_WP; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Admin screens. */ class Admin { const SLUG = 'pumpaudit'; /** * Hooks. */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_action( 'wp_dashboard_setup', array( __CLASS__, 'widget' ) ); add_action( 'admin_post_pumpaudit_wp_recheck', array( __CLASS__, 'recheck' ) ); add_action( 'wp_ajax_pumpaudit_wp_scan', array( __CLASS__, 'scan' ) ); add_action( 'admin_bar_menu', array( __CLASS__, 'toolbar' ), 35 ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'toolbar_style' ) ); add_action( 'wp_enqueue_scripts', array( __CLASS__, 'toolbar_style' ) ); add_filter( 'admin_body_class', array( __CLASS__, 'body_class' ) ); add_filter( 'plugin_action_links_' . plugin_basename( PUMPAUDIT_WP_FILE ), array( __CLASS__, 'action_links' ) ); } /** * Who may see the report and apply fixes. On a network only network * admins, because updates, plugins and wp-config.php are shared. * * @return string */ public static function cap() { return is_multisite() ? 'manage_network_options' : 'manage_options'; } /** * Report screen URL. * * @return string */ public static function page_url() { return admin_url( 'tools.php?page=' . self::SLUG ); } /** * Adds Tools > PumpAudit. */ public static function menu() { add_management_page( 'PumpAudit', 'PumpAudit', self::cap(), self::SLUG, array( __CLASS__, 'render' ) ); } /** * "Open report" link on the Plugins screen. * * @param string[] $links Links. * @return string[] */ public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Open report', 'pumpaudit' ) . '' ); return $links; } /** * Styles and script, on the report screen and the dashboard only. * * @param string $hook Screen hook. */ public static function assets( $hook ) { if ( 'tools_page_' . self::SLUG !== $hook && 'index.php' !== $hook ) { return; } wp_enqueue_style( 'pumpaudit-wp', PUMPAUDIT_WP_URL . 'assets/admin.css', array(), PUMPAUDIT_WP_VERSION ); if ( 'index.php' !== $hook ) { // The 3D pipeline (three.js bundled in, see src/pipe3d.js), on the report screen only. wp_enqueue_script( 'pumpaudit-wp-3d', PUMPAUDIT_WP_URL . 'assets/pipe3d.js', array(), PUMPAUDIT_WP_VERSION, true ); wp_enqueue_script( 'pumpaudit-wp', PUMPAUDIT_WP_URL . 'assets/admin.js', array( 'pumpaudit-wp-3d' ), PUMPAUDIT_WP_VERSION, true ); } } /** * Colour of a report: red with urgent items, yellow with recommended ones, else green. * * @param array $report Report. * @return string urgent|recommended|passed */ public static function tone( $report ) { $c = $report['counts']; return $c['urgent'] ? 'urgent' : ( $c['recommended'] ? 'recommended' : 'passed' ); } /** * Score in the toolbar, right after the site name, so other plugins' items never push it out of sight. In the admin area and on * the site, only for people who may see the report. Reads the saved report and never runs the checks itself. * * @param \WP_Admin_Bar $bar Toolbar. */ public static function toolbar( $bar ) { if ( ! current_user_can( self::cap() ) ) { return; } $report = Checks::cached(); if ( ! $report ) { return; } $bar->add_node( array( 'id' => 'pumpaudit', 'title' => 'PumpAudit ' . (int) $report['score'] . '', 'href' => self::page_url(), 'meta' => array( 'title' => self::headline( $report['counts'] ) ), ) ); } /** * Toolbar dot colours, wherever the toolbar shows for people who may see the report. */ public static function toolbar_style() { if ( ! is_admin_bar_showing() || ! current_user_can( self::cap() ) ) { return; } wp_add_inline_style( 'admin-bar', '#wpadminbar .pa-wp-ab__dot{display:inline-block;width:9px;height:9px;border-radius:50%;margin-right:6px;vertical-align:0;background:#00a32a}#wpadminbar .pa-wp-ab__dot--urgent{background:#d63638}#wpadminbar .pa-wp-ab__dot--recommended{background:#dba617}#wpadminbar .pa-wp-ab__score{font-weight:600}@media screen and (max-width:782px){#wpadminbar li#wp-admin-bar-pumpaudit{display:block}#wpadminbar #wp-admin-bar-pumpaudit .ab-item{padding:0 8px}#wpadminbar .pa-wp-ab__name{display:none}#wpadminbar .pa-wp-ab__dot{margin:0 4px 0 0}}' ); } /** * Dark theme, on the report screen only. * * @param string $classes Body classes. * @return string */ public static function body_class( $classes ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && 'tools_page_' . self::SLUG === $screen->id ) { $classes .= ' pa-wp-dark'; } return $classes; } /** * Runs the checks again. */ public static function recheck() { check_admin_referer( 'pumpaudit_wp_recheck' ); if ( ! current_user_can( self::cap() ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'pumpaudit' ) ); } delete_site_option( Checks::STATE ); Checks::run( true ); wp_safe_redirect( self::page_url() ); exit; } /** * Runs the checks again for the live scan on the report screen. */ public static function scan() { check_ajax_referer( 'pumpaudit_wp_recheck' ); if ( ! current_user_can( self::cap() ) ) { wp_send_json_error( null, 403 ); } delete_site_option( Checks::STATE ); $report = Checks::run( true ); wp_send_json_success( array( 'score' => (int) $report['score'] ) ); } /** * Links to pumpmedya.com. Only opened when the user clicks. Both carry the * site's domain; "Fix it for me" also carries the report summary (counts and * the ids of the open items) so the request form does not ask for them. * Disclosed in readme.txt > External services. * * @param string $which fix|scan. * @return string */ public static function outbound( $which ) { $args = array( 'site' => (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); if ( 'fix' === $which ) { $report = Checks::run(); $open = array(); foreach ( $report['results'] as $r ) { if ( 'passed' !== $r['status'] ) { $open[] = $r['id']; } } $args += array( 'u' => (int) $report['counts']['urgent'], 'r' => (int) $report['counts']['recommended'], 'i' => implode( ',', $open ), ); $url = 'https://pumpmedya.com/wordpress-care'; } else { $url = 'https://pumpmedya.com/website-audit'; } $args += array( 'utm_source' => 'pumpaudit-wp', 'utm_medium' => 'plugin', 'utm_campaign' => $which, ); $utm = $args; /** * Filters the pumpmedya.com links on the report. * * @param string $url Link. * @param string $which fix|scan. */ return (string) apply_filters( 'pumpaudit_wp_outbound_url', add_query_arg( $utm, $url ), $which ); } /** * Group names. * * @return array */ private static function groups() { return array( 'security' => __( 'Security', 'pumpaudit' ), 'visibility' => __( 'Visibility', 'pumpaudit' ), 'maintenance' => __( 'Maintenance', 'pumpaudit' ), 'storage' => __( 'Storage and speed', 'pumpaudit' ), ); } /** * Summary sentence. * * @param array $c Counts. * @return string */ public static function headline( $c ) { if ( $c['urgent'] ) { /* translators: %d: number of urgent issues. */ return sprintf( _n( 'Your site works, but %d thing needs attention today', 'Your site works, but %d things need attention today', $c['urgent'], 'pumpaudit' ), $c['urgent'] ); } if ( $c['recommended'] ) { return __( 'Nothing urgent. A few things would make your site better', 'pumpaudit' ); } return __( 'Everything we check looks good', 'pumpaudit' ); } /** * Report screen. */ public static function render() { if ( ! current_user_can( self::cap() ) ) { return; } $report = Checks::run(); $c = $report['counts']; $issues = array_values( array_filter( $report['results'], static function ( $r ) { return 'passed' !== $r['status']; } ) ); usort( $issues, static function ( $a, $b ) { return ( 'urgent' === $a['status'] ? 0 : 1 ) - ( 'urgent' === $b['status'] ? 0 : 1 ); } ); $passed = array_values( array_filter( $report['results'], static function ( $r ) { return 'passed' === $r['status']; } ) ); $groups = self::groups(); $tone = self::tone( $report ); $help = self::outbound( 'fix' ); $fixed = isset( $_GET['pumpaudit-fixed'] ) ? sanitize_key( wp_unslash( $_GET['pumpaudit-fixed'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. $prev = isset( $_GET['pumpaudit-prev'] ) ? min( 100, absint( $_GET['pumpaudit-prev'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. wp_localize_script( 'pumpaudit-wp', 'pumpauditWp', array( 'confirm' => __( 'Apply this change to your site?', 'pumpaudit' ), 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'pumpaudit_wp_recheck' ), 'page' => self::page_url(), 'score' => (int) $report['score'], 'layers' => array_map( static function ( $key ) use ( $report ) { $state = self::group_state( $report, $key ); return $state['tone']; }, array_keys( self::groups() ) ), 'checks' => array_map( static function ( $r ) { return $r['title'] ? $r['title'] : ucfirst( $r['passed'] ); }, array_values( $report['results'] ) ), ) ); ?>

PumpAudit

/100



$r ) : ?>
Pumpmedya

$groups Group names. */ private static function pipe( $report, $groups ) { $x0 = 64; $len = 832; $xs = array( 230, 410, 590, 770 ); $keys = array_keys( $groups ); $score = (int) $report['score']; $level = round( 92 * $score / 100, 1 ); ?> $tone, 'open' => $open, ); } /** * Dashboard widget. */ public static function widget() { if ( ! current_user_can( self::cap() ) ) { return; } // First in the main column until the user moves it; WordPress remembers their order. wp_add_dashboard_widget( 'pumpaudit_wp', 'PumpAudit', array( __CLASS__, 'render_widget' ), null, null, 'normal', 'high' ); } /** * Dashboard widget body. */ public static function render_widget() { // No report yet (new install, or the daily cache ran out): run the checks now, once a day at most. $report = Checks::run(); $c = $report['counts']; $top = ''; foreach ( array( 'urgent', 'recommended' ) as $status ) { foreach ( $report['results'] as $r ) { if ( $status === $r['status'] ) { $top = $r['title']; break 2; } } } echo '
'; echo '
' . (int) $report['score'] . '
'; echo '
' . esc_html( self::headline( $c ) ) . ''; echo '

'; /* translators: %d: number. */ echo '' . esc_html( sprintf( __( '%d urgent', 'pumpaudit' ), $c['urgent'] ) ) . ' '; /* translators: %d: number. */ echo '' . esc_html( sprintf( __( '%d recommended', 'pumpaudit' ), $c['recommended'] ) ) . ''; echo '

'; if ( $top ) { echo '

' . esc_html( $top ) . '

'; } echo '

' . esc_html__( 'See the report', 'pumpaudit' ) . ' '; /* translators: %s: date and time. */ echo esc_html( sprintf( __( 'Last check: %s.', 'pumpaudit' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $report['time'] ) ) ); echo '

'; } }