Skip to content

Commit

Permalink
#4 - AJAX compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-pilotin committed Mar 17, 2020
1 parent 5ef55f9 commit 4608c02
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 34 deletions.
75 changes: 63 additions & 12 deletions assets/js/pilopress-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* Layout admin page
*/

var $title = $('#title');
var $prepend = $('.acf-input-prepend span');
var $layoutSlug = $('#acf_field_group-_pip_layout_slug');
var $title = $('#title');
var $prepend = $('.acf-input-prepend span');
var $layoutSlug = $('#acf_field_group-_pip_layout_slug');
var $layoutTemplate = $('#acf_field_group-_pip_render_layout');
var $renderCSS = $('#acf_field_group-_pip_render_style');
var $renderSCSS = $('#acf_field_group-_pip_render_style_scss');
var $renderScript = $('#acf_field_group-_pip_render_script');
var layoutSwitch = false;
var $renderCSS = $('#acf_field_group-_pip_render_style');
var $renderSCSS = $('#acf_field_group-_pip_render_style_scss');
var $renderScript = $('#acf_field_group-_pip_render_script');
var layoutSwitch = false;

/**
* When something is typed in "title" field
Expand Down Expand Up @@ -79,11 +79,11 @@
*/
function sanitize_title ($val) {
return $val.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/\_\_+/g, '_') // Replace multiple _ with single _
.replace(/^-+/, ''); // Trim - from start of text
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/\_\_+/g, '_') // Replace multiple _ with single _
.replace(/^-+/, ''); // Trim - from start of text
}

/**
Expand All @@ -93,6 +93,57 @@
if ($('body').hasClass('wp-admin', 'post-type-acf-field-group') && searchParams.get('layouts') == 1) {
$('.subsubsub li:last-child:not([class])').remove();
}

// Compile styles button
$('#wp-admin-bar-compile_scss').on('click', function (e) {
e.preventDefault();

// Variables
let confirmed = '';
let page_title = '.wrap h1';
let body = $('body');
let compiling_message = '<div class="notice notice-info is-dismissible compiling-notice"><p>Compiling...</p></div>';
let compiled_message = '<div class="notice notice-success is-dismissible compiling-notice"><p>Styles compiled successfully!</p></div>';
let compiled_error_message = '<div class="notice notice-error is-dismissible compiling-notice"><p>An error occurred while compiling.</p></div>';

// On styles option page, check if modified options
if ((body.hasClass('admin_page_pip-styles-demo')
|| body.hasClass('admin_page_pip-styles-css')
|| body.hasClass('admin_page_pip-styles-fonts')
|| body.hasClass('admin_page_pip-styles-colors')
|| body.hasClass('admin_page_pip-styles-bt-options')
|| body.hasClass('admin_page_pip-styles-typography')
|| body.hasClass('admin_page_pip-styles-btn-form')
|| body.hasClass('admin_page_pip-styles-image-sizes'))
&& $('#_acf_changed').val() == 1) {
confirmed = confirm('Are you sure? You will lose all your changes. Click on "Update" button to save it.');
}

// Cancel action
if (!confirmed) {
return;
}

// Show loading message
$(compiling_message).insertAfter(page_title);

// AJAX action
$.post(
ajaxurl,
{
'action': 'compile_styles',
},
function (response) {
if (response === '1') {
$('.compiling-notice').remove();
$(compiled_message).insertAfter(page_title);
} else {
$('.compiling-notice').remove();
$(compiled_error_message).insertAfter(page_title);
}
}
);
});
});

})(jQuery);
42 changes: 20 additions & 22 deletions includes/classes/admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
class PIP_Admin {
public function __construct() {
// WP hooks
add_action( 'admin_init', array( $this, 'compile_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_menu', array( $this, 'add_admin_menu' ), 20 );
add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 9999 );
Expand All @@ -15,45 +14,44 @@ public function __construct() {
add_action( 'adminmenu', array( $this, 'admin_menu_parent' ) );
add_filter( 'admin_url', array( $this, 'change_admin_url' ), 10, 2 );
add_filter( 'upload_mimes', array( $this, 'allow_mimes_types' ) );

// AJAX hooks
add_action( 'wp_ajax_compile_styles', array( $this, 'compile_styles' ) );
add_action( 'wp_ajax_nopriv_compile_styles', array( $this, 'compile_styles' ) );
}

/**
* Enqueue admin style & scripts
*/
public function enqueue_scripts() {
// Style
wp_enqueue_style( 'admin-style', PIP_URL . 'assets/css/pilopress-admin.css', array(), null );
wp_enqueue_style( 'pilopress-admin-style', PIP_URL . 'assets/css/pilopress-admin.css', array(), null );

// Scripts
wp_enqueue_script( 'admin-script', PIP_URL . 'assets/js/pilopress-admin.js', array( 'jquery' ), null );
wp_enqueue_script( 'pilopress-admin-script', PIP_URL . 'assets/js/pilopress-admin.js', array( 'jquery' ), null );
wp_localize_script( 'pilopress-admin-script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}

/**
* Compile styles
*/
public function compile_styles() {
// Compile styles
if ( acf_maybe_get_GET( 'compile_scss' ) === '1' ) {
// Get action
$action = filter_input( INPUT_POST, 'action', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

// Compile
$compiled = PIP_Styles_Settings::compile_styles_settings();

// Redirect
$url = remove_query_arg( 'compile_scss' );
wp_redirect( add_query_arg( array( 'compiled_scss' => $compiled ? 'success' : 'error' ), $url ) );
exit;
// If not compile_styles action, return
if ( $action !== 'compile_styles' ) {
return;
}

// Show notice after styles compiled
$compiled_scss = acf_maybe_get_GET( 'compiled_scss' );
switch ( $compiled_scss ) {
case 'success' :
acf_add_admin_notice( __( 'Styles compiled successfully.', 'pilopress' ), 'success' );
break;
case 'error' :
acf_add_admin_notice( __( 'An error appended. Please try again later.', 'pilopress' ), 'error' );
break;
}
// Compile
$compiled = PIP_Styles_Settings::compile_styles_settings();

// Return result
echo (string) $compiled;

// End AJAX action
die();
}

/**
Expand Down

0 comments on commit 4608c02

Please sign in to comment.