PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.1
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.1
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / admin / campaigns / class-charitable-campaign-meta-boxes.php

class-charitable-campaign-meta-boxes.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.1, at includes/admin/campaigns/class-charitable-campaign-meta-boxes.php

481 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The class that defines how campaigns are managed on the admin side.
4 *
5 * @package Charitable/Admin/Charitable_Campaign_Meta_Boxes
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.5.0
10 * @version 1.6.45
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Campaign_Meta_Boxes' ) ) :
18
19 /**
20 * Charitable_Campaign_Meta_Boxes class.
21 *
22 * @final
23 * @since 1.0.0
24 */
25 final class Charitable_Campaign_Meta_Boxes {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_Campaign_Meta_Boxes|null
31 */
32 private static $instance = null;
33
34 /**
35 * Meta Box Helper class instance.
36 *
37 * @var Charitable_Meta_Box_Helper
38 */
39 private $meta_box_helper;
40
41 /**
42 * Create object instance.
43 *
44 * @since 1.0.0
45 */
46 private function __construct() {
47 $this->meta_box_helper = new Charitable_Meta_Box_Helper( 'charitable-campaign' );
48 }
49
50 /**
51 * Returns and/or create the single instance of this class.
52 *
53 * @since 1.2.0
54 *
55 * @return Charitable_Campaign_Meta_Boxes
56 */
57 public static function get_instance() {
58 if ( is_null( self::$instance ) ) {
59 self::$instance = new self();
60 }
61
62 return self::$instance;
63 }
64
65 /**
66 * Return the instance of the meta box helper.
67 *
68 * @since 1.6.0
69 *
70 * @return Charitable_Meta_Box_Helper
71 */
72 public function get_meta_box_helper() {
73 return $this->meta_box_helper;
74 }
75
76 /**
77 * Add meta boxes.
78 *
79 * @see add_meta_boxes hook
80 *
81 * @since 1.0.0
82 *
83 * @return void
84 */
85 public function add_meta_boxes() {
86 $meta_boxes = array_merge(
87 $this->get_campaign_top_meta_boxes(),
88 array(
89 array(
90 'id' => 'campaign-settings',
91 'title' => __( 'Campaign Settings', 'charitable' ),
92 'context' => 'normal',
93 'priority' => 'high',
94 'view' => 'metaboxes/campaign-settings',
95 ),
96 )
97 );
98
99 /**
100 * Filter campaign meta boxes.
101 *
102 * @since 1.0.0
103 *
104 * @param array $meta_boxes The meta boxes.
105 */
106 $meta_boxes = apply_filters( 'charitable_campaign_meta_boxes', $meta_boxes );
107
108 foreach ( $meta_boxes as $meta_box ) {
109 add_meta_box(
110 $meta_box['id'],
111 $meta_box['title'],
112 array( $this->meta_box_helper, 'metabox_display' ),
113 Charitable::CAMPAIGN_POST_TYPE,
114 $meta_box['context'],
115 $meta_box['priority'],
116 $meta_box
117 );
118 }
119 }
120
121 /**
122 * Display fields at the very top of the page.
123 *
124 * @since 1.0.0
125 *
126 * @param WP_Post $post Current post.
127 * @return void
128 */
129 public function campaign_form_top( $post ) {
130 if ( Charitable::CAMPAIGN_POST_TYPE === $post->post_type ) {
131 do_meta_boxes( Charitable::CAMPAIGN_POST_TYPE, 'campaign-top', $post );
132 }
133 }
134
135 /**
136 * Return campaign settings panels.
137 *
138 * @since 1.6.0
139 *
140 * @return array
141 */
142 public function get_campaign_settings_panels() {
143 $panels = charitable()->campaign_fields()->get_sections( 'admin' );
144
145 foreach ( $panels as $section => $label ) {
146 $panels[ $section ] = array(
147 'title' => $label,
148 'fields' => $this->get_section_fields( $section ),
149 );
150
151 if ( 'campaign-donation-options' === $section ) {
152 /**
153 * Filter the fields in the Donation Options panel.
154 *
155 * @since 1.0.0
156 *
157 * @param array $fields The array of fields.
158 */
159 $panels[ $section ]['fields'] = apply_filters( 'charitable_campaign_donation_options_fields', $panels[ $section ]['fields'] );
160 }
161 }
162
163 $panels = $this->add_legacy_meta_boxes( $panels );
164
165 /**
166 * Filter out empty panels.
167 */
168 $panels = array_filter( $panels, array( $this, 'panel_has_fields' ) );
169
170 /**
171 * Filter the array of settings panels.
172 *
173 * @since 1.6.0
174 *
175 * @param $panels array Set of panels.
176 */
177 return apply_filters( 'charitable_campaign_settings_panels', $panels );
178 }
179
180 /**
181 * Save meta for the campaign.
182 *
183 * @since 1.0.0
184 *
185 * @param int $campaign_id The campaign ID.
186 * @param WP_Post $post Current Post object.
187 * @return void
188 */
189 public function save_campaign( $campaign_id, WP_Post $post ) {
190 if ( ! $this->meta_box_helper->user_can_save( $campaign_id ) ) {
191 return;
192 }
193
194 $meta_keys = $this->get_campaign_meta_keys();
195 $submitted = $_POST;
196 $data = array(
197 'ID' => $campaign_id,
198 );
199
200 foreach ( $meta_keys as $key ) {
201 $value = isset( $submitted[ $key ] ) ? $submitted[ $key ] : false;
202
203 /**
204 * This filter is deprecated. Use charitable_sanitize_campaign_meta{$key} instead.
205 *
206 * @deprecated 1.7.0
207 *
208 * @since 1.4.12 Deprecated.
209 */
210 $value = apply_filters( 'charitable_sanitize_campaign_meta', $value, $key, $submitted, $campaign_id );
211
212 /**
213 * Filter this meta value. The filter hook is
214 * charitable_sanitize_campaign_meta{$key}. For example,
215 * for _campaign_end_date the filter hook will be:
216 * charitable_sanitize_campaign_meta_campaign_end_date
217 *
218 * @since 1.4.12
219 *
220 * @param mixed $value The submitted value, or false.
221 * @param array $submitted All of the submitted values.
222 * @param int $campaign_id The campaign ID.
223 */
224 $data[ $key ] = apply_filters( 'charitable_sanitize_campaign_meta' . $key, $value, $submitted, $campaign_id );
225 }
226
227 $processor = new Charitable_Campaign_Processor( $data, $meta_keys );
228 $processor->save_meta();
229
230 /**
231 * Do something with the posted data.
232 *
233 * @since 1.0.0
234 *
235 * @param WP_Post $post An instance of `WP_Post`.
236 */
237 do_action( 'charitable_campaign_save', $post );
238 }
239
240 /**
241 * Set default post content when the extended description is left empty.
242 *
243 * @since 1.4.0
244 *
245 * @param array $data Submitted data.
246 * @return array
247 */
248 public function set_default_post_content( $data ) {
249 if ( Charitable::CAMPAIGN_POST_TYPE != $data['post_type'] ) {
250 return $data;
251 }
252
253 if ( 0 === strlen( $data['post_content'] ) ) {
254 $data['post_content'] = '<!-- Code is poetry -->';
255 }
256
257 return $data;
258 }
259
260 /**
261 * Sets the placeholder text of the campaign title field.
262 *
263 * @since 1.0.0
264 *
265 * @param string $placeholder Placeholder text.
266 * @param WP_Post $post Current Post object.
267 * @return string
268 */
269 public function campaign_enter_title( $placeholder, WP_Post $post ) {
270 if ( 'campaign' == $post->post_type ) {
271 $placeholder = __( 'Enter campaign title', 'charitable' );
272 }
273
274 return $placeholder;
275 }
276
277 /**
278 * Change messages when a post type is updated.
279 *
280 * @global WP_Post $post
281 * @global int $post_ID
282 * @param array $messages Messages to display.
283 * @return array
284 */
285 public function post_messages( $messages ) {
286 global $post, $post_ID;
287
288 $messages[ Charitable::CAMPAIGN_POST_TYPE ] = array(
289 // Unused. Messages start at index 1.
290 0 => '',
291 1 => sprintf(
292 __( 'Campaign updated. <a href="%s">View Campaign</a>', 'charitable' ),
293 esc_url( get_permalink( $post_ID ) )
294 ),
295 2 => __( 'Custom field updated.', 'charitable' ),
296 3 => __( 'Custom field deleted.', 'charitable' ),
297 4 => __( 'Campaign updated.', 'charitable' ),
298 5 => isset( $_GET['revision'] )
299 ? sprintf( __( 'Campaign restored to revision from %s', 'charitable' ), wp_post_revision_title( (int) $_GET['revision'], false ) )
300 : false,
301 6 => sprintf(
302 __( 'Campaign published. <a href="%s">View Campaign</a>', 'charitable' ),
303 esc_url( get_permalink( $post_ID ) )
304 ),
305 7 => __( 'Campaign saved.', 'charitable' ),
306 8 => sprintf(
307 __( 'Campaign submitted. <a target="_blank" href="%s">Preview Campaign</a>', 'charitable' ),
308 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
309 ),
310 9 => sprintf(
311 __( 'Campaign scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Campaign</a>', 'charitable' ),
312 date_i18n( __( 'M j, Y @ G:i', 'charitable' ),strtotime( $post->post_date ) ),
313 esc_url( get_permalink( $post_ID ) )
314 ),
315 10 => sprintf(
316 __( 'Campaign draft updated. <a target="_blank" href="%s">Preview Campaign</a>', 'charitable' ),
317 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
318 ),
319 );
320
321 return $messages;
322 }
323
324 /**
325 * Return the set of meta boxes to be shown in the "campaign-top" section of the campaign editor.
326 *
327 * @since 1.6.0
328 *
329 * @return array
330 */
331 private function get_campaign_top_meta_boxes() {
332 $boxes = $this->get_section_fields( 'campaign-top' );
333
334 foreach ( $boxes as $key => $box ) {
335 $box['id'] = str_replace( '_', '-', substr( $key, 1 ) );
336 $box['title'] = $box['label'];
337 $box['context'] = 'campaign-top';
338 $box['priority'] = 'high';
339 $boxes[ $key ] = $box;
340 }
341
342 return $boxes;
343 }
344
345 /**
346 * Add panels for any meta boxes previously added to the campaign-advanced section
347 * using the `charitable_campaign_meta_boxes` filter.
348 *
349 * @since 1.6.0
350 *
351 * @param array $panels Core registered panels.
352 * @return array
353 */
354 private function add_legacy_meta_boxes( $panels ) {
355 foreach ( apply_filters( 'charitable_campaign_meta_boxes', array() ) as $panel ) {
356 if ( 'campaign-advanced' != $panel['context'] ) {
357 continue;
358 }
359
360 $id = $panel['id'];
361
362 unset( $panel['id'] );
363
364 $panels[ $id ] = $panel;
365 }
366
367 return $panels;
368 }
369
370 /**
371 * Return the campaign meta keys to save.
372 *
373 * @since 1.6.0
374 *
375 * @return array
376 */
377 public function get_campaign_meta_keys() {
378 $registry = charitable()->campaign_fields();
379 $keys = $registry->get_sanitized_keys( $registry->get_admin_form_fields() );
380
381 /**
382 * Filter the set of meta keys to be saved.
383 *
384 * @since 1.0.0
385 *
386 * @param string[] $keys An array of strings representing meta keys.
387 */
388 return apply_filters( 'charitable_campaign_meta_keys', $keys );
389 }
390
391 /**
392 * Set a field's initial value.
393 *
394 * @since 1.6.0
395 *
396 * @param array $field Field definition.
397 * @param string $key The key of the field.
398 * @return array
399 */
400 private function sanitize_campaign_field( $field, $key ) {
401 $field_name = preg_replace( '/^_campaign_/', '', $key );
402
403 if ( array_key_exists( $key, $_POST ) ) {
404 $field['value'] = $_POST[ $key ];
405 return $field;
406 }
407
408 /* Checkboxes don't need a value set. */
409 if ( 'checkbox' != $field['type'] ) {
410 $field['value'] = array_key_exists( 'default', $field ) ? $field['default'] : '';
411 }
412
413 if ( ! array_key_exists( 'post', $_GET ) ) {
414 return $field;
415 }
416
417 $campaign = charitable_get_campaign( $_GET['post'] );
418
419 if ( in_array( $campaign->post_status, array( 'auto-draft' ) ) ) {
420 return $field;
421 }
422
423 if ( array_key_exists( 'value_callback', $field ) && is_callable( $field['value_callback'] ) ) {
424 $field['value'] = call_user_func( $field['value_callback'], $campaign, $key );
425 return $field;
426 }
427
428 $value = $campaign->get( $field_name );
429
430 if ( ! $value ) {
431 return $field;
432 }
433
434 if ( 'checkbox' == $field['type'] ) {
435 $field['checked'] = $value;
436 } else {
437 $field['value'] = $value;
438 }
439
440 return $field;
441 }
442
443 /**
444 * Return the fields in a particular section.
445 *
446 * @since 1.6.0
447 *
448 * @param string $section The section.
449 * @return array
450 */
451 private function get_section_fields( $section ) {
452 $registry = charitable()->campaign_fields();
453 $fields = $registry->get_admin_form_fields( $section );
454
455 if ( empty( $fields ) ) {
456 return array();
457 }
458
459 $keys = $registry->get_sanitized_keys( $fields, false );
460
461 return array_combine(
462 $keys,
463 array_map( array( $this, 'sanitize_campaign_field' ), wp_list_pluck( $fields, 'admin_form' ), $keys )
464 );
465 }
466
467 /**
468 * Returns whether a particular panel has fields.
469 *
470 * @since 1.6.0
471 *
472 * @param array $panel The panel definition.
473 * @return boolean
474 */
475 private function panel_has_fields( $panel ) {
476 return array_key_exists( 'view', $panel ) || count( $panel['fields'] );
477 }
478 }
479
480 endif;
481