1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11: 
 12: function papi_delete_field( $post_id = null, $slug = null, $type = 'post' ) {
 13:     if ( ! is_numeric( $post_id ) && is_string( $post_id ) ) {
 14:         $slug    = $post_id;
 15:         $post_id = null;
 16:     }
 17: 
 18:     if ( ! is_string( $slug ) || empty( $slug ) ) {
 19:         return false;
 20:     }
 21: 
 22:     $post_id = papi_get_post_id( $post_id );
 23: 
 24:     if ( $post_id === 0 && $type === Papi_Core_Page::TYPE_POST ) {
 25:         return false;
 26:     }
 27: 
 28:     $page = papi_get_page( $post_id, $type );
 29: 
 30:     if ( is_null( $page ) ) {
 31:         return false;
 32:     }
 33: 
 34:     $property = $page->get_property( $slug );
 35: 
 36:     if ( ! papi_is_property( $property ) ) {
 37:         return false;
 38:     }
 39: 
 40:     papi_cache_delete( $slug, $post_id );
 41: 
 42:     papi_action_delete_value( $type, $slug, $post_id );
 43: 
 44:     return $property->delete_value( $slug, $post_id, $type );
 45: }
 46: 
 47:  48:  49:  50:  51:  52:  53:  54:  55: 
 56: function papi_field_shortcode( $atts ) {
 57:     $atts['id'] = isset( $atts['id'] ) ? $atts['id'] : 0;
 58:     $atts['id'] = papi_get_post_id( $atts['id'] );
 59:     $default    = isset( $atts['default'] ) ? $atts['default'] : '';
 60: 
 61:     if ( empty( $atts['id'] ) || empty( $atts['slug'] ) ) {
 62:         $value = $default;
 63:     } else {
 64:         $value = papi_get_field( $atts['id'], $atts['slug'], $default );
 65:     }
 66: 
 67:     if ( is_array( $value ) ) {
 68:         $value = implode( ', ', $value );
 69:     }
 70: 
 71:     return $value;
 72: }
 73: 
 74: add_shortcode( 'papi_field', 'papi_field_shortcode' );
 75: 
 76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88: 
 89: function papi_field_value( $slugs, $value, $default = null ) {
 90:     if ( empty( $value ) && is_null( $value ) ) {
 91:         return $default;
 92:     }
 93: 
 94:     if ( ! empty( $slugs ) && ( is_object( $value ) || is_array( $value ) ) ) {
 95: 
 96:         if ( is_object( $value ) ) {
 97:             $value = (array) $value;
 98:         }
 99: 
100:         foreach ( $slugs as $key ) {
101:             if ( isset( $value[ $key ] ) ) {
102:                 $value = $value[ $key ];
103:             }
104:         }
105:     }
106: 
107:     return $value;
108: }
109: 
110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 
120: function papi_get_field( $post_id = null, $slug = null, $default = null, $type = 'post' ) {
121:     if ( ! is_numeric( $post_id ) && is_string( $post_id ) ) {
122:         $default = $slug;
123:         $slug    = $post_id;
124:         $post_id = null;
125:     }
126: 
127:     if ( ! is_string( $slug ) || empty( $slug ) ) {
128:         return $default;
129:     }
130: 
131:     $post_id = papi_get_post_id( $post_id );
132: 
133:     if ( $post_id === 0 && $type === Papi_Core_Page::TYPE_POST ) {
134:         return $default;
135:     }
136: 
137:     $value = papi_cache_get( $slug, $post_id );
138: 
139:     if ( $value === null || $value === false ) {
140:         
141:         $slugs = explode( '.', $slug );
142:         $slug  = $slugs[0];
143:         $slugs = array_slice( $slugs, 1 );
144: 
145:         
146:         $page = papi_get_page( $post_id, $type );
147: 
148:         
149:         if ( is_null( $page ) ) {
150:             return $default;
151:         }
152: 
153:         $value = papi_field_value( $slugs, $page->get_value( $slug ), $default );
154: 
155:         if ( papi_is_empty( $value ) ) {
156:             return $default;
157:         }
158: 
159:         papi_cache_set( $slug, $post_id, $value );
160:     }
161: 
162:     return $value;
163: }
164: 
165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 
175: function papi_update_field( $post_id = null, $slug = null, $value = null, $type = 'post' ) {
176:     if ( ! is_numeric( $post_id ) && is_string( $post_id ) ) {
177:         $value   = $slug;
178:         $slug    = $post_id;
179:         $post_id = null;
180:     }
181: 
182:     if ( ! is_string( $slug ) || empty( $slug ) ) {
183:         return false;
184:     }
185: 
186:     if ( papi_is_empty( $value ) ) {
187:         return papi_delete_field( $post_id, $slug, $type );
188:     }
189: 
190:     $post_id = papi_get_post_id( $post_id );
191: 
192:     if ( $post_id === 0 && $type === Papi_Core_Page::TYPE_POST ) {
193:         return false;
194:     }
195: 
196:     $page = papi_get_page( $post_id, $type );
197: 
198:     if ( is_null( $page ) ) {
199:         return false;
200:     }
201: 
202:     $property = $page->get_property( $slug );
203: 
204:     if ( ! papi_is_property( $property ) ) {
205:         return false;
206:     }
207: 
208:     papi_delete_field( $post_id, $slug, $type );
209: 
210:     $value = $property->update_value( $value, $slug, $post_id );
211:     $value = papi_filter_update_value( $property->get_option( 'type' ), $value, $slug, $post_id );
212: 
213:     return papi_update_property_meta_value( [
214:         'post_id'       => $post_id,
215:         'slug'          => $slug,
216:         'value'         => $value
217:     ] );
218: }
219: 
220: 221: 222: 223: 224: 225: 226: 
227: function the_papi_field( $post_id = null, $slug = null, $default = null ) {
228:     $value = papi_get_field( $post_id, $slug, $default );
229: 
230:     if ( is_array( $value ) ) {
231:         $value = implode( ', ', $value );
232:     }
233: 
234:     echo $value;
235: }
236: