| 1 |
<?php |
| 2 |
|
| 3 |
class WPUPG_Grid_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 |
$grid = new WPUPG_Grid( $post_id ); |
| 19 |
|
| 20 |
// Basic metadata |
| 21 |
$fields = $grid->fields(); |
| 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 |
// Field specific adjustments |
| 29 |
if( isset( $new ) && $field == 'wpupg_post_types' ) { |
| 30 |
$new = array( $new ); |
| 31 |
} |
| 32 |
|
| 33 |
// Update or delete meta data if changed |
| 34 |
if( isset( $new ) && $new != $old ) { |
| 35 |
update_post_meta( $post_id, $field, $new ); |
| 36 |
} elseif ( $new == '' && $old ) { |
| 37 |
delete_post_meta( $post_id, $field, $old ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Limit rules |
| 42 |
|
| 43 |
$limit_rules = array(); |
| 44 |
|
| 45 |
if( isset( $_POST['wpupg_limit_posts_rule'] ) ) { |
| 46 |
foreach( $_POST['wpupg_limit_posts_rule'] as $id => $limit_rule ) { |
| 47 |
if($id != 0) { |
| 48 |
$field = $limit_rule['field']; |
| 49 |
$type = $limit_rule['type']; |
| 50 |
|
| 51 |
$field_parts = explode( '|', $field ); |
| 52 |
$values_name = 'values_' . $field_parts[0] . '_' . $field_parts[1]; |
| 53 |
|
| 54 |
if( isset( $limit_rule[$values_name] ) && $limit_rule[$values_name] ) { |
| 55 |
$values = $limit_rule[$values_name]; |
| 56 |
|
| 57 |
if( !is_array( $values ) ) $values = explode( ';', $values ); |
| 58 |
|
| 59 |
$limit_rules[] = array( |
| 60 |
'field' => $field, |
| 61 |
'post_type' => $field_parts[0], |
| 62 |
'taxonomy' => $field_parts[1], |
| 63 |
'values' => $values, |
| 64 |
'type' => $type, |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
update_post_meta( $post_id, 'wpupg_limit_rules', $limit_rules ); |
| 72 |
|
| 73 |
// Filter Taxonomies |
| 74 |
$post_type = $_POST['wpupg_post_types']; |
| 75 |
|
| 76 |
if( isset( $_POST['wpupg_filter_taxonomy_' . $post_type] ) ) { |
| 77 |
$filter_taxonomies = $_POST['wpupg_filter_taxonomy_' . $post_type]; |
| 78 |
update_post_meta( $post_id, 'wpupg_filter_taxonomies', $filter_taxonomies ); |
| 79 |
} |
| 80 |
|
| 81 |
// Filter Limit Terms |
| 82 |
$filter_limit_terms = []; |
| 83 |
if( isset( $filter_taxonomies ) ) { |
| 84 |
foreach( $filter_taxonomies as $filter_taxonomy ) { |
| 85 |
if( isset( $_POST['wpupg_filter_limit_terms_' . $filter_taxonomy] ) ) { |
| 86 |
$filter_limit_terms[$filter_taxonomy] = $_POST['wpupg_filter_limit_terms_' . $filter_taxonomy]; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
update_post_meta( $post_id, 'wpupg_filter_limit_terms', $filter_limit_terms ); |
| 91 |
|
| 92 |
// Filter style metadata |
| 93 |
$styles = $grid->filter_style_fields(); |
| 94 |
$filter_style = array(); |
| 95 |
|
| 96 |
foreach( $styles as $style => $fields ) { |
| 97 |
$filter_style[$style] = array(); |
| 98 |
|
| 99 |
foreach( $fields as $field => $default ) { |
| 100 |
$field_name = 'wpupg_' . $style . '_filter_style_' . $field; |
| 101 |
if( isset( $_POST[$field_name] ) ) { |
| 102 |
$filter_style[$style][$field] = $_POST[$field_name]; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
update_post_meta( $post_id, 'wpupg_filter_style', $filter_style ); |
| 108 |
|
| 109 |
// Pagination metadata |
| 110 |
$pagination_fields = $grid->pagination_fields(); |
| 111 |
$pagination = array(); |
| 112 |
|
| 113 |
foreach( $pagination_fields as $type => $fields ) { |
| 114 |
$pagination[$type] = array(); |
| 115 |
|
| 116 |
foreach( $fields as $field => $default ) { |
| 117 |
$field_name = 'wpupg_pagination_' . $type . '_' . $field; |
| 118 |
if( isset( $_POST[$field_name] ) ) { |
| 119 |
$pagination[$type][$field] = $_POST[$field_name]; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
update_post_meta( $post_id, 'wpupg_pagination', $pagination ); |
| 125 |
|
| 126 |
// Pagination style metadata |
| 127 |
$pagination_style_fields = $grid->pagination_style_fields(); |
| 128 |
$pagination_style = array(); |
| 129 |
|
| 130 |
foreach( $pagination_style_fields as $field => $default ) { |
| 131 |
$field_name = 'wpupg_pagination_style_' . $field; |
| 132 |
if( isset( $_POST[$field_name] ) ) { |
| 133 |
$pagination_style[$field] = $_POST[$field_name]; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
update_post_meta( $post_id, 'wpupg_pagination_style', $pagination_style ); |
| 138 |
|
| 139 |
// Cache gets automatically generated in WPUPG_Grid_Cache |
| 140 |
} |
| 141 |
} |
| 142 |
} |