route( '/drafts', \WP_REST_Server::READABLE, array( $this, 'get_draft' ), $cap ); $this->route( '/drafts/items', \WP_REST_Server::CREATABLE, array( $this, 'save_items' ), $cap ); $this->route( '/drafts/items/(?P\d+)', \WP_REST_Server::DELETABLE, array( $this, 'remove_item' ), $cap ); $this->route( '/drafts/paste', \WP_REST_Server::CREATABLE, array( $this, 'paste' ), $cap ); $this->route( '/drafts/discard', \WP_REST_Server::CREATABLE, array( $this, 'discard' ), $cap ); $this->route( '/slugs/validate', \WP_REST_Server::CREATABLE, array( $this, 'validate' ), $cap ); } /** * The current draft's changes, used for live duplicate detection across pages. * * @return \WP_REST_Response */ public function get_draft() { $job_id = DraftManager::get_editor_draft( false ); $items = array(); if ( $job_id ) { foreach ( JobRepository::get_items( $job_id, array( 'kind' => 'direct' ) ) as $item ) { $items[] = self::item_payload( $item ); } } return $this->respond( array( 'job_id' => $job_id, 'items' => $items, ) ); } /** * Public shape of a draft item. * * @param object $item Item. * @return array */ private static function item_payload( $item ) { return array( 'post_id' => $item->post_id, 'post_type' => $item->post_type, 'parent_id' => (int) $item->parent_id, 'old_slug' => rawurldecode( $item->old_post_name ), 'new_slug' => rawurldecode( $item->requested_post_name ), 'key' => JobRepository::duplicate_key( $item->post_type, $item->parent_id, $item->requested_post_name ), ); } /** * Save one or more draft changes: items = [{post_id, new_slug}]. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function save_items( \WP_REST_Request $request ) { $items = (array) $request->get_param( 'items' ); $job_id = DraftManager::get_editor_draft( true ); $out = array(); foreach ( array_slice( $items, 0, 500 ) as $entry ) { $post_id = isset( $entry['post_id'] ) ? absint( $entry['post_id'] ) : 0; $slug = isset( $entry['new_slug'] ) ? Sanitizer::slug_input( $entry['new_slug'] ) : ''; $res = DraftManager::upsert_item( $job_id, $post_id, $slug ); $item = JobRepository::find_item( $job_id, $post_id ); $out[] = array( 'post_id' => $post_id, 'status' => $res['status'], 'message' => isset( $res['message'] ) ? $res['message'] : '', 'item' => $item ? self::item_payload( $item ) : null, ); } return $this->respond( array( 'job_id' => $job_id, 'results' => $out, ) ); } /** * Remove a change from the draft. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function remove_item( \WP_REST_Request $request ) { $job_id = DraftManager::get_editor_draft( false ); return $this->respond( array( 'removed' => $job_id && DraftManager::remove_item( $job_id, absint( $request['post_id'] ) ) ) ); } /** * Bulk paste by ID ("content_id,new_slug" lines). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function paste( \WP_REST_Request $request ) { $job_id = DraftManager::get_editor_draft( true ); $text = $this->text_param( $request, 'text' ); if ( strlen( $text ) > 1048576 ) { return new \WP_Error( 'too_large', __( 'Paste at most 1 MB at a time.', 'slugh' ), array( 'status' => 413 ) ); } $report = DraftManager::apply_bulk_paste( $job_id, wp_check_invalid_utf8( $text ) ); $report['job_id'] = $job_id; return $this->respond( $report ); } /** * Discard the whole draft job. * * @return \WP_REST_Response */ public function discard() { $job_id = DraftManager::get_editor_draft( false ); return $this->respond( array( 'discarded' => $job_id && DraftManager::discard( $job_id ) ) ); } /** * Validate a single proposed slug (inline hints in the editor). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function validate( \WP_REST_Request $request ) { $result = SlugValidator::validate( absint( $request->get_param( 'post_id' ) ), Sanitizer::slug_input( $request->get_param( 'new_slug' ) ) ); return $this->respond( array_intersect_key( $result, array_flip( array( 'state', 'messages', 'sanitized_decoded', 'predicted_decoded', 'old_permalink', 'predicted_permalink', 'descendants', 'is_valid' ) ) ) ); } }