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