__( 'Outercite suggested edits', 'outercite' ), 'public' => false, 'show_ui' => false, 'show_in_rest' => false, 'exclude_from_search' => true, 'publicly_queryable' => false, 'rewrite' => false, 'supports' => array( 'title', 'editor' ), 'capability_type' => 'page', 'map_meta_cap' => true, ) ); } /** * Which editor a new written draft opens in, from what the site runs. */ public static function targets(): array { $targets = array( function_exists( 'use_block_editor_for_post_type' ) && use_block_editor_for_post_type( 'page' ) ? 'block' : 'classic' => __( 'WordPress editor', 'outercite' ), ); if ( defined( 'ELEMENTOR_VERSION' ) ) { $targets['elementor'] = __( 'Elementor', 'outercite' ); } if ( defined( 'ET_BUILDER_VERSION' ) || function_exists( 'et_setup_theme' ) ) { $targets['divi'] = __( 'Divi', 'outercite' ); } return $targets; } /** * Whether a page is laid out with Elementor or Divi. * * @param int $post_id Post ID. */ public static function is_builder_page( int $post_id ): bool { return 'builder' === get_post_meta( $post_id, '_elementor_edit_mode', true ) || 'on' === get_post_meta( $post_id, '_et_pb_use_builder', true ); } /** * A page's text as Outercite reads it for a suggested edit: no markup, no * block comments, no shortcodes, paragraphs kept. * * @param \WP_Post $post The page. */ public static function page_text( \WP_Post $post ): string { $html = preg_replace( '//s', '', (string) $post->post_content ); $html = strip_shortcodes( (string) $html ); $html = preg_replace( '##i', "\n\n", (string) $html ); $text = html_entity_decode( wp_strip_all_tags( (string) $html ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); $text = preg_replace( "/[ \t]+/", ' ', $text ); $text = preg_replace( "/\n\s*\n+/", "\n\n", (string) $text ); $title = html_entity_decode( get_the_title( $post ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); return trim( $title . "\n\n" . trim( (string) $text ) ); } /** * Just the text of a post's content, no title: the new sections of a suggestion. * * @param \WP_Post $post The post. */ public static function section_text( \WP_Post $post ): string { $html = preg_replace( '//s', '', (string) $post->post_content ); $html = preg_replace( '##i', "\n\n", (string) $html ); $text = html_entity_decode( wp_strip_all_tags( (string) $html ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); return trim( (string) preg_replace( "/\n\s*\n+/", "\n\n", (string) preg_replace( "/[ \t]+/", ' ', $text ) ) ); } /** * Key of the pending write for a change. * * @param string $change_id Outercite change id. */ private static function pending_key( string $change_id ): string { return self::PENDING . md5( $change_id ); } /** * The write Outercite is doing for a change, if any. * * @param string $change_id Outercite change id. * @return array|null */ public static function pending( string $change_id ): ?array { $pending = get_transient( self::pending_key( $change_id ) ); return is_array( $pending ) ? $pending : null; } /** * Start a draft: a whole page, an FAQ entry, or the missing sections of an existing page. */ public static function handle_write(): void { check_admin_referer( 'outercite_write' ); if ( ! current_user_can( Roles::DRAFTS ) || ! current_user_can( 'edit_pages' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } $change = isset( $_POST['change'] ) ? sanitize_text_field( wp_unslash( $_POST['change'] ) ) : ''; $change = preg_match( '/^[A-Za-z0-9_-]{1,64}$/', $change ) ? $change : ''; $kind = isset( $_POST['kind'] ) ? sanitize_key( wp_unslash( $_POST['kind'] ) ) : ''; $target = isset( $_POST['target'] ) ? sanitize_key( wp_unslash( $_POST['target'] ) ) : ''; $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; $back = Admin::brief_url( $change ) . '#outercite-write'; if ( '' === $change || '' === $kind ) { Admin::flash( 'error', __( 'Pick what to write.', 'outercite' ) ); wp_safe_redirect( $back ); exit; } if ( self::pending( $change ) && empty( self::pending( $change )['result'] ) ) { Admin::flash( 'warning', __( 'Outercite is already writing a draft for this change.', 'outercite' ) ); wp_safe_redirect( $back ); exit; } $existing = null; if ( $post_id ) { $post = get_post( $post_id ); if ( ! $post || ! current_user_can( 'edit_post', $post_id ) || 'page' !== $kind ) { Admin::flash( 'error', __( 'Pick a page you can edit.', 'outercite' ) ); wp_safe_redirect( $back ); exit; } // The page's text leaves the site only here, because someone asked for a suggested edit. $existing = self::page_text( $post ); } $targets = self::targets(); if ( ! isset( $targets[ $target ] ) ) { $target = (string) array_key_first( $targets ); } $res = Ledger::start_draft( $change, $kind, $existing ); if ( ! $res['ok'] ) { Admin::flash( 'error', $res['error'] ); wp_safe_redirect( $back ); exit; } set_transient( self::pending_key( $change ), array( 'since' => (string) ( $res['data']['startedAt'] ?? gmdate( 'c' ) ), 'kind' => $kind, 'post_id' => $post_id, 'target' => $target, 'user' => get_current_user_id(), ), 30 * MINUTE_IN_SECONDS ); Activity::add( 'write', $post_id ? __( 'Asked Outercite for the missing sections of a page', 'outercite' ) : __( 'Asked Outercite to write a draft', 'outercite' ), $post_id ); wp_safe_redirect( $back ); exit; } /** * Poll from the brief screen: still writing, ready (with the draft saved here), or failed. */ public static function ajax_status(): void { check_ajax_referer( 'outercite_ajax' ); if ( ! current_user_can( Roles::DRAFTS ) ) { wp_send_json_error( null, 403 ); } $change = isset( $_POST['change'] ) ? sanitize_text_field( wp_unslash( $_POST['change'] ) ) : ''; $pending = self::pending( $change ); if ( ! $pending ) { wp_send_json_success( array( 'state' => 'failed', 'message' => __( 'Nothing is being written for this change. Start again from the brief.', 'outercite' ), ) ); } if ( ! empty( $pending['result'] ) ) { wp_send_json_success( self::ready_view( (int) $pending['result'], ! empty( $pending['post_id'] ) ) ); } $res = Ledger::drafts( $change, (string) $pending['since'] ); if ( ! $res['ok'] ) { wp_send_json_success( array( 'state' => 'writing' ) ); } $attempt = $res['data']['latestAttempt'] ?? null; if ( ! is_array( $attempt ) ) { wp_send_json_success( array( 'state' => 'writing' ) ); } if ( 'ready' !== ( $attempt['status'] ?? '' ) ) { delete_transient( self::pending_key( $change ) ); wp_send_json_success( array( 'state' => 'failed', 'message' => (string) ( $attempt['message'] ?? __( 'Outercite could not write this draft. Nothing was charged.', 'outercite' ) ), ) ); } // Two polls can arrive together: only one saves the draft. $lock = self::pending_key( $change ) . '_lock'; if ( (int) get_option( $lock, 0 ) && (int) get_option( $lock, 0 ) < time() - MINUTE_IN_SECONDS ) { // A poll that died while saving: let this one take over. delete_option( $lock ); } if ( ! add_option( $lock, time(), '', false ) ) { wp_send_json_success( array( 'state' => 'writing' ) ); } $draft = (array) $attempt['draft']; $post_id = ! empty( $pending['post_id'] ) ? self::save_suggestion( (int) $pending['post_id'], $change, $draft ) : self::save_draft( $change, $draft, (string) $pending['target'] ); delete_option( self::pending_key( $change ) . '_lock' ); if ( is_wp_error( $post_id ) ) { delete_transient( self::pending_key( $change ) ); wp_send_json_success( array( 'state' => 'failed', 'message' => $post_id->get_error_message(), ) ); } $pending['result'] = $post_id; set_transient( self::pending_key( $change ), $pending, 10 * MINUTE_IN_SECONDS ); Activity::add( 'written', empty( $pending['post_id'] ) ? __( 'Saved a draft Outercite wrote', 'outercite' ) : __( 'Saved a suggested edit Outercite wrote', 'outercite' ), (int) ( $pending['post_id'] ? $pending['post_id'] : $post_id ) ); wp_send_json_success( self::ready_view( (int) $post_id, ! empty( $pending['post_id'] ) ) ); } /** * What the brief screen shows once the draft is saved. * * @param int $post_id The new draft or suggestion. * @param bool $suggestion Whether it is a suggested edit. */ private static function ready_view( int $post_id, bool $suggestion ): array { return array( 'state' => 'ready', 'editUrl' => $suggestion ? self::review_url( $post_id ) : (string) get_edit_post_link( $post_id, 'raw' ), 'label' => $suggestion ? __( 'Review the suggested edit', 'outercite' ) : __( 'Open the draft', 'outercite' ), ); } /** * Address of the review screen for a suggested edit. * * @param int $suggestion_id Suggested edit ID. */ public static function review_url( int $suggestion_id ): string { return add_query_arg( array( 'page' => 'outercite', 'view' => 'suggestion', 'suggestion' => $suggestion_id, ), admin_url( 'admin.php' ) ); } /** * Split a written draft into a title and blocks of text. * * @param string $body Draft text: a title line, paragraphs, "## " headings, "- " lists. * @param bool $with_title Whether the first line is the title. * @return array{title:string,parts:array} */ public static function parse( string $body, bool $with_title ): array { $lines = preg_split( '/\r\n|\r|\n/', trim( $body ) ); $title = ''; if ( $with_title && $lines ) { $title = trim( ltrim( (string) array_shift( $lines ), '# ' ) ); } $parts = array(); $para = array(); $list = array(); $flush = static function () use ( &$parts, &$para, &$list ) { if ( $para ) { $parts[] = array( 'type' => 'p', 'text' => implode( ' ', $para ), ); $para = array(); } if ( $list ) { $parts[] = array( 'type' => 'ul', 'text' => $list, ); $list = array(); } }; foreach ( $lines as $line ) { $line = trim( (string) $line ); if ( '' === $line ) { $flush(); continue; } if ( preg_match( '/^(#{1,4})\s+(.+)$/', $line, $m ) ) { $flush(); $parts[] = array( 'type' => 'h' . max( 2, min( 4, strlen( $m[1] ) ) ), 'text' => $m[2], ); continue; } if ( preg_match( '/^[-*]\s+(.+)$/', $line, $m ) ) { if ( $para ) { $flush(); } $list[] = $m[1]; continue; } if ( $list ) { $flush(); } $para[] = $line; } $flush(); return array( 'title' => $title, 'parts' => $parts, ); } /** * One line of draft text as safe HTML: escaped, with markdown links and * bold turned into tags. A [placeholder] with no address after it stays as text. * * @param string $text Text. */ public static function inline( string $text ): string { $html = esc_html( $text ); $html = (string) preg_replace_callback( '#\[([^\]\n]{1,200})\]\((https?://[^\s)]{1,500})\)#', static function ( $m ) { $url = esc_url( html_entity_decode( $m[2], ENT_QUOTES ) ); return '' !== $url ? '' . $m[1] . '' : $m[0]; }, $html ); $html = (string) preg_replace( '#\*\*([^*\n]{1,300})\*\*#', '$1', $html ); return (string) preg_replace( '#(?$1', $html ); } /** * Parts as block editor markup or as plain HTML. * * @param array $parts Parts from parse(). * @param bool $blocks Block markup (true) or HTML (false). */ public static function render( array $parts, bool $blocks ): string { $out = ''; foreach ( $parts as $part ) { if ( 'ul' === $part['type'] ) { $items = ''; foreach ( (array) $part['text'] as $item ) { $items .= $blocks ? "\n
  • " . self::inline( (string) $item ) . "
  • \n\n" : '
  • ' . self::inline( (string) $item ) . "
  • \n"; } $out .= $blocks ? "\n\n\n\n" : '\n"; continue; } $text = self::inline( (string) $part['text'] ); if ( 'p' === $part['type'] ) { $out .= $blocks ? "\n

    " . $text . "

    \n\n\n" : '

    ' . $text . "

    \n"; continue; } $level = (int) substr( $part['type'], 1 ); $out .= $blocks ? '\n' . $text . '\n\n\n" : '' . $text . '\n"; } return $out; } /** * Save a written draft as a new linked draft page, in the editor picked. * Elementor and Divi get one text section on this new page only. * * @param string $change_id Outercite change id. * @param array $draft The draft as Outercite returns it. * @param string $target block, classic, elementor or divi. * @return int|\WP_Error */ public static function save_draft( string $change_id, array $draft, string $target ) { $is_faq = 'faq' === ( $draft['kind'] ?? '' ); $parsed = self::parse( (string) ( $draft['body'] ?? '' ), ! $is_faq ); $parts = $parsed['parts']; $change = Ledger::change( $change_id ); $title = '' !== $parsed['title'] ? $parsed['title'] : ( $change['ok'] ? (string) ( $change['data']['title'] ?? '' ) : '' ); if ( $is_faq && $parts && 'p' === $parts[0]['type'] ) { // An FAQ entry starts with its question: make it the heading. $parts[0]['type'] = 'h2'; } $html = self::render( $parts, false ); $content = 'block' === $target ? self::render( $parts, true ) : $html; if ( 'divi' === $target ) { $content = '[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text]' . $html . '[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]'; } $post_id = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'draft', 'post_title' => '' !== $title ? $title : __( 'New page', 'outercite' ), 'post_content' => $content, ), true ); if ( is_wp_error( $post_id ) ) { return $post_id; } if ( 'elementor' === $target ) { $id = static function () { return substr( md5( wp_generate_password( 12, false ) ), 0, 7 ); }; $data = array( array( 'id' => $id(), 'elType' => 'section', 'settings' => array(), 'elements' => array( array( 'id' => $id(), 'elType' => 'column', 'settings' => array( '_column_size' => 100 ), 'elements' => array( array( 'id' => $id(), 'elType' => 'widget', 'widgetType' => 'text-editor', 'settings' => array( 'editor' => $html ), 'elements' => array(), ), ), ), ), ), ); update_post_meta( $post_id, '_elementor_edit_mode', 'builder' ); update_post_meta( $post_id, '_elementor_template_type', 'wp-page' ); update_post_meta( $post_id, '_elementor_version', defined( 'ELEMENTOR_VERSION' ) ? ELEMENTOR_VERSION : '3.0.0' ); update_post_meta( $post_id, '_elementor_data', wp_slash( (string) wp_json_encode( $data ) ) ); } if ( 'divi' === $target ) { update_post_meta( $post_id, '_et_pb_use_builder', 'on' ); } Links::link( (int) $post_id, $change_id, $change['ok'] ? (string) ( $change['data']['title'] ?? $title ) : $title ); update_post_meta( $post_id, self::DRAFT_ID, (string) ( $draft['id'] ?? '' ) ); return (int) $post_id; } /** * Keep the missing sections for an existing page as a suggested edit: a * private post holding only the new sections, to add to the page or, on a * builder page, to copy in. * * @param int $page_id The existing page. * @param string $change_id Outercite change id. * @param array $draft The draft as Outercite returns it. * @return int|\WP_Error */ public static function save_suggestion( int $page_id, string $change_id, array $draft ) { $page = get_post( $page_id ); if ( ! $page ) { return new \WP_Error( 'outercite_gone', __( 'The page was deleted while Outercite was writing.', 'outercite' ) ); } $parsed = self::parse( (string) ( $draft['body'] ?? '' ), false ); $builder = self::is_builder_page( $page_id ); // Only the new sections are kept. Apply adds them to the page as it is // then, so nothing on the page can be stripped or reverted on the way. $content = self::render( $parsed['parts'], ! $builder && has_blocks( $page->post_content ) ); $id = wp_insert_post( array( 'post_type' => self::SUGGESTION, 'post_status' => 'draft', 'post_title' => get_the_title( $page ), 'post_content' => $content, ), true ); if ( is_wp_error( $id ) ) { return $id; } update_post_meta( $id, self::SUGGESTS, $page_id ); update_post_meta( $id, self::FOR_CHANGE, $change_id ); update_post_meta( $id, self::DRAFT_ID, (string) ( $draft['id'] ?? '' ) ); if ( $builder ) { update_post_meta( $id, self::COPY_ONLY, 1 ); } return (int) $id; } /** * Suggested edits waiting for review on a page. * * @param int $page_id The page. * @return \WP_Post[] */ public static function suggestions_for( int $page_id ): array { return get_posts( array( 'post_type' => self::SUGGESTION, 'post_status' => 'draft', 'posts_per_page' => 10, 'meta_key' => self::SUGGESTS, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- a handful of private rows. 'meta_value' => $page_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- as above. ) ); } /** * The suggested edit named in a request, if the user may act on its page. * * @param int $id Suggested edit ID. */ private static function suggestion( int $id ): ?\WP_Post { $post = get_post( $id ); if ( ! $post || self::SUGGESTION !== $post->post_type ) { return null; } $page_id = (int) get_post_meta( $id, self::SUGGESTS, true ); return $page_id && current_user_can( 'edit_post', $page_id ) && current_user_can( Roles::DRAFTS ) ? $post : null; } /** * Apply a suggested edit to its page. A person clicks this; the page then * asks "Is this change live now?" like any update of a linked page. */ public static function handle_apply(): void { check_admin_referer( 'outercite_apply_suggestion' ); $id = isset( $_POST['suggestion'] ) ? absint( $_POST['suggestion'] ) : 0; $post = self::suggestion( $id ); if ( ! $post || get_post_meta( $id, self::COPY_ONLY, true ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } $page_id = (int) get_post_meta( $id, self::SUGGESTS, true ); $change_id = (string) get_post_meta( $id, self::FOR_CHANGE, true ); if ( ! Links::change_for_post( $page_id ) && '' !== $change_id ) { $change = Ledger::change( $change_id ); Links::link( $page_id, $change_id, $change['ok'] ? (string) ( $change['data']['title'] ?? '' ) : '' ); } $page = get_post( $page_id ); if ( ! $page ) { wp_die( esc_html__( 'The page is gone.', 'outercite' ) ); } $res = wp_update_post( wp_slash( array( 'ID' => $page_id, 'post_content' => rtrim( (string) $page->post_content ) . "\n\n" . (string) $post->post_content, ) ), true ); if ( is_wp_error( $res ) ) { Admin::flash( 'error', $res->get_error_message() ); wp_safe_redirect( self::review_url( $id ) ); exit; } wp_delete_post( $id, true ); Activity::add( 'applied', __( 'Applied a suggested edit to a page', 'outercite' ), $page_id ); Admin::flash( 'success', sprintf( /* translators: %s: page title */ __( 'Applied to "%s". The page keeps its earlier version in Revisions.', 'outercite' ), get_the_title( $page_id ) ) ); wp_safe_redirect( '' !== $change_id ? Admin::brief_url( $change_id ) : (string) get_edit_post_link( $page_id, 'raw' ) ); exit; } /** * Discard a suggested edit. */ public static function handle_discard(): void { check_admin_referer( 'outercite_discard_suggestion' ); $id = isset( $_POST['suggestion'] ) ? absint( $_POST['suggestion'] ) : 0; $post = self::suggestion( $id ); if ( ! $post ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } $change_id = (string) get_post_meta( $id, self::FOR_CHANGE, true ); $page_id = (int) get_post_meta( $id, self::SUGGESTS, true ); wp_delete_post( $id, true ); Activity::add( 'discarded', __( 'Discarded a suggested edit', 'outercite' ), $page_id ); Admin::flash( 'success', __( 'Discarded the suggested edit.', 'outercite' ) ); wp_safe_redirect( Admin::brief_url( $change_id ) ); exit; } /** * The review screen for a suggested edit: what changes, then apply or discard. * * @param int $id Suggested edit ID. */ public static function render_review( int $id ): void { $post = self::suggestion( $id ); if ( ! $post ) { Admin::header( __( 'Suggested edit', 'outercite' ), array( 'back' => admin_url( 'admin.php?page=outercite' ), 'back_label' => __( 'Changes', 'outercite' ), ) ); echo '

    ' . esc_html__( 'That suggested edit is gone, or you cannot edit its page.', 'outercite' ) . '

    '; return; } $page_id = (int) get_post_meta( $id, self::SUGGESTS, true ); $page = get_post( $page_id ); $change_id = (string) get_post_meta( $id, self::FOR_CHANGE, true ); $copy_only = (bool) get_post_meta( $id, self::COPY_ONLY, true ); $live = $page && 'publish' === $page->post_status; $post_action = admin_url( 'admin-post.php' ); $discard = '
    ' . wp_nonce_field( 'outercite_discard_suggestion', '_wpnonce', true, false ); $discard .= ''; $discard .= '
    '; $actions = $discard; if ( ! $copy_only ) { $apply = '
    ' . wp_nonce_field( 'outercite_apply_suggestion', '_wpnonce', true, false ); $apply .= ''; $apply .= '
    '; $actions .= $apply; } Admin::header( $page ? html_entity_decode( get_the_title( $page ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) : __( 'Suggested edit', 'outercite' ), array( 'back' => Admin::brief_url( $change_id ), 'back_label' => __( 'Back to the brief', 'outercite' ), 'kicker' => __( 'Suggested edit', 'outercite' ), 'pills' => Admin::pill( $live ? __( 'Published page', 'outercite' ) : __( 'Draft page', 'outercite' ), $live ? 'green' : 'blue' ), 'actions' => $actions, ) ); if ( $copy_only ) { echo '

    ' . esc_html__( 'New sections to paste in', 'outercite' ) . '

    ' . esc_html__( 'This page is laid out with a page builder, so Outercite does not write into it. Copy the new sections and paste them into a text block in the builder.', 'outercite' ) . '

    '; $plain = trim( html_entity_decode( wp_strip_all_tags( str_replace( array( '

    ', '', '', '' ), "\n\n", $post->post_content ) ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); echo ''; echo '
    '; echo '' . esc_html__( 'Open the page', 'outercite' ) . '
    '; return; } echo '

    ' . esc_html__( 'What changes', 'outercite' ) . '

    ' . esc_html__( 'Green lines are added. Nothing changes on the page until you apply it, and the page keeps its earlier version in Revisions.', 'outercite' ) . '

    '; $now = $page ? self::page_text( $page ) : ''; $diff = wp_text_diff( $now, $now . "\n\n" . self::section_text( $post ), array( 'title_left' => __( 'The page now', 'outercite' ), 'title_right' => __( 'With the suggested sections', 'outercite' ), ) ); echo $diff ? wp_kses_post( $diff ) : '

    ' . esc_html__( 'No difference.', 'outercite' ) . '

    '; echo '
    '; } /** * The "Write it for me" box on the brief screen. * * @param string $id Outercite change id. * @param \WP_Post[] $posts Pages linked to the change. */ public static function render_box( string $id, array $posts ): void { echo '

    ' . esc_html__( 'Write it for me', 'outercite' ) . '

    '; $pending = self::pending( $id ); if ( $pending && empty( $pending['result'] ) ) { echo '

    ' . esc_html__( 'Outercite is writing. This takes about a minute; you can leave this page and come back.', 'outercite' ) . '

    '; return; } if ( $pending && ! empty( $pending['result'] ) ) { $view = self::ready_view( (int) $pending['result'], ! empty( $pending['post_id'] ) ); echo '

    ' . esc_html__( 'Draft ready.', 'outercite' ) . ' ' . esc_html( $view['label'] ) . '

    '; } $res = Ledger::drafts( $id ); if ( ! $res['ok'] ) { echo '

    ' . esc_html( $res['error'] ) . '

    '; return; } $data = (array) $res['data']; if ( empty( $data['available'] ) ) { echo '

    ' . esc_html__( 'Write it for me uses credits, which this plan does not include. Write it from the brief, or use your own AI assistant.', 'outercite' ) . '

    '; return; } $options = array_values( array_filter( (array) ( $data['options'] ?? array() ), static function ( $o ) { return in_array( $o['kind'] ?? '', array( 'page', 'faq' ), true ); } ) ); if ( ! $options ) { echo '

    ' . esc_html__( 'Outercite has nothing to write for this change.', 'outercite' ) . '

    '; return; } echo '

    ' . esc_html__( 'Outercite writes it from the brief and checks every claim against what it knows about the business. Missing facts show as [placeholders]. A draft that fails the check costs nothing.', 'outercite' ) . '

    '; $post_action = admin_url( 'admin-post.php' ); $targets = self::targets(); echo '
    '; wp_nonce_field( 'outercite_write' ); echo ''; echo '
    '; foreach ( $options as $i => $option ) { printf( '', esc_attr( (string) $option['kind'] ), checked( 0, $i, false ), esc_html( (string) ( $option['label'] ?? $option['kind'] ) ), esc_html( sprintf( /* translators: %d: number of credits */ _n( '%d credit', '%d credits', (int) $option['credits'], 'outercite' ), (int) $option['credits'] ) ), ! empty( $option['line'] ) ? '' . esc_html( (string) $option['line'] ) . '' : '' ); } echo '
    '; if ( count( $targets ) > 1 ) { echo '
    '; } else { echo ''; } echo '
    '; $page_option = null; foreach ( $options as $option ) { if ( 'page' === $option['kind'] ) { $page_option = $option; } } $existing = array_filter( $posts, static function ( $p ) { return '' !== trim( wp_strip_all_tags( (string) $p->post_content ) ) && current_user_can( 'edit_post', $p->ID ); } ); if ( $page_option && $existing ) { echo '

    '; echo '

    ' . esc_html( sprintf( /* translators: %d: number of credits */ __( 'Outercite reads the page and writes only the sections it is missing. You review them before anything changes. %d credits.', 'outercite' ), (int) $page_option['credits'] ) ) . '

    '; echo '
    '; wp_nonce_field( 'outercite_write' ); echo ''; echo '
    '; } $waiting = array(); foreach ( $posts as $p ) { $waiting = array_merge( $waiting, self::suggestions_for( $p->ID ) ); } if ( $waiting ) { echo '

    ' . esc_html__( 'Waiting for your review', 'outercite' ) . '

      '; foreach ( $waiting as $s ) { printf( '
    • %1$s%2$s%4$s
    • ', esc_html( html_entity_decode( get_the_title( $s ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ), esc_html( mysql2date( get_option( 'date_format' ), $s->post_modified ) ), esc_url( self::review_url( $s->ID ) ), esc_html__( 'Review', 'outercite' ) ); } echo '
    '; } echo ''; } }