Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track internal search queries #242

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions inc/class-statify-dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=' ) ) {
florianbrinkmann marked this conversation as resolved.
Show resolved Hide resolved
$target = preg_replace( '/^\/\?s=/', '', $target );
$target = urldecode( $target );
return sprintf( /* translators: s = search term */
__( 'Search query: “%s”', 'statify' ),
florianbrinkmann marked this conversation as resolved.
Show resolved Hide resolved
$target
);
}
return $target;
}
}
18 changes: 15 additions & 3 deletions inc/class-statify-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
if ( 0 !== strpos( $data['target'], '/?s=' ) ) {
florianbrinkmann marked this conversation as resolved.
Show resolved Hide resolved
// No search, so we only need the `PHP_URL_PATH`.
$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'] );
florianbrinkmann marked this conversation as resolved.
Show resolved Hide resolved
}

// Sanitize target url.
Expand Down Expand Up @@ -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' );
}
Expand Down
2 changes: 1 addition & 1 deletion views/widget-front.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class_exists( 'Statify' ) || exit;
</td>
<td class="t">
<a href="<?php echo esc_url( home_url( $target['url'] ) ); ?>" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $target['url'] ); ?>
<?php echo esc_html( Statify_Dashboard::parse_target( $target['url'] ) ); ?>
</a>
</td>
</tr>
Expand Down