bility categories. * * @since 6.9.0 * * @param WP_REST_Request $request Full details about the request. * @return bool True if the request has read access. */ public function get_items_permissions_check( $request ) { return current_user_can( 'read' ); } /** * Checks if a given request has access to read an ability category. * * @since 6.9.0 * * @param WP_REST_Request $request Full details about the request. * @return bool True if the request has read access. */ public function get_item_permissions_check( $request ) { return current_user_can( 'read' ); } /** * Prepares an ability category for response. * * @since 6.9.0 * * @param WP_Ability_Category $category The ability category object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ public function prepare_item_for_response( $category, $request ) { $data = array( 'slug' => $category->get_slug(), 'label' => $category->get_label(), 'description' => $category->get_description(), 'meta' => $category->get_meta(), ); $context = $request['context'] ?? 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context ); $response = rest_ensure_response( $data ); $fields = $this->get_fields_for_response( $request ); if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) { $links = array( 'self' => array( 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $category->get_slug() ) ), ), 'collection' => array( 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), ), 'abilities' => array( 'href' => rest_url( sprintf( '%s/abilities?category=%s', $this->namespace, $category->get_slug() ) ), ), ); $response->add_links( $links ); } return $response; } /** * Retrieves the ability category's schema, conforming to JSON Schema. * * @since 6.9.0 * * @return array Item schema data. */ public function get_item_schema(): array { $schema = array( '$schema' => 'http://json-schema.org/draft-04/schema#', 'title' => 'ability-category', 'type' => 'object', 'properties' => array( 'slug' => array( 'description' => __( 'Unique identifier for the ability category.' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, ), 'label' => array( 'description' => __( 'Display label for the category.' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, ), 'description' => array( 'description' => __( 'Description of the category.' ), 'type' => 'string', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), 'meta' => array( 'description' => __( 'Meta information about the category.' ), 'type' => 'object', 'context' => array( 'view', 'edit' ), 'readonly' => true, ), ), ); return $this->add_additional_fields_schema( $schema ); } /** * Retrieves the query params for collections. * * @since 6.9.0 * * @return array Collection parameters. */ public function get_collection_params(): array { return array( 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 'page' => array( 'description' => __( 'Current page of the collection.' ), 'type' => 'integer', 'default' => 1, 'minimum' => 1, ), 'per_page' => array( 'description' => __( 'Maximum number of items to be returned in result set.' ), 'type' => 'integer', 'default' => 50, 'minimum' => 1, 'maximum' => 100, ), ); } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } } $meta_robots = \explode( ',', $noindex_advanced ); foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { $indexable->breadcrumb_title = \wp_strip_all_tags( \get_the_title( $post_id ), true ); } $this->handle_social_images( $indexable ); $indexable->author_id = $post->post_author; $indexable->post_parent = $post->post_parent; $indexable->number_of_pages = $this->get_number_of_pages_for_post( $post ); $indexable->post_status = $post->post_status; $indexable->is_protected = $post->post_password !== ''; $indexable->is_public = $this->is_public( $indexable ); $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; $indexable->version = $this->version; return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param int $post_id The post ID. * * @return WP_Error|string|false The permalink. */ protected function get_permalink( $post_type, $post_id ) { if ( $post_type !== 'attachment' ) { return \get_permalink( $post_id ); } return \wp_get_attachment_url( $post_id ); } /** * Determines the value of is_public. * * @param Indexable $indexable The indexable. * * @return bool|null Whether or not the post type is public. Null if no override is set. */ protected function is_public( $indexable ) { if ( $indexable->is_protected === true ) { return false; } if ( $indexable->is_robots_noindex === true ) { return false; } // Attachments behave differently than the other post types, since they inherit from their parent. if ( $indexable->object_sub_type === 'attachment' ) { return $this->is_public_attachment( $indexable ); } if ( ! \in_array( $indexable->post_status, $this->post_helper->get_public_post_statuses(), true ) ) { return false; } if ( $indexable->is_robots_noindex === false ) { return true; } return null; } /** * Determines the value of is_public for attachments. * * @param Indexable $indexable The indexable. * * @return bool|null False when it has no parent. Null when it has a parent. */ protected function is_public_attachment( $indexable ) { // If the attachment has no parent, it should not be public. if ( empty( $indexable->post_parent ) ) { return false; } // If the attachment has a parent, the is_public should be NULL. return null; } /** * Determines the value of has_public_posts. * * @param Indexable $indexable The indexable. * * @return bool|null Whether the attachment has a public parent, can be true, false and null. Null when it is not an attachment. */ protected function has_public_posts( $indexable ) { // Only attachments (and authors) have this value. if ( $indexable->object_sub_type !== 'attachment' ) { return null; } // The attachment should have a post parent. if ( empty( $indexable->post_parent ) ) { return false; } // The attachment should inherit the post status. if ( $indexable->post_status !== 'inherit' ) { return false; } // The post parent should be public. $post_parent_indexable = $this->indexable_repository->find_by_id_and_type( $indexable->post_parent, 'post' ); if ( $post_parent_indexable !== false ) { return $post_parent_indexable->is_public; } return false; } /** * Converts the meta robots noindex value to the indexable value. * * @param int $value Meta value to convert. * * @return bool|null True for noindex, false for index, null for default of parent/type. */ protected function get_robots_noindex( $value ) { $value = (int) $value; switch ( $value ) { case 1: return true; case 2: return false; } return null; } /** * Retrieves the robot options to search for. * * @return array List of robots values. */ protected function get_robots_options() { return [ 'noimageindex', 'noarchive', 'nosnippet' ]; } /** * Determines the focus keyword score. * * @param string $keyword The focus keyword that is set. * @param int $score The score saved on the meta data. * * @return int|null Score to use. */ protected function get_keyword_score( $keyword, $score ) { if ( empty( $keyword ) ) { return null; } return $score; } /** * Retrieves the lookup table. * * @return array Lookup table for the indexable fields. */ protected function get_indexable_lookup() { return [ 'focuskw' => 'primary_focus_keyword', 'canonical' => 'canonical', 'title' => 'title', 'metadesc' => 'description', 'bctitle' => 'breadcrumb_title', 'opengraph-title' => 'open_graph_title', 'opengraph-image' => 'open_graph_image', 'opengraph-image-id' => 'open_graph_image_id', 'opengraph-description' => 'open_graph_description', 'twitter-title' => 'twitter_title', 'twitter-image' => 'twitter_image', 'twitter-image-id' => 'twitter_image_id', 'twitter-description' => 'twitter_description', 'estimated-reading-time-minutes' => 'estimated_reading_time_minutes', ]; } /** * Finds an alternative image for the social image. * * @param Indexable $indexable The indexable. * * @return array|bool False when not found, array with data when found. */ protected function find_alternative_image( Indexable $indexable ) { if ( $indexable->object_sub_type === 'attachment' && $this->image->is_valid_attachment( $indexable->object_id ) ) { return [ 'image_id' => $indexable->object_id, 'source' => 'attachment-image', ]; } $featured_image_id = $this->image->get_featured_image_id( $indexable->object_id ); if ( $featured_image_id ) { return [ 'image_id' => $featured_image_id, 'source' => 'featured-image', ]; } $gallery_image = $this->image->get_gallery_image( $indexable->object_id ); if ( $gallery_image ) { return [ 'image' => $gallery_image, 'source' => 'gallery-image', ]; } $content_image = $this->image->get_post_content_image( $indexable->object_id ); if ( $content_image ) { return [ 'image' => $content_image, 'source' => 'first-content-image', ]; } return false; } /** * Gets the number of pages for a post. * * @param object $post The post object. * * @return int|null The number of pages or null if the post isn't paginated. */ protected function get_number_of_pages_for_post( $post ) { $number_of_pages = ( \substr_count( $post->post_content, '' ) + 1 ); if ( $number_of_pages <= 1 ) { return null; } return $number_of_pages; } /** * Checks whether an indexable should be built for this post. * * @param WP_Post $post The post for which an indexable should be built. * * @return bool `true` if the post should be excluded from building, `false` if not. */ protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } /** * Transforms an empty string into null. Leaves non-empty strings intact. * * @param string $text The string. * * @return string|null The input string or null. */ protected function empty_string_to_null( $text ) { if ( ! \is_string( $text ) || $text === '' ) { return null; } return $text; } }