prefix . self::TABLE; $charset = $wpdb->get_charset_collate(); $sql = "CREATE TABLE IF NOT EXISTS {$table} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, street VARCHAR(255) NOT NULL DEFAULT '', city VARCHAR(120) NOT NULL DEFAULT '', region VARCHAR(120) NOT NULL DEFAULT '', postal_code VARCHAR(30) NOT NULL DEFAULT '', country VARCHAR(2) NOT NULL DEFAULT 'US', phone VARCHAR(40) NOT NULL DEFAULT '', email VARCHAR(190) NOT NULL DEFAULT '', lat VARCHAR(30) NOT NULL DEFAULT '', lng VARCHAR(30) NOT NULL DEFAULT '', price_range VARCHAR(10) NOT NULL DEFAULT '', hours TEXT, page_id BIGINT UNSIGNED NOT NULL DEFAULT 0, is_primary TINYINT(1) NOT NULL DEFAULT 0, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) ) {$charset};"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } // ── CRUD ───────────────────────────────────────────────────────────────── public static function get_locations() { global $wpdb; $table = $wpdb->prefix . self::TABLE; return $wpdb->get_results( "SELECT * FROM {$table} ORDER BY is_primary DESC, name ASC" ); } public static function get_location( $id ) { global $wpdb; $table = $wpdb->prefix . self::TABLE; return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ) ); } public static function get_primary_location() { global $wpdb; $table = $wpdb->prefix . self::TABLE; $row = $wpdb->get_row( "SELECT * FROM {$table} WHERE is_primary = 1 ORDER BY id ASC LIMIT 1" ); if ( ! $row ) { $row = $wpdb->get_row( "SELECT * FROM {$table} ORDER BY id ASC LIMIT 1" ); } return $row; } public static function save_location( $data ) { global $wpdb; $table = $wpdb->prefix . self::TABLE; $row = [ 'name' => sanitize_text_field( $data['name'] ?? '' ), 'street' => sanitize_text_field( $data['street'] ?? '' ), 'city' => sanitize_text_field( $data['city'] ?? '' ), 'region' => sanitize_text_field( $data['region'] ?? '' ), 'postal_code' => sanitize_text_field( $data['postal_code'] ?? '' ), 'country' => sanitize_text_field( $data['country'] ?? 'US' ), 'phone' => sanitize_text_field( $data['phone'] ?? '' ), 'email' => sanitize_email( $data['email'] ?? '' ), 'lat' => sanitize_text_field( $data['lat'] ?? '' ), 'lng' => sanitize_text_field( $data['lng'] ?? '' ), 'price_range' => sanitize_text_field( $data['price_range'] ?? '' ), 'hours' => wp_json_encode( self::sanitize_hours( $data['hours'] ?? [] ) ), 'page_id' => (int) ( $data['page_id'] ?? 0 ), 'updated_at' => current_time( 'mysql' ), ]; if ( ! $row['name'] ) { return new WP_Error( 'awb_local_seo', 'Location name is required.' ); } $id = (int) ( $data['id'] ?? 0 ); if ( $id ) { $wpdb->update( $table, $row, [ 'id' => $id ] ); } else { $row['created_at'] = current_time( 'mysql' ); $wpdb->insert( $table, $row ); $id = (int) $wpdb->insert_id; } if ( ! empty( $data['is_primary'] ) ) { self::set_primary( $id ); } return $id; } public static function set_primary( $id ) { global $wpdb; $table = $wpdb->prefix . self::TABLE; $wpdb->query( "UPDATE {$table} SET is_primary = 0" ); $wpdb->update( $table, [ 'is_primary' => 1 ], [ 'id' => (int) $id ] ); } public static function delete_location( $id ) { global $wpdb; $table = $wpdb->prefix . self::TABLE; return $wpdb->delete( $table, [ 'id' => (int) $id ] ); } private static function sanitize_hours( $hours ) { if ( ! is_array( $hours ) ) return []; $clean = []; foreach ( $hours as $row ) { $day = sanitize_text_field( $row['day'] ?? '' ); $opens = sanitize_text_field( $row['opens'] ?? '' ); $closes = sanitize_text_field( $row['closes'] ?? '' ); if ( $day && $opens && $closes ) { $clean[] = compact( 'day', 'opens', 'closes' ); } } return $clean; } // ── Schema ─────────────────────────────────────────────────────────────── /** * Build a LocalBusiness JSON-LD array for a single location row. */ public static function build_schema( $location ) { if ( ! $location ) return null; $schema = [ '@type' => 'LocalBusiness', 'name' => $location->name, 'url' => home_url( '/' ), 'telephone' => $location->phone ?: null, 'email' => $location->email ?: null, 'priceRange' => $location->price_range ?: null, 'address' => [ '@type' => 'PostalAddress', 'streetAddress' => $location->street ?: null, 'addressLocality' => $location->city ?: null, 'addressRegion' => $location->region ?: null, 'postalCode' => $location->postal_code ?: null, 'addressCountry' => $location->country ?: 'US', ], ]; if ( $location->lat && $location->lng ) { $schema['geo'] = [ '@type' => 'GeoCoordinates', 'latitude' => (float) $location->lat, 'longitude' => (float) $location->lng, ]; } $hours = json_decode( $location->hours ?: '[]', true ); if ( $hours ) { $spec = []; foreach ( $hours as $h ) { $spec[] = [ '@type' => 'OpeningHoursSpecification', 'dayOfWeek' => $h['day'], 'opens' => $h['opens'], 'closes' => $h['closes'], ]; } $schema['openingHoursSpecification'] = $spec; } if ( $location->page_id ) { $schema['hasMap'] = get_permalink( $location->page_id ) ?: null; } return $schema; } public static function output_schema() { $locations = self::get_locations(); if ( ! $locations ) return; $matched_id = 0; $emitted = []; // Page-specific location, if the current page is pinned to one. if ( is_singular() ) { $post_id = get_the_ID(); foreach ( $locations as $loc ) { if ( (int) $loc->page_id === (int) $post_id ) { $matched_id = (int) $loc->id; $schema = self::build_schema( $loc ); if ( $schema ) { $emitted[] = $schema; } } } } // Primary location's schema, site-wide, unless already emitted above. $primary = self::get_primary_location(); if ( $primary && (int) $primary->id !== $matched_id ) { $schema = self::build_schema( $primary ); if ( $schema ) { $emitted[] = $schema; } } foreach ( $emitted as $schema ) { // JSON_HEX_TAG escapes literal < and > inside encoded string // values (e.g. a location name containing "") so they // can never break out of this \n"; } } // ── NAP Consistency Check ──────────────────────────────────────────────── /** * Scans published pages for phone numbers that don't match the primary * location's phone, and flags whether the primary location's city is * mentioned anywhere on the homepage. Rule-based - no external API * required, mirrors the approach used by AWBSEO_Content_Decay. */ public static function nap_consistency_check() { $primary = self::get_primary_location(); if ( ! $primary || ! $primary->phone ) { return [ 'status' => 'no_primary', 'issues' => [] ]; } $primary_digits = preg_replace( '/\D/', '', $primary->phone ); $issues = []; $pages = get_posts( [ 'post_type' => [ 'page', 'post' ], 'post_status' => 'publish', 'posts_per_page' => 60, ] ); foreach ( $pages as $page ) { // Strip shortcodes first — form plugins (WPForms, Gravity Forms, etc.) // embed entry IDs / Unix timestamps in shortcode attributes that look // like long digit strings and get falsely flagged as phone mismatches. $text = wp_strip_all_tags( strip_shortcodes( $page->post_content ) ); preg_match_all( '/(\+?\d[\d\-.\(\) ]{7,}\d)/', $text, $matches ); foreach ( $matches[1] ?? [] as $raw ) { $digits = preg_replace( '/\D/', '', $raw ); if ( strlen( $digits ) < 7 ) continue; // too short — not a phone // GPS coordinates contain a decimal point; phone numbers never do. if ( strpos( $raw, '.' ) !== false ) continue; // Skip bare unformatted digit strings over 12 digits — those are // database IDs or Unix timestamps, not phone numbers. Real phone // numbers (even international with country code) are ≤12 digits // and almost always include formatting (+, spaces, dashes, etc.). if ( strlen( $digits ) > 12 && trim( $raw ) === $digits ) continue; // Skip year ranges like "2025-2026" or "2019-20" — these are // never phone numbers but match the digit-dash-digit pattern. if ( preg_match( '/^(19|20)\d{2}-(19|20)?\d{2,4}$/', trim( $raw ) ) ) continue; if ( $digits !== $primary_digits && substr( $digits, -7 ) !== substr( $primary_digits, -7 ) ) { $issues[] = [ 'post_id' => $page->ID, 'title' => $page->post_title, 'found' => trim( $raw ), 'type' => 'phone_mismatch', ]; } } } // Homepage city mention check. if ( $primary->city ) { $front_id = (int) get_option( 'page_on_front' ); $front = $front_id ? get_post( $front_id ) : null; $haystack = $front ? wp_strip_all_tags( $front->post_content ) : ''; if ( $haystack && stripos( $haystack, $primary->city ) === false ) { $issues[] = [ 'post_id' => $front_id, 'title' => $front ? $front->post_title : 'Homepage', 'found' => '', 'type' => 'city_missing_homepage', ]; } } return [ 'status' => $issues ? 'issues' : 'clean', 'issues' => $issues ]; } }