| 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 |
if( empty( $setting_screen_id ) ) { |
| 40 |
|
| 41 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , $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['model'] ) ) { |
| 60 |
|
| 61 |
return false; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
return $controller['model']; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
public static function print_form_item_must( $setting_screen_id = false , $action = false ) { |
| 70 |
|
| 71 |
$called_text = sprintf( '%s::%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , ' $action' ); |
| 72 |
|
| 73 |
if( empty( $setting_screen_id ) ) { |
| 74 |
|
| 75 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 76 |
|
| 77 |
return false; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 82 |
|
| 83 |
if( empty( $action ) ) { |
| 84 |
|
| 85 |
MywpHelper::error_require_message( '$action' , $called_text ); |
| 86 |
|
| 87 |
return false; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
$action = strip_tags( $action ); |
| 92 |
|
| 93 |
printf( '<input type="hidden" name="mywp[setting_screen]" value="%s" />' , esc_attr( $setting_screen_id ) ); |
| 94 |
printf( '<input type="hidden" name="mywp[action]" value="%s" />' , esc_attr( $action ) ); |
| 95 |
|
| 96 |
$nonce_key = self::get_nonce_key( $setting_screen_id , $action ); |
| 97 |
|
| 98 |
wp_nonce_field( $nonce_key , $nonce_key ); |
| 99 |
|
| 100 |
do_action( "mywp_print_form_item_must_{$setting_screen_id}_{$action}" ); |
| 101 |
do_action( 'mywp_print_form_item_must' , $setting_screen_id , $action ); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
public static function get_nonce_key( $setting_screen_id = false , $action = false ) { |
| 106 |
|
| 107 |
$called_text = sprintf( '%s::%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , ' $action' ); |
| 108 |
|
| 109 |
if( empty( $setting_screen_id ) ) { |
| 110 |
|
| 111 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 112 |
|
| 113 |
return false; |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 118 |
|
| 119 |
if( empty( $action ) ) { |
| 120 |
|
| 121 |
MywpHelper::error_require_message( '$action' , $called_text ); |
| 122 |
|
| 123 |
return false; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
$action = strip_tags( $action ); |
| 128 |
|
| 129 |
return sprintf( 'mywp_nonce_%s_%s' , $setting_screen_id , $action ); |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
public static function get_ajax_action_name( $setting_screen_id = false , $action = false ) { |
| 134 |
|
| 135 |
$called_text = sprintf( '%s::%s( %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , ' $action' ); |
| 136 |
|
| 137 |
if( empty( $setting_screen_id ) ) { |
| 138 |
|
| 139 |
MywpHelper::error_require_message( '$setting_screen_id' , $called_text ); |
| 140 |
|
| 141 |
return false; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 146 |
|
| 147 |
if( empty( $action ) ) { |
| 148 |
|
| 149 |
MywpHelper::error_require_message( '$action' , $called_text ); |
| 150 |
|
| 151 |
return false; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
$action = strip_tags( $action ); |
| 156 |
|
| 157 |
return sprintf( 'mywp_setting_%s_%s' , $setting_screen_id , $action ); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
public static function is_mywp_form_action( $post_data = array() ) { |
| 162 |
|
| 163 |
$called_text = sprintf( '%s::%s( %s )' , __CLASS__ , __FUNCTION__ , '$post_data' ); |
| 164 |
|
| 165 |
if( empty( $post_data ) ) { |
| 166 |
|
| 167 |
MywpHelper::error_require_message( '$post_data' , $called_text ); |
| 168 |
|
| 169 |
return false; |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
if( empty( $post_data['mywp'] ) ) { |
| 174 |
|
| 175 |
return false; |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
if( empty( $post_data['mywp']['setting_screen'] ) ) { |
| 180 |
|
| 181 |
MywpHelper::error_require_message( 'mywp[setting_screen]' , $called_text ); |
| 182 |
|
| 183 |
return false; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
if( empty( $post_data['mywp']['action'] ) ) { |
| 188 |
|
| 189 |
MywpHelper::error_require_message( 'mywp[action]' , $called_text ); |
| 190 |
|
| 191 |
return false; |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
$found_nonce = false; |
| 196 |
|
| 197 |
foreach( $post_data as $key => $val ) { |
| 198 |
|
| 199 |
if( strpos( $key , 'mywp_nonce_' ) !== false ) { |
| 200 |
|
| 201 |
$found_nonce = true; |
| 202 |
break; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
if( empty( $found_nonce ) ) { |
| 209 |
|
| 210 |
MywpHelper::error_require_message( '$found_nonce' , $called_text ); |
| 211 |
|
| 212 |
return false; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
return $found_nonce; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
public static function post_data_format( $setting_screen_id = false , $action = false , $form_data = false ) { |
| 221 |
|
| 222 |
$mywp_notice = new MywpNotice(); |
| 223 |
|
| 224 |
$called_text = sprintf( '%s::%s( %s , %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , '$action' , '$form_data' ); |
| 225 |
|
| 226 |
if( empty( $setting_screen_id ) ) { |
| 227 |
|
| 228 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$setting_screen_id' ) ); |
| 229 |
return $form_data; |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 234 |
|
| 235 |
if( empty( $action ) ) { |
| 236 |
|
| 237 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$action' ) ); |
| 238 |
return $form_data; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
if( empty( $form_data['data'] ) or ! is_array( $form_data['data'] ) ) { |
| 243 |
|
| 244 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$form_data["data"]' ) ); |
| 245 |
return $form_data; |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
$action = strip_tags( $action ); |
| 250 |
|
| 251 |
if( empty( $form_data['data']['advance'] ) ) { |
| 252 |
|
| 253 |
$form_data['data']['advance'] = false; |
| 254 |
|
| 255 |
} else { |
| 256 |
|
| 257 |
$form_data['data']['advance'] = true; |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
$formatted_data = $form_data['data']; |
| 262 |
|
| 263 |
$formatted_data = apply_filters( "mywp_setting_post_data_format_{$setting_screen_id}_{$action}" , $formatted_data , $form_data['data'] ); |
| 264 |
$formatted_data = apply_filters( 'mywp_setting_post_data_format' , $formatted_data , $form_data['data'] , $setting_screen_id , $action ); |
| 265 |
|
| 266 |
return $formatted_data; |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
public static function post_data_validate( $setting_screen_id = false , $action = false , $formatted_data = false ) { |
| 271 |
|
| 272 |
$mywp_notice = new MywpNotice(); |
| 273 |
|
| 274 |
$called_text = sprintf( '%s::%s( %s , %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , '$action' , '$formatted_data' ); |
| 275 |
|
| 276 |
if( empty( $setting_screen_id ) ) { |
| 277 |
|
| 278 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$setting_screen_id' ) ); |
| 279 |
return $formatted_data; |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 284 |
|
| 285 |
if( empty( $action ) ) { |
| 286 |
|
| 287 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$action' ) ); |
| 288 |
return $formatted_data; |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
$action = strip_tags( $action ); |
| 293 |
|
| 294 |
if( empty( $formatted_data ) ) { |
| 295 |
|
| 296 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$formatted_data' ) ); |
| 297 |
return $formatted_data; |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
if( ! isset( $formatted_data['advance'] ) ) { |
| 302 |
|
| 303 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$formatted_data["advance"]' ) ); |
| 304 |
return $formatted_data; |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
$validated_data = $formatted_data; |
| 309 |
|
| 310 |
$validated_data = apply_filters( "mywp_setting_post_data_validate_{$setting_screen_id}_{$action}" , $validated_data , $formatted_data ); |
| 311 |
$validated_data = apply_filters( 'mywp_setting_post_data_validate' , $validated_data , $formatted_data , $setting_screen_id , $action ); |
| 312 |
|
| 313 |
return $formatted_data; |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
public static function post_data_action( $setting_screen_id = false , $action = false , $validated_data = false ) { |
| 318 |
|
| 319 |
$called_text = sprintf( '%s::%s( %s , %s , %s )' , __CLASS__ , __FUNCTION__ , '$setting_screen_id' , '$action' , '$validated_data' ); |
| 320 |
|
| 321 |
$mywp_notice = new MywpNotice(); |
| 322 |
|
| 323 |
if( empty( $setting_screen_id ) ) { |
| 324 |
|
| 325 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$setting_screen_id' ) ); |
| 326 |
return false; |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
$setting_screen_id = strip_tags( $setting_screen_id ); |
| 331 |
|
| 332 |
if( empty( $action ) ) { |
| 333 |
|
| 334 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$action' ) ); |
| 335 |
return false; |
| 336 |
|
| 337 |
} |
| 338 |
|
| 339 |
$action = strip_tags( $action ); |
| 340 |
|
| 341 |
if( empty( $validated_data ) ) { |
| 342 |
|
| 343 |
$mywp_notice->add_notice_error( sprintf( __( 'The %1$s is required for %2$s.' , 'mywp' ) , $called_text , '$validated_data' ) ); |
| 344 |
return false; |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
do_action( "mywp_setting_before_post_data_action_{$setting_screen_id}_{$action}" , $validated_data ); |
| 349 |
do_action( 'mywp_setting_before_post_data_action' , $validated_data , $setting_screen_id , $action ); |
| 350 |
|
| 351 |
$mywp_model = self::get_model( $setting_screen_id ); |
| 352 |
|
| 353 |
if( ! empty( $mywp_model ) ) { |
| 354 |
|
| 355 |
if( $action == 'update' ) { |
| 356 |
|
| 357 |
$mywp_model->update_data( $validated_data ); |
| 358 |
|
| 359 |
$mywp_notice->add_notice( __( 'Settings saved.' ) ); |
| 360 |
|
| 361 |
} elseif( $action == 'remove' ) { |
| 362 |
|
| 363 |
$mywp_model->remove_data(); |
| 364 |
|
| 365 |
$mywp_notice->add_notice( __( 'Settings saved.' ) ); |
| 366 |
|
| 367 |
} else { |
| 368 |
|
| 369 |
do_action( "mywp_setting_post_data_action_custom_{$setting_screen_id}_$action" , $validated_data ); |
| 370 |
do_action( 'mywp_setting_post_data_action_custom' , $validated_data , $setting_screen_id , $action ); |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
} |
| 375 |
|
| 376 |
do_action( "mywp_setting_post_data_action_{$setting_screen_id}_{$action}" , $validated_data ); |
| 377 |
do_action( 'mywp_setting_post_data_action' , $validated_data , $setting_screen_id , $action ); |
| 378 |
|
| 379 |
$is_redirect = true; |
| 380 |
|
| 381 |
$is_redirect = apply_filters( "mywp_setting_post_data_action_redirect_{$setting_screen_id}_{$action}" , $is_redirect ); |
| 382 |
$is_redirect = apply_filters( 'mywp_setting_post_data_action_redirect' , $is_redirect , $setting_screen_id , $action ); |
| 383 |
|
| 384 |
return $is_redirect; |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
} |
| 389 |
|
| 390 |
endif; |
| 391 |
|