class-llms-helper-options.php 3.15 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 153 154 155 156 157 158 159 160 161
<?php
/**
 * Get & Set Helper options
 *
 * @package LifterLMS_Helper/Classes
 *
 * @since 3.0.0
 * @version 3.2.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_Helper_Options
 *
 * @since 3.0.0
 * @since 3.2.0 Use `$instance` in favor of `$_instance`.
 */
class LLMS_Helper_Options {

	/**
	 * Singleton instance
	 *
	 * @var null|LLMS_Helper_Options
	 */
	protected static $instance = null;

	/**
	 * Main Instance
	 *
	 * @since 3.0.0
	 * @since 3.2.0 Use `self::$instance` in favor of `self::$_instance`.
	 *
	 * @return LLMS_Helper_Options
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Retrive a single option
	 *
	 * @since 3.0.0
	 *
	 * @param string $key     Option name.
	 * @param mixed  $default Default option value if option isn't already set.
	 * @return mixed
	 */
	private function get_option( $key, $default = '' ) {

		$options = $this->get_options();

		if ( isset( $options[ $key ] ) ) {
			return $options[ $key ];
		}

		return $default;

	}

	/**
	 * Retrieve all upgrader options array
	 *
	 * @since 3.0.0
	 *
	 * @return array
	 */
	private function get_options() {
		return get_option( 'llms_helper_options', array() );
	}

	/**
	 * Update the value of an option
	 *
	 * @since 3.0.0
	 *
	 * @param string $key Option name.
	 * @param mixed  $val Option value.
	 * @return boolean True if option value has changed, false if not or if update failed.
	 */
	private function set_option( $key, $val ) {

		$options         = $this->get_options();
		$options[ $key ] = $val;
		return update_option( 'llms_helper_options', $options, false );

	}

	/**
	 * Get info about addon channel subscriptions
	 *
	 * @since 3.0.0
	 *
	 * @return array
	 */
	public function get_channels() {
		return $this->get_option( 'channels', array() );
	}

	/**
	 * Set info about addon channel subscriptions
	 *
	 * @since 3.0.0
	 *
	 * @param array $channels Array of channel information.
	 * @return boolean True if option value has changed, false if not or if update failed.
	 */
	public function set_channels( $channels ) {
		return $this->set_option( 'channels', $channels );
	}

	/**
	 * Retrieve a timestamp for the last time the keys check cron was run
	 *
	 * @since 3.0.0
	 *
	 * @return int
	 */
	public function get_last_keys_cron_check() {
		return $this->get_option( 'last_keys_cron_check', 0 );
	}

	/**
	 * Set the last cron check time
	 *
	 * @since 3.0.0
	 *
	 * @param int $time Timestamp.
	 * @return boolean True if option value has changed, false if not or if update failed.
	 */
	public function set_last_keys_cron_check( $time ) {
		return $this->set_option( 'last_keys_cron_check', $time );
	}

	/**
	 * Retrieve saved license key data
	 *
	 * @since 3.0.0
	 *
	 * @return array
	 */
	public function get_license_keys() {
		return $this->get_option( 'license_keys', array() );
	}

	/**
	 * Update saved license key data
	 *
	 * @since 3.0.0
	 *
	 * @param array $keys Key data to save.
	 * @return boolean True if option value has changed, false if not or if update failed.
	 */
	public function set_license_keys( $keys ) {
		return $this->set_option( 'license_keys', $keys );
	}

}