'views', 'conversion' => 'conversions', 'close' => 'closes' ]; public static function init(): void { add_action( 'wp_ajax_eppro_popup_event', [ __CLASS__, 'ajax_event' ] ); add_action( 'wp_ajax_nopriv_eppro_popup_event', [ __CLASS__, 'ajax_event' ] ); add_action( 'wp_ajax_eppro_popup_stats_reset', [ __CLASS__, 'ajax_reset' ] ); add_action( 'admin_menu', [ __CLASS__, 'add_menu' ], 20 ); } // ========================================================================= // Recording // ========================================================================= public static function ajax_event(): void { if ( ! check_ajax_referer( 'eppro_popup_view', '_wpnonce', false ) ) { wp_send_json_error( [], 403 ); } $popup_id = absint( $_POST['post_id'] ?? 0 ); $type = sanitize_key( wp_unslash( $_POST['event'] ?? 'view' ) ); if ( ! $popup_id || ! isset( self::EVENTS[ $type ] ) || ! class_exists( 'EPPRO_Popup_CPT' ) || get_post_type( $popup_id ) !== EPPRO_Popup_CPT::POST_TYPE ) { wp_send_json_error( [], 400 ); } self::record( $popup_id, $type ); wp_send_json_success(); } /** Add one event to today's row. Skipped for administrators. */ public static function record( int $popup_id, string $type ): void { if ( ! isset( self::EVENTS[ $type ] ) ) return; if ( current_user_can( 'manage_options' ) ) return; global $wpdb; $table = EPPRO_DB::table( 'popup_stats' ); $day = wp_date( 'Y-m-d' ); // One literal query per event (no column names built from variables). switch ( $type ) { case 'conversion': $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (popup_id, day, conversions) VALUES (%d, %s, 1) ON DUPLICATE KEY UPDATE conversions = conversions + 1', $table, $popup_id, $day ) ); break; case 'close': $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (popup_id, day, closes) VALUES (%d, %s, 1) ON DUPLICATE KEY UPDATE closes = closes + 1', $table, $popup_id, $day ) ); break; default: // view $wpdb->query( $wpdb->prepare( 'INSERT INTO %i (popup_id, day, views) VALUES (%d, %s, 1) ON DUPLICATE KEY UPDATE views = views + 1', $table, $popup_id, $day ) ); } // Lifetime view counter shown in the popup list. if ( 'view' === $type ) { update_post_meta( $popup_id, '_eppro_popup_views', (int) get_post_meta( $popup_id, '_eppro_popup_views', true ) + 1 ); } } // ========================================================================= // Reading // ========================================================================= /** * Stats for the last $days days (today included). * * @return array{totals: array, daily: array, popups: array} */ public static function get_stats( int $days, int $popup_id = 0 ): array { global $wpdb; $table = EPPRO_DB::table( 'popup_stats' ); $from = wp_date( 'Y-m-d', time() - ( $days - 1 ) * DAY_IN_SECONDS ); // popup_id = 0 means "all popups": "%d = 0 OR popup_id = %d" keeps one literal query. $daily_rows = $wpdb->get_results( $wpdb->prepare( 'SELECT day, SUM(views) AS views, SUM(conversions) AS conversions, SUM(closes) AS closes FROM %i WHERE day >= %s AND ( %d = 0 OR popup_id = %d ) GROUP BY day', $table, $from, $popup_id, $popup_id ), ARRAY_A ); $popup_rows = $wpdb->get_results( $wpdb->prepare( 'SELECT popup_id, SUM(views) AS views, SUM(conversions) AS conversions, SUM(closes) AS closes FROM %i WHERE day >= %s AND ( %d = 0 OR popup_id = %d ) GROUP BY popup_id', $table, $from, $popup_id, $popup_id ), ARRAY_A ); // Fill every day of the range, so the chart has no gaps. $by_day = []; foreach ( (array) $daily_rows as $row ) { $by_day[ $row['day'] ] = $row; } $daily = []; for ( $i = $days - 1; $i >= 0; $i-- ) { $d = wp_date( 'Y-m-d', time() - $i * DAY_IN_SECONDS ); $daily[ $d ] = [ 'views' => (int) ( $by_day[ $d ]['views'] ?? 0 ), 'conversions' => (int) ( $by_day[ $d ]['conversions'] ?? 0 ), 'closes' => (int) ( $by_day[ $d ]['closes'] ?? 0 ), ]; } $totals = [ 'views' => 0, 'conversions' => 0, 'closes' => 0 ]; foreach ( $daily as $row ) { foreach ( $totals as $k => $v ) { $totals[ $k ] += $row[ $k ]; } } $popups = []; foreach ( (array) $popup_rows as $row ) { $popups[ (int) $row['popup_id'] ] = [ 'views' => (int) $row['views'], 'conversions' => (int) $row['conversions'], 'closes' => (int) $row['closes'], ]; } return [ 'totals' => $totals, 'daily' => $daily, 'popups' => $popups ]; } /** "12.5%" — conversions per view. */ public static function rate( int $part, int $whole ): string { if ( $whole <= 0 ) return '—'; return number_format_i18n( $part / $whole * 100, 1 ) . '%'; } // ========================================================================= // Admin // ========================================================================= public static function ajax_reset(): void { check_ajax_referer( 'eppro_popup_stats_reset', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [], 403 ); } global $wpdb; $popup_id = absint( $_POST['popup_id'] ?? 0 ); $table = EPPRO_DB::table( 'popup_stats' ); if ( $popup_id ) { $wpdb->delete( $table, [ 'popup_id' => $popup_id ], [ '%d' ] ); delete_post_meta( $popup_id, '_eppro_popup_views' ); } else { $wpdb->query( $wpdb->prepare( 'DELETE FROM %i', $table ) ); delete_post_meta_by_key( '_eppro_popup_views' ); } wp_send_json_success(); } public static function add_menu(): void { add_submenu_page( 'eppro-settings', __( 'Popup Analytics', 'admore-widgets-suite' ), __( 'Popup Analytics', 'admore-widgets-suite' ), 'manage_options', 'eppro-popup-analytics', [ __CLASS__, 'render_page' ] ); } public static function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) return; // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only filters. $days = absint( $_GET['days'] ?? 30 ); $days = in_array( $days, [ 7, 30, 90 ], true ) ? $days : 30; $popup_id = absint( $_GET['popup'] ?? 0 ); // phpcs:enable WordPress.Security.NonceVerification.Recommended $stats = self::get_stats( $days, $popup_id ); $totals = $stats['totals']; $all = get_posts( [ 'post_type' => class_exists( 'EPPRO_Popup_CPT' ) ? EPPRO_Popup_CPT::POST_TYPE : 'eppro_popup', 'post_status' => [ 'publish', 'draft', 'private' ], 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', ] ); $base = admin_url( 'admin.php?page=eppro-popup-analytics' ); ?>