PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12
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 1.7.0.9 1.8.0 All 210 releases
charitable / includes / admin / customizer / class-charitable-customizer.php

class-charitable-customizer.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12, at includes/admin/customizer/class-charitable-customizer.php

440 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class that sets up the WordPress Customizer integration.
4 *
5 * @package Charitable/Classes/Charitable_Customizer
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.2.0
10 * @version 1.6.44
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Customizer' ) ) :
19
20 /**
21 * Sets up the WordPress customizer.
22 *
23 * @since 1.2.0
24 */
25 class Charitable_Customizer {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_Customizer|null
31 */
32 private static $instance = null;
33
34 /**
35 * Create object instance.
36 *
37 * @since 1.2.0
38 */
39 private function __construct() {
40 add_action( 'customize_save_after', array( $this, 'customize_save_after' ) );
41 add_action( 'customize_register', array( $this, 'register_customizer_fields' ) );
42 add_action( 'customize_preview_init', array( $this, 'load_customizer_script' ) );
43 }
44
45 /**
46 * Returns and/or create the single instance of this class.
47 *
48 * @since 1.2.0
49 *
50 * @global WP_Customize_Manager $wp_customize
51 * @return Charitable_Customizer
52 */
53 public static function start() {
54 global $wp_customize;
55
56 if ( ! $wp_customize ) {
57 return;
58 }
59
60 if ( is_null( self::$instance ) ) {
61 self::$instance = new self();
62 }
63
64 return self::$instance;
65 }
66
67 /**
68 * After the customizer has finished saving each of the fields, delete the transient.
69 *
70 * @see customize_save_after hook
71 *
72 * @since 1.2.0
73 *
74 * @return void
75 */
76 public function customize_save_after() {
77 delete_transient( 'charitable_custom_styles' );
78 }
79
80 /**
81 * Plugin customization.
82 *
83 * @return void
84 */
85 public function register_customizer_fields() {
86 $fields = array(
87 'title' => __( 'Charitable', 'charitable' ),
88 'priority' => 1000,
89 'capability' => 'manage_charitable_settings',
90 'sections' => array(
91 'charitable_donation_form' => array(
92 'title' => __( 'Donation Form', 'charitable' ),
93 'priority' => 1020,
94 'settings' => array(
95 'donation_form_display' => array(
96 'setting' => array(
97 'transport' => 'refresh',
98 'default' => 'separate_page',
99 'sanitize_callback' => array( $this, 'sanitize_donation_form_display_option' ),
100 ),
101 'control' => array(
102 'type' => 'select',
103 'label' => __( 'How do you want a campaign\'s donation form to show?', 'charitable' ),
104 'priority' => 1022,
105 'choices' => array(
106 'separate_page' => __( 'Show on a Separate Page', 'charitable' ),
107 'same_page' => __( 'Show on the Same Page', 'charitable' ),
108 'modal' => __( 'Reveal in a Modal', 'charitable' ),
109 ),
110 ),
111 ),
112 'donation_form_minimal_fields' => array(
113 'setting' => array(
114 'transport' => 'refresh',
115 'default' => 0,
116 ),
117 'control' => array(
118 'type' => 'radio',
119 'label' => __( 'Only Show Required Fields', 'charitable' ),
120 'priority' => 1024,
121 'choices' => array(
122 1 => __( 'Yes', 'charitable' ),
123 0 => __( 'No', 'charitable' ),
124 ),
125 ),
126 ),
127 ),
128 ),
129 'charitable_privacy' => array(
130 'title' => __( 'User Privacy', 'charitable' ),
131 'priority' => 1030,
132 'settings' => array(
133 'privacy_policy_page' => array(
134 'setting' => array(
135 'transport' => 'refresh',
136 'default' => '',
137 ),
138 'control' => array(
139 'type' => 'dropdown-pages',
140 'label' => __( 'Privacy policy page', 'charitable' ),
141 'priority' => 1032,
142 'allow_addition' => true,
143 ),
144 ),
145 'privacy_policy' => array(
146 'setting' => array(
147 'transport' => 'refresh',
148 'default' => __( 'Your personal data will be used to process your donation, support your experience throughout this website, and for other purposes described in our [privacy_policy].', 'charitable' ),
149 'sanitize_callback' => function_exists( 'sanitize_textarea_field' ) ? 'sanitize_textarea_field' : 'sanitize_text_field',
150 ),
151 'control' => array(
152 'type' => 'textarea',
153 'label' => __( 'Privacy policy', 'charitable' ),
154 'description' => __( 'Information about your website privacy policy for people to see when they donate, register an account, or manage their profile. Leave empty to disable.', 'charitable' ),
155 'priority' => 1034,
156 ),
157 ),
158 'contact_consent' => array(
159 'setting' => array(
160 'transport' => 'refresh',
161 'default' => 0,
162 ),
163 'control' => array(
164 'type' => 'radio',
165 'label' => __( 'Contact consent field', 'charitable' ),
166 'description' => __( 'Display a checkbox asking people for their consent to being contacted when they donate, register an account, or manage their profile.', 'charitable' ),
167 'priority' => 1036,
168 'choices' => array(
169 1 => __( 'Yes', 'charitable' ),
170 0 => __( 'No', 'charitable' ),
171 ),
172 ),
173 ),
174 'contact_consent_label' => array(
175 'setting' => array(
176 'transport' => 'refresh',
177 'default' => __( 'Yes, I am happy for you to contact me via email or phone.', 'charitable' ),
178 'sanitize_callback' => function_exists( 'sanitize_textarea_field' ) ? 'sanitize_textarea_field' : 'sanitize_text_field',
179 ),
180 'control' => array(
181 'type' => 'textarea',
182 'label' => __( 'Contact consent label', 'charitable' ),
183 'priority' => 1038,
184 'description' => __( 'A short statement describing how you would like to contact people.', 'charitable' ),
185 ),
186 ),
187 ),
188 ),
189 'charitable_terms_and_conditions' => array(
190 'title' => __( 'Terms & Conditions', 'charitable' ),
191 'priority' => 1040,
192 'settings' => array(
193 'terms_conditions_page' => array(
194 'setting' => array(
195 'transport' => 'refresh',
196 'default' => '',
197 ),
198 'control' => array(
199 'type' => 'dropdown-pages',
200 'label' => __( 'Terms and conditions page', 'charitable' ),
201 'priority' => 1042,
202 'allow_addition' => true,
203 ),
204 ),
205 'terms_conditions' => array(
206 'setting' => array(
207 'transport' => 'refresh',
208 'default' => __( 'I have read and agree to the website [terms].', 'charitable' ),
209 'sanitize_callback' => 'sanitize_text_field',
210 ),
211 'control' => array(
212 'type' => 'textarea',
213 'label' => __( 'Terms and conditions', 'charitable' ),
214 'description' => __( 'Display a checkbox asking people to accept the website terms and conditions when they make a donation or register an account. Leave empty to disable.', 'charitable' ),
215 'priority' => 1044,
216 ),
217 ),
218 ),
219 ),
220 ),
221 );
222
223 /* Remove the contact consent fields if the database upgrade hasn't been completed yet. */
224 if ( ! Charitable_Upgrade::get_instance()->upgrade_has_been_completed( 'upgrade_donor_tables' ) ) {
225 unset(
226 $fields['sections']['charitable_donation_form']['contact_consent'],
227 $fields['sections']['charitable_donation_form']['contact_consent_label']
228 );
229 }
230
231 /**
232 * Set whether to add custom styles.
233 *
234 * @since 1.2.3
235 *
236 * @param boolean Whether to add styles.
237 */
238 if ( apply_filters( 'charitable_add_custom_styles', true ) ) {
239
240 /**
241 * Filter the default highlight colour.
242 *
243 * @since 1.2.0
244 *
245 * @param string $colour_code HEX color code.
246 */
247 $highlight_colour = apply_filters( 'charitable_default_highlight_colour', '#f89d35' );
248
249 $fields['sections']['charitable_design'] = array(
250 'title' => __( 'Design Options', 'charitable' ),
251 'priority' => 1010,
252 'settings' => array(
253 'highlight_colour' => array(
254 'setting' => array(
255 'transport' => 'postMessage',
256 'default' => $highlight_colour,
257 'sanitize_callback' => 'sanitize_hex_color',
258 ),
259 'control' => array(
260 'control_type' => 'WP_Customize_Color_Control',
261 'priority' => 1110,
262 'label' => __( 'Highlight Color', 'charitable' ),
263 ),
264 ),
265 ),
266 );
267 }//end if
268
269 /**
270 * Filter the customizer fields.
271 *
272 * @since 1.2.0
273 *
274 * @param array $fields The array of customizer fields.
275 */
276 $fields = apply_filters( 'charitable_customizer_fields', $fields );
277
278 $this->add_panel( 'charitable', $fields );
279 }
280
281 /**
282 * Make sure the donation form display option is valid.
283 *
284 * @since 1.2.0
285 *
286 * @param string $option Submitted option.
287 * @return string
288 */
289 public function sanitize_donation_form_display_option( $option ) {
290 if ( ! in_array( $option, array( 'separate_page', 'same_page', 'modal' ) ) ) {
291 $option = 'separate_page';
292 }
293
294 return $option;
295 }
296
297 /**
298 * Adds a panel.
299 *
300 * @since 1.2.0
301 *
302 * @global WP_Customize_Manager $wp_customize
303 * @param string $panel_id Panel identifier.
304 * @param array $panel Panel definition.
305 * @return void
306 */
307 private function add_panel( $panel_id, $panel ) {
308 global $wp_customize;
309
310 if ( empty( $panel ) ) {
311 return;
312 }
313
314 $wp_customize->add_panel( $panel_id, array(
315 'title' => $panel['title'],
316 'priority' => $panel['priority'],
317 ) );
318
319 $this->add_panel_sections( $panel_id, $panel['sections'] );
320 }
321
322 /**
323 * Adds sections to a panel.
324 *
325 * @since 1.2.0
326 *
327 * @param string $panel_id Panel identifier.
328 * @param array $sections Array of sections inside panel.
329 * @return void
330 */
331 private function add_panel_sections( $panel_id, $sections ) {
332 if ( empty( $sections ) ) {
333 return;
334 }
335
336 foreach ( $sections as $section_id => $section ) {
337 $this->add_section( $section_id, $section, $panel_id );
338 }
339 }
340
341 /**
342 * Adds section & settings
343 *
344 * @since 1.2.0
345 *
346 * @global WP_Customize_Manager $wp_customize
347 * @param string $section_id Section identifier.
348 * @param array $section Section definition.
349 * @param string $panel Panel identifier.
350 * @return void
351 */
352 private function add_section( $section_id, $section, $panel ) {
353 global $wp_customize;
354
355 if ( empty( $section ) ) {
356 return;
357 }
358
359 $settings = $section['settings'];
360
361 unset( $section['settings'] );
362
363 $section['panel'] = $panel;
364
365 $wp_customize->add_section( $section_id, $section );
366
367 $this->add_section_settings( $section_id, $settings );
368 }
369
370
371 /**
372 * Adds settings to a given section.
373 *
374 * @since 1.2.0
375 *
376 * @global WP_Customize_Manager $wp_customize
377 * @param string $section_id Section identifier.
378 * @param array $settings Section definition.
379 * @return void
380 */
381 private function add_section_settings( $section_id, $settings ) {
382 global $wp_customize;
383
384 if ( empty( $settings ) ) {
385 return;
386 }
387
388 foreach ( $settings as $setting_id => $setting ) {
389 if ( ! isset( $setting['setting']['type'] ) ) {
390 $setting['setting']['type'] = 'option';
391 }
392
393 $setting_id = "charitable_settings[$setting_id]";
394
395 $wp_customize->add_setting( $setting_id, $setting['setting'] );
396
397 $setting_control = $setting['control'];
398 $setting_control['section'] = $section_id;
399
400 if ( isset( $setting_control['control_type'] ) ) {
401
402 $setting_control_type = $setting_control['control_type'];
403
404 unset( $setting_control['control_type'] );
405
406 $wp_customize->add_control( new $setting_control_type( $wp_customize, $setting_id, $setting_control ) );
407
408 } else {
409
410 $wp_customize->add_control( $setting_id, $setting_control );
411
412 }
413 }
414 }
415
416 /**
417 * Load the theme-customizer.js file.
418 *
419 * @since 1.2.0
420 *
421 * @return void
422 */
423 public function load_customizer_script() {
424
425 $min = charitable_get_min_suffix();
426
427 wp_register_script(
428 'charitable-customizer',
429 charitable()->get_path( 'assets', false ) . 'js/charitable-customizer' . $min . '.js',
430 array( 'jquery', 'customize-preview' ),
431 '1.2.0-beta5',
432 true
433 );
434
435 wp_enqueue_script( 'charitable-customizer' );
436 }
437 }
438
439 endif;
440