post_password !== ''; } /** * Whether the robots tag on this post's page says noindex. * * Site-wide directives, then the post type's own (when its robots switch * is on), then the post's override. A post-level `noindex: false` beats a * noindexed type, exactly as it does in the tag. * * @since 2.15.0 * * @param \WP_Post $post Post. * @return bool */ public static function is_post_noindexed(\WP_Post $post): bool { $settings = self::merge_entity_robots(self::global_robots(), (string) $post->post_type); $settings = self::merge_override( $settings, (bool) get_post_meta($post->ID, '_thinkrank_robots_meta_enabled', true), get_post_meta($post->ID, '_thinkrank_robots_meta', true) ); return !empty($settings['noindex']); } /** * Whether the robots tag on this term's archive says noindex. * * Site-wide directives, then the taxonomy's matrix row, then the term's * override. * * @since 2.15.0 * * @param \WP_Term $term Term. * @return bool */ public static function is_term_noindexed(\WP_Term $term): bool { $settings = self::merge_entity_robots( self::global_robots(), Content_Type_Settings::PREFIX_TAXONOMY . $term->taxonomy ); $settings = self::merge_override( $settings, (bool) get_term_meta($term->term_id, '_thinkrank_robots_meta_enabled', true), get_term_meta($term->term_id, '_thinkrank_robots_meta', true) ); return !empty($settings['noindex']); } /** * Whether a request for this post is redirected elsewhere. * * @since 2.15.0 * * @param \WP_Post $post Post. * @return bool */ public static function is_post_redirected(\WP_Post $post): bool { if (Object_Redirect::get('post', (int) $post->ID)['url'] !== '') { return true; } $url = get_permalink($post); return is_string($url) && $url !== '' && self::url_is_redirected($url, $post); } /** * Whether a request for this term's archive is redirected elsewhere. * * @since 2.15.0 * * @param \WP_Term $term Term. * @return bool */ public static function is_term_redirected(\WP_Term $term): bool { if (Object_Redirect::get('term', (int) $term->term_id)['url'] !== '') { return true; } $url = get_term_link($term); return is_string($url) && $url !== '' && self::url_is_redirected($url, $term); } /** * Ask the redirect provider whether a URL is redirected. * * @param string $url The object's URL. * @param \WP_Post|\WP_Term $item The object it belongs to. * @return bool */ private static function url_is_redirected(string $url, $item): bool { /** * Filter whether a URL is redirected by a rule that is not tied to an object. * * Per-object redirects are already read through `thinkrank_object_redirect`. * This covers the rest: a rule whose source path happens to be a post's * or term's URL. Nothing answers it in the free plugin; a redirect * provider such as ThinkRank Pro does, from its own rule store. * * @since 2.15.0 * * @param bool $redirected Whether the URL redirects. Default false. * @param string $url The post's permalink or the term's archive link. * @param \WP_Post|\WP_Term $item The object the URL belongs to. */ return (bool) apply_filters('thinkrank_url_redirects', false, $url, $item); } /** * Site-wide robots directives with the same defaults the robots tag uses. * * @return array */ private static function global_robots(): array { $stored = get_option(self::GLOBAL_ROBOTS_OPTION, []); return array_merge( ['index' => true, 'noindex' => false], is_array($stored) ? $stored : [] ); } /** * Merge a matrix row's robots directives when its robots switch is on. * * @param array $settings Directives so far. * @param string $entity_key Content_Type_Settings entity key. * @return array */ private static function merge_entity_robots(array $settings, string $entity_key): array { $entity = Content_Type_Settings::get_entity_settings($entity_key); if (!empty($entity['robots_meta_enabled']) && is_array($entity['robots_meta'] ?? null)) { $settings = array_merge($settings, $entity['robots_meta']); } return $settings; } /** * Merge an object's stored robots override when it is switched on. * * @param array $settings Directives so far. * @param bool $enabled The object's `_thinkrank_robots_meta_enabled`. * @param mixed $raw The object's `_thinkrank_robots_meta` JSON. * @return array */ private static function merge_override(array $settings, bool $enabled, $raw): array { if (!$enabled || !is_string($raw) || $raw === '') { return $settings; } $override = json_decode($raw, true); if (!is_array($override)) { return $settings; } return array_merge($settings, array_intersect_key($override, array_flip(self::OVERRIDE_KEYS))); } }