| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpModel' ) ) : |
| 8 |
|
| 9 |
final class MywpModel { |
| 10 |
|
| 11 |
private $id = false; |
| 12 |
private $model_id = false; |
| 13 |
private $type = false; |
| 14 |
private $network = false; |
| 15 |
|
| 16 |
private $initial_data = false; |
| 17 |
private $default_data = false; |
| 18 |
|
| 19 |
private $option_key = false; |
| 20 |
private $option_autoload = false; |
| 21 |
|
| 22 |
private $pre_get_option = false; |
| 23 |
private $option = false; |
| 24 |
private $change_get_option = false; |
| 25 |
|
| 26 |
private $pre_setting_data = false; |
| 27 |
private $setting_data = false; |
| 28 |
private $change_setting_data = false; |
| 29 |
|
| 30 |
public function __construct( $model_id = false , $type = false , $network = false ) { |
| 31 |
|
| 32 |
if( empty( $model_id ) ) { |
| 33 |
|
| 34 |
$called_text = sprintf( 'new %s( %s , %s , %s )' , __CLASS__ , '$model_id' , '$type' , '$network' ); |
| 35 |
|
| 36 |
MywpHelper::error_require_message( '$model_id' , $called_text ); |
| 37 |
|
| 38 |
return false; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
$this->model_id = strip_tags( $model_id ); |
| 43 |
|
| 44 |
if( empty( $type ) ) { |
| 45 |
|
| 46 |
$called_text = sprintf( 'new %s( %s , %s , %s )' , __CLASS__ , '$model_id' , '$type' , '$network' ); |
| 47 |
|
| 48 |
MywpHelper::error_require_message( '$type' , $called_text ); |
| 49 |
|
| 50 |
return false; |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
$this->type = strip_tags( $type ); |
| 55 |
|
| 56 |
$this->id = "mywp_{$this->model_id}"; |
| 57 |
|
| 58 |
if( ! empty( $network ) ) { |
| 59 |
|
| 60 |
$this->network = true; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
public function get_id() { |
| 67 |
|
| 68 |
return $this->id; |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public function get_type() { |
| 73 |
|
| 74 |
return $this->type; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
public function is_network() { |
| 79 |
|
| 80 |
if( ! empty( $this->network ) ) { |
| 81 |
|
| 82 |
return true; |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
return false; |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public function set_initial_data( $initial_data = false ) { |
| 91 |
|
| 92 |
$this->initial_data = $initial_data; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
public function get_initial_data() { |
| 97 |
|
| 98 |
return $this->initial_data; |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
public function set_default_data( $default_data = false ) { |
| 103 |
|
| 104 |
$this->default_data = $default_data; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
public function get_default_data() { |
| 109 |
|
| 110 |
return $this->default_data; |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
public function get_option_key() { |
| 115 |
|
| 116 |
$id = $this->get_id(); |
| 117 |
|
| 118 |
if( empty( $id ) ) { |
| 119 |
|
| 120 |
return false; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
$this->option_key = apply_filters( "mywp_model_get_option_key_{$id}" , $id , $this->get_type() , $this->is_network() ); |
| 125 |
|
| 126 |
return $this->option_key; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
public function get_option_autoload() { |
| 131 |
|
| 132 |
$id = $this->get_id(); |
| 133 |
|
| 134 |
if( empty( $id ) ) { |
| 135 |
|
| 136 |
return false; |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
$this->option_autoload = apply_filters( "mywp_model_get_option_autoload_{$id}" , $this->option_autoload , $this->get_type() , $this->is_network() ); |
| 141 |
|
| 142 |
return $this->option_autoload; |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
public function get_option() { |
| 147 |
|
| 148 |
$option_key = $this->get_option_key(); |
| 149 |
|
| 150 |
if( empty( $option_key ) ) { |
| 151 |
|
| 152 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 153 |
|
| 154 |
MywpHelper::error_not_found_message( '$option_key' , $called_text ); |
| 155 |
|
| 156 |
return false; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
do_action( 'mywp_model_before_get_option' , $option_key , $this->get_type() , $this->is_network() ); |
| 161 |
|
| 162 |
$this->pre_get_option = apply_filters( "mywp_model_pre_get_option_{$option_key}" , false , $this->get_type() , $this->is_network() ); |
| 163 |
|
| 164 |
if( $this->pre_get_option !== false ) { |
| 165 |
|
| 166 |
return $this->pre_get_option; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
if( $this->is_network() ) { |
| 171 |
|
| 172 |
$this->option = get_site_option( $option_key ); |
| 173 |
|
| 174 |
} else { |
| 175 |
|
| 176 |
$this->option = get_option( $option_key ); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
$change_get_option = apply_filters( "mywp_model_change_get_option_{$option_key}" , $this->option , $this->get_type() , $this->is_network() ); |
| 181 |
|
| 182 |
do_action( 'mywp_model_after_get_option' , $this->option , $change_get_option , $option_key , $this->get_type() , $this->is_network() ); |
| 183 |
|
| 184 |
if( $change_get_option !== $this->option ) { |
| 185 |
|
| 186 |
$this->change_get_option = $change_get_option; |
| 187 |
|
| 188 |
return $this->change_get_option; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
return $this->option; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
public function update_data( $update_data ) { |
| 197 |
|
| 198 |
$option_key = $this->get_option_key(); |
| 199 |
|
| 200 |
if( empty( $option_key ) ) { |
| 201 |
|
| 202 |
$called_text = sprintf( '(object) %s->%s( %s )' , __CLASS__ , __FUNCTION__ , '$update_data' ); |
| 203 |
|
| 204 |
MywpHelper::error_not_found_message( '$option_key' , $called_text ); |
| 205 |
|
| 206 |
return false; |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
do_action( "mywp_model_before_update_data_{$option_key}" , $update_data , $this->get_type() , $this->is_network() ); |
| 211 |
|
| 212 |
if( $this->is_network() ) { |
| 213 |
|
| 214 |
$return = update_site_option( $option_key , $update_data ); |
| 215 |
|
| 216 |
} else { |
| 217 |
|
| 218 |
$option_autoload = $this->get_option_autoload(); |
| 219 |
|
| 220 |
$return = update_option( $option_key , $update_data , $option_autoload ); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
do_action( "mywp_model_after_update_data_{$option_key}" , $return , $update_data , $this->get_type() , $this->is_network() ); |
| 225 |
|
| 226 |
return $return; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
public function remove_data() { |
| 231 |
|
| 232 |
$option_key = $this->get_option_key(); |
| 233 |
|
| 234 |
if( empty( $option_key ) ) { |
| 235 |
|
| 236 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 237 |
|
| 238 |
MywpHelper::error_not_found_message( '$option_key' , $called_text ); |
| 239 |
|
| 240 |
return false; |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
do_action( "mywp_model_before_remove_data_{$option_key}" , $this->get_type() , $this->is_network() ); |
| 245 |
|
| 246 |
if( $this->is_network() ) { |
| 247 |
|
| 248 |
$return = delete_site_option( $option_key ); |
| 249 |
|
| 250 |
} else { |
| 251 |
|
| 252 |
$return = delete_option( $option_key ); |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
do_action( "mywp_model_after_remove_data_{$option_key}" , $return , $this->get_type() , $this->is_network() ); |
| 257 |
|
| 258 |
return $return; |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
public function get_setting_data() { |
| 263 |
|
| 264 |
$option_key = $this->get_option_key(); |
| 265 |
|
| 266 |
if( empty( $option_key ) ) { |
| 267 |
|
| 268 |
$called_text = sprintf( '(object) %s->%s()' , __CLASS__ , __FUNCTION__ ); |
| 269 |
|
| 270 |
MywpHelper::error_not_found_message( '$option_key' , $called_text ); |
| 271 |
|
| 272 |
return false; |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
do_action( 'mywp_model_before_get_setting_data' , $option_key , $this->get_type() , $this->is_network() ); |
| 277 |
|
| 278 |
$this->pre_setting_data = apply_filters( "mywp_model_pre_get_setting_data_{$option_key}" , false , $this->get_type() , $this->is_network() ); |
| 279 |
|
| 280 |
if( $this->pre_setting_data !== false ) { |
| 281 |
|
| 282 |
$this->pre_setting_data = wp_parse_args( $this->pre_setting_data , $this->get_initial_data() ); |
| 283 |
|
| 284 |
return $this->pre_setting_data; |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
$this->setting_data = $this->get_option(); |
| 289 |
|
| 290 |
if( $this->setting_data === false ) { |
| 291 |
|
| 292 |
$this->setting_data = $this->get_default_data(); |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
$this->setting_data = wp_parse_args( $this->setting_data , $this->get_initial_data() ); |
| 297 |
|
| 298 |
$change_setting_data = apply_filters( "mywp_model_change_get_setting_data_{$option_key}" , $this->setting_data , $this->get_type() , $this->is_network() ); |
| 299 |
|
| 300 |
do_action( 'mywp_model_after_get_setting_data' , $this->setting_data , $change_setting_data , $option_key , $this->get_type() , $this->is_network() ); |
| 301 |
|
| 302 |
if( $change_setting_data !== $this->setting_data ) { |
| 303 |
|
| 304 |
$this->change_setting_data = $change_setting_data; |
| 305 |
|
| 306 |
return $this->change_setting_data; |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
return $this->setting_data; |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
endif; |
| 317 |
|