1: <?php
2: /**
3: * Metabox Oject
4: *
5: * @package EPL
6: * @subpackage Classes/MetaboxesCustomFields
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 2.3
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_METABOX class
19:
20: * Can also be used by extensions to handle all the metabox functionality
21: * it adds metabox wrapper & renders metabox fields and finally save it on save_post hook
22: * the constructor of the class accepts one or more array of metabox to be rendered
23: * the structure of the metabox array should be similar to make it work
24: *
25: * @since 2.0
26: */
27: class EPL_METABOX {
28:
29: /**
30: * Holds the user submitted metabox array
31: *
32: * @var array $epl_meta_boxes
33: */
34: protected $epl_meta_boxes;
35:
36: /**
37: * Prefix used in nonces and other places to make them unique
38: *
39: * Default is epl_
40: *
41: * @var array $epl_meta_boxes
42: */
43: protected $prefix;
44:
45: /**
46: * Translation domain used to translate string
47: *
48: * Default is epl
49: *
50: * @var array $text_domain
51: */
52: protected $text_domain;
53:
54: /**
55: * Constructor
56: *
57: * Register a mea box.
58: *
59: * @param mixed $epl_meta_boxes The name(s) of the meta box.
60: * @param string $prefix Prefix of meta box.
61: * @param string $text_domain Text domain name.
62: */
63: public function __construct( $epl_meta_boxes, $prefix = 'epl_', $text_domain = 'easy-property-listings' ) {
64:
65: $this->epl_meta_boxes = $epl_meta_boxes;
66:
67: $this->prefix = (string) $prefix;
68:
69: $this->text_domain = (string) $text_domain;
70:
71: // Register meta boxes.
72: $this->add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) );
73:
74: // Save meta boxes.
75: $this->add_action( 'save_post', array( &$this, 'save_meta_box' ) );
76: }
77:
78: /**
79: * Add Action
80: *
81: * Helper function to add add_action WordPress filters.
82: *
83: * @param string $action Name of the action.
84: * @param string $callback_function Function to hook that will run on action.
85: * @param int $priority Order in which to execute the function, relation to other functions hooked to this action.
86: * @param integer $accepted_args The number of arguments the function accepts.
87: */
88: public function add_action( $action, $callback_function, $priority = 10, $accepted_args = 1 ) {
89:
90: // Pass variables into WordPress add_action function.
91: add_action( $action, $callback_function, $priority, $accepted_args );
92: }
93:
94: /**
95: * Add Filter
96: *
97: * Create add_filter WordPress filter.
98: *
99: * @see http://codex.wordpress.org/Function_Reference/add_filter
100: *
101: * @param string $action Name of the action to hook to, e.g 'init'.
102: * @param string $callback_function Function to hook that will run on @action.
103: * @param int $priority Order in which to execute the function, relation to other function hooked to this action.
104: * @param int $accepted_args The number of arguements the function accepts.
105: */
106: public function add_filter( $action, $callback_function, $priority = 10, $accepted_args = 1 ) {
107:
108: // Pass variables into WordPress add_action function.
109: add_filter( $action, $callback_function, $priority, $accepted_args );
110: }
111:
112: /**
113: * Add metaboxes
114: *
115: * See how to register custom field metaboxes to listings here.
116: *
117: * @link https://codex.easypropertylistings.com.au/article/127-epllistingmetaboxes-filter
118: */
119: public function add_meta_boxes() {
120:
121: if ( ! empty( $this->epl_meta_boxes ) ) {
122: foreach ( $this->epl_meta_boxes as $epl_meta_box ) {
123:
124: // If we have multiple metaboxes.
125: if ( isset( $epl_meta_box['id'] ) && is_array( $epl_meta_box ) ) {
126: // Multiple post type.
127: if ( is_array( $epl_meta_box['post_type'] ) ) {
128: foreach ( $epl_meta_box['post_type'] as $post_type ) {
129: $this->add_meta_box(
130: $epl_meta_box['id'],
131: $epl_meta_box['label'],
132: 'inner_meta_box',
133: $post_type,
134: $epl_meta_box['context'],
135: $epl_meta_box['priority'],
136: $epl_meta_box
137: );
138: }
139: } else {
140: $this->add_meta_box(
141: $epl_meta_box['id'],
142: $epl_meta_box['label'],
143: 'inner_meta_box',
144: $epl_meta_box['post_type'],
145: $epl_meta_box['context'],
146: $epl_meta_box['priority'],
147: $epl_meta_box
148: );
149: }
150: } else {
151: // If we have single metabox.
152: $epl_meta_box = $this->epl_meta_boxes;
153: // Multiple post type.
154: if ( is_array( $epl_meta_box['post_type'] ) ) {
155: foreach ( $epl_meta_box['post_type'] as $post_type ) {
156: $this->add_meta_box(
157: $epl_meta_box['id'],
158: $epl_meta_box['label'],
159: 'inner_meta_box',
160: $post_type,
161: $epl_meta_box['context'],
162: $epl_meta_box['priority'],
163: $epl_meta_box
164: );
165: }
166: } else {
167: $this->add_meta_box(
168: $epl_meta_box['id'],
169: $epl_meta_box['label'],
170: 'inner_meta_box',
171: $epl_meta_box['post_type'],
172: $epl_meta_box['context'],
173: $epl_meta_box['priority'],
174: $epl_meta_box
175: );
176: }
177: break;
178: }
179: }
180: }
181: }
182:
183: /**
184: * Class wrapper for WordPress function add_meta_box
185: *
186: * @see https://codex.wordpress.org/Function_Reference/add_meta_box
187: *
188: * @param string $id Meta box ID (used in the 'id' attribute for the meta box).
189: * @param string $label Title of the meta box.
190: * @param string $func Function that fills the box with the desired content.
191: * The function should echo its output.
192: * @param array $post_type Post type name.
193: * @param string $context Optional. The context within the screen where the boxes
194: * should display. Available contexts vary from screen to
195: * screen. Post edit screen contexts include 'normal', 'side',
196: * and 'advanced'. Comments screen contexts include 'normal'
197: * and 'side'. Menus meta boxes (accordion sections) all use
198: * the 'side' context. Global default is 'advanced'.
199: * @param string $priority Optional. The priority within the context where the boxes
200: * should show ('high', 'low'). Default 'default'.
201: * @param array $args Optional. Data that should be set as the $args property
202: * of the box array (which is the second parameter passed
203: * to your callback). Default null.
204: */
205: public function add_meta_box( $id = '', $label = '', $func = 'inner_meta_box', $post_type = array(), $context = 'normal', $priority = 'default', $args = null ) {
206: add_meta_box(
207: $id,
208: $label,
209: array( $this, $func ),
210: $post_type,
211: $context,
212: $priority,
213: $args
214: );
215: }
216:
217: /**
218: * Used to render the metabox fields
219: *
220: * @param array $post Post object.
221: * @param array $args Array of options.
222: *
223: * @since 3.4.19 Updated to new html structure for fields using html lists
224: */
225: public function inner_meta_box( $post, $args ) {
226: $groups = $args['args']['groups'];
227: $groups = array_filter( $groups );
228: $render_as = empty( $args['args']['render_as'] ) ? 'default' : sanitize_text_field( $args['args']['render_as'] );
229: // This supports both tabs-horizontal and tabs-vertical options.
230: if ( ! empty( $groups ) ) {
231: wp_nonce_field( $this->prefix . 'inner_custom_box', $this->prefix . 'inner_custom_box_nonce' );
232: if ( in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) {
233: // Wrapper for tabs.
234: echo '<div class="epl-group-tab-wrapper epl-' . esc_attr( $render_as ) . '">';
235: }
236: if ( in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) { ?>
237: <ul class="epl-group-tabs">
238: <?php
239: foreach ( $groups as $group ) {
240: echo '<li><a href="#epl-field-group-' . esc_attr( $group['id'] ) . '"><h3>' . esc_attr( $group['label'] ) . '</h3></a></li>';
241: }
242: ?>
243: </ul>
244: <?php
245: }
246: foreach ( $groups as $group ) {
247: ?>
248: <div id="epl-field-group-<?php echo esc_attr( $group['id'] ); ?>">
249: <div class="epl-inner-div col-<?php echo esc_attr( $group['columns'] ); ?> table-<?php echo esc_attr( $args['args']['context'] ); ?>">
250: <?php
251: $group['label'] = trim( $group['label'] );
252: if ( ! empty( $group['label'] ) && ! in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) {
253: echo '<h3>' . esc_attr( $group['label'] ) . '</h3>';
254: }
255: ?>
256: <ul class="form-table epl-form-table">
257: <?php
258: $fields = $group['fields'];
259: $gp_field_width = isset( $group['width'] ) ? $group['width'] : '1';
260: $fields = array_filter( $fields );
261: if ( ! empty( $fields ) ) {
262: foreach ( $fields as $field ) {
263: if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) {
264: if ( in_array( $post->post_type, $field['exclude'], true ) ) {
265: continue;
266: }
267: }
268:
269: if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) {
270: if ( ! in_array( $post->post_type, $field['include'], true ) ) {
271: continue;
272: }
273: }
274: $val = get_post_meta( $post->ID, $field['name'], true );
275: if ( has_action( 'epl_before_meta_field_' . $field['name'] ) ) {
276: do_action( 'epl_before_meta_field_' . $field['name'], $post, $val );
277: }
278:
279: $field_width = isset( $field['width'] ) ? $field['width'] : $gp_field_width;
280: ?>
281: <li id="epl_<?php echo esc_attr( $field['name'] ); ?>_wrap" class="epl-form-field-wrap epl-grid-<?php echo esc_attr( $field_width ); ?> epl_<?php echo esc_attr( $field['name'] ); ?>_wrap epl-field-type-<?php echo esc_attr( $field['type'] ); ?>">
282:
283:
284: <?php if ( 'checkbox_single' !== $field['type'] || ( isset( $field['opts'] ) && 1 !== count( $field['opts'] ) ) ) : ?>
285: <div class="form-field epl-form-field-label">
286: <span valign="top" scope="row">
287: <label for="<?php echo esc_attr( $field['name'] ); ?>"><?php echo esc_attr( $field['label'] ); ?></label>
288: </span>
289: </div>
290: <?php endif; ?>
291:
292:
293: <div id="epl_<?php echo esc_attr( $field['name'] ); ?>" class="form-field epl-form-field-value epl_<?php echo esc_attr( $field['name'] ); ?>">
294: <?php
295: epl_render_html_fields( $field, $val );
296: ?>
297: </div>
298:
299: </li>
300:
301: <?php
302: if ( has_action( 'epl_after_meta_field_' . $field['name'] ) ) {
303: do_action( 'epl_after_meta_field_' . $field['name'], $post, $val );
304: }
305: ?>
306: <?php
307: }
308: }
309: ?>
310: </ul>
311: </div>
312: </div>
313: <?php
314: }
315: ?>
316: <input type="hidden" name="epl_meta_box_ids[]" value="<?php echo esc_attr( $args['id'] ); ?>" />
317: <div class="epl-clear"></div>
318: <?php
319: if ( in_array( $render_as, array( 'tabs-horizontal', 'tabs-vertical' ), true ) ) {
320: // Wrapper end.
321: echo '</div>';
322: }
323: }
324: }
325:
326: /**
327: * Callback function hooked on WordPress save_post hook
328: * used to save all the meta fields
329: *
330: * @see https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
331: *
332: * @param int $post_ID The post ID.
333: *
334: * @return int
335: * @since 3.4.17 Fixed issue : empty values not getting saved for decimals & numbers
336: */
337: public function save_meta_box( $post_ID ) {
338:
339: if ( ! isset( $_POST[ $this->prefix . 'inner_custom_box_nonce' ] ) ) {
340: return $post_ID;
341: }
342:
343: $nonce = $_POST[ $this->prefix . 'inner_custom_box_nonce' ]; //phpcs:ignore
344:
345: if ( ! wp_verify_nonce( $nonce, $this->prefix . 'inner_custom_box' ) ) {
346: return $post_ID;
347: }
348:
349: if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
350: return $post_ID;
351: }
352:
353: if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) {
354: if ( ! current_user_can( 'edit_page', $post_ID ) ) {
355: return $post_ID;
356: }
357: } else {
358: if ( ! current_user_can( 'edit_post', $post_ID ) ) {
359: return $post_ID;
360: }
361: }
362:
363: $epl_meta_box_ids = '';
364: if ( isset( $_POST['epl_meta_box_ids'] ) ) {
365: $epl_meta_box_ids = wp_unslash( $_POST['epl_meta_box_ids'] ); //phpcs:ignore
366: }
367:
368: if ( ! empty( $epl_meta_box_ids ) ) {
369: if ( ! empty( $this->epl_meta_boxes ) ) {
370: foreach ( $epl_meta_box_ids as $epl_meta_box_id ) {
371: foreach ( $this->epl_meta_boxes as $epl_meta_box ) {
372: if ( $epl_meta_box['id'] === $epl_meta_box_id ) {
373: if ( ! empty( $epl_meta_box['groups'] ) ) {
374: foreach ( $epl_meta_box['groups'] as $group ) {
375:
376: $fields = $group['fields'];
377: if ( ! empty( $fields ) ) {
378: foreach ( $fields as $field ) {
379:
380: // Dont go further if the current post type is in excluded list of the current field.
381: if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) {
382: if ( isset( $_POST['post_type'] ) && in_array( $_POST['post_type'], $field['exclude'], true ) ) {
383: continue;
384: }
385: }
386:
387: // Dont go further if the current post type is not in included list of the current field.
388: if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) {
389: if ( ! in_array( $_POST['post_type'], $field['include'] ) ) { // phpcs:ignore
390: continue;
391: }
392: }
393:
394: if ( 'radio' === $field['type'] ) {
395: if ( ! isset( $_POST[ $field['name'] ] ) ) {
396: continue;
397: }
398: } elseif ( 'checkbox_single' === $field['type'] ) {
399: if ( ! isset( $_POST[ $field['name'] ] ) ) {
400: $_POST[ $field['name'] ] = '';
401: }
402: } elseif ( in_array( $field['type'], array( 'number', 'decimal' ), true ) ) {
403:
404: // Validate numeric data.
405: if ( ! is_numeric( $_POST[ $field['name'] ] ) && ! empty( $_POST[ $field['name'] ] ) ) {
406: continue;
407: }
408: } elseif ( in_array( $field['type'], array( 'textarea' ), true ) ) {
409:
410: if ( function_exists( 'sanitize_textarea_field' ) ) {
411: $_POST[ $field['name'] ] =
412: sanitize_textarea_field( wp_unslash( $_POST[ $field['name'] ] ) );
413: }
414: } elseif ( in_array( $field['type'], array( 'url', 'file' ), true ) ) {
415:
416: // Sanitize URLs.
417: $_POST[ $field['name'] ] = esc_url_raw( wp_unslash( $_POST[ $field['name'] ] ) );
418:
419: } elseif ( 'auction-date' === $field['type'] && ! empty( $_POST[ $field['name'] ] ) ) {
420: $epl_date = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) );
421: if ( strpos( $epl_date, 'T' ) !== false ) {
422: $epl_date = gmdate( 'Y-m-d\TH:i', strtotime( $epl_date ) );
423: } else {
424: $epl_date = DateTime::createFromFormat( 'Y-m-d-H:i:s', $epl_date );
425:
426: if ( $epl_date ) {
427: $epl_date = $epl_date->format( 'Y-m-d\TH:i' );
428: }
429: }
430: $_POST[ $field['name'] ] = $epl_date;
431: } elseif ( 'sold-date' === $field['type'] && ! empty( $_POST[ $field['name'] ] ) ) {
432: $epl_date = sanitize_text_field( wp_unslash( $_POST[ $field['name'] ] ) );
433: if ( strpos( $epl_date, 'T' ) !== false ) {
434: $epl_date = gmdate( 'Y-m-d\TH:i', strtotime( $epl_date ) );
435: } else {
436: $epl_date = DateTime::createFromFormat( 'Y-m-d', $epl_date );
437:
438: if ( $epl_date ) {
439: $epl_date = $epl_date->format( 'Y-m-d' );
440: }
441: }
442: $_POST[ $field['name'] ] = $epl_date;
443: }
444: if ( isset( $_POST[ $field['name'] ] ) ) {
445: $meta_value = wp_unslash( $_POST[ $field['name'] ] ); // phpcs:ignore
446: update_post_meta( $post_ID, $field['name'], $meta_value );
447: }
448: }
449: }
450: }
451: }
452: }
453: }
454: }
455: }
456: }
457: }
458: }
459: