%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('#