D, $this->thumbnailSize ) : ''; } /** * Returns the first image found in the post content. * * @since 4.0.0 * * @return string The image URL. */ private function getFirstImageInContent() { $cachedImage = $this->getCachedImage(); if ( $cachedImage ) { return $cachedImage; } $postContent = aioseo()->helpers->getPostContent( $this->post ); preg_match_all( '||i', $postContent, $matches ); // Ignore cover block background image - WP >= 5.7. if ( ! empty( $matches[0] ) && apply_filters( 'aioseo_social_image_ignore_cover_block', true, $this->post, $matches ) ) { foreach ( $matches[0] as $key => $match ) { if ( false !== stripos( $match, 'wp-block-cover__image-background' ) ) { unset( $matches[1][ $key ] ); } } } return ! empty( $matches[1] ) ? current( $matches[1] ) : ''; } /** * Returns the author avatar. * * @since 4.0.0 * * @return string The image URL. */ private function getAuthorAvatar() { $avatar = get_avatar( $this->post->post_author, 300 ); preg_match( "/src='(.*?)'/i", $avatar, $matches ); return ! empty( $matches[1] ) ? $matches[1] : ''; } /** * Returns the first available image. * * @since 4.0.0 * * @return string The image URL. */ private function getFirstAvailableImage() { // Disable the cache. $this->useCache = false; $image = $this->getCustomFieldImage(); if ( ! $image ) { $image = $this->getFeaturedImage(); } if ( ! $image ) { $image = $this->getFirstAttachedImage(); } if ( ! $image ) { $image = $this->getFirstImageInContent(); } if ( ! $image && 'twitter' === strtolower( $this->type ) ) { $image = aioseo()->options->social->twitter->homePage->image; } // Enable the cache. $this->useCache = true; return $image ? $image : aioseo()->options->social->facebook->homePage->image; } /** * Returns the image from a custom field. * * @since 4.0.0 * * @return string The image URL. */ private function getCustomFieldImage() { $cachedImage = $this->getCachedImage(); if ( $cachedImage ) { return $cachedImage; } $prefix = 'facebook' === strtolower( $this->type ) ? 'og_' : 'twitter_'; $aioseoPost = Models\Post::getPost( $this->post->ID ); $customFields = ! empty( $aioseoPost->{ $prefix . 'image_custom_fields' } ) ? $aioseoPost->{ $prefix . 'image_custom_fields' } : aioseo()->options->social->{$this->type}->general->customFieldImagePosts; if ( ! $customFields ) { return ''; } $customFields = explode( ',', $customFields ); foreach ( $customFields as $customField ) { $image = get_post_meta( $this->post->ID, $customField, true ); if ( ! empty( $image ) ) { return is_numeric( $image ) ? wp_get_attachment_image_src( $image, $this->thumbnailSize ) : $image; } } return ''; } /** * Returns the cached image if there is one. * * @since 4.1.6.2 * * @param \WP_Term $object The object for which we need to get the cached image. * @return string|array The image URL or data. */ protected function getCachedImage( $object = null ) { if ( null === $object ) { // This isn't null if we call it from the Pro class. $object = $this->post; } $metaData = aioseo()->meta->metaData->getMetaData( $object ); switch ( $this->type ) { case 'facebook': if ( ! empty( $metaData->og_image_url ) && $this->useCache ) { return aioseo()->meta->metaData->getCachedOgImage( $metaData ); } break; case 'twitter': if ( ! empty( $metaData->twitter_image_url ) && $this->useCache ) { return $metaData->twitter_image_url; } break; default: break; } return ''; } }