option_group = $option_group; $this->full_option_name = $this->prefix . $option_name; $this->option_name = $option_name; $this->autoload = array_key_exists( 'autoload', $args ) ? $args['autoload'] : null; $this->args = array_diff_key( $args, [ 'autoload' => '' ] ); } /** * Call registration hooks. * * @return void */ public function register() { add_action( 'admin_init', [ $this, 'registerSetting' ] ); add_action( 'rest_api_init', [ $this, 'registerSetting' ] ); if ( null !== $this->autoload ) { add_filter( 'wp_default_autoload_value', [ $this, 'filterAutoload' ], 10, 3 ); } } /** * Filter the default autoload value for this option. * * @param string|null $autoload The default autoload value. * @param string $option The option name. * @param mixed $value The option value. * * @return string|null */ public function filterAutoload( $autoload, $option, $value ) { if ( $this->full_option_name !== $option ) { return $autoload; } return $this->autoload ? 'yes' : 'no'; } /** * Register the setting * * @return void */ public function registerSetting() { register_setting( $this->option_group, $this->full_option_name, $this->args, ); } }