| 1 |
<?php |
| 2 |
|
| 3 |
class WPUPG_Post_Save { |
| 4 |
|
| 5 |
public function __construct() |
| 6 |
{ |
| 7 |
add_action( 'save_post', array( $this, 'save' ), 10, 2 ); |
| 8 |
} |
| 9 |
|
| 10 |
public function save( $post_id, $post ) |
| 11 |
{ |
| 12 |
if( $post->post_type != WPUPG_POST_TYPE ) |
| 13 |
{ |
| 14 |
if ( !isset( $_POST['wpupg_nonce'] ) || !wp_verify_nonce( $_POST['wpupg_nonce'], 'grid' ) ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
// Basic metadata |
| 19 |
$fields = array( |
| 20 |
'wpupg_custom_link' |
| 21 |
); |
| 22 |
|
| 23 |
foreach ( $fields as $field ) |
| 24 |
{ |
| 25 |
$old = get_post_meta( $post_id, $field, true ); |
| 26 |
$new = isset( $_POST[$field] ) ? $_POST[$field] : null; |
| 27 |
|
| 28 |
// Update or delete meta data if changed |
| 29 |
if( isset( $new ) && $new != $old ) { |
| 30 |
update_post_meta( $post_id, $field, $new ); |
| 31 |
} elseif ( $new == '' && $old ) { |
| 32 |
delete_post_meta( $post_id, $field, $old ); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
} |