<?php
/**
 * Name: Search Engine Visibility Status
 * Description: Show prominent admin bar warning when search engines are discouraged in Reading Settings to prevent accidentally blocking Google and other search engines from indexing
 * Category: Utilities
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Prevent Launch Mistakes",
 *     "description": "Get prominent warning if search engines are blocked so you don't accidentally launch site with 'Discourage search engines' checked."
 *   },
 *   {
 *     "title": "Team Awareness",
 *     "description": "Show all admins and editors that site is not being indexed so no one wonders why Google isn't finding new content."
 *   },
 *   {
 *     "title": "Quick Status Check",
 *     "description": "See indexing status in admin bar without navigating to Reading Settings every time."
 *   }
 * ]
 * Added: 2025-09-12
 * Tags: seo, search-engines, visibility, indexing, notice
 * WhereToFind: {
 *   "location": "Admin bar indicator",
 *   "url": "",
 *   "instructions": "Warning appears in admin bar when search engines discouraged",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardSearchVisibilityStatus {

    public function __construct() {
        add_action('admin_bar_menu', [$this, 'add_admin_bar_notice'], 999);
        add_action('wp_head', [$this, 'add_admin_bar_styles']);
        add_action('admin_head', [$this, 'add_admin_bar_styles']);
    }

    public function add_admin_bar_styles() {
        if (!is_admin_bar_showing() || !current_user_can('edit_posts') || get_option('blog_public') != '0') {
            return;
        }
        ?>
        <style>
        #wpadminbar #wp-admin-bar-search-visibility > .ab-item {
            background: #d63638 !important;
            color: #fff !important;
            animation: sb-attention-pulse 2s ease-in-out 3;
        }
        #wpadminbar #wp-admin-bar-search-visibility:hover > .ab-item {
            background: #b32d2e !important;
            color: #fff !important;
        }
        #wpadminbar #wp-admin-bar-search-visibility .ab-item .sb-warning-icon {
            display: inline-flex;
            align-items: center;
            margin-right: 6px;
        }
        #wpadminbar #wp-admin-bar-search-visibility .ab-item .sb-warning-icon svg {
            fill: currentColor;
        }
        @keyframes sb-attention-pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.7; }
        }
        </style>
        <?php
    }

    public function add_admin_bar_notice($wp_admin_bar) {
        if (!current_user_can('edit_posts')) {
            return;
        }

        if (get_option('blog_public') == '0') {
            $wp_admin_bar->add_node([
                'id' => 'search-visibility',
                'title' => '<span class="sb-warning-icon"><svg width="16" height="16" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10 2L2 18h16L10 2zm0 3.5l5.5 11h-11L10 5.5zM9 8v4h2V8H9zm0 5v2h2v-2H9z"/></svg></span>' . esc_html__('Search Engines Blocked', 'wpswitchboard'),
                'href' => esc_url(admin_url('options-reading.php')),
                'meta' => [
                    'class' => 'search-visibility-warning',
                    'title' => esc_attr__('Your site is discouraging search engines from indexing. Click to change this setting.', 'wpswitchboard')
                ]
            ]);
        }
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array Data storage locations
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardSearchVisibilityStatus();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
