l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } l ) { return ''; } return $image_url; } /** * Gets the first image url of a gallery. * * @param int $post_id Post ID to use. * * @return string The image url or an empty string when not found. */ public function get_gallery_image( $post_id ) { $post = \get_post( $post_id ); if ( \strpos( $post->post_content, '[gallery' ) === false ) { return ''; } $images = \get_post_gallery_images( $post ); if ( empty( $images ) ) { return ''; } return \reset( $images ); } /** * Gets the image url from the term content. * * @param int $term_id The term id to extract the images from. * * @return string The image url or an empty string when not found. */ public function get_term_content_image( $term_id ) { $image_url = $this->get_first_content_image_for_term( $term_id ); if ( $image_url === null ) { return ''; } return $image_url; } /** * Retrieves the caption for an attachment. * * @param int $attachment_id Attachment ID. * * @return string The caption when found, empty string when no caption is found. */ public function get_caption( $attachment_id ) { $caption = \wp_get_attachment_caption( $attachment_id ); if ( ! empty( $caption ) ) { return $caption; } $caption = \get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ); if ( ! empty( $caption ) ) { return $caption; } return ''; } /** * Retrieves the attachment metadata. * * @param int $attachment_id Attachment ID. * * @return array The metadata, empty array when no metadata is found. */ public function get_metadata( $attachment_id ) { $metadata = \wp_get_attachment_metadata( $attachment_id ); if ( ! $metadata || ! \is_array( $metadata ) ) { return []; } return $metadata; } /** * Retrieves the attachment image url. * * @param int $attachment_id Attachment ID. * @param string $size The size to get. * * @return string The url when found, empty string otherwise. */ public function get_attachment_image_url( $attachment_id, $size ) { $url = \wp_get_attachment_image_url( $attachment_id, $size ); if ( ! $url ) { return ''; } return $url; } /** * Find the right version of an image based on size. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id Attachment ID. * @param string $size Size name. * * @return array|false Returns an array with image data on success, false on failure. */ public function get_image( $attachment_id, $size ) { return WPSEO_Image_Utils::get_image( $attachment_id, $size ); } /** * Retrieves the best attachment variation for the given attachment. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $attachment_id The attachment id. * * @return bool|string The attachment url or false when no variations found. */ public function get_best_attachment_variation( $attachment_id ) { $variations = WPSEO_Image_Utils::get_variations( $attachment_id ); $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 false; } // The variations are ordered so the first variations is by definition the best one. return \reset( $variations ); } /** * Find an attachment ID for a given URL. * * @param string $url The URL to find the attachment for. * @param bool $use_link_table Whether the SEO Links table will be used to retrieve the id. * * @return int The found attachment ID, or 0 if none was found. */ public function get_attachment_by_url( $url, $use_link_table = true ) { // Don't try to do this for external URLs. $parsed_url = \wp_parse_url( $url ); if ( $this->url_helper->get_link_type( $parsed_url ) === SEO_Links::TYPE_EXTERNAL ) { return 0; } /** The `wpseo_force_creating_and_using_attachment_indexables` filter is documented in indexable-link-builder.php */ if ( ! $this->options_helper->get( 'disable-attachment' ) || \apply_filters( 'wpseo_force_creating_and_using_attachment_indexables', false ) ) { // Strip out the size part of an image URL. $url = \preg_replace( '/(.*)-\d+x\d+\.(jpeg|jpg|png|gif)$/', '$1.$2', $url ); $indexable = $this->indexable_repository->find_by_permalink( $url ); if ( $indexable && $indexable->object_type === 'post' && $indexable->object_sub_type === 'attachment' ) { return $indexable->object_id; } $post_id = WPSEO_Image_Utils::get_attachment_by_url( $url ); if ( $post_id !== 0 ) { // Find the indexable, this triggers creating it so it can be found next time. $this->indexable_repository->find_by_id_and_type( $post_id, 'post' ); } return $post_id; } if ( ! $use_link_table ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } $cache_key = 'attachment_seo_link_object_' . \md5( $url ); $found = false; $link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found ); if ( $found === false ) { $link = $this->seo_links_repository->find_one_by_url( $url ); \wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS ); } if ( ! \is_a( $link, SEO_Links::class ) ) { return WPSEO_Image_Utils::get_attachment_by_url( $url ); } return $link->target_post_id; } /** * Retrieves an attachment ID for an image uploaded in the settings. * * Due to self::get_attachment_by_url returning 0 instead of false. * 0 is also a possibility when no ID is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param string $setting The setting the image is stored in. * * @return int|bool The attachment id, or false or 0 if no ID is available. */ public function get_attachment_id_from_settings( $setting ) { return WPSEO_Image_Utils::get_attachment_id_from_settings( $setting ); } /** * Based on and image ID return array with the best variation of that image. If it's not saved to the DB, save it * to an option. * * @param string $setting The setting name. Should be company or person. * * @return array|bool Array with image details when the image is found, boolean when it's not found. */ public function get_attachment_meta_from_settings( $setting ) { $image_meta = $this->options_helper->get( $setting . '_meta', false ); if ( ! $image_meta ) { $image_id = $this->options_helper->get( $setting . '_id', false ); if ( $image_id ) { // There is not an option to put a URL in an image field in the settings anymore, only to upload it through the media manager. // This means an attachment always exists, so doing this is only needed once. $image_meta = $this->get_best_attachment_variation( $image_id ); if ( $image_meta ) { $this->options_helper->set( $setting . '_meta', $image_meta ); } } } return $image_meta; } /** * Retrieves the first usable content image for a post. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $post_id The post id to extract the images from. * * @return string|null */ protected function get_first_usable_content_image_for_post( $post_id ) { return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $post_id ); } /** * Gets the term's first usable content image. Null if none is available. * * @codeCoverageIgnore - We have to write test when this method contains own code. * * @param int $term_id The term id. * * @return string|null The image URL. */ protected function get_first_content_image_for_term( $term_id ) { return WPSEO_Image_Utils::get_first_content_image_for_term( $term_id ); } } « Sourire d’Espoir »: clap de fin pour la 3e édition à la MACO | Infos Culture du Faso
ven 22 novembre 2024

Suivez-nous sur les réseaux sociaux

spot_img

« Sourire d’Espoir »: clap de fin pour la 3e édition à la MACO

La Maison d’Arrêt et de Correction de Ouagadougou (MACO) a abrité, ce vendredi 16 juin 2023, la restitution de formation humoristique dénommée « Sourire d’Espoir ». Il s’agit d’une initiative du groupe d’humoristes Génération 2000.

Pour la 3e année consécutive, le groupe d’humoristes Génération 2000 a réitéré sa coutume, celle d’initier des pensionnaires de la MACO aux outils humoristiques. Cette formation entre dans la dynamique de réinsertion prônée au profit des détenus de toutes les prisons du pays. Il s’agit en effet, à travers cette fenêtre ouverte, de non seulement leur donner un tout petit peu de sourire, mais aussi et surtout susciter de l’espoir en leur offrant l’opportunité de faire connaissances avec les différents outils de l’humour.

« Ce projet nous tient vraiment à cœur. Et nous essaierons dans la mesure du possible de faire plaisir à ces détenus », Neya Babou Michel

Ils étaient donc quatre, ces détenus, à bénéficier de cette formation. L’écriture de texte, la diction, la mise en scène, la gestuelle, tels ont été l’essentiel du contenu de cette formation. À en croire Neya Babou Michel alias Michou de Génération 2000, les détenus ont aussi droit aux opportunités. C’est ce qui justifie, selon lui, la mise en Å“uvre de ce projet. « Ceux que vous avez eu le plaisir de voir en prestations aujourd’hui, ont reçu des outils d’initiation à la 2e édition. Nous avons voulu continuer avec eux, afin de leur permettre de se perfectionner davantage. L’idée, c’est de pouvoir les suivre, tant que nous le pouvons, de sorte à ce qu’ils puissent en faire un métier à l’issue de leurs peines. D’ailleurs, seulement trois détenus ont presté par ce qu’il y a un qui a bénéficié d’une semi-liberté », a-t-il signifié.

Le Directeur de la MACO, Frédéric Ouedraogo, dit esperer que l’initiative perdure

C’est donc devant leurs camarades détenus, humoristes confirmés et bien évidemment les premiers responsables de l’établissement pénitentiaire que ces probables futurs humoristes ont fait montre de leur talent. Frédéric Ouédraogo, Directeur de la MACO, a par ailleurs salué les initiateurs, qui depuis quelques années, font de leur mieux pour apporter de la joie dans les cÅ“urs de ces détenus. « Les professionnels de l’humour qui étaient là aujourd’hui ont tous reconnu qu’il y a du talent à revendre. C’est pourquoi nous ne cesserons de réitérer notre gratitude à l’endroit du groupe Génération 2000 pour cette initiative. Nous espérons que d’autres gestes pareils suivront, pour le bonheur des détenus », a-t-il soutenu.

Le JAP, Lassané Guelbéogo promet faire de son mieux pour que ces détenus formés puissent aller faire valoir leur talent dans des festivals

C’est une activité noble qui mérite amplement d’être encouragé. C’est du moins ce que nous confie le Juge d’Application des Peines (JAP) de Ouaga 1, Guelbéogo Lassané. Pour lui, « Sourire d’Espoir » permet non seulement aux détenus de se recréer mais aussi de s’essayer à des activités qui pourraient leur servir après avoir purger leurs peines. « Pour notre part, nous menons des réflexions pour voir comment les faire participer à des festivals. L’objectif est qu’ils puissent faire valoir leur talent dehors afin que les populations découvrent leur talent mais également se rendent à l’évidence que la prison n’est pas une fatalité », foi de monsieur Guelbéogo Lassané, le Juge d’Application des Peines (JAP) de Ouaga 1.

Une remise de cadeaux composés de vivres, est venue mettre fin à cette cérémonie de restitution. Les projecteurs sont d’ores et déjà tournés vers la 4e édition.

Boukari OUÉDRAOGO

LAISSER UN COMMENTAIRE

S'il vous plaît entrez votre commentaire!
S'il vous plaît entrez votre nom ici

Publicité

spot_img

Publicité

spot_img

Publicité

spot_img

Publicité

spot_img

Plus d'articles

Vous ne pouvez pas copier le contenu de cette page