min_width = $min_width; $this->min_height = $min_height; } /** * Find first image in post content * * @param int $post_id Post ID. * @return array|null Image data or null if not found. */ public function find_first_image( $post_id ) { $post = get_post( $post_id ); if ( ! $post || empty( $post->post_content ) ) { return null; } // Process shortcodes before searching for images $content = do_shortcode( $post->post_content ); // Find all images in content $images = $this->find_all_images( $content ); if ( empty( $images ) ) { return null; } // Return first suitable image return $images[0]; } /** * Find all images in content * * @param string $content Post content. * @return array Array of image data. */ private function find_all_images( $content ) { $images = array(); $matches = array(); // Get all img tags using regex preg_match_all( '/<\s*img\s+.*?src\s*=\s*[\"\']?([^\"\'> ]*).*?>/i', $content, $matches ); if ( empty( $matches[0] ) ) { return $images; } foreach ( $matches[0] as $key => $img_tag ) { $url = $matches[1][ $key ] ?? ''; if ( empty( $url ) ) { continue; } // Extract title attribute if exists $title = ''; $title_matches = array(); preg_match_all( '/<\s*img\s+[^\>]*title\s*=\s*[\"\']?([^\"\'> ]*)/i', $img_tag, $title_matches ); if ( ! empty( $title_matches[1][0] ) ) { $title = $title_matches[1][0]; } $images[] = array( 'tag' => $img_tag, 'url' => $url, 'title' => $title, ); } return $images; } /** * Get thumbnail ID from image data * * @param array $image Image data with 'tag' and 'url'. * @return int|false Attachment ID or false. */ public function get_thumbnail_id( $image ) { global $wpdb; $thumb_id = 0; // First, check if there's a wp-image-{ID} class in the tag // WordPress automatically adds this class for media library images if ( isset( $image['tag'] ) && ! empty( $image['tag'] ) ) { $class_matches = array(); preg_match( '/wp-image-([\d]+)/i', $image['tag'], $class_matches ); if ( ! empty( $class_matches[1] ) ) { $thumb_id = (int) $class_matches[1]; // Check if this attachment exists if ( get_post( $thumb_id ) ) { return $thumb_id; } $thumb_id = 0; } } // If not found by class, search by URL in database if ( ! $thumb_id && isset( $image['url'] ) && ! empty( $image['url'] ) ) { $image_url = $image['url']; // Remove size suffixes from URL (-150x150, -300x200, etc.) // to find the original image $image_url = preg_replace( '/-[0-9]{1,4}x[0-9]{1,4}\./i', '.', $image_url ); // Try to get from cache $cache_key = 'aitm_img_' . md5( $image_url ); $thumb_id = wp_cache_get( $cache_key, 'aitm' ); if ( false === $thumb_id ) { // Search in database by guid $thumb_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE guid LIKE %s AND post_type = 'attachment'", '%' . $wpdb->esc_like( $image_url ) . '%' ) ); // Cache for 1 hour if ( $thumb_id ) { wp_cache_set( $cache_key, $thumb_id, 'aitm', 3600 ); } } if ( $thumb_id ) { return (int) $thumb_id; } } return false; } /** * Check if image meets size requirements * * @param int $attachment_id Attachment ID. * @return bool True if meets requirements. */ public function check_image_size( $attachment_id ) { // If no requirements set, image is suitable if ( $this->min_width <= 0 && $this->min_height <= 0 ) { return true; } $metadata = wp_get_attachment_metadata( $attachment_id ); if ( empty( $metadata ) || ! isset( $metadata['width'], $metadata['height'] ) ) { return false; } $width = $metadata['width']; $height = $metadata['height']; if ( $this->min_width > 0 && $width < $this->min_width ) { return false; } if ( $this->min_height > 0 && $height < $this->min_height ) { return false; } return true; } }