mindkomm / timmy

Advanced image handling for Timber.
MIT License
171 stars 13 forks source link

PHP warning in timber_should_resize #39

Closed therealgilles closed 3 years ago

therealgilles commented 3 years ago

I am getting a PHP warning in the following function on the line marked '<-- HERE' because $parent is empty/null:

  private function timber_should_resize( $attachment_parent_id, $img_size ) {
    /**
     * Normally we don’t have a post type associated with the attachment
     *
     * We use an empty array to tell the function that there is no post type
     * associated with the attachment.
     */
    $attachment_post_type = array( '' );

    // Check if image is attached to a post and sort out post type.
    if ( 0 !== $attachment_parent_id ) {
      $parent               = get_post( $attachment_parent_id );
      $attachment_post_type = array( $parent->post_type ); // <-- HERE
    }

    return self::is_size_for_post_type( $img_size, $attachment_post_type );
  }

I'm not sure exactly why the attachment parent ID is invalid for the photo. The image is used in an ACF image field, so it's not attached to a particular post. When I try to find the post corrresponding to the attachment parent ID, WordPress tells me that maybe it was deleted.

therealgilles commented 3 years ago

Maybe this fix would do:

      if ( ! empty( $parent ) ) {
        $attachment_post_type = array( $parent->post_type );
      } 
gchtr commented 3 years ago

Hey @therealgilles, thanks for your issue and for taking the time to hunt down the problem. I fixed the problem in https://github.com/mindkomm/timmy/commit/43cb35b4766da25b7c2adfbe997e03fe047eead7 and released the fix in a new version.