Overhauled Widget class

This commit is contained in:
Stefan Kalscheuer 2017-11-12 11:33:23 +01:00
parent f831027aec
commit c5d945fba9
6 changed files with 250 additions and 204 deletions

View File

@ -25,8 +25,8 @@ class WPLiveticker2_Admin extends WPLiveticker2 {
$total_files = wp_count_posts( 'wplt2_tick' ); $total_files = wp_count_posts( 'wplt2_tick' );
echo '<tr>'; echo '<tr>';
echo '<td class="first b b-tags"><a href="edit.php?post_type=wplt_tick">' . $total_files->publish . '</a></td>'; echo '<td class="first b b-tags"><a href="edit.php?post_type=wplt_tick">' . esc_html( $total_files->publish ) . '</a></td>';
echo '<td class="t tags"><a href="edit.php?post_type=wplt_tick">' . __( 'Ticks', 'wplt2' ) . '</a></td>'; echo '<td class="t tags"><a href="edit.php?post_type=wplt_tick">' . esc_html__( 'Ticks', 'wplt2' ) . '</a></td>';
echo '</tr>'; echo '</tr>';
} }
} }

View File

@ -12,7 +12,6 @@ defined( 'ABSPATH' ) || exit;
/** /**
* WP Liveticker 2 system configuration. * WP Liveticker 2 system configuration.
*
*/ */
class WPLiveticker2_System extends WPLiveticker2 { class WPLiveticker2_System extends WPLiveticker2 {

View File

@ -0,0 +1,236 @@
<?php
/**
* WP Liveticker 2: Widget class.
*
* This file contains the liveticker widget.
*
* @package WPLiveticker2
*/
/**
* Class WPLiveticker2_Widget.
*/
class WPLiveticker2_Widget extends WP_Widget {
/**
* WPLiveticker2_Widget constructor.
*/
public function __construct() {
parent::__construct( false, 'Liveticker' );
}
/**
* Register the widget.
*/
public static function register() {
register_widget( 'WPLiveticker2_Widget' );
}
/**
* Echoes the widget content.
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance The settings for the particular instance of the widget.
*/
public function widget( $args, $instance ) {
$instance = self::fill_options_with_defaults( $instance );
$before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
$after_widget = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
$before_title = isset( $args['before_title'] ) ? $args['before_title'] : '';
$after_title = isset( $args['after_title'] ) ? $args['after_title'] : '';
$title = apply_filters( 'wplt2_catlit', $instance['title'] );
$category = apply_filters( 'wplt2_catlit', $instance['category'] );
$count = apply_filters( 'wplt2_catlit', $instance['count'] );
$link = apply_filters( 'wplt2_catlit', $instance['link'] );
$highlight = apply_filters( 'wplt2_catlit', $instance['highlight'] );
$highlight_time = apply_filters( 'wplt2_catlit', $instance['highlight_time'] );
$ajax = apply_filters( 'wplt2_catlit', $instance['ajax'] );
?>
<?php
echo $before_widget;
?>
<?php
if ( $title ) {
echo $before_title . esc_html( $title ) . $after_title;
}
?>
<ul class="wplt_widget">
<?php
$args = array(
'post_type' => 'wplt2_tick',
'tax_query' => array(
array(
'taxonomy' => 'wplt2_ticker',
'field' => 'slug',
'terms' => $category,
),
),
);
$wp_query = new WP_Query( $args );
$cnt = 0;
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<li>
<span class="wplt_widget_time"><?php echo esc_html( get_the_time( 'd.m.Y - H.i' ) ); ?></span><span class="wplt_widget_content<?php if ( '1' === $highlight && get_the_time( 'U' ) > ( time() - $highlight_time ) ) {
echo '_new';
} ?>"><br /><?php echo the_title(); ?></span></li>
<?php
if ( $count > 0 && ++ $cnt === $count ) {
break;
}
endwhile;
?>
</ul>
<?php
if ( $link ) {
print '<p class="wplt_widget_link"><a href="' . esc_attr( $link ) . '">' . esc_html__( 'show all', 'wplt2' ) . '...</a></p>';
}
echo $after_widget;
?>
<?php
}
/**
* Updates a particular instance of a widget.
*
* @param array $new_instance New settings for this instance as input by the user via WP_Widget::form().
* @param array $old_instance Old settings for this instance.
*
* @return array Settings to save or bool false to cancel saving.
*/
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
/**
* Outputs the settings update form.
*
* @param array $instance Current settings.
* @return void
*/
public function form( $instance ) {
// Determine configuration flags with fallback to default.
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$category = isset( $instance['category'] ) ? esc_attr( $instance['category'] ) : '';
$count = isset( $instance['count'] ) ? esc_attr( $instance['count'] ) : '';
$link = isset( $instance['link'] ) ? esc_attr( $instance['link'] ) : '';
$highlight = isset( $instance['highlight'] ) ? esc_attr( $instance['highlight'] ) : '0';
$highlight_time = isset( $instance['highlight_time'] ) ? esc_attr( $instance['highlight_time'] ) : '0';
$ajax = isset( $instance['ajax'] ) ? esc_attr( $instance['ajax'] ) : '0';
$categories = get_terms( 'wplt2_ticker', 'orderby=name&order=ASC' );
?>
<table>
<tr>
<td>
<label for="<?php echo esc_html( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</td>
</tr>
<tr>
<td>
<label for="<?php echo esc_html( $this->get_field_id( 'category' ) ); ?>"><?php esc_html_e( 'Ticker:', 'wplt2' ); ?></label>
</td>
<td>
<select id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>">
<?php foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->slug ) . '"';
if ( $category === $cat->slug ) {
echo ' selected="selected"';
}
echo '>' . esc_html( $cat->name ) . '</option>';
} ?>
</select>
</td>
</tr>
<tr>
<td>
<label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php esc_html_e( 'Number of Ticks:', 'wplt2' ); ?></label>
</td>
<td>
<select id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>">
<option value="0"<?php if ( '0' === $count ) { echo ' selected="selected"'; } ?>><?php esc_html_e( 'all', 'wplt2' ); ?></option>
<option value="1"<?php if ( '1' === $count ) { echo ' selected="selected"'; } ?>>1</option>
<option value="2"<?php if ( '2' === $count ) { echo ' selected="selected"'; } ?>>2</option>
<option value="3"<?php if ( '3' === $count ) { echo ' selected="selected"'; } ?>>3</option>
<option value="4"<?php if ( '4' === $count ) { echo ' selected="selected"'; } ?>>4</option>
<option value="5"<?php if ( '5' === $count ) { echo ' selected="selected"'; } ?>>5</option>
<option value="6"<?php if ( '6' === $count ) { echo ' selected="selected"'; } ?>>6</option>
<option value="7"<?php if ( '7' === $count ) { echo ' selected="selected"'; } ?>>7</option>
<option value="8"<?php if ( '8' === $count ) { echo ' selected="selected"'; } ?>>8</option>
<option value="9"<?php if ( '9' === $count ) { echo ' selected="selected"'; } ?>>9</option>
<option value="10"<?php if ( '10' === $count ) { echo ' selected="selected"'; } ?>>10</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php esc_html_e( 'Link (optional):', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
</td>
</tr>
<tr>
<td>
<label for="<?php echo esc_attr( $this->get_field_id( 'highlight' ) ); ?>"><?php esc_html_e( 'Highlight new:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'highlight' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'highlight' ) ); ?>" type="checkbox" value="1" <?php if ( '1' === $highlight ) {
echo ' checked="checked"';
} ?> /></td>
</tr>
<tr>
<td>
<label for="<?php echo esc_attr( $this->get_field_id( 'highlight_time' ) ); ?>"><?php esc_html_e( 'Highlight time [s]:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'highlight_time' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'highlight_time' ) ); ?>" type="text" value="<?php echo esc_html( $highlight_time ); ?>" />
</td>
</tr>
<tr>
<td>
<label for="<?php echo esc_attr( $this->get_field_id( 'ajax' ) ); ?>"><?php esc_html_e( 'Auto refresh:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'ajax' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'ajax' ) ); ?>" type="checkbox" value="1"<?php if ( '1' === $ajax ) {
echo ' checked="checked"';
} ?> disabled="disabled" />
<small><?php esc_html_e( '(enables ajax)', 'wplt2' ); ?></small>
</td>
</tr>
</table>
<?php
}
/**
* Fill instance configuration with default options.
*
* @param array $instance Potentially incomplete instance configuration.
*
* @return array Complete instance configuration.
*/
private static function fill_options_with_defaults( $instance ) {
$default = array(
'title' => '',
'category' => '',
'count' => '',
'link' => '',
'highlight' => '0',
'highlight_time' => '0',
'ajax' => '0',
);
return array_merge( $default, $instance );
}
}

View File

@ -158,8 +158,8 @@ class WPLiveticker2 {
'taxonomy' => 'wplt2_ticker', 'taxonomy' => 'wplt2_ticker',
'field' => 'slug', 'field' => 'slug',
'terms' => $atts['ticker'], 'terms' => $atts['ticker'],
) ),
) ),
); );
$wp_query = new WP_Query( $args ); $wp_query = new WP_Query( $args );
@ -175,10 +175,10 @@ class WPLiveticker2 {
$output .= '</ul>'; $output .= '</ul>';
// Show RSS feed link, if configured. // Show RSS feed link, if configured.
if (1 === self::$_options['show_feed']) { if ( 1 === self::$_options['show_feed'] ) {
$output .= '<a href="/feed/liveticker/' . esc_html( $atts['ticker'] ) . '"><img class="wplt2_rss" src="/wp-content/plugins/wp-liveticker2/images/rss.jpg" alt="RSS" /></a>'; $output .= '<a href="/feed/liveticker/' . esc_html( $atts['ticker'] ) . '"><img class="wplt2_rss" src="/wp-content/plugins/wp-liveticker2/images/rss.jpg" alt="RSS" /></a>';
} }
} }// End if().
return $output; return $output;
} }

View File

@ -1,192 +0,0 @@
<?php
class wplt_widget extends WP_Widget {
function __construct() {
parent::__construct( false, $name = 'Liveticker' );
}
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'] );
?>
<?php
echo $before_widget;
?>
<?php
if ( $title ) {
echo $before_title . $title . $after_title;
}
?>
<ul class="wplt_widget">
<?php
$args = array(
'post_type' => 'wplt_tick',
'tax_query' => array(
array(
'taxonomy' => 'wplt_ticker',
'field' => 'slug',
'terms' => $category,
)
)
);
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<li>
<span class="wplt_widget_time"><?php echo get_the_time( 'd.m.Y - H.i' ); ?></span><span class="wplt_widget_content<?php if ( $highlight == "1" && get_the_time( 'U' ) > ( time() - $highlight_time ) ) {
echo '_new';
} ?>"><br /><?php echo the_title(); ?></span></li>
<?php
if ( $count > 0 && ++ $cnt == $count ) {
break;
}
endwhile;
?>
</ul>
<?php
if ( $link ) {
print '<p class="wplt_widget_link"><a href="' . $link . '">' . __( 'show all', 'wplt2' ) . '...</a></p>';
}
echo $after_widget;
?>
<?php
}
function update( $new_instance, $old_instance ) {
return $new_instance;
}
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' );
?>
<table>
<tr>
<td><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label></td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Ticker:', 'wplt2' ); ?></label>
</td>
<td>
<select id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>">
<?php foreach ( $categories as $cat ) {
echo '<option value="' . $cat->slug . '"';
if ( $category == $cat->slug ) {
echo ' selected="selected"';
}
echo '>' . $cat->name . '</option>';
} ?>
</select>
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Number of Ticks:', 'wplt2' ); ?></label>
</td>
<td>
<select id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>">
<option value="0"<?php if ( $count == 0 )
echo ' selected="selected"' ?>><?php _e( 'all', 'wplt2' ); ?></option>
<option value="1"<?php if ( $count == 1 )
echo ' selected="selected"' ?>>1
</option>
<option value="2"<?php if ( $count == 2 )
echo ' selected="selected"' ?>>2
</option>
<option value="3"<?php if ( $count == 3 )
echo ' selected="selected"' ?>>3
</option>
<option value="4"<?php if ( $count == 4 )
echo ' selected="selected"' ?>>4
</option>
<option value="5"<?php if ( $count == 5 )
echo ' selected="selected"' ?>>5
</option>
<option value="6"<?php if ( $count == 6 )
echo ' selected="selected"' ?>>6
</option>
<option value="7"<?php if ( $count == 7 )
echo ' selected="selected"' ?>>7
</option>
<option value="8"<?php if ( $count == 8 )
echo ' selected="selected"' ?>>8
</option>
<option value="9"<?php if ( $count == 9 )
echo ' selected="selected"' ?>>9
</option>
<option value="10"<?php if ( $count == 10 )
echo ' selected="selected"' ?>>10
</option>
</select>
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link (optional):', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo $link; ?>" />
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'highlight' ); ?>"><?php _e( 'Highlight new:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'highlight' ); ?>" name="<?php echo $this->get_field_name( 'highlight' ); ?>" type="checkbox" value="1" <?php if ( $highlight == "1" ) {
echo ' checked="checked"';
} ?> /></td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'highlight_time' ); ?>"><?php _e( 'Highlight time [s]:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'highlight_time' ); ?>" name="<?php echo $this->get_field_name( 'highlight_time' ); ?>" type="text" value="<?php echo $highlight_time; ?>" />
</td>
</tr>
<tr>
<td>
<label for="<?php echo $this->get_field_id( 'ajax' ); ?>"><?php _e( 'Auto refresh:', 'wplt2' ); ?></label>
</td>
<td>
<input class="widefat" id="<?php echo $this->get_field_id( 'ajax' ); ?>" name="<?php echo $this->get_field_name( 'ajax' ); ?>" type="checkbox" value="1"<?php if ( $ajax == "1" ) {
echo ' checked="checked"';
} ?> disabled="disabled" />
<small><?php _e( '(enables ajax)', 'wplt2' ); ?></small>
</td>
</tr>
</table>
<?php
}
}
add_action( 'widgets_init', 'wplt_widget_init' );
function wplt_widget_init() {
register_widget( 'wplt_widget' );
}

View File

@ -49,6 +49,8 @@ add_filter( 'widget_text', 'do_shortcode' );
// Add shortcode. // Add shortcode.
add_shortcode( 'liveticker', array( 'WPLiveticker2', 'shortcode_ticker_show' ) ); add_shortcode( 'liveticker', array( 'WPLiveticker2', 'shortcode_ticker_show' ) );
// Add Widget.
add_action( 'widgets_init', array( 'WPLiveticker2_Widget', 'register' ) );
// Autoload. // Autoload.
spl_autoload_register( 'wplt2_autoload' ); spl_autoload_register( 'wplt2_autoload' );
@ -65,14 +67,15 @@ function wplt2_autoload( $class ) {
'WPLiveticker2', 'WPLiveticker2',
'WPLiveticker2_Admin', 'WPLiveticker2_Admin',
'WPLiveticker2_System', 'WPLiveticker2_System',
'WPLiveticker2_Widget',
); );
if ( in_array( $class, $plugin_classes, true ) ) { if ( in_array( $class, $plugin_classes, true ) ) {
require_once( require_once(
sprintf( sprintf(
'%s/includes/class-%s.php', '%s/includes/class-%s.php',
WPLT2_DIR, WPLT2_DIR,
strtolower( str_replace( '_', '-', $class ) ) strtolower( str_replace( '_', '-', $class ) )
) )
); );
} }
} }