From f831027aec03acfb48e7d492f8b6912a5aa9efe4 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 11 Nov 2017 21:44:08 +0100 Subject: [PATCH] Begin transformation into classes Grouped single PHP files with various funcitons into classes for basic, admin and system scope. --- Gulpfile.js | 61 +++++ includes/admin/dashboard.php | 43 ---- includes/class-wpliveticker2-admin.php | 32 +++ includes/class-wpliveticker2-system.php | 41 ++++ includes/class-wpliveticker2.php | 211 +++++++++++++++++ includes/functions.php | 20 -- includes/post-types.php | 78 ------- includes/shortcodes.php | 70 ------ includes/widget.php | 292 +++++++++++++++--------- styles/wp-liveticker2.css | 24 +- wp-liveticker2.php | 85 +++---- 11 files changed, 572 insertions(+), 385 deletions(-) create mode 100644 Gulpfile.js delete mode 100644 includes/admin/dashboard.php create mode 100644 includes/class-wpliveticker2-admin.php create mode 100644 includes/class-wpliveticker2-system.php create mode 100644 includes/class-wpliveticker2.php delete mode 100644 includes/functions.php delete mode 100644 includes/post-types.php delete mode 100644 includes/shortcodes.php diff --git a/Gulpfile.js b/Gulpfile.js new file mode 100644 index 0000000..e4da418 --- /dev/null +++ b/Gulpfile.js @@ -0,0 +1,61 @@ +var gulp = require('gulp'); +var clean = require('gulp-clean'); +var copy = require('gulp-copy'); +var zip = require('gulp-zip'); +var composer = require('gulp-composer'); +var phpunit = require('gulp-phpunit'); +var exec = require('child_process').exec; +var phpcs = require('gulp-phpcs'); +var config = require('./package.json'); + +// Clean the target directory. +gulp.task('clean', function () { + console.log('Cleaning up target directory ...'); + return gulp.src('dist', {read: false}) + .pipe(clean()); +}); + +// Prepare composer. +gulp.task('compose', function () { + console.log('Preparing Composer ...'); + return composer('install'); +}); + +// Execute unit tests. +gulp.task('test', ['compose'], function () { + console.log('Running PHPUnit tests ...'); + return gulp.src('phpunit.xml') + .pipe(phpunit('./vendor/bin/phpunit', {debug: false})); +}); + +// Execute PHP Code Sniffer. +gulp.task('test-cs', function (cb) { + return exec('./vendor/bin/phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs', function (err, stdout, stderr) { + console.log(stdout); + console.log(stderr); + if (null === err) { + console.log('Running PHP Code Sniffer tests ...'); + gulp.src(['statify-blacklist.php', 'inc/**/*.php']) + .pipe(phpcs({bin: './vendor/bin/phpcs', standard: 'phpcs.xml'})) + .pipe(phpcs.reporter('log')); + } + cb(err); + }); +}); + +// 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', '!test/**', '!vendor/**', 'README.md', 'LICENSE.md'], {base: './'}) + .pipe(copy('./dist/' + config.name + '.' + config.version + '/' + config.name)); +}); + +// Create a ZIP package of the relevant files for plugin distribution. +gulp.task('package', ['bundle'], function () { + console.log('Building package dist/' + config.name + config.version + '.zip ...'); + return gulp.src('./dist/' + config.name + '.' + config.version + '/**') + .pipe(zip(config.name + '.' + config.version + '.zip')) + .pipe(gulp.dest('./dist')); +}); + +gulp.task('default', ['clean', 'compose', 'test', 'test-cs', 'bundle', 'package']); \ No newline at end of file diff --git a/includes/admin/dashboard.php b/includes/admin/dashboard.php deleted file mode 100644 index d968c0e..0000000 --- a/includes/admin/dashboard.php +++ /dev/null @@ -1,43 +0,0 @@ -'; - echo '' . $total_files->publish . ''; - echo '' . __( 'Ticks', 'wplt2' ) . ''; - echo ''; -} -add_action( 'right_now_content_table_end' , 'wplt_dashboard_right_now' ); - -/** - * Register dashboard widgets - * - * @return void - */ -function wplt_register_dashboard_widgets() { - wp_add_dashboard_widget( 'wplt_dashboard_downloads', __( 'Download Stats', 'wplt2' ), 'wplt_dashboard_downloads_widget' ); -} -//add_action( 'wp_dashboard_setup', 'wplt_register_dashboard_widgets' ); - -/** - * Ticks Dashboard Widget - * - * @access private - * @since 1.0 - * @return void -*/ -function wplt_dashboard_ticks_widget() { - echo 'Content to follow...'; -} \ No newline at end of file diff --git a/includes/class-wpliveticker2-admin.php b/includes/class-wpliveticker2-admin.php new file mode 100644 index 0000000..2eb414c --- /dev/null +++ b/includes/class-wpliveticker2-admin.php @@ -0,0 +1,32 @@ +'; + echo '' . $total_files->publish . ''; + echo '' . __( 'Ticks', 'wplt2' ) . ''; + echo ''; + } +} diff --git a/includes/class-wpliveticker2-system.php b/includes/class-wpliveticker2-system.php new file mode 100644 index 0000000..7e429ec --- /dev/null +++ b/includes/class-wpliveticker2-system.php @@ -0,0 +1,41 @@ + _x( 'Ticker', 'taxonomy general name' ), + 'singular_name' => _x( 'Ticker', 'taxonomy singular name' ), + 'search_items' => __( 'Search Tickers', 'wplt2' ), + 'all_items' => __( 'All Tickers', 'wplt2' ), + 'parent_item' => __( 'Parent Ticker', 'wplt2' ), + 'parent_item_colon' => __( 'Parent Ticker:', 'wplt2' ), + 'edit_item' => __( 'Edit Ticker', 'wplt2' ), + 'update_item' => __( 'Update Ticker', 'wplt2' ), + 'add_new_item' => __( 'Add New Ticker', 'wplt2' ), + 'new_item_name' => __( 'New Ticker', 'wplt2' ), + 'menu_name' => __( 'Ticker', 'wplt2' ), + ); + + register_taxonomy( + 'wplt2_ticker', + array( 'wplt2_tick' ), + array( + 'hierarchical' => true, + 'labels' => $labels, + 'show_ui' => true, + 'show_admin_column' => true, + 'query_var' => true, + ) + ); + + // Post type arguments. + $args = array( + 'labels' => array( + 'name' => __( 'Ticks', 'wplt2' ), + 'singular_name' => __( 'Tick', 'wplt2' ), + 'add_new' => __( 'Add New', 'wplt2' ), + 'add_new_item' => __( 'Add New Tick', 'wplt2' ), + 'edit_item' => __( 'Edit Tick', 'wplt2' ), + 'new_item' => __( 'New Tick', 'wplt2' ), + 'all_items' => __( 'All Ticks', 'wplt2' ), + 'view_item' => __( 'View Tick', 'wplt2' ), + 'search_items' => __( 'Search Ticks', 'wplt2' ), + 'not_found' => __( 'No Ticks found', 'wplt2' ), + 'not_found_in_trash' => __( 'No Ticks found in Trash', 'wplt2' ), + 'parent_item_colon' => '', + 'menu_name' => __( 'Liveticker', 'wplt2' ), + ), + 'public' => false, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_icon' => 'dashicons-rss', + 'capability_type' => 'post', + 'supports' => array( 'title', 'editor', 'author' ), + 'taxonomies' => array( 'wplt2_ticker' ), + ); + + register_post_type( 'wplt2_tick', $args ); + } + + /** + * Output Liveticker + * + * @param array $atts Shortcode attributes. + * + * @return string + */ + public static function shortcode_ticker_show( $atts ) { + // Indicate presence of shortcode (to enqueue styles/scripts later). + self::$shortcode_present = true; + + // Initialize output. + $output = ''; + + // Check if first attribute is filled. + if ( $atts['ticker'] ) { + // Set limit to infinite, if not set explicitly. + if ( ! isset( $atts['limit'] ) ) { + $atts['limit'] = - 1; + } + + $output = ''; + + // Show RSS feed link, if configured. + if (1 === self::$_options['show_feed']) { + $output .= 'RSS'; + } + } + + return $output; + } + + /** + * Update options. + * + * @param array $options Optional. New options to save. + */ + protected static function update_options( $options = null ) { + self::$_options = wp_parse_args( + get_option( self::OPTION ), + self::default_options() + ); + } + + /** + * Create default plugin configuration. + * + * @return array The options array. + */ + protected static function default_options() { + return array( + 'enable_ajax' => 1, + 'enable_css' => 1, + 'show_feed' => 0, + 'reset_settings' => 0, + ); + } +} diff --git a/includes/functions.php b/includes/functions.php deleted file mode 100644 index 0d7a124..0000000 --- a/includes/functions.php +++ /dev/null @@ -1,20 +0,0 @@ - 1, - 'reset_settings' => 0, - 'shortcode_present' => false - ); -} \ No newline at end of file diff --git a/includes/post-types.php b/includes/post-types.php deleted file mode 100644 index 9b9bfad..0000000 --- a/includes/post-types.php +++ /dev/null @@ -1,78 +0,0 @@ - array( - 'name' => __( 'Ticks', 'wplt2' ), - 'singular_name' => __( 'Tick', 'wplt2' ), - 'add_new' => __( 'Add New', 'wplt2' ), - 'add_new_item' => __( 'Add New Tick', 'wplt2' ), - 'edit_item' => __( 'Edit Tick', 'wplt2' ), - 'new_item' => __( 'New Tick', 'wplt2' ), - 'all_items' => __( 'All Ticks', 'wplt2' ), - 'view_item' => __( 'View Tick', 'wplt2' ), - 'search_items' => __( 'Search Ticks', 'wplt2' ), - 'not_found' => __( 'No Ticks found', 'wplt2' ), - 'not_found_in_trash' => __( 'No Ticks found in Trash', 'wplt2' ), - 'parent_item_colon' => '', - 'menu_name' => __( 'Liveticker', 'wplt2' ) - ), - 'public' => false, - 'show_ui' => true, - 'show_in_menu' => true, - 'menu_icon' => 'dashicons-rss', - 'capability_type' => 'post', - 'supports' => array( 'title', 'editor', 'author'), - 'taxonomies' => array('wplt_ticker') - ); - - register_post_type( 'wplt_tick', $args ); -} -add_action( 'init', 'wplt_tick_post_type' ); - - -/** - * Register custom taxamony (category) - * - * @return void - */ -//hook into the init action and call create_book_taxonomies when it fires -add_action( 'init', 'wplt_ticker_taxonomy', 0 ); - -//create two taxonomies, genres and writers for the post type "book" -function wplt_ticker_taxonomy() -{ - // Add new taxonomy, make it hierarchical (like categories) - $labels = array( - 'name' => _x( 'Ticker', 'taxonomy general name' ), - 'singular_name' => _x( 'Ticker', 'taxonomy singular name' ), - 'search_items' => __( 'Search Tickers', 'wplt2' ), - 'all_items' => __( 'All Tickers', 'wplt2' ), - 'parent_item' => __( 'Parent Ticker', 'wplt2' ), - 'parent_item_colon' => __( 'Parent Ticker:', 'wplt2' ), - 'edit_item' => __( 'Edit Ticker', 'wplt2' ), - 'update_item' => __( 'Update Ticker', 'wplt2' ), - 'add_new_item' => __( 'Add New Ticker', 'wplt2' ), - 'new_item_name' => __( 'New Ticker', 'wplt2' ), - 'menu_name' => __( 'Ticker', 'wplt2' ), - ); - - register_taxonomy('wplt_ticker',array('wplt_tick'), array( - 'hierarchical' => true, - 'labels' => $labels, - 'show_ui' => true, - 'show_admin_column' => true, - 'query_var' => true, - )); -} \ No newline at end of file diff --git a/includes/shortcodes.php b/includes/shortcodes.php deleted file mode 100644 index bbdabbd..0000000 --- a/includes/shortcodes.php +++ /dev/null @@ -1,70 +0,0 @@ - $wplt_ticker_options['id'], - 'count' => $wplt_ticker_options['count'], - 'order' => $wplt_ticker_options['order'] - ), $atts ) - );*/ - - if($atts[0]) - { - if(!$atts[1]) $atts[1] = -1; - - $output = ''; - $output .= 'RSS'; - } - - return $output; -} - -add_shortcode( 'liveticker', 'wplt_shortcode_ticker_show' ); \ No newline at end of file diff --git a/includes/widget.php b/includes/widget.php index eeb17ff..3671a57 100644 --- a/includes/widget.php +++ b/includes/widget.php @@ -1,128 +1,192 @@ + function widget( $args, $instance ) { + extract( $args ); + $title = apply_filters( 'wplt_catlit', $instance['title'] ); + $category = apply_filters( 'wplt_catlit', $instance['category'] ); + $count = apply_filters( 'wplt_catlit', $instance['count'] ); + $link = apply_filters( 'wplt_catlit', $instance['link'] ); + $highlight = apply_filters( 'wplt_catlit', $instance['highlight'] ); + $highlight_time = apply_filters( 'wplt_catlit', $instance['highlight_time'] ); + $ajax = apply_filters( 'wplt_catlit', $instance['ajax'] ); + ?> - + - - + ?> + - function update( $new_instance, $old_instance ) { - return $new_instance; - } + ' . __( 'show all', 'wplt2' ) . '...

'; + } - function form( $instance ) { - $title = esc_attr( $instance['title'] ); - $category = esc_attr( $instance['category'] ); - $count = esc_attr( $instance['count'] ); - $link = esc_attr( $instance['link'] ); - $highlight = esc_attr( $instance['highlight'] ); - $highlight_time = esc_attr( $instance['highlight_time'] ); - $ajax = esc_attr( $instance['ajax'] ); - $categories = get_terms('wplt_ticker', 'orderby=name&order=ASC'); - ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- -
/>
disabled="disabled" />
- - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + +
+ + + +
+ + + +
+ + + />
+ + + +
+ + + disabled="disabled" /> + +
+ + +