| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! class_exists( 'MywpSetting' ) ) : |
| 8 |
|
| 9 |
final class MywpSetting { |
| 10 |
|
| 11 |
private static $instance; |
| 12 |
|
| 13 |
private function __construct() {} |
| 14 |
|
| 15 |
public static function get_instance() { |
| 16 |
|
| 17 |
if ( !isset( self::$instance ) ) { |
| 18 |
|
| 19 |
self::$instance = new self(); |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
return self::$instance; |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
private function __clone() {} |
| 28 |
|
| 29 |
private function __wakeup() {} |
| 30 |
|
| 31 |
public static function setting_view() { |
| 32 |
|
| 33 |
MywpApi::include_file( MYWP_PLUGIN_PATH . 'views/setting-screen.php' ); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public static function get_model( $setting_screen_id = false ) { |
| 38 |
|
| 39 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , $setting_screen_id ); |
| 40 |
|
| 41 |
if( empty( $setting_screen_id ) ) { |
| 42 |
|
| 43 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 44 |
|
| 45 |
return false; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
$setting_screen = MywpSettingScreen::get_setting_screen( $setting_screen_id ); |
| 50 |
|
| 51 |
if( empty( $setting_screen['controller'] ) ) { |
| 52 |
|
| 53 |
return false; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
$controller = MywpController::get_controller( $setting_screen['controller'] ); |
| 58 |
|
| 59 |
if( empty( $controller['id'] ) ) { |
| 60 |
|
| 61 |
MywpHelper::error_require_message( '$controller["id"]' , $called_text ); |
| 62 |
|
| 63 |
return false; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
$controller_id = $controller['id']; |
| 68 |
|
| 69 |
do_action( 'mywp_setting_before_get_model' , $setting_screen_id , $controller_id ); |
| 70 |
|
| 71 |
$pre_model = apply_filters( "mywp_setting_pre_get_model_{$controller_id}" , false , $setting_screen_id , $controller ); |
| 72 |
$pre_model = apply_filters( 'mywp_setting_pre_get_model' , $pre_model , $setting_screen_id , $controller_id , $controller ); |
| 73 |
|
| 74 |
if( $pre_model !== false ) { |
| 75 |
|
| 76 |
return $pre_model; |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
$mywp_model = new MywpModel( $controller_id , 'setting' , $controller['network'] ); |
| 81 |
|
| 82 |
$mywp_model->set_initial_data( $controller['initial_data'] ); |
| 83 |
$mywp_model->set_default_data( $controller['default_data'] ); |
| 84 |
|
| 85 |
do_action( 'mywp_setting_after_get_model' , $setting_screen_id , $controller_id ); |
| 86 |
|
| 87 |
return $mywp_model; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
public static function get_setting_data( $setting_screen_id = false ) { |
| 92 |
|
| 93 |
$model = self::get_model( $setting_screen_id ); |
| 94 |
|
| 95 |
if( empty( $model ) ) { |
| 96 |
|
| 97 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , $setting_screen_id ); |
| 98 |
|
| 99 |
MywpHelper::error_require_message( '$model' , $called_text ); |
| 100 |
|
| 101 |
return false; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
$setting_data = apply_filters( 'mywp_setting_get_setting_data' , $model->get_setting_data() , $setting_screen_id , $model ); |
| 106 |
|
| 107 |
return $setting_data; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
public static function get_posts( $args = array() , $setting_screen_id = false ) { |
| 112 |
|
| 113 |
do_action( 'mywp_setting_before_get_posts' , $args , $setting_screen_id ); |
| 114 |
|
| 115 |
$args = apply_filters( "mywp_setting_pre_get_posts_args_{$setting_screen_id}" , $args ); |
| 116 |
$args = apply_filters( 'mywp_setting_pre_get_posts_args' , $args , $setting_screen_id ); |
| 117 |
|
| 118 |
$posts = apply_filters( "mywp_setting_pre_get_posts_{$setting_screen_id}" , false , $args , $setting_screen_id ); |
| 119 |
$posts = apply_filters( 'mywp_setting_pre_get_posts' , $posts , $args , $setting_screen_id ); |
| 120 |
|
| 121 |
if( $posts !== false ) { |
| 122 |
|
| 123 |
return $posts; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
$posts = MywpPostType::get_posts( $args ); |
| 128 |
|
| 129 |
$posts = apply_filters( "mywp_setting_change_get_posts_{$setting_screen_id}" , $posts , $args ); |
| 130 |
$posts = apply_filters( 'mywp_setting_change_get_posts' , $posts , $args , $setting_screen_id ); |
| 131 |
|
| 132 |
do_action( 'mywp_setting_after_get_posts' , $posts , $args , $setting_screen_id ); |
| 133 |
|
| 134 |
return $posts; |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
public static function print_form_item_must( $setting_screen_id = false , $action = false ) { |
| 139 |
|
| 140 |
$called_text = sprintf( '%s::%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , ' $action' ); |
| 141 |
|
| 142 |
if( empty( $setting_screen_id ) ) { |
| 143 |
|
| 144 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 145 |
|
| 146 |
return false; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 151 |
|
| 152 |
if( empty( $action ) ) { |
| 153 |
|
| 154 |
MywpHelper::error_require_message( '$action' , $called_text ); |
| 155 |
|
| 156 |
return false; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
$action = strip_tags( $action ); |
| 161 |
|
| 162 |
printf( '<input type="hidden" name="mywp[setting_screen]" value="%s" />' , esc_attr( $setting_screen_id ) ); |
| 163 |
printf( '<input type="hidden" name="mywp[action]" value="%s" />' , esc_attr( $action ) ); |
| 164 |
|
| 165 |
$nonce_key = self::get_nonce_key( $setting_screen_id , $action ); |
| 166 |
|
| 167 |
wp_nonce_field( $nonce_key , $nonce_key ); |
| 168 |
|
| 169 |
do_action( "mywp_setting_print_form_item_must_{$setting_screen_id}_{$action}" ); |
| 170 |
do_action( 'mywp_setting_print_form_item_must' , $setting_screen_id , $action ); |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
public static function get_nonce_key( $setting_screen_id = false , $action = false ) { |
| 175 |
|
| 176 |
$called_text = sprintf( '%s::%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , ' $action' ); |
| 177 |
|
| 178 |
if( empty( $setting_screen_id ) ) { |
| 179 |
|
| 180 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 181 |
|
| 182 |
return false; |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 187 |
|
| 188 |
if( empty( $action ) ) { |
| 189 |
|
| 190 |
MywpHelper::error_require_message( '$action' , $called_text ); |
| 191 |
|
| 192 |
return false; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
$action = strip_tags( $action ); |
| 197 |
|
| 198 |
return sprintf( 'mywp_setting_nonce_%s_%s' , $setting_screen_id , $action ); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
public static function get_ajax_action_name( $setting_screen_id = false , $action = false ) { |
| 203 |
|
| 204 |
$called_text = sprintf( '%s::%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , ' $action' ); |
| 205 |
|
| 206 |
if( empty( $setting_screen_id ) ) { |
| 207 |
|
| 208 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 209 |
|
| 210 |
return false; |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 215 |
|
| 216 |
if( empty( $action ) ) { |
| 217 |
|
| 218 |
MywpHelper::error_require_message( '$action' , $called_text ); |
| 219 |
|
| 220 |
return false; |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
$action = strip_tags( $action ); |
| 225 |
|
| 226 |
return sprintf( 'mywp_setting_%s_%s' , $setting_screen_id , $action ); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
public static function is_mywp_form_action( $post_data = array() ) { |
| 231 |
|
| 232 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , '$post_data' ); |
| 233 |
|
| 234 |
if( empty( $post_data ) ) { |
| 235 |
|
| 236 |
MywpHelper::error_require_message( '$post_data' , $called_text ); |
| 237 |
|
| 238 |
return false; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
if( empty( $post_data['mywp'] ) ) { |
| 243 |
|
| 244 |
return false; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
if( empty( $post_data['mywp']['setting_screen'] ) ) { |
| 249 |
|
| 250 |
MywpHelper::error_require_message( 'mywp[setting_screen]' , $called_text ); |
| 251 |
|
| 252 |
return false; |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
if( empty( $post_data['mywp']['action'] ) ) { |
| 257 |
|
| 258 |
MywpHelper::error_require_message( 'mywp[action]' , $called_text ); |
| 259 |
|
| 260 |
return false; |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
$found_nonce = false; |
| 265 |
|
| 266 |
foreach( $post_data as $key => $val ) { |
| 267 |
|
| 268 |
if( strpos( $key , 'mywp_setting_nonce_' ) !== false ) { |
| 269 |
|
| 270 |
$found_nonce = true; |
| 271 |
break; |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
if( $found_nonce === false ) { |
| 278 |
|
| 279 |
MywpHelper::error_require_message( '$found_nonce' , $called_text ); |
| 280 |
|
| 281 |
return false; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
return $found_nonce; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
public static function post_data_format( $setting_screen_id = false , $action = false , $form_data = false ) { |
| 290 |
|
| 291 |
$mywp_notice = new MywpNotice(); |
| 292 |
|
| 293 |
$called_text = sprintf( '%s::%s( %s , %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , '$action' , '$form_data' ); |
| 294 |
|
| 295 |
if( empty( $setting_screen_id ) ) { |
| 296 |
|
| 297 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$setting_screen_id' ) ); |
| 298 |
|
| 299 |
return $form_data; |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 304 |
|
| 305 |
if( empty( $action ) ) { |
| 306 |
|
| 307 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$action' ) ); |
| 308 |
|
| 309 |
return $form_data; |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
if( empty( $form_data['data'] ) or ! is_array( $form_data['data'] ) ) { |
| 314 |
|
| 315 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$form_data["data"]' ) ); |
| 316 |
|
| 317 |
return $form_data; |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
$action = strip_tags( $action ); |
| 322 |
|
| 323 |
if( empty( $form_data['data']['advance'] ) ) { |
| 324 |
|
| 325 |
$form_data['data']['advance'] = false; |
| 326 |
|
| 327 |
} else { |
| 328 |
|
| 329 |
$form_data['data']['advance'] = true; |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
$formatted_data = $form_data['data']; |
| 334 |
|
| 335 |
$formatted_data = apply_filters( "mywp_setting_post_data_format_{$setting_screen_id}_{$action}" , $formatted_data , $form_data['data'] ); |
| 336 |
$formatted_data = apply_filters( 'mywp_setting_post_data_format' , $formatted_data , $form_data['data'] , $setting_screen_id , $action ); |
| 337 |
|
| 338 |
return $formatted_data; |
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
public static function post_data_validate( $setting_screen_id = false , $action = false , $formatted_data = false ) { |
| 343 |
|
| 344 |
$mywp_notice = new MywpNotice(); |
| 345 |
|
| 346 |
$called_text = sprintf( '%s::%s( %s , %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , '$action' , '$formatted_data' ); |
| 347 |
|
| 348 |
if( empty( $setting_screen_id ) ) { |
| 349 |
|
| 350 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$setting_screen_id' ) ); |
| 351 |
|
| 352 |
return $formatted_data; |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 357 |
|
| 358 |
if( empty( $action ) ) { |
| 359 |
|
| 360 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$action' ) ); |
| 361 |
|
| 362 |
return $formatted_data; |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
$action = strip_tags( $action ); |
| 367 |
|
| 368 |
if( empty( $formatted_data ) ) { |
| 369 |
|
| 370 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$formatted_data' ) ); |
| 371 |
|
| 372 |
return $formatted_data; |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
if( ! isset( $formatted_data['advance'] ) ) { |
| 377 |
|
| 378 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$formatted_data["advance"]' ) ); |
| 379 |
|
| 380 |
return $formatted_data; |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
$validated_data = $formatted_data; |
| 385 |
|
| 386 |
$validated_data = apply_filters( "mywp_setting_post_data_validate_{$setting_screen_id}_{$action}" , $validated_data , $formatted_data ); |
| 387 |
$validated_data = apply_filters( 'mywp_setting_post_data_validate' , $validated_data , $formatted_data , $setting_screen_id , $action ); |
| 388 |
|
| 389 |
return $formatted_data; |
| 390 |
|
| 391 |
} |
| 392 |
|
| 393 |
public static function post_data_action( $setting_screen_id = false , $action = false , $validated_data = false ) { |
| 394 |
|
| 395 |
$called_text = sprintf( '%s::%s( %s , %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , '$action' , '$validated_data' ); |
| 396 |
|
| 397 |
$mywp_notice = new MywpNotice(); |
| 398 |
|
| 399 |
if( empty( $setting_screen_id ) ) { |
| 400 |
|
| 401 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$setting_screen_id' ) ); |
| 402 |
|
| 403 |
return false; |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 408 |
|
| 409 |
if( empty( $action ) ) { |
| 410 |
|
| 411 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$action' ) ); |
| 412 |
|
| 413 |
return false; |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
$action = strip_tags( $action ); |
| 418 |
|
| 419 |
if( empty( $validated_data ) ) { |
| 420 |
|
| 421 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'my-wp' ) , $called_text , '$validated_data' ) ); |
| 422 |
|
| 423 |
return false; |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
do_action( "mywp_setting_before_post_data_action_{$setting_screen_id}_{$action}" , $validated_data ); |
| 428 |
do_action( 'mywp_setting_before_post_data_action' , $validated_data , $setting_screen_id , $action ); |
| 429 |
|
| 430 |
$mywp_model = self::get_model( $setting_screen_id ); |
| 431 |
|
| 432 |
if( ! empty( $mywp_model ) ) { |
| 433 |
|
| 434 |
if( $action === 'update' ) { |
| 435 |
|
| 436 |
$mywp_model->update_data( $validated_data ); |
| 437 |
|
| 438 |
$mywp_notice->add_notice( __( 'Settings saved.' ) ); |
| 439 |
|
| 440 |
} elseif( $action === 'remove' ) { |
| 441 |
|
| 442 |
$mywp_model->remove_data(); |
| 443 |
|
| 444 |
$mywp_notice->add_notice( __( 'Settings saved.' ) ); |
| 445 |
|
| 446 |
} else { |
| 447 |
|
| 448 |
do_action( "mywp_setting_post_data_action_custom_{$setting_screen_id}_$action" , $validated_data ); |
| 449 |
do_action( 'mywp_setting_post_data_action_custom' , $validated_data , $setting_screen_id , $action ); |
| 450 |
|
| 451 |
} |
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
do_action( "mywp_setting_after_post_data_action_{$setting_screen_id}_{$action}" , $validated_data ); |
| 456 |
do_action( 'mywp_setting_after_post_data_action' , $validated_data , $setting_screen_id , $action ); |
| 457 |
|
| 458 |
$is_redirect = true; |
| 459 |
|
| 460 |
$is_redirect = apply_filters( "mywp_setting_post_data_action_redirect_{$setting_screen_id}_{$action}" , $is_redirect ); |
| 461 |
$is_redirect = apply_filters( 'mywp_setting_post_data_action_redirect' , $is_redirect , $setting_screen_id , $action ); |
| 462 |
|
| 463 |
return $is_redirect; |
| 464 |
|
| 465 |
} |
| 466 |
|
| 467 |
} |
| 468 |
|
| 469 |
endif; |
| 470 |
|