$post_id, 'restPath' => '/outercite/v1/post/' . $post_id, 'restRoot' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'logoUrl' => OUTERCITE_URL . 'assets/logo.png', 'changesUrl' => admin_url( 'admin.php?page=outercite' ), ); } // Block editor. /** * Load the sidebar panel in the block editor for a linked post. */ public static function block_editor(): void { $post = get_post(); if ( ! $post || ! Links::change_for_post( $post->ID ) || ! current_user_can( Roles::BRIEFS ) ) { return; } wp_enqueue_script( 'outercite-editor-sidebar', OUTERCITE_URL . 'assets/editor-sidebar.js', array( 'wp-plugins', 'wp-element', 'wp-components', 'wp-data', 'wp-api-fetch', 'wp-edit-post', 'wp-editor', 'wp-notices', 'wp-dom-ready' ), outercite_asset_version( 'assets/editor-sidebar.js' ), true ); wp_localize_script( 'outercite-editor-sidebar', 'OutercitePost', self::script_data( $post->ID ) ); wp_enqueue_style( 'outercite-brief', OUTERCITE_URL . 'assets/brief.css', array(), outercite_asset_version( 'assets/brief.css' ) ); } // Classic editor. /** * Add the brief box to the classic editor for a linked post. * * @param string $post_type Post type being edited. * @param \WP_Post $post The post. */ public static function meta_box( $post_type, $post ): void { if ( ! $post instanceof \WP_Post || ! Links::change_for_post( $post->ID ) || ! current_user_can( Roles::BRIEFS ) ) { return; } add_meta_box( 'outercite-brief', __( 'Outercite brief', 'outercite' ), array( __CLASS__, 'render_meta_box' ), null, 'side', 'high', // The block editor has its own sidebar panel instead. array( '__back_compat_meta_box' => true ) ); } /** * Render the brief box in the classic editor. * * @param \WP_Post $post The post. */ public static function render_meta_box( \WP_Post $post ): void { $change_id = (string) Links::change_for_post( $post->ID ); echo '
'; echo '

' . esc_html( (string) get_post_meta( $post->ID, Links::TITLE, true ) ) . '

'; if ( Links::is_shipped( $post->ID ) ) { echo '

' . esc_html__( 'Marked live. Outercite is checking the page and measuring AI answers.', 'outercite' ) . '

'; } if ( ! Connection::is_linked() ) { echo '

' . esc_html__( 'This site is not connected to Outercite.', 'outercite' ) . '

'; return; } $brief = Ledger::brief( $change_id ); if ( $brief['ok'] && isset( $brief['data']['brief'] ) ) { echo '
' . esc_html( (string) $brief['data']['brief'] ) . '
'; } else { echo '

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

'; } echo '

' . esc_html__( 'Open the full brief', 'outercite' ) . ''; if ( current_user_can( Roles::DRAFTS ) ) { echo ' | ' . esc_html__( 'Write it for me', 'outercite' ) . ''; } echo '

'; echo ''; } /** * Classic editor: the publish result, and the "is it live now?" question. */ public static function classic_notices(): void { $screen = get_current_screen(); if ( ! $screen || 'post' !== $screen->base || ( method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) ) { return; } $post_id = self::post_id_in_request(); if ( ! $post_id || ! Links::change_for_post( $post_id ) ) { return; } $notice = Publish::take_notice(); if ( $notice ) { printf( '

%2$s

', $notice['ok'] ? 'success' : 'warning', esc_html( $notice['message'] ) ); } if ( get_post_meta( $post_id, Links::ASK_LIVE, true ) && ! Links::is_shipped( $post_id ) ) { $action = admin_url( 'admin-post.php' ); echo '

' . esc_html__( 'Is this change live now?', 'outercite' ) . ' '; echo esc_html( sprintf( /* translators: %s: change title */ __( 'You updated a page linked to "%s". Say yes once the change is on the published page. A small fix or typo does not need it.', 'outercite' ), (string) get_post_meta( $post_id, Links::TITLE, true ) ) ); echo '

'; foreach ( array( 'yes' => __( 'Yes, mark it live', 'outercite' ), 'no' => __( 'Not yet', 'outercite' ), ) as $answer => $label ) { echo '' . esc_html( $label ) . ' '; } echo '

'; } } /** * Handle the yes or not yet answer from the classic editor notice. */ public static function answer_live(): void { $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; check_admin_referer( 'outercite_answer_live_' . $post_id ); if ( ! current_user_can( 'edit_post', $post_id ) ) { wp_die( esc_html__( 'You cannot edit this page.', 'outercite' ) ); } if ( isset( $_GET['answer'] ) && 'yes' === $_GET['answer'] ) { Publish::mark_live( $post_id, 'confirm' ); } else { delete_post_meta( $post_id, Links::ASK_LIVE ); } wp_safe_redirect( (string) get_edit_post_link( $post_id, 'raw' ) ); exit; } // Elementor and Divi: the drawer. /** * Load the floating brief drawer for a linked post. * * @param int $post_id Post ID. */ private static function enqueue_drawer( int $post_id ): void { if ( ! $post_id || ! Links::change_for_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) || ! current_user_can( Roles::BRIEFS ) ) { return; } wp_enqueue_script( 'outercite-drawer', OUTERCITE_URL . 'assets/drawer.js', array(), outercite_asset_version( 'assets/drawer.js' ), true ); wp_localize_script( 'outercite-drawer', 'OutercitePost', self::script_data( $post_id ) ); wp_enqueue_style( 'outercite-brief', OUTERCITE_URL . 'assets/brief.css', array(), outercite_asset_version( 'assets/brief.css' ) ); } /** * Load the drawer in the Elementor editor. */ public static function drawer_in_elementor(): void { self::enqueue_drawer( self::post_id_in_request() ); } /** * The brief as a section in Elementor's page settings panel, next to the drawer. * Elementor builds these controls on the front end too, so it only runs in * the editor (wp-admin or its AJAX calls), never while a visitor loads a page. * * @param object $document Elementor document being edited. */ public static function elementor_panel( $document ): void { if ( ! ( is_admin() || wp_doing_ajax() ) || ! method_exists( $document, 'get_main_id' ) || ! class_exists( '\\Elementor\\Controls_Manager' ) ) { return; } $post_id = (int) $document->get_main_id(); $change_id = Links::change_for_post( $post_id ); if ( ! $change_id || ! current_user_can( 'edit_post', $post_id ) || ! current_user_can( Roles::BRIEFS ) || ! Connection::is_linked() ) { return; } $brief = Ledger::brief( $change_id ); $html = '

' . esc_html( (string) get_post_meta( $post_id, Links::TITLE, true ) ) . '

'; $html .= $brief['ok'] && isset( $brief['data']['brief'] ) ? '
' . esc_html( (string) $brief['data']['brief'] ) . '
' : '

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

'; $html .= '

' . esc_html__( 'Open the full brief', 'outercite' ) . ''; if ( current_user_can( Roles::DRAFTS ) ) { $html .= ' | ' . esc_html__( 'Write it for me', 'outercite' ) . ''; } $html .= '

'; $document->start_controls_section( 'outercite_brief_section', array( 'label' => __( 'Outercite brief', 'outercite' ), 'tab' => \Elementor\Controls_Manager::TAB_SETTINGS, ) ); $document->add_control( 'outercite_brief_html', array( 'type' => \Elementor\Controls_Manager::RAW_HTML, 'raw' => $html, ) ); $document->end_controls_section(); } /** * Divi's Visual Builder runs on the front end, with ?et_fb=1, for a signed-in editor only. */ public static function drawer_in_divi(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only decides whether to load a script. $builder = isset( $_GET['et_fb'] ) || ( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() ); if ( ! $builder || ! is_user_logged_in() || ! is_singular() ) { return; } self::enqueue_drawer( (int) get_queried_object_id() ); } }