Skip to content

Commit

Permalink
Fix PHP 7.1 Warning: illegal string offset 'src' (#6118)
Browse files Browse the repository at this point in the history
* Simple fix for warning about illegal string offset

* Simplify how image array is built from post/author data

* Fix typo in 51e8a8f

* Replace direct use of `$post` with Core helpers

Cleans up the function to remove a global declared inside a conditional, where it could be overlooked. Also, helper functions FTW!
  • Loading branch information
ethitter authored and samhotchkiss committed Jan 26, 2017
1 parent 14e9813 commit 9621690
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions functions.opengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,18 @@ function jetpack_og_tags() {
}

function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 200x200
$image = '';
$image = array();

if ( is_singular() && ! is_home() ) {
global $post;
$image = '';

// Grab obvious image if $post is an attachment page for an image
if ( is_attachment( $post->ID ) && 'image' == substr( $post->post_mime_type, 0, 5 ) ) {
$image = wp_get_attachment_url( $post->ID );
// Grab obvious image if post is an attachment page for an image
if ( is_attachment( get_the_ID() ) && 'image' == substr( get_post_mime_type(), 0, 5 ) ) {
$image['src'] = wp_get_attachment_url( get_the_ID() );
}

// Attempt to find something good for this post using our generalized PostImages code
if ( ! $image && class_exists( 'Jetpack_PostImages' ) ) {
$post_images = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) );
if ( empty( $image ) && class_exists( 'Jetpack_PostImages' ) ) {
$post_images = Jetpack_PostImages::get_images( get_the_ID(), array( 'width' => $width, 'height' => $height ) );
if ( $post_images && ! is_wp_error( $post_images ) ) {
$image = array();
foreach ( (array) $post_images as $post_image ) {
$image['src'] = $post_image['src'];
if ( isset( $post_image['src_width'], $post_image['src_height'] ) ) {
Expand Down Expand Up @@ -309,14 +305,6 @@ function jetpack_og_get_image( $width = 200, $height = 200, $max_images = 4 ) {
}
}

if ( empty( $image ) ) {
$image = array();
} else if ( ! is_array( $image ) ) {
$image = array(
'src' => $image
);
}

// First fall back, blavatar
if ( empty( $image ) && function_exists( 'blavatar_domain' ) ) {
$blavatar_domain = blavatar_domain( site_url() );
Expand Down

0 comments on commit 9621690

Please sign in to comment.