route( '/jobs', \WP_REST_Server::READABLE, array( $this, 'list_jobs' ), $view ); $this->route( '/jobs/(?P\d+)', \WP_REST_Server::READABLE, array( $this, 'get_job' ), $view ); $this->route( '/jobs/(?P\d+)/validate', \WP_REST_Server::CREATABLE, array( $this, 'validate' ), $edit ); $this->route( '/jobs/(?P\d+)/confirm', \WP_REST_Server::CREATABLE, array( $this, 'confirm' ), $edit ); $this->route( '/jobs/(?P\d+)/items/(?P\d+)', \WP_REST_Server::CREATABLE, array( $this, 'item_choice' ), $edit ); $this->route( '/jobs/(?P\d+)/run', \WP_REST_Server::CREATABLE, array( $this, 'run' ), $edit ); $this->route( '/jobs/(?P\d+)/resume', \WP_REST_Server::CREATABLE, array( $this, 'resume' ), $edit ); $this->route( '/jobs/(?P\d+)/cancel', \WP_REST_Server::CREATABLE, array( $this, 'cancel' ), $edit ); $this->route( '/jobs/(?P\d+)/rollback', \WP_REST_Server::CREATABLE, array( $this, 'rollback' ), $edit ); $this->route( '/jobs/(?P\d+)/promote', \WP_REST_Server::CREATABLE, array( $this, 'promote' ), Capabilities::CAP_MANAGE_REDIRECTS ); $this->route( '/jobs/(?P\d+)/create-missing', \WP_REST_Server::CREATABLE, array( $this, 'create_missing' ), Capabilities::CAP_MANAGE_REDIRECTS ); $this->route( '/audit-log', \WP_REST_Server::READABLE, array( $this, 'audit_log' ), $view ); } /** * List jobs. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function list_jobs( \WP_REST_Request $request ) { $args = array( 'page' => $request->get_param( 'page' ), 'per_page' => $request->get_param( 'per_page' ), ); if ( $request->get_param( 'type' ) ) { $args['type'] = (array) $request->get_param( 'type' ); } if ( $request->get_param( 'status' ) ) { $args['status'] = (array) $request->get_param( 'status' ); } return $this->respond( JobRepository::query( $args ) ); } /** * Job with progress and a page of items. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function get_job( \WP_REST_Request $request ) { $job = JobRepository::get( absint( $request['id'] ) ); if ( ! $job ) { return new \WP_Error( 'not_found', __( 'Job not found.', 'slugh' ), array( 'status' => 404 ) ); } $per_page = min( 500, max( 1, absint( $request->get_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 100 ) ) ); $page = max( 1, absint( $request->get_param( 'page' ) ) ); return $this->respond( array( 'job' => $job, 'progress' => JobRunner::progress( $job->id ), 'items' => JobRepository::get_items( $job->id, array( 'limit' => $per_page, 'offset' => ( $page - 1 ) * $per_page, ) ), ) ); } /** * Validate (preview) a slice of a draft job. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function validate( \WP_REST_Request $request ) { return $this->respond( JobRunner::validate( absint( $request['id'] ), absint( $request->get_param( 'offset' ) ) ) ); } /** * Confirm a validated job. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function confirm( \WP_REST_Request $request ) { $resolutions = array(); foreach ( (array) $request->get_param( 'resolutions' ) as $item_id => $resolution ) { $resolutions[ absint( $item_id ) ] = sanitize_key( (string) $resolution ); } $only = $request->get_param( 'only' ); return $this->respond( JobRunner::confirm( absint( $request['id'] ), array( 'resolutions' => $resolutions, 'leave_out' => array_map( 'absint', (array) $request->get_param( 'exclude' ) ), 'only' => is_array( $only ) ? array_map( 'absint', $only ) : null, 'confirmed' => rest_sanitize_boolean( $request->get_param( 'confirmed' ) ), ) ) ); } /** * Exclude an item or resolve its conflict. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function item_choice( \WP_REST_Request $request ) { $choice = array(); if ( null !== $request->get_param( 'excluded' ) ) { $choice['excluded'] = rest_sanitize_boolean( $request->get_param( 'excluded' ) ); } if ( null !== $request->get_param( 'resolution' ) ) { $choice['resolution'] = sanitize_key( $this->text_param( $request, 'resolution' ) ); } return $this->respond( JobRunner::set_item_choice( absint( $request['id'] ), absint( $request['item_id'] ), $choice ) ); } /** * Run the next batch. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function run( \WP_REST_Request $request ) { return $this->respond( JobRunner::run_batch( absint( $request['id'] ) ) ); } /** * Resume a paused job. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function resume( \WP_REST_Request $request ) { $result = JobRunner::resume( absint( $request['id'] ) ); return $this->respond( is_wp_error( $result ) ? $result : JobRunner::progress( absint( $request['id'] ) ) ); } /** * Cancel a job. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function cancel( \WP_REST_Request $request ) { $result = JobRunner::cancel( absint( $request['id'] ) ); return $this->respond( is_wp_error( $result ) ? $result : JobRunner::progress( absint( $request['id'] ) ) ); } /** * Create a rollback job (whole job or item_ids). * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function rollback( \WP_REST_Request $request ) { $item_ids = $request->get_param( 'item_ids' ); $result = RollbackManager::create( absint( $request['id'] ), is_array( $item_ids ) && ! empty( $item_ids ) ? array_map( 'absint', $item_ids ) : null ); return $this->respond( is_wp_error( $result ) ? $result : array( 'rollback_job_id' => $result ) ); } /** * Convert a job's trial 302 redirects to 301. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function promote( \WP_REST_Request $request ) { return $this->respond( array( 'promoted' => RedirectRepository::promote_job_redirects( absint( $request['id'] ) ) ) ); } /** * Hand-off mode: create the missing redirects reported after a job. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function create_missing( \WP_REST_Request $request ) { return $this->respond( JobRunner::create_missing_redirects( absint( $request['id'] ) ) ); } /** * Audit log entries. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function audit_log( \WP_REST_Request $request ) { $per_page = min( 200, max( 1, absint( $request->get_param( 'per_page' ) ? $request->get_param( 'per_page' ) : 50 ) ) ); $page = max( 1, absint( $request->get_param( 'page' ) ) ); return $this->respond( AuditLogger::query( array( 'limit' => $per_page, 'offset' => ( $page - 1 ) * $per_page, 'event' => sanitize_key( $this->text_param( $request, 'event' ) ), 'object_type' => sanitize_key( $this->text_param( $request, 'object_type' ) ), 'job_id' => absint( $request->get_param( 'job_id' ) ), ) ) ); } }