add_option()

最后更新于:2021-11-25 19:22:43

add_option( string$option, mixed$value=”, string$deprecated=”, string|bool$autoload=’yes’)

Adds a new option.

参数

$option

(string) (Required) Name of the option to add. Expected to not be SQL-escaped.

$value

(mixed) (Optional) Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.

Default value: ”

$deprecated

(string) (Optional) Description. Not used anymore.

Default value: ”

$autoload

(string|bool) (Optional) Whether to load the option when GeChiUI starts up. Default is enabled. Accepts ‘no’ to disable for legacy reasons.

Default value: ‘yes’

响应

(bool) True if the option was added, false otherwise.

源文件

文件: gc-includes/option.php

function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
	global $gcdb;

	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '2.3.0' );
	}

	$option = trim( $option );
	if ( empty( $option ) ) {
		return false;
	}

	/*
	 * Until a proper _deprecated_option() function can be introduced,
	 * redirect requests to deprecated keys to the new, correct ones.
	 */
	$deprecated_keys = array(
		'blacklist_keys'    => 'disallowed_keys',
		'comment_whitelist' => 'comment_previously_approved',
	);

	if ( ! gc_installing() && isset( $deprecated_keys[ $option ] ) ) {
		_deprecated_argument(
			__FUNCTION__,
			'5.5.0',
			sprintf(
				/* translators: 1: Deprecated option key, 2: New option key. */
				__( 'The "%1$s" option key has been renamed to "%2$s".' ),
				$option,
				$deprecated_keys[ $option ]
			)
		);
		return add_option( $deprecated_keys[ $option ], $value, $deprecated, $autoload );
	}

	gc_protect_special_option( $option );

	if ( is_object( $value ) ) {
		$value = clone $value;
	}

	$value = sanitize_option( $option, $value );

	// Make sure the option doesn't already exist.
	// We can check the 'notoptions' cache before we ask for a DB query.
	$notoptions = gc_cache_get( 'notoptions', 'options' );

	if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) {
		/** This filter is documented in gc-includes/option.php */
		if ( apply_filters( "default_option_{$option}", false, $option, false ) !== get_option( $option ) ) {
			return false;
		}
	}

	$serialized_value = maybe_serialize( $value );
	$autoload         = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes';

	/**
	 * Fires before an option is added.
	 *
	 * @since 2.9.0
	 *
	 * @param string $option Name of the option to add.
	 * @param mixed  $value  Value of the option.
	 */
	do_action( 'add_option', $option, $value );

	$result = $gcdb->query( $gcdb->prepare( "INSERT INTO `$gcdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) );
	if ( ! $result ) {
		return false;
	}

	if ( ! gc_installing() ) {
		if ( 'yes' === $autoload ) {
			$alloptions            = gc_load_alloptions( true );
			$alloptions[ $option ] = $serialized_value;
			gc_cache_set( 'alloptions', $alloptions, 'options' );
		} else {
			gc_cache_set( $option, $serialized_value, 'options' );
		}
	}

	// This option exists now.
	$notoptions = gc_cache_get( 'notoptions', 'options' ); // Yes, again... we need it to be fresh.

	if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
		unset( $notoptions[ $option ] );
		gc_cache_set( 'notoptions', $notoptions, 'options' );
	}

	/**
	 * Fires after a specific option has been added.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the option name.
	 *
	 * @since 2.5.0 As "add_option_{$name}"
	 * @since 3.0.0
	 *
	 * @param string $option Name of the option to add.
	 * @param mixed  $value  Value of the option.
	 */
	do_action( "add_option_{$option}", $option, $value );

	/**
	 * Fires after an option has been added.
	 *
	 * @since 2.9.0
	 *
	 * @param string $option Name of the added option.
	 * @param mixed  $value  Value of the option.
	 */
	do_action( 'added_option', $option, $value );

	return true;
}