<?php
/**
 * Plugin Name:       Noindex Controls by ConvertAtlas
 * Plugin URI:        https://convertatlas.org/en/wordpress/noindex-controls
 * Description:       Settings to noindex search results, tag archives, author archives and date archives — common thin-content pages that can dilute your SEO.
 * Version:           1.0.0
 * Requires at least: 5.5
 * Requires PHP:      7.4
 * Author:            ConvertAtlas
 * Author URI:        https://convertatlas.org
 * License:           GPL-2.0-or-later
 * Text Domain:       ca-noindex
 *
 * Built with convertatlas.org — free developer tools & templates.
 */

if (!defined('ABSPATH')) { exit; }

final class CA_Noindex_Controls {

    const OPTION = 'ca_noindex_options';

    public static function init() {
        $self = new self();
        add_action('admin_menu', array($self, 'menu'));
        add_action('admin_init', array($self, 'register'));
        add_action('wp_head', array($self, 'output'), 1);
    }

    private function defaults() {
        return array(
            'search'  => 1,
            'tag'     => 0,
            'author'  => 1,
            'date'    => 1,
            'paged'   => 0,
        );
    }

    private function opts() {
        $o = get_option(self::OPTION, array());
        return wp_parse_args(is_array($o) ? $o : array(), $this->defaults());
    }

    public function menu() {
        add_options_page('Noindex Controls', 'Noindex Controls', 'manage_options', 'ca-noindex', array($this, 'page'));
    }

    public function register() {
        register_setting('ca_noindex_group', self::OPTION, array($this, 'sanitize'));
    }

    public function sanitize($input) {
        $out = array();
        foreach (array_keys($this->defaults()) as $k) {
            $out[$k] = empty($input[$k]) ? 0 : 1;
        }
        return $out;
    }

    public function page() {
        $o = $this->opts();
        $fields = array(
            'search' => 'Search results pages',
            'tag'    => 'Tag archives',
            'author' => 'Author archives',
            'date'   => 'Date archives',
            'paged'  => 'Paginated pages (page 2, 3, …)',
        );
        ?>
        <div class="wrap">
            <h1>Noindex Controls</h1>
            <p>Choose which low-value, thin-content page types should be excluded from search engines with a <code>noindex</code> tag.</p>
            <form method="post" action="options.php">
                <?php settings_fields('ca_noindex_group'); ?>
                <table class="form-table" role="presentation"><tbody>
                <?php foreach ($fields as $key => $label): ?>
                    <tr>
                        <th scope="row"><?php echo esc_html($label); ?></th>
                        <td><label><input type="checkbox" name="<?php echo esc_attr(self::OPTION . '[' . $key . ']'); ?>" value="1" <?php checked($o[$key], 1); ?>> noindex</label></td>
                    </tr>
                <?php endforeach; ?>
                </tbody></table>
                <?php submit_button(); ?>
            </form>
            <p style="color:#666">Built with <a href="https://convertatlas.org" target="_blank" rel="noopener">convertatlas.org</a>.</p>
        </div>
        <?php
    }

    public function output() {
        $o = $this->opts();
        $noindex = false;
        if (!empty($o['search']) && is_search()) $noindex = true;
        if (!empty($o['tag']) && is_tag()) $noindex = true;
        if (!empty($o['author']) && is_author()) $noindex = true;
        if (!empty($o['date']) && is_date()) $noindex = true;
        if (!empty($o['paged']) && is_paged()) $noindex = true;

        if ($noindex) {
            echo "\n<!-- Noindex Controls by ConvertAtlas (convertatlas.org) -->\n";
            echo '<meta name="robots" content="noindex,follow">' . "\n";
        }
    }
}

add_action('init', array('CA_Noindex_Controls', 'init'));