| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base model for managing settings. |
| 4 |
* |
| 5 |
* @package Calotes\Model |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Calotes\Model; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
use Calotes\Base\Model; |
| 12 |
|
| 13 |
/** |
| 14 |
* Base model for managing settings. |
| 15 |
*/ |
| 16 |
class Setting extends Model { |
| 17 |
|
| 18 |
/** |
| 19 |
* The excluded properties. |
| 20 |
* |
| 21 |
* @var string[] |
| 22 |
*/ |
| 23 |
protected $exclude = array( 'table' ); |
| 24 |
|
| 25 |
/** |
| 26 |
* The old settings. |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $old_settings = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor method for the Setting class. |
| 34 |
* This method is responsible for parsing annotations, executing the before_load, load, after_load, and sanitize |
| 35 |
* methods. |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
// We parse the annotations here, and only one. |
| 39 |
$this->parse_annotations(); |
| 40 |
$this->before_load(); |
| 41 |
$this->load(); |
| 42 |
$this->after_load(); |
| 43 |
$this->sanitize(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Saves the data by updating the site option with the prepared data. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function save() { |
| 52 |
$prepared_data = $this->prepare_data(); |
| 53 |
$data = wp_json_encode( $prepared_data ); |
| 54 |
$ret = update_site_option( $this->table, $data ); |
| 55 |
if ( false === $ret ) { |
| 56 |
$this->internal_logging[] = sprintf( |
| 57 |
'Saving fail on %s with data %s.', |
| 58 |
$this->table, |
| 59 |
wp_json_encode( $data ) |
| 60 |
); |
| 61 |
} else { |
| 62 |
/** |
| 63 |
* Handle settings update. No from WP CLI commands. |
| 64 |
* |
| 65 |
* @param array $old_settings The old option values. |
| 66 |
* @param array $prepared_data The new option values. |
| 67 |
* |
| 68 |
* @since 4.2.0 |
| 69 |
*/ |
| 70 |
do_action( 'wd_settings_update', $this->old_settings, $prepared_data ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Load data. |
| 76 |
* |
| 77 |
* @throws Exception Table must be defined before using. |
| 78 |
*/ |
| 79 |
public function load() { |
| 80 |
if ( '' === $this->table ) { |
| 81 |
throw new Exception( 'Table must be defined before using.' ); |
| 82 |
} |
| 83 |
|
| 84 |
$data = get_site_option( $this->table ); |
| 85 |
if ( false === $data ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if ( ! is_array( $data ) ) { |
| 90 |
$data = json_decode( $data, true ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! is_array( $data ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$data = $this->prepare_data( $data ); |
| 98 |
$this->old_settings = $data; |
| 99 |
$this->import( $data ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Deletes the site option associated with the current table. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function delete() { |
| 108 |
delete_site_option( $this->table ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Hook method called after the data is loaded. |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
protected function after_load(): void { |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Hook method called before the data is loaded. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
protected function before_load(): void { |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get table name. |
| 129 |
* |
| 130 |
* @return string |
| 131 |
*/ |
| 132 |
public function get_table(): string { |
| 133 |
return $this->table; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get old setting values. |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function get_old_settings(): array { |
| 142 |
return $this->old_settings; |
| 143 |
} |
| 144 |
} |
| 145 |
|