shopping_vendors = $shopping_vendors; } /** * Register settings. * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @since 1.0.0 */ public function register(): void { register_setting( self::SETTING_GROUP, self::SETTING_NAME_TRACKING_ID, [ 'description' => __( 'Google Analytics Tracking ID', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_USING_LEGACY_ANALYTICS, [ 'description' => __( 'Using legacy analytics configuration', 'web-stories' ), 'type' => 'boolean', 'default' => false, 'show_in_rest' => true, 'sanitize_callback' => 'rest_sanitize_boolean', ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_AD_NETWORK, [ 'description' => __( 'Ad Network', 'web-stories' ), 'type' => 'string', 'default' => 'none', 'enum' => [ 'none', 'adsense', 'admanager' ], 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_ADSENSE_PUBLISHER_ID, [ 'description' => __( 'Google AdSense Publisher ID', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_ADSENSE_SLOT_ID, [ 'description' => __( 'Google AdSense Slot ID', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_AD_MANAGER_SLOT_ID, [ 'description' => __( 'Google Ad Manager Slot ID', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_MGID_WIDGET_ID, [ 'description' => __( 'MGID Widget ID', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_ACTIVE_PUBLISHER_LOGO, [ 'description' => __( 'Default Publisher Logo', 'web-stories' ), 'type' => 'integer', 'default' => 0, 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_PUBLISHER_LOGOS, [ 'description' => __( 'Publisher Logos', 'web-stories' ), 'type' => 'array', 'default' => [], 'show_in_rest' => [ 'schema' => [ 'items' => [ 'type' => 'integer', ], ], ], // WPGraphQL errors when encountering array or object types. // See https://github.com/wp-graphql/wp-graphql/issues/2065. 'show_in_graphql' => false, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_VIDEO_CACHE, [ 'description' => __( 'Video Cache', 'web-stories' ), 'type' => 'boolean', 'default' => false, 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_DATA_REMOVAL, [ 'description' => __( 'Data Removal', 'web-stories' ), 'type' => 'boolean', 'default' => false, 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_ARCHIVE, [ 'description' => __( 'Web Stories Archive', 'web-stories' ), 'type' => 'string', 'default' => 'default', 'show_in_rest' => [ 'schema' => [ 'type' => 'string', 'enum' => [ 'default', 'disabled', 'custom' ], ], ], ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_ARCHIVE_PAGE_ID, [ 'description' => __( 'Web Stories Archive Page ID', 'web-stories' ), 'type' => 'integer', 'default' => 0, 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP_EXPERIMENTS, self::SETTING_NAME_EXPERIMENTS, [ 'description' => __( 'Experiments', 'web-stories' ), 'type' => 'object', 'default' => [], 'show_in_rest' => [ 'schema' => [ 'properties' => [], 'additionalProperties' => true, ], ], // WPGraphQL errors when encountering array or object types. // See https://github.com/wp-graphql/wp-graphql/issues/2065. 'show_in_graphql' => false, ] ); $vendors = $this->shopping_vendors->get_vendors(); $vendor_options = array_keys( $vendors ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_SHOPPING_PROVIDER, [ 'description' => __( 'Shopping provider', 'web-stories' ), 'type' => 'string', 'default' => 'none', 'enum' => $vendor_options, 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_SHOPIFY_HOST, [ 'description' => __( 'Shopify Host', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_SHOPIFY_ACCESS_TOKEN, [ 'description' => __( 'Shopify API Access Token', 'web-stories' ), 'type' => 'string', 'default' => '', 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_AUTO_ADVANCE, [ 'description' => __( 'Auto-advance', 'web-stories' ), 'type' => 'boolean', 'default' => true, 'show_in_rest' => true, ] ); register_setting( self::SETTING_GROUP, self::SETTING_NAME_DEFAULT_PAGE_DURATION, [ 'description' => __( 'Default Page Duration', 'web-stories' ), 'type' => 'number', 'default' => 7, 'show_in_rest' => true, ] ); } /** * Returns the value for a given setting. * * @SuppressWarnings(PHPMD.BooleanArgumentFlag) * * @since 1.12.0 * * @param string $key Setting key. * @param mixed $default_value Optional. Default value to return if the option does not exist. * @return string|array|bool|int Setting value. */ public function get_setting( string $key, $default_value = false ) { // Distinguish between `false` as a default, and not passing one, just like WordPress. $passed_default = \func_num_args() > 1; if ( $passed_default ) { /** * Setting value. * * @var string|array|bool */ $option = get_option( $key, $default_value ); if ( $option === $default_value ) { return $option; } } else { /** * Setting value. * * @var string|array|bool */ $option = get_option( $key ); } $settings = $this->get_registered_options(); if ( isset( $settings[ $key ] ) ) { $value = rest_sanitize_value_from_schema( $option, $settings[ $key ] ); if ( is_wp_error( $value ) ) { return $option; } /** * Setting value. * * @var string|array|bool */ $option = $value; } return $option; } /** * Updates the given setting with a new value. * * @since 1.12.0 * * @param string $key Setting key. * @param mixed $value Setting value. * @return mixed Setting value. */ public function update_setting( string $key, $value ) { return update_option( $key, $value ); } /** * Act on plugin uninstall. * * @since 1.26.0 */ public function on_plugin_uninstall(): void { delete_option( self::SETTING_NAME_ARCHIVE ); delete_option( self::SETTING_NAME_EXPERIMENTS ); delete_option( self::SETTING_NAME_TRACKING_ID ); delete_option( self::SETTING_NAME_USING_LEGACY_ANALYTICS ); delete_option( self::SETTING_NAME_AD_NETWORK ); delete_option( self::SETTING_NAME_ADSENSE_PUBLISHER_ID ); delete_option( self::SETTING_NAME_ADSENSE_SLOT_ID ); delete_option( self::SETTING_NAME_AD_MANAGER_SLOT_ID ); delete_option( self::SETTING_NAME_MGID_WIDGET_ID ); delete_option( self::SETTING_NAME_ACTIVE_PUBLISHER_LOGO ); delete_option( self::SETTING_NAME_PUBLISHER_LOGOS ); delete_option( self::SETTING_NAME_VIDEO_CACHE ); delete_option( self::SETTING_NAME_DATA_REMOVAL ); delete_option( self::SETTING_NAME_ARCHIVE ); delete_option( self::SETTING_NAME_ARCHIVE_PAGE_ID ); delete_option( self::SETTING_NAME_SHOPPING_PROVIDER ); delete_option( self::SETTING_NAME_SHOPIFY_HOST ); delete_option( self::SETTING_NAME_SHOPIFY_ACCESS_TOKEN ); delete_option( self::SETTING_NAME_DEFAULT_PAGE_DURATION ); delete_option( self::SETTING_NAME_AUTO_ADVANCE ); } /** * Retrieves all of the registered options for the Settings API. * Inspired by get_registered_options method found in WordPress. But also get settings that are registered without `show_in_rest` property. * * @since 1.28.0 * * @link https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php#L211-L267 * * @return array> Array of registered options. */ protected function get_registered_options(): array { $rest_options = []; foreach ( get_registered_settings() as $name => $args ) { $rest_args = []; if ( ! empty( $args['show_in_rest'] ) && \is_array( $args['show_in_rest'] ) ) { $rest_args = $args['show_in_rest']; } $defaults = [ 'name' => ! empty( $rest_args['name'] ) ? $rest_args['name'] : $name, 'schema' => [], ]; $rest_args = array_merge( $defaults, $rest_args ); $default_schema = [ 'type' => empty( $args['type'] ) ? null : $args['type'], 'description' => empty( $args['description'] ) ? '' : $args['description'], 'default' => $args['default'] ?? null, ]; $schema = array_merge( $default_schema, $rest_args['schema'] ); $schema = rest_default_additional_properties_to_false( $schema ); $rest_options[ $name ] = $schema; } return $rest_options; } }