Skip to content

Commit

Permalink
Add popup to customizer VisualComposer#328
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-kovalov committed Jul 6, 2022
1 parent f44c96a commit 5ea8acb
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 2 deletions.
72 changes: 71 additions & 1 deletion css/customizer-custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,74 @@

.hiddenSection {
display: none !important;
}
}

.vct-popup {
background-color: rgba(0,0,0, .4);
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1000;
align-items: center;
justify-content: center;
}

.vct-popup-content {
max-width: 100%;
width: 600px;
background: #fff;
overflow: auto;
position: relative;
padding: 35px;
margin: 0 15px;
box-sizing: border-box;
border-radius: 5px;
text-align: center;
}

.vct-popup-close {
position: absolute;
right: 5px;
top: 5px;
cursor: pointer;
background: transparent;
border: none;
height: 30px;
width: 30px;
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
}

.vct-popup-close::before,
.vct-popup-close::after {
content: '';
height: 2px;
width: 20px;
background: #bdbdbd;
position: absolute;
}

.vct-popup-close::before {
transform: rotate(45deg);
}

.vct-popup-close::after {
transform: rotate(-45deg);
}

.vct-popup-buttons {
margin-top: 30px;
}

.vct-popup-buttons button {
background: #a8a8a8;
border-radius: initial;
border: none;
padding: 10px 15px;
color: #fff;
cursor: pointer;
}
33 changes: 32 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,45 @@ function visualcomposerstarter_script() {
* @see add_action('customize_preview_init',$func)
*/
function visualcomposerstarter_customizer_live_preview() {
wp_enqueue_script( 'visualcomposerstarter-themecustomizer', get_template_directory_uri() . '/js/customize-preview.js', array(
wp_enqueue_script( 'visualcomposerstarter-themecustomizer', get_template_directory_uri() . '/js/customize-preview.min.js', array(
'jquery',
'customize-preview',
), '', true );
}
}
add_action( 'customize_preview_init', 'visualcomposerstarter_customizer_live_preview' );

if ( ! function_exists( 'visualcomposerstarter_customizer_scripts' ) ) {
/**
* Enqueue script in customizer (not preview!)
*
* Used by hook: 'customize_controls_enqueue_scripts'
*
* @return void
*/
function visualcomposerstarter_customizer_scripts() {
wp_enqueue_script( 'visualcomposerstarter-customizer-popup', get_template_directory_uri() . '/js/customize-popup.js', array(
'jquery',
'wp-util',
), null, true );

// Add current fonts for "revert" button
$fonts = array(
'vct_fonts_and_style_h1_font_family' => get_theme_mod( 'vct_fonts_and_style_h1_font_family' ),
'vct_fonts_and_style_h2_font_family' => get_theme_mod( 'vct_fonts_and_style_h2_font_family' ),
'vct_fonts_and_style_h3_font_family' => get_theme_mod( 'vct_fonts_and_style_h3_font_family' ),
'vct_fonts_and_style_h4_font_family' => get_theme_mod( 'vct_fonts_and_style_h4_font_family' ),
'vct_fonts_and_style_h5_font_family' => get_theme_mod( 'vct_fonts_and_style_h5_font_family' ),
'vct_fonts_and_style_h6_font_family' => get_theme_mod( 'vct_fonts_and_style_h6_font_family' ),
'vct_fonts_and_style_body_font_family' => get_theme_mod( 'vct_fonts_and_style_body_font_family' ),
'vct_fonts_and_style_buttons_font_family' => get_theme_mod( 'vct_fonts_and_style_buttons_font_family' ),
);

wp_localize_script( 'visualcomposerstarter-customizer-popup', 'vctCurrentFonts', $fonts );
}
}
add_action( 'customize_controls_enqueue_scripts', 'visualcomposerstarter_customizer_scripts' );

if ( ! function_exists( 'visualcomposerstarter_body_classes' ) ) {
/**
* Adds custom classes to the array of body classes.
Expand Down
94 changes: 94 additions & 0 deletions inc/customizer/class-visualcomposerstarter-customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public function __construct() {
add_action( 'customize_register', array( $this, 'custom_css' ) );
add_action( 'customize_register', array( $this, 'register_customize_sections' ) );
add_action( 'wp_ajax_vct_check_font', array( $this, 'ajax_check_font' ) );
add_action( 'wp_ajax_vct_check_fonts', array( $this, 'ajax_check_fonts' ) );
add_action( 'wp_ajax_vct_download_font', array( $this, 'ajax_download_font' ) );
add_action( 'wp_ajax_vct_download_fonts', array( $this, 'ajax_download_fonts' ) );
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_popup_template' ), 999 );
}

/**
Expand Down Expand Up @@ -2936,6 +2939,32 @@ public function ajax_check_font() {
) );
}

/**
* Batch check whether provided fonts is from Google Fonts library and exists locally
*
* Callback for ajax "wp_ajax_vct_check_fonts".
*
* @return void
*/
public function ajax_check_fonts() {
if ( ! check_ajax_referer( 'vct_google_fonts_ajax_nonce', 'security', false ) ) {
wp_send_json_error( esc_html__( 'Invalid security token sent.', 'visual-composer-starter' ) );
}

if ( empty( $_POST['fonts'] ) ) {
wp_send_json_error( esc_html__( 'Fonts are missing.', 'visual-composer-starter' ) );
}

foreach ( $_POST['fonts'] as $font ) {
$is_google_font = VisualComposerStarter_Fonts::is_google_font( $font );
if ( $is_google_font && ! VisualComposerStarter_Fonts::is_google_font_exists_locally( $font ) ) {
wp_send_json_success( array( 'at_least_one_missing' => true ) );
}
}

wp_send_json_success( array( 'all_fonts_exists' => true ) );
}

/**
* Download google font
*
Expand Down Expand Up @@ -2973,4 +3002,69 @@ public function ajax_download_font() {

wp_send_json_success();
}

/**
* Batch download Google Fonts
*
* Callback for ajax "wp_ajax_vct_download_fonts"
*
* @return void
*/
public function ajax_download_fonts() {
if ( ! check_ajax_referer( 'vct_google_fonts_ajax_nonce', 'security', false ) ) {
wp_send_json_error( esc_html__( 'Invalid security token sent.', 'visual-composer-starter' ) );
}

if ( empty( $_POST['fonts'] ) ) {
wp_send_json_error( esc_html__( 'Missing fonts.', 'visual-composer-starter' ) );
}

foreach ( $_POST['fonts'] as $font ) {
// Check if provided font is from Google Fonts library
if ( ! VisualComposerStarter_Fonts::is_google_font( $font ) ) {
continue;
}

// Check if provided font already downloaded
if ( VisualComposerStarter_Fonts::is_google_font_exists_locally( $font ) ) {
continue;
}

$is_downloaded = VisualComposerStarter_Fonts::download_google_font( $font );
if ( is_wp_error( $is_downloaded ) ) {
wp_send_json_error( $is_downloaded );
}
}

wp_send_json_success();
}

/**
* Print popup template
*
* @return void
*/
public function print_popup_template() {
if ( ! is_customize_preview() ) {
return;
}

?>
<div class="vct-popup" id="vct-popup" style="display: none;">
<div class="vct-popup-content">
<button class="vct-popup-close"></button>
<p>
<?php esc_html_e(
'In order to comply with the GDPR, you need to download all font families and store them locally on your server.',
'visual-composer-starter'
); ?>
</p>
<div class="vct-popup-buttons">
<button id="vct-popup-accept-button"><?php esc_html_e( 'Download & Publish', 'visual-composer-starter' ); ?></button>
<button id="vct-popup-cancel-button"><?php esc_html_e( 'Revert & Publish', 'visual-composer-starter' ); ?></button>
</div>
</div>
</div>
<?php
}
}
120 changes: 120 additions & 0 deletions js/customize-popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
( function ( $, document, window, wp ) {
let popupElement = null

const handlePopupClose = ( e ) => {
e && e.preventDefault && e.preventDefault();
closePopup();
}

const closePopup = () => {
popupElement.style = 'display: none;';
}

const handleOpenPopup = ( id ) => {
popupElement = document.getElementById( id );
if ( popupElement ) {
popupElement.style = "display: flex;";
}
}

window.vctHandleOpenPopup = handleOpenPopup

const fontSettings = [
'vct_fonts_and_style_h1_font_family',
'vct_fonts_and_style_h2_font_family',
'vct_fonts_and_style_h3_font_family',
'vct_fonts_and_style_h4_font_family',
'vct_fonts_and_style_h5_font_family',
'vct_fonts_and_style_h6_font_family',
'vct_fonts_and_style_body_font_family',
'vct_fonts_and_style_buttons_font_family'
];

let changedFonts = {};

wp.customize.bind( 'ready', function () {
wp.customize.bind( 'change', function ( setting ) {
if ( fontSettings.includes( setting.id ) ) {
changedFonts[setting.id] = setting.get();
}
} );
} );

$( function () {
// Handle click on "Publish"
$( '#save' ).off( 'click' ).on( 'click', function ( e ) {
// Make sure all font are downloaded if changed any
if ( !$.isEmptyObject( changedFonts ) ) {
$.post( window.ajaxurl, {
'action': 'vct_check_fonts',
'security': window.googleFontControlData.nonce,
'fonts': changedFonts,
} ).fail( function ( xhr, status, error ) {
alert( xhr.responseText );
} ).done( function ( response ) {
if ( false === response.success ) {
alert( response.data );
} else {
let data = response.data;
// All fonts are downloaded: just save
if ( data.hasOwnProperty( 'all_fonts_exists' ) && data.all_fonts_exists === true ) {
wp.customize.previewer.save();
} else if ( data.hasOwnProperty( 'at_least_one_missing' ) && data.at_least_one_missing === true ) {
// Show popup
window.vctHandleOpenPopup( 'vct-popup' );
}
}
} );
} else {
wp.customize.previewer.save();
}

e.preventDefault();
} );
} );

// Download fonts
$( document ).on( 'click', '#vct-popup-accept-button', function ( e ) {
e && e.preventDefault && e.preventDefault();
if ( !$.isEmptyObject( changedFonts ) ) {
$.post( window.ajaxurl, {
'action': 'vct_download_fonts',
'security': window.googleFontControlData.nonce,
'fonts': changedFonts,
} ).fail( function ( xhr, status, error ) {
alert( xhr.responseText );
} ).done( function ( response ) {
if ( false === response.success ) {
alert( response.data );
} else {
closePopup();
wp.customize.previewer.save();
}
} );
} else {
wp.customize.previewer.save();
}
} );

// Handle click of "Revert" button
$( document ).on( 'click', '#vct-popup-cancel-button', function ( e ) {
e && e.preventDefault && e.preventDefault();
let previousFonts = window.vctCurrentFonts;
if ( !$.isEmptyObject( changedFonts ) && !$.isEmptyObject( previousFonts ) ) {
$.each( changedFonts, function ( id, value ) {
if ( previousFonts.hasOwnProperty( id ) ) {
wp.customize( id, function ( setting ) {
setting.set( previousFonts[id] );
} );
}
} );
}

closePopup();
wp.customize.previewer.save();
} );

// Close popup
$( document ).on( 'click', '.vct-popup-close', handlePopupClose );
}( jQuery, document, window, window.wp ) );

0 comments on commit 5ea8acb

Please sign in to comment.