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