'GET', 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'admin_permission_check' ), 'args' => array(), ), array( 'methods' => 'POST', 'callback' => array( $this, 'update_settings' ), 'permission_callback' => array( $this, 'admin_permission_check' ), 'args' => $this->schema(), ), ) ); } /** * Current settings, plus the read-only facts the screen shows. * * @return \WP_REST_Response */ public function get_settings() { return rest_ensure_response( $this->prepare( Plugin_Settings::all() ) ); } /** * Write settings. * * @param WP_REST_Request $request The request. * @return \WP_REST_Response */ public function update_settings( WP_REST_Request $request ) { $incoming = array(); foreach ( array_keys( $this->schema() ) as $key ) { if ( null !== $request->get_param( $key ) ) { $incoming[ $key ] = $request->get_param( $key ); } } return rest_ensure_response( $this->prepare( Plugin_Settings::update( $incoming ) ) ); } /** * Settings plus the package directory, which is reported and never set. * * @param array $settings Stored settings. * @return array */ private function prepare( array $settings ): array { $dir = Package_Store::dir(); // Shown from the WordPress root down; the server path above it is noise to the user. $settings['package_dir'] = is_wp_error( $dir ) ? '' : str_replace( wp_normalize_path( ABSPATH ), '', wp_normalize_path( $dir ) ); return $settings; } /** * The writable settings, as a REST args schema. * * @return array> */ private function schema(): array { return array( 'default_conflict' => array( 'description' => __( 'What to do when content already exists.', 'transtrix' ), 'type' => 'string', 'enum' => Conflict_Resolver::strategies(), 'sanitize_callback' => 'sanitize_key', 'validate_callback' => static function ( $value ) { return in_array( (string) $value, Conflict_Resolver::strategies(), true ); }, ), 'restore_retention_days' => array( 'description' => __( 'How long restore points are kept.', 'transtrix' ), 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => static function ( $value ) { return (int) $value >= 1 && (int) $value <= 365; }, ), 'full_site_packages' => array( 'description' => __( 'How many entire-site packages are kept.', 'transtrix' ), 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => static function ( $value ) { return (int) $value >= 1 && (int) $value <= Job_Store::LIMIT; }, ), 'capability' => array( 'description' => __( 'Capability a user needs to import or export.', 'transtrix' ), 'type' => 'string', 'enum' => Plugin_Settings::CAPABILITIES, 'sanitize_callback' => 'sanitize_key', 'validate_callback' => static function ( $value ) { return in_array( (string) $value, Plugin_Settings::CAPABILITIES, true ); }, ), 'delete_on_uninstall' => array( 'description' => __( 'Delete packages, reports and restore points on uninstall.', 'transtrix' ), 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'validate_callback' => static function ( $value ) { return is_bool( rest_sanitize_boolean( $value ) ); }, ), ); } }