Skip to content

Commit

Permalink
Merge pull request #44 from WebberZone/offset
Browse files Browse the repository at this point in the history
New offset feature in both shortcode and widget
  • Loading branch information
ajaydsouza authored Sep 21, 2016
2 parents 4a1137a + 666c3e4 commit ecc630d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions includes/modules/class-top-10-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function __construct() {
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$limit = isset( $instance['limit'] ) ? esc_attr( $instance['limit'] ) : '';
$offset = isset( $instance['offset'] ) ? esc_attr( $instance['offset'] ) : '';
$disp_list_count = isset( $instance['disp_list_count'] ) ? esc_attr( $instance['disp_list_count'] ) : '';
$show_excerpt = isset( $instance['show_excerpt'] ) ? esc_attr( $instance['show_excerpt'] ) : '';
$show_author = isset( $instance['show_author'] ) ? esc_attr( $instance['show_author'] ) : '';
Expand Down Expand Up @@ -81,6 +82,11 @@ function form( $instance ) {
<?php esc_html_e( 'No. of posts', 'top-10' ); ?>: <input class="widefat" id="<?php echo esc_attr_e( $this->get_field_id( 'limit' ) ); ?>" name="<?php echo esc_attr_e( $this->get_field_name( 'limit' ) ); ?>" type="text" value="<?php echo esc_attr( $limit ); ?>" />
</label>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>">
<?php esc_html_e( 'Offset', 'top-10' ); ?>: <input class="widefat" id="<?php echo esc_attr_e( $this->get_field_id( 'offset' ) ); ?>" name="<?php echo esc_attr_e( $this->get_field_name( 'offset' ) ); ?>" type="text" value="<?php echo esc_attr( $offset ); ?>" />
</label>
</p>
<p>
<select class="widefat" id="<?php echo esc_attr_e( $this->get_field_id( 'daily' ) ); ?>" name="<?php echo esc_attr_e( $this->get_field_name( 'daily' ) ); ?>">
<option value="overall" <?php checked( 'overall', $daily, true ); ?>><?php esc_html_e( 'Overall', 'top-10' ); ?></option>
Expand Down Expand Up @@ -178,6 +184,7 @@ function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['limit'] = $new_instance['limit'];
$instance['offset'] = $new_instance['offset'];
$instance['daily'] = $new_instance['daily'];
$instance['daily_range'] = strip_tags( $new_instance['daily_range'] );
$instance['hour_range'] = strip_tags( $new_instance['hour_range'] );
Expand Down Expand Up @@ -232,6 +239,7 @@ function widget( $args, $instance ) {
$limit = $tptn_settings['limit'];
}

$offset = isset( $instance['offset'] ) ? $instance['offset'] : 0;
$daily_range = ( empty( $instance['daily_range'] ) ) ? $tptn_settings['daily_range'] : $instance['daily_range'];
$hour_range = ( empty( $instance['hour_range'] ) ) ? $tptn_settings['hour_range'] : $instance['hour_range'];

Expand All @@ -256,6 +264,7 @@ function widget( $args, $instance ) {
'instance_id' => $this->number,
'heading' => 0,
'limit' => $limit,
'offset' => $offset,
'daily' => $daily,
'daily_range' => $daily_range,
'hour_range' => $hour_range,
Expand Down
1 change: 1 addition & 0 deletions includes/modules/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function tptn_shortcode( $atts, $content = null ) {
'heading' => 1,
'daily' => 0,
'is_shortcode' => 1,
'offset' => 0,
)
), $atts, 'top-10' );

Expand Down
5 changes: 4 additions & 1 deletion includes/public/display-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function tptn_pop_posts( $args ) {
'strict_limit' => false,
'posts_only' => false,
'heading' => 1,
'offset' => 0,
);

// Merge the $defaults array with the $tptn_settings array
Expand Down Expand Up @@ -298,6 +299,7 @@ function get_tptn_pop_posts( $args = array() ) {
'daily' => false,
'strict_limit' => true,
'posts_only' => false,
'offset' => 0,
);

// Merge the $defaults array with the $tptn_settings array
Expand All @@ -313,6 +315,7 @@ function get_tptn_pop_posts( $args = array() ) {
}

$limit = ( $args['strict_limit'] ) ? $args['limit'] : ( $args['limit'] * 5 );
$offset = isset( $args['offset'] ) ? $args['offset'] : 0;

// If post_types is empty or contains a query string then use parse_str else consider it comma-separated.
if ( ! empty( $args['post_types'] ) && false === strpos( $args['post_types'], '=' ) ) {
Expand Down Expand Up @@ -396,7 +399,7 @@ function get_tptn_pop_posts( $args = array() ) {
$orderby = ' sum_count DESC ';

// Create the base LIMITS clause
$limits .= $wpdb->prepare( ' LIMIT %d ', $limit );
$limits .= $wpdb->prepare( ' LIMIT %d, %d ', $offset, $limit );

/**
* Filter the SELECT clause of the query.
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ add_filter( 'manage_edit-projects_sortable_columns', 'tptn_column_register_sorta

* Features:
* New tracker using a properly enqueued `.js` file. Two inbuilt options to use query variables or ajaxurl to process the counts
* Shortcode and the widget now have an added parameter for 'offset'. This is useful if you would like to display different widgets/shortcodes but not always start from the first post

* Bug fixes:
* Attachments now work with the widget and elsewhere
Expand Down

0 comments on commit ecc630d

Please sign in to comment.