' . "\n", esc_attr( $description ) ); } /** * Replaces the canonical URL core prints for a post. * * @param string $canonical_url The canonical URL core computed. * @param \WP_Post $post The post. * @return string */ public static function filter_canonical( $canonical_url, $post ) { if ( ! $post instanceof \WP_Post || ! self::active() ) { return $canonical_url; } $stored = self::field( $post->ID, 'canonical' ); return null === $stored ? $canonical_url : $stored; } /** * Adds noindex to the robots directives of a single post or page. * * @param array $robots Directives core will print. * @return array */ public static function filter_robots( $robots ) { $post_id = self::current_post_id(); if ( $post_id && true === self::fields( $post_id )['noindex'] ) { $robots['noindex'] = true; } return $robots; } /** * Whether the connector should print anything at all: when an SEO store * is available and no SEO plugin is printing the fields itself. * * @return bool */ private static function active() { return null !== Stores::current() && ! Environment::seo_plugin_prints_head(); } /** * The post being viewed, when the request is for one post or page and * the connector is printing; otherwise 0. * * @return int */ private static function current_post_id() { if ( ! is_singular() || ! self::active() ) { return 0; } return (int) get_queried_object_id(); } /** * All SEO fields for a post, from the store Blaze writes to. * * @param int $post_id Post id. * @return array */ private static function fields( $post_id ) { $store = Stores::current(); return $store ? $store->read( $post_id ) : array( 'noindex' => false ); } /** * One printable text field, or null when it is unset, holds a template * variable, or nothing should be printed for this post. * * @param int $post_id Post id, or 0. * @param string $name Field name. * @return string|null */ private static function field( $post_id, $name ) { if ( ! $post_id ) { return null; } $value = self::fields( $post_id )[ $name ] ?? null; if ( null === $value || preg_match( '/%%?[a-z0-9_]+%%?/i', $value ) ) { return null; } return $value; } }