Skip to content

Commit

Permalink
Clean things up a bit and make a little more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Sep 9, 2015
1 parent 718a122 commit fddb1d7
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions includes/CMB2_Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,43 @@ class CMB2_Utils {
public function image_id_from_url( $img_url ) {
$attachment_id = 0;
$dir = wp_upload_dir();
if ( false !== strpos( $img_url, $dir['baseurl'] . '/' ) ) { // Is URL in uploads directory?
$file = basename( $img_url );
$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
array(
'value' => $file,
'compare' => 'LIKE',
'key' => '_wp_attachment_metadata',
),
)
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $post_id ) {
$meta = wp_get_attachment_metadata( $post_id );
$original_file = basename( $meta['file'] );
$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
$attachment_id = $post_id;
break;
}

// Is URL in uploads directory?
if ( false === strpos( $img_url, $dir['baseurl'] . '/' ) ) {
return false;
}

$file = basename( $img_url );

$query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'meta_query' => array(
array(
'value' => $file,
'compare' => 'LIKE',
'key' => '_wp_attachment_metadata',
),
)
);

$query = new WP_Query( $query_args );

if ( $query->have_posts() ) {

foreach ( $query->posts as $post_id ) {
$meta = wp_get_attachment_metadata( $post_id );
$original_file = basename( $meta['file'] );
$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
$attachment_id = $post_id;
break;
}
}

}

return 0 === $attachment_id ? false : $attachment_id;
}

Expand Down

0 comments on commit fddb1d7

Please sign in to comment.