llms.abstract.options.data.php 4.26 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
<?php
/**
 * LifterLMS Options Table Data Store abstract class
 *
 * @package LifterLMS/Abstracts/Classes
 *
 * @since 3.8.0
 * @version 4.21.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LifterLMS Options Table Data Store abstract
 *
 * @since 3.8.0
 */
abstract class LLMS_Abstract_Options_Data {

	/**
	 * Option name prefix.
	 *
	 * @var string
	 */
	protected $option_prefix = 'llms_';

	/**
	 * Options data abstract version
	 *
	 * This is used to determine the behavior of the `get_option()` method.
	 *
	 * Concrete classes should use version 2 in order to use the new (future default)
	 * behavior of the method.
	 *
	 * @var int
	 */
	protected $version = 1;

	/**
	 * Retrieve the value of an option from the database
	 *
	 * @since 3.8.0
	 * @since 4.21.0 Changed the behavior of the function when the concrete class defines `$this->version` greater than 1.
	 *
	 * @param string $name     Option name (unprefixed).
	 * @param mixed  $default  Default value to use if no option is found.
	 * @return mixed The option value.
	 */
	public function get_option( $name, $default = false ) {

		$full_name = $this->get_option_name( $name );

		// If the class is version 1, use the old method.
		if ( 1 === $this->version ) {
			// If only one argument is passed switch the default to the old argument default (an empty string).
			$default = 1 === func_num_args() ? '' : $default;
			return $this->get_option_deprecated( $full_name, $default );
		}

		add_filter( "default_option_{$full_name}", array( $this, 'get_option_default_value' ), 10, 3 );

		// Call this way so that the `$passed_default_value` of the filter is accurate based on the number of arguments actually passed.
		$args = func_num_args() > 1 ? array( $full_name, $default ) : array( $full_name );
		$val  = get_option( ...$args );

		remove_filter( "default_option_{$full_name}", array( $this, 'get_option_default_value' ), 10, 3 );

		return $val;

	}

	/**
	 * Retrieve the value of an option from the database
	 *
	 * This is the "old" (to be deprecated) version of the function.
	 *
	 * We will transition extending classes little by little to use the new behavior and deprecate this once
	 * all classes are fully transitioned.
	 *
	 * @since 4.21.0
	 *
	 * @param string $name     Full (prefixed) option name.
	 * @param mixed  $default  Default value to use if no option is found.
	 * @return mixed The option value.
	 */
	private function get_option_deprecated( $name, $default = '' ) {
		$val = get_option( $name, '' );
		if ( '' === $val ) {
			return $default;
		}
		return $val;
	}

	/**
	 * Option default value autoloader
	 *
	 * By default, this method does nothing but extending classes can implement an autoloader to pull
	 * default values from other sources.
	 *
	 * This is a callback function for the WP core filter `default_option_{$option}`.
	 *
	 * @since 4.21.0
	 *
	 * @param mixed  $default_value        The default value. If no value is passed to `get_option()`, this will be an empty string.
	 *                                     Otherwise it will be the default value passed to the method.
	 * @param string $full_option_name     The full (prefixed) option name.
	 * @param bool   $passed_default_value Whether or not a default value was passed to `get_option()`.
	 * @return mixed The default option value.
	 */
	public function get_option_default_value( $default_value, $full_option_name, $passed_default_value ) {
		return $default_value;
	}

	/**
	 * Retrieve a prefix for options
	 *
	 * @since 3.8.0
	 *
	 * @return string
	 */
	protected function get_option_prefix() {
		return $this->option_prefix;
	}

	/**
	 * Retrieve a prefixed option name from the database
	 * Prefix automatically adds a trigger and type to the option name
	 * in addition to llms_notification
	 *
	 * @since 3.8.0
	 *
	 * @param string $name Option name (unprefixed).
	 * @return string
	 */
	public function get_option_name( $name ) {
		return $this->get_option_prefix() . $name;
	}

	/**
	 * Set the value of an option
	 *
	 * @since 3.17.8
	 *
	 * @param string $name  Option name (unprefixed).
	 * @param mixed  $value Option value.
	 * @return bool Returns `true` if option value has changed and `false` if no update or the update failed.
	 */
	public function set_option( $name, $value ) {
		return update_option( $this->get_option_name( $name ), $value );
	}

}