Skip to content

Commit

Permalink
Shortcode changes
Browse files Browse the repository at this point in the history
Fixes #148

* Shortcode `tptn_views` takes two new parameters: `format_number` to apply `number_format_i18n` and `post_id` which takes a post ID. Default is the post being viewed
  • Loading branch information
ajaydsouza committed Aug 19, 2023
1 parent 7f5405e commit b7089a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
23 changes: 16 additions & 7 deletions includes/class-counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,10 @@ public static function get_post_count( $post = 0, $blog_id = 0 ) {
* @param int|\WP_Post $post Post ID or WP_Post object.
* @param string $counter Which count to return? total, daily or overall.
* @param int $blog_id Blog ID.
* @return int Post count
* @param array $args Additional arguments.
* @return int|string Post count
*/
public static function get_post_count_only( $post = 0, $counter = 'total', $blog_id = 0 ) {
public static function get_post_count_only( $post = 0, $counter = 'total', $blog_id = 0, $args = array() ) {
global $wpdb;

if ( $post instanceof \WP_Post ) {
Expand All @@ -239,6 +240,11 @@ public static function get_post_count_only( $post = 0, $counter = 'total', $blog
$id = absint( $post );
}

$defaults = array(
'format_number' => false,
);
$args = wp_parse_args( $args, $defaults );

$table_name = Helpers::get_tptn_table( false );
$table_name_daily = Helpers::get_tptn_table( true );

Expand All @@ -263,18 +269,21 @@ public static function get_post_count_only( $post = 0, $counter = 'total', $blog
}

$post_count = $resultscount ? $resultscount->visits : 0;
if ( $args['format_number'] ) {
$post_count = number_format_i18n( $post_count );
}

/**
* Returns the post count.
*
* @since 2.6.0
*
* @param int $post_count Formatted post count.
* @param mixed $id Post ID.
* @param string $counter Which count to return? total, daily or overall.
* @param int $blog_id Blog ID.
* @param int|string $post_count Post count.
* @param mixed $id Post ID.
* @param string $counter Which count to return? total, daily or overall.
* @param int $blog_id Blog ID.
*/
return apply_filters( 'tptn_post_count_only', (int) $post_count, $id, $counter, $blog_id );
return apply_filters( 'tptn_post_count_only', $post_count, $id, $counter, $blog_id );
} else {
return 0;
}
Expand Down
12 changes: 7 additions & 5 deletions includes/frontend/class-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,24 @@ public static function tptn_list( $atts, $content = null ) { // phpcs:ignore Gen
/**
* Creates a shortcode [tptn_views daily="0"].
*
* @param array $atts Shortcode attributes.
* @param string $content Content.
* @param array $atts Shortcode attributes.
* @param string $content Content.
* @return int Views of the post
*/
public static function tptn_views( $atts, $content = null ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
$a = shortcode_atts(
array(
'daily' => '0',
'count' => 'total',
'daily' => '0',
'count' => 'total',
'format_number' => true,
'post_id' => get_the_ID(),
),
$atts
);

// If daily is explicitly set to 1, then pass daily, else pass count.
$count = $a['daily'] ? 'daily' : $a['count'];

return \WebberZone\Top_Ten\Counter::get_post_count_only( get_the_ID(), $count );
return \WebberZone\Top_Ten\Counter::get_post_count_only( $a['post_id'], $count, 0, array( 'format_number' => $a['format_number'] ) );
}
}
10 changes: 7 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ Top 10 is one of the many plugins developed by WebberZone. Check out our other p

= 3.3.2 =

* Fix: Plugin activator function didn't run on activation - Top 10's database tables were not created
Release post: [https://webberzone.com/announcements/top-10-v3-3-0/](https://webberzone.com/announcements/top-10-v3-3-0/)

* Enhancements:
* Shortcode `tptn_views` takes two new parameters: `format_number` to apply `number_format_i18n` and `post_id` which takes a post ID. Default is the post being viewed

* Bug fixes:
* Plugin activator function didn't run on activation - Top 10's database tables were not created

= 3.3.1 =

Expand All @@ -163,8 +169,6 @@ Top 10 is one of the many plugins developed by WebberZone. Check out our other p

= 3.3.0 =

Release post: [https://webberzone.com/announcements/top-10-v3-3-0/](https://webberzone.com/announcements/top-10-v3-3-0/)

* Features:
* Added new setting to stop tracking bots. Top 10 now includes a comprehensive set of bot user agents via https://github.com/janusman/robot-user-agents

Expand Down

0 comments on commit b7089a9

Please sign in to comment.