__( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } $response = ABPW_Api_Client::get( '/posts' ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } // Fetch usage data from account endpoint. $usage = array(); $account = ABPW_Api_Client::get( '/account' ); if ( ! is_wp_error( $account ) && isset( $account['usage'] ) ) { $usage = $account['usage']; } wp_send_json_success( array( 'posts' => isset( $response['posts'] ) ? $response['posts'] : array(), 'usage' => $usage, ) ); } /** * Handle generating a post from an approved topic via AJAX. */ public function handle_generate_post() { check_ajax_referer( 'abpw_posts_nonce', 'abpw_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } $topic_id = isset( $_POST['topic_id'] ) ? sanitize_text_field( wp_unslash( $_POST['topic_id'] ) ) : ''; if ( empty( $topic_id ) ) { wp_send_json_error( array( 'message' => __( 'Please select a topic.', 'abp-writer-ai-blog-generator' ), ) ); } $response = ABPW_Api_Client::post( '/posts/generate', array( 'topic_id' => $topic_id ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } // Increment generated post counter for review prompt. $gen_count = (int) get_option( 'aiblogpress_posts_generated_count', 0 ); update_option( 'aiblogpress_posts_generated_count', $gen_count + 1 ); wp_send_json_success( array( 'post' => isset( $response['post'] ) ? $response['post'] : $response, ) ); } /** * Handle getting a single post (for polling and preview) via AJAX. */ public function handle_get_post() { check_ajax_referer( 'abpw_posts_nonce', 'abpw_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : ''; if ( empty( $post_id ) ) { wp_send_json_error( array( 'message' => __( 'Post ID is required.', 'abp-writer-ai-blog-generator' ), ) ); } $response = ABPW_Api_Client::get( '/posts/' . $post_id ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } wp_send_json_success( array( 'post' => isset( $response['post'] ) ? $response['post'] : $response, ) ); } /** * Handle loading approved topics for the topic selector via AJAX. */ public function handle_load_approved_topics() { check_ajax_referer( 'abpw_posts_nonce', 'abpw_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } $response = ABPW_Api_Client::get( '/topics', array( 'status' => 'approved', 'limit' => 100, ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } wp_send_json_success( array( 'topics' => isset( $response['topics'] ) ? $response['topics'] : array(), ) ); } /** * Handle publishing a post to WordPress via wp_insert_post. */ public function handle_publish_post() { check_ajax_referer( 'abpw_posts_nonce', 'abpw_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : ''; if ( empty( $post_id ) ) { wp_send_json_error( array( 'message' => __( 'Post ID is required.', 'abp-writer-ai-blog-generator' ), ) ); } // Fetch the full post from the API. $response = ABPW_Api_Client::get( '/posts/' . $post_id ); if ( is_wp_error( $response ) ) { wp_send_json_error( array( 'message' => $response->get_error_message(), ) ); } $post = isset( $response['post'] ) ? $response['post'] : $response; if ( empty( $post['title'] ) || empty( $post['content'] ) ) { wp_send_json_error( array( 'message' => __( 'Post data is incomplete. Cannot publish.', 'abp-writer-ai-blog-generator' ), ) ); } // Create the WordPress post. $wp_post_data = array( 'post_title' => sanitize_text_field( $post['title'] ), 'post_content' => wp_kses_post( $post['content'] ), 'post_status' => 'publish', 'post_type' => 'post', 'post_name' => sanitize_title( isset( $post['slug'] ) ? $post['slug'] : $post['title'] ), ); $wp_post_id = wp_insert_post( $wp_post_data, true ); if ( is_wp_error( $wp_post_id ) ) { wp_send_json_error( array( 'message' => $wp_post_id->get_error_message(), ) ); } // Download and set featured image if available. if ( ! empty( $post['featured_image_url'] ) ) { require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $tmp_file = download_url( $post['featured_image_url'] ); if ( is_wp_error( $tmp_file ) ) { error_log( 'AiBlogPress: Failed to download featured image from ' . $post['featured_image_url'] . ' — ' . $tmp_file->get_error_message() ); } else { $filename = wp_basename( wp_parse_url( $post['featured_image_url'], PHP_URL_PATH ) ); if ( empty( $filename ) || false === strpos( $filename, '.' ) ) { $filename = 'abp-writer-featured.jpg'; } $file_array = array( 'name' => $filename, 'tmp_name' => $tmp_file, ); $attachment_id = media_handle_sideload( $file_array, $wp_post_id, sanitize_text_field( $post['title'] ) ); if ( is_wp_error( $attachment_id ) ) { @unlink( $file_array['tmp_name'] ); error_log( 'AiBlogPress: Failed to sideload featured image — ' . $attachment_id->get_error_message() ); } else { set_post_thumbnail( $wp_post_id, $attachment_id ); } } } // Increment published post counter for review prompt. $count = (int) get_option( 'aiblogpress_published_count', 0 ); update_option( 'aiblogpress_published_count', $count + 1 ); // Set SEO meta description if Yoast SEO is active. if ( ! empty( $post['meta_description'] ) ) { if ( defined( 'WPSEO_VERSION' ) ) { update_post_meta( $wp_post_id, '_yoast_wpseo_metadesc', sanitize_text_field( $post['meta_description'] ) ); } // Set SEO meta description if Rank Math is active. if ( class_exists( 'RankMath' ) ) { update_post_meta( $wp_post_id, 'rank_math_description', sanitize_text_field( $post['meta_description'] ) ); } } // Update post status to published on the API server. ABPW_Api_Client::patch( '/posts/' . $post_id, array( 'status' => 'published', 'wp_post_id' => $wp_post_id, ) ); wp_send_json_success( array( 'wp_post_id' => $wp_post_id, 'edit_url' => get_edit_post_link( $wp_post_id, 'raw' ), 'view_url' => get_permalink( $wp_post_id ), ) ); } }