Support » Plugin: Simple Image Sizes » Two bugs in the attachment queries

  • Resolved kitchin


    The queries in simple-image-sizes/classes/admin/media.php (Version 3.0) have a few bugs:

    1. Line 395

    $attachments = get_children( array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'numberposts' => -1,
    	'post_status' => null,
    	'post_parent' => null, // any parent
    	'output' => 'ids',
    ) );

    I have a couple of notes about this one, so I’ll break them down:

    1a. The first parameter argument 'output' => 'ids', does nothing. There’s a second parameter called output but it doesn’t work that way either. So this function returns a potentially very large array of full posts.

    1b. The actual sql query generated is:

    SELECT wp_posts.* FROM wp_posts  WHERE 1=1
    AND (wp_posts.post_mime_type LIKE 'image/%')
    AND wp_posts.post_type = 'attachment'
    AND ((wp_posts.post_status = 'inherit'))
    ORDER BY wp_posts.post_date DESC

    1c. I suggest this replacement at line 395, which is similar to your other counting query:

    $attachments = $wpdb->get_var( "SELECT COUNT( ID )
    FROM $wpdb->posts
    WHERE 1 = 1
    AND post_type = 'attachment'
    AND post_status = 'inherit'
    $whichmimetype" );

    2. Line 405
    There’s a real bug here, affecting the post_types branch.

    // Return the Id's and Title of medias
    SIS_Admin_Main::displayJson( array( 'total' => count( $attachments ) ) );

    I suggest:

    // Return the Id's of medias
    SIS_Admin_Main::displayJson( array( 'total' => $attachments ) );

    The problem is that when the query was $attachments = get_var( "SELECT COUNT( ID )..., then count( $attachments ) is always 1, because it’s not an array. Your code has that kind of query in the post_types branch (and my suggested code has it in the other branch).

    3. Line 462
    The query here has
    post_status' => 'any'
    and generates this sql query:

    SELECT wp_posts.ID FROM wp_posts  WHERE 1=1
    AND (wp_posts.post_mime_type LIKE 'image/%')
    AND wp_posts.post_type = 'attachment'
    AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft'))
    ORDER BY wp_posts.post_date DESC LIMIT 1, 1

    You can see the ‘post_status’ clause does not exactly match the above stuff in #1b, when the code was making a count. So I’d suggest for consistency at line 462:
    post_status' => 'inherit'

    So in summary:
    1. efficiency for non-post_types branch
    2. real bug for post_types branch
    3. consistency for non-post_types branch


Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Two bugs in the attachment queries’ is closed to new replies.