' . esc_html__( 'Settings', 'acs-article-generator' ) . ''; array_unshift( $links, $settings ); return $links; } /** * Register the menu and its four screens. */ public static function register_menu() { add_menu_page( __( 'Article Generator', 'acs-article-generator' ), __( 'Article Generator', 'acs-article-generator' ), 'edit_posts', 'acsagen-generate', array( __CLASS__, 'render_generate' ), 'dashicons-edit-page', 31 ); add_submenu_page( 'acsagen-generate', __( 'Write an article', 'acs-article-generator' ), __( 'Write an article', 'acs-article-generator' ), 'edit_posts', 'acsagen-generate', array( __CLASS__, 'render_generate' ) ); add_submenu_page( 'acsagen-generate', __( 'Category hub page', 'acs-article-generator' ), __( 'Category hub page', 'acs-article-generator' ), 'edit_posts', 'acsagen-hub', array( __CLASS__, 'render_hub' ) ); add_submenu_page( 'acsagen-generate', __( 'House style', 'acs-article-generator' ), __( 'House style', 'acs-article-generator' ), 'edit_posts', 'acsagen-tone', array( __CLASS__, 'render_tone' ) ); add_submenu_page( 'acsagen-generate', __( 'Settings', 'acs-article-generator' ), __( 'Settings', 'acs-article-generator' ), 'manage_options', 'acsagen-settings', array( __CLASS__, 'render_settings' ) ); add_submenu_page( 'acsagen-generate', __( 'How to use', 'acs-article-generator' ), __( 'How to use', 'acs-article-generator' ), 'edit_posts', 'acsagen-help', array( __CLASS__, 'render_help' ) ); } /** * Load the stylesheet and script on this plugin's screens only. * * @param string $hook Current admin page. */ public static function enqueue( $hook ) { if ( false === strpos( (string) $hook, 'acsagen-' ) ) { return; } wp_enqueue_style( 'acsagen-admin', ACSAGEN_URL . 'assets/css/admin.css', array(), ACSAGEN_VERSION ); wp_enqueue_script( 'acsagen-admin', ACSAGEN_URL . 'assets/js/admin.js', array(), ACSAGEN_VERSION, true ); wp_localize_script( 'acsagen-admin', 'acsagenData', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'strings' => array( 'working' => __( 'Working. A long article can take two or three minutes.', 'acs-article-generator' ), 'failed' => __( 'The request could not be completed.', 'acs-article-generator' ), 'noPosts' => __( 'No category on this site has a published post yet.', 'acs-article-generator' ), 'articles' => __( 'published articles', 'acs-article-generator' ), 'numbers' => __( 'Numbers to check: the article states these as first hand experience, but they are not in the experience you entered. The article was not changed.', 'acs-article-generator' ), ), ) ); } // ----------------------------------------------------------------- // Screens. // ----------------------------------------------------------------- /** * The screen where an article is written. */ public static function render_generate() { if ( ! current_user_can( 'edit_posts' ) ) { wp_die( esc_html__( 'You are not allowed to open this screen.', 'acs-article-generator' ) ); } $has_key = ACSAGEN_Settings::has_api_key(); $last = get_option( 'acsagen_last_result', array() ); $ideas = (string) get_option( 'acsagen_last_ideas', '' ); ?>

' . esc_html__( 'Settings', 'acs-article-generator' ) . '' ); ?>

' . esc_html__( 'House style', 'acs-article-generator' ) . '' ); ?>

>

ACS Developer' ); ?>

isset( $_POST['model'] ) ? sanitize_text_field( wp_unslash( $_POST['model'] ) ) : '', 'site_concept' => isset( $_POST['site_concept'] ) ? sanitize_textarea_field( wp_unslash( $_POST['site_concept'] ) ) : '', 'output_language' => isset( $_POST['output_language'] ) ? sanitize_text_field( wp_unslash( $_POST['output_language'] ) ) : '', 'sample_posts' => isset( $_POST['sample_posts'] ) ? absint( wp_unslash( $_POST['sample_posts'] ) ) : 10, 'sync_seo_meta' => isset( $_POST['sync_seo_meta'] ) ? '1' : '0', 'draft_category' => isset( $_POST['draft_category'] ) ? '1' : '0', 'create_categories' => isset( $_POST['create_categories'] ) ? '1' : '0', 'fix_link_anchors' => isset( $_POST['fix_link_anchors'] ) ? '1' : '0', ); ACSAGEN_Settings::save( $input ); if ( isset( $_POST['delete_api_key'] ) ) { ACSAGEN_Settings::delete_api_key(); } elseif ( isset( $_POST['api_key'] ) ) { // Read straight into the store. The value is never echoed, logged // or put in a redirect. $key = trim( sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) ); if ( '' !== $key ) { ACSAGEN_Settings::save_api_key( $key ); } } wp_safe_redirect( add_query_arg( array( 'page' => 'acsagen-settings', 'acsagen-message' => 'saved', ), admin_url( 'admin.php' ) ) ); exit; } /** * Save or clear the style guide. */ public static function handle_save_tone() { if ( ! current_user_can( 'edit_posts' ) ) { wp_die( esc_html__( 'You are not allowed to change the style guide.', 'acs-article-generator' ) ); } check_admin_referer( self::NONCE ); if ( isset( $_POST['acsagen_clear'] ) ) { ACSAGEN_Tone::clear(); } else { $tone = isset( $_POST['tone'] ) ? sanitize_textarea_field( wp_unslash( $_POST['tone'] ) ) : ''; ACSAGEN_Tone::save( $tone ); } wp_safe_redirect( add_query_arg( array( 'page' => 'acsagen-tone', 'acsagen-message' => 'saved', ), admin_url( 'admin.php' ) ) ); exit; } // ----------------------------------------------------------------- // Request handlers for the three model calls. // ----------------------------------------------------------------- /** * Run the house style analysis. */ public static function ajax_analyse_tone() { check_ajax_referer( self::NONCE, 'nonce' ); self::require_cap( 'edit_posts' ); $result = ACSAGEN_Tone::analyse(); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'tone' => $result ) ); } /** * Ask for subject suggestions. */ public static function ajax_suggest_ideas() { check_ajax_referer( self::NONCE, 'nonce' ); self::require_cap( 'edit_posts' ); $result = ACSAGEN_Writer::suggest_ideas(); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } update_option( 'acsagen_last_ideas', $result ); wp_send_json_success( array( 'ideas' => $result ) ); } /** * Write an article and save it as a draft. */ public static function ajax_write_article() { check_ajax_referer( self::NONCE, 'nonce' ); self::require_cap( 'edit_posts' ); $args = array( 'topic' => isset( $_POST['topic'] ) ? sanitize_textarea_field( wp_unslash( $_POST['topic'] ) ) : '', 'length' => isset( $_POST['length'] ) ? sanitize_text_field( wp_unslash( $_POST['length'] ) ) : 'standard', 'custom' => isset( $_POST['custom'] ) ? sanitize_text_field( wp_unslash( $_POST['custom'] ) ) : '', 'notes' => isset( $_POST['notes'] ) ? sanitize_textarea_field( wp_unslash( $_POST['notes'] ) ) : '', 'experience' => isset( $_POST['experience'] ) ? sanitize_textarea_field( wp_unslash( $_POST['experience'] ) ) : '', 'offer' => isset( $_POST['offer'] ) ? sanitize_text_field( wp_unslash( $_POST['offer'] ) ) : '', 'links' => isset( $_POST['links'] ) ? sanitize_textarea_field( wp_unslash( $_POST['links'] ) ) : '', ); if ( ! array_key_exists( $args['length'], ACSAGEN_Writer::lengths() ) ) { $args['length'] = 'standard'; } $result = ACSAGEN_Writer::create_draft( $args ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } wp_send_json_success( array( 'title' => $result['title'], 'slug' => $result['slug'], 'description' => $result['description'], 'editUrl' => $result['edit_url'], 'notice' => __( 'The draft was created. Nothing has been published.', 'acs-article-generator' ), 'notices' => isset( $result['notices'] ) ? array_values( (array) $result['notices'] ) : array(), 'checkNumbers' => isset( $result['check_numbers'] ) ? array_values( (array) $result['check_numbers'] ) : array(), ) ); } /** * List the terms that have at least one published post. */ public static function ajax_hub_terms() { check_ajax_referer( self::NONCE, 'nonce' ); self::require_cap( 'edit_posts' ); $taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_key( wp_unslash( $_POST['taxonomy'] ) ) : 'category'; if ( ! array_key_exists( $taxonomy, ACSAGEN_Hub::taxonomies() ) ) { wp_send_json_error( array( 'message' => __( 'That is not a taxonomy a hub page can be built from.', 'acs-article-generator' ) ), 400 ); } $terms = array(); foreach ( ACSAGEN_Hub::available_terms( $taxonomy ) as $term ) { $terms[] = array( 'id' => $term['term_id'], 'name' => $term['name'], 'count' => $term['count'], 'hubDate' => $term['hub'] ? $term['hub']['built'] : '', 'hubSize' => $term['hub'] ? count( $term['hub']['post_ids'] ) : 0, ); } wp_send_json_success( array( 'terms' => $terms ) ); } /** * Build a hub page for one category. */ public static function ajax_build_hub() { check_ajax_referer( self::NONCE, 'nonce' ); self::require_cap( 'edit_posts' ); $result = ACSAGEN_Hub::build( array( 'taxonomy' => isset( $_POST['taxonomy'] ) ? sanitize_key( wp_unslash( $_POST['taxonomy'] ) ) : 'category', 'term_id' => isset( $_POST['term_id'] ) ? absint( wp_unslash( $_POST['term_id'] ) ) : 0, 'post_type' => isset( $_POST['post_type'] ) ? sanitize_key( wp_unslash( $_POST['post_type'] ) ) : 'page', 'notes' => isset( $_POST['notes'] ) ? sanitize_textarea_field( wp_unslash( $_POST['notes'] ) ) : '', ) ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message() ) ); } $difference = $result['difference']; if ( ! empty( $difference['first'] ) ) { $change = __( 'This is the first hub page for this category.', 'acs-article-generator' ); } else { $change = sprintf( /* translators: 1: number of articles added since the last hub, 2: number no longer in the category. */ __( 'Since the last hub page for this category: %1$d new articles, %2$d no longer in it.', 'acs-article-generator' ), count( $difference['added'] ), count( $difference['gone'] ) ); } wp_send_json_success( array( 'title' => $result['title'], 'editUrl' => $result['edit_url'], 'summary' => sprintf( /* translators: 1: category name, 2: number of articles covered, 3: number linked from the body. */ __( 'Hub page drafted for "%1$s", covering %2$d articles, %3$d of them linked from the body.', 'acs-article-generator' ), $result['term_name'], (int) $result['covered'], (int) $result['linked'] ), 'change' => $change, 'notice' => __( 'The draft was created. Nothing has been published and no existing page was changed.', 'acs-article-generator' ), ) ); } /** * Stop a request from a user who may not do this. * * The nonce is checked with check_ajax_referer() at the top of each * handler, before anything reads the request. * * @param string $capability Capability the caller must have. */ private static function require_cap( $capability ) { if ( ! current_user_can( $capability ) ) { wp_send_json_error( array( 'message' => __( 'You are not allowed to do that.', 'acs-article-generator' ) ), 403 ); } } }