until). */ public const DISMISSED_META = 'adpresso_dismissed_notices'; public static function get(): self { if ( self::$instance === null ) { self::$instance = new self(); } return self::$instance; } protected function __construct() { global $wpdb; $this->db = $wpdb; $this->table = $wpdb->prefix . 'adpresso_notifications'; } /** * Registers or updates a persistent notification in the database. * * @param array $data * * @return void */ public function register( array $data ): void { if ( empty( $data['slug'] ) || empty( $data['message'] ) ) { return; } $user_id = ! empty( $data['user_id'] ) ? absint( $data['user_id'] ) : null; // Check if the slug/user combination already exists, and if so, in what state. $query = $user_id ? $this->db->prepare( "SELECT id, status FROM `{$this->table}` WHERE slug = %s AND user_id = %d", $data['slug'], $user_id ) : $this->db->prepare( "SELECT id, status FROM `{$this->table}` WHERE slug = %s AND user_id IS NULL", $data['slug'] ); $existing = $this->db->get_row( $query ); $db_data = [ 'type' => sanitize_key( $data['type'] ?? 'info' ), 'slug' => sanitize_key( $data['slug'] ), 'location' => sanitize_text_field( $data['location'] ?? 'dashboard' ), 'message' => wp_kses_post( $data['message'] ), 'link' => esc_url_raw( $data['link'] ?? '' ), 'dismissible' => intval( $data['dismissible'] ?? 1 ), 'user_id' => $user_id, 'context' => sanitize_key( $data['context'] ?? 'adpresso' ), ]; if ( $existing ) { // A user's own dismissal must not be resurrected by re-registering the same // slug. A system resolve() is different - the condition recurring is a fresh // occurrence, not a resurrection, so re-activate it in that case. if ( $existing->status === 'resolved' ) { $db_data['status'] = 'active'; $db_data['dismissed_until'] = null; } $this->db->update( $this->table, $db_data, [ 'id' => $existing->id ] ); } else { // A genuinely new notice always starts active. $db_data['status'] = 'active'; $db_data['dismissed_until'] = null; $db_data['created_at'] = current_time( 'mysql' ); $this->db->insert( $this->table, $db_data ); } } /** * Fetch all active (undismissed) notices for a user and location. * * @param string $location * @param int $user_id * * @return array */ public function get_active_notices_by_location( string $location, int $user_id ): array { $sql = $this->db->prepare( "SELECT * FROM $this->table WHERE location = %s AND (user_id = %d OR user_id IS NULL) AND status = 'active' AND (dismissed_until IS NULL OR dismissed_until < NOW())", $location, $user_id ); return $this->without_dismissed( (array) $this->db->get_results( $sql ), $user_id ); } /** * Get all dashboard notifications for a user. * * @param int|null $user_id * * @return array */ public function get_dashboard_notifications( ?int $user_id = null ): array { $user_id = $user_id ?? get_current_user_id(); $sql = $this->db->prepare( "SELECT * FROM $this->table WHERE (user_id = %d OR user_id IS NULL) AND status = 'active' AND (dismissed_until IS NULL OR dismissed_until < NOW())", $user_id ); return $this->without_dismissed( (array) $this->db->get_results( $sql ), $user_id ); } /** * Dismisses a notice for one user: a notice addressed to the user is marked in its row, * a global one is noted in the user's meta, so every user hides it on their own. * * @param string $slug Notice slug. * @param int|null $user_id User, default the current one. * @param string|null $until Dismissed until (MySQL datetime), default for good. * * @return void */ public function dismiss( string $slug, ?int $user_id = null, ?string $until = null ): void { $user_id = $user_id ?? get_current_user_id(); $until = $until ?? '9999-12-31 23:59:59'; $updated = $this->db->update( $this->table, [ 'status' => 'dismissed', 'dismissed_until' => $until, ], [ 'slug' => $slug, 'user_id' => $user_id, ], [ '%s', '%s' ], [ '%s', '%d' ] ); if ( ! $updated && $user_id > 0 ) { $dismissed = $this->user_dismissals( $user_id ); $dismissed[ $slug ] = $until; update_user_meta( $user_id, self::DISMISSED_META, $dismissed ); } } /** * Turns the per-user rows written before 1.8 into one global row per slug; every user's dismissal moves to their meta. * * @return void */ public function globalize_notices(): void { // phpcs:disable WordPress.DB.DirectDatabaseQuery -- Own table, one-time migration. $global_slugs = (array) $this->db->get_col( "SELECT slug FROM `{$this->table}` WHERE user_id IS NULL" ); $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT id, slug, user_id, status, dismissed_until FROM `{$this->table}` WHERE user_id > %d ORDER BY id ASC", 0 ) ); $by_slug = []; foreach ( $rows as $row ) { $by_slug[ $row->slug ][] = $row; } foreach ( $by_slug as $slug => $slug_rows ) { // A global row exists already: the per-user rows only hand over their dismissals. $keep = in_array( $slug, $global_slugs, true ) ? null : $slug_rows[0]; $active = false; foreach ( $slug_rows as $row ) { if ( 'dismissed' === $row->status && (int) $row->user_id > 0 ) { $dismissed = $this->user_dismissals( (int) $row->user_id ); $dismissed[ $slug ] = '' !== (string) $row->dismissed_until ? (string) $row->dismissed_until : '9999-12-31 23:59:59'; update_user_meta( (int) $row->user_id, self::DISMISSED_META, $dismissed ); } $active = $active || 'active' === $row->status; if ( null === $keep || (int) $row->id !== (int) $keep->id ) { $this->db->delete( $this->table, [ 'id' => (int) $row->id ], [ '%d' ] ); } } if ( $keep ) { $this->db->update( $this->table, [ 'user_id' => null, 'status' => $active ? 'active' : 'resolved', 'dismissed_until' => null, ], [ 'id' => (int) $keep->id ], [ '%d', '%s', '%s' ], [ '%d' ] ); } } // phpcs:enable } /** * Global notices the user dismissed: slug => dismissed until. * * @param int $user_id User. * * @return array */ private function user_dismissals( int $user_id ): array { $dismissed = $user_id > 0 ? get_user_meta( $user_id, self::DISMISSED_META, true ) : []; return is_array( $dismissed ) ? $dismissed : []; } /** * Drops the global notices the user dismissed (until the dismissal runs out). * * @param object[] $rows Notice rows. * @param int $user_id User. * * @return object[] */ private function without_dismissed( array $rows, int $user_id ): array { $dismissed = $this->user_dismissals( $user_id ); if ( empty( $dismissed ) ) { return $rows; } $now = current_time( 'mysql' ); return array_values( array_filter( $rows, static fn( $row ) => ! isset( $dismissed[ $row->slug ] ) || $dismissed[ $row->slug ] <= $now ) ); } /** * Slugs of all active notices that start with $prefix. * * @param string $prefix * * @return string[] */ public function get_active_slugs( string $prefix ): array { $sql = $this->db->prepare( "SELECT DISTINCT slug FROM `{$this->table}` WHERE status = 'active' AND slug LIKE %s", $this->db->esc_like( $prefix ) . '%' ); return $this->db->get_col( $sql ); } /** * Deletes the notices imports and block conversions stacked before 1.8 (one per run, slug ending in a timestamp). * The fixed-slug notice holds the last result. * * @return int Deleted rows. */ public function delete_stacked_notices(): int { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Own table, no user input. return (int) $this->db->query( "DELETE FROM `{$this->table}` WHERE slug REGEXP '-[0-9]{10}$'" ); } /** * Clears a notice for every user it was registered for, because the condition that caused it no longer holds (as opposed to dismiss()) * * @param string $slug * * @return void */ public function resolve( string $slug ): void { $this->db->update( $this->table, [ 'status' => 'resolved' ], [ 'slug' => $slug, 'status' => 'active' ], [ '%s' ], [ '%s', '%s' ] ); } }