'1' ) or just '1'. $sitemap = (array) get_option( 'seopress_xml_sitemap_option_name', array() ); $tick = $sitemap['seopress_xml_sitemap_taxonomies_list'][ $taxonomy ] ?? ''; return '1' === (string) ( is_array( $tick ) ? ( $tick['include'] ?? '' ) : $tick ); } return true; } /** * Which of this taxonomy's terms with no published posts the active SEO plugin's sitemap lists: * 'none' (as WordPress's own sitemap), 'all', 'parents' (those with a direct child that has posts), * or 'ancestors' (those with posts anywhere below them: WordPress's default for a terms query, which * Yoast and SEOPress keep; WordPress's own sitemap and Rank Math turn it off). * * @param string $taxonomy Taxonomy name. */ public static function empty_terms( $taxonomy ) { switch ( self::active() ) { case self::YOAST: $hide = apply_filters( 'wpseo_sitemap_exclude_empty_terms', true, array( $taxonomy ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Yoast's own filter. return apply_filters( 'wpseo_sitemap_exclude_empty_terms_taxonomy', $hide, $taxonomy ) ? 'ancestors' : 'all'; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Yoast's own filter. case self::RANK_MATH: $sitemap = (array) get_option( 'rank-math-options-sitemap', array() ); return self::on( $sitemap[ "tax_{$taxonomy}_include_empty" ] ?? 'off' ) ? 'all' : 'none'; case self::AIOSEO: return 'parents'; case self::SEOPRESS: return 'ancestors'; } return 'none'; } /** * Category and tag pages of these taxonomies that the active SEO plugin leaves out of its sitemap. * * @param string[] $taxonomies Taxonomies the sitemap lists. * @return int[] Term IDs. */ public static function excluded_term_ids( array $taxonomies ) { $ids = array(); switch ( self::active() ) { case self::YOAST: // wpseo_taxonomy_meta[taxonomy][term_id]: 'wpseo_noindex' is 'noindex', 'index' or 'default' (a // taxonomy that is noindex isn't listed at all), and 'wpseo_canonical'. $meta = (array) get_option( 'wpseo_taxonomy_meta', array() ); foreach ( $taxonomies as $taxonomy ) { foreach ( (array) ( $meta[ $taxonomy ] ?? array() ) as $term_id => $values ) { $canonical = $values['wpseo_canonical'] ?? ''; if ( 'noindex' === ( $values['wpseo_noindex'] ?? '' ) || ( is_string( $canonical ) && '' !== $canonical && get_term_link( (int) $term_id, $taxonomy ) !== $canonical ) ) { $ids[] = (int) $term_id; } } } $ids = array_merge( $ids, (array) apply_filters( 'wpseo_exclude_from_sitemap_by_term_ids', array() ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Yoast's own filter. break; case self::RANK_MATH: $ids = self::rank_math_excluded_terms( $taxonomies ); break; case self::AIOSEO: // Its exclude list: plain IDs, or JSON strings such as {"value": 12, "label": "…"} (an ID // given as a string there is skipped, as AIOSEO skips it). foreach ( (array) ( self::aioseo_option( 'aioseo_options' )['sitemap']['general']['advancedSettings']['excludeTerms'] ?? array() ) as $entry ) { if ( is_numeric( $entry ) ) { $ids[] = (int) $entry; } elseif ( is_string( $entry ) ) { $decoded = json_decode( $entry, true ); if ( isset( $decoded['value'] ) && is_int( $decoded['value'] ) ) { $ids[] = $decoded['value']; } } } break; case self::SEOPRESS: // Any value in '_seopress_robots_index' means noindex for a term. Its type-level noindex isn't read. $ids = array_merge( self::term_meta_set( '_seopress_robots_index' ), self::term_canonical_elsewhere( '_seopress_robots_canonical' ) ); break; } /** * Filters the category and tag pages the multilingual sitemap leaves out. * * @param int[] $ids Term IDs. * @param string[] $taxonomies Taxonomies the sitemap lists. */ $ids = (array) apply_filters( 'langferry_sitemap_excluded_term_ids', $ids, $taxonomies ); return array_values( array_unique( array_filter( array_map( 'intval', $ids ) ) ) ); } /** * Rank Math: its exclude list; posts whose robots meta says noindex; in a type whose default is * noindex, every post not set to index; posts whose canonical points elsewhere. */ private static function rank_math_excluded( array $types ) { global $wpdb; $sitemap = (array) get_option( 'rank-math-options-sitemap', array() ); $titles = (array) get_option( 'rank-math-options-titles', array() ); $ids = wp_parse_id_list( $sitemap['exclude_posts'] ?? '' ); // The meta is a serialized array of strings such as ["noindex"]; Rank Math's own query matches it with LIKE. $ids = array_merge( $ids, array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'rank_math_robots' AND meta_value LIKE %s", '%' . $wpdb->esc_like( 'noindex' ) . '%' ) ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $types as $type ) { $robots = self::on( $titles[ "pt_{$type}_custom_robots" ] ?? 'off' ) ? ( $titles[ "pt_{$type}_robots" ] ?? array() ) : ( $titles['robots_global'] ?? array() ); if ( in_array( 'noindex', (array) $robots, true ) ) { $ids = array_merge( $ids, self::posts_without_meta( $type, 'rank_math_robots', '%' . $wpdb->esc_like( '"index"' ) . '%' ) ); } } return array_merge( $ids, self::canonical_elsewhere( 'rank_math_canonical_url' ) ); } /** * Rank Math: its exclude list; category pages whose robots meta says noindex; in a taxonomy whose * default is noindex, every one not set to index; those whose canonical points elsewhere. */ private static function rank_math_excluded_terms( array $taxonomies ) { global $wpdb; $sitemap = (array) get_option( 'rank-math-options-sitemap', array() ); $titles = (array) get_option( 'rank-math-options-titles', array() ); $ids = wp_parse_id_list( $sitemap['exclude_terms'] ?? '' ); $ids = array_merge( $ids, array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM {$wpdb->termmeta} WHERE meta_key = 'rank_math_robots' AND meta_value LIKE %s", '%' . $wpdb->esc_like( 'noindex' ) . '%' ) ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( $taxonomies as $taxonomy ) { $robots = self::on( $titles[ "tax_{$taxonomy}_custom_robots" ] ?? 'off' ) ? ( $titles[ "tax_{$taxonomy}_robots" ] ?? array() ) : ( $titles['robots_global'] ?? array() ); if ( in_array( 'noindex', (array) $robots, true ) ) { $ids = array_merge( $ids, self::terms_without_meta( $taxonomy, 'rank_math_robots', '%' . $wpdb->esc_like( '"index"' ) . '%' ) ); } } return array_merge( $ids, self::term_canonical_elsewhere( 'rank_math_canonical_url' ) ); } /** * All in One SEO: whether a post type or taxonomy is hidden from search: not shown, its own robots * setting is noindex, or it follows a global noindex. One with no setting stored has AIOSEO's * defaults (shown, following the global setting). * * @param string $group 'postTypes' or 'taxonomies'. * @param string $name Post type or taxonomy name. */ private static function aioseo_hidden( $group, $name ) { $setting = (array) ( self::aioseo_option( 'aioseo_options_dynamic' )['searchAppearance'][ $group ][ $name ] ?? array() ); $robots = (array) ( $setting['advanced']['robotsMeta'] ?? array() ); $global = (array) ( self::aioseo_option( 'aioseo_options' )['searchAppearance']['advanced']['globalRobotsMeta'] ?? array() ); if ( ! ( $setting['show'] ?? true ) ) { return true; } if ( ! ( $robots['default'] ?? true ) ) { return ! empty( $robots['noindex'] ); } return ! ( $global['default'] ?? true ) && ! empty( $global['noindex'] ); } /** * All in One SEO: posts with their own noindex (robots_default = 0, robots_noindex = 1); in a type * hidden from search, every post not set to index (robots_default = 0, robots_noindex = 0); its * exclude list. Its sitemap doesn't look at canonicals. */ private static function aioseo_excluded( array $types ) { global $wpdb; $options = self::aioseo_option( 'aioseo_options' ); $table = $wpdb->prefix . 'aioseo_posts'; $suppress = $wpdb->suppress_errors( true ); // No table: AIOSEO has never saved a post's settings here. $noindex = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( 'SELECT post_id FROM %i WHERE robots_default = 0 AND robots_noindex = 1', $table ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $indexed = array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( 'SELECT post_id FROM %i WHERE robots_default = 0 AND robots_noindex = 0', $table ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->suppress_errors( $suppress ); $ids = $noindex; foreach ( $types as $type ) { if ( self::aioseo_hidden( 'postTypes', $type ) ) { $ids = array_merge( $ids, array_diff( self::published_ids( $type ), $indexed ) ); } } // Plain IDs, or JSON strings such as {"value": 12, "label": "…"}. foreach ( (array) ( $options['sitemap']['general']['advancedSettings']['excludePosts'] ?? array() ) as $entry ) { if ( is_numeric( $entry ) ) { $ids[] = (int) $entry; } elseif ( is_string( $entry ) ) { $decoded = json_decode( $entry, true ); if ( isset( $decoded['value'] ) && is_numeric( $decoded['value'] ) ) { $ids[] = (int) $decoded['value']; } } } return $ids; } /** Rank Math stores switches as 'on' or 'off'. */ private static function on( $value ) { return true === $value || 'on' === $value; } /** AIOSEO keeps its settings as a JSON string. */ private static function aioseo_option( $name ) { $value = get_option( $name, '' ); $value = is_string( $value ) ? json_decode( $value, true ) : $value; return is_array( $value ) ? $value : array(); } /** Posts with this meta value. */ private static function meta_ids( $key, $value ) { global $wpdb; return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s", $key, $value ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** Published posts of a type. */ private static function published_ids( $type ) { global $wpdb; return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'publish'", $type ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** Published posts of a type whose meta doesn't match the LIKE pattern (or that have none). */ private static function posts_without_meta( $type, $key, $like ) { global $wpdb; return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p WHERE p.post_type = %s AND p.post_status = 'publish' AND NOT EXISTS ( SELECT 1 FROM {$wpdb->postmeta} m WHERE m.post_id = p.ID AND m.meta_key = %s AND m.meta_value LIKE %s )", $type, $key, $like ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** Posts whose canonical meta is set and isn't their own address: they aren't the page to index. */ private static function canonical_elsewhere( $key ) { global $wpdb; $ids = array(); foreach ( $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value <> ''", $key ) ) as $row ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( get_permalink( (int) $row->post_id ) !== $row->meta_value ) { $ids[] = (int) $row->post_id; } } return $ids; } /** Terms with a value (anything but '' or '0') for this meta key. */ private static function term_meta_set( $key ) { global $wpdb; return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM {$wpdb->termmeta} WHERE meta_key = %s AND meta_value NOT IN ( '', '0' )", $key ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** Terms of a taxonomy whose meta doesn't match the LIKE pattern (or that have none). */ private static function terms_without_meta( $taxonomy, $key, $like ) { global $wpdb; return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM {$wpdb->term_taxonomy} tt WHERE tt.taxonomy = %s AND NOT EXISTS ( SELECT 1 FROM {$wpdb->termmeta} m WHERE m.term_id = tt.term_id AND m.meta_key = %s AND m.meta_value LIKE %s )", $taxonomy, $key, $like ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } /** Terms whose canonical meta is set and isn't their own address. */ private static function term_canonical_elsewhere( $key ) { global $wpdb; $ids = array(); foreach ( $wpdb->get_results( $wpdb->prepare( "SELECT term_id, meta_value FROM {$wpdb->termmeta} WHERE meta_key = %s AND meta_value <> ''", $key ) ) as $row ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery if ( get_term_link( (int) $row->term_id ) !== $row->meta_value ) { $ids[] = (int) $row->term_id; } } return $ids; } }