1: <?php
2:
3: 4: 5:
6: final class Papi_Admin_Post_Handler extends Papi_Core_Data_Handler {
7:
8: 9: 10:
11: public function __construct() {
12: $this->setup_actions();
13: }
14:
15: 16: 17: 18: 19:
20: private function overwrite_post_data( $post_id ) {
21: global $wpdb;
22:
23: if ( empty( $post_id ) || empty( $this->overwrite ) ) {
24: return;
25: }
26:
27: $wpdb->update( $wpdb->posts, $this->overwrite, ['ID' => $post_id] );
28: }
29:
30: 31: 32: 33: 34:
35: private function pre_save( $post_id ) {
36: if ( empty( $post_id ) ) {
37: return;
38: }
39:
40: $data = $this->get_pre_data();
41:
42: foreach ( $data as $key => $value ) {
43: if ( empty( $value ) ) {
44: continue;
45: }
46:
47: if ( is_array( $value ) ) {
48: list( $keys, $value ) = $this->get_pre_deep_keys_value( $value );
49: $key = sprintf( '%s_%s', $key, implode( '_', $keys ) );
50: }
51:
52: update_post_meta( $post_id, $key, $value );
53: }
54: }
55:
56: 57: 58: 59: 60: 61:
62: public function save_meta_boxes( $post_id, $post ) {
63:
64: if ( empty( $post_id ) || empty( $post ) ) {
65: return;
66: }
67:
68:
69: if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
70: return;
71: }
72:
73:
74:
75: if ( $this->valid_post_id( $post_id ) ) {
76: return;
77: }
78:
79:
80: if ( ! wp_verify_nonce( papi_get_sanitized_post( 'papi_meta_nonce' ), 'papi_save_data' ) ) {
81: return;
82: }
83:
84:
85: if ( ! current_user_can( 'edit_posts' ) || ! current_user_can( 'edit_pages' ) ) {
86: return;
87: }
88:
89: $this->save_properties( $post_id );
90: }
91:
92: 93: 94: 95: 96:
97: public function save_properties( $post_id ) {
98:
99: $this->pre_save( $post_id );
100:
101:
102: $data = $this->get_post_data();
103:
104:
105: $data = $this->prepare_properties_data( $data, $post_id );
106:
107:
108: $this->overwrite_post_data( $post_id );
109:
110:
111: foreach ( $data as $key => $value ) {
112: papi_update_property_meta_value( [
113: 'post_id' => $post_id,
114: 'slug' => $key,
115: 'value' => $value
116: ] );
117: }
118: }
119:
120: 121: 122:
123: private function setup_actions() {
124: add_action( 'save_post', [$this, 'save_meta_boxes'], 1, 2 );
125: }
126:
127: 128: 129: 130: 131: 132: 133:
134: private function valid_post_id( $post_id ) {
135: $key = papi_get_sanitized_post( 'action' ) === 'save-attachment-compat'
136: ? 'id'
137: : 'post_ID';
138:
139: return papi_get_sanitized_post( $key ) !== strval( $post_id );
140: }
141: }
142:
143: if ( is_admin() ) {
144: new Papi_Admin_Post_Handler;
145: }
146: