| 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 |
add_action( 'edit_attachment', array( $this, 'save_attachment' ) ); |
| 9 |
} |
| 10 |
|
| 11 |
public function save_attachment( $post_id ) |
| 12 |
{ |
| 13 |
$post = get_post( $post_id ); |
| 14 |
$this->save( $post_id, $post ); |
| 15 |
} |
| 16 |
|
| 17 |
public function save( $post_id, $post ) |
| 18 |
{ |
| 19 |
if( $post->post_type != WPUPG_POST_TYPE ) |
| 20 |
{ |
| 21 |
if ( !isset( $_POST['wpupg_nonce'] ) || !wp_verify_nonce( $_POST['wpupg_nonce'], 'grid' ) ) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
// Basic metadata |
| 26 |
$fields = array( |
| 27 |
'wpupg_custom_link', |
| 28 |
'wpupg_custom_link_behaviour', |
| 29 |
'wpupg_custom_image', |
| 30 |
'wpupg_custom_image_id', |
| 31 |
); |
| 32 |
|
| 33 |
foreach ( $fields as $field ) |
| 34 |
{ |
| 35 |
$old = get_post_meta( $post_id, $field, true ); |
| 36 |
$new = isset( $_POST[$field] ) ? $_POST[$field] : null; |
| 37 |
|
| 38 |
// Update or delete meta data if changed |
| 39 |
if( isset( $new ) && $new != $old ) { |
| 40 |
update_post_meta( $post_id, $field, $new ); |
| 41 |
} elseif ( $new == '' && $old ) { |
| 42 |
delete_post_meta( $post_id, $field, $old ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |