-1, //'post_type' => array( 'post', 'page', 'attachment' ), 'post_type' => array( 'post', 'page' ), 'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private' ), 'order' => 'ASC', 'orderby' => 'title' ); $content_options = array(); $posts = get_posts( $args ); foreach ( $posts as $post ) { if ( empty( $content_options[ $post->post_type ] ) ) { $content_options[ $post->post_type ] = array(); } $content_options[ $post->post_type ][] = $post; } $content_options = apply_filters( 'ewd_ufaq_ai_faqs_content_options', $content_options ); ?>
settings->get_setting( 'access-role' ) ) ) { ewdufaqHelper::admin_nopriv_ajax(); } if ( ! is_array( json_decode( stripslashes_deep( $_POST['selected_posts'] ), true ) ) ) { wp_send_json_error( array( 'error' => 'No content has been selected', ) ); } $selected_posts = array_map( 'intval', json_decode( stripslashes_deep( $_POST['selected_posts'] ), true ) ); $faq_count = min( intval( $_POST['faq_count'] ), ( $ewd_ufaq_controller->permissions->check_permission( 'premium' ) ? 999 : 1 ) ); $set_categories = $_POST['set_categories'] == 'yes' ? true : false; $selected_posts = $ewd_ufaq_controller->permissions->check_permission( 'premium' ) ? $selected_posts : array( reset( $selected_posts ) ); $args = array( 'posts_per_page' => -1, 'post__in' => $selected_posts, ); $content = ''; $posts = get_posts( $args ); foreach ( $posts as $post ) { $content .= wp_strip_all_tags( $post->post_content ) . ' '; } $categories = ''; if ( $set_categories ) { $args = array( 'taxonomy' => EWD_UFAQ_FAQ_CATEGORY_TAXONOMY, 'hide_empty' => false, ); $category_terms = get_terms( $args ); foreach ( $category_terms as $category_term ) { $categories .= $category_term->name . ','; } $categories = trim( $categories, ',' ); } $url = add_query_arg( array( 'faq_count' => $faq_count, 'categories' => $categories, ), 'https://etoilewebdesign.com/open-ai/open-ai-client.php' ); $args = array( 'body' => $content, 'timeout' => 30, ); $response = wp_remote_post( $url, $args ); if ( ! is_array( $response ) or is_wp_error( $response ) ) { wp_send_json_error( array( 'error' => 'Invalid response received', ) ); } $body = json_decode( $response['body'], true ); if ( empty( $body['choices'][0]['message']['content'] ) ) { wp_send_json_error( array( 'error' => 'JSON decode failed', ) ); } $message_content = $this->parseCSVFromString( $body['choices'][0]['message']['content'] ); $faq_content = array(); $increment = $set_categories ? 3 : 2; foreach ( $message_content as $created_faq ) { $faq = array( 'title' => $created_faq['Question'], 'content' => $created_faq['Answer'], ); if ( $set_categories ) { $faq_categories = array(); $category_names = explode( ',', $created_faq['Categories'] ); foreach ( $category_names as $category_name ) { foreach ( $category_terms as $category_term ) { if ( $category_term->name != $category_name ) { continue; } $faq_categories[] = array( $category_term->term_id => $category_name ); } } $faq['categories'] = $faq_categories; } $faq_content[] = $faq; } wp_send_json_success( array( 'faqs' => $faq_content ) ); } /** * Turn the AI generated FAQs into posts, after being edited and * approved by the admin * * @since 2.3.0 */ public function publish_faqs() { global $ewd_ufaq_controller; if ( ! check_ajax_referer( 'ewd-ufaq-admin-js', 'nonce' ) || ! current_user_can( $ewd_ufaq_controller->settings->get_setting( 'access-role' ) ) ) { ewdufaqHelper::admin_nopriv_ajax(); } if ( ! is_array( json_decode( stripslashes_deep( $_POST['faqs'] ), true ) ) ) { wp_send_json_error( array( 'error' => 'No faqs to be created', ) ); } $faqs = $this->sanitize_faqs( json_decode( stripslashes_deep( $_POST['faqs'] ), true ) ); foreach ( $faqs as $faq ) { $post = array( 'post_title' => $faq['title'], 'post_content' => $faq['content'], 'post_status' => 'publish', 'post_type' => EWD_UFAQ_FAQ_POST_TYPE ); $post_id = wp_insert_post( $post ); if ( ! $post_id or empty( $faq['categories'] ) ) { continue; } wp_set_object_terms( $post_id, array_map('intval', $faq['categories'] ), EWD_UFAQ_FAQ_CATEGORY_TAXONOMY ); } wp_send_json_success(); } /** * Sanitize new FAQ title, content and categories sent by admin * * @since 2.3.0 */ public function sanitize_faqs( $faqs ) { foreach ( $faqs as $index => $faq ) { $faqs[ $index ]['title'] = sanitize_text_field( $faq['title'] ); $faqs[ $index ]['content'] = sanitize_textarea_field( $faq['content'] ); } return $faqs; } /** * Parse returned CSV content into an array * * @since 2.3.0 */ public function parseCSVFromString( $string ) { $faqs = array(); $lines = explode( "\n", $string ); $header = array(); // Find the correct header line while ( sizeof( $header ) < 2 ) { $header = str_getcsv( array_shift( $lines ) ); } foreach ( $lines as $line ) { if ( trim( $line ) === '' ) { continue; } if ( sizeof( $header ) != sizeof( str_getcsv( $line ) ) ) { continue; } $faqs[] = array_combine( $header, str_getcsv( $line ) ); } return $faqs; } } } // endif;