| 1 |
<?php |
| 2 |
if ( ! class_exists( 'LoftLoader_Customize' ) ) { |
| 3 |
/** |
| 4 |
* Load the Loftloader lite customize related functions |
| 5 |
* |
| 6 |
* @author Loft.Ocean |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
class LoftLoader_Customize { |
| 10 |
public function __construct() { |
| 11 |
$this->load_default_settings(); |
| 12 |
if ( loftloader_is_customize() ) { |
| 13 |
$this->load_customize_controls(); |
| 14 |
add_action( 'customize_controls_init', array( $this, 'remove_sections' ), 1000 ); |
| 15 |
add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_scripts' ), 9999 ); |
| 16 |
add_action( 'customize_preview_init', array( $this, 'preview_scripts' ) ); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
private function load_default_settings() { |
| 21 |
require_once LOFTLOADER_ROOT . 'inc/configs/default-settings.php'; |
| 22 |
} |
| 23 |
|
| 24 |
public function load_customize_controls() { |
| 25 |
$config_dir = LOFTLOADER_ROOT . 'inc/configs/'; |
| 26 |
require_once $config_dir . 'customize-main.php'; |
| 27 |
require_once $config_dir . 'customize-range.php'; |
| 28 |
require_once $config_dir . 'customize-background.php'; |
| 29 |
require_once $config_dir . 'customize-loader.php'; |
| 30 |
require_once $config_dir . 'customize-promo.php'; |
| 31 |
require_once $config_dir . 'customize-more.php'; |
| 32 |
require_once $config_dir . 'customize-advanced.php'; |
| 33 |
} |
| 34 |
|
| 35 |
public function remove_sections() { |
| 36 |
global $wp_customize; |
| 37 |
foreach($wp_customize->containers() as $id => $container){ |
| 38 |
if ( $container instanceof WP_Customize_Panel ) { |
| 39 |
( strpos( $id, 'loftloader_' ) === false ) ? $wp_customize->remove_panel( $id ) : ''; |
| 40 |
} else if ( $container instanceof WP_Customize_Section){ |
| 41 |
( strpos( $id, 'loftloader_' ) === false ) ? $wp_customize->remove_section( $id ) : ''; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public function customize_scripts() { |
| 47 |
global $wp_scripts, $wp_styles; |
| 48 |
$js_url = LOFTLOADER_URI . 'assets/js/customize.min.js'; |
| 49 |
$js_dep = array('jquery', 'wp-color-picker', 'jquery-ui-slider', 'customize-controls', 'media-editor' ); |
| 50 |
$ui_css = LOFTLOADER_URI . 'assets/css/jquery-ui.css'; |
| 51 |
$loader_css = LOFTLOADER_URI . 'assets/css/loftloader-settings.min.css'; |
| 52 |
|
| 53 |
wp_register_script( 'loftloader-lite-customize', $js_url, $js_dep, LOFTLOADER_ASSET_VERSION ); |
| 54 |
wp_localize_script( 'loftloader-lite-customize', 'loftloader_lite_i18n', array( 'name' => esc_html__( 'LoftLoader Lite', 'loftloader' ) ) ); // Change the site title in string "You are customizing ..." |
| 55 |
wp_enqueue_script( 'loftloader-lite-customize' ); |
| 56 |
|
| 57 |
wp_enqueue_style( 'loftloader-lite-ui', $ui_css, array(), LOFTLOADER_ASSET_VERSION ); |
| 58 |
wp_enqueue_style( 'loftloader-lite-customize', $loader_css, array(), LOFTLOADER_ASSET_VERSION ); |
| 59 |
|
| 60 |
foreach ( $wp_scripts->registered as $h => $o ) { |
| 61 |
if ( strpos($o->src, 'wp-content/themes') !== false ) { |
| 62 |
wp_dequeue_script( $h ); |
| 63 |
} |
| 64 |
}; |
| 65 |
foreach ( $wp_styles->registered as $h => $o ) { |
| 66 |
if ( strpos($o->src, 'wp-content/themes') !== false ) { |
| 67 |
wp_dequeue_style( $h ); |
| 68 |
} |
| 69 |
}; |
| 70 |
} |
| 71 |
|
| 72 |
public function preview_scripts() { |
| 73 |
$js_url = LOFTLOADER_URI . 'assets/js/preview.min.js'; |
| 74 |
wp_register_script( 'loftloader-lite-preview', $js_url, array( 'jquery', 'customize-preview' ), LOFTLOADER_ASSET_VERSION, true ); |
| 75 |
wp_localize_script( 'loftloader-lite-preview', 'loftloader_lite', array( 'preview' => 'on' ) ); |
| 76 |
wp_enqueue_script( 'loftloader-lite-preview' ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
new LoftLoader_Customize(); |
| 81 |
} |
| 82 |
|
| 83 |
if ( class_exists( 'WP_Customize_Setting' ) ) { |
| 84 |
/** |
| 85 |
* LoftLoader related customization api classes |
| 86 |
* |
| 87 |
* @since 2.0.0 |
| 88 |
*/ |
| 89 |
|
| 90 |
// LoftLoader base section class, changed the json function to modify the customize action text |
| 91 |
class LoftLoader_Customize_Section extends WP_Customize_Section { |
| 92 |
public function json() { |
| 93 |
$array = parent::json(); |
| 94 |
$array['customizeAction'] = esc_html__( 'Setting', 'loftloader' ); |
| 95 |
return $array; |
| 96 |
} |
| 97 |
/** |
| 98 |
* render function for LoftLoader Switch section |
| 99 |
*/ |
| 100 |
protected function render() { |
| 101 |
if ( 'loftloader_switch' === $this->type ) : |
| 102 |
$switch = $this->manager->get_setting('loftloader_main_switch')->value(); |
| 103 |
$classes = 'accordion-section control-section control-section-' . $this->type; ?> |
| 104 |
|
| 105 |
<li |
| 106 |
id="accordion-section-<?php echo esc_attr( $this->id ); ?>" |
| 107 |
class="accordion-section control-section control-section-<?php echo esc_attr( $this->type ); ?>" |
| 108 |
> |
| 109 |
<h3 class="accordion-section-title" tabindex="0"> |
| 110 |
<?php echo esc_html( $this->title ); ?> |
| 111 |
<span class="screen-reader-text"><?php esc_html_e( 'Press return or enter to open this section', 'loftloader' ); ?></span> |
| 112 |
<input type="checkbox" name="loftloader-main-switch" value="on" <?php checked( $switch, 'on' ); ?> /> |
| 113 |
</h3> |
| 114 |
<ul class="accordion-section-content"> |
| 115 |
<li class="customize-section-description-container"> |
| 116 |
<div class="customize-section-title"> |
| 117 |
<button class="customize-section-back" tabindex="-1"> |
| 118 |
<span class="screen-reader-text"><?php esc_html_e( 'Back', 'loftloader' ); ?></span> |
| 119 |
</button> |
| 120 |
<h3> |
| 121 |
<span class="customize-action"><?php esc_html_e( 'Setting', 'loftloader' ); ?></span> |
| 122 |
<?php echo wp_kses_post( $this->title ); ?> |
| 123 |
</h3> |
| 124 |
</div> <?php |
| 125 |
if ( ! empty( $this->description ) ) : ?> |
| 126 |
<div class="description customize-section-description"><?php echo wp_kses_post( $this->description ); ?></div> <?php |
| 127 |
endif; ?> |
| 128 |
</li> |
| 129 |
</ul> |
| 130 |
</li> <?php |
| 131 |
else : |
| 132 |
parent::render(); |
| 133 |
endif; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// LoftLoader base customize control class: add class properties as displaying dependency. |
| 138 |
class LoftLoader_Customize_Control extends WP_Customize_Control { |
| 139 |
public $filter = false; |
| 140 |
public $text = ''; |
| 141 |
public $parent_setting_id = ''; |
| 142 |
public $show_filter = array(); |
| 143 |
public $img = ''; |
| 144 |
public $href = ''; |
| 145 |
public $note_below = ''; |
| 146 |
public function active_callback() { |
| 147 |
if ( $this->filter && ( $this->manager->get_setting($this->parent_setting_id ) instanceof WP_Customize_Setting ) && ! empty( $this->show_filter ) ) { |
| 148 |
$parent_setting_value = $this->manager->get_setting( $this->parent_setting_id )->value(); |
| 149 |
return in_array( $parent_setting_value, $this->show_filter ) ? true : false; |
| 150 |
} |
| 151 |
return true; |
| 152 |
} |
| 153 |
public function render_content() { |
| 154 |
switch ( $this->type ) { |
| 155 |
case 'loftloader-ad': |
| 156 |
if ( ! empty( $this->label ) ) : ?> |
| 157 |
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> <?php |
| 158 |
endif; |
| 159 |
if ( ! empty( $this->img ) ) : ?> |
| 160 |
<a href="<?php echo esc_url( $this->href ); ?>" target="_blank"> |
| 161 |
<img src="<?php echo esc_url( $this->img ); ?>" > |
| 162 |
</a> <?php |
| 163 |
endif; ?> |
| 164 |
<div class="customize-control-notifications-container"></div> <?php |
| 165 |
break; |
| 166 |
case 'loftloader-any-page': |
| 167 |
if ( ! empty( $this->label ) ) : ?> |
| 168 |
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> <?php |
| 169 |
endif; |
| 170 |
if ( ! empty( $this->description ) ) : ?> |
| 171 |
<span class="description customize-control-description"><?php echo wp_kses_post( $this->description ); ?></span> <?php |
| 172 |
endif; ?> |
| 173 |
<input |
| 174 |
type="button" |
| 175 |
<?php $this->link(); ?> |
| 176 |
class="button button-primary loftloader-any-page-generate" |
| 177 |
value="<?php esc_attr_e( 'Generate', 'loftloader' ); ?>" |
| 178 |
/><br/><br/> |
| 179 |
<textarea class="loftloader-any-page-shortcode" cols="30" rows="4"></textarea> |
| 180 |
<div class="customize-control-notifications-container"></div> <?php |
| 181 |
break; |
| 182 |
case 'check': ?> |
| 183 |
<label> <?php |
| 184 |
if ( ! empty( $this->label ) ) : ?> |
| 185 |
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> <?php |
| 186 |
endif; |
| 187 |
if ( ! empty( $this->description ) ) : ?> |
| 188 |
<span class="description customize-control-description"><?php echo wp_kses_post( $this->description ); ?></span> <?php |
| 189 |
endif; ?> |
| 190 |
<input class="loftlader-checkbox" type="checkbox" value="on" name="<?php echo esc_attr( $this->id ); ?>" <?php checked( 'on', $this->value() ); ?> /> |
| 191 |
<input style="display:none;" type="checkbox" value="on" <?php $this->link(); ?> <?php checked( 'on', $this->value() ); ?> /> |
| 192 |
</label> <?php break; |
| 193 |
default: |
| 194 |
parent::render_content(); |
| 195 |
if ( ! empty( $this->text ) ) { |
| 196 |
echo esc_html( $this->text ) . '<br/>'; |
| 197 |
} |
| 198 |
if ( ! empty( $this->note_below ) ) : ?> |
| 199 |
<span class="description"><?php echo esc_html( $this->note_below ); ?></span><?php |
| 200 |
endif; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
// Modify the color control class to add the display dependency logic. |
| 205 |
class LoftLoader_Customize_Color_Control extends WP_Customize_Color_Control { |
| 206 |
public $filter = false; |
| 207 |
public $parent_setting_id = ''; |
| 208 |
public $show_filter = array(); |
| 209 |
public function active_callback() { |
| 210 |
if ( $this->filter && ( $this->manager->get_setting( $this->parent_setting_id ) instanceof WP_Customize_Setting ) && ! empty( $this->show_filter ) ) { |
| 211 |
$parent_setting_value = $this->manager->get_setting( $this->parent_setting_id )->value(); |
| 212 |
return in_array( $parent_setting_value, $this->show_filter ) ? true : false; |
| 213 |
} |
| 214 |
return true; |
| 215 |
} |
| 216 |
} |
| 217 |
// Modify the image control class to add the display dependency logic. |
| 218 |
class LoftLoader_Customize_Image_Control extends WP_Customize_Image_Control { |
| 219 |
public $filter = false; |
| 220 |
public $parent_setting_id = ''; |
| 221 |
public $show_filter = array(); |
| 222 |
public function active_callback() { |
| 223 |
if ( $this->filter && ( $this->manager->get_setting($this->parent_setting_id ) instanceof WP_Customize_Setting ) && ! empty( $this->show_filter ) ) { |
| 224 |
$parent_setting_value = $this->manager->get_setting( $this->parent_setting_id )->value(); |
| 225 |
return in_array( $parent_setting_value, $this->show_filter ) ? true : false; |
| 226 |
} |
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
// Add new slider control class with jqueryui slider function |
| 231 |
class LoftLoader_Customize_Slider_Control extends LoftLoader_Customize_Control { |
| 232 |
public $input_class = ''; |
| 233 |
public $after_text = '%'; |
| 234 |
public function render_content() { |
| 235 |
if ( empty( $this->input_attrs ) ) { |
| 236 |
return; |
| 237 |
} ?> |
| 238 |
|
| 239 |
<label class="amount opacity"> <?php |
| 240 |
if ( ! empty( $this->label ) ) : ?> |
| 241 |
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> <?php |
| 242 |
endif; ?> |
| 243 |
<span class="<?php echo esc_attr( $this->input_class ); ?>"> |
| 244 |
<input readonly="readonly" type="text" <?php $this->link(); ?> value="<?php echo esc_attr( $this->value() ); ?>" > |
| 245 |
<?php echo wp_kses_post( $this->after_text ); ?> |
| 246 |
</span> |
| 247 |
</label> |
| 248 |
<div |
| 249 |
class="ui-slider loader-ui-slider" |
| 250 |
data-value="<?php echo esc_attr( $this->manager->get_setting($this->id)->value() ); ?>" |
| 251 |
<?php $this->input_attrs(); ?> |
| 252 |
> |
| 253 |
</div> |
| 254 |
<div class="customize-control-notifications-container"></div> <?php |
| 255 |
} |
| 256 |
} |
| 257 |
// Add new radio type control class for loader animation choices. |
| 258 |
class LoftLoader_Customize_Animation_Types_Control extends WP_Customize_Control { |
| 259 |
public function render_content() { |
| 260 |
if ( empty( $this->choices ) ) |
| 261 |
return; |
| 262 |
|
| 263 |
if ( ! empty( $this->label ) ) : ?> |
| 264 |
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> |
| 265 |
<?php endif; ?> |
| 266 |
<button class="customize-more-toggle" aria-expanded="false"> |
| 267 |
<span class="screen-reader-text"><?php esc_html_e('More info', 'loftloader'); ?></span> |
| 268 |
</button> <?php |
| 269 |
if ( ! empty( $this->description ) ) : ?> |
| 270 |
<span class="description customize-control-description" style="display: none;"><?php echo wp_kses_post( $this->description ); ?></span> |
| 271 |
<?php endif; ?> |
| 272 |
|
| 273 |
<div id="loftloader_option_animation"> |
| 274 |
<?php |
| 275 |
$name = '_customize-radio-' . $this->id; |
| 276 |
foreach ( $this->choices as $value => $attrs ) : |
| 277 |
$attr = ''; |
| 278 |
if ( ! empty( $attrs['attr'] ) ) { |
| 279 |
foreach ( (array)$attrs['attr'] as $attr_name => $attr_value ) { |
| 280 |
$attr .= ' ' . $attr_name . '="' . $attr_value . '"'; |
| 281 |
} |
| 282 |
} |
| 283 |
$item_id = sanitize_title( $this->id . '-' . $value ); |
| 284 |
?> |
| 285 |
<label for="<?php echo esc_attr( $item_id ); ?>" title="<?php echo esc_attr($attrs['label']); ?>"> |
| 286 |
<input |
| 287 |
id="<?php echo esc_attr( $item_id ); ?>" |
| 288 |
class="loftloader-radiobtn <?php echo esc_attr( $value ); ?>" |
| 289 |
type="radio" |
| 290 |
value="<?php echo esc_attr( $value ); ?>" |
| 291 |
name="<?php echo esc_attr( $name ); ?>" |
| 292 |
<?php $this->link(); ?> |
| 293 |
<?php checked( $this->value(), $value ); ?> |
| 294 |
<?php echo wp_kses_post( $attr ); ?> |
| 295 |
/> |
| 296 |
<span></span> |
| 297 |
</label> |
| 298 |
<?php endforeach; ?> |
| 299 |
</div> |
| 300 |
<?php |
| 301 |
} |
| 302 |
} |
| 303 |
// Add new number type control class with text after the element. |
| 304 |
class LoftLoader_Customize_Number_Text_Control extends LoftLoader_Customize_Control { |
| 305 |
public $after_text = ''; |
| 306 |
public $input_class = ''; |
| 307 |
public $input_wrap_class = ''; |
| 308 |
public function render_content() { ?> |
| 309 |
<label> |
| 310 |
<?php if ( ! empty( $this->label ) ) : ?> |
| 311 |
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> |
| 312 |
<?php endif; |
| 313 |
if ( ! empty( $this->description ) ) : ?> |
| 314 |
<span class="description customize-control-description"><?php echo wp_kses_post( $this->description ); ?></span> |
| 315 |
<?php endif; ?> |
| 316 |
<span class="<?php echo esc_attr($this->input_wrap_class); ?>"> |
| 317 |
<input |
| 318 |
class="<?php echo esc_attr( $this->input_class ); ?>" |
| 319 |
type="<?php echo esc_attr( $this->type ); ?>" |
| 320 |
<?php $this->input_attrs(); ?> |
| 321 |
value="<?php echo esc_attr( $this->value() ); ?>" |
| 322 |
<?php $this->link(); ?> |
| 323 |
/> |
| 324 |
<?php echo esc_attr( $this->after_text ); ?> |
| 325 |
</span> |
| 326 |
</label> |
| 327 |
<?php |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if ( ! function_exists( 'loftloader_sanitize_checkbox' ) ) { |
| 332 |
/** |
| 333 |
* Check the switch checkbox value |
| 334 |
* |
| 335 |
* @param string the value from user |
| 336 |
* @return mix if set return string 'on', otherwise return false |
| 337 |
*/ |
| 338 |
function loftloader_sanitize_checkbox( $input ) { |
| 339 |
return empty( $input ) ? 'off' : 'on'; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if ( ! function_exists( 'loftloader_sanitize_choice' ) ) { |
| 344 |
/** |
| 345 |
* Check the value is one of the choices from customize control |
| 346 |
* |
| 347 |
* @param string the value from user |
| 348 |
* @param object customize setting object |
| 349 |
* @return string the value from user or the default setting value |
| 350 |
*/ |
| 351 |
|
| 352 |
function loftloader_sanitize_choice( $input, $setting ) { |
| 353 |
$choices = $setting->manager->get_control( $setting->id )->choices; |
| 354 |
$choices = array_keys( $choices ); |
| 355 |
return in_array( $input, $choices ) ? $input : $setting->default; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if ( ! function_exists( 'loftloader_sanitize_number' ) ) { |
| 360 |
/** |
| 361 |
* Check the value is float with 1 decimal |
| 362 |
* |
| 363 |
* @param string the value from user |
| 364 |
* @param object customize setting object |
| 365 |
* @return string the value from user or the default setting value |
| 366 |
*/ |
| 367 |
|
| 368 |
function loftloader_sanitize_number( $value) { |
| 369 |
if ( ! empty( $value ) ) { |
| 370 |
$value = floatval( $value ); |
| 371 |
return number_format( $value, 1, '.', '' ); |
| 372 |
} |
| 373 |
return 0; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|