\d+)', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'post_state' ), 'permission_callback' => $can_edit, ) ); register_rest_route( self::NS, '/post/(?P\d+)/live', array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'confirm_live' ), 'permission_callback' => $can_mark, ) ); register_rest_route( self::NS, '/post/(?P\d+)/not-yet', array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'not_yet' ), 'permission_callback' => $can_mark, ) ); } /** * Everything the editor needs about one post: its change, brief, and any question to ask. * * @param \WP_REST_Request $req The REST request. */ public static function post_state( \WP_REST_Request $req ): \WP_REST_Response { $post_id = (int) $req['id']; $change_id = Links::change_for_post( $post_id ); $state = array( 'linked' => (bool) $change_id, 'connected' => Connection::is_linked(), 'staging' => Site::is_staging(), 'changeId' => $change_id, 'changeTitle' => $change_id ? (string) get_post_meta( $post_id, Links::TITLE, true ) : null, 'shipped' => Links::is_shipped( $post_id ), 'askLive' => (bool) get_post_meta( $post_id, Links::ASK_LIVE, true ) && ! Links::is_shipped( $post_id ) && current_user_can( Roles::DRAFTS ), 'notice' => Publish::take_notice(), 'brief' => null, 'briefError' => null, 'briefUrl' => $change_id ? Admin::brief_url( $change_id ) : null, 'writeUrl' => $change_id && current_user_can( Roles::DRAFTS ) ? Admin::brief_url( $change_id ) . '#outercite-write' : null, ); if ( $change_id && Connection::is_linked() ) { $brief = Ledger::brief( $change_id ); if ( $brief['ok'] ) { $state['brief'] = $brief['data']; } else { $state['briefError'] = $brief['error']; } } return new \WP_REST_Response( $state ); } /** * Mark the post's change live after the editor answered yes. * * @param \WP_REST_Request $req The REST request. */ public static function confirm_live( \WP_REST_Request $req ): \WP_REST_Response { $result = Publish::mark_live( (int) $req['id'], 'confirm' ); Publish::take_notice(); return new \WP_REST_Response( $result, $result['ok'] ? 200 : 409 ); } /** * Clear the question after the editor answered not yet. * * @param \WP_REST_Request $req The REST request. */ public static function not_yet( \WP_REST_Request $req ): \WP_REST_Response { delete_post_meta( (int) $req['id'], Links::ASK_LIVE ); return new \WP_REST_Response( array( 'ok' => true ) ); } }