From 26e05eee376b7ff7ec02f9ee72f58347d3a2a230 Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 11:29:37 +0100 Subject: [PATCH 01/11] refactoring --- noindex-seo.php | 1050 +++++++++++++---------------------------------- 1 file changed, 293 insertions(+), 757 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index 2b0f820..41b26d1 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -2,7 +2,7 @@ /** * Plugin Name: noindex SEO * Plugin URI: https://wordpress.org/plugins/noindex-seo/ - * Description: Allows to add a meta-tag for robots noindex in some parts of your WordPress site. + * Description: Allows adding a meta-tag for robots noindex in specific parts of your WordPress site. * Requires at least: 4.1 * Requires PHP: 5.6 * Version: 1.1.0 @@ -19,172 +19,64 @@ defined( 'ABSPATH' ) || die( 'Bye bye!' ); /** - * Retrieves and sets the SEO 'noindex' values for various WordPress contexts. + * Outputs a 'noindex' meta robots tag to the page. + * + * This function prints the 'noindex' meta robots tag, instructing search engines + * not to index the content of the current page. + * + * @since 1.0.0 + * + * @return void + */ +function noindex_seo_metarobots() { + echo '' . "\n"; +} +/** + * Retrieves and sets the SEO 'noindex' values for various WordPress contexts. + * * @global WP_Post $post The post global for the current post, if within The Loop. * * @return void * * @since 1.0.0 - * - * @uses get_option() Fetches an option value based on an option name. */ function noindex_seo_show() { global $post; - $enter = true; + $noindex_seo_values = array( - 'error' => (bool) get_option( 'noindex_seo_error' ), - 'archive' => (bool) get_option( 'noindex_seo_archive' ), - 'attachment' => (bool) get_option( 'noindex_seo_attachment' ), - 'author' => (bool) get_option( 'noindex_seo_author' ), - 'category' => (bool) get_option( 'noindex_seo_category' ), - 'comment_feed' => (bool) get_option( 'noindex_seo_comment_feed' ), - 'customize_preview' => (bool) get_option( 'noindex_seo_customize_preview' ), - 'date' => (bool) get_option( 'noindex_seo_date' ), - 'day' => (bool) get_option( 'noindex_seo_day' ), - 'feed' => (bool) get_option( 'noindex_seo_feed' ), - 'front_page' => (bool) get_option( 'noindex_seo_front_page' ), - 'home' => (bool) get_option( 'noindex_seo_home' ), - 'month' => (bool) get_option( 'noindex_seo_month' ), - 'page' => (bool) get_option( 'noindex_seo_page' ), - 'paged' => (bool) get_option( 'noindex_seo_paged' ), - 'post_type_archive' => (bool) get_option( 'noindex_seo_post_type_archive' ), - 'preview' => (bool) get_option( 'noindex_seo_preview' ), - 'privacy_policy' => (bool) get_option( 'noindex_seo_privacy_policy' ), - 'robots' => (bool) get_option( 'noindex_seo_robots' ), - 'search' => (bool) get_option( 'noindex_seo_search' ), - 'single' => (bool) get_option( 'noindex_seo_single' ), - 'singular' => (bool) get_option( 'noindex_seo_singular' ), - 'tag' => (bool) get_option( 'noindex_seo_tag' ), - 'time' => (bool) get_option( 'noindex_seo_time' ), - 'year' => (bool) get_option( 'noindex_seo_year' ), + 'front_page' => is_front_page(), + 'home' => is_home(), + 'page' => is_page(), + 'privacy_policy' => function_exists( 'is_privacy_policy' ) ? is_privacy_policy() : false, + 'single' => is_single(), + 'singular' => is_singular(), + 'category' => is_category(), + 'tag' => is_tag(), + 'date' => is_date(), + 'day' => is_day(), + 'month' => is_month(), + 'time' => is_time(), + 'year' => is_year(), + 'archive' => is_archive(), + 'author' => is_author(), + 'post_type_archive' => is_post_type_archive(), + 'paged' => is_paged(), + 'search' => is_search(), + 'attachment' => is_attachment(), + 'customize_preview' => is_customize_preview(), + 'preview' => is_preview(), + 'error' => is_404(), ); - // GLOBAL IMPORTANT PAGES. - if ( $enter && $noindex_seo_values['front_page'] && function_exists( 'is_front_page' ) && is_front_page() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['home'] && function_exists( 'is_home' ) && is_home() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // PAGES / POSTS. - if ( $enter && $noindex_seo_values['page'] && function_exists( 'is_page' ) && is_page() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['privacy_policy'] && function_exists( 'is_privacy_policy' ) && is_privacy_policy() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['single'] && function_exists( 'is_single' ) && is_single() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['singular'] && function_exists( 'is_singular' ) && is_singular() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // CATEGORIES / TAGS. - if ( $enter && $noindex_seo_values['category'] && function_exists( 'is_category' ) && is_category() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['tag'] && function_exists( 'is_tag' ) && is_tag() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // DATES. - if ( $enter && $noindex_seo_values['date'] && function_exists( 'is_date' ) && is_date() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['day'] && function_exists( 'is_day' ) && is_day() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['month'] && function_exists( 'is_month' ) && is_month() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['time'] && function_exists( 'is_time' ) && is_time() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['year'] && function_exists( 'is_year' ) && is_year() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // ARCHIVE. - if ( $enter && $noindex_seo_values['archive'] && function_exists( 'is_archive' ) && is_archive() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['author'] && function_exists( 'is_author' ) && is_author() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['post_type_archive'] && function_exists( 'is_post_type_archive' ) && is_post_type_archive() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // PAGINATION. - if ( $enter && $noindex_seo_values['paged'] && function_exists( 'is_paged' ) && is_paged() ) { - noindex_seo_metarobots(); - $enter = false; + foreach ( $noindex_seo_values as $key => $condition ) { + if ( $condition && (bool) get_option( 'noindex_seo_' . $key ) ) { + noindex_seo_metarobots(); + break; // Prevent multiple meta tags from being added. + } } - // SEARCH. - if ( $enter && $noindex_seo_values['search'] && function_exists( 'is_search' ) && is_search() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // ATTACHMENT. - if ( $enter && $noindex_seo_values['attachment'] && function_exists( 'is_attachment' ) && is_attachment() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // PREVIEW. - if ( $enter && $noindex_seo_values['customize_preview'] && function_exists( 'is_customize_preview' ) && is_customize_preview() ) { - noindex_seo_metarobots(); - $enter = false; - } - if ( $enter && $noindex_seo_values['preview'] && function_exists( 'is_preview' ) && is_preview() ) { - noindex_seo_metarobots(); - $enter = false; - } - - // ERROR. - if ( $enter && $noindex_seo_values['error'] && function_exists( 'is_404' ) && is_404() ) { - noindex_seo_metarobots(); - $enter = false; - } - - unset( $enter, $noindex_seo_values ); -} - -/** - * Outputs a 'noindex' meta robots tag to the page. - * - * This function prints the 'noindex' meta robots tag, instructing search engines - * not to index the content of the current page. - * - * @since 1.0.0 - * - * @return void - * - * @uses echo Outputs the meta robots tag to the webpage. - */ -function noindex_seo_metarobots() { - echo '' . "\n"; + unset( $noindex_seo_values ); } add_action( 'wp_head', 'noindex_seo_show' ); @@ -195,656 +87,300 @@ function noindex_seo_metarobots() { /** * Adds a 'Settings' link to the plugin action links on the plugins page. * - * This function appends a direct link to the plugin's settings page within the WordPress admin dashboard, - * making it easier for users to access and configure the plugin settings. - * * @since 1.0.0 * * @param array $links An array of action links already present for the plugin. * - * @return array Returns the updated array of action links including the 'Settings' link. - * - * @uses get_admin_url() Fetches the admin URL for the current site with optional path attached. - * @uses __() Retrieves a translated string. + * @return array Returns the updated array of action links including the 'Settings' link. */ function noindex_seo_settings_link( $links ) { - $links[] = '' . esc_html( __( 'Settings' ) ) . ''; + $settings_link = '' . esc_html__( 'Settings', 'noindex-seo' ) . ''; + $links[] = $settings_link; return $links; } /** * Adds a 'noindex SEO' options page to the WordPress admin menu. * - * This function creates an options page titled 'noindex SEO' within the WordPress admin dashboard. - * The page provides a UI for configuring settings related to the 'noindex SEO' functionality. - * * @since 1.0.0 * * @return void - * - * @uses add_options_page() Adds a new options page to the admin menu. - * @uses __() Retrieves a translated string. */ function noindex_seo_menu() { - add_options_page( __( 'noindex SEO', 'noindex-seo' ), __( 'noindex SEO', 'noindex-seo' ), 'manage_options', 'noindex_seo', 'noindex_seo_admin' ); + add_options_page( + __( 'noindex SEO', 'noindex-seo' ), + __( 'noindex SEO', 'noindex-seo' ), + 'manage_options', + 'noindex_seo', + 'noindex_seo_admin' + ); } /** * Registers settings for the 'noindex SEO' plugin. * - * This function registers a variety of settings associated with the 'noindex SEO' functionality in WordPress. - * Each setting determines whether specific pages or post types should be excluded from search engine indexing. - * The settings values are integers, with a default value of 0 (which typically represents 'false' or 'off' in boolean context). - * * @since 1.0.0 * * @return void - * - * @uses register_setting() Registers a setting and its data. */ function noindex_seo_register() { - register_setting( - 'noindexseo', - 'noindex_seo_error', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_archive', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_attachment', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_author', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_category', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_comment_feed', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_customize_preview', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_date', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_day', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_feed', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_front_page', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_home', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_month', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_page', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_paged', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_post_type_archive', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_preview', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_privacy_policy', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_robots', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_search', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_single', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_singular', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_tag', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_time', - array( - 'type' => 'integer', - 'default' => 0, - ) - ); - register_setting( - 'noindexseo', - 'noindex_seo_year', - array( - 'type' => 'integer', - 'default' => 0, - ) + $settings = array( + 'error', + 'archive', + 'attachment', + 'author', + 'category', + 'comment_feed', + 'customize_preview', + 'date', + 'day', + 'feed', + 'front_page', + 'home', + 'month', + 'page', + 'paged', + 'post_type_archive', + 'preview', + 'privacy_policy', + 'robots', + 'search', + 'single', + 'singular', + 'tag', + 'time', + 'year', ); + + foreach ( $settings as $setting ) { + register_setting( + 'noindexseo', + 'noindex_seo_' . $setting, + array( + 'type' => 'integer', + 'default' => 0, + ) + ); + } } /** * Displays the administration settings page for the 'noindex SEO' plugin. * - * This function provides the markup for the settings page of the plugin in the WordPress admin area. - * It shows the header for the settings, followed by the form that contains the settings fields. - * Each setting determines if specific pages or post types should be excluded from search engine indexing. - * The options are retrieved from the WordPress database and are displayed on the form. - * * @since 1.0.0 * * @return void - * - * @uses settings_fields() Outputs nonce, action, and option_page fields for a settings page in the form. - * @uses get_option() Retrieves option value based on option name from WordPress database. */ function noindex_seo_admin() { + // Define sections and their respective settings. + $sections = array( + 'main_pages' => array( + 'title' => __( 'Main Pages', 'noindex-seo' ), + 'fields' => array( + 'front_page' => array( + 'label' => __( 'Front Page', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the site\'s front page.', 'noindex-seo' ), + 'view_url' => get_site_url(), + ), + 'home' => array( + 'label' => __( 'Home', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the site\'s home page.', 'noindex-seo' ), + 'view_url' => get_home_url(), + ), + ), + ), + 'pages_posts' => array( + 'title' => __( 'Pages and Posts', 'noindex-seo' ), + 'fields' => array( + 'page' => array( + 'label' => __( 'Page', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the site\'s pages.', 'noindex-seo' ), + ), + 'privacy_policy' => array( + 'label' => __( 'Privacy Policy', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the site\'s privacy policy page.', 'noindex-seo' ), + 'view_url' => get_privacy_policy_url(), + 'conditional' => version_compare( $GLOBALS['wp_version'], '5.2', '>=' ), + ), + 'single' => array( + 'label' => __( 'Single', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of a post on the site.', 'noindex-seo' ), + ), + 'singular' => array( + 'label' => __( 'Singular', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of a post or a page of the site.', 'noindex-seo' ), + ), + ), + ), + 'taxonomies' => array( + 'title' => __( 'Taxonomies', 'noindex-seo' ), + 'fields' => array( + 'category' => array( + 'label' => __( 'Category', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the site categories. The lists where the posts appear.', 'noindex-seo' ), + ), + 'tag' => array( + 'label' => __( 'Tag', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the site\'s tags. The lists where the posts appear.', 'noindex-seo' ), + ), + ), + ), + 'dates' => array( + 'title' => __( 'Dates', 'noindex-seo' ), + 'fields' => array( + 'date' => array( + 'label' => __( 'Date', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of any date-based archive page (i.e., monthly, yearly, daily, or time-based archive) of the site. The lists where the posts appear.', 'noindex-seo' ), + ), + 'day' => array( + 'label' => __( 'Day', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of a daily archive of the site. The lists where the posts appear.', 'noindex-seo' ), + ), + 'month' => array( + 'label' => __( 'Month', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of a monthly archive of the site. The lists where the posts appear.', 'noindex-seo' ), + ), + 'time' => array( + 'label' => __( 'Time', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of an hourly, "minutely", or "secondly" archive of the site. The lists where the posts appear.', 'noindex-seo' ), + ), + 'year' => array( + 'label' => __( 'Year', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of a yearly archive of the site. The lists where the posts appear.', 'noindex-seo' ), + ), + ), + ), + 'archives' => array( + 'title' => __( 'Archives', 'noindex-seo' ), + 'fields' => array( + 'archive' => array( + 'label' => __( 'Archive', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of any type of Archive page. Category, Tag, Author, and Date-based pages are all types of Archives. The lists where the posts appear.', 'noindex-seo' ), + ), + 'author' => array( + 'label' => __( 'Author', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the author\'s page, where the author\'s publications appear.', 'noindex-seo' ), + ), + 'post_type_archive' => array( + 'label' => __( 'Post Type Archive', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of any post type page.', 'noindex-seo' ), + ), + ), + ), + 'pagination' => array( + 'title' => __( 'Pagination', 'noindex-seo' ), + 'fields' => array( + 'paged' => array( + 'label' => __( 'Pagination', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the pagination, i.e., all pages other than the main page of an archive.', 'noindex-seo' ), + ), + ), + ), + 'search' => array( + 'title' => __( 'Search', 'noindex-seo' ), + 'fields' => array( + 'search' => array( + 'label' => __( 'Search', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of the internal search result pages.', 'noindex-seo' ), + ), + ), + ), + 'attachments' => array( + 'title' => __( 'Attachments', 'noindex-seo' ), + 'fields' => array( + 'attachment' => array( + 'label' => __( 'Attachment', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing of an attachment document to a post or page. An attachment is an image or other file uploaded through the post editor\'s upload utility. Attachments can be displayed on their own "page" or template. This will not cause the indexing of the image or file to be blocked.', 'noindex-seo' ), + ), + ), + ), + 'previews' => array( + 'title' => __( 'Previews', 'noindex-seo' ), + 'fields' => array( + 'customize_preview' => array( + 'label' => __( 'Customize Preview', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing when content is being displayed in customize mode.', 'noindex-seo' ), + ), + 'preview' => array( + 'label' => __( 'Preview', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will block the indexing when a single post is being displayed in draft mode.', 'noindex-seo' ), + ), + ), + ), + 'error_page' => array( + 'title' => __( 'Error Page', 'noindex-seo' ), + 'fields' => array( + 'error' => array( + 'label' => __( 'Error 404', 'noindex-seo' ), + 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'description' => __( 'This will cause an error page to be blocked from being indexed. As it is an error page, it should not be indexed per se, but just in case.', 'noindex-seo' ), + ), + ), + ), + ); + ?> -
+

- -

- -

- - - - - - - - - -
- > : .
- > : .
- -

- - - - - -=' ) ) { -?> - - - - - - - - - - - - - -

- > : .
- > : .
- > : .
- > : .
- -

- - - - - - - - - -
- > : .
- > : .
- -

- - - - - - - - - - - - - - - - - - - - - -
- > : .
- > : .
- > : .
$section ) { + echo '

' . esc_html( $section['title'] ) . '

'; + echo ''; + foreach ( $section['fields'] as $field_id => $field ) { + // Check for conditional display. + if ( isset( $field['conditional'] ) && ! $field['conditional'] ) { + continue; + } + + // Get current option value. + $option = get_option( 'noindex_seo_' . $field_id, 0 ); + + echo ''; + echo ''; + echo ''; + echo ''; + } + echo '
'; + echo ' '; + echo esc_html( $field['recommended'] ) . ': . '; + echo '' . esc_html( $field['description'] ) . ''; + + if ( isset( $field['view_url'] ) ) { + echo ' ' . esc_html__( 'View', 'noindex-seo' ) . ''; + } + + echo '
'; } ?> - > : .
- > : .
- -

- - - - - - - - - - - - - -
- > : .
- > : .
- > : .
- -

- - - - - -
- > : .
- -

- - - - - -
- > : .
- -

- - - - - -
- > : .
- -

- - - - - - - - - -
- > : .
- > : .
- -

- - - - - -
- > : .
-
-
- + Date: Sat, 2 Nov 2024 11:55:52 +0100 Subject: [PATCH 02/11] suggestions --- noindex-seo.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/noindex-seo.php b/noindex-seo.php index 41b26d1..dbd2114 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -180,12 +180,14 @@ function noindex_seo_admin() { 'front_page' => array( 'label' => __( 'Front Page', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s front page.', 'noindex-seo' ), 'view_url' => get_site_url(), ), 'home' => array( 'label' => __( 'Home', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s home page.', 'noindex-seo' ), 'view_url' => get_home_url(), ), @@ -197,11 +199,13 @@ function noindex_seo_admin() { 'page' => array( 'label' => __( 'Page', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s pages.', 'noindex-seo' ), ), 'privacy_policy' => array( 'label' => __( 'Privacy Policy', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of the site\'s privacy policy page.', 'noindex-seo' ), 'view_url' => get_privacy_policy_url(), 'conditional' => version_compare( $GLOBALS['wp_version'], '5.2', '>=' ), @@ -209,11 +213,13 @@ function noindex_seo_admin() { 'single' => array( 'label' => __( 'Single', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of a post on the site.', 'noindex-seo' ), ), 'singular' => array( 'label' => __( 'Singular', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of a post or a page of the site.', 'noindex-seo' ), ), ), @@ -224,11 +230,13 @@ function noindex_seo_admin() { 'category' => array( 'label' => __( 'Category', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site categories. The lists where the posts appear.', 'noindex-seo' ), ), 'tag' => array( 'label' => __( 'Tag', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s tags. The lists where the posts appear.', 'noindex-seo' ), ), ), @@ -239,26 +247,31 @@ function noindex_seo_admin() { 'date' => array( 'label' => __( 'Date', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of any date-based archive page (i.e., monthly, yearly, daily, or time-based archive) of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'day' => array( 'label' => __( 'Day', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of a daily archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'month' => array( 'label' => __( 'Month', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of a monthly archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'time' => array( 'label' => __( 'Time', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of an hourly, "minutely", or "secondly" archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'year' => array( 'label' => __( 'Year', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of a yearly archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), ), @@ -269,16 +282,19 @@ function noindex_seo_admin() { 'archive' => array( 'label' => __( 'Archive', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of any type of Archive page. Category, Tag, Author, and Date-based pages are all types of Archives. The lists where the posts appear.', 'noindex-seo' ), ), 'author' => array( 'label' => __( 'Author', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of the author\'s page, where the author\'s publications appear.', 'noindex-seo' ), ), 'post_type_archive' => array( 'label' => __( 'Post Type Archive', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => false, 'description' => __( 'This will block the indexing of any post type page.', 'noindex-seo' ), ), ), @@ -289,6 +305,7 @@ function noindex_seo_admin() { 'paged' => array( 'label' => __( 'Pagination', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of the pagination, i.e., all pages other than the main page of an archive.', 'noindex-seo' ), ), ), @@ -299,6 +316,7 @@ function noindex_seo_admin() { 'search' => array( 'label' => __( 'Search', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of the internal search result pages.', 'noindex-seo' ), ), ), @@ -309,6 +327,7 @@ function noindex_seo_admin() { 'attachment' => array( 'label' => __( 'Attachment', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing of an attachment document to a post or page. An attachment is an image or other file uploaded through the post editor\'s upload utility. Attachments can be displayed on their own "page" or template. This will not cause the indexing of the image or file to be blocked.', 'noindex-seo' ), ), ), @@ -319,11 +338,13 @@ function noindex_seo_admin() { 'customize_preview' => array( 'label' => __( 'Customize Preview', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing when content is being displayed in customize mode.', 'noindex-seo' ), ), 'preview' => array( 'label' => __( 'Preview', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will block the indexing when a single post is being displayed in draft mode.', 'noindex-seo' ), ), ), @@ -334,6 +355,7 @@ function noindex_seo_admin() { 'error' => array( 'label' => __( 'Error 404', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), + 'suggestion' => true, 'description' => __( 'This will cause an error page to be blocked from being indexed. As it is an error page, it should not be indexed per se, but just in case.', 'noindex-seo' ), ), ), @@ -366,7 +388,7 @@ function noindex_seo_admin() { echo ''; echo '
'; echo ' '; - echo esc_html( $field['recommended'] ) . ': . '; + echo esc_html( $field['recommended'] ) . ': . '; echo '' . esc_html( $field['description'] ) . ''; if ( isset( $field['view_url'] ) ) { From c1a790d56a836ef0d4921767e4c2134912d811a0 Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 11:56:23 +0100 Subject: [PATCH 03/11] PHPCS --- noindex-seo.php | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index dbd2114..7f118f2 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -180,14 +180,14 @@ function noindex_seo_admin() { 'front_page' => array( 'label' => __( 'Front Page', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s front page.', 'noindex-seo' ), 'view_url' => get_site_url(), ), 'home' => array( 'label' => __( 'Home', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s home page.', 'noindex-seo' ), 'view_url' => get_home_url(), ), @@ -199,13 +199,13 @@ function noindex_seo_admin() { 'page' => array( 'label' => __( 'Page', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s pages.', 'noindex-seo' ), ), 'privacy_policy' => array( 'label' => __( 'Privacy Policy', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of the site\'s privacy policy page.', 'noindex-seo' ), 'view_url' => get_privacy_policy_url(), 'conditional' => version_compare( $GLOBALS['wp_version'], '5.2', '>=' ), @@ -213,13 +213,13 @@ function noindex_seo_admin() { 'single' => array( 'label' => __( 'Single', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of a post on the site.', 'noindex-seo' ), ), 'singular' => array( 'label' => __( 'Singular', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of a post or a page of the site.', 'noindex-seo' ), ), ), @@ -230,13 +230,13 @@ function noindex_seo_admin() { 'category' => array( 'label' => __( 'Category', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site categories. The lists where the posts appear.', 'noindex-seo' ), ), 'tag' => array( 'label' => __( 'Tag', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of the site\'s tags. The lists where the posts appear.', 'noindex-seo' ), ), ), @@ -247,31 +247,31 @@ function noindex_seo_admin() { 'date' => array( 'label' => __( 'Date', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of any date-based archive page (i.e., monthly, yearly, daily, or time-based archive) of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'day' => array( 'label' => __( 'Day', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of a daily archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'month' => array( 'label' => __( 'Month', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of a monthly archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'time' => array( 'label' => __( 'Time', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of an hourly, "minutely", or "secondly" archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), 'year' => array( 'label' => __( 'Year', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of a yearly archive of the site. The lists where the posts appear.', 'noindex-seo' ), ), ), @@ -282,19 +282,19 @@ function noindex_seo_admin() { 'archive' => array( 'label' => __( 'Archive', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of any type of Archive page. Category, Tag, Author, and Date-based pages are all types of Archives. The lists where the posts appear.', 'noindex-seo' ), ), 'author' => array( 'label' => __( 'Author', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of the author\'s page, where the author\'s publications appear.', 'noindex-seo' ), ), 'post_type_archive' => array( 'label' => __( 'Post Type Archive', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => false, + 'suggestion' => false, 'description' => __( 'This will block the indexing of any post type page.', 'noindex-seo' ), ), ), @@ -305,7 +305,7 @@ function noindex_seo_admin() { 'paged' => array( 'label' => __( 'Pagination', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of the pagination, i.e., all pages other than the main page of an archive.', 'noindex-seo' ), ), ), @@ -316,7 +316,7 @@ function noindex_seo_admin() { 'search' => array( 'label' => __( 'Search', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of the internal search result pages.', 'noindex-seo' ), ), ), @@ -327,7 +327,7 @@ function noindex_seo_admin() { 'attachment' => array( 'label' => __( 'Attachment', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing of an attachment document to a post or page. An attachment is an image or other file uploaded through the post editor\'s upload utility. Attachments can be displayed on their own "page" or template. This will not cause the indexing of the image or file to be blocked.', 'noindex-seo' ), ), ), @@ -338,13 +338,13 @@ function noindex_seo_admin() { 'customize_preview' => array( 'label' => __( 'Customize Preview', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing when content is being displayed in customize mode.', 'noindex-seo' ), ), 'preview' => array( 'label' => __( 'Preview', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will block the indexing when a single post is being displayed in draft mode.', 'noindex-seo' ), ), ), @@ -355,7 +355,7 @@ function noindex_seo_admin() { 'error' => array( 'label' => __( 'Error 404', 'noindex-seo' ), 'recommended' => __( 'Recommended', 'noindex-seo' ), - 'suggestion' => true, + 'suggestion' => true, 'description' => __( 'This will cause an error page to be blocked from being indexed. As it is an error page, it should not be indexed per se, but just in case.', 'noindex-seo' ), ), ), From d13f402c9f3a4ceeeeecf36f4caaec98b06de547 Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 12:22:51 +0100 Subject: [PATCH 04/11] filters and other seo plugins --- noindex-seo.php | 130 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 123 insertions(+), 7 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index 7f118f2..95d23e2 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -35,16 +35,66 @@ function noindex_seo_metarobots() { /** * Retrieves and sets the SEO 'noindex' values for various WordPress contexts. * + * This function optimizes option retrieval by fetching all relevant settings at once + * using transient caching, reducing the number of database queries. + * * @global WP_Post $post The post global for the current post, if within The Loop. * - * @return void + * @return void. * - * @since 1.0.0 + * @since 1.1.0. */ function noindex_seo_show() { global $post; - $noindex_seo_values = array( + // Intentar obtener las opciones desde el transiente. + $options = get_transient( 'noindex_seo_options' ); + + if ( false === $options ) { + // Transiente no establecido, recuperar opciones de la base de datos. + $options = get_option( 'noindexseo', array() ); + + // Establecer el transiente por 1 hora para almacenar en caché las opciones. + set_transient( 'noindex_seo_options', $options, HOUR_IN_SECONDS ); + } + + /** + * Filtrar los contextos y las claves de opciones correspondientes utilizadas para noindex. + * + * @since 1.0.0. + * + * @param array $contexts Array asociativo de contexto => clave_opción. + */ + $contexts = apply_filters( + 'noindex_seo_contexts', + array( + 'front_page' => 'noindex_seo_front_page', + 'home' => 'noindex_seo_home', + 'page' => 'noindex_seo_page', + 'privacy_policy' => 'noindex_seo_privacy_policy', + 'single' => 'noindex_seo_single', + 'singular' => 'noindex_seo_singular', + 'category' => 'noindex_seo_category', + 'tag' => 'noindex_seo_tag', + 'date' => 'noindex_seo_date', + 'day' => 'noindex_seo_day', + 'month' => 'noindex_seo_month', + 'time' => 'noindex_seo_time', + 'year' => 'noindex_seo_year', + 'archive' => 'noindex_seo_archive', + 'author' => 'noindex_seo_author', + 'post_type_archive' => 'noindex_seo_post_type_archive', + 'paged' => 'noindex_seo_paged', + 'search' => 'noindex_seo_search', + 'attachment' => 'noindex_seo_attachment', + 'customize_preview' => 'noindex_seo_customize_preview', + 'preview' => 'noindex_seo_preview', + 'error' => 'noindex_seo_error', + ) + ); + + // Definir condiciones actuales. + $current_conditions = array( 'front_page' => is_front_page(), 'home' => is_home(), 'page' => is_page(), @@ -69,16 +119,23 @@ function noindex_seo_show() { 'error' => is_404(), ); - foreach ( $noindex_seo_values as $key => $condition ) { - if ( $condition && (bool) get_option( 'noindex_seo_' . $key ) ) { + // Iterar a través de los contextos y aplicar 'noindex' si la condición y la configuración son verdaderas. + foreach ( $contexts as $context => $option_key ) { + if ( + isset( $current_conditions[ $context ] ) && + $current_conditions[ $context ] && + isset( $options[ $option_key ] ) && + (bool) $options[ $option_key ] + ) { noindex_seo_metarobots(); - break; // Prevent multiple meta tags from being added. + break; // Evitar que se agreguen múltiples meta tags. } } - unset( $noindex_seo_values ); + unset( $contexts, $options, $current_conditions ); } + add_action( 'wp_head', 'noindex_seo_show' ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'noindex_seo_settings_link' ); add_action( 'admin_init', 'noindex_seo_register' ); @@ -119,6 +176,10 @@ function noindex_seo_menu() { /** * Registers settings for the 'noindex SEO' plugin. * + * This function registers a variety of settings associated with the 'noindex SEO' functionality in WordPress. + * Each setting determines whether specific pages or post types should be excluded from search engine indexing. + * The settings values are integers, with a default value of 0 (which typically represents 'false' or 'off' in boolean context). + * * @since 1.0.0 * * @return void @@ -162,8 +223,63 @@ function noindex_seo_register() { ) ); } + + // Hook to settings update to clear transient cache. + add_action( 'update_option_noindexseo', 'noindex_seo_clear_transient', 10, 2 ); } +/** + * Clears the transient cache when settings are updated. + * + * @return void + */ +function noindex_seo_clear_transient() { + delete_transient( 'noindex_seo_options' ); +} + +/** + * Detects conflicting SEO plugins and notifies the administrator. + * + * This function checks if any known conflicting SEO plugins are active. + * If a conflict is detected, it displays an admin notice warning the user. + * + * @since 1.1.0. + * + * @return void. + */ +function noindex_seo_detect_conflicts() { + // Incluir el archivo plugin.php si la función no está disponible. + if ( ! function_exists( 'is_plugin_active' ) ) { + include_once ABSPATH . 'wp-admin/includes/plugin.php'; + } + + // Definir un array asociativo de plugins conflictivos: slug/fichero => nombre real del plugin. + $conflicting_plugins = array( + 'wordpress-seo/wp-seo.php' => 'Yoast SEO', + 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO Pack', + // Añadir otros plugins SEO conflictivos según sea necesario. + ); + + // Iterar a través de los plugins conflictivos para verificar si alguno está activo. + foreach ( $conflicting_plugins as $plugin_path => $plugin_name ) { + if ( is_plugin_active( $plugin_path ) ) { + // Añadir una notificación de administrador si se detecta un plugin conflictivo activo. + add_action( + 'admin_notices', + function () use ( $plugin_name ) { + echo '

'; + // translators: plugin name. + printf( esc_html__( 'noindex SEO ha detectado que %s está activo. Esto puede causar conflictos. Por favor, configura las opciones en consecuencia.', 'noindex-seo' ), esc_html( $plugin_name ) ); + echo '

'; + } + ); + break; // Detener la verificación después de encontrar el primer conflicto. + } + } +} + +add_action( 'admin_init', 'noindex_seo_detect_conflicts' ); + /** * Displays the administration settings page for the 'noindex SEO' plugin. * From e83cf2d1f0144a8a9a587003da4df7506ad6f6aa Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 12:28:41 +0100 Subject: [PATCH 05/11] other SEO plugins --- noindex-seo.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index 95d23e2..29d8405 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -255,9 +255,14 @@ function noindex_seo_detect_conflicts() { // Definir un array asociativo de plugins conflictivos: slug/fichero => nombre real del plugin. $conflicting_plugins = array( + 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO', + 'premium-seo-pack/index.php' => 'Premium SEO Pack', + 'seo-by-rank-math/rank-math.php' => 'Rank Math SEO', + 'wp-seopress/seopress.php' => 'SEOPress', + 'slim-seo/slim-seo.php' => 'Slim SEO', + 'squirrly-seo/squirrly.php' => 'Squirrly SEO', + 'autodescription/autodescription.php' => 'The SEO Framework', 'wordpress-seo/wp-seo.php' => 'Yoast SEO', - 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO Pack', - // Añadir otros plugins SEO conflictivos según sea necesario. ); // Iterar a través de los plugins conflictivos para verificar si alguno está activo. From bbbe3b4248267364618d501be1761f059107745e Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 12:31:35 +0100 Subject: [PATCH 06/11] fix some translations and comments --- noindex-seo.php | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index 29d8405..8b8d39f 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -47,23 +47,23 @@ function noindex_seo_metarobots() { function noindex_seo_show() { global $post; - // Intentar obtener las opciones desde el transiente. + // Try to get the options from the transient. $options = get_transient( 'noindex_seo_options' ); if ( false === $options ) { - // Transiente no establecido, recuperar opciones de la base de datos. + // Transient not set, retrieve options from the database. $options = get_option( 'noindexseo', array() ); - // Establecer el transiente por 1 hora para almacenar en caché las opciones. + // Set the transient for 1 hour to cache the options. set_transient( 'noindex_seo_options', $options, HOUR_IN_SECONDS ); } /** - * Filtrar los contextos y las claves de opciones correspondientes utilizadas para noindex. + * Filter the contexts and corresponding option keys used for noindex. * * @since 1.0.0. * - * @param array $contexts Array asociativo de contexto => clave_opción. + * @param array $contexts Associative array of context => option_key. */ $contexts = apply_filters( 'noindex_seo_contexts', @@ -93,7 +93,7 @@ function noindex_seo_show() { ) ); - // Definir condiciones actuales. + // Define current conditions. $current_conditions = array( 'front_page' => is_front_page(), 'home' => is_home(), @@ -119,7 +119,7 @@ function noindex_seo_show() { 'error' => is_404(), ); - // Iterar a través de los contextos y aplicar 'noindex' si la condición y la configuración son verdaderas. + // Iterate through the contexts and apply 'noindex' if the condition and setting are true. foreach ( $contexts as $context => $option_key ) { if ( isset( $current_conditions[ $context ] ) && @@ -128,14 +128,13 @@ function noindex_seo_show() { (bool) $options[ $option_key ] ) { noindex_seo_metarobots(); - break; // Evitar que se agreguen múltiples meta tags. + break; // Prevent multiple meta tags from being added. } } unset( $contexts, $options, $current_conditions ); } - add_action( 'wp_head', 'noindex_seo_show' ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'noindex_seo_settings_link' ); add_action( 'admin_init', 'noindex_seo_register' ); @@ -248,12 +247,12 @@ function noindex_seo_clear_transient() { * @return void. */ function noindex_seo_detect_conflicts() { - // Incluir el archivo plugin.php si la función no está disponible. + // Include the plugin.php file if the function is not available. if ( ! function_exists( 'is_plugin_active' ) ) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } - // Definir un array asociativo de plugins conflictivos: slug/fichero => nombre real del plugin. + // Define an associative array of conflicting plugins: slug/file => real plugin name. $conflicting_plugins = array( 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'All in One SEO', 'premium-seo-pack/index.php' => 'Premium SEO Pack', @@ -265,20 +264,20 @@ function noindex_seo_detect_conflicts() { 'wordpress-seo/wp-seo.php' => 'Yoast SEO', ); - // Iterar a través de los plugins conflictivos para verificar si alguno está activo. + // Iterate through the conflicting plugins to check if any are active. foreach ( $conflicting_plugins as $plugin_path => $plugin_name ) { if ( is_plugin_active( $plugin_path ) ) { - // Añadir una notificación de administrador si se detecta un plugin conflictivo activo. + // Add an admin notice if a conflicting plugin is active. add_action( 'admin_notices', function () use ( $plugin_name ) { echo '

'; // translators: plugin name. - printf( esc_html__( 'noindex SEO ha detectado que %s está activo. Esto puede causar conflictos. Por favor, configura las opciones en consecuencia.', 'noindex-seo' ), esc_html( $plugin_name ) ); + printf( esc_html__( 'noindex SEO has detected that %s is active. This may cause conflicts. Please configure the options accordingly.', 'noindex-seo' ), esc_html( $plugin_name ) ); echo '

'; } ); - break; // Detener la verificación después de encontrar el primer conflicto. + break; // Stop checking after finding the first conflict. } } } @@ -491,7 +490,7 @@ function noindex_seo_admin() { settings_fields( 'noindexseo' ); do_settings_sections( 'noindexseo' ); // In case you have sections added later. ?> -

+

$section ) { echo '

' . esc_html( $section['title'] ) . '

'; From 47446e7d68cbb63ccaf428e065589703211242d3 Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 13:03:23 +0100 Subject: [PATCH 07/11] fix the options / cache --- noindex-seo.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index 8b8d39f..de53cd3 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -47,17 +47,6 @@ function noindex_seo_metarobots() { function noindex_seo_show() { global $post; - // Try to get the options from the transient. - $options = get_transient( 'noindex_seo_options' ); - - if ( false === $options ) { - // Transient not set, retrieve options from the database. - $options = get_option( 'noindexseo', array() ); - - // Set the transient for 1 hour to cache the options. - set_transient( 'noindex_seo_options', $options, HOUR_IN_SECONDS ); - } - /** * Filter the contexts and corresponding option keys used for noindex. * @@ -93,6 +82,21 @@ function noindex_seo_show() { ) ); + // Try to get the options from the transient. + $options = get_transient( 'noindex_seo_options' ); + + if ( false === $options || empty( $options ) ) { + // Transient not set, retrieve options from the database. + $options = array(); + + foreach ( $contexts as $context => $option_key ) { + $options[ $option_key ] = get_option( $option_key, 0 ); + } + + // Set the transient for 1 hour to cache the options. + set_transient( 'noindex_seo_options', $options, HOUR_IN_SECONDS ); + } + // Define current conditions. $current_conditions = array( 'front_page' => is_front_page(), From 4cbb598053ac5f3a73e85092f442d5c5dccb9bcb Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 14:09:55 +0100 Subject: [PATCH 08/11] fixes --- noindex-seo.php | 76 ++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/noindex-seo.php b/noindex-seo.php index de53cd3..679fb46 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -21,17 +21,29 @@ /** * Outputs a 'noindex' meta robots tag to the page. * - * This function prints the 'noindex' meta robots tag, instructing search engines - * not to index the content of the current page. + * This function ensures the 'noindex' directive is set in the robots meta tag, + * instructing search engines not to index the content of the current page. * * @since 1.0.0 * * @return void */ function noindex_seo_metarobots() { - echo '' . "\n"; + + if ( function_exists( 'wp_robots' ) ) { + add_filter( + 'wp_robots', + function ( $robots ) { + $robots['noindex'] = true; + return $robots; + } + ); + } else { + echo '' . "\n"; + } } + /** * Retrieves and sets the SEO 'noindex' values for various WordPress contexts. * @@ -40,13 +52,11 @@ function noindex_seo_metarobots() { * * @global WP_Post $post The post global for the current post, if within The Loop. * - * @return void. + * @return void * - * @since 1.1.0. + * @since 1.1.0 */ function noindex_seo_show() { - global $post; - /** * Filter the contexts and corresponding option keys used for noindex. * @@ -57,28 +67,28 @@ function noindex_seo_show() { $contexts = apply_filters( 'noindex_seo_contexts', array( - 'front_page' => 'noindex_seo_front_page', - 'home' => 'noindex_seo_home', + 'single' => 'noindex_seo_single', 'page' => 'noindex_seo_page', 'privacy_policy' => 'noindex_seo_privacy_policy', - 'single' => 'noindex_seo_single', - 'singular' => 'noindex_seo_singular', + 'attachment' => 'noindex_seo_attachment', 'category' => 'noindex_seo_category', 'tag' => 'noindex_seo_tag', + 'author' => 'noindex_seo_author', + 'post_type_archive' => 'noindex_seo_post_type_archive', 'date' => 'noindex_seo_date', 'day' => 'noindex_seo_day', 'month' => 'noindex_seo_month', - 'time' => 'noindex_seo_time', 'year' => 'noindex_seo_year', 'archive' => 'noindex_seo_archive', - 'author' => 'noindex_seo_author', - 'post_type_archive' => 'noindex_seo_post_type_archive', - 'paged' => 'noindex_seo_paged', 'search' => 'noindex_seo_search', - 'attachment' => 'noindex_seo_attachment', - 'customize_preview' => 'noindex_seo_customize_preview', - 'preview' => 'noindex_seo_preview', 'error' => 'noindex_seo_error', + 'front_page' => 'noindex_seo_front_page', + 'home' => 'noindex_seo_home', + 'singular' => 'noindex_seo_singular', + 'paged' => 'noindex_seo_paged', + 'preview' => 'noindex_seo_preview', + 'customize_preview' => 'noindex_seo_customize_preview', + 'time' => 'noindex_seo_time', ) ); @@ -97,30 +107,30 @@ function noindex_seo_show() { set_transient( 'noindex_seo_options', $options, HOUR_IN_SECONDS ); } - // Define current conditions. + // Define current conditions, ordered from most specific to most general. $current_conditions = array( - 'front_page' => is_front_page(), - 'home' => is_home(), + 'single' => is_single(), 'page' => is_page(), + 'attachment' => is_attachment(), 'privacy_policy' => function_exists( 'is_privacy_policy' ) ? is_privacy_policy() : false, - 'single' => is_single(), - 'singular' => is_singular(), 'category' => is_category(), 'tag' => is_tag(), - 'date' => is_date(), + 'author' => is_author(), + 'post_type_archive' => is_post_type_archive(), 'day' => is_day(), 'month' => is_month(), - 'time' => is_time(), 'year' => is_year(), - 'archive' => is_archive(), - 'author' => is_author(), - 'post_type_archive' => is_post_type_archive(), - 'paged' => is_paged(), + 'time' => is_time(), + 'date' => is_date() && ! ( is_day() || is_month() || is_year() || is_time() ), + 'archive' => is_archive() && ! ( is_category() || is_tag() || is_author() || is_post_type_archive() || is_date() ), 'search' => is_search(), - 'attachment' => is_attachment(), - 'customize_preview' => is_customize_preview(), - 'preview' => is_preview(), 'error' => is_404(), + 'front_page' => is_front_page() && ! is_paged() && ! is_home(), + 'home' => is_home() && ! is_paged(), + 'singular' => is_singular() && ! ( is_single() || is_page() || is_attachment() ), + 'paged' => is_paged() && ! is_front_page() && ! is_home(), + 'preview' => is_preview(), + 'customize_preview' => is_customize_preview(), ); // Iterate through the contexts and apply 'noindex' if the condition and setting are true. @@ -139,7 +149,7 @@ function noindex_seo_show() { unset( $contexts, $options, $current_conditions ); } -add_action( 'wp_head', 'noindex_seo_show' ); +add_action( 'template_redirect', 'noindex_seo_show' ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'noindex_seo_settings_link' ); add_action( 'admin_init', 'noindex_seo_register' ); add_action( 'admin_menu', 'noindex_seo_menu' ); From 889c47358cc27d4c0fa40a0cd054f68b57ce5eaf Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 14:12:26 +0100 Subject: [PATCH 09/11] version fix --- noindex-seo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noindex-seo.php b/noindex-seo.php index 679fb46..49a332f 100644 --- a/noindex-seo.php +++ b/noindex-seo.php @@ -24,7 +24,7 @@ * This function ensures the 'noindex' directive is set in the robots meta tag, * instructing search engines not to index the content of the current page. * - * @since 1.0.0 + * @since 1.1.0 * * @return void */ From 70e16a8c5de0828a5b39e905c8e66fdb47f4c81c Mon Sep 17 00:00:00 2001 From: Javier Casares Date: Sat, 2 Nov 2024 14:14:58 +0100 Subject: [PATCH 10/11] unistaller... --- uninstall.php | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/uninstall.php b/uninstall.php index c0194a6..bd2659d 100644 --- a/uninstall.php +++ b/uninstall.php @@ -1,35 +1,31 @@ Date: Sat, 2 Nov 2024 14:16:06 +0100 Subject: [PATCH 11/11] changelog --- changelog.txt | 6 ++++++ readme.txt | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/changelog.txt b/changelog.txt index 4f1f992..f63f3bf 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,8 +2,14 @@ = 1.1.0 [2024-11-02] = +**Added** + +* Detects other WordPress SEO plugins, and creates a notice about it, to avoid conflicts. +* Has filters, so other plugins can hack. + **Changed** +* Uses native wp_robots functions (since WP 5.7+) * Big refactory. * Less size, improved code quality. diff --git a/readme.txt b/readme.txt index 5fe8d61..17e7b6b 100644 --- a/readme.txt +++ b/readme.txt @@ -88,8 +88,14 @@ Extract the contents of the ZIP and upload the contents to the `/wp-content/plug = 1.1.0 [2024-11-02] = +**Added** + +* Detects other WordPress SEO plugins, and creates a notice about it, to avoid conflicts. +* Has filters, so other plugins can hack. + **Changed** +* Uses native wp_robots functions (since WP 5.7+) * Big refactory. * Less size, improved code quality.