array( 'name' => 'First Shield', 'description' => 'Complete your first security scan', 'icon' => '🛡️', 'points' => 10, ), 'clean_slate' => array( 'name' => 'Clean Slate', 'description' => 'Create your first baseline', 'icon' => '📸', 'points' => 15, ), 'watchful_eye' => array( 'name' => 'Watchful Eye', 'description' => 'Enable email alerts', 'icon' => '👁️', 'points' => 10, ), 'threat_detector' => array( 'name' => 'Threat Detector', 'description' => 'Detect your first suspicious file', 'icon' => '🔍', 'points' => 20, ), 'update_champion' => array( 'name' => 'Update Champion', 'description' => 'Keep all plugins updated', 'icon' => '🔄', 'points' => 20, ), ); /** * Initialize */ public static function init() { // FIX M-11 (audit 2026-05-25): Removed dead AJAX registration for // guardpress_get_achievements. The handler's nonce action was // 'guardpress_nonce' which the plugin NEVER generates anywhere — so // every legitimate call would fail nonce verification. The // achievements popup is rendered server-side inline (see // render_achievements_popup below); no JS code calls this endpoint. // Removing the dead registration also closes the WP.org reviewer // signal of "AJAX handler exists but is unreachable in normal flow". // If we ever add JS-driven achievement reloading, register the // handler at that point with the correct 'guardpress_ajax' nonce. // Create database table add_action( 'admin_init', array( __CLASS__, 'maybe_create_table' ) ); // Check for achievement triggers on admin pages add_action( 'admin_init', array( __CLASS__, 'check_achievement_triggers' ), 20 ); // Add to admin footer on GuardPress pages add_action( 'admin_footer', array( __CLASS__, 'render_achievements_popup' ) ); // Show achievement notification add_action( 'admin_notices', array( __CLASS__, 'show_achievement_notice' ) ); } /** * Create database table */ public static function maybe_create_table() { global $wpdb; $table_name = $wpdb->prefix . 'guardpress_achievements'; $db_version = get_option( 'guardpress_achievements_db_version', '0' ); if ( version_compare( $db_version, '1.0.0', '<' ) ) { $charset_collate = $wpdb->get_charset_collate(); // FIX H-1 / M-COMPAT-1 (audit 2026-05-25): dbDelta requires a very // strict CREATE TABLE form to perform schema upgrades correctly. // Three things were wrong here: // 1. `CREATE TABLE IF NOT EXISTS` — dbDelta requires plain // `CREATE TABLE`. With `IF NOT EXISTS` it silently no-ops // on schema deltas, breaking future migrations. // 2. Inline `PRIMARY KEY` after the column type — dbDelta needs // `PRIMARY KEY` as a separate key declaration. // 3. `INDEX` keyword — dbDelta needs `KEY`. // // Reference: https://developer.wordpress.org/reference/functions/dbdelta/ // Sibling that does it right: includes/security/class-login-security.php:111-134. $sql = "CREATE TABLE {$table_name} ( id INT NOT NULL AUTO_INCREMENT, user_id BIGINT UNSIGNED NOT NULL, achievement_id VARCHAR(50) NOT NULL, earned_at DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), UNIQUE KEY unique_user_achievement (user_id, achievement_id), KEY idx_user (user_id) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); // FIX H-10 (audit 2026-05-25): autoload=false. Read only at the // start of init() to decide whether to run dbDelta; no need to // live in alloptions on every request. update_option( 'guardpress_achievements_db_version', '1.0.0', false ); } } /** * Check for achievement triggers based on plugin state * This replaces the non-existent hooks */ public static function check_achievement_triggers() { // FIX (audit 2026-06-10): gate on capability. This fires on admin_init for // ANY logged-in user (a Subscriber loading their profile qualifies) and // performs DB writes. Achievements only track admin actions, so restrict // it to users who can actually use the plugin — no DB writes for low-priv // users on every admin page load. if ( ! current_user_can( 'manage_options' ) ) { return; } // Only check on GuardPress pages or periodically $screen = get_current_screen(); if ( ! $screen ) { return; } // Check on all admin pages, but rate limit $last_check = get_transient( 'guardpress_achievements_last_check' ); if ( $last_check && strpos( $screen->id, 'ai-cyberguard' ) === false ) { return; // Only check frequently on GuardPress pages } set_transient( 'guardpress_achievements_last_check', time(), 60 ); // Check every minute // Get saved state $state = get_option( self::STATE_OPTION, array() ); // Check 1: First scan completed (guardpress_last_scan_meta exists) $last_scan = get_option( 'guardpress_last_scan_meta', array() ); if ( ! empty( $last_scan ) && empty( $state['scan_detected'] ) ) { self::award( 'first_shield' ); $state['scan_detected'] = true; // Also check for threats detected if ( ! empty( $last_scan['changed'] ) || ! empty( $last_scan['new'] ) ) { self::award( 'threat_detector' ); $state['threat_detected'] = true; } } // Check 2: Baseline created (check via info metadata, not full DB load) $baseline_exists = class_exists( 'GuardPress_Baseline' ) ? GuardPress_Baseline::exists() : false; if ( $baseline_exists && empty( $state['baseline_detected'] ) ) { self::award( 'clean_slate' ); $state['baseline_detected'] = true; } // Check 3: Email alerts enabled (guardpress_settings_v1 has email_enabled) $settings = get_option( 'guardpress_settings_v1', array() ); if ( ! empty( $settings['email_enabled'] ) && empty( $state['email_detected'] ) ) { self::award( 'watchful_eye' ); $state['email_detected'] = true; } // Check 4: All plugins updated if ( empty( $state['updates_checked'] ) || ( time() - ( $state['updates_checked'] ?? 0 ) ) > 3600 ) { $update_plugins = get_site_transient( 'update_plugins' ); if ( $update_plugins && empty( $update_plugins->response ) ) { self::award( 'update_champion' ); } $state['updates_checked'] = time(); } // Save state update_option( self::STATE_OPTION, $state, false ); // ACG-122 D5: autoload=false, matching the FIX H-10 policy this file applies to its sibling option } /** * Get all achievements with user status */ public static function get_user_achievements( $user_id = null ) { global $wpdb; if ( ! $user_id ) { $user_id = get_current_user_id(); } $table_name = $wpdb->prefix . 'guardpress_achievements'; // Ensure table exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query on the plugin's own table; live security data must not be cached $table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) === $table_name; $earned = array(); if ( $table_exists ) { // Get earned achievements // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- direct query on the plugin's own table; the interpolated table name is a trusted internal constant, not user input $result = $wpdb->get_col( $wpdb->prepare( "SELECT achievement_id FROM {$table_name} WHERE user_id = %d", $user_id ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( is_array( $result ) ) { $earned = $result; } } $result = array(); foreach ( self::$achievements as $id => $achievement ) { $result[ $id ] = array_merge( $achievement, array( 'id' => $id, 'earned' => in_array( $id, $earned, true ), ) ); } return $result; } /** * Award achievement */ public static function award( $achievement_id, $user_id = null ) { global $wpdb; if ( ! $user_id ) { $user_id = get_current_user_id(); } if ( ! $user_id || ! isset( self::$achievements[ $achievement_id ] ) ) { return false; } $table_name = $wpdb->prefix . 'guardpress_achievements'; // Ensure table exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query on the plugin's own table; live security data must not be cached $table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) === $table_name; if ( ! $table_exists ) { self::maybe_create_table(); } // Check if already earned // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- direct query on the plugin's own table; the interpolated table name is a trusted internal constant, not user input $exists = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE user_id = %d AND achievement_id = %s", $user_id, $achievement_id ) ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( $exists ) { return false; } // Award // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct write to the plugin's own table; live security data must not be cached $result = $wpdb->insert( $table_name, array( 'user_id' => $user_id, 'achievement_id' => $achievement_id, 'earned_at' => current_time( 'mysql' ), ), array( '%d', '%s', '%s' ) ); if ( $result ) { // Store notification set_transient( "guardpress_new_achievement_{$user_id}", $achievement_id, 300 // 5 minutes ); } return (bool) $result; } /** * Get total points */ public static function get_total_points( $user_id = null ) { $achievements = self::get_user_achievements( $user_id ); $points = 0; foreach ( $achievements as $a ) { if ( ! empty( $a['earned'] ) ) { $points += $a['points']; } } return $points; } /** * Get max possible points */ public static function get_max_points() { $max = 0; foreach ( self::$achievements as $a ) { $max += $a['points']; } return $max; } /** * Show achievement notification */ public static function show_achievement_notice() { $user_id = get_current_user_id(); $new_achievement = get_transient( "guardpress_new_achievement_{$user_id}" ); if ( ! $new_achievement || ! isset( self::$achievements[ $new_achievement ] ) ) { return; } delete_transient( "guardpress_new_achievement_{$user_id}" ); $achievement = self::$achievements[ $new_achievement ]; ?>

🏆 - + pts

id, 'ai-cyberguard' ) === false ) { return; } // ACG-122 D1 (audit 2026-07-16): do NOT render this floating 🏆 button on the Security Hub. The Hub // renders the newer, self-contained "Streaks & Achievements" card (GuardPress_FreePlus_Streaks), and // this legacy points-based popup is a SECOND, different gamification model with overlapping badge // names ("Watchful" vs "Watchful Eye"). Showing both on the flagship page reads as unfinished to a // buyer, and the fixed-position button covers the bottom-right of the page content. // The class is deliberately NOT unloaded: it still awards achievements elsewhere, and per the // Safe-Fix guide removing the require without tracing every caller risks a fatal. Suppressing the // duplicate UI where the Streaks card already exists is the minimal, reversible fix. if ( false !== strpos( $screen->id, 'guardpress-hub' ) ) { return; } $achievements = self::get_user_achievements(); $earned_count = 0; foreach ( $achievements as $a ) { if ( ! empty( $a['earned'] ) ) { $earned_count++; } } $total = count( $achievements ); $total_points = self::get_total_points(); $max_points = self::get_max_points(); $progress_percent = $max_points > 0 ? round( ( $total_points / $max_points ) * 100 ) : 0; ?>
🏆 /