| @@ -15,9 +15,9 @@ | ||
| 15 | 15 | exit; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | /** |
| 19 | - * Admin Columns Class. | |
| 19 | + * Displays the post view count in content and feeds. | |
| 20 | 20 | * |
| 21 | 21 | * @since 3.3.0 |
| 22 | 22 | */ |
| 23 | 23 | class Counter { |
| @@ -127,17 +127,33 @@ | ||
| 127 | 127 | $home_url = strtok( $home_url, '?' ); |
| 128 | 128 | |
| 129 | 129 | $id = intval( $post->ID ); |
| 130 | 130 | |
| 131 | - $nonce_action = 'tptn-nonce-' . $id; | |
| 132 | - $nonce = wp_create_nonce( $nonce_action ); | |
| 131 | + if ( \tptn_get_option( 'dynamic_post_count' ) ) { | |
| 132 | + $nonce_action = 'tptn-nonce-' . $id; | |
| 133 | + $nonce = wp_create_nonce( $nonce_action ); | |
| 133 | 134 | |
| 134 | - if ( \tptn_get_option( 'dynamic_post_count' ) ) { | |
| 135 | + $fetch_url = add_query_arg( | |
| 136 | + array( | |
| 137 | + 'top_ten_id' => $id, | |
| 138 | + 'view_counter' => 1, | |
| 139 | + '_wpnonce' => $nonce, | |
| 140 | + ), | |
| 141 | + $home_url | |
| 142 | + ); | |
| 143 | + | |
| 144 | + wp_enqueue_script( | |
| 145 | + 'tptn-counter', | |
| 146 | + plugins_url( 'includes/js/top-10-counter.min.js', TOP_TEN_PLUGIN_FILE ), | |
| 147 | + array(), | |
| 148 | + TOP_TEN_VERSION, | |
| 149 | + true | |
| 150 | + ); | |
| 151 | + | |
| 135 | 152 | $output = sprintf( |
| 136 | - '<div class="tptn_counter" id="tptn_counter_%1$d"><script type="text/javascript" data-cfasync="false" src="%2$s?top_ten_id=%1$d&view_counter=1&_wpnonce=%3$s"></script></div>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript | |
| 153 | + '<div class="tptn_counter" id="tptn_counter_%1$d" data-tptn-url="%2$s"></div>', | |
| 137 | 154 | $id, |
| 138 | - $home_url, | |
| 139 | - $nonce | |
| 155 | + esc_url( $fetch_url ) | |
| 140 | 156 | ); |
| 141 | 157 | } else { |
| 142 | 158 | $output = '<div class="tptn_counter" id="tptn_counter_' . $id . '">' . self::get_post_count( $id ) . '</div>'; |
| 143 | 159 | } |
| @@ -220,8 +236,15 @@ | ||
| 220 | 236 | } |
| 221 | 237 | } |
| 222 | 238 | } |
| 223 | 239 | |
| 240 | + /** | |
| 241 | + * Filters the formatted view count displayed for a post. | |
| 242 | + * | |
| 243 | + * @since 2.2.0 | |
| 244 | + * | |
| 245 | + * @param string $count_display The formatted count text. | |
| 246 | + */ | |
| 224 | 247 | return apply_filters( 'tptn_post_count', ( $is_zero_total_count && ! $is_singular ) ? $count_disp_form_zero : $count_disp_form ); |
| 225 | 248 | } |
| 226 | 249 | |
| 227 | 250 | /** |
| @@ -285,9 +308,9 @@ | ||
| 285 | 308 | $post_count = number_format_i18n( $post_count ); |
| 286 | 309 | } |
| 287 | 310 | |
| 288 | 311 | /** |
| 289 | - * Returns the post count. | |
| 312 | + * Filters the post count returned for a post. | |
| 290 | 313 | * |
| 291 | 314 | * @since 2.6.0 |
| 292 | 315 | * |
| 293 | 316 | * @param int|string $post_count Post count. |