Admin
6 years ago
Ajax
6 years ago
Autoloader
6 years ago
Capabilities
6 years ago
Check
6 years ago
Column
6 years ago
Deprecated
6 years ago
Exception
6 years ago
Form
6 years ago
Helper
6 years ago
Integration
6 years ago
ListScreen
6 years ago
Message
6 years ago
Meta
6 years ago
Plugin
6 years ago
Preferences
6 years ago
Relation
6 years ago
Request
6 years ago
Response
6 years ago
Screen
6 years ago
Settings
6 years ago
Storage
6 years ago
Table
6 years ago
ThirdParty
6 years ago
Transient
6 years ago
API.php
6 years ago
Addon.php
6 years ago
Admin.php
6 years ago
AdminColumns.php
6 years ago
ArrayIterator.php
6 years ago
Autoloader.php
6 years ago
Builder.php
6 years ago
Capabilities.php
6 years ago
Collection.php
6 years ago
Column.php
6 years ago
Config.php
6 years ago
DefaultColumns.php
6 years ago
Dependencies.php
6 years ago
Expirable.php
6 years ago
Groups.php
6 years ago
Helper.php
6 years ago
Integration.php
6 years ago
IntegrationFactory.php
6 years ago
Integrations.php
6 years ago
ListScreen.php
6 years ago
ListScreenFactory.php
6 years ago
ListScreenGroups.php
6 years ago
ListScreenPost.php
6 years ago
ListScreenWP.php
6 years ago
Message.php
6 years ago
MetaType.php
6 years ago
Middleware.php
6 years ago
Plugin.php
6 years ago
PluginInformation.php
6 years ago
Preferences.php
6 years ago
Registrable.php
6 years ago
Relation.php
6 years ago
Request.php
6 years ago
Screen.php
6 years ago
ScreenController.php
6 years ago
Settings.php
6 years ago
Transient.php
6 years ago
TypedArrayIterator.php
6 years ago
View.php
6 years ago
Preferences.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC; |
| 4 | |
| 5 | abstract class Preferences { |
| 6 | |
| 7 | /** |
| 8 | * @var int |
| 9 | */ |
| 10 | private $user_id; |
| 11 | |
| 12 | /** |
| 13 | * The label for this set of preferences |
| 14 | * @var string |
| 15 | */ |
| 16 | private $label; |
| 17 | |
| 18 | /** |
| 19 | * Preferences of this user |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $data = array(); |
| 23 | |
| 24 | /** |
| 25 | * Retrieves data from DB |
| 26 | * return array|false |
| 27 | */ |
| 28 | abstract protected function load(); |
| 29 | |
| 30 | /** |
| 31 | * Stores data to DB |
| 32 | * @return bool |
| 33 | */ |
| 34 | abstract public function save(); |
| 35 | |
| 36 | /** |
| 37 | * @param string $label |
| 38 | * @param null|int $user_id |
| 39 | */ |
| 40 | public function __construct( $label, $user_id = null ) { |
| 41 | if ( null === $user_id ) { |
| 42 | $user_id = get_current_user_id(); |
| 43 | } |
| 44 | |
| 45 | $this->user_id = intval( $user_id ); |
| 46 | $this->label = sanitize_key( (string) $label ); |
| 47 | |
| 48 | $data = $this->load(); |
| 49 | |
| 50 | if ( is_array( $data ) ) { |
| 51 | foreach ( $data as $k => $v ) { |
| 52 | $this->set( $k, $v, false ); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Return the key used to store and retrieve this preference |
| 59 | * @return string |
| 60 | */ |
| 61 | protected function get_key() { |
| 62 | return 'ac_preferences_' . $this->label; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return int |
| 67 | */ |
| 68 | protected function get_user_id() { |
| 69 | return $this->user_id; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param string $key |
| 74 | * |
| 75 | * @return mixed |
| 76 | */ |
| 77 | public function get( $key ) { |
| 78 | if ( ! isset( $this->data[ $key ] ) ) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | return $this->data[ $key ]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param string $key |
| 87 | * @param mixed $data |
| 88 | * @param bool $save Immediately save changes to database |
| 89 | * |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function set( $key, $data, $save = true ) { |
| 93 | $this->data[ $key ] = $data; |
| 94 | |
| 95 | if ( $save ) { |
| 96 | return $this->save(); |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param string $key |
| 104 | * @param bool $save Immediately save changes to database |
| 105 | * |
| 106 | * @return bool |
| 107 | */ |
| 108 | public function delete( $key, $save = true ) { |
| 109 | if ( ! $this->get( $key ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | unset( $this->data[ $key ] ); |
| 114 | |
| 115 | if ( $save ) { |
| 116 | return $this->save(); |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reset site preferences for all users that match on the current label |
| 124 | */ |
| 125 | public function reset_for_all_users() { |
| 126 | if ( empty( $this->label ) ) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | global $wpdb; |
| 131 | |
| 132 | $sql = " |
| 133 | DELETE |
| 134 | FROM {$wpdb->usermeta} |
| 135 | WHERE meta_key LIKE %s |
| 136 | "; |
| 137 | |
| 138 | $sql = $wpdb->prepare( $sql, $wpdb->esc_like( $wpdb->get_blog_prefix() . $this->get_key() ) . '%' ); |
| 139 | |
| 140 | return (bool) $wpdb->query( $sql ); |
| 141 | } |
| 142 | |
| 143 | } |