/* |-------------------------------------------------------------------------- | 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( '