| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Meta Box Helper |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Meta_Box_Helper |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2022, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Meta_Box_Helper' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable Meta Box Helper |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Charitable_Meta_Box_Helper { |
| 26 |
|
| 27 |
/** |
| 28 |
* Nonce action. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $nonce_action; |
| 35 |
|
| 36 |
/** |
| 37 |
* Nonce name. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $nonce_name = '_charitable_nonce'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Whether nonce has been added. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
* |
| 50 |
* @var boolean |
| 51 |
*/ |
| 52 |
protected $nonce_added = false; |
| 53 |
|
| 54 |
/** |
| 55 |
* Create a helper instance. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @global WP_Post $post |
| 60 |
* @param string $nonce_action Action for the nonce. |
| 61 |
*/ |
| 62 |
public function __construct( $nonce_action = 'charitable' ) { |
| 63 |
$this->nonce_action = $nonce_action; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Metabox callback wrapper. |
| 68 |
* |
| 69 |
* Every meta box is registered with this method as its callback, |
| 70 |
* and then delegates to the appropriate view. |
| 71 |
* |
| 72 |
* @since 1.0.0 |
| 73 |
* |
| 74 |
* @param WP_Post $post The post object. |
| 75 |
* @param array $args The arguments passed to the meta box, including the view to render. |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function metabox_display( WP_Post $post, array $args ) { |
| 79 |
if ( ! isset( $args['args']['view'] ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$view_args = $args['args']; |
| 84 |
|
| 85 |
unset( $view_args['view'] ); |
| 86 |
|
| 87 |
$this->display( $args['args']['view'], $view_args ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Display a metabox with the given view. |
| 92 |
* |
| 93 |
* @since 1.0.0 |
| 94 |
* |
| 95 |
* @param string $view The view to render. |
| 96 |
* @param array $view_args Arguments to pass to the view. |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function display( $view, $view_args ) { |
| 100 |
/** |
| 101 |
* Set the nonce. |
| 102 |
*/ |
| 103 |
if ( false === $this->nonce_added ) { |
| 104 |
|
| 105 |
wp_nonce_field( $this->nonce_action, $this->nonce_name ); |
| 106 |
|
| 107 |
$this->nonce_added = true; |
| 108 |
} |
| 109 |
|
| 110 |
do_action( 'charitable_metabox_before', $view, $view_args ); |
| 111 |
|
| 112 |
charitable_admin_view( $view, $view_args ); |
| 113 |
|
| 114 |
do_action( 'charitable_metabox_after', $view, $view_args ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Display the fields to show inside a metabox. |
| 119 |
* |
| 120 |
* The fields parameter should contain an array of fields, |
| 121 |
* all of which are arrays with a 'priority' key and a 'view' |
| 122 |
* key. |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
* |
| 126 |
* @param array $fields The set of fields to be displayed. |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function display_fields( array $fields ) { |
| 130 |
$form = new Charitable_Admin_Form(); |
| 131 |
$form->set_fields( $fields ); |
| 132 |
$form->view()->render_fields(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Verifies that the user who is currently logged in has permission to save the data |
| 137 |
* from the meta box to the database. |
| 138 |
* |
| 139 |
* Hat tip Tom McFarlin: http://tommcfarlin.com/wordpress-meta-boxes-each-component/ |
| 140 |
* |
| 141 |
* @since 1.0.0 |
| 142 |
* |
| 143 |
* @param integer $post_id The current post being saved. |
| 144 |
* @return boolean True if the user can save the information. |
| 145 |
*/ |
| 146 |
public function user_can_save( $post_id ) { |
| 147 |
$is_autosave = wp_is_post_autosave( $post_id ); |
| 148 |
$is_revision = wp_is_post_revision( $post_id ); |
| 149 |
$is_valid_nonce = ( isset( $_POST[ $this->nonce_name ] ) && wp_verify_nonce( $_POST[ $this->nonce_name ], $this->nonce_action ) ); |
| 150 |
|
| 151 |
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
endif; |
| 156 |
|