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

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