From 8100d07eedd76bf3e9d54260345f0da87a691412 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Thu, 7 Dec 2017 21:15:40 +0100 Subject: [PATCH] Implemented AJAX skeleton with configurable poll interval --- Gulpfile.js | 28 +++++- includes/class-wpliveticker2-admin.php | 31 +++++- includes/class-wpliveticker2.php | 125 ++++++++++++++++++------- includes/scripts.php | 19 ---- package.json | 1 + scripts/wp-liveticker2.js | 29 ++++++ 6 files changed, 175 insertions(+), 58 deletions(-) delete mode 100644 includes/scripts.php create mode 100644 scripts/wp-liveticker2.js diff --git a/Gulpfile.js b/Gulpfile.js index 0793b16..c0c9dc0 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -7,6 +7,7 @@ const phpunit = require('gulp-phpunit'); const exec = require('child_process').exec; const phpcs = require('gulp-phpcs'); const cleanCSS = require('gulp-clean-css'); +const minify = require('gulp-minify'); const argv = require('yargs').argv; const config = require('./package.json'); @@ -52,7 +53,7 @@ gulp.task('test-cs', function (cb) { // Bundle files as required for plugin distribution. gulp.task('bundle', ['clean'], function () { console.log('Collecting files for package dist/' + config.name + config.version + ' ...'); - return gulp.src(['**/*.php', 'styles/*.css', '!test/**', '!vendor/**', 'README.md', 'LICENSE.md'], {base: './'}) + return gulp.src(['**/*.php', 'styles/*.css', 'scripts/*.js', '!test/**', '!vendor/**', 'README.md', 'LICENSE.md'], {base: './'}) .pipe(copy('./dist/' + finalName + '/' + config.name)); }); @@ -69,12 +70,33 @@ gulp.task('minify-css', function () { } }); +// Minify JavaScript. +gulp.task('minify-js', function () { + if (!dev) { + console.log('Minifying JS ...'); + return gulp.src('./dist/' + finalName + '/' + config.name + '/scripts/**/*.js') + .pipe(minify({ + ext : { + source: '.js', + min : '.js' + }, + ignoreFiles : ['*.min.js'], + noSource : true, + preserveComments: 'some' + })) + .pipe(gulp.dest('./dist/' + finalName + '/' + config.name + '/scripts')); + } else { + console.log('Development flag detected, not minifying JS ...'); + } +}); + + // Create a ZIP package of the relevant files for plugin distribution. -gulp.task('package', ['bundle'], function () { +gulp.task('package', ['minify-js', 'minify-css', 'bundle'], function () { console.log('Building package dist/' + config.name + config.version + '.zip ...'); return gulp.src('./dist/' + config.name + '.' + config.version + '/**') .pipe(zip(finalName + '.zip')) .pipe(gulp.dest('./dist')); }); -gulp.task('default', ['clean', 'compose', 'test', 'test-cs', 'bundle', 'minify-css', 'package']); +gulp.task('default', ['clean', 'compose', 'test', 'test-cs', 'bundle', 'minify-css', 'minify-css', 'package']); diff --git a/includes/class-wpliveticker2-admin.php b/includes/class-wpliveticker2-admin.php index 46047e8..c782d8b 100644 --- a/includes/class-wpliveticker2-admin.php +++ b/includes/class-wpliveticker2-admin.php @@ -77,6 +77,13 @@ class WPLiveticker2_Admin extends WPLiveticker2 { 'wplt2-settings-page', 'wplt2_settings_general' ); + add_settings_field( + 'poll_interval', + __( 'AJAX poll interval', 'wplt2' ), + array( 'WPLiveticker2_Admin', 'settings_poll_interval_field' ), + 'wplt2-settings-page', + 'wplt2_settings_general' ); + add_settings_field( 'enable_css', __( 'Default CSS Styles', 'wplt2' ), @@ -131,6 +138,21 @@ class WPLiveticker2_Admin extends WPLiveticker2 { echo '

' . esc_html__( 'Disable this option to not use AJAX update. This means all liveticker widgets and shortcodes are only updated once on site load.', 'wplt2' ) . '

'; } + /** + * Render AJAX poll interval field. + * + * @return void + */ + public static function settings_poll_interval_field() { + $pollInterval = self::$_options['poll_interval']; + + echo ''; + echo '

' . esc_html__( 'Interval (in seconds) to update ticker if AJAX is enabled.', 'wplt2' ) . '

'; + } + /** * Render default style field. * @@ -182,6 +204,13 @@ class WPLiveticker2_Admin extends WPLiveticker2 { */ public static function validate_settings( $input ) { $defaults = self::default_options(); - return wp_parse_args( $input, $defaults ); + $result = wp_parse_args( $input, $defaults ); + foreach ( $defaults as $k => $v ) { + if ( is_int( $v ) ) { + $result[ $k ] = intval( $result[ $k ] ); + } + } + + return $result; } } diff --git a/includes/class-wpliveticker2.php b/includes/class-wpliveticker2.php index d7957d4..14512ce 100644 --- a/includes/class-wpliveticker2.php +++ b/includes/class-wpliveticker2.php @@ -48,14 +48,19 @@ class WPLiveticker2 { * @return void */ public static function init() { - // Skip on autosave or AJAX. - if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { + // Skip on autosave. + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Load plugin options. self::update_options(); + // Skip on AJAX if not enabled disabled. + if ( ( ! isset( self::$_options['enable_ajax'] ) || 1 !== self::$_options['enable_ajax'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { + return; + } + // Load Textdomain. load_plugin_textdomain( 'wplt2', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); @@ -68,9 +73,13 @@ class WPLiveticker2 { // Enqueue styles. add_action( 'wp_footer', array( 'WPLiveticker2', 'enqueue_styles' ) ); + // Enqueue JavaScript. + add_action( 'wp_footer', array( 'WPLiveticker2', 'enqueue_scripts' ) ); + // Add AJAX hook if configured. if ( 1 === self::$_options['enable_ajax'] ) { add_action( 'wp_ajax_wplt2_update-ticks', array( 'WPLiveticker2', 'ajax_update' ) ); + add_action( 'wp_ajax_nopriv_wplt2_update-ticks', array( 'WPLiveticker2', 'ajax_update' ) ); } // Admin only actions. @@ -167,7 +176,13 @@ class WPLiveticker2 { $atts['limit'] = - 1; } - $output = '