/* |-------------------------------------------------------------------------- | Generate Images From Article Content |-------------------------------------------------------------------------- */ add_action( 'wp_ajax_aigp_generate_content_images', 'aigp_generate_content_images' ); function aigp_generate_content_images(){ check_ajax_referer( 'aiig_nonce', 'nonce' ); if(!current_user_can('edit_posts')){ wp_send_json_error( 'Permission denied' ); } $post_id = intval( $_POST['post_id'] ); if(!$post_id){ wp_send_json_error( 'Post ID missing' ); } $content = get_post_field( 'post_content', $post_id ); /* |-------------------------------------------------------------------------- | Find Image Prompts |-------------------------------------------------------------------------- */ preg_match_all( '/\[image\](.*?)\[\/image\]/is', wp_strip_all_tags($content), $matches ); if(empty($matches[1])){ wp_send_json_error( 'No image prompts found' ); } $generated = 0; foreach($matches[1] as $prompt){ $prompt = trim($prompt); if(empty($prompt)){ continue; } /* |-------------------------------------------------------------------------- | Generate Image |-------------------------------------------------------------------------- */ $image_url = aigp_create_article_image( $prompt ); if(!$image_url){ continue; } /* |-------------------------------------------------------------------------- | Replace Prompt With Image |-------------------------------------------------------------------------- */ $image_html = sprintf( '

%s

', esc_url($image_url), esc_attr($prompt) ); $content = preg_replace( '/\[image\]\s*'.preg_quote($prompt,'/').'\s*\[\/image\]/is', $image_html, $content, 1 ); $generated++; } /* |-------------------------------------------------------------------------- | Update Post |-------------------------------------------------------------------------- */ wp_update_post(array( 'ID'=>$post_id, 'post_content'=>$content )); wp_send_json_success(array( 'message'=>$generated.' images generated successfully' )); } /* |-------------------------------------------------------------------------- | Create Single Article Image |-------------------------------------------------------------------------- */ function aigp_create_article_image($prompt){ $worker_url = get_option( 'aiig_worker_url' ); $secret_key = get_option( 'aiig_secret_key' ); if(empty($worker_url)){ return false; } $seed = wp_rand( 1, 999999999 ); $response = wp_remote_post( $worker_url, array( 'timeout'=>120, 'headers'=>array( 'Authorization'=>$secret_key, 'Content-Type'=>'application/json' ), 'body'=>wp_json_encode(array( 'prompt'=>$prompt, 'seed'=>$seed )) ) ); if(is_wp_error($response)){ return false; } $data = json_decode( wp_remote_retrieve_body($response), true ); if(empty($data['image'])){ return false; } $image_data = base64_decode( $data['image'], true ); if(!$image_data){ return false; } /* |-------------------------------------------------------------------------- | Save Image |-------------------------------------------------------------------------- */ $upload = wp_upload_dir(); $image_name = sanitize_title( wp_trim_words( $prompt, 8, '' ) ); if(empty($image_name)){ $image_name='ai-image'; } $filename = $image_name .'-' .wp_rand(1000,9999) .'.png'; $file = $upload['path'].'/'.$filename; file_put_contents( $file, $image_data ); if(!getimagesize($file)){ unlink($file); return false; } /* |-------------------------------------------------------------------------- | Convert WebP |-------------------------------------------------------------------------- */ if(function_exists('aiig_convert_to_webp')){ $webp = aiig_convert_to_webp( $file ); if(file_exists($webp)){ $file=$webp; } } require_once ABSPATH . 'wp-admin/includes/image.php'; $file_name = basename($file); $file_type = wp_check_filetype( $file_name ); $attachment=array( 'post_mime_type'=>$file_type['type'], 'post_title'=>sanitize_text_field($prompt), 'post_status'=>'inherit' ); $attachment_id = wp_insert_attachment( $attachment, $file ); if(!$attachment_id){ return false; } $metadata = wp_generate_attachment_metadata( $attachment_id, $file ); wp_update_attachment_metadata( $attachment_id, $metadata ); return wp_get_attachment_url( $attachment_id ); }jQuery(function($){ $('#aigp_generate_content_images').click(function(){ let button = $(this); button.prop( 'disabled', true ); $('#aigp_content_result').html( '⏳ Generating images, please wait...' ); $.ajax({ url:aigp_content_ajax.url, type:'POST', data:{ action:'aigp_generate_content_images', nonce:aigp_content_ajax.nonce, post_id:$('#post_ID').val() }, success:function(response){ if(response.success){ $('#aigp_content_result').html( '✅ '+response.data.message ); setTimeout(function(){ location.reload(); },2000); }else{ $('#aigp_content_result').html( '❌ '+response.data ); button.prop( 'disabled', false ); } }, error:function(){ $('#aigp_content_result').html( '❌ Server error' ); button.prop( 'disabled', false ); } }); }); }); Earthy Home Decor » Best Collections