diff --git a/inc/admin/settings.php b/inc/admin/settings.php new file mode 100644 index 0000000..0241d2b --- /dev/null +++ b/inc/admin/settings.php @@ -0,0 +1,360 @@ + get_post_types(), + 'post_status' => 'publish', + 'numberposts' => -1, + ]); + foreach($posts as $post){ + $options_posts[$post->ID] = $post->post_title; + } + + // Register a new field in the "yoast_sitemaps_exclude_section_specific_posts" section, inside the "yoast_sitemaps_exclude_specific_posts" page + add_settings_field( + 'yoast_sitemaps_exclude_specific_posts_field', + __( 'Posts', 'yoast_sitemaps_exclude' ), + array($this, 'fields_callback'), + 'yoast_sitemaps_exclude_specific_posts', + 'yoast_sitemaps_exclude_section_specific_posts', + array( + 'options_name' => 'yoast_sitemaps_exclude_specific_posts_options', + 'field_name' => 'yoast_sitemaps_exclude_specific_posts_field', + 'options' => $options_posts, + 'class' => 'js-yoast-sitemaps-exclude' + ) + ); + + // Register a new setting for "yoast_sitemaps_exclude_post_type" page. + register_setting( 'yoast_sitemaps_exclude_post_type', 'yoast_sitemaps_exclude_post_type_options' ); + + + $options_post_types = array(); + + $post_types = get_post_types(); + foreach($post_types as $key => $post_type){ + $options_post_types[$key] = $key; + } + + // Register a new section in the "yoast_sitemaps_exclude_post_type" page. + add_settings_section( + 'yoast_sitemaps_exclude_section_post_type', + __( 'Exclude a post type.', 'yoast_sitemaps_exclude' ), + null, + 'yoast_sitemaps_exclude_post_type' + ); + + // Register a new field in the "yoast_sitemaps_exclude_section_specific_posts" section, inside the "yoast_sitemaps_exclude_specific_posts" page + add_settings_field( + 'yoast_sitemaps_exclude_post_type_field', + __( 'Post type', 'yoast_sitemaps_exclude' ), + array($this, 'fields_callback'), + 'yoast_sitemaps_exclude_post_type', + 'yoast_sitemaps_exclude_section_post_type', + array( + 'options_name' => 'yoast_sitemaps_exclude_post_type_options', + 'field_name' => 'yoast_sitemaps_exclude_post_type_field', + 'options' => $options_post_types, + 'class' => 'js-yoast-sitemaps-exclude' + ) + ); + + // Register a new setting for "yoast_sitemaps_exclude_taxonomy" page. + register_setting( 'yoast_sitemaps_exclude_taxonomy', 'yoast_sitemaps_exclude_taxonomy_options' ); + + $options_taxonomies = array(); + $taxonomies = get_taxonomies(); + foreach($taxonomies as $taxonomy){ + $options_taxonomies[$taxonomy] = $taxonomy; + } + + // Register a new section in the "yoast_sitemaps_exclude_taxonomy" page. + add_settings_section( + 'yoast_sitemaps_exclude_section_taxonomy', + __( 'Exclude a taxonomy.', 'yoast_sitemaps_exclude' ), + null, + 'yoast_sitemaps_exclude_taxonomy' + ); + + // Register a new field in the "yoast_sitemaps_exclude_section_specific_posts" section, inside the "yoast_sitemaps_exclude_specific_posts" page + add_settings_field( + 'yoast_sitemaps_exclude_taxonomy_field', + __( 'Taxonomy', 'yoast_sitemaps_exclude' ), + array($this, 'fields_callback'), + 'yoast_sitemaps_exclude_taxonomy', + 'yoast_sitemaps_exclude_section_taxonomy', + array( + 'options_name' => 'yoast_sitemaps_exclude_taxonomy_options', + 'field_name' => 'yoast_sitemaps_exclude_taxonomy_field', + 'options' => $options_taxonomies, + 'class' => 'js-yoast-sitemaps-exclude' + ) + ); + + // Register a new setting for "yoast_sitemaps_exclude_author" page. + register_setting( 'yoast_sitemaps_exclude_author', 'yoast_sitemaps_exclude_author_options' ); + + $options_authors = array(); + + $authors = get_users(); + foreach($authors as $author){ + $options_authors[$author->ID] = $author->display_name . '(' . $author->ID . ')'; + } + + // Register a new section in the "yoast_sitemaps_exclude" page. + add_settings_section( + 'yoast_sitemaps_exclude_section_author', + __( 'Exclude an author.', 'yoast_sitemaps_exclude' ), + null, + 'yoast_sitemaps_exclude_author' + ); + + // Register a new field in the "yoast_sitemaps_exclude_section_specific_posts" section, inside the "yoast_sitemaps_exclude_specific_posts" page + add_settings_field( + 'yoast_sitemaps_exclude_author_field', + __( 'Author', 'yoast_sitemaps_exclude' ), + array($this, 'fields_callback'), + 'yoast_sitemaps_exclude_author', + 'yoast_sitemaps_exclude_section_author', + array( + 'options_name' => 'yoast_sitemaps_exclude_author_options', + 'field_name' => 'yoast_sitemaps_exclude_author_field', + 'options' => $options_authors, + 'class' => 'js-yoast-sitemaps-exclude' + ) + ); + + // Register a new setting for "yoast_sitemaps_exclude_taxonomy_term" page. + register_setting( 'yoast_sitemaps_exclude_taxonomy_term', 'yoast_sitemaps_exclude_taxonomy_term_options' ); + + $options_terms = array(); + + $terms = get_terms(); + foreach($terms as $term){ + $options_terms[$term->term_id] = $term->name . '(' . $term->term_id . ')'; + } + + // Register a new section in the "yoast_sitemaps_exclude_taxonomy_term" page. + add_settings_section( + 'yoast_sitemaps_exclude_section_taxonomy_term', + __( 'Exclude a taxonomy term.', 'yoast_sitemaps_exclude' ), + null, + 'yoast_sitemaps_exclude_taxonomy_term' + ); + + // Register a new field in the "yoast_sitemaps_exclude_section_specific_posts" section, inside the "yoast_sitemaps_exclude_specific_posts" page + add_settings_field( + 'yoast_sitemaps_exclude_taxonomy_term_field', + __( 'Taxonomy term', 'yoast_sitemaps_exclude' ), + array($this, 'fields_callback'), + 'yoast_sitemaps_exclude_taxonomy_term', + 'yoast_sitemaps_exclude_section_taxonomy_term', + array( + 'options_name' => 'yoast_sitemaps_exclude_taxonomy_term_options', + 'field_name' => 'yoast_sitemaps_exclude_taxonomy_term_field', + 'options' => $options_terms, + 'class' => 'js-yoast-sitemaps-exclude' + ) + ); + + + } + + /** + * Pill field callbakc function. + * + * @param array $args + */ + public function fields_callback( $args ) { + // Get the value of the setting we've registered with register_setting() + $options_name = $args['options_name']; + $options = get_option( $options_name ); + $field_name = $args['field_name']; + $field_value = !empty($options[$field_name]) ? $options[$field_name] : array(); + ?> + + +

+ +

+ +
+

+ + +
+
+ getTabContent($tab);?> + +
+
+
+ 'yoast_sitemaps_exclude_author_options', + 'field_name' => 'yoast_sitemaps_exclude_author_field' + ); + + /** + * Post type option. + * + * @var array + */ + private $option_post_type = array( + 'option_name' => 'yoast_sitemaps_exclude_post_type_options', + 'field_name' => 'yoast_sitemaps_exclude_post_type_field' + ); + + /** + * Specific posts option. + * + * @var array + */ + private $option_specific_posts = array( + 'option_name' => 'yoast_sitemaps_exclude_specific_posts_options', + 'field_name' => 'yoast_sitemaps_exclude_specific_posts_field' + ); + + /** + * Taxonomy option. + * + * @var array + */ + private $option_taxonomy = array( + 'option_name' => 'yoast_sitemaps_exclude_taxonomy_options', + 'field_name' => 'yoast_sitemaps_exclude_taxonomy_field' + ); + /** + * List of options. + * + * @var array + */ + private $option_taxonomy_term = array( + 'option_name' => 'yoast_sitemaps_exclude_taxonomy_term_options', + 'field_name' => 'yoast_sitemaps_exclude_taxonomy_term_field' + ); + + public function __construct(){ + if(!empty($this->get_options($this->option_author))){ + add_filter( 'wpseo_sitemap_exclude_author', array( $this, 'sitemap_exclude_authors' )); + } + if(!empty($this->get_options($this->option_post_type))){ + add_filter( 'wpseo_sitemap_exclude_post_type', array( $this, 'sitemap_exclude_post_type'), 10, 2 ); + } + if(!empty($this->get_options($this->option_specific_posts))){ + add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'exclude_posts_from_xml_sitemaps') ); + } + if(!empty($this->get_options($this->option_taxonomy))){ + add_filter( 'wpseo_sitemap_exclude_taxonomy', array( $this, 'sitemap_exclude_taxonomy'), 10, 2 ); + } + if(!empty($this->get_options($this->option_taxonomy_term))){ + add_filter( 'wpseo_exclude_from_sitemap_by_term_ids', array( $this, 'sitemap_exclude_terms') ); + } + } + + /** + * Retrieves an option value based on an option name. + * + * @param array $option Array. + * + * @return array. + */ + public function get_options($option){ + $value = get_option( $option['option_name'] ); + if(!empty($value) && !empty($value[$option['field_name']])){ + return $value[$option['field_name']]; + } + return false; + } + + /** + * Excludes author with ID from author sitemaps. + * + * @param array $users Array of User objects to filter through. + * + * @return array The remaining authors. + */ + public function sitemap_exclude_authors( $users ) { + $users_values = $this->get_options($this->option_author); + return array_filter( $users, function( $user ) use ($users_values) { + if ( in_array($user->ID, $users_values) ) { + return false; + } + return true; + } ); + } + + /** + * Exclude a post type from XML sitemaps. + * + * @param boolean $excluded Whether the post type is excluded by default. + * @param string $post_type The post type to exclude. + * + * @return bool Whether or not a given post type should be excluded. + */ + public function sitemap_exclude_post_type( $excluded, $post_type ) { + $post_type_values = $this->get_options($this->option_post_type); + return in_array($post_type, $post_type_values); + } + + /** + * Excludes posts from XML sitemaps. + * + * @return array The IDs of posts to exclude. + */ + public function exclude_posts_from_xml_sitemaps() { + return $this->get_options($this->option_specific_posts); + } + + /** + * Exclude a taxonomy from XML sitemaps. + * + * @param boolean $excluded Whether the taxonomy is excluded by default. + * @param string $taxonomy The taxonomy to exclude. + * + * @return bool Whether or not a given taxonomy should be excluded. + */ + public function sitemap_exclude_taxonomy( $excluded, $taxonomy ) { + return $taxonomy === 'ingredients'; + } + + /** + * Excludes terms with ID from terms sitemaps. + * + * @param array $terms Array of term IDs already excluded. + * + * @return array The terms to exclude. + */ + function sitemap_exclude_terms( $terms ) { + return $this->get_options($this->option_taxonomy_term); + } + } + new XmlSitemapsExcludesFrontend(); +} + diff --git a/xml-sitemaps-excludes-by-codechute.php b/xml-sitemaps-excludes-by-codechute.php new file mode 100644 index 0000000..4bc1ddd --- /dev/null +++ b/xml-sitemaps-excludes-by-codechute.php @@ -0,0 +1,78 @@ +setup(); + } + public function setup(){ + require_once YSE_PLUGIN_PATH . 'inc/admin/settings.php'; + require_once YSE_PLUGIN_PATH . 'inc/xml-sitemaps-excludes-frontend.php'; + + } + + /** + * Get the instane of VC_Manager + * + * @return self + */ + public static function getInstance() { + if ( ! ( self::$instance instanceof self ) ) { + self::$instance = new self(); + } + return self::$instance; + } + + /** + * Checks if Yoast SEO activated. + * + * @return bool + */ + public function check_requirements() { + if(!is_plugin_active('wordpress-seo/wp-seo.php')){ + add_action( 'admin_notices', array($this, 'admin_notice__error') ); + return; + } + } + + public function admin_notice__error(){ + $class = 'notice notice-error'; + $message = __( 'The following required plugin is currently inactive: Yoast SEO.', 'yoast-sitemaps-exclude' ); + printf( '

%2$s

', esc_attr( $class ), esc_html( $message ) ); + } + } +} + +global $xml_sitemaps_excludes_by_codechute; +if ( ! $xml_sitemaps_excludes_by_codechute ) { + $xml_sitemaps_excludes_by_codechute = XmlSitemapsExcludesByCodeChute::getInstance(); +}