WP_REST_Server::READABLE, 'callback' => 'inspry_toolkit_settings_rest_read', 'permission_callback' => 'inspry_toolkit_settings_rest_read_permission', ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => 'inspry_toolkit_settings_rest_write', 'permission_callback' => 'inspry_toolkit_settings_rest_write_permission', 'args' => array( 'settings' => array( 'description' => 'The settings to change, option name => new value.', 'type' => 'object', 'required' => true, ), 'expected_etag' => array( 'description' => 'The etag from the GET response the change was decided against.', 'type' => 'string', ), 'dry_run' => array( 'description' => 'Report what would change without changing it.', 'type' => 'boolean', 'default' => false, ), ), ), ) ); } /** * Reads: any administrator. * * @return bool */ function inspry_toolkit_settings_rest_read_permission() { return current_user_can( 'manage_options' ); } /** * Writes: an administrator the "Limit the users who can view the Agency Toolkit * Settings Page" setting allows. * * The status must be set explicitly: core passes a WP_Error from a permission * callback through as-is, and one without a status becomes a 500. * * @return true|WP_Error */ function inspry_toolkit_settings_rest_write_permission() { if ( ! current_user_can( 'manage_options' ) ) { return new WP_Error( 'agency_toolkit_forbidden', __( 'Changing Agency Toolkit settings requires the manage_options capability.', 'agency-toolkit' ), array( 'status' => 403 ) ); } if ( ! inspry_toolkit_settings_rest_limit_admins_allows( get_current_user_id() ) ) { return new WP_Error( 'agency_toolkit_access_restricted', __( 'This user is not on the list of administrators allowed to change Agency Toolkit settings.', 'agency-toolkit' ), array( 'status' => 403 ) ); } return true; } /** * The settings page's rule, for the REST write: with the restriction on and a * non-empty list, only listed administrators. An empty list means no restriction * (so an administrator cannot lock everyone out). * * @param int $user_id User to check. * @return bool */ function inspry_toolkit_settings_rest_limit_admins_allows( $user_id ) { if ( ! inspry_toolkit_settings_is_on( get_option( 'inspry_toolkit_limit_admins' ) ) ) { return true; } $list = get_option( 'inspry_toolkit_limit_admins_list' ); if ( ! is_array( $list ) || empty( $list ) ) { return true; } $allowed = array(); foreach ( $list as $id ) { if ( is_scalar( $id ) ) { $allowed[] = (string) $id; } } return in_array( (string) $user_id, $allowed, true ); } /** * Every setting as stored, in canonical form, plus what is wrong with any of them. * * @return array array( 'settings' => array, 'invalid' => array, 'etag' => string ) */ function inspry_toolkit_settings_rest_state() { $schema = inspry_toolkit_settings_schema(); $settings = array(); $invalid = array(); $raw = array(); foreach ( $schema as $name => $spec ) { $raw[ $name ] = get_option( $name ); $settings[ $name ] = inspry_toolkit_settings_normalize( $spec['type'], $raw[ $name ] ); } foreach ( $schema as $name => $spec ) { $parent_on = null === $spec['parent'] ? true : inspry_toolkit_settings_is_on( $raw[ $spec['parent'] ] ); $problem = inspry_toolkit_settings_shape_error( $spec['type'], $raw[ $name ], $parent_on ); if ( null !== $problem ) { $invalid[] = array( 'option' => $name, 'reason' => $problem, ); } } ksort( $settings ); return array( 'settings' => $settings, 'invalid' => $invalid, 'etag' => hash( 'sha256', (string) wp_json_encode( $settings ) ), ); } /** * The address a user sees in the password-changed and email-changed notices. * * Mirrors functions/change_user_notifications_email.php exactly: the stored * sender only when its setting is on and it is a valid address, otherwise * WordPress's own admin_email. * * @return string */ function inspry_toolkit_settings_rest_effective_user_notifications_email() { $sender = get_option( 'inspry_toolkit_change_user_notifications_email_sender' ); $sender = is_scalar( $sender ) ? trim( (string) $sender ) : ''; if ( inspry_toolkit_settings_is_on( get_option( 'inspry_toolkit_change_user_notifications_email' ) ) && is_email( $sender ) ) { return $sender; } return (string) get_option( 'admin_email' ); } /** * Attach the no-cache headers users-summary uses, so an edge cache never serves * one administrator's answer to the next request. * * @param WP_REST_Response $response Response to mark. * @return WP_REST_Response */ function inspry_toolkit_settings_rest_no_cache( $response ) { foreach ( wp_get_nocache_headers() as $name => $value ) { // wp_get_nocache_headers() marks Last-Modified as false, meaning "unset". if ( false === $value ) { continue; } $response->header( $name, $value ); } return $response; } /** * GET. * * @return WP_REST_Response */ function inspry_toolkit_settings_rest_read() { $schema = inspry_toolkit_settings_schema(); $state = inspry_toolkit_settings_rest_state(); $writable = array(); foreach ( $schema as $name => $spec ) { if ( $spec['rest_writable'] ) { $writable[] = $name; } } $response = rest_ensure_response( array( 'version' => INSPRY_TOOLKIT_CURRENT_VERSION, 'etag' => $state['etag'], 'settings' => $state['settings'], 'invalid' => $state['invalid'], 'rest_writable' => $writable, 'admin_email' => (string) get_option( 'admin_email' ), 'effective' => array( 'user_notifications_email' => inspry_toolkit_settings_rest_effective_user_notifications_email(), ), ) ); return inspry_toolkit_settings_rest_no_cache( $response ); } /** * While the route is restoring previous values after a failed write, the email * sanitize callback must store them verbatim, even one that would not pass today. * * @param bool|null $set New state, or null to read. * @return bool */ function inspry_toolkit_settings_rest_restoring( $set = null ) { static $restoring = false; if ( null !== $set ) { $restoring = (bool) $set; } return $restoring; } /** * POST / PUT / PATCH. * * @param WP_REST_Request $request The request. * @return WP_REST_Response|WP_Error */ function inspry_toolkit_settings_rest_write( $request ) { // A GET carrying ?_method=POST still arrives as a GET here. Refuse it. if ( isset( $_SERVER['REQUEST_METHOD'] ) && in_array( strtoupper( (string) $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) { return new WP_Error( 'agency_toolkit_method_not_allowed', __( 'Settings are changed with POST, PUT or PATCH, not GET.', 'agency-toolkit' ), array( 'status' => 405 ) ); } $schema = inspry_toolkit_settings_schema(); $submitted = $request->get_param( 'settings' ); if ( ! is_array( $submitted ) ) { return new WP_Error( 'agency_toolkit_bad_request', __( '"settings" must be an object of option name => value.', 'agency-toolkit' ), array( 'status' => 400 ) ); } foreach ( array_keys( $submitted ) as $name ) { if ( ! isset( $schema[ $name ] ) || ! $schema[ $name ]['rest_writable'] ) { return new WP_Error( 'agency_toolkit_not_writable', /* translators: %s: option name */ sprintf( __( '%s cannot be changed through this route.', 'agency-toolkit' ), (string) $name ), array( 'status' => 403, 'option' => (string) $name, ) ); } } $state = inspry_toolkit_settings_rest_state(); $expected = $request->get_param( 'expected_etag' ); $expected = is_string( $expected ) ? trim( $expected ) : ''; if ( '' === $expected ) { return new WP_Error( 'agency_toolkit_etag_required', __( 'Send expected_etag from a GET of this route; changes are made against a reviewed state.', 'agency-toolkit' ), array( 'status' => 428, 'etag' => $state['etag'], ) ); } if ( ! hash_equals( $state['etag'], $expected ) ) { return new WP_Error( 'agency_toolkit_etag_stale', __( 'The settings changed since that GET. Fetch them again and decide against the current state.', 'agency-toolkit' ), array( 'status' => 412, 'etag' => $state['etag'], ) ); } $errors = array(); $canonical = array(); foreach ( $submitted as $name => $value ) { $result = inspry_toolkit_settings_validate_rest( $schema[ $name ]['type'], $value ); if ( null !== $result['error'] ) { $errors[ $name ] = $result['error']; } else { $canonical[ $name ] = $result['value']; } } /* * Cross-field rules run on the merged state (what is stored, with the submitted * values over it), but only when the request touches one of the fields involved: * a stored inconsistency must not block an unrelated change, or the route could * never be used to repair it one field at a time. */ $merged = array_merge( $state['settings'], $canonical ); $touches = array_intersect_key( $canonical, array( 'inspry_toolkit_change_user_notifications_email' => true, 'inspry_toolkit_change_user_notifications_email_sender' => true, ) ); if ( $touches && '1' === $merged['inspry_toolkit_change_user_notifications_email'] && ! is_email( (string) $merged['inspry_toolkit_change_user_notifications_email_sender'] ) ) { $errors['inspry_toolkit_change_user_notifications_email_sender'] = 'a valid email address is required while "Change admin email address shared in user emails" is on'; } if ( $errors ) { return new WP_Error( 'agency_toolkit_invalid_settings', __( 'One or more settings are invalid; nothing was changed.', 'agency-toolkit' ), array( 'status' => 400, 'errors' => $errors, ) ); } $changed = array(); foreach ( $canonical as $name => $value ) { if ( $state['settings'][ $name ] !== $value ) { $changed[] = array( 'option' => $name, 'old' => $state['settings'][ $name ], 'new' => $value, ); } } if ( $request->get_param( 'dry_run' ) ) { return inspry_toolkit_settings_rest_no_cache( rest_ensure_response( array( 'dry_run' => true, 'changed' => $changed, 'etag' => $state['etag'], ) ) ); } $previous = array(); foreach ( $changed as $change ) { $previous[ $change['option'] ] = get_option( $change['option'] ); update_option( $change['option'], $change['new'] ); } // Read back: the stored value must now normalize to exactly what was sent. $mismatch = null; foreach ( $changed as $change ) { $stored = inspry_toolkit_settings_normalize( $schema[ $change['option'] ]['type'], get_option( $change['option'] ) ); if ( $stored !== $change['new'] ) { $mismatch = $change['option']; break; } } if ( null !== $mismatch ) { /* * Put every changed option back as it was: an option that did not exist is * deleted again, not stored empty. The restore is then verified too, because * whatever rewrote the first write (a filter on pre_update_option, an object * cache) can rewrite this one, and the caller must know whether the site is * back where it was. */ inspry_toolkit_settings_rest_restoring( true ); foreach ( $previous as $name => $raw ) { if ( false === $raw ) { delete_option( $name ); } else { update_option( $name, $raw ); } } inspry_toolkit_settings_rest_restoring( false ); $restored = true; foreach ( $previous as $name => $raw ) { $type = $schema[ $name ]['type']; if ( inspry_toolkit_settings_normalize( $type, get_option( $name ) ) !== inspry_toolkit_settings_normalize( $type, $raw ) ) { $restored = false; break; } } return new WP_Error( 'agency_toolkit_write_failed', $restored /* translators: %s: option name */ ? sprintf( __( '%s did not store the value sent (another plugin may filter it); the previous values were restored.', 'agency-toolkit' ), $mismatch ) /* translators: %s: option name */ : sprintf( __( '%s did not store the value sent, and the previous values could not be restored either; check the site\'s settings.', 'agency-toolkit' ), $mismatch ), array( 'status' => 500, 'option' => $mismatch, 'restored' => $restored, ) ); } $after = inspry_toolkit_settings_rest_state(); return inspry_toolkit_settings_rest_no_cache( rest_ensure_response( array( 'changed' => $changed, 'etag' => $after['etag'], ) ) ); }