diff --git a/css/dashboard.css b/css/dashboard.css index 3804b1a..e1cc0ef 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,15 +87,15 @@ overflow: hidden; } -#statify_dashboard .table.referrer, -#statify_dashboard .table.target { - flex: 1 0 50%; -} - #statify_dashboard .table.total { 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/inc/class-statify-dashboard.php b/inc/class-statify-dashboard.php index b45cbf3..9761a4f 100644 --- a/inc/class-statify-dashboard.php +++ b/inc/class-statify-dashboard.php @@ -301,8 +301,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 */ @@ -322,7 +323,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 @@ -332,15 +336,41 @@ 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, + '/?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", $current_date, + '/?s%', $limit ), ARRAY_A ); $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 ), @@ -349,16 +379,43 @@ 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, + '/?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", $current_date, $days_show, + '/?s%', $limit ), ARRAY_A ); $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 @@ -371,12 +428,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 ), ); diff --git a/inc/class-statify.php b/inc/class-statify.php index ed2f863..69dbfde 100644 --- a/inc/class-statify.php +++ b/inc/class-statify.php @@ -131,12 +131,33 @@ protected static function track( $referrer, $target ) { // Relative target URL. $target = user_trailingslashit( str_replace( home_url( '/', 'relative' ), '/', $target ) ); - /* Global vars */ + // Global vars. global $wp_rewrite; - // Trim target URL. - if ( $wp_rewrite->permalink_structure ) { - $target = wp_parse_url( $target, PHP_URL_PATH ); + // Maybe add trailing slash if that is no search query. + if ( ! self::is_search( $target ) ) { + $target = user_trailingslashit( $target ); + + // Trim target url. + if ( $wp_rewrite->permalink_structure ) { + $target = wp_parse_url( $target, PHP_URL_PATH ); + } + } else { + // Lowercase search query. + $target = strtolower( $target ); + + // Remove additional query parameters that might exist. + $target = preg_replace( '/^\/\?/', '', $target, -1, $count ); + + if ( 0 === $count ) { + // 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. @@ -180,6 +201,27 @@ public static function parse_date( $date ) { return date_i18n( get_option( 'date_format' ), strtotime( $date ) ); } + /** + * Checks URL path/query string for search. + * + * @param string $query_string String with query parameters. + * + * @since 2.0.0 + * + * @return bool + */ + 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 ); + + return isset( $query_params['s'] ) && ! empty( $query_params['s'] ); + } + /** * Check JavaScript tracking. * @@ -286,7 +328,7 @@ private static function is_bot() { */ protected 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/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 5b70a10..e70c27e 100644 --- a/views/widget-front.php +++ b/views/widget-front.php @@ -44,6 +44,17 @@ class_exists( 'Statify' ) || exit; +
+

+ + + + + + +
 
+
+

@@ -55,4 +66,6 @@ class_exists( 'Statify' ) || exit;
- +
+ +