et_current_blog_id(); $indexable->schema_page_type = $this->get_meta_value( $post_id, 'schema_page_type' ); $indexable->schema_article_type = $this->get_meta_value( $post_id, 'schema_article_type' ); return $indexable; } /** * Retrieves the permalink for a post with the given post type and ID. * * @param string $post_type The post type. * @param integer $post_id The post ID. * * @return false|string|WP_Error 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->is_public_post_status(), 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; } /** * Retrieves the list of public posts statuses. * * @return array The public post statuses. */ protected function is_public_post_status() { /** * Filter: 'wpseo_public_post_statuses' - List of public post statuses. * * @api array $post_statuses Post status list, defaults to array( 'publish' ). */ return \apply_filters( 'wpseo_public_post_statuses', [ 'publish' ] ); } /** * 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 null|int 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', ]; } /** * Retrieves the current value for the meta field. * * @param int $post_id The post ID to use. * @param string $meta_key Meta key to fetch. * * @return mixed The value of the indexable entry to use. */ protected function get_meta_value( $post_id, $meta_key ) { $value = WPSEO_Meta::get_value( $meta_key, $post_id ); if ( \is_string( $value ) && $value === '' ) { return null; } return $value; } /** * 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 \in_array( $post->post_type, $this->post_type_helper->get_excluded_post_types_for_indexables(), true ); } }