| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class that sets up the WordPress Customizer integration. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Customizer |
| 6 |
* @version 1.2.0 |
| 7 |
* @author Eric Daams |
| 8 |
* @copyright Copyright (c) 2015, Studio 164a |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 13 |
|
| 14 |
if ( ! class_exists( 'Charitable_Customizer' ) ) : |
| 15 |
|
| 16 |
/** |
| 17 |
* Sets up the Wordpress customizer |
| 18 |
* |
| 19 |
* @since 1.2.0 |
| 20 |
*/ |
| 21 |
class Charitable_Customizer { |
| 22 |
|
| 23 |
/** |
| 24 |
* The single instance of this class. |
| 25 |
* |
| 26 |
* @var Charitable_Customizer|null |
| 27 |
* @access private |
| 28 |
* @static |
| 29 |
*/ |
| 30 |
private static $instance = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Create object instance. |
| 34 |
* |
| 35 |
* @access private |
| 36 |
* @since 1.2.0 |
| 37 |
*/ |
| 38 |
private function __construct() { |
| 39 |
add_action('customize_save_after', array( $this, 'customize_save_after' ) ); |
| 40 |
add_action('customize_register', array( $this, 'register_customizer_fields' ) ); |
| 41 |
add_action('customize_preview_init', array( $this, 'load_customizer_script' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns and/or create the single instance of this class. |
| 46 |
* |
| 47 |
* @global WP_Customize_Manager $wp_customize |
| 48 |
* @return Charitable_Customizer |
| 49 |
* @access public |
| 50 |
* @since 1.2.0 |
| 51 |
*/ |
| 52 |
public static function start() { |
| 53 |
global $wp_customize; |
| 54 |
|
| 55 |
if ( ! $wp_customize ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( is_null( self::$instance ) ) { |
| 60 |
self::$instance = new Charitable_Customizer(); |
| 61 |
} |
| 62 |
|
| 63 |
return self::$instance; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* After the customizer has finished saving each of the fields, delete the transient. |
| 68 |
* |
| 69 |
* @see customize_save_after hook |
| 70 |
* @return void |
| 71 |
* @access public |
| 72 |
* @since 1.2.0 |
| 73 |
*/ |
| 74 |
public function customize_save_after() { |
| 75 |
delete_transient( 'charitable_custom_styles' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Theme customization. |
| 80 |
* |
| 81 |
* @param WP_Customize_Manager $wp_customize |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function register_customizer_fields($wp_customize) { |
| 85 |
$fields = array( |
| 86 |
'title' => __( 'Charitable', 'charitable' ), |
| 87 |
'priority' => 1000, |
| 88 |
'capability'=> 'manage_charitable_settings', |
| 89 |
'sections' => array( |
| 90 |
'charitable_donation_form' => array( |
| 91 |
'title' => __( 'Donation Form', 'charitable' ), |
| 92 |
'priority' => 1020, |
| 93 |
'settings' => array( |
| 94 |
'donation_form_display' => array( |
| 95 |
'setting' => array( |
| 96 |
'transport' => 'refresh', |
| 97 |
'default' => 'separate_page', |
| 98 |
'sanitize_callback' => array( $this, 'sanitize_donation_form_display_option' ) |
| 99 |
), |
| 100 |
'control' => array( |
| 101 |
'type' => 'select', |
| 102 |
'label' => __( 'How do you want a campaign\'s donation form to show?', 'charitable' ), |
| 103 |
'priority' => 1022, |
| 104 |
'choices' => array( |
| 105 |
'separate_page' => __( 'Show on a Separate Page', 'charitable' ), |
| 106 |
'same_page' => __( 'Show on the Same Page', 'charitable' ), |
| 107 |
'modal' => __( 'Reveal in a Modal', 'charitable' ) |
| 108 |
) |
| 109 |
) |
| 110 |
), |
| 111 |
'donation_form_minimal_fields' => array( |
| 112 |
'setting' => array( |
| 113 |
'transport' => 'refresh', |
| 114 |
'default' => 0 |
| 115 |
), |
| 116 |
'control' => array( |
| 117 |
'type' => 'radio', |
| 118 |
'label' => __( 'Only show required fields', 'charitable' ), |
| 119 |
'priority' => 1024, |
| 120 |
'choices' => array( |
| 121 |
1 => __( 'Yes', 'charitable' ), |
| 122 |
0 => __( 'No', 'charitable' ) |
| 123 |
) |
| 124 |
) |
| 125 |
) |
| 126 |
) |
| 127 |
) |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
if ( apply_filters( 'charitable_add_custom_styles', true ) ) { |
| 132 |
$highlight_colour = apply_filters( 'charitable_default_highlight_colour', '#f89d35' ); |
| 133 |
|
| 134 |
$fields[ 'sections' ][ 'charitable_design' ] = array( |
| 135 |
'title' => __( 'Design Options', 'charitable' ), |
| 136 |
'priority' => 1010, |
| 137 |
'settings' => array( |
| 138 |
'highlight_colour' => array( |
| 139 |
'setting' => array( |
| 140 |
'transport' => 'postMessage', |
| 141 |
'default' => $highlight_colour, |
| 142 |
'sanitize_callback' => 'sanitize_hex_color' |
| 143 |
), |
| 144 |
'control' => array( |
| 145 |
'control_type' => 'WP_Customize_Color_Control', |
| 146 |
'priority' => 1110, |
| 147 |
'label' => __( 'Highlight Colour', 'charitable' ) |
| 148 |
) |
| 149 |
) |
| 150 |
) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
$fields = apply_filters( 'charitable_customizer_fields', $fields ); |
| 155 |
|
| 156 |
$this->add_panel( 'charitable', $fields ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Make sure the donation form display option is valid. |
| 161 |
* |
| 162 |
* @return boolean |
| 163 |
* @access public |
| 164 |
* @since 1.2.0 |
| 165 |
*/ |
| 166 |
public function sanitize_donation_form_display_option( $option ) { |
| 167 |
if ( ! in_array( $option, array( 'separate_page', 'same_page', 'modal' ) ) ) { |
| 168 |
$option = 'separate_page'; |
| 169 |
} |
| 170 |
|
| 171 |
return $option; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Adds a panel. |
| 176 |
* |
| 177 |
* @param string $panel_id |
| 178 |
* @param array $panel |
| 179 |
* @return void |
| 180 |
* @access private |
| 181 |
* @since 1.2.0 |
| 182 |
*/ |
| 183 |
private function add_panel( $panel_id, $panel ) { |
| 184 |
global $wp_customize; |
| 185 |
|
| 186 |
if ( empty( $panel ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
$wp_customize->add_panel( $panel_id, array( |
| 191 |
'title' => $panel[ 'title' ], |
| 192 |
'priority' => $panel[ 'priority' ] |
| 193 |
) ); |
| 194 |
|
| 195 |
$this->add_panel_sections( $panel_id, $panel[ 'sections' ] ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Adds sections to a panel. |
| 200 |
* |
| 201 |
* @param string $panel_id |
| 202 |
* @param array $sections |
| 203 |
* @return void |
| 204 |
* @access private |
| 205 |
* @since 1.2.0 |
| 206 |
*/ |
| 207 |
private function add_panel_sections( $panel_id, $sections ) { |
| 208 |
global $wp_customize; |
| 209 |
|
| 210 |
if ( empty( $sections ) ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
foreach ( $sections as $section_id => $section ) { |
| 215 |
$this->add_section( $section_id, $section, $panel_id ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Adds section & settings |
| 221 |
* |
| 222 |
* @param string $section_id |
| 223 |
* @param array $section |
| 224 |
* @param string $panel |
| 225 |
* @return void |
| 226 |
* @access private |
| 227 |
* @since 1.2.0 |
| 228 |
*/ |
| 229 |
private function add_section( $section_id, $section, $panel ) { |
| 230 |
global $wp_customize; |
| 231 |
|
| 232 |
if ( empty( $section ) ) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
$settings = $section[ 'settings' ]; |
| 237 |
|
| 238 |
unset( $section[ 'settings' ] ); |
| 239 |
|
| 240 |
$section[ 'panel' ] = $panel; |
| 241 |
|
| 242 |
$wp_customize->add_section( $section_id, $section ); |
| 243 |
|
| 244 |
$this->add_section_settings( $section_id, $settings ); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* Adds settings to a given section. |
| 250 |
* |
| 251 |
* @param string $section_id |
| 252 |
* @param array $settings |
| 253 |
* @return void |
| 254 |
* @access private |
| 255 |
* @since 1.2.0 |
| 256 |
*/ |
| 257 |
private function add_section_settings( $section_id, $settings ) { |
| 258 |
global $wp_customize; |
| 259 |
|
| 260 |
if ( empty( $settings ) ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
foreach ( $settings as $setting_id => $setting ) { |
| 265 |
if ( ! isset( $setting[ 'setting' ][ 'type' ] ) ) { |
| 266 |
$setting[ 'setting' ][ 'type' ] = 'option'; |
| 267 |
} |
| 268 |
|
| 269 |
$setting_id = "charitable_settings[$setting_id]"; |
| 270 |
|
| 271 |
$wp_customize->add_setting( $setting_id, $setting[ 'setting' ] ); |
| 272 |
|
| 273 |
$setting_control = $setting[ 'control' ]; |
| 274 |
$setting_control[ 'section' ] = $section_id; |
| 275 |
// $setting_control[ 'type' ] = $setting[ 'control' ][ 'type' ]; |
| 276 |
|
| 277 |
if ( isset( $setting_control[ 'control_type' ] ) ) { |
| 278 |
|
| 279 |
$setting_control_type = $setting_control[ 'control_type' ]; |
| 280 |
|
| 281 |
unset( $setting_control[ 'control_type'] ); |
| 282 |
|
| 283 |
$wp_customize->add_control( new $setting_control_type( $wp_customize, $setting_id, $setting_control ) ); |
| 284 |
|
| 285 |
} |
| 286 |
else { |
| 287 |
|
| 288 |
$wp_customize->add_control( $setting_id, $setting_control ); |
| 289 |
|
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Load the theme-customizer.js file. |
| 296 |
* |
| 297 |
* @return void |
| 298 |
* @access public |
| 299 |
* @since 1.2.0 |
| 300 |
*/ |
| 301 |
public function load_customizer_script() { |
| 302 |
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 303 |
wp_register_script( 'charitable-customizer', charitable()->get_path( 'assets', false ) . 'js/charitable-customizer' . $suffix . '.js', array( 'jquery', 'customize-preview' ), '1.2.0-beta5', true ); |
| 304 |
wp_enqueue_script( 'charitable-customizer' ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
endif; // End class_exists check |