Go Pro ⭐', 'manage_options', 'awbseo-go-pro', [ __CLASS__, 'render_go_pro' ] ); } // ── Hidden tool pages — registered for direct URL access, removed from sidebar below ── add_submenu_page( 'awbseo', __( 'Rank Tracker', 'adworksbid-seo' ), __( 'Rank Tracker', 'adworksbid-seo' ), 'edit_posts', 'awbseo-rank-tracker', [ __CLASS__, 'render_rank_tracker' ] ); add_submenu_page( 'awbseo', __( 'Sitemap', 'adworksbid-seo' ), __( 'Sitemap', 'adworksbid-seo' ), 'manage_options', 'awbseo-sitemap', [ __CLASS__, 'render_sitemap' ] ); add_submenu_page( 'awbseo', __( 'Instant Indexing', 'adworksbid-seo' ), __( 'Instant Indexing', 'adworksbid-seo' ), 'manage_options', 'awbseo-instant-indexing',[ __CLASS__, 'render_instant_indexing' ] ); add_submenu_page( 'awbseo', __( 'Robots.txt', 'adworksbid-seo' ), __( 'Robots.txt', 'adworksbid-seo' ), 'manage_options', 'awbseo-robots-txt', [ __CLASS__, 'render_robots_txt' ] ); add_submenu_page( 'awbseo', __( 'Redirects', 'adworksbid-seo' ), __( 'Redirects', 'adworksbid-seo' ), 'manage_options', 'awbseo-redirects', [ __CLASS__, 'render_redirects' ] ); add_submenu_page( 'awbseo', __( 'Bulk Editor', 'adworksbid-seo' ), __( 'Bulk Editor', 'adworksbid-seo' ), 'edit_posts', 'awbseo-bulk', [ __CLASS__, 'render_bulk_editor' ] ); add_submenu_page( 'awbseo', __( 'Competing Pages', 'adworksbid-seo' ), __( 'Competing Pages', 'adworksbid-seo' ), 'edit_posts', 'awbseo-cannibalization', [ __CLASS__, 'render_cannibalization' ] ); add_submenu_page( 'awbseo', __( 'Fading Content', 'adworksbid-seo' ), __( 'Fading Content', 'adworksbid-seo' ), 'edit_posts', 'awbseo-decay', [ __CLASS__, 'render_decay' ] ); add_submenu_page( 'awbseo', __( 'Schema', 'adworksbid-seo' ), __( 'Schema', 'adworksbid-seo' ), 'manage_options', 'awbseo-schema', [ __CLASS__, 'render_schema' ] ); if ( class_exists( 'WooCommerce' ) ) { add_submenu_page( 'awbseo', __( 'WooCommerce SEO', 'adworksbid-seo' ), __( 'WooCommerce SEO', 'adworksbid-seo' ), 'manage_options', 'awbseo-woo', [ __CLASS__, 'render_woocommerce_seo' ] ); } add_submenu_page( 'awbseo', __( 'Local SEO', 'adworksbid-seo' ), __( 'Local SEO', 'adworksbid-seo' ), 'manage_options', 'awbseo-local', [ __CLASS__, 'render_local_seo' ] ); add_submenu_page( 'awbseo', __( 'Broken Links', 'adworksbid-seo' ), __( 'Broken Links', 'adworksbid-seo' ), 'manage_options', 'awbseo-broken-links', [ __CLASS__, 'render_broken_links' ] ); /** * Fires after the free AWBSEO admin menu is registered. * Pro plugin uses this hook to add its own submenu pages under awbseo. * * @since 1.0.0 */ do_action( 'awbseo_admin_menu' ); } /** * Enqueue the menu icon fix on every admin page so the sidebar icon * renders at the correct 20×20 px size regardless of which page is open. * Uses a virtual (src-less) stylesheet handle so no HTTP request is made. * * Tool pages (rank tracker, sitemap, etc.) are hidden from the sidebar via * CSS :has() selectors rather than remove_submenu_page(). Using * remove_submenu_page() strips the entry from $submenu, causing WordPress * to compute a different hookname on the next request and reject the URL * with "you are not allowed to access this page." * * Pro adds its own hidden slugs via the awbseo_hidden_tool_slugs filter. */ public static function enqueue_menu_icon_css() { wp_register_style( 'awbseo-menu-icon', false, [], AWBSEO_VERSION ); wp_enqueue_style( 'awbseo-menu-icon' ); $hidden_slugs = apply_filters( 'awbseo_hidden_tool_slugs', [ 'awbseo-rank-tracker', 'awbseo-sitemap', 'awbseo-instant-indexing', 'awbseo-robots-txt', 'awbseo-redirects', 'awbseo-bulk', 'awbseo-cannibalization', 'awbseo-decay', 'awbseo-schema', 'awbseo-woo', 'awbseo-local', 'awbseo-broken-links', ] ); $hide_css = ''; foreach ( $hidden_slugs as $slug ) { $hide_css .= '#toplevel_page_awbseo .wp-submenu li:has(>a[href*="page=' . esc_attr( $slug ) . '"]){display:none!important}'; } wp_add_inline_style( 'awbseo-menu-icon', '#toplevel_page_awbseo .wp-menu-image img{width:20px!important;height:20px!important;padding:6px 0 0!important;filter:none!important;opacity:1!important;}' . $hide_css ); } public static function enqueue_assets( $hook ) { if ( strpos( $hook, 'adworksbid-seo' ) === false && strpos( $hook, 'awbseo' ) === false && strpos( $hook, 'awb-setup-wizard' ) === false && $hook !== 'post.php' && $hook !== 'post-new.php' && $hook !== 'edit.php' ) return; wp_enqueue_style( 'awbseo-admin', AWBSEO_PLUGIN_URL . 'admin/assets/css/admin.css', [], AWBSEO_VERSION ); wp_style_add_data( 'awbseo-admin', 'rtl', 'replace' ); // loads admin-rtl.css on RTL sites wp_enqueue_style( 'awbseo-admin-inline', AWBSEO_PLUGIN_URL . 'admin/assets/css/admin-inline.css', [ 'awbseo-admin' ], AWBSEO_VERSION ); wp_enqueue_script( 'awbseo-chart', AWBSEO_PLUGIN_URL . 'admin/assets/js/chart.umd.min.js', [], '4.4.0', true ); wp_enqueue_script( 'awbseo-admin', AWBSEO_PLUGIN_URL . 'admin/assets/js/admin.js', [ 'jquery', 'awbseo-chart' ], AWBSEO_VERSION, true ); wp_localize_script( 'awbseo-admin', 'AWBSEO', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'awbseo_admin' ), 'pro_active' => AWBSEO_PRO_ACTIVE, 'strings' => [ 'saving' => __( 'Saving…', 'adworksbid-seo' ), 'saved' => __( 'Saved!', 'adworksbid-seo' ), 'error' => __( 'Error — please try again.', 'adworksbid-seo' ), 'confirm' => __( 'Are you sure?', 'adworksbid-seo' ), ], ] ); } public static function render_footer_credit( $text ) { $screen = get_current_screen(); if ( ! $screen || ( strpos( $screen->id, 'adworksbid-seo' ) === false && strpos( $screen->id, 'awbseo' ) === false ) ) return $text; return sprintf( esc_html__( 'By %s', 'adworksbid-seo' ), 'AdWorks.Bid' ); } public static function add_action_links( $links ) { $wizard_link = '' . esc_html__( 'Setup Wizard', 'adworksbid-seo' ) . ''; array_unshift( $links, $wizard_link ); return $links; } // ── Page renderers ──────────────────────────────────────────────────────── public static function render_dashboard() { require AWBSEO_PLUGIN_DIR . 'admin/views/dashboard.php'; } public static function render_bulk_editor() { require AWBSEO_PLUGIN_DIR . 'admin/views/bulk-editor.php'; } public static function render_schema() { require AWBSEO_PLUGIN_DIR . 'admin/views/schema.php'; } public static function render_redirects() { require AWBSEO_PLUGIN_DIR . 'admin/views/redirects.php'; } public static function render_decay() { require AWBSEO_PLUGIN_DIR . 'admin/views/content-decay.php'; } public static function render_cannibalization() { require AWBSEO_PLUGIN_DIR . 'admin/views/cannibalization.php'; } public static function render_broken_links() { require AWBSEO_PLUGIN_DIR . 'admin/views/broken-links.php'; } public static function render_instant_indexing() { require AWBSEO_PLUGIN_DIR . 'admin/views/instant-indexing.php'; } public static function render_sitemap() { require AWBSEO_PLUGIN_DIR . 'admin/views/sitemap.php'; } public static function render_settings() { require AWBSEO_PLUGIN_DIR . 'admin/views/settings.php'; } public static function render_portability() { require AWBSEO_PLUGIN_DIR . 'admin/views/data-portability.php'; } public static function render_rank_tracker() { require AWBSEO_PLUGIN_DIR . 'admin/views/rank-tracker.php'; } public static function render_local_seo() { require AWBSEO_PLUGIN_DIR . 'admin/views/local-seo.php'; } public static function render_woocommerce_seo() { require AWBSEO_PLUGIN_DIR . 'admin/views/woocommerce-seo.php'; } public static function render_robots_txt() { require AWBSEO_PLUGIN_DIR . 'admin/views/robots-txt.php'; } public static function render_analytics() { require AWBSEO_PLUGIN_DIR . 'admin/views/analytics.php'; } // ── Category landing pages ──────────────────────────────────────────────── /** * Shared category page renderer. Outputs a card grid of tool links. * * @param array $config { * @type string $title Page heading. * @type string $desc Optional subtitle. * @type array[] $tools Each tool: icon, name, desc, url, pro (bool). * } */ private static function render_category_page( array $config ) { $tools = $config['tools'] ?? []; ?>
'📈', 'name' => __( 'Rank Tracker', 'adworksbid-seo' ), 'desc' => __( 'Track keyword positions daily and see ranking trends over time.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-rank-tracker' ), 'pro' => true, ], [ 'icon' => '📉', 'name' => __( 'Fading Content', 'adworksbid-seo' ), 'desc' => __( 'Find pages that used to rank but are slowly losing traffic and visibility.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-decay' ), ], [ 'icon' => '⚠️', 'name' => __( 'Competing Pages', 'adworksbid-seo' ), 'desc' => __( 'Detect keywords where multiple pages cannibalize each other in search results.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-cannibalization' ), ], ], 'growth' ); self::render_category_page( [ 'title' => __( 'Track Your Growth', 'adworksbid-seo' ), 'desc' => __( 'Monitor keyword rankings, spot fading content, and catch pages competing against each other.', 'adworksbid-seo' ), 'tools' => $tools, ] ); } public static function render_cat_visibility() { $tools = apply_filters( 'awbseo_category_tools', [ [ 'icon' => '🗺', 'name' => __( 'Sitemap', 'adworksbid-seo' ), 'desc' => __( 'Generate and configure your XML sitemap so search engines discover every page.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-sitemap' ), ], [ 'icon' => '⚡', 'name' => __( 'Instant Indexing', 'adworksbid-seo' ), 'desc' => __( 'Push new and updated pages directly to Google and Bing for faster indexing.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-instant-indexing' ), ], [ 'icon' => '🤖', 'name' => __( 'Robots.txt', 'adworksbid-seo' ), 'desc' => __( 'Control which pages search engine crawlers can and cannot access on your site.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-robots-txt' ), ], [ 'icon' => '🔀', 'name' => __( 'Redirects', 'adworksbid-seo' ), 'desc' => __( 'Manage 301 and 302 redirects to preserve link equity and fix broken paths.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-redirects' ), ], ], 'visibility' ); self::render_category_page( [ 'title' => __( 'Get Found on Google', 'adworksbid-seo' ), 'desc' => __( 'Control how search engines crawl, index, and surface your site in search results.', 'adworksbid-seo' ), 'tools' => $tools, ] ); } public static function render_cat_optimize() { $tools = apply_filters( 'awbseo_category_tools', [ [ 'icon' => '✏️', 'name' => __( 'Bulk Editor', 'adworksbid-seo' ), 'desc' => __( 'Edit SEO titles, meta descriptions, and focus keywords across all posts and pages at once.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-bulk' ), ], ], 'optimize' ); self::render_category_page( [ 'title' => __( 'Optimize Your Pages', 'adworksbid-seo' ), 'desc' => __( 'Edit titles, descriptions, and focus keywords across your entire site in one place.', 'adworksbid-seo' ), 'tools' => $tools, ] ); } public static function render_cat_local() { $tools = apply_filters( 'awbseo_category_tools', [ [ 'icon' => '📍', 'name' => __( 'Local SEO', 'adworksbid-seo' ), 'desc' => __( 'Manage NAP data, business hours, and local schema to rank in map and local searches.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-local' ), ], ], 'local' ); self::render_category_page( [ 'title' => __( 'Build Local Authority', 'adworksbid-seo' ), 'desc' => __( 'Set up local business signals so Google and AI engines surface you in local searches.', 'adworksbid-seo' ), 'tools' => $tools, ] ); } public static function render_cat_ai() { $tools = [ [ 'icon' => '🧩', 'name' => __( 'Schema Markup', 'adworksbid-seo' ), 'desc' => __( 'Add structured data so AI engines and search engines understand what your content is about.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-schema' ), ], ]; if ( class_exists( 'WooCommerce' ) ) { $tools[] = [ 'icon' => '🛒', 'name' => __( 'WooCommerce SEO', 'adworksbid-seo' ), 'desc' => __( 'Optimize product titles, descriptions, and schema for your WooCommerce store.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-woo' ), ]; } $tools = apply_filters( 'awbseo_category_tools', $tools, 'ai' ); self::render_category_page( [ 'title' => __( 'Enhance AI Search', 'adworksbid-seo' ), 'desc' => __( 'Structure your content so AI engines can understand, cite, and recommend your pages.', 'adworksbid-seo' ), 'tools' => $tools, ] ); } public static function render_cat_fix() { $tools = apply_filters( 'awbseo_category_tools', [ [ 'icon' => '🔗', 'name' => __( 'Broken Links', 'adworksbid-seo' ), 'desc' => __( 'Scan your site for dead links that hurt your SEO and user experience, then fix them.', 'adworksbid-seo' ), 'url' => admin_url( 'admin.php?page=awbseo-broken-links' ), ], ], 'fix' ); self::render_category_page( [ 'title' => __( 'Fix & Maintain', 'adworksbid-seo' ), 'desc' => __( 'Find broken links and catch technical issues before they affect your rankings.', 'adworksbid-seo' ), 'tools' => $tools, ] ); } public static function render_go_pro() { $pro_url = 'https://adworks.bid/wordpress-seo-plugin/?utm_source=plugin&utm_medium=go_pro_page'; ?> get_error_message() ); } // Let PRO record bulk edits in the Activity Log. if ( $result > 0 ) { do_action( 'awbseo_activity', 'on-page', 'Bulk updated SEO data', $result . ' post' . ( $result !== 1 ? 's' : '' ) . ' updated' ); } wp_send_json_success( [ 'saved' => $result ] ); } public static function ajax_save_redirect() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error(); $source = sanitize_text_field( $_POST['source'] ?? '' ); $target = esc_url_raw( $_POST['target'] ?? '' ); $type = (int) ( $_POST['type'] ?? 301 ); if ( ! $source || ! $target ) wp_send_json_error( 'Source and target are required.' ); $result = AWBSEO_Redirects::add( $source, $target, $type ); if ( false === $result ) { global $wpdb; wp_send_json_error( $wpdb->last_error ?: 'Database write failed. Try deactivating and reactivating the plugin to rebuild tables.' ); } do_action( 'awbseo_activity', 'technical', 'Added ' . $type . ' redirect', $source . ' -> ' . $target ); wp_send_json_success(); } public static function ajax_delete_redirect() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error(); AWBSEO_Redirects::delete( (int) ( $_POST['id'] ?? 0 ) ); wp_send_json_success(); } public static function ajax_import_redirects() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error(); $csv = isset( $_POST['csv'] ) ? sanitize_textarea_field( wp_unslash( $_POST['csv'] ) ) : ''; if ( ! $csv ) wp_send_json_error( 'No CSV data provided.' ); $imported = AWBSEO_Redirects::import_csv( $csv ); wp_send_json_success( [ 'imported' => $imported ] ); } public static function ajax_add_keyword() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) wp_send_json_error( 'Forbidden' ); $kw = sanitize_text_field( $_POST['keyword'] ?? '' ); $url = esc_url_raw( $_POST['url'] ?? '' ); $ok = AWBSEO_Rank_Tracker::add_keyword( $kw, $url ); if ( ! $ok ) wp_send_json_error( 'Keyword already tracked or invalid.' ); // Let PRO record keyword additions in the Activity Log. do_action( 'awbseo_activity', 'content', 'Started tracking keyword', '"' . $kw . '"' ); $position = AWBSEO_Rank_Tracker::fetch_position( $kw ); wp_send_json_success( [ 'position' => $position ] ); } public static function ajax_remove_keyword() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) wp_send_json_error( 'Forbidden' ); AWBSEO_Rank_Tracker::remove_keyword( sanitize_text_field( $_POST['keyword'] ?? '' ) ); wp_send_json_success(); } // ── Local SEO ───────────────────────────────────────────────────────────── public static function ajax_get_location() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden' ); $loc = AWBSEO_Local_SEO::get_location( (int) ( $_POST['id'] ?? 0 ) ); $loc ? wp_send_json_success( $loc ) : wp_send_json_error( 'Not found.' ); } public static function ajax_save_location() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden' ); $raw_hours = json_decode( wp_unslash( $_POST['hours'] ?? '[]' ), true ); $hours = is_array( $raw_hours ) ? array_map( function( $row ) { return is_array( $row ) ? array_map( 'sanitize_text_field', $row ) : []; }, $raw_hours ) : []; $result = AWBSEO_Local_SEO::save_location( [ 'id' => absint( $_POST['id'] ?? 0 ), 'name' => sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ), 'street' => sanitize_text_field( wp_unslash( $_POST['street'] ?? '' ) ), 'city' => sanitize_text_field( wp_unslash( $_POST['city'] ?? '' ) ), 'region' => sanitize_text_field( wp_unslash( $_POST['region'] ?? '' ) ), 'postal_code' => sanitize_text_field( wp_unslash( $_POST['postal_code'] ?? '' ) ), 'country' => sanitize_text_field( wp_unslash( $_POST['country'] ?? 'US' ) ), 'phone' => sanitize_text_field( wp_unslash( $_POST['phone'] ?? '' ) ), 'email' => sanitize_email( wp_unslash( $_POST['email'] ?? '' ) ), 'lat' => sanitize_text_field( wp_unslash( $_POST['lat'] ?? '' ) ), 'lng' => sanitize_text_field( wp_unslash( $_POST['lng'] ?? '' ) ), 'price_range' => sanitize_text_field( wp_unslash( $_POST['price_range'] ?? '' ) ), 'hours' => $hours, 'page_id' => absint( $_POST['page_id'] ?? 0 ), 'is_primary' => absint( $_POST['is_primary'] ?? 0 ), ] ); if ( is_wp_error( $result ) ) wp_send_json_error( $result->get_error_message() ); // Let PRO record location saves in the Activity Log. do_action( 'awbseo_activity', 'local', 'Saved local SEO location', sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) ) ); wp_send_json_success( [ 'id' => $result ] ); } public static function ajax_delete_location() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden' ); AWBSEO_Local_SEO::delete_location( (int) ( $_POST['id'] ?? 0 ) ); wp_send_json_success(); } // ── WooCommerce SEO ─────────────────────────────────────────────────────── public static function ajax_save_woo_settings() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden' ); wp_send_json_success( AWBSEO_Woocommerce_SEO::save_settings( $_POST ) ); } public static function ajax_woo_bulk_fix_alt() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) wp_send_json_error( 'Forbidden' ); wp_send_json_success( [ 'fixed' => AWBSEO_Woocommerce_SEO::bulk_fix_alt_text() ] ); } public static function handle_export_csv() { check_admin_referer( 'awbseo_export' ); AWBSEO_Bulk_Editor::export_csv(); } public static function save_settings() { check_admin_referer( 'awbseo_save_settings' ); if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Forbidden' ); $defaults = AWBSEO_Core::default_settings(); $current = get_option( 'awbseo_settings', $defaults ); $new = []; $preserve_if_blank = [ 'openai_api_key', 'anthropic_api_key', 'gemini_api_key', 'perplexity_api_key' ]; $not_on_this_form = [ 'sitemap_taxonomies', 'cwv_threshold_lcp', 'cwv_threshold_fid', 'cwv_threshold_inp', 'cwv_threshold_cls', 'pagespeed_api_key' ]; foreach ( $defaults as $key => $default ) { if ( in_array( $key, $not_on_this_form, true ) ) { $new[ $key ] = $current[ $key ] ?? $default; } elseif ( is_bool( $default ) ) { $new[ $key ] = isset( $_POST[ 'awb_' . $key ] ); } elseif ( is_array( $default ) ) { $new[ $key ] = isset( $_POST[ 'awb_' . $key ] ) ? array_map( 'sanitize_text_field', (array) $_POST[ 'awb_' . $key ] ) : []; } elseif ( in_array( $key, $preserve_if_blank, true ) ) { $submitted = sanitize_text_field( $_POST[ 'awb_' . $key ] ?? '' ); $new[ $key ] = ( '' !== $submitted ) ? $submitted : ( $current[ $key ] ?? $default ); } else { $new[ $key ] = sanitize_text_field( $_POST[ 'awb_' . $key ] ?? $default ); } } // Hreflang pairs. $pairs = []; if ( isset( $_POST['awb_hreflang_lang'] ) && is_array( $_POST['awb_hreflang_lang'] ) ) { foreach ( array_keys( $_POST['awb_hreflang_lang'] ) as $i ) { $lang = sanitize_text_field( $_POST['awb_hreflang_lang'][ $i ] ); $url = esc_url_raw( $_POST['awb_hreflang_url'][ $i ] ?? '' ); if ( $lang && $url ) $pairs[] = compact( 'lang', 'url' ); } } $new['hreflang_pairs'] = $pairs; update_option( 'awbseo_settings', array_merge( $current, $new ) ); AWBSEO_Sitemap::flush_cache(); // Allow Pro plugin to flush its own caches on settings save. do_action( 'awbseo_settings_saved', $new ); wp_redirect( admin_url( 'admin.php?page=awbseo-settings&saved=1' ) ); exit; } // ── Dashboard data helpers ──────────────────────────────────────────────── private static function pct_change( $cur, $prev ) { if ( $prev == 0 ) return null; return round( ( ( $cur - $prev ) / $prev ) * 100, 1 ); } private static function summarize_site_stats( $site_stats ) { $summary = [ 'clicks' => 0, 'impressions' => 0, 'ctr' => 0, 'position' => 0, 'dates' => [], 'daily_clicks' => [], 'daily_impr' => [], 'clicks_change' => null, 'impr_change' => null, ]; if ( empty( $site_stats['daily'] ) ) return $summary; $ctr_sum = 0; $pos_sum = 0; foreach ( $site_stats['daily'] as $row ) { $summary['clicks'] += $row['clicks']; $summary['impressions'] += $row['impressions']; $ctr_sum += $row['ctr']; $pos_sum += $row['position']; $summary['dates'][] = $row['keys'][0]; $summary['daily_clicks'][] = (int) $row['clicks']; $summary['daily_impr'][] = (int) $row['impressions']; } $count = count( $site_stats['daily'] ); $summary['ctr'] = $count ? round( ( $ctr_sum / $count ) * 100, 1 ) : 0; $summary['position'] = $count ? round( $pos_sum / $count, 1 ) : 0; $summary['clicks_change'] = self::pct_change( $summary['clicks'], $site_stats['prev_sum']['clicks'] ?? 0 ); $summary['impr_change'] = self::pct_change( $summary['impressions'], $site_stats['prev_sum']['impressions'] ?? 0 ); $summary['ctr_change'] = self::pct_change( $summary['ctr'], $site_stats['prev_sum']['ctr'] ?? 0 ); $summary['position_change'] = self::pct_change( $summary['position'], $site_stats['prev_sum']['position'] ?? 0 ); return $summary; } public static function get_dashboard_data( $range_days = 28, $start = null, $end = null ) { $valid = [ 7 => true, 28 => true, 90 => true, 180 => true ]; if ( ! isset( $valid[ $range_days ] ) ) $range_days = 28; $use_custom = (bool) ( $start && $end ); if ( $use_custom && $start > $end ) [ $start, $end ] = [ $end, $start ]; $range_label = $use_custom ? date_i18n( 'M j', strtotime( $start ) ) . ' – ' . date_i18n( 'M j, Y', strtotime( $end ) ) : "last {$range_days} days"; $range_days_display = $use_custom ? max( 1, (int) ( ( strtotime( $end ) - strtotime( $start ) ) / DAY_IN_SECONDS ) + 1 ) : $range_days; $gsc = AWBSEO_GSC::get_instance(); $gsc_on = $gsc->is_connected(); $gsc_stats = $gsc_on ? $gsc->get_site_stats( $range_days, $use_custom ? $start : null, $use_custom ? $end : null ) : null; $gsc_summary = $gsc_on ? self::summarize_site_stats( $gsc_stats ) : null; $top_pages = $gsc_on ? $gsc->get_top_pages( $range_days, 10, $use_custom ? $start : null, $use_custom ? $end : null ) : []; $top_queries = $gsc_on ? $gsc->get_top_queries( $range_days, 10, $use_custom ? $start : null, $use_custom ? $end : null ) : []; $gsc_last_error = ( $gsc_on && empty( $gsc_summary['dates'] ) ) ? $gsc->get_last_error() : null; $bing = AWBSEO_Bing::get_instance(); $bing_on = $bing->is_connected(); $bing_stats = $bing_on ? $bing->get_site_stats( $range_days, $use_custom ? $start : null, $use_custom ? $end : null ) : null; $bing_summary = $bing_on ? self::summarize_site_stats( $bing_stats ) : null; $bing_top_pages = $bing_on ? $bing->get_top_pages( $range_days, 10, $use_custom ? $start : null, $use_custom ? $end : null ) : []; $bing_top_queries = $bing_on ? $bing->get_top_queries( $range_days, 10, $use_custom ? $start : null, $use_custom ? $end : null ) : []; return compact( 'range_days', 'use_custom', 'start', 'end', 'range_label', 'range_days_display', 'gsc_on', 'gsc_stats', 'gsc_summary', 'gsc_last_error', 'top_pages', 'top_queries', 'bing_on', 'bing_stats', 'bing_summary', 'bing_top_pages', 'bing_top_queries' ); } public static function render_page_rows( $rows ) { if ( ! $rows ) return ''; ob_start(); foreach ( $rows as $row ) : $url = $row['keys'][0]; $path = parse_url( $url, PHP_URL_PATH ) ?: '/'; $pos = round( $row['position'], 1 ); $pos_class = $pos <= 3 ? 'top' : ( $pos <= 10 ? 'good' : ( $pos <= 20 ? 'ok' : 'low' ) ); ?>