| 1 |
<?php |
| 2 |
|
| 3 |
namespace Calotes\Model; |
| 4 |
|
| 5 |
use Calotes\Base\Model; |
| 6 |
use WP_Defender\Component\Error_Code; |
| 7 |
|
| 8 |
class Setting extends Model { |
| 9 |
protected $exclude = array( 'table' ); |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
// we parse the annotations here, and only one |
| 13 |
$this->parse_annotations(); |
| 14 |
$this->before_load(); |
| 15 |
$this->load(); |
| 16 |
$this->after_load(); |
| 17 |
$this->sanitize(); |
| 18 |
} |
| 19 |
|
| 20 |
public function save() { |
| 21 |
$data = $this->prepare_data(); |
| 22 |
$data = json_encode( $data ); |
| 23 |
$ret = update_site_option( $this->table, $data ); |
| 24 |
if ( $ret === false ) { |
| 25 |
$this->internal_logging[] = sprintf( 'Saving fail on %s with data %s', $this->table, json_encode( $data ) ); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Load data |
| 31 |
* |
| 32 |
* @throws \ReflectionException |
| 33 |
*/ |
| 34 |
public function load() { |
| 35 |
$time = microtime( true ); |
| 36 |
if ( empty( $this->table ) ) { |
| 37 |
throw new \Exception( 'Table must be defined before using' ); |
| 38 |
} |
| 39 |
|
| 40 |
$data = get_site_option( $this->table ); |
| 41 |
if ( $data === false ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! is_array( $data ) ) { |
| 46 |
$data = json_decode( $data, true ); |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! is_array( $data ) ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
$data = $this->prepare_data( $data ); |
| 54 |
$this->import( $data ); |
| 55 |
$this->log( sprintf( 'loaded %s - %s', $this->table, microtime( true ) - $time ) ); |
| 56 |
} |
| 57 |
|
| 58 |
public function delete() { |
| 59 |
delete_site_option( $this->table ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Empty function |
| 64 |
*/ |
| 65 |
protected function after_load() { |
| 66 |
return 'For child class to implement'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
protected function before_load() { |
| 73 |
return 'For child class to implement'; |
| 74 |
} |
| 75 |
} |
| 76 |
|