| 1 |
<?php |
| 2 |
|
| 3 |
namespace BetterDocs\WPNotice\Utils; |
| 4 |
|
| 5 |
use function property_exists; |
| 6 |
|
| 7 |
class Storage extends Base { |
| 8 |
private $app = 'wpnotice'; |
| 9 |
private $type = 'options'; |
| 10 |
private $version = '1.0.0'; |
| 11 |
private $storage_key = 'wpnotice_options'; |
| 12 |
|
| 13 |
public function __construct( $args ){ |
| 14 |
$this->app = ! empty( $args['id'] ) ? $args['id'] : $this->app; |
| 15 |
$this->type = ! empty( $args['store'] ) ? $args['store'] : $this->type; |
| 16 |
$this->version = ! empty( $args['version'] ) ? $args['version'] : $this->version; |
| 17 |
$this->storage_key = ! empty( $args['storage_key'] ) ? $this->app . '_' . $args['storage_key'] : $this->storage_key; |
| 18 |
} |
| 19 |
|
| 20 |
public function __get( $name ){ |
| 21 |
return property_exists( $this, $name ) ? $this->$name : null; |
| 22 |
} |
| 23 |
|
| 24 |
public function save( $value, $key = '' ){ |
| 25 |
if( empty( $key ) ) { |
| 26 |
$key = $this->storage_key; |
| 27 |
$value['version'] = $this->version; |
| 28 |
} |
| 29 |
|
| 30 |
if( $this->type === 'options' ) { |
| 31 |
return update_site_option( $key, $value ); |
| 32 |
} |
| 33 |
|
| 34 |
return false; |
| 35 |
} |
| 36 |
|
| 37 |
public function get( $key = '', $default = false ){ |
| 38 |
$key = empty( $key ) ? $this->storage_key : $key; |
| 39 |
|
| 40 |
if( $this->type === 'options' ) { |
| 41 |
return get_site_option( $key, $default ); |
| 42 |
} |
| 43 |
|
| 44 |
return $default; |
| 45 |
} |
| 46 |
|
| 47 |
public function save_meta( $id, $value = true ){ |
| 48 |
return update_user_meta( get_current_user_id(), "{$this->app}_{$id}_notice_dismissed", $value ); |
| 49 |
} |
| 50 |
|
| 51 |
public function get_meta( $id ){ |
| 52 |
return boolval( get_user_meta( get_current_user_id(), "{$this->app}_{$id}_notice_dismissed", true ) ); |
| 53 |
} |
| 54 |
|
| 55 |
public function remove_meta( $id ){ |
| 56 |
return delete_user_meta( get_current_user_id(), "{$this->app}_{$id}_notice_dismissed" ); |
| 57 |
} |
| 58 |
|
| 59 |
} |