PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.1
WebberZone Top 10 — Popular Posts v4.5.1
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / admin / settings / class-metabox-api.php

class-metabox-api.php in WebberZone Top 10 — Popular Posts 4.5.1, at includes/admin/settings/class-metabox-api.php

321 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to display and save a Metabox.
4 *
5 * @package WebberZone\Top_Ten
6 */
7
8 namespace WebberZone\Top_Ten\Admin\Settings;
9
10 // If this file is called directly, abort.
11 if ( ! defined( 'WPINC' ) ) {
12 die;
13 }
14
15 /**
16 * Metabox API class.
17 *
18 * @since 4.0.0
19 */
20 class Metabox_API {
21
22 /**
23 * Current version number
24 *
25 * @var string
26 */
27 const VERSION = '2.3.0';
28
29 /**
30 * Settings Key.
31 *
32 * @var string Settings Key.
33 */
34 public $settings_key;
35
36 /**
37 * Prefix which is used for creating the unique filters and actions.
38 *
39 * @var string Prefix.
40 */
41 public $prefix;
42
43 /**
44 * Name of the Post type.
45 *
46 * @var string Post type.
47 */
48 protected $post_type;
49
50 /**
51 * Title of the Metabox.
52 *
53 * @var string Post type.
54 */
55 protected $title;
56
57 /**
58 * Translation strings.
59 *
60 * @var array Translation strings.
61 */
62 public $translation_strings;
63
64 /**
65 * Array containing the settings' fields.
66 *
67 * @var array Settings fields array.
68 */
69 protected $registered_settings = array();
70
71 /**
72 * Main constructor class.
73 *
74 * @param array|string $args {
75 * Array or string of arguments. Default is blank array.
76 *
77 * @type string $settings_key Settings key - is used to prepare the form fields. It is not the meta key.
78 * @type string $prefix Used to create the meta keys. The meta key format is _{$prefix}_{$setting_id}.
79 * @type string|array|\WP_Screen $post_type The post type(s) on which to show the box.
80 * @type array $registered_settings Settings fields array.
81 * @type array $translation_strings Translation strings.
82 * }
83 */
84 public function __construct( $args ) {
85 $defaults = array(
86 'settings_key' => '',
87 'prefix' => '',
88 'post_type' => '',
89 'title' => '',
90 'registered_settings' => array(),
91 'translation_strings' => array(),
92 );
93
94 $args = wp_parse_args( $args, $defaults );
95
96 foreach ( $args as $name => $value ) {
97 $this->$name = $value;
98 }
99
100 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
101 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
102 add_action( "save_post_{$this->post_type}", array( $this, 'save' ) );
103 }
104
105 /**
106 * Function to add the metabox.
107 */
108 public function add_meta_boxes() {
109 add_meta_box(
110 $this->prefix . '_metabox_id',
111 $this->title,
112 array( $this, 'html' ),
113 $this->post_type,
114 'advanced',
115 'high'
116 );
117 }
118
119 /**
120 * Enqueue scripts and styles.
121 *
122 * @param string $hook The current admin page.
123 */
124 public function admin_enqueue_scripts( $hook ) {
125 if ( in_array( $hook, array( 'post.php', 'post-new.php' ), true ) || get_current_screen()->post_type === $this->post_type ) {
126 $args = array(
127 'strings' => array(
128 'no_results' => isset( $this->translation_strings['tom_select_no_results'] ) ? esc_html( $this->translation_strings['tom_select_no_results'] ) : 'No results found for "%s"',
129 ),
130 );
131 self::enqueue_scripts_styles( $this->prefix, $args );
132 }
133 }
134
135 /**
136 * Enqueues all scripts, styles, settings, and templates necessary to use the Settings API.
137 *
138 * @param string $prefix Prefix which is used for creating the unique filters and actions.
139 * @param array $args Array of arguments.
140 */
141 public static function enqueue_scripts_styles( $prefix, $args = array() ) {
142 Settings_API::enqueue_scripts_styles( $prefix, $args );
143 }
144
145 /**
146 * Function to save the metabox.
147 *
148 * @param int|string $post_id Post ID.
149 */
150 public function save( $post_id ) {
151
152 $post_meta = array();
153
154 // Bail if we're doing an auto save.
155 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
156 return;
157 }
158
159 // If our nonce isn't there, or we can't verify it, bail.
160 if ( ! isset( $_POST[ $this->prefix . '_meta_box_nonce' ] ) || ! wp_verify_nonce( sanitize_key( $_POST[ $this->prefix . '_meta_box_nonce' ] ), $this->prefix . '_meta_box' ) ) {
161 return;
162 }
163
164 // If our current user can't edit this post, bail.
165 if ( ! current_user_can( 'edit_post', $post_id ) ) {
166 return;
167 }
168
169 if ( empty( $_POST[ $this->settings_key ] ) ) {
170 return;
171 }
172
173 $settings_sanitize = new Settings_Sanitize(
174 array(
175 'settings_key' => $this->settings_key,
176 'prefix' => $this->prefix,
177 )
178 );
179
180 $posted = $_POST[ $this->settings_key ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash
181
182 foreach ( $this->registered_settings as $setting ) {
183 $id = $setting['id'];
184 $type = $setting['type'] ?? 'text';
185
186 /**
187 * Skip settings that are not really settings.
188 *
189 * @param array $non_setting_types Array of types which are not settings.
190 */
191 $non_setting_types = apply_filters( $this->prefix . '_metabox_non_setting_types', array( 'header', 'descriptive_text' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
192
193 if ( in_array( $type, $non_setting_types, true ) ) {
194 continue;
195 }
196
197 if ( isset( $posted[ $id ] ) ) {
198 $value = $posted[ $id ];
199 $sanitize_callback = is_callable( array( $settings_sanitize, "sanitize_{$type}_field" ) ) ? array( $settings_sanitize, "sanitize_{$type}_field" ) : array( $settings_sanitize, 'sanitize_missing' );
200 $post_meta[ $id ] = call_user_func( $sanitize_callback, $value );
201 }
202 }
203
204 // Run the array through a generic function that allows access to all of the settings.
205 $post_meta = call_user_func( array( $this, 'sanitize_post_meta' ), $post_meta );
206
207 /**
208 * Filter the post meta array which contains post-specific settings.
209 *
210 * @param array $post_meta Array of metabox settings.
211 * @param int $post_id Post ID
212 */
213 $post_meta = apply_filters( "{$this->prefix}_meta_key", $post_meta, $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
214
215 // Now loop through the settings array and either save or delete the meta key.
216 foreach ( $this->registered_settings as $setting ) {
217 if ( empty( $post_meta[ $setting['id'] ] ) ) {
218 delete_post_meta( $post_id, "_{$this->prefix}_{$setting['id']}" );
219 }
220 }
221
222 foreach ( $post_meta as $setting => $value ) {
223 if ( empty( $post_meta[ $setting ] ) ) {
224 delete_post_meta( $post_id, "_{$this->prefix}_$setting" );
225 } else {
226 update_post_meta( $post_id, "_{$this->prefix}_$setting", $value );
227 }
228 }
229 }
230
231 /**
232 * Function to display the metabox.
233 *
234 * @param \WP_Post $post Post object.
235 */
236 public function html( $post ) {
237 // Add an nonce field so we can check for it later.
238 wp_nonce_field( $this->prefix . '_meta_box', $this->prefix . '_meta_box_nonce' );
239
240 $settings_form = new Settings_Form(
241 array(
242 'settings_key' => $this->settings_key,
243 'prefix' => $this->prefix,
244 'translation_strings' => $this->translation_strings,
245 )
246 );
247
248 echo '<table class="form-table">';
249 foreach ( $this->registered_settings as $setting ) {
250
251 $args = Settings_API::parse_field_args( $setting );
252
253 $id = $args['id'];
254 $value = get_post_meta( $post->ID, "_{$this->prefix}_{$id}", true );
255 $args['value'] = ! empty( $value ) ? $value : ( $args['default'] ?? '' );
256 $type = $args['type'] ?? 'text';
257 $callback = method_exists( $settings_form, "callback_{$type}" ) ? array( $settings_form, "callback_{$type}" ) : array( $settings_form, 'callback_missing' );
258
259 echo '<tr>';
260 echo '<th scope="row">' . wp_kses_post( $args['name'] ) . '</th>';
261 echo '<td>';
262 call_user_func( $callback, $args );
263 echo '</td>';
264 echo '</tr>';
265 }
266 echo '</table>';
267
268 /**
269 * Action triggered when displaying the meta box.
270 *
271 * @param object $post Post object.
272 */
273 do_action( $this->prefix . '_meta_box', $post ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
274 }
275
276 /**
277 * Sanitize Post Meta array.
278 *
279 * @param array $settings Post meta settings array.
280 * @return array Sanitized value.
281 */
282 public function sanitize_post_meta( $settings ) {
283
284 // This array holds a list of keys that will be passed through our category/tags loop to determine the ids.
285 $keys = array(
286 'include_on_category' => array(
287 'tax' => 'category',
288 'ids_field' => 'include_on_category_ids',
289 ),
290 'include_on_post_tag' => array(
291 'tax' => 'post_tag',
292 'ids_field' => 'include_on_post_tag_ids',
293 ),
294 );
295
296 foreach ( $keys as $key => $fields ) {
297 if ( isset( $settings[ $key ] ) ) {
298 $ids = array();
299 $names = array();
300
301 $taxes = array_unique( str_getcsv( $settings[ $key ], ',', '"', '' ) );
302
303 foreach ( $taxes as $tax ) {
304 $tax_name = get_term_by( 'name', $tax, $fields['tax'] );
305
306 if ( isset( $tax_name->term_taxonomy_id ) ) {
307 $ids[] = $tax_name->term_taxonomy_id;
308 $names[] = $tax_name->name;
309 }
310 }
311 $settings[ $fields['ids_field'] ] = join( ',', $ids );
312 $settings[ $key ] = Settings_Sanitize::str_putcsv( $names );
313 } else {
314 $settings[ $fields['ids_field'] ] = '';
315 }
316 }
317
318 return $settings;
319 }
320 }
321