← All changes
|
admin/views/sp-framework/classes/metabox.class.php
+68
-112
2.4.16
→
2.2.10
View file →
| @@ -1,55 +1,24 @@ | ||
| 1 | 1 | <?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 | 2 | die; |
| 3 | 3 | } // Cannot access directly. |
| 4 | - | |
| 4 | +/** | |
| 5 | + * | |
| 6 | + * Metabox Class | |
| 7 | + * | |
| 8 | + * @since 1.0.0 | |
| 9 | + * @version 1.0.0 | |
| 10 | + */ | |
| 5 | 11 | if ( ! class_exists( 'SP_PC_Metabox' ) ) { |
| 6 | - /** | |
| 7 | - * | |
| 8 | - * Metabox Class | |
| 9 | - * | |
| 10 | - * @since 1.0.0 | |
| 11 | - * @version 1.0.0 | |
| 12 | - */ | |
| 13 | 12 | class SP_PC_Metabox extends SP_PC_Abstract { |
| 14 | 13 | |
| 15 | - /** | |
| 16 | - * Property. | |
| 17 | - * | |
| 18 | - * @var string | |
| 19 | - */ | |
| 20 | - public $unique = ''; | |
| 21 | - /** | |
| 22 | - * Properties for metabox. | |
| 23 | - * | |
| 24 | - * @var string | |
| 25 | - */ | |
| 26 | - public $abstract = 'metabox'; | |
| 27 | - /** | |
| 28 | - * Pre fields property. | |
| 29 | - * | |
| 30 | - * @var array | |
| 31 | - */ | |
| 14 | + // constants. | |
| 15 | + public $unique = ''; | |
| 16 | + public $abstract = 'metabox'; | |
| 32 | 17 | public $pre_fields = array(); |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Sections. | |
| 36 | - * | |
| 37 | - * @var array | |
| 38 | - */ | |
| 39 | - public $sections = array(); | |
| 40 | - /** | |
| 41 | - * Post Types. | |
| 42 | - * | |
| 43 | - * @var array | |
| 44 | - */ | |
| 45 | - public $post_type = array(); | |
| 46 | - /** | |
| 47 | - * Arguments. | |
| 48 | - * | |
| 49 | - * @var array | |
| 50 | - */ | |
| 51 | - public $args = array( | |
| 18 | + public $sections = array(); | |
| 19 | + public $post_type = array(); | |
| 20 | + public $args = array( | |
| 52 | 21 | 'title' => '', |
| 53 | 22 | 'post_type' => 'post', |
| 54 | 23 | 'data_type' => 'serialize', |
| 55 | 24 | 'context' => 'advanced', |
| @@ -145,11 +114,9 @@ | ||
| 145 | 114 | $saved_post_format = ( ! empty( $saved_post_format ) ) ? $saved_post_format : 'default'; |
| 146 | 115 | |
| 147 | 116 | $classes[] = 'spf-post-formats'; |
| 148 | 117 | |
| 149 | - /** | |
| 150 | - * Sanitize post format for standard to default. | |
| 151 | - */ | |
| 118 | + // Sanitize post format for standard to default. | |
| 152 | 119 | if ( ( $key = array_search( 'standard', $this->post_formats ) ) !== false ) { |
| 153 | 120 | $this->post_formats[ $key ] = 'default'; |
| 154 | 121 | } |
| 155 | 122 | |
| @@ -201,14 +168,9 @@ | ||
| 201 | 168 | } |
| 202 | 169 | |
| 203 | 170 | } |
| 204 | 171 | |
| 205 | - /** | |
| 206 | - * Get default value. | |
| 207 | - * | |
| 208 | - * @param array $field The field value. | |
| 209 | - * @return mixed | |
| 210 | - */ | |
| 172 | + // get default value. | |
| 211 | 173 | public function get_default( $field ) { |
| 212 | 174 | |
| 213 | 175 | // $default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : ''; |
| 214 | 176 | $default = ( isset( $field['id'] ) && isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : null; |
| @@ -231,9 +193,9 @@ | ||
| 231 | 193 | $value = null; |
| 232 | 194 | |
| 233 | 195 | if ( is_object( $post ) && ! empty( $field['id'] ) ) { |
| 234 | 196 | |
| 235 | - if ( 'serialize' !== $this->args['data_type'] ) { | |
| 197 | + if ( $this->args['data_type'] !== 'serialize' ) { | |
| 236 | 198 | $meta = get_post_meta( $post->ID, $field['id'] ); |
| 237 | 199 | $value = ( isset( $meta[0] ) ) ? $meta[0] : null; |
| 238 | 200 | } else { |
| 239 | 201 | $meta = get_post_meta( $post->ID, $this->unique, true ); |
| @@ -240,9 +202,9 @@ | ||
| 240 | 202 | $value = ( isset( $meta[ $field['id'] ] ) ) ? $meta[ $field['id'] ] : null; |
| 241 | 203 | } |
| 242 | 204 | } |
| 243 | 205 | |
| 244 | - $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 206 | + $default = $this->get_default( $field ); | |
| 245 | 207 | $value = ( isset( $value ) ) ? $value : $default; |
| 246 | 208 | |
| 247 | 209 | return $value; |
| 248 | 210 | |
| @@ -250,10 +212,10 @@ | ||
| 250 | 212 | |
| 251 | 213 | /** |
| 252 | 214 | * Add metabox content |
| 253 | 215 | * |
| 254 | - * @param object $post The post. | |
| 255 | - * @param array $callback The callback function. | |
| 216 | + * @param array $post The post. | |
| 217 | + * @param array $callback The callback function. | |
| 256 | 218 | * @return void |
| 257 | 219 | */ |
| 258 | 220 | public function add_meta_box_content( $post, $callback ) { |
| 259 | 221 | |
| @@ -258,14 +220,14 @@ | ||
| 258 | 220 | public function add_meta_box_content( $post, $callback ) { |
| 259 | 221 | |
| 260 | 222 | global $post; |
| 261 | 223 | |
| 262 | - $has_nav = ( count( $this->sections ) > 1 && 'side' !== $this->args['context'] ) ? true : false; | |
| 224 | + $has_nav = ( count( $this->sections ) > 1 && $this->args['context'] !== 'side' ) ? true : false; | |
| 263 | 225 | $show_all = ( ! $has_nav ) ? ' spf-show-all' : ''; |
| 264 | 226 | $errors = ( is_object( $post ) ) ? get_post_meta( $post->ID, '_spf_errors', true ) : array(); |
| 265 | 227 | $errors = ( ! empty( $errors ) ) ? $errors : array(); |
| 266 | 228 | $theme = ( $this->args['theme'] ) ? ' spf-theme-' . $this->args['theme'] : ''; |
| 267 | - // $class = ( $this->args['class'] ) ? ' ' . $this->args['class'] : ''; | |
| 229 | +//$class = ( $this->args['class'] ) ? ' ' . $this->args['class'] : ''; | |
| 268 | 230 | |
| 269 | 231 | if ( is_object( $post ) && ! empty( $errors ) ) { |
| 270 | 232 | delete_post_meta( $post->ID, '_spf_errors' ); |
| 271 | 233 | } |
| @@ -271,15 +233,15 @@ | ||
| 271 | 233 | } |
| 272 | 234 | |
| 273 | 235 | wp_nonce_field( 'spf_pcp_metabox_nonce', 'spf_pcp_metabox_nonce' . $this->unique ); |
| 274 | 236 | |
| 275 | - echo '<div class="spf spf-metabox' . esc_attr( $theme ) . '">'; | |
| 237 | + echo '<div class="spf spf-metabox' . $theme . '">'; | |
| 276 | 238 | |
| 277 | - echo '<div class="spf-wrapper' . esc_attr( $show_all ) . '">'; | |
| 239 | + echo '<div class="spf-wrapper' . $show_all . '">'; | |
| 278 | 240 | |
| 279 | 241 | if ( $has_nav ) { |
| 280 | 242 | |
| 281 | - echo '<div class="spf-nav spf-nav-metabox" data-unique="' . esc_attr( $this->unique ) . '">'; | |
| 243 | + echo '<div class="spf-nav spf-nav-metabox" data-unique="' . $this->unique . '">'; | |
| 282 | 244 | |
| 283 | 245 | echo '<ul>'; |
| 284 | 246 | $tab_key = 1; |
| 285 | 247 | foreach ( $this->sections as $section ) { |
| @@ -284,11 +246,11 @@ | ||
| 284 | 246 | $tab_key = 1; |
| 285 | 247 | foreach ( $this->sections as $section ) { |
| 286 | 248 | |
| 287 | 249 | $tab_error = ( ! empty( $errors['sections'][ $tab_key ] ) ) ? '<i class="spf-label-error spf-error">!</i>' : ''; |
| 288 | - $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="spf-icon ' . esc_attr( $section['icon'] ) . '"></i>' : ''; | |
| 250 | + $tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="spf-icon ' . $section['icon'] . '"></i>' : ''; | |
| 289 | 251 | // Added li class to hide when needs, -ShapedPlugin. |
| 290 | - echo '<li class="menu-item_' . esc_attr( $this->unique ) . '_' . esc_attr( $tab_key ) . '"><a href="#" data-section="' . esc_attr( $this->unique ) . '_' . esc_attr( $tab_key ) . '">' . wp_kses_post( $tab_icon ) . esc_html( $section['title'] ) . esc_html( $tab_error ) . '</a></li>'; | |
| 252 | + echo '<li class="menu-item_' . $this->unique . '_' . $tab_key . '"><a href="#" data-section="' . $this->unique . '_' . $tab_key . '">' . $tab_icon . $section['title'] . $tab_error . '</a></li>'; | |
| 291 | 253 | |
| 292 | 254 | $tab_key++; |
| 293 | 255 | } |
| 294 | 256 | echo '</ul>'; |
| @@ -306,14 +268,14 @@ | ||
| 306 | 268 | foreach ( $this->sections as $section ) { |
| 307 | 269 | |
| 308 | 270 | $onload = ( ! $has_nav ) ? ' spf-onload' : ''; |
| 309 | 271 | |
| 310 | - echo '<div id="spf-section-' . esc_attr( $this->unique ) . '_' . esc_attr( $section_key ) . '" class="spf-section' . esc_attr( $onload ) . '">'; | |
| 272 | + echo '<div id="spf-section-' . $this->unique . '_' . $section_key . '" class="spf-section' . $onload . '">'; | |
| 311 | 273 | |
| 312 | 274 | $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="spf-icon ' . $section['icon'] . '"></i>' : ''; |
| 313 | 275 | $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; |
| 314 | 276 | |
| 315 | - echo ( $section_title || $section_icon ) ? '<div class="spf-section-title"><h3>' . esc_html( $section_icon ) . esc_html( $section_title ) . '</h3></div>' : ''; | |
| 277 | + echo ( $section_title || $section_icon ) ? '<div class="spf-section-title"><h3>' . $section_icon . $section_title . '</h3></div>' : ''; | |
| 316 | 278 | |
| 317 | 279 | if ( ! empty( $section['fields'] ) ) { |
| 318 | 280 | |
| 319 | 281 | foreach ( $section['fields'] as $field ) { |
| @@ -326,9 +288,9 @@ | ||
| 326 | 288 | |
| 327 | 289 | } |
| 328 | 290 | } else { |
| 329 | 291 | |
| 330 | - echo '<div class="spf-no-option spf-text-muted">' . esc_html__( 'No option provided by developer.', 'post-carousel' ) . '</div>'; | |
| 292 | + echo '<div class="spf-no-option spf-text-muted">' . esc_html__( 'No option provided by developer.', 'smart-post-show' ) . '</div>'; | |
| 331 | 293 | |
| 332 | 294 | } |
| 333 | 295 | |
| 334 | 296 | echo '</div>'; |
| @@ -336,9 +298,9 @@ | ||
| 336 | 298 | $section_key++; |
| 337 | 299 | } |
| 338 | 300 | |
| 339 | 301 | echo '</div>'; |
| 340 | - echo '<a class="btn btn-success" id="spsp-show-preview" data-id="' . esc_attr( $post->ID ) . '"href=""><i class="fa fa-eye"></i> Show Preview</a>'; | |
| 302 | + | |
| 341 | 303 | echo '<div class="clear"></div>'; |
| 342 | 304 | |
| 343 | 305 | if ( ! empty( $this->args['show_restore'] ) ) { |
| 344 | 306 | |
| @@ -343,11 +305,11 @@ | ||
| 343 | 305 | if ( ! empty( $this->args['show_restore'] ) ) { |
| 344 | 306 | |
| 345 | 307 | echo '<div class="spf-restore-wrapper">'; |
| 346 | 308 | echo '<label>'; |
| 347 | - echo '<input type="checkbox" name="' . esc_attr( $this->unique ) . '[_restore]" />'; | |
| 348 | - echo '<span class="button spf-button-restore">' . esc_html__( 'Restore', 'post-carousel' ) . '</span>'; | |
| 349 | - echo '<span class="button spf-button-cancel">' . sprintf( '<small>( %s )</small> %s', esc_html__( 'update post for restore ', 'post-carousel' ), esc_html__( 'Cancel', 'post-carousel' ) ) . '</span>'; | |
| 309 | + echo '<input type="checkbox" name="' . $this->unique . '[_restore]" />'; | |
| 310 | + echo '<span class="button spf-button-restore">' . esc_html__( 'Restore', 'smart-post-show' ) . '</span>'; | |
| 311 | + echo '<span class="button spf-button-cancel">' . sprintf( '<small>( %s )</small> %s', esc_html__( 'update post for restore ', 'smart-post-show' ), esc_html__( 'Cancel', 'smart-post-show' ) ) . '</span>'; | |
| 350 | 312 | echo '</label>'; |
| 351 | 313 | echo '</div>'; |
| 352 | 314 | |
| 353 | 315 | } |
| @@ -371,21 +333,19 @@ | ||
| 371 | 333 | * @return statement |
| 372 | 334 | */ |
| 373 | 335 | public function save_meta_box( $post_id ) { |
| 374 | 336 | |
| 375 | - $count = 1; | |
| 376 | - $data = array(); | |
| 377 | - $errors = array(); | |
| 378 | - $noncekey = 'spf_pcp_metabox_nonce' . $this->unique; | |
| 379 | - $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : ''; | |
| 380 | - if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'spf_pcp_metabox_nonce' ) ) { | |
| 337 | + if ( ! wp_verify_nonce( spf_get_var( 'spf_pcp_metabox_nonce' . $this->unique ), 'spf_pcp_metabox_nonce' ) ) { | |
| 381 | 338 | return $post_id; |
| 382 | 339 | } |
| 383 | 340 | |
| 384 | - // XSS ok. | |
| 385 | - // No worries, This "POST" requests is sanitizing in the below foreach. | |
| 386 | - $request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array(); // phpcs:ignore | |
| 341 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
| 342 | + return $post_id; | |
| 343 | + } | |
| 387 | 344 | |
| 345 | + $errors = array(); | |
| 346 | + $request = spf_get_var( $this->unique ); | |
| 347 | + | |
| 388 | 348 | if ( ! empty( $request ) ) { |
| 389 | 349 | |
| 390 | 350 | // ignore _nonce. |
| 391 | 351 | if ( isset( $request['_nonce'] ) ) { |
| @@ -400,42 +360,38 @@ | ||
| 400 | 360 | |
| 401 | 361 | foreach ( $section['fields'] as $field ) { |
| 402 | 362 | |
| 403 | 363 | if ( ! empty( $field['id'] ) ) { |
| 404 | - $field_id = $field['id']; | |
| 405 | - $field_value = isset( $request[ $field_id ] ) ? $request[ $field_id ] : ''; | |
| 406 | 364 | |
| 407 | - // Sanitize "post" request of field. | |
| 408 | - if ( ! isset( $field['sanitize'] ) ) { | |
| 365 | + // sanitize. | |
| 366 | + if ( ! empty( $field['sanitize'] ) ) { | |
| 409 | 367 | |
| 410 | - if ( is_array( $field_value ) ) { | |
| 411 | - $data[ $field_id ] = wp_kses_post_deep( $field_value ); | |
| 412 | - } else { | |
| 413 | - $data[ $field_id ] = wp_kses_post( $field_value ); | |
| 414 | - } | |
| 415 | - } elseif ( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { | |
| 368 | + $sanitize = $field['sanitize']; | |
| 369 | + $value_sanitize = isset( $request[ $field['id'] ] ) ? $request[ $field['id'] ] : ''; | |
| 370 | + $request[ $field['id'] ] = call_user_func( $sanitize, $value_sanitize ); | |
| 416 | 371 | |
| 417 | - $data[ $field_id ] = call_user_func( $field['sanitize'], $field_value ); | |
| 418 | - | |
| 419 | - } else { | |
| 420 | - | |
| 421 | - $data[ $field_id ] = $field_value; | |
| 422 | - | |
| 423 | 372 | } |
| 424 | 373 | |
| 425 | - // Validate "post" request of field. | |
| 426 | - if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { | |
| 374 | + // validate. | |
| 375 | + if ( ! empty( $field['validate'] ) ) { | |
| 427 | 376 | |
| 428 | - $has_validated = call_user_func( $field['validate'], $field_value ); | |
| 377 | + $validate = $field['validate']; | |
| 378 | + $value_validate = isset( $request[ $field['id'] ] ) ? $request[ $field['id'] ] : ''; | |
| 379 | + $has_validated = call_user_func( $validate, $value_validate ); | |
| 429 | 380 | |
| 430 | 381 | if ( ! empty( $has_validated ) ) { |
| 431 | 382 | |
| 432 | - $errors['sections'][ $count ] = true; | |
| 433 | - $errors['fields'][ $field_id ] = $has_validated; | |
| 434 | - $data[ $field_id ] = $this->get_meta_value( $field ); | |
| 383 | + $errors['sections'][ $section_key ] = true; | |
| 384 | + $errors['fields'][ $field['id'] ] = $has_validated; | |
| 385 | + $request[ $field['id'] ] = $this->get_meta_value( $field ); | |
| 435 | 386 | |
| 436 | 387 | } |
| 437 | 388 | } |
| 389 | + | |
| 390 | + // auto sanitize. | |
| 391 | + if ( ! isset( $request[ $field['id'] ] ) || is_null( $request[ $field['id'] ] ) ) { | |
| 392 | + $request[ $field['id'] ] = ''; | |
| 393 | + } | |
| 438 | 394 | } |
| 439 | 395 | } |
| 440 | 396 | } |
| 441 | 397 | |
| @@ -441,16 +397,16 @@ | ||
| 441 | 397 | |
| 442 | 398 | $section_key++; |
| 443 | 399 | } |
| 444 | 400 | } |
| 445 | - $data = apply_filters( "spf_{$this->unique}_save", $data, $post_id, $this ); | |
| 401 | + $request = apply_filters( "spf_{$this->unique}_save", $request, $post_id, $this ); | |
| 446 | 402 | |
| 447 | - do_action( "spf_{$this->unique}_save_before", $data, $post_id, $this ); | |
| 403 | + do_action( "spf_{$this->unique}_save_before", $request, $post_id, $this ); | |
| 448 | 404 | |
| 449 | - if ( empty( $data ) || ! empty( $request['_restore'] ) ) { | |
| 405 | + if ( empty( $request ) || ! empty( $request['_restore'] ) ) { | |
| 450 | 406 | |
| 451 | - if ( 'serialize' !== $this->args['data_type'] ) { | |
| 452 | - foreach ( $data as $key => $value ) { | |
| 407 | + if ( $this->args['data_type'] !== 'serialize' ) { | |
| 408 | + foreach ( $request as $key => $value ) { | |
| 453 | 409 | delete_post_meta( $post_id, $key ); |
| 454 | 410 | } |
| 455 | 411 | } else { |
| 456 | 412 | delete_post_meta( $post_id, $this->unique ); |
| @@ -456,14 +412,14 @@ | ||
| 456 | 412 | delete_post_meta( $post_id, $this->unique ); |
| 457 | 413 | } |
| 458 | 414 | } else { |
| 459 | 415 | |
| 460 | - if ( 'serialize' !== $this->args['data_type'] ) { | |
| 461 | - foreach ( $data as $key => $value ) { | |
| 416 | + if ( $this->args['data_type'] !== 'serialize' ) { | |
| 417 | + foreach ( $request as $key => $value ) { | |
| 462 | 418 | update_post_meta( $post_id, $key, $value ); |
| 463 | 419 | } |
| 464 | 420 | } else { |
| 465 | - update_post_meta( $post_id, $this->unique, $data ); | |
| 421 | + update_post_meta( $post_id, $this->unique, $request ); | |
| 466 | 422 | } |
| 467 | 423 | |
| 468 | 424 | if ( ! empty( $errors ) ) { |
| 469 | 425 | update_post_meta( $post_id, '_spf_errors', $errors ); |
| @@ -469,10 +425,10 @@ | ||
| 469 | 425 | update_post_meta( $post_id, '_spf_errors', $errors ); |
| 470 | 426 | } |
| 471 | 427 | } |
| 472 | 428 | |
| 473 | - do_action( "spf_{$this->unique}_saved", $data, $post_id, $this ); | |
| 429 | + do_action( "spf_{$this->unique}_saved", $request, $post_id, $this ); | |
| 474 | 430 | |
| 475 | - do_action( "spf_{$this->unique}_save_after", $data, $post_id, $this ); | |
| 431 | + do_action( "spf_{$this->unique}_save_after", $request, $post_id, $this ); | |
| 476 | 432 | } |
| 477 | 433 | } |
| 478 | 434 | } |