From 66e28c32064d0f356449ee2daee2056472c93d73 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sat, 18 Mar 2023 14:02:45 +0100 Subject: [PATCH 01/13] Track internal search queries --- inc/class-statify-dashboard.php | 20 ++++++++++++++++++++ inc/class-statify-frontend.php | 18 +++++++++++++++--- views/widget-front.php | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index 5d0c78a..4850442 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -385,4 +385,24 @@ private static function _select_data() { return $data; } + + /** + * Parses a visit target and, if needed, modifies it for displaying. + * + * @param string $target URL target as displayed in widget. + * + * @return string + */ + public static function parse_target( $target ) { + // Modify for search strings. + if ( 0 === strpos( $target, '/?s=' ) ) { + $target = preg_replace( '/^\/\?s=/', '', $target ); + $target = urldecode( $target ); + return sprintf( /* translators: s = search term */ + __( 'Search query: ā€œ%sā€', 'statify' ), + $target + ); + } + return $target; + } } diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 2dda945..cdb8817 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -92,11 +92,23 @@ public static function track_visit( $is_snippet = false ) { } /* Relative target url */ - $data['target'] = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $target ) ); + $data['target'] = str_replace( home_url( '/', 'relative' ), '/', $target ); + // Maybe add trailing slash if that is no search query. + if ( 0 !== strpos( $data['target'], '/?s=' ) ) { + $data['target'] = user_trailingslashit( $data['target'] ); + } // Trim target url. if ( $wp_rewrite->permalink_structure ) { - $data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH ); + // Check if that is not a search. + if ( 0 !== strpos( $data['target'], '/?s=' ) ) { + $data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH ); + } + } + + // Convert search query to lowercase. + if ( 0 === strpos( $data['target'], '/?s=' ) ) { + $data['target'] = strtolower( $data['target'] ); } // Sanitize target url. @@ -250,7 +262,7 @@ private static function is_bot( $user_agent ) { */ private static function _is_internal() { // Skip for preview, 404 calls, feed, search, favicon and sitemap access. - return is_preview() || is_404() || is_feed() || is_search() + return is_preview() || is_404() || is_feed() || ( function_exists( 'is_favicon' ) && is_favicon() ) || '' !== get_query_var( 'sitemap' ) || '' !== get_query_var( 'sitemap-stylesheet' ); } diff --git a/views/widget-front.php b/views/widget-front.php index 679d1f5..75e9668 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -72,7 +72,7 @@ class_exists( 'Statify' ) || exit; - + From f07175a78e1787200ed9f6e485c4e8ffd07df02e Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sat, 18 Mar 2023 14:08:33 +0100 Subject: [PATCH 02/13] Better inline docs --- inc/class-statify-frontend.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index cdb8817..ceefd5e 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -100,8 +100,8 @@ public static function track_visit( $is_snippet = false ) { // Trim target url. if ( $wp_rewrite->permalink_structure ) { - // Check if that is not a search. if ( 0 !== strpos( $data['target'], '/?s=' ) ) { + // No search, so we only need the `PHP_URL_PATH`. $data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH ); } } From 6e17f40573de477c189a4843ce241e4fae1893c9 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sat, 18 Mar 2023 14:18:16 +0100 Subject: [PATCH 03/13] Add escape function to dashboard output again --- views/widget-front.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/widget-front.php b/views/widget-front.php index 75e9668..ccb964e 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -72,7 +72,7 @@ class_exists( 'Statify' ) || exit; - + From d83f4c1e348da1420a8e68327643022b3c2ee30a Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sun, 19 Mar 2023 12:26:35 +0100 Subject: [PATCH 04/13] Modifications from review suggestions --- inc/class-statify-dashboard.php | 2 +- inc/class-statify-frontend.php | 18 +++++++----------- inc/class-statify.php | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index 4850442..9667af7 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -395,7 +395,7 @@ private static function _select_data() { */ public static function parse_target( $target ) { // Modify for search strings. - if ( 0 === strpos( $target, '/?s=' ) ) { + if ( self::query_string_contains_search( $target ) ) { $target = preg_replace( '/^\/\?s=/', '', $target ); $target = urldecode( $target ); return sprintf( /* translators: s = search term */ diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index ceefd5e..0c20f85 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -91,23 +91,19 @@ public static function track_visit( $is_snippet = false ) { $data['referrer'] = esc_url_raw( $referrer, array( 'http', 'https' ) ); } - /* Relative target url */ + // Relative target url $data['target'] = str_replace( home_url( '/', 'relative' ), '/', $target ); + // Maybe add trailing slash if that is no search query. - if ( 0 !== strpos( $data['target'], '/?s=' ) ) { + if ( ! self::query_string_contains_search( $data['target'] ) ) { $data['target'] = user_trailingslashit( $data['target'] ); - } - // Trim target url. - if ( $wp_rewrite->permalink_structure ) { - if ( 0 !== strpos( $data['target'], '/?s=' ) ) { - // No search, so we only need the `PHP_URL_PATH`. + // Trim target url. + if ( $wp_rewrite->permalink_structure ) { $data['target'] = wp_parse_url( $data['target'], PHP_URL_PATH ); } - } - - // Convert search query to lowercase. - if ( 0 === strpos( $data['target'], '/?s=' ) ) { + } else { + // Lowercase search query. $data['target'] = strtolower( $data['target'] ); } diff --git a/inc/class-statify.php b/inc/class-statify.php index 3044090..afb5eee 100644 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -108,6 +108,23 @@ public static function parse_date( $date ) { return date_i18n( get_option( 'date_format' ), strtotime( $date ) ); } + /** + * Checks a string of query parameters for the `s` query param. + * + * @param string $query_string String with query parameters. + * + * @since 1.9.0 + * + * @return bool + */ + public static function query_string_contains_search( $query_string ) { + // Remove leading `/?`. + $query_string = preg_replace( '/^\/\?/', '', $query_string ); + wp_parse_str( $query_string, $query_params ); + + return isset( $query_params['s'] ) && ! empty( $query_params['s'] ); + } + /** * Check JavaScript tracking. * From a03fba11f9faf1e1b9a5cffed25333e1d31eb336 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sun, 19 Mar 2023 12:44:24 +0100 Subject: [PATCH 05/13] Check for additional query params on search queries and remove them --- inc/class-statify-frontend.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 0c20f85..1c640bb 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -105,6 +105,11 @@ public static function track_visit( $is_snippet = false ) { } else { // Lowercase search query. $data['target'] = strtolower( $data['target'] ); + + // Remove additional query parameters that might exist. + $data['target'] = preg_replace( '/^\/\?/', '', $data['target'] ); + wp_parse_str( $data['target'], $tmp ); + $data['target'] = "/?s={$tmp['s']}"; } // Sanitize target url. From a75a9e1291e3c176dec75e063715c398f6cdd865 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sun, 19 Mar 2023 12:59:00 +0100 Subject: [PATCH 06/13] Add section for search queries --- css/dashboard.css | 6 +----- inc/class-statify-dashboard.php | 27 ++++++++++++++++++++------- inc/class-statify-frontend.php | 2 +- views/widget-front.php | 25 +++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/css/dashboard.css b/css/dashboard.css index 177864a..7fd07b1 100644 --- a/css/dashboard.css +++ b/css/dashboard.css @@ -75,6 +75,7 @@ #statify_dashboard .table { padding-bottom: 12px; + flex: 1 0 50%; } #statify_dashboard .table p.sub { @@ -86,11 +87,6 @@ overflow: hidden; } -#statify_dashboard .table.referrer, -#statify_dashboard .table.target { - flex: 1 0 50%; -} - #statify_dashboard .table.total { flex: 0 0 100%; } diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index 9667af7..8c73a4e 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -333,7 +333,15 @@ private static function _select_data() { if ( $today ) { $data['target'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target NOT LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + $current_date, + $limit + ), + ARRAY_A + ); + $data['searches'] = $wpdb->get_results( + $wpdb->prepare( + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", $current_date, $limit ), @@ -350,7 +358,16 @@ private static function _select_data() { } else { $data['target'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target NOT LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + $current_date, + $days_show, + $limit + ), + ARRAY_A + ); + $data['searches'] = $wpdb->get_results( + $wpdb->prepare( + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", $current_date, $days_show, $limit @@ -397,11 +414,7 @@ public static function parse_target( $target ) { // Modify for search strings. if ( self::query_string_contains_search( $target ) ) { $target = preg_replace( '/^\/\?s=/', '', $target ); - $target = urldecode( $target ); - return sprintf( /* translators: s = search term */ - __( 'Search query: ā€œ%sā€', 'statify' ), - $target - ); + return urldecode( $target ); } return $target; } diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 1c640bb..397e9a0 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -91,7 +91,7 @@ public static function track_visit( $is_snippet = false ) { $data['referrer'] = esc_url_raw( $referrer, array( 'http', 'https' ) ); } - // Relative target url + // Relative target url. $data['target'] = str_replace( home_url( '/', 'relative' ), '/', $target ); // Maybe add trailing slash if that is no search query. diff --git a/views/widget-front.php b/views/widget-front.php index ccb964e..87e8a0d 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -82,6 +82,31 @@ class_exists( 'Statify' ) || exit; + +
+

+ +

+ +
+ + + + + + + +
+ + + + + +
+
+
+ +

From dcb6d8b39231bfd9f3a13770e6875bf1af8d75be Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Sun, 19 Mar 2023 13:05:30 +0100 Subject: [PATCH 07/13] Use SQL wildcards for SQL like --- inc/class-statify-dashboard.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index 8c73a4e..abd0f73 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -333,16 +333,18 @@ private static function _select_data() { if ( $today ) { $data['target'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target NOT LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target NOT LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", $current_date, + '/?s%', $limit ), ARRAY_A ); $data['searches'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", $current_date, + '/?s%', $limit ), ARRAY_A @@ -358,18 +360,20 @@ private static function _select_data() { } else { $data['target'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target NOT LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target NOT LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", $current_date, $days_show, + '/?s%', $limit ), ARRAY_A ); $data['searches'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target LIKE '/?s%' GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", $current_date, $days_show, + '/?s%', $limit ), ARRAY_A From b32e817ed91ef3a42ee9653c8c824f6443ba2ba9 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Mon, 20 Mar 2023 09:26:44 +0100 Subject: [PATCH 08/13] Update @since version --- inc/class-statify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/class-statify.php b/inc/class-statify.php index afb5eee..ad092a8 100644 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -113,7 +113,7 @@ public static function parse_date( $date ) { * * @param string $query_string String with query parameters. * - * @since 1.9.0 + * @since 2.0.0 * * @return bool */ From ebfba0ece91a0237e23e8eac2325ef70517c20b9 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Mon, 20 Mar 2023 09:30:29 +0100 Subject: [PATCH 09/13] Docs --- inc/class-statify-dashboard.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index abd0f73..98d4041 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -302,8 +302,9 @@ public static function get_stats( $force_refresh = false ) { /** * Get stats from DB * - * @since 0.1.0 - * @version 1.4.0 + * @since 0.1.0 + * @since 1.4.0 + * @since 2.0.0 Add search queries to `$data` array. * * @return array DB results */ From 5861540793435e9876e59679c369382229ef0b07 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Mon, 20 Mar 2023 10:03:21 +0100 Subject: [PATCH 10/13] Split SQL queries in multiple lines --- inc/class-statify-dashboard.php | 56 +++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index 98d4041..711a307 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -324,7 +324,10 @@ private static function _select_data() { $data = array( 'visits' => $wpdb->get_results( $wpdb->prepare( - "SELECT `created` as `date`, COUNT(`created`) as `count` FROM `$wpdb->statify` GROUP BY `created` ORDER BY `created` DESC LIMIT %d", + "SELECT `created` as `date`, COUNT(`created`) as `count` + FROM `$wpdb->statify` + GROUP BY `created` + ORDER BY `created` DESC LIMIT %d", $days_show ), ARRAY_A @@ -334,7 +337,11 @@ private static function _select_data() { if ( $today ) { $data['target'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target NOT LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` + FROM `$wpdb->statify` + WHERE created = %s AND target NOT LIKE %s + GROUP BY `target` + ORDER BY `count` DESC LIMIT %d", $current_date, '/?s%', $limit @@ -343,7 +350,11 @@ private static function _select_data() { ); $data['searches'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created = %s AND target LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` + FROM `$wpdb->statify` + WHERE created = %s AND target LIKE %s + GROUP BY `target` + ORDER BY `count` DESC LIMIT %d", $current_date, '/?s%', $limit @@ -352,7 +363,15 @@ private static function _select_data() { ); $data['referrer'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created = %s GROUP BY `host` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, + SUBSTRING_INDEX( + SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), + ':', 1 + ) as `host` + FROM `$wpdb->statify` + WHERE `referrer` != '' AND created = %s + GROUP BY `host` + ORDER BY `count` DESC LIMIT %d", $current_date, $limit ), @@ -361,7 +380,11 @@ private static function _select_data() { } else { $data['target'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target NOT LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` + FROM `$wpdb->statify` + WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target NOT LIKE %s + GROUP BY `target` + ORDER BY `count` DESC LIMIT %d", $current_date, $days_show, '/?s%', @@ -371,7 +394,11 @@ private static function _select_data() { ); $data['searches'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target LIKE %s GROUP BY `target` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`target`) as `count`, `target` as `url` + FROM `$wpdb->statify` + WHERE created > DATE_SUB(%s, INTERVAL %d DAY) AND target LIKE %s + GROUP BY `target` + ORDER BY `count` DESC LIMIT %d", $current_date, $days_show, '/?s%', @@ -381,7 +408,15 @@ private static function _select_data() { ); $data['referrer'] = $wpdb->get_results( $wpdb->prepare( - "SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, SUBSTRING_INDEX(SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), ':', 1) as `host` FROM `$wpdb->statify` WHERE `referrer` != '' AND created > DATE_SUB(%s, INTERVAL %d DAY) GROUP BY `host` ORDER BY `count` DESC LIMIT %d", + "SELECT COUNT(`referrer`) as `count`, `referrer` as `url`, + SUBSTRING_INDEX( + SUBSTRING_INDEX(TRIM(LEADING 'www.' FROM(TRIM(LEADING 'https://' FROM TRIM(LEADING 'http://' FROM TRIM(`referrer`))))), '/', 1), + ':', 1 + ) as `host` + FROM `$wpdb->statify` + WHERE `referrer` != '' AND created > DATE_SUB(%s, INTERVAL %d DAY) + GROUP BY `host` + ORDER BY `count` DESC LIMIT %d", $current_date, $days_show, $limit @@ -394,12 +429,15 @@ private static function _select_data() { $data['visit_totals'] = array( 'today' => $wpdb->get_var( $wpdb->prepare( - "SELECT COUNT(`created`) FROM `$wpdb->statify` WHERE created = %s", + "SELECT COUNT(`created`) + FROM `$wpdb->statify` + WHERE created = %s", $current_date ) ), 'since_beginning' => $wpdb->get_row( - "SELECT COUNT(`created`) AS `count`, MIN(`created`) AS `date` FROM `$wpdb->statify`", + "SELECT COUNT(`created`) AS `count`, MIN(`created`) AS `date` + FROM `$wpdb->statify`", ARRAY_A ), ); From 288541f6955381ef5fd906f04b7a655217e96701 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Mon, 20 Mar 2023 11:01:00 +0100 Subject: [PATCH 11/13] Switch searches display to JS --- inc/class-statify-dashboard.php | 16 ----------- inc/class-statify.php | 24 ++++++++++++---- js/dashboard.js | 19 +++++++++++++ package-lock.json | 1 - views/widget-front.php | 50 ++++++--------------------------- 5 files changed, 45 insertions(+), 65 deletions(-) diff --git a/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index 4912a48..9761a4f 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -444,20 +444,4 @@ private static function _select_data() { return $data; } - - /** - * Parses a visit target and, if needed, modifies it for displaying. - * - * @param string $target URL target as displayed in widget. - * - * @return string - */ - public static function parse_target( $target ) { - // Modify for search strings. - if ( self::query_string_contains_search( $target ) ) { - $target = preg_replace( '/^\/\?s=/', '', $target ); - return urldecode( $target ); - } - return $target; - } } diff --git a/inc/class-statify.php b/inc/class-statify.php index 76560ed..dfe0bdf 100644 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -135,7 +135,7 @@ protected static function track( $referrer, $target ) { global $wp_rewrite; // Maybe add trailing slash if that is no search query. - if ( ! self::query_string_contains_search( $target ) ) { + if ( ! self::is_search( $target ) ) { $target = user_trailingslashit( $target ); // Trim target url. @@ -147,9 +147,17 @@ protected static function track( $referrer, $target ) { $target = strtolower( $target ); // Remove additional query parameters that might exist. - $target = preg_replace( '/^\/\?/', '', $target ); - wp_parse_str( $target, $tmp ); - $target = "/?s={$tmp['s']}"; + $target = preg_replace( '/^\/\?/', '', $target, -1, $count ); + + if ( $count === 0 ) { + // If `/search/search query` is used, remove the `/search/` + $target = urldecode( preg_replace( '/^\/search\/(.*)\/$/', '$1', $target ) ); + } else { + // ?s= was used. + wp_parse_str( $target, $tmp ); + $target = urldecode( $tmp['s'] ); + } + $target = "/?s={$target}"; } // Init rows. @@ -194,7 +202,7 @@ public static function parse_date( $date ) { } /** - * Checks a string of query parameters for the `s` query param. + * Checks URL path/query string for search. * * @param string $query_string String with query parameters. * @@ -202,7 +210,11 @@ public static function parse_date( $date ) { * * @return bool */ - public static function query_string_contains_search( $query_string ) { + public static function is_search( $query_string ) { + if ( 1 === preg_match( '/^\/search\/.+/', $query_string ) ) { + return true; + } + // Remove leading `/?`. $query_string = preg_replace( '/^\/\?/', '', $query_string ); wp_parse_str( $query_string, $query_params ); diff --git a/js/dashboard.js b/js/dashboard.js index 39c6e11..1d31a2f 100644 --- a/js/dashboard.js +++ b/js/dashboard.js @@ -3,6 +3,7 @@ var chartElem = document.getElementById( 'statify_chart' ); var referrerTable = document.querySelector( '#statify_dashboard .table.referrer table tbody' ); var targetTable = document.querySelector( '#statify_dashboard .table.target table tbody' ); + var searchesTable = document.querySelector( '#statify_dashboard .table.searches table tbody' ); var totalsTable = document.querySelector( '#statify_dashboard .table.total table tbody' ); var refreshBtn = document.getElementById( 'statify_refresh' ); @@ -146,6 +147,24 @@ } } + if ( searchesTable ) { + rows = searchesTable.querySelectorAll( 'tr' ); + + data.searches.forEach( function( r, idx ) { + row = document.createElement( 'TR' ); + row.innerHTML = '' + r.count + '' + + '' + decodeURI( r.url.replace( '/?s=', '' ) ) + ''; + if ( rows.length > idx ) { + searchesTable.replaceChild( row, rows[idx] ); + } else { + searchesTable.appendChild( row ); + } + } ); + for ( i = data.searches.length; i < rows.length; i++ ) { + searchesTable.removeChild( rows[i] ); + } + } + if ( totalsTable ) { rows = totalsTable.querySelectorAll( 'tr' ); row = document.createElement( 'TR' ); diff --git a/package-lock.json b/package-lock.json index b94daf4..b64904f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,7 +4,6 @@ "requires": true, "packages": { "": { - "name": "statify", "devDependencies": { "@wordpress/eslint-plugin": "^14.2.0", "@wordpress/stylelint-config": "^21.12.0", diff --git a/views/widget-front.php b/views/widget-front.php index 502da89..a39ea08 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -34,56 +34,22 @@ class_exists( 'Statify' ) || exit;

-<<<<<<< HEAD -

- -

- -
- - - - - - +

+
- - - - - -
+ + + -
 
-
+ +
- -
-

- -

- -
- - - - - - -======= -

+

- - - - - -
->>>>>>> develop
 
From 88374937b55ed2fffbeeef424341ea9dcf0508f0 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Mon, 20 Mar 2023 11:06:54 +0100 Subject: [PATCH 12/13] Fix refresh button styling --- css/dashboard.css | 5 +++++ views/widget-front.php | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/css/dashboard.css b/css/dashboard.css index ed6ec29..e1cc0ef 100644 --- a/css/dashboard.css +++ b/css/dashboard.css @@ -91,6 +91,11 @@ flex: 0 0 100%; } +#statify_dashboard .statify-refresh-button-wrapper { + padding-top: 12px; + width: 100%; +} + #statify_dashboard td { padding: 8px 0 0; line-height: 1.4; diff --git a/views/widget-front.php b/views/widget-front.php index a39ea08..14eb706 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -66,4 +66,6 @@ class_exists( 'Statify' ) || exit;
- +
+ +
From 19208f0eceb955fab373a3d9570a346055fe4b77 Mon Sep 17 00:00:00 2001 From: Florian Brinkmann Date: Mon, 20 Mar 2023 11:09:26 +0100 Subject: [PATCH 13/13] Fix PHP CS things --- inc/class-statify.php | 4 ++-- views/widget-front.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/class-statify.php b/inc/class-statify.php index dfe0bdf..69dbfde 100644 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -149,8 +149,8 @@ protected static function track( $referrer, $target ) { // Remove additional query parameters that might exist. $target = preg_replace( '/^\/\?/', '', $target, -1, $count ); - if ( $count === 0 ) { - // If `/search/search query` is used, remove the `/search/` + if ( 0 === $count ) { + // If `/search/search query` is used, remove the `/search/`. $target = urldecode( preg_replace( '/^\/search\/(.*)\/$/', '$1', $target ) ); } else { // ?s= was used. diff --git a/views/widget-front.php b/views/widget-front.php index 14eb706..e70c27e 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -67,5 +67,5 @@ class_exists( 'Statify' ) || exit;
- -
+ +