*/ private $pending = array(); /** * Constructor. * * @param Connection $connection Connection. */ public function __construct( Connection $connection ) { $this->connection = $connection; } /** * Hooks. */ public function register(): void { add_action( 'enqueue_block_editor_assets', array( $this, 'boot' ) ); add_action( 'rest_api_init', array( $this, 'register_pending_field' ) ); } /** * Registers the pending field. Nothing is stored: a save answers with the * value it was sent, so the page is no longer changed afterwards. */ public function register_pending_field(): void { register_rest_field( Knowledge::post_types(), self::PENDING_FIELD, array( 'get_callback' => function ( $post ) { return $this->pending[ (int) ( $post['id'] ?? 0 ) ] ?? ''; }, 'update_callback' => function ( $value, $post ) { if ( $post instanceof \WP_Post ) { $this->pending[ $post->ID ] = sanitize_text_field( (string) $value ); } return true; }, 'schema' => array( 'description' => __( 'Changes in the Stand Chat panel waiting for the page to be saved.', 'stand-chat' ), 'type' => 'string', 'maxLength' => 40, 'context' => array( 'edit' ), ), ) ); } /** * Adds the editor bootstrap data. */ public function boot(): void { $preview = State::get( 'standin_preview', array() ); $preview = is_array( $preview ) ? $preview : array(); $data = array( 'restPath' => '/' . Rest::NAMESPACE, 'connection' => Connection::fingerprint( $this->connection->identity_seen() ), 'connected' => $this->connection->is_usable(), 'siteIdSet' => '' !== Settings::get( 'site_id' ), 'canManage' => current_user_can( Admin::capability() ), 'isOwner' => $this->connection->is_owner(), 'postTypes' => Knowledge::post_types(), 'preview' => array( 'name' => (string) ( $preview['displayName'] ?? '' ), 'avatar' => (string) ( $preview['avatarUrl'] ?? '' ), 'brand' => (string) ( $preview['brand'] ?? wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ), ), 'urls' => array( 'chats' => Service_Urls::app( '/' ), 'sites' => Service_Urls::app( '/sites' ), 'standins' => admin_url( 'admin.php?page=stand-chat-standins' ), 'widget' => admin_url( 'admin.php?page=stand-chat-widget' ), 'overview' => admin_url( 'admin.php?page=stand-chat' ), ), ); wp_add_inline_script( Blocks::EDITOR_HANDLE, 'window.standChatEditor = ' . wp_json_encode( $data ) . ';', 'before' ); wp_enqueue_script( Blocks::EDITOR_HANDLE ); // The document panel sits outside the editor iframe. wp_enqueue_style( Blocks::EDITOR_HANDLE ); } }