PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
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.11, at includes/admin/campaigns/class-charitable-campaign-meta-boxes.php

493 lines 12.5 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 if ( $key === '_campaign_suggested_donations_default' && isset( $value[0] ) && isset( $submitted['_campaign_suggested_donations'][ $value[0] ]['amount'] ) ) {
213 $value = ( false !== $submitted['_campaign_suggested_donations'][ $value[0] ]['amount'] ) ? array( $submitted['_campaign_suggested_donations'][ $value[0] ]['amount'] ) : false;
214 }
215
216 /**
217 * Filter this meta value. The filter hook is
218 * charitable_sanitize_campaign_meta{$key}. For example,
219 * for _campaign_end_date the filter hook will be:
220 * charitable_sanitize_campaign_meta_campaign_end_date
221 *
222 * @since 1.4.12
223 *
224 * @param mixed $value The submitted value, or false.
225 * @param array $submitted All of the submitted values.
226 * @param int $campaign_id The campaign ID.
227 */
228 $data[ $key ] = apply_filters( 'charitable_sanitize_campaign_meta' . $key, $value, $submitted, $campaign_id );
229 }
230
231
232 $processor = new Charitable_Campaign_Processor( $data, $meta_keys );
233 $processor->save_meta();
234
235 /**
236 * Do something with the posted data.
237 *
238 * @since 1.0.0
239 *
240 * @param WP_Post $post An instance of `WP_Post`.
241 */
242 do_action( 'charitable_campaign_save', $post );
243 }
244
245 /**
246 * Set default post content when the extended description is left empty.
247 *
248 * @since 1.4.0
249 *
250 * @param array $data Submitted data.
251 * @return array
252 */
253 public function set_default_post_content( $data ) {
254 if ( Charitable::CAMPAIGN_POST_TYPE != $data['post_type'] ) {
255 return $data;
256 }
257
258 if ( 0 === strlen( $data['post_content'] ) ) {
259 $data['post_content'] = '<!-- Code is poetry -->';
260 }
261
262 return $data;
263 }
264
265 /**
266 * Sets the placeholder text of the campaign title field.
267 *
268 * @since 1.0.0
269 *
270 * @param string $placeholder Placeholder text.
271 * @param WP_Post $post Current Post object.
272 * @return string
273 */
274 public function campaign_enter_title( $placeholder, WP_Post $post ) {
275 if ( 'campaign' == $post->post_type ) {
276 $placeholder = __( 'Enter campaign title', 'charitable' );
277 }
278
279 return $placeholder;
280 }
281
282 /**
283 * Change messages when a post type is updated.
284 *
285 * @global WP_Post $post
286 * @global int $post_ID
287 * @param array $messages Messages to display.
288 * @return array
289 */
290 public function post_messages( $messages ) {
291 global $post, $post_ID;
292
293 $messages[ Charitable::CAMPAIGN_POST_TYPE ] = array(
294 // Unused. Messages start at index 1.
295 0 => '',
296 1 => sprintf(
297 __( 'Campaign updated. <a href="%s">View Campaign</a>', 'charitable' ),
298 esc_url( get_permalink( $post_ID ) )
299 ),
300 2 => __( 'Custom field updated.', 'charitable' ),
301 3 => __( 'Custom field deleted.', 'charitable' ),
302 4 => __( 'Campaign updated.', 'charitable' ),
303 5 => isset( $_GET['revision'] )
304 ? sprintf( __( 'Campaign restored to revision from %s', 'charitable' ), wp_post_revision_title( (int) $_GET['revision'], false ) )
305 : false,
306 6 => sprintf(
307 __( 'Campaign published. <a href="%s">View Campaign</a>', 'charitable' ),
308 esc_url( get_permalink( $post_ID ) )
309 ),
310 7 => __( 'Campaign saved.', 'charitable' ),
311 8 => sprintf(
312 __( 'Campaign submitted. <a target="_blank" href="%s">Preview Campaign</a>', 'charitable' ),
313 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
314 ),
315 9 => sprintf(
316 __( 'Campaign scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Campaign</a>', 'charitable' ),
317 date_i18n( __( 'M j, Y @ G:i', 'charitable' ),strtotime( $post->post_date ) ),
318 esc_url( get_permalink( $post_ID ) )
319 ),
320 10 => sprintf(
321 __( 'Campaign draft updated. <a target="_blank" href="%s">Preview Campaign</a>', 'charitable' ),
322 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
323 ),
324 );
325
326 return $messages;
327 }
328
329 /**
330 * Return the set of meta boxes to be shown in the "campaign-top" section of the campaign editor.
331 *
332 * @since 1.6.0
333 *
334 * @return array
335 */
336 private function get_campaign_top_meta_boxes() {
337 $boxes = $this->get_section_fields( 'campaign-top' );
338
339 foreach ( $boxes as $key => $box ) {
340 $box['id'] = str_replace( '_', '-', substr( $key, 1 ) );
341 $box['title'] = $box['label'];
342 $box['context'] = 'campaign-top';
343 $box['priority'] = 'high';
344 $boxes[ $key ] = $box;
345 }
346
347 return $boxes;
348 }
349
350 /**
351 * Add panels for any meta boxes previously added to the campaign-advanced section
352 * using the `charitable_campaign_meta_boxes` filter.
353 *
354 * @since 1.6.0
355 *
356 * @param array $panels Core registered panels.
357 * @return array
358 */
359 private function add_legacy_meta_boxes( $panels ) {
360 foreach ( apply_filters( 'charitable_campaign_meta_boxes', array() ) as $panel ) {
361 if ( 'campaign-advanced' != $panel['context'] ) {
362 continue;
363 }
364
365 $id = $panel['id'];
366
367 unset( $panel['id'] );
368
369 $panels[ $id ] = $panel;
370 }
371
372 return $panels;
373 }
374
375 /**
376 * Return the campaign meta keys to save.
377 *
378 * @since 1.6.0
379 *
380 * @return array
381 */
382 public function get_campaign_meta_keys() {
383 $registry = charitable()->campaign_fields();
384 $keys = $registry->get_sanitized_keys( $registry->get_admin_form_fields() );
385
386 $keys[] = '_campaign_suggested_donations_default';
387 $keys[] = '_campaign_hide_amount_donated';
388 $keys[] = '_campaign_hide_number_of_donors';
389 $keys[] = '_campaign_hide_percent_raised';
390 $keys[] = '_campaign_hide_time_remaining';
391 $keys[] = '_campaign_donate_button_text';
392
393 /**
394 * Filter the set of meta keys to be saved.
395 *
396 * @since 1.0.0
397 *
398 * @param string[] $keys An array of strings representing meta keys.
399 */
400 return apply_filters( 'charitable_campaign_meta_keys', $keys );
401 }
402
403 /**
404 * Set a field's initial value.
405 *
406 * @since 1.6.0
407 *
408 * @param array $field Field definition.
409 * @param string $key The key of the field.
410 * @return array
411 */
412 private function sanitize_campaign_field( $field, $key ) {
413 $field_name = preg_replace( '/^_campaign_/', '', $key );
414
415 if ( array_key_exists( $key, $_POST ) ) {
416 $field['value'] = $_POST[ $key ];
417 return $field;
418 }
419
420 /* Checkboxes don't need a value set. */
421 if ( 'checkbox' != $field['type'] ) {
422 $field['value'] = array_key_exists( 'default', $field ) ? $field['default'] : '';
423 }
424
425 if ( ! array_key_exists( 'post', $_GET ) ) {
426 return $field;
427 }
428
429 $campaign = charitable_get_campaign( $_GET['post'] );
430
431 if ( in_array( $campaign->post_status, array( 'auto-draft' ) ) ) {
432 return $field;
433 }
434
435 if ( array_key_exists( 'value_callback', $field ) && is_callable( $field['value_callback'] ) ) {
436 $field['value'] = call_user_func( $field['value_callback'], $campaign, $key );
437 return $field;
438 }
439
440 $value = $campaign->get( $field_name );
441
442 if ( ! $value ) {
443 return $field;
444 }
445
446 if ( 'checkbox' == $field['type'] ) {
447 $field['checked'] = $value;
448 } else {
449 $field['value'] = $value;
450 }
451
452 return $field;
453 }
454
455 /**
456 * Return the fields in a particular section.
457 *
458 * @since 1.6.0
459 *
460 * @param string $section The section.
461 * @return array
462 */
463 private function get_section_fields( $section ) {
464 $registry = charitable()->campaign_fields();
465 $fields = $registry->get_admin_form_fields( $section );
466
467 if ( empty( $fields ) ) {
468 return array();
469 }
470
471 $keys = $registry->get_sanitized_keys( $fields, false );
472
473 return array_combine(
474 $keys,
475 array_map( array( $this, 'sanitize_campaign_field' ), wp_list_pluck( $fields, 'admin_form' ), $keys )
476 );
477 }
478
479 /**
480 * Returns whether a particular panel has fields.
481 *
482 * @since 1.6.0
483 *
484 * @param array $panel The panel definition.
485 * @return boolean
486 */
487 private function panel_has_fields( $panel ) {
488 return array_key_exists( 'view', $panel ) || count( $panel['fields'] );
489 }
490 }
491
492 endif;
493