%s', esc_html( get_admin_page_title() ) ); } /** * Display admin settings header description contnet. */ function aiwriter_settings_header_desc() { global $aiwriter__; // change support url based on state of pro plugin availability. $support_url = $aiwriter__['plugin']['request_quote']; echo sprintf( '%s | %s', esc_url( $aiwriter__['plugin']['docs'] ), 'DOCUMENTATION', esc_url( $support_url ), 'SUPPORT' ); } /** * For displaying icon * * @param string $tab_name menu name. */ function aiwriter_settings_get_menu_icon( $tab_name ) { $s = ''; $menu = sanitize_title( $tab_name ); if ( 'settings' === $menu ) { $s = 'admin-settings'; } elseif ( 'new-content' === $menu ) { $s = 'welcome-write-blog'; } if ( ! empty( $s ) ) { echo sprintf( ' ', esc_attr( $s ) ); } echo esc_html( $tab_name ); } /** * Get menu url. * * @param string $menu tab name. */ function aiwriter_settings_menu_url( $menu ) { global $aiwriter__; $url = ''; foreach ( $aiwriter__['menu'] as $page => $tabs ) { if ( ! in_array( $menu, $tabs, true ) ) { continue; } if( 'New Content' === $menu ){ $url = admin_url( 'admin.php?page=ai-writer' ); }else{ $url = admin_url( 'admin.php?page=aiwriter-' . $page . '&tab=' . sanitize_title( $menu ) . '&nonce=' . wp_create_nonce( 'aiwriter_tab_nonce' ) ); } } return $url; } /** * Display navigation items */ function aiwriter_settings_menu() { global $aiwriter__; $section = $aiwriter__['settings_section']; $tab = $aiwriter__['settings_tab']; // url nonce. $nonce = wp_create_nonce( 'aiwriter_tab_nonce' ); foreach ( $aiwriter__['menu'][ $section ] as $menu ) { // get static url for the menu. $url = ''; $classes = array(); $url = aiwriter_settings_menu_url( $menu ); // check if this menu is active? if ( sanitize_title( $menu ) === $tab ) { $classes[] = 'nav-tab-active'; } ?> $title, 'max_tokens' => 500, // 1000 token = 750 words, limit : (send 4000) (receive 2000) 'n' => 1, 'stop' => null, 'temperature' => 0.7, ); $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, ); $response = wp_remote_post( $url, array( 'timeout' => 90, // how long connection should stay open - in seconds. 'method' => 'POST', 'headers' => $headers, 'body' => wp_json_encode( $data ), 'data_format' => 'body', ) ); if ( is_wp_error( $response ) ) { $error_msg = $response->get_error_message(); return 'Error: ' . wp_kses_post( $error_msg ); } $response_data = json_decode( wp_remote_retrieve_body( $response ), true ); $choices = $response_data['choices']; $content = $choices[0]['text']; return $content; } /** * Ajax AI Content Generator */ function aiwriter_get_content() { if ( ! check_ajax_referer( 'ajax-nonce', 'aiwriter_nonce', false ) ) { wp_send_json_error( array( 'error' => true, 'msg' => 'Invalid security token sent.', ) ); } $api_key = aiwriter_api_key(); if( empty( $api_key ) ){ wp_send_json_error( array( 'error' => true, 'msg' => 'Please set your OpenAI API key in the AI Writer settings page.', ) ); } $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : ''; $starter = isset( $_POST['starter'] ) ? sanitize_text_field( wp_unslash( $_POST['starter'] ) ) : ''; $content = aiwriter_ai_content( $api_key, $title ); $r = array( 'error' => false, 'req' => $_POST ); if( empty( $content ) ){ wp_send_json( array( 'error' => true, 'msg' => 'Something is wrong. Please double check your API key.', ) ); } // Extract title and content from given content. if( 'true' === $starter ){ $s = strpos( $content, '' ) + 5; $r['title'] = strip_tags( substr( $content, 0, $s ) ); $content = substr( $content, $s ); preg_match_all('#.*?#', $content, $h2s); $headings = array(); foreach( $h2s[0] as $i => $h ){ $headings[] = strip_tags( $h ); } $r['headings'] = $headings; }else{ $r['content'] = $content; } wp_send_json( $r ); } add_action( 'wp_ajax_aiwriter_get_content', 'aiwriter_get_content' ); /** * Create a new post or page */ function aiwriter_new_post() { if ( ! check_ajax_referer( 'ajax-nonce', 'aiwriter_nonce', false ) ) { wp_send_json_error( array( 'error' => true, 'html' => 'Invalid security token sent.', ) ); } $title = isset( $_POST['post_title'] ) ? sanitize_text_field( wp_unslash( $_POST['post_title'] ) ) : ''; $post_content = isset( $_POST['post_content'] ) ? wp_kses_post( $_POST['post_content'] ) : ''; $post_type = isset( $_POST['post_type'] ) ? sanitize_key( $_POST['post_type'] ) : ''; $post_status = isset( $_POST['post_status'] ) ? sanitize_key( $_POST['post_status'] ) : ''; // Create a new post or page. $post_data = array( 'post_title' => $title, 'post_content' => $post_content, 'post_status' => $post_status, 'post_author' => get_current_user_id(), 'post_type' => $post_type, ); $post_id = 0; $r = array( 'error' => false ); if ( ! empty( $title ) && ! empty( $post_content ) ) { $post_id = wp_insert_post( $post_data ); } if( 0 !== $post_id ){ $r['post_id'] = $post_id; $r['view'] = get_permalink( $post_id ); $r['edit'] = get_edit_post_link( $post_id ); $r['trash'] = get_delete_post_link( $post_id ); }else{ $r['error'] = true; $r['msg'] = 'Error creating post'; } wp_send_json( $r ); } add_action( 'wp_ajax_aiwriter_new_post', 'aiwriter_new_post' ); /** * New content submit button */ function aiwriter_content_submit_button(){ // check if api key saved. $api_key = aiwriter_api_key(); $url = admin_url( 'admin.php?page=aiwriter-settings&tab=settings&nonce=' . wp_create_nonce( 'aiwriter_tab_nonce' ) ); ?>

Please add OpenAI API key first

OpenAI API Key