diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..88304e7 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "./vendor/npm-asset/eslint-config-wordpress/index.js" +} diff --git a/README.md b/README.md index 84b69ce..3923525 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ * Requires at least: 4.0 * Tested up to: 4.9 * Requires PHP: 5.2 -* Stable tag: 1.0.0-alpha +* Stable tag: 1.0.0-beta * License: GPLv2 or later * License URI: http://www.gnu.org/licenses/gpl-2.0.html diff --git a/composer.json b/composer.json index 3be3a2c..e27023e 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "stklcode/wp-liveticker2", - "version": "1.0.0-alpha", + "version": "1.0.0-beta", "description": "A simple Liveticker for Wordpress.", "keywords": [ "wordpress", @@ -32,7 +32,8 @@ "wp-coding-standards/wpcs": "~0.14", "patchwork/jsqueeze": "^2.0.5", "natxet/CssMin": "^3.0.5", - "matthiasmullie/minify": "^1.3" + "matthiasmullie/minify": "^1.3", + "npm-asset/eslint-config-wordpress": "^2.0" }, "scripts": { "post-install-cmd": [ @@ -70,5 +71,11 @@ "minifycss styles/wp-liveticker2.css > styles/wp-liveticker2.min.css", "minifyjs scripts/wp-liveticker2.js > scripts/wp-liveticker2.min.js" ] - } + }, + "repositories": [ + { + "type": "composer", + "url": "https://asset-packagist.org" + } + ] } diff --git a/includes/class-wpliveticker2.php b/includes/class-wpliveticker2.php index 865a434..9f0ca13 100644 --- a/includes/class-wpliveticker2.php +++ b/includes/class-wpliveticker2.php @@ -274,7 +274,7 @@ class WPLiveticker2 { // Add endpoint to script. wp_localize_script( 'wplt2-js', - 'ajax_object', + 'wplt2Ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wplt2_update-ticks' ), diff --git a/package.json b/package.json index 24c1b26..5b82622 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wp-liveticker2", - "version": "1.0.0-alpha", + "version": "1.0.0-beta", "description": "A simple Liveticker for Wordpress.", "author": "Stefan Kalscheuer", "license": "GPL-2.0+" diff --git a/scripts/wp-liveticker2.js b/scripts/wp-liveticker2.js index 99e65d9..5686f0b 100644 --- a/scripts/wp-liveticker2.js +++ b/scripts/wp-liveticker2.js @@ -1,13 +1,28 @@ +/** + * Contructor of the WPLT object. + * + * @constructor + */ function WPLT2() { - } /** * Initialize WP-Liveticker 2 JS component. * - * @return void + * @return {void} */ -WPLT2.init = function () { +WPLT2.init = function() { + + // Opt out if AJAX pobject not present. + if ( 'undefined' === typeof wplt2Ajax ) { + return; + } + + // Extract AJAX settings. + WPLT2.ajaxURL = wplt2Ajax.ajax_url; + WPLT2.nonce = wplt2Ajax.nonce; + WPLT2.pollInterval = wplt2Ajax.poll_interval; + // Get ticker elements. WPLT2.ticker = [].map.call( document.querySelectorAll( 'ul.wplt2-ticker-ajax' ), @@ -34,13 +49,8 @@ WPLT2.init = function () { } ); - // Extract AJAX settings. - WPLT2.ajaxURL = ajax_object.ajax_url; - WPLT2.nonce = ajax_object.nonce; - WPLT2.pollInterval = ajax_object.poll_interval; - // Trigger update, if necessary. - if ( ( WPLT2.ticker.length > 0 || WPLT2.widgets.length ) && WPLT2.pollInterval > 0 ) { + if ( ( 0 < WPLT2.ticker.length || WPLT2.widgets.length ) && 0 < WPLT2.pollInterval ) { setTimeout( WPLT2.update, WPLT2.pollInterval ); } }; @@ -48,13 +58,14 @@ WPLT2.init = function () { /** * Update liveticker on current page via AJAX call. * - * @return void + * @return {void} */ -WPLT2.update = function () { +WPLT2.update = function() { + // Extract ticker-slug, limit and timestamp of last poll. var updateReq = 'action=wplt2_update-ticks&_ajax_nonce=' + WPLT2.nonce; - var i; - var j; + var i, j; + var xhr = new XMLHttpRequest(); for ( i = 0; i < WPLT2.ticker.length; i++ ) { updateReq = updateReq + @@ -70,39 +81,38 @@ WPLT2.update = function () { } // Issue AJAX request. - var xhr = new XMLHttpRequest(); xhr.open( 'POST', WPLT2.ajaxURL, true ); xhr.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded;' ); xhr.onreadystatechange = function() { - if( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) { + var update; + if ( XMLHttpRequest.DONE === this.readyState && 200 === this.status ) { try { - var update = JSON.parse( this.responseText ); + update = JSON.parse( this.responseText ); if ( update ) { update.forEach( function( u ) { WPLT2.ticker.forEach( function( t ) { if ( t.s === u.s ) { - // Update last poll timestamp. + t.t = u.t; // Update last poll timestamp. + WPLT2.updateHTML( t, u ); // Update HTML markup. + } + } + ); + WPLT2.widgets.forEach( + function( t ) { + if ( t.w === u.w ) { t.t = u.t; - // Update HTML markup. WPLT2.updateHTML( t, u ); } } ); - WPLT2.widgets.forEach( function ( t ) { - if ( t.w === u.w ) { - t.t = u.t; - WPLT2.updateHTML( t, u ); - } - } ); } ); } - // Re-trigger update. - setTimeout( WPLT2.update, WPLT2.pollInterval ); + setTimeout( WPLT2.update, WPLT2.pollInterval ); // Re-trigger update. } catch ( e ) { - console.warn( 'WP-Liveticker 2 AJAX update failed, stopping automatic updates.' ) + console.warn( 'WP-Liveticker 2 AJAX update failed, stopping automatic updates.' ); } } }; @@ -112,16 +122,22 @@ WPLT2.update = function () { /** * Do actual update of HTML code. * - * @param t Ticker or Widget reference. - * @param u Update entity. - * @return void + * @param {Object} t Ticker or Widget reference. + * @param {number} t.l Limit of entries to display. + * @param {HTMLElement} t.e HTML element of the ticker/widget. + * @param {Object} u Update entity. + * @param {string} u.h HTML code to append. + * @param {number} u.t Timetsamp of last update. + * @return {void} */ WPLT2.updateHTML = function( t, u ) { + // Prepend HTML of new ticks. t.e.innerHTML = u.h + t.e.innerHTML; t.e.setAttribute( 'data-wplt2-last', u.t ); + // Remove tail, if limit is set. - if ( t.l > 0 ) { + if ( 0 < t.l ) { [].slice.call( t.e.getElementsByTagName( 'li' ), t.l ).forEach( function( li ) { li.remove(); @@ -133,7 +149,6 @@ WPLT2.updateHTML = function( t, u ) { document.addEventListener( 'DOMContentLoaded', function() { - // Trigger periodic update of livetickers. - WPLT2.init(); + WPLT2.init(); // Trigger periodic update of livetickers. } );