Skip to content

Commit

Permalink
Social Meta Module - OG Image not pulling in full size image (#1461)
Browse files Browse the repository at this point in the history
* Social Meta Module - OG Image not pulling in full size image #260

* PR comments

* PR comments

1) Using 3 filters: aioseop_attachment_size, aioseop_thumbnail_size and
post_thumbnail_size
2) image in content also being tested for being an attachment

* support for #1557

* PR comments

#260 (comment)

* test cases

* show full size image

* Update aioseop_opengraph.php

* PR comments

#1461 (comment)

* class name conflict

* moved methods

* PR comments

#1461 (comment)

* corrected test case
  • Loading branch information
contactashish13 authored and michaeltorbert committed Oct 17, 2018
1 parent 1a8519d commit 48716fb
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 202 deletions.
22 changes: 9 additions & 13 deletions admin/aioseop_module_class.php
Expand Up @@ -1575,8 +1575,7 @@ function get_the_image_by_post_thumbnail( $p = null ) {
return false;
}

$size = apply_filters( 'post_thumbnail_size', 'large' ); // Check if someone is using built-in WP filter.
$size = apply_filters( 'aioseop_thumbnail_size', $size );
$size = apply_filters( 'aioseop_attachment_size', apply_filters( 'aioseop_thumbnail_size', apply_filters( 'post_thumbnail_size', 'full' ) ) );
$image = wp_get_attachment_image_src( $post_thumbnail_id, $size );

return $image[0];
Expand Down Expand Up @@ -1607,8 +1606,7 @@ function get_the_image_by_attachment( $p = null ) {
);

if ( empty( $attachments ) && 'attachment' == get_post_type( $post->ID ) ) {
$size = 'large';
$size = apply_filters( 'aioseop_attachment_size', $size );
$size = apply_filters( 'aioseop_attachment_size', apply_filters( 'aioseop_thumbnail_size', apply_filters( 'post_thumbnail_size', 'full' ) ) );
$image = wp_get_attachment_image_src( $post->ID, $size );
}

Expand All @@ -1623,8 +1621,7 @@ function get_the_image_by_attachment( $p = null ) {
/* Loop through each attachment. Once the $order_of_image (default is '1') is reached, break the loop. */
foreach ( $attachments as $id => $attachment ) {
if ( ++ $i == 1 ) {
$size = 'large';
$size = apply_filters( 'aioseop_attachment_size', $size );
$size = apply_filters( 'aioseop_attachment_size', apply_filters( 'aioseop_thumbnail_size', apply_filters( 'post_thumbnail_size', 'full' ) ) );
$image = wp_get_attachment_image_src( $id, $size );
$alt = trim( strip_tags( get_post_field( 'post_excerpt', $id ) ) );
break;
Expand All @@ -1643,24 +1640,23 @@ function get_the_image_by_attachment( $p = null ) {
* @return bool
*/
function get_the_image_by_scan( $p = null ) {
$url = false;

if ( $p === null ) {
global $post;
} else {
$post = $p;
}

/* Search the post's content for the <img /> tag and get its URL. */
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field( 'post_content', $post->ID ), $matches );

/* If there is a match for the image, return its URL. */
if ( isset( $matches ) && ! empty( $matches[1][0] ) ) {
return $matches[1][0];
$images = aiosp_common::parse_content_for_images( $post );
if ( $images ) {
$url = aiosp_common::get_image_src_for_url( $images[0], 'full' );
}

return false;
return $url;
}


/**
* @param $default_options
* @param $options
Expand Down
3 changes: 2 additions & 1 deletion inc/aiosp_common.php
Expand Up @@ -201,6 +201,7 @@ public static function get_image_src_for_url( $url, $size = 'thumbnail' ) {
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s", '%' . basename( $path ) . '%' ) );

if ( $attachment_id ) {
$size = apply_filters( 'aioseop_attachment_size', apply_filters( 'aioseop_thumbnail_size', apply_filters( 'post_thumbnail_size', $size ) ) );
// if this is a valid attachment, get the correct size.
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( $image ) {
Expand Down Expand Up @@ -545,7 +546,7 @@ public static function is_image_valid( $image ) {

}
return true;
}
}

/**
* Check whether a url is valid.
Expand Down
8 changes: 7 additions & 1 deletion modules/aioseop_opengraph.php
Expand Up @@ -185,7 +185,7 @@ function __construct() {
add_action( 'wp', array( $this, 'type_setup' ) );
}

if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
if ( ! is_admin() || defined( 'DOING_AJAX' ) || ( defined( 'AIOSEOP_UNIT_TESTING' ) && AIOSEOP_UNIT_TESTING ) ) {
$this->do_opengraph();
}
// Set variables after WordPress load.
Expand Down Expand Up @@ -971,6 +971,12 @@ function add_attributes( $output ) {
function add_meta() {
global $post, $aiosp, $aioseop_options, $wp_query;
$metabox = $this->get_current_options( array(), 'settings' );

// we have to introduce this because, for some reason, $this->options is not being populated at all while testing.
if ( defined( 'AIOSEOP_UNIT_TESTING' ) && AIOSEOP_UNIT_TESTING ) {
$this->options = $aioseop_options['modules']['aiosp_opengraph_options'];
}

$key = $this->options['aiosp_opengraph_key'];
$dimg = $this->options['aiosp_opengraph_dimg'];
$current_post_type = get_post_type();
Expand Down
126 changes: 12 additions & 114 deletions modules/aioseop_sitemap.php
Expand Up @@ -3085,7 +3085,7 @@ private function get_images_from_post( $post ) {
}

if ( isset( $post_thumbnails[ $post->ID ] ) ) {
$attachment_url = wp_get_attachment_image_url( $post_thumbnails[ $post->ID ], 'post-thumbnail' );
$attachment_url = wp_get_attachment_image_url( $post_thumbnails[ $post->ID ], 'full' );
if ( $attachment_url ) {
$images[] = $attachment_url;
}
Expand Down Expand Up @@ -3120,126 +3120,24 @@ private function get_images_from_post( $post ) {
return $images;
}

/**
* Fetch images from WP, Jetpack and WooCommerce galleries.
*
* @param string $post The post.
* @param array $images the array of images.
*
* @since 2.4.2
*/
private function get_gallery_images( $post, &$images ) {
if ( false === apply_filters( 'aioseo_include_images_in_wp_gallery', true ) ) {
return;
}

// Check images galleries in the content. DO NOT run the_content filter here as it might cause issues with other shortcodes.
if ( has_shortcode( $post->post_content, 'gallery' ) ) {
// Get the jetpack gallery images.
if ( class_exists( 'Jetpack_PostImages' ) ) {
$jetpack = Jetpack_PostImages::get_images( $post->ID );
if ( $jetpack ) {
foreach ( $jetpack as $jetpack_image ) {
$images[] = $jetpack_image['src'];
}
}
}

// Get the default WP gallery images.
$galleries = get_post_galleries( $post, false );
if ( $galleries ) {
foreach ( $galleries as $gallery ) {
$images = array_merge( $images, $gallery['src'] );
}
}
}

// Check WooCommerce product gallery.
if ( class_exists( 'WooCommerce' ) ) {
$woo_images = get_post_meta( $post->ID, '_product_image_gallery', true );
if ( ! empty( $woo_images ) ) {
$woo_images = array_filter( explode( ',', $woo_images ) );
if ( is_array( $woo_images ) ) {
foreach ( $woo_images as $id ) {
$images[] = wp_get_attachment_url( $id );
}
}
}
}

$images = array_unique( $images );
}

/**
* Parses the content to find out if specified images galleries exist and if they do, parse them for images.
* Supports NextGen.
* Cleans the URL so that its acceptable in the sitemap.
*
* @param string $content The post content.
* @param string $url The image url.
*
* @since 2.4.2
* @since 2.4.1
*
* @return string
*/
private function get_content_from_galleries( $content ) {
// Support for NextGen Gallery.
static $gallery_types = array( 'ngg_images' );
$types = apply_filters( 'aioseop_gallery_shortcodes', $gallery_types );

$gallery_content = '';

if ( ! $types ) {
return $gallery_content;
}

$found = array();
if ( $types ) {
foreach ( $types as $type ) {
if ( has_shortcode( $content, $type ) ) {
$found[] = $type;
}
}
}

// If none of the shortcodes-of-interest are found, bail.
if ( empty( $found ) ) {
return $gallery_content;
}

$galleries = array();

if ( ! preg_match_all( '/' . get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER ) ) {
return $gallery_content;
}

// Collect the shortcodes and their attributes.
foreach ( $found as $type ) {
foreach ( $matches as $shortcode ) {
if ( $type === $shortcode[2] ) {

$attributes = shortcode_parse_atts( $shortcode[3] );

if ( '' === $attributes ) { // Valid shortcode without any attributes.
$attributes = array();
}

$galleries[ $shortcode[2] ] = $attributes;
}
}
}

// Recreate the shortcodes and then render them to get the HTML content.
if ( $galleries ) {
foreach ( $galleries as $shortcode => $attributes ) {
$code = '[' . $shortcode;
foreach ( $attributes as $key => $value ) {
$code .= " $key=$value";
}
$code .= ']';
$gallery_content .= do_shortcode( $code );
}
}

return $gallery_content;
function clean_url( $url ) {
// remove the query string.
$url = strtok( $url, '?' );
// make the url XML-safe.
$url = htmlspecialchars( $url );
// Make the url absolute, if its relative.
$url = aiosp_common::absolutize_url( $url );
return apply_filters( 'aioseop_clean_url', $url );
}

/**
Expand Down
73 changes: 0 additions & 73 deletions tests/modules/opengraph/test-meta.php

This file was deleted.

0 comments on commit 48716fb

Please sign in to comment.