| @@ -1,5 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * DataModel class file. | |
| 4 | + * | |
| 5 | + * Provides a simple option storage abstraction with | |
| 6 | + * source-based singleton instances and optional caching. | |
| 7 | + * | |
| 8 | + * @package RadiusTheme\SB\Models | |
| 9 | + */ | |
| 2 | 10 | |
| 3 | 11 | namespace RadiusTheme\SB\Models; |
| 4 | 12 | |
| 5 | 13 | // Do not allow directly accessing this file. |
| @@ -6,16 +14,47 @@ | ||
| 6 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | 15 | exit( 'This script cannot be accessed directly.' ); |
| 8 | 16 | } |
| 9 | 17 | |
| 18 | +/** | |
| 19 | + * Class DataModel | |
| 20 | + * | |
| 21 | + * Handles grouped WordPress options using a prefixed database key. | |
| 22 | + * Supports multiple data sources via singleton instances. | |
| 23 | + */ | |
| 10 | 24 | class DataModel { |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Option key used in the WordPress options table. | |
| 28 | + * | |
| 29 | + * @var string | |
| 30 | + */ | |
| 11 | 31 | private $db_key; |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Cached instances per data source. | |
| 35 | + * | |
| 36 | + * @var array<string, self> | |
| 37 | + */ | |
| 12 | 38 | private static $instances = []; |
| 13 | 39 | |
| 40 | + /** | |
| 41 | + * DataModel constructor. | |
| 42 | + * | |
| 43 | + * Private to enforce singleton usage via source(). | |
| 44 | + * | |
| 45 | + * @param string $source Data source identifier. | |
| 46 | + */ | |
| 14 | 47 | private function __construct( $source ) { |
| 15 | 48 | $this->set_db_key( $source ); |
| 16 | 49 | } |
| 17 | 50 | |
| 51 | + /** | |
| 52 | + * Get DataModel instance for a specific source. | |
| 53 | + * | |
| 54 | + * @param string $source Data source name. | |
| 55 | + * @return self | |
| 56 | + */ | |
| 18 | 57 | public static function source( $source = 'settings' ) { |
| 19 | 58 | if ( ! isset( self::$instances[ $source ] ) ) { |
| 20 | 59 | self::$instances[ $source ] = new self( $source ); |
| 21 | 60 | } |
| @@ -22,22 +61,43 @@ | ||
| 22 | 61 | |
| 23 | 62 | return self::$instances[ $source ]; |
| 24 | 63 | } |
| 25 | 64 | |
| 65 | + /** | |
| 66 | + * Build and set the database option key. | |
| 67 | + * | |
| 68 | + * @param string $source Data source identifier. | |
| 69 | + * @return void | |
| 70 | + */ | |
| 26 | 71 | private function set_db_key( $source ) { |
| 27 | 72 | $this->db_key = 'rtsb_' . $source; |
| 28 | 73 | } |
| 29 | 74 | |
| 75 | + /** | |
| 76 | + * Retrieve an option value by key. | |
| 77 | + * | |
| 78 | + * @param string $key Option key. | |
| 79 | + * @param mixed $default Default value if option does not exist. | |
| 80 | + * @param bool $cache Whether to use global cache. | |
| 81 | + * @return mixed | |
| 82 | + */ | |
| 30 | 83 | public function get_option( $key, $default = null, $cache = true ) { |
| 31 | 84 | if ( $cache && isset( $GLOBALS[ $this->db_key ] ) ) { |
| 32 | 85 | $db = $GLOBALS[ $this->db_key ]; |
| 33 | 86 | } else { |
| 34 | 87 | $db = get_option( $this->db_key, [] ); |
| 35 | - $GLOBALS[ $this->db_key ] = $db; | |
| 88 | + $GLOBALS[ $this->db_key ] = $db; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound | |
| 36 | 89 | } |
| 37 | 90 | return $db[ $key ] ?? $default; |
| 38 | 91 | } |
| 39 | 92 | |
| 93 | + /** | |
| 94 | + * Update or create an option value. | |
| 95 | + * | |
| 96 | + * @param string $key Option key. | |
| 97 | + * @param mixed $value Option value. | |
| 98 | + * @return bool True on success, false on failure. | |
| 99 | + */ | |
| 40 | 100 | public function set_option( $key, $value ) { |
| 41 | 101 | $db = get_option( $this->db_key, [] ); |
| 42 | 102 | |
| 43 | 103 | if ( is_object( $db ) ) { |
| @@ -52,8 +112,14 @@ | ||
| 52 | 112 | |
| 53 | 113 | return update_option( $this->db_key, $db ); |
| 54 | 114 | } |
| 55 | 115 | |
| 116 | + /** | |
| 117 | + * Delete an option value by key. | |
| 118 | + * | |
| 119 | + * @param string $key Option key. | |
| 120 | + * @return bool True on success, false on failure. | |
| 121 | + */ | |
| 56 | 122 | public function delete_option( $key ) { |
| 57 | 123 | $db = get_option( $this->db_key, [] ); |
| 58 | 124 | if ( isset( $db[ $key ] ) ) { |
| 59 | 125 | unset( $db[ $key ] ); |