e() { if ( $this->has_images() ) { return; } $default_image_url = WPSEO_Options::get( 'og_default_image', '' ); $default_image_id = WPSEO_Options::get( 'og_default_image_id', '' ); if ( $default_image_url === '' && $default_image_id === '' ) { return; } $this->add_image_by_id_or_url( $default_image_id, $default_image_url, array( $this, 'save_default_image_id' ) ); } /** * Saves the Open Graph image meta to the database for the current post. * * @param string $attachment_id The ID to save. * * @return void */ private function save_opengraph_image_id_meta( $attachment_id ) { $post_id = $this->get_post_id(); WPSEO_Meta::set_value( 'opengraph-image-id', (string) $attachment_id, $post_id ); } /** * If opengraph-image is set, call add_image and return true. * * @param int $post_id Optional post ID to use. * * @return void */ private function set_image_post_meta( $post_id = 0 ) { $image_id = WPSEO_Meta::get_value( 'opengraph-image-id', $post_id ); $image_url = WPSEO_Meta::get_value( 'opengraph-image', $post_id ); $this->add_image_by_id_or_url( $image_id, $image_url, array( $this, 'save_opengraph_image_id_meta' ) ); } /** * Check if taxonomy has an image and add this image. * * @return void */ private function set_taxonomy_image() { $image_url = WPSEO_Taxonomy_Meta::get_meta_without_term( 'opengraph-image' ); $this->add_image_by_url( $image_url ); } /** * If there is a featured image, check image size. If image size is correct, call add_image and return true. * * @param int $post_id The post ID. * * @return void */ private function set_featured_image( $post_id ) { if ( has_post_thumbnail( $post_id ) ) { $attachment_id = get_post_thumbnail_id( $post_id ); $this->add_image_by_id( $attachment_id ); } } /** * If this is an attachment page, call add_image with the attachment. * * @return void */ private function set_attachment_page_image() { $post_id = $this->get_post_id(); if ( wp_attachment_is_image( $post_id ) ) { $this->add_image_by_id( $post_id ); } } /** * Adds an image based on a given URL, and attempts to be smart about it. * * @param string $url The given URL. * * @return null|number Returns the found attachment ID if it exists. Otherwise -1. * If the URL is empty we return null. */ public function add_image_by_url( $url ) { if ( empty( $url ) ) { return null; } $attachment_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $attachment_id > 0 ) { $this->add_image_by_id( $attachment_id ); return $attachment_id; } $this->add_image( array( 'url' => $url ) ); return -1; } /** * Returns the overridden image size if it has been overridden. * * @return null|string The overridden image size or null. */ protected function get_overridden_image_size() { /** * Filter: 'wpseo_opengraph_image_size' - Allow overriding the image size used * for OpenGraph sharing. If this filter is used, the defined size will always be * used for the og:image. The image will still be rejected if it is too small. * * Only use this filter if you manually want to determine the best image size * for the `og:image` tag. * * Use the `wpseo_image_sizes` filter if you want to use our logic. That filter * can be used to add an image size that needs to be taken into consideration * within our own logic. * * @api string $size Size string. */ return apply_filters( 'wpseo_opengraph_image_size', null ); } /** * Determines if the OpenGraph image size should overridden. * * @return bool Whether the size should be overridden. */ protected function is_size_overridden() { return $this->get_overridden_image_size() !== null; } /** * Adds the possibility to short-circuit all the optimal variation logic with * your own size. * * @param int $attachment_id The attachment ID that is used. * * @return void */ protected function get_overridden_image( $attachment_id ) { $attachment = WPSEO_Image_Utils::get_image( $attachment_id, $this->get_overridden_image_size() ); if ( $attachment ) { $this->add_image( $attachment ); } } /** * Adds an image to the list by attachment ID. * * @param int $attachment_id The attachment ID to add. * * @return void */ public function add_image_by_id( $attachment_id ) { if ( ! $this->is_valid_attachment( $attachment_id ) ) { return; } if ( $this->is_size_overridden() ) { $this->get_overridden_image( $attachment_id ); return; } $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $variations = WPSEO_Image_Utils::filter_usable_dimensions( $this->image_params, $variations ); $variations = WPSEO_Image_Utils::filter_usable_file_size( $variations ); // If we are left without variations, there is no valid variation for this attachment. if ( empty( $variations ) ) { return; } // The variations are ordered so the first variations is by definition the best one. $attachment = $variations[0]; if ( $attachment ) { $this->add_image( $attachment ); } } /** * Sets the images based on the page type. * * @return void */ protected function set_images() { /** * Filter: wpseo_add_opengraph_images - Allow developers to add images to the OpenGraph tags. * * @api WPSEO_OpenGraph_Image The current object. */ do_action( 'wpseo_add_opengraph_images', $this ); switch ( true ) { case is_front_page(): $this->set_front_page_image(); break; case is_home(): $this->set_posts_page_image(); break; case is_attachment(): $this->set_attachment_page_image(); break; case $this->get_frontend_page_type()->is_simple_page(): $this->set_singular_image(); break; case is_category(): case is_tag(): case is_tax(): $this->set_taxonomy_image(); } /** * Filter: wpseo_add_opengraph_additional_images - Allows to add additional images to the OpenGraph tags. * * @api WPSEO_OpenGraph_Image The current object. */ do_action( 'wpseo_add_opengraph_additional_images', $this ); $this->maybe_set_default_image(); } /** * Determines whether or not the wanted attachment is considered valid. * * @param int $attachment_id The attachment ID to get the attachment by. * * @return bool Whether or not the attachment is valid. */ protected function is_valid_attachment( $attachment_id ) { $attachment = get_post_mime_type( $attachment_id ); if ( $attachment === false ) { return false; } return $this->is_valid_image_type( $attachment ); } /** * Determines whether the passed mime type is a valid image type. * * @param string $mime_type The detected mime type. * * @return bool Whether or not the attachment is a valid image type. */ protected function is_valid_image_type( $mime_type ) { return in_array( $mime_type, $this->valid_image_types, true ); } /** * Determines whether the passed URL is considered valid. * * @param string $url The URL to check. * * @return bool Whether or not the URL is a valid image. */ protected function is_valid_image_url( $url ) { if ( ! is_string( $url ) ) { return false; } $image_extension = $this->get_extension_from_url( $url ); $is_valid = in_array( $image_extension, $this->valid_image_extensions, true ); /** * Filter: 'wpseo_opengraph_is_valid_image_url' - Allows extra validation for an image url. * * @api bool - Current validation result. * * @param string $url The image url to validate. */ return apply_filters( 'wpseo_opengraph_is_valid_image_url', $is_valid, $url ); } /** * Gets the image path from the passed URL. * * @param string $url The URL to get the path from. * * @return string The path of the image URL. Returns an empty string if URL parsing fails. */ protected function get_image_url_path( $url ) { return (string) wp_parse_url( $url, PHP_URL_PATH ); } /** * Determines the file extension of the passed URL. * * @param string $url The URL. * * @return string The extension. */ protected function get_extension_from_url( $url ) { $extension = ''; $path = $this->get_image_url_path( $url ); if ( $path === '' ) { return $extension; } $parts = explode( '.', $path ); if ( ! empty( $parts ) ) { $extension = end( $parts ); } return $extension; } /** * Gets the post ID. * * @return int The post ID. */ protected function get_post_id() { return $this->get_frontend_page_type()->get_simple_page_id(); } /** * Adds the first usable attachment image from the post content. * * @param WP_Post $post The post object. * * @return void */ private function add_first_usable_content_image( $post ) { $image_finder = new WPSEO_Content_Images(); $images = $image_finder->get_images( $post->ID, $post ); if ( ! is_array( $images ) || $images === array() ) { return; } $image_url = reset( $images ); if ( ! $image_url ) { return; } $this->add_image( array( 'url' => $image_url ) ); } }