= trim( (string) ( $data['source'] ?? '' ) ); $matchType = (string) ( $data['match_type'] ?? 'exact' ); $targetType = (string) ( $data['target_type'] ?? 'link' ); $targetUrl = trim( (string) ( $data['target_url'] ?? '' ) ); // On create every field is new; on update only what the request sends is judged. $touches = static function ( ...$fields ) use ( $changed ) { if ( null === $changed ) { return true; } return (bool) array_intersect( $fields, $changed ); }; if ( $touches( 'source' ) && '' === $source ) { return $this->invalid( 'rest_missing_source', __( 'Enter the source URL or pattern to redirect from.', '404-to-301' ), 'source' ); } if ( $touches( 'source', 'match_type' ) && 'regex' === $matchType && ! aioseo404To301()->helpers->isValidRegex( $source ) ) { return $this->invalid( 'rest_invalid_regex', __( 'That is not a valid regular expression. Check the pattern and its delimiters.', '404-to-301' ), 'source' ); } if ( $touches( 'target_url', 'target_type' ) && 'link' === $targetType && '' !== $targetUrl && ! aioseo404To301()->helpers->isAllowedRedirectTarget( $targetUrl ) ) { return $this->invalid( 'rest_invalid_target', __( 'The destination must be a relative path or an http(s) URL.', '404-to-301' ), 'target_url' ); } if ( $touches( 'target_page_id', 'target_type' ) && 'page' === $targetType ) { $pageId = (int) ( $data['target_page_id'] ?? 0 ); if ( $pageId <= 0 || null === get_post( $pageId ) ) { return $this->invalid( 'rest_invalid_target_page', __( 'Choose an existing page to redirect to.', '404-to-301' ), 'target_page_id' ); } } // Only meaningful for a literal source; a pattern doesn't have one path to compare. if ( $touches( 'source', 'target_url', 'target_type', 'match_type' ) && 'exact' === $matchType && 'link' === $targetType && '' !== $targetUrl ) { $helpers = aioseo404To301()->helpers; if ( $helpers->urlHashWithQuery( $source ) === $helpers->urlHashWithQuery( $targetUrl ) ) { return $this->invalid( 'rest_redirect_loop', __( 'The destination is the same as the source, which would redirect in a loop.', '404-to-301' ), 'target_url' ); } } return null; } /** * Build a 400 for a rejected field. * * @since 4.0.4 * * @param string $code Error code. * @param string $message Human-readable reason. * @param string $field Field the React form should highlight. * @return WP_Error */ private function invalid( string $code, string $message, string $field ): WP_Error { return new WP_Error( $code, $message, [ 'status' => 400, 'field' => $field, ] ); } /** * Build the 409 response returned when a create / update would * collide with an existing row. * * Carries the conflicting row's id and source in the `data` bag so * the React form can attach the message to the right field (and a * future revision could add an "Edit existing" shortcut). * * @since 4.0.0 * * @param RedirectRow $existing The row already using this source. * * @return WP_Error */ private function duplicateError( RedirectRow $existing ): WP_Error { return new WP_Error( 'rest_duplicate_source', sprintf( /* translators: %s: source URL/path of the existing redirect. */ __( 'A redirect for "%s" already exists. Edit the existing rule instead of creating a duplicate.', '404-to-301' ), $existing->source ), [ 'status' => 409, 'field' => 'source', 'existing_id' => (int) $existing->id, 'source' => (string) $existing->source, ] ); } /** * Shape a RedirectRow into the REST response payload. * * Casts each column to the type the React client expects and resolves * the modifying user's display name so the UI can render it without a * follow-up request. * * @since 4.0.0 * * @param mixed $row Row instance from the data store. Anything other * than a {@see RedirectRow} returns an empty array. * * @return array */ private function shape( $row ): array { if ( ! $row instanceof RedirectRow ) { return []; } $modifiedBy = null === $row->modified_by ? null : (int) $row->modified_by; $modifiedByName = ''; if ( $modifiedBy ) { // `display_name` is the canonical human label across the // admin UI. `get_userdata()` returns false on a stale id so // we degrade gracefully when the user has been deleted. $user = get_userdata( $modifiedBy ); $modifiedByName = $user ? (string) $user->display_name : ''; } return [ 'id' => (int) $row->id, 'source' => (string) $row->source, 'match_type' => (string) $row->match_type, 'target_type' => (string) $row->target_type, 'target_url' => (string) $row->target_url, 'target_page_id' => null === $row->target_page_id ? null : (int) $row->target_page_id, 'redirect_type' => (int) $row->redirect_type, 'is_active' => (bool) $row->is_active, 'hits' => (int) $row->hits, 'last_hit_at' => $row->last_hit_at, 'notes' => $row->notes, 'query_handling' => (string) $row->query_handling, 'modified_by' => $modifiedBy, 'modified_by_name' => $modifiedByName, 'created_at' => $row->created_at, 'updated_at' => $row->updated_at, 'has_linked_log' => RedirectRow::hasLinkedLog( (int) $row->id ), ]; } /** * REST argument schema for the list endpoint. * * @since 4.0.0 * * @return array */ private function listArgs(): array { return [ 'page' => [ 'type' => 'integer', 'default' => 1, ], 'per_page' => [ 'type' => 'integer', 'default' => 20, ], 'orderby' => [ 'type' => 'string', 'default' => 'id', ], 'order' => [ 'type' => 'string', 'enum' => [ 'ASC', 'DESC', 'asc', 'desc' ], 'default' => 'DESC', ], 'search' => [ 'type' => 'string' ], 'match_type' => [ 'type' => 'string', 'enum' => [ 'exact', 'prefix', 'regex' ], ], 'target_type' => [ 'type' => 'string', 'enum' => [ 'link', 'page', 'none' ], ], 'redirect_type' => [ 'type' => 'integer' ], 'is_active' => [ 'type' => 'boolean' ], ]; } /** * REST argument schema for create / update. * * `source` is required on create, optional on update. * * @since 4.0.0 * * @param bool $isCreate Whether this is a create request. * * @return array */ private function writableArgs( bool $isCreate ): array { return [ 'source' => [ 'type' => 'string', 'required' => $isCreate, ], 'match_type' => [ 'type' => 'string', 'enum' => [ 'exact', 'prefix', 'regex' ], ], 'target_type' => [ 'type' => 'string', 'enum' => [ 'link', 'page', 'none' ], ], 'target_url' => [ 'type' => 'string' ], 'target_page_id' => [ 'type' => 'integer' ], 'redirect_type' => [ 'type' => 'integer', // Sourced from the canonical catalogue in // `aioseo404To301()->helpers->redirectStatuses()`. Terminal codes (410/451) // are included — they don't redirect; the front controller // emits the status header and exits. 'enum' => aioseo404To301()->helpers->redirectStatusCodes(), ], 'is_active' => [ 'type' => 'boolean' ], 'notes' => [ 'type' => 'string' ], 'query_handling' => [ 'type' => 'string', 'enum' => [ 'ignore', 'preserve', 'require' ], ], ]; } /** * Pull the writable columns from a request body. * * @since 4.0.0 * * @param WP_REST_Request $request REST request. * @param bool $withDefaults Apply defaults for missing columns. * * @return array Column => value map (only set keys are included). */ private function collectWritable( WP_REST_Request $request, bool $withDefaults ): array { $keys = array_keys( $this->writableArgs( $withDefaults ) ); $data = []; foreach ( $keys as $key ) { $value = $request->get_param( $key ); if ( null === $value ) { continue; } switch ( $key ) { case 'source': case 'target_url': case 'notes': $data[ $key ] = sanitize_text_field( (string) $value ); break; case 'match_type': case 'target_type': $data[ $key ] = (string) $value; break; case 'query_handling': $mode = (string) $value; $data[ $key ] = in_array( $mode, [ 'ignore', 'preserve', 'require' ], true ) ? $mode : 'ignore'; break; case 'target_page_id': case 'redirect_type': $data[ $key ] = (int) $value; break; case 'is_active': $data[ $key ] = (int) (bool) $value; break; default: // Unknown keys are dropped rather than trusted. break; } } // Sensible create-time defaults. if ( $withDefaults ) { $data['match_type'] = $data['match_type'] ?? 'exact'; $data['target_type'] = $data['target_type'] ?? 'link'; $data['redirect_type'] = $data['redirect_type'] ?? 301; $data['is_active'] = $data['is_active'] ?? 1; } return $data; } }