settings = $settings; $generation_method = $settings['generation_method'] ?? 'first_content_image'; if ( $generation_method === 'ai_generation' ) { require_once AITM_PLUGIN_DIR . '/includes/class-ai-image-generator.php'; try { $this->ai_generator = new AITM_AI_Image_Generator( $settings ); } catch ( Exception $e ) { // If AI generator fails to initialize, fall back to content image finder $min_width = $settings['min_image_width'] ?? 0; $min_height = $settings['min_image_height'] ?? 0; $this->image_finder = new AITM_Content_Image_Finder( $min_width, $min_height ); } } else { $min_width = $settings['min_image_width'] ?? 0; $min_height = $settings['min_image_height'] ?? 0; $this->image_finder = new AITM_Content_Image_Finder( $min_width, $min_height ); } } /** * Generate thumbnail for post * * @param int $post_id Post ID. * @return array Result data. */ public function generate_for_post( $post_id ) { $post = get_post( $post_id ); if ( ! $post ) { return array( 'success' => false, 'message' => __( 'Post not found', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => '', ); } // Check if post already has thumbnail $overwrite_existing = $this->settings['overwrite_existing'] ?? false; if ( ! $overwrite_existing && has_post_thumbnail( $post_id ) ) { return array( 'success' => false, 'message' => __( 'Post already has thumbnail', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, ); } // Choose generation method $generation_method = $this->settings['generation_method'] ?? 'first_content_image'; switch ( $generation_method ) { case 'ai_generation': if ( isset( $this->ai_generator ) ) { return $this->generate_with_ai( $post_id, $post ); } break; case 'generate_from_title': return $this->generate_from_title( $post_id, $post ); case 'search_or_generate': $result = $this->generate_from_content( $post_id, $post ); if ( ! $result['success'] ) { return $this->generate_from_title( $post_id, $post ); } return $result; case 'use_default_image': $result = $this->generate_from_content( $post_id, $post ); if ( ! $result['success'] ) { return $this->use_default_image( $post_id, $post ); } return $result; } // Fallback to default method return $this->generate_from_content( $post_id, $post ); } /** * Generate thumbnail from content image * * @param int $post_id Post ID. * @param WP_Post $post Post object. * @return array Result data. */ private function generate_from_content( $post_id, $post ) { // Find image in content $image = $this->image_finder->find_first_image( $post_id ); if ( ! $image ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'No suitable image found in content', 'ai-thumbnails-maker' ), 'type' => 'first_content_image', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'No suitable image found in content', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'first_content_image', ); } // Get attachment ID $attachment_id = $this->image_finder->get_thumbnail_id( $image ); if ( ! $attachment_id ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Image not found in media library', 'ai-thumbnails-maker' ), 'type' => 'first_content_image', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Image not found in media library', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'first_content_image', ); } // Check image size requirements if ( ! $this->image_finder->check_image_size( $attachment_id ) ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Image does not meet size requirements', 'ai-thumbnails-maker' ), 'type' => 'first_content_image', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Image does not meet size requirements', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'first_content_image', ); } // Set as featured image // If overwrite_existing is enabled, delete current thumbnail first if ( $overwrite_existing && has_post_thumbnail( $post_id ) ) { delete_post_thumbnail( $post_id ); } $result = set_post_thumbnail( $post_id, $attachment_id ); if ( $result || get_post_thumbnail_id( $post_id ) == $attachment_id ) { $metadata = wp_get_attachment_metadata( $attachment_id ); $image_size = ''; if ( isset( $metadata['width'], $metadata['height'] ) ) { $width = $metadata['width']; $height = $metadata['height']; // Get file size $file_path = get_attached_file( $attachment_id ); $file_size = ''; if ( $file_path && file_exists( $file_path ) ) { $size_bytes = filesize( $file_path ); $file_size = size_format( $size_bytes, 2 ); } $image_size = $width . 'x' . $height; if ( $file_size ) { $image_size .= ' (' . $file_size . ')'; } } $thumbnail_url = wp_get_attachment_image_url( $attachment_id, 'thumbnail' ); $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => true, 'message' => __( 'Thumbnail generated successfully', 'ai-thumbnails-maker' ), 'type' => 'first_content_image', 'image_size' => $image_size, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $attachment_id, ) ); return array( 'success' => true, 'message' => __( 'Thumbnail generated successfully', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $attachment_id, 'image_size' => $image_size, 'type' => 'first_content_image', ); } $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Failed to set thumbnail', 'ai-thumbnails-maker' ), 'type' => 'first_content_image', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Failed to set thumbnail', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'first_content_image', ); } /** * Generate thumbnail using AI * * @param int $post_id Post ID. * @param WP_Post $post Post object. * @return array Result data. */ private function generate_with_ai( $post_id, $post ) { try { $attachment_id = $this->ai_generator->generate_for_post( $post_id ); if ( ! $attachment_id ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Failed to generate AI image', 'ai-thumbnails-maker' ), 'type' => 'ai_generation', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Failed to generate AI image', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'ai_generation', ); } // Get image metadata $metadata = wp_get_attachment_metadata( $attachment_id ); $image_size = ''; if ( isset( $metadata['width'], $metadata['height'] ) ) { $width = $metadata['width']; $height = $metadata['height']; // Get file size $file_path = get_attached_file( $attachment_id ); $file_size = ''; if ( $file_path && file_exists( $file_path ) ) { $size_bytes = filesize( $file_path ); $file_size = size_format( $size_bytes, 2 ); } $image_size = $width . 'x' . $height; if ( $file_size ) { $image_size .= ' (' . $file_size . ')'; } } // Set thumbnail $overwrite_existing = $this->settings['overwrite_existing'] ?? false; if ( $overwrite_existing && has_post_thumbnail( $post_id ) ) { delete_post_thumbnail( $post_id ); } $result = set_post_thumbnail( $post_id, $attachment_id ); if ( $result || get_post_thumbnail_id( $post_id ) == $attachment_id ) { $thumbnail_url = wp_get_attachment_image_url( $attachment_id, 'thumbnail' ); $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => true, 'message' => __( 'AI image generated successfully', 'ai-thumbnails-maker' ), 'type' => 'ai_generation', 'image_size' => $image_size, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $attachment_id, ) ); return array( 'success' => true, 'message' => __( 'AI image generated successfully', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $attachment_id, 'image_size' => $image_size, 'type' => 'ai_generation', ); } else { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Failed to set thumbnail', 'ai-thumbnails-maker' ), 'type' => 'ai_generation', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Failed to set thumbnail', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'ai_generation', ); } } catch ( Exception $e ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => $e->getMessage(), 'type' => 'ai_generation', 'image_size' => '', ) ); return array( 'success' => false, 'message' => $e->getMessage(), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'ai_generation', ); } } /** * Add entry to generation log * * @param array $log_entry Log entry data. */ private function add_to_log( $log_entry ) { // Get log from separate option for better reliability $log = get_option( 'aitm_generation_log', array() ); // Add timestamp $log_entry['timestamp'] = current_time( 'mysql' ); // Add to the beginning of array array_unshift( $log, $log_entry ); // Limit log size to 100 entries $log = array_slice( $log, 0, 100 ); // Save updated log to separate option update_option( 'aitm_generation_log', $log, false ); } /** * Generate thumbnail from title * * @param int $post_id Post ID. * @param WP_Post $post Post object. * @return array Result data. */ private function generate_from_title( $post_id, $post ) { require_once AITM_PLUGIN_DIR . '/includes/class-title-image-generator.php'; try { $title_generator = new AITM_Title_Image_Generator( $this->settings ); $attachment_id = $title_generator->generate_for_post( $post_id ); if ( ! $attachment_id ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Failed to generate title image', 'ai-thumbnails-maker' ), 'type' => 'generate_from_title', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Failed to generate title image', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'generate_from_title', ); } // Get image metadata $metadata = wp_get_attachment_metadata( $attachment_id ); $image_size = ''; if ( isset( $metadata['width'], $metadata['height'] ) ) { $width = $metadata['width']; $height = $metadata['height']; // Get file size $file_path = get_attached_file( $attachment_id ); $file_size = ''; if ( $file_path && file_exists( $file_path ) ) { $size_bytes = filesize( $file_path ); $file_size = size_format( $size_bytes, 2 ); } $image_size = $width . 'x' . $height; if ( $file_size ) { $image_size .= ' (' . $file_size . ')'; } } // Set thumbnail $overwrite_existing = $this->settings['overwrite_existing'] ?? false; if ( $overwrite_existing && has_post_thumbnail( $post_id ) ) { delete_post_thumbnail( $post_id ); } $result = set_post_thumbnail( $post_id, $attachment_id ); if ( $result || get_post_thumbnail_id( $post_id ) == $attachment_id ) { $thumbnail_url = wp_get_attachment_image_url( $attachment_id, 'thumbnail' ); $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => true, 'message' => __( 'Title image generated successfully', 'ai-thumbnails-maker' ), 'type' => 'generate_from_title', 'image_size' => $image_size, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $attachment_id, ) ); return array( 'success' => true, 'message' => __( 'Title image generated successfully', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $attachment_id, 'image_size' => $image_size, 'type' => 'generate_from_title', ); } $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Failed to set thumbnail', 'ai-thumbnails-maker' ), 'type' => 'generate_from_title', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Failed to set thumbnail', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'generate_from_title', ); } catch ( Exception $e ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => $e->getMessage(), 'type' => 'generate_from_title', 'image_size' => '', ) ); return array( 'success' => false, 'message' => $e->getMessage(), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'generate_from_title', ); } } /** * Use default image as thumbnail * * @param int $post_id Post ID. * @param WP_Post $post Post object. * @return array Result data. */ private function use_default_image( $post_id, $post ) { $default_image_id = $this->settings['default_image_id'] ?? 0; if ( ! $default_image_id || ! wp_attachment_is_image( $default_image_id ) ) { $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'No default image configured or image not found', 'ai-thumbnails-maker' ), 'type' => 'use_default_image', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'No default image configured or image not found', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'use_default_image', ); } // Get image metadata $metadata = wp_get_attachment_metadata( $default_image_id ); $image_size = ''; if ( isset( $metadata['width'], $metadata['height'] ) ) { $width = $metadata['width']; $height = $metadata['height']; // Get file size $file_path = get_attached_file( $default_image_id ); $file_size = ''; if ( $file_path && file_exists( $file_path ) ) { $size_bytes = filesize( $file_path ); $file_size = size_format( $size_bytes, 2 ); } $image_size = $width . 'x' . $height; if ( $file_size ) { $image_size .= ' (' . $file_size . ')'; } } // Set thumbnail $overwrite_existing = $this->settings['overwrite_existing'] ?? false; if ( $overwrite_existing && has_post_thumbnail( $post_id ) ) { delete_post_thumbnail( $post_id ); } $result = set_post_thumbnail( $post_id, $default_image_id ); if ( $result || get_post_thumbnail_id( $post_id ) == $default_image_id ) { $thumbnail_url = wp_get_attachment_image_url( $default_image_id, 'thumbnail' ); $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => true, 'message' => __( 'Default image set successfully', 'ai-thumbnails-maker' ), 'type' => 'use_default_image', 'image_size' => $image_size, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $default_image_id, ) ); return array( 'success' => true, 'message' => __( 'Default image set successfully', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'thumbnail_url' => $thumbnail_url, 'thumbnail_id' => $default_image_id, 'image_size' => $image_size, 'type' => 'use_default_image', ); } $this->add_to_log( array( 'post_id' => $post_id, 'post_title' => $post->post_title, 'success' => false, 'message' => __( 'Failed to set default image', 'ai-thumbnails-maker' ), 'type' => 'use_default_image', 'image_size' => '', ) ); return array( 'success' => false, 'message' => __( 'Failed to set default image', 'ai-thumbnails-maker' ), 'post_id' => $post_id, 'post_title' => $post->post_title, 'type' => 'use_default_image', ); } /** * Get statistics * * @param string $post_type Post type. * @return array Statistics data. */ public function get_statistics( $post_type = 'post' ) { // Посты с превью $with_thumbnail = new WP_Query( array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS', ), ), ) ); // Посты без превью $without_thumbnail = new WP_Query( array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => array( array( 'key' => '_thumbnail_id', 'compare' => 'NOT EXISTS', ), ), ) ); $with_count = $with_thumbnail->found_posts; $without_count = $without_thumbnail->found_posts; $total = $with_count + $without_count; $percentage = $total > 0 ? round( ( $with_count / $total ) * 100 ) : 0; return array( 'with_thumbnail' => $with_count, 'without_thumbnail' => $without_count, 'total' => $total, 'percentage' => $percentage, ); } }