Skip to content

Commit

Permalink
Get the number of views for a single route (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vthwang authored May 12, 2024
1 parent 4086089 commit 71dff31
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ Export JSON API for Next.js blog, with support for posts and projects.
5. Get all visits
- Method: GET
- Description: Retrieves the total number of visits across all routes.
- Sample Request: [Base URL]/visits
- Sample Requests:
- To retrieve the total visits for all routes: [Base URL]/visits.
- To retrieve visits for a specific route: [Base URL]/visits?route=/example-route.
- Response Example

```json
Expand Down
13 changes: 10 additions & 3 deletions admin/classes/class-wp-json-exporter-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function get_post( WP_REST_Request $request ): WP_REST_Response|WP_Error
/** Get Visits Data */
$route = '/posts/' . $slug;
$visit_count = $this->wpdb->get_var(
$this->wpdb->prepare( "SELECT count FROM {$this->table_name} WHERE route = %s", $route ) // phpcs:ignore
$this->wpdb->prepare( "SELECT count FROM $this->table_name WHERE route = %s", $route ) // phpcs:ignore
);
$visit_count = $visit_count ? (int) $visit_count : 0;

Expand Down Expand Up @@ -288,7 +288,7 @@ public function get_project( WP_REST_Request $request ): WP_REST_Response|WP_Err
/** Get Visits Data */
$route = '/projects/' . $slug;
$visit_count = $this->wpdb->get_var(
$this->wpdb->prepare( "SELECT count FROM {$this->table_name} WHERE route = %s", $route ) // phpcs:ignore
$this->wpdb->prepare( "SELECT count FROM $this->table_name WHERE route = %s", $route ) // phpcs:ignore
);
$visit_count = $visit_count ? (int) $visit_count : 0;
$data['visits'] = $visit_count;
Expand Down Expand Up @@ -436,7 +436,14 @@ private function get_adjacent_post_custom( string $current_post_date, string $po
* @return WP_REST_Response
*/
public function get_visits(): WP_REST_Response {
$visits = $this->wpdb->get_var( "SELECT SUM(count) FROM $this->table_name" ); // phpcs:ignore
// phpcs:ignore
$route = $_GET['route'] ?? null;

if ( $route ) {
$visits = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT count FROM $this->table_name WHERE route = %s", $route ) ); // phpcs:ignore
} else {
$visits = $this->wpdb->get_var( "SELECT SUM(count) FROM $this->table_name" ); // phpcs:ignore
}

$response = array(
'data' => (int) $visits,
Expand Down
4 changes: 2 additions & 2 deletions wp-json-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WordPress JSON Exporter
* Plugin URI: https://github.com/vthwang/wp-json-exporter
* Description: Customized for blog systems, this plugin enables the export of your WordPress posts and projects in JSON format.
* Version: 1.0.4
* Version: 1.0.5
* Requires at least: 6.5
* Text Domain: wp-json-exporter
* Author: Vincent Wang
Expand All @@ -19,7 +19,7 @@
}

if ( ! class_exists( 'WPJsonExporter' ) ) {
define( 'WP_JSON_EXPORTER_VERSION', '1.0.4' );
define( 'WP_JSON_EXPORTER_VERSION', '1.0.5' );
define( 'WP_JSON_EXPORTER_DIR', __DIR__ );
define( 'WP_JSON_EXPORTER_VISITS_TABLE', 'json_exporter_visits' );

Expand Down

0 comments on commit 71dff31

Please sign in to comment.