Skip to content

Commit 29b7ba2

Browse files
committed
Merge branch 'jdv/search-2' of github.com:jdevalk/plausible into track_search_events
2 parents 6048052 + a9f975a commit 29b7ba2

File tree

2 files changed

+399
-0
lines changed

2 files changed

+399
-0
lines changed

src/Includes/Actions.php

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* Plausible Analytics | Actions.
4+
*
5+
* @since 1.0.0
6+
*
7+
* @package WordPress
8+
* @subpackage Plausible Analytics
9+
*/
10+
11+
namespace Plausible\Analytics\WP\Includes;
12+
13+
use Plausible\Analytics\WP\Includes\Helpers;
14+
15+
// Bailout, if accessed directly.
16+
if ( ! defined( 'ABSPATH' ) ) {
17+
exit;
18+
}
19+
20+
class Actions {
21+
22+
/**
23+
* Constructor.
24+
*
25+
* @since 1.0.0
26+
* @access public
27+
*
28+
* @return void
29+
*/
30+
public function __construct() {
31+
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_register_assets' ] );
32+
add_action( 'admin_bar_menu', [ $this, 'admin_bar_node' ], 100 );
33+
}
34+
35+
/**
36+
* Register Assets.
37+
*
38+
* @since 1.0.0
39+
* @access public
40+
*
41+
* @return void
42+
*/
43+
public function maybe_register_assets() {
44+
$settings = Helpers::get_settings();
45+
$user_role = Helpers::get_user_role();
46+
47+
/**
48+
* Bail if tracked_user_roles is empty (which means no roles should be tracked) or,
49+
* if current role should not be tracked.
50+
*/
51+
if (
52+
( ! empty( $user_role )
53+
&& ! isset( $settings['tracked_user_roles'] ) )
54+
|| ( ! empty( $user_role )
55+
&& ! in_array( $user_role, $settings['tracked_user_roles'], true ) )
56+
) {
57+
return;
58+
}
59+
60+
wp_enqueue_script( 'plausible-analytics', Helpers::get_analytics_url(), '', PLAUSIBLE_ANALYTICS_VERSION );
61+
62+
// Goal tracking inline script (Don't disable this as it is required by 404).
63+
wp_add_inline_script( 'plausible-analytics', 'window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }' );
64+
65+
// Track 404 pages.
66+
if ( apply_filters( 'plausible_analytics_enable_404', true ) && is_404() ) {
67+
wp_add_inline_script( 'plausible-analytics', 'plausible("404",{ props: { path: document.location.pathname } });' );
68+
}
69+
70+
// Track search results. Tracks a search event with the search term and the number of results, and a pageview with the site's search URL.
71+
if ( apply_filters( 'plausible_analytics_track_search', true ) && is_search() ) {
72+
$search_url = str_replace( '%search%', '', get_site_url( null, $GLOBALS['wp_rewrite']->get_search_permastruct() ) );
73+
$data = 'plausible("pageview", { u: "' . esc_attr( $search_url ) . '" });' .
74+
'plausible( \'Search\', {props: {keyword: \'' . get_search_query() . '\', resultCount: ' . intval( $GLOBALS['wp_query']->found_posts ) . '}});';
75+
wp_add_inline_script( 'plausible-analytics', $data );
76+
}
77+
}
78+
79+
/**
80+
* Create admin bar nodes.
81+
*
82+
* @param \WP_Admin_Bar $admin_bar Admin bar object.
83+
*
84+
* @return void
85+
* @since 1.3.0
86+
* @access public
87+
*/
88+
public function admin_bar_node( $admin_bar ) {
89+
$disable = ! empty( Helpers::get_settings()['disable_toolbar_menu'][0] );
90+
91+
if ( $disable ) {
92+
return;
93+
}
94+
95+
// Add main admin bar node.
96+
$args = [
97+
'id' => 'plausible-admin-bar',
98+
'title' => 'Plausible Analytics',
99+
];
100+
$admin_bar->add_node( $args );
101+
102+
// Add link to view all stats.
103+
$args = [];
104+
$args[] = [
105+
'id' => 'view-analytics',
106+
'title' => esc_html__( 'View Analytics', 'plausible-analytics' ),
107+
'href' => admin_url( 'index.php?page=plausible_analytics_statistics' ),
108+
'parent' => 'plausible-admin-bar',
109+
];
110+
111+
// Add link to individual page stats.
112+
if ( is_singular() ) {
113+
global $post;
114+
$args[] = [
115+
'id' => 'view-page-analytics',
116+
'title' => esc_html__( 'View Page Analytics', 'plausible-analytics' ),
117+
'href' => add_query_arg( 'page-url', is_home() ? '' : trailingslashit( urlencode( '/' . $post->post_name ) ), admin_url( 'index.php?page=plausible_analytics_statistics' ) ),
118+
'parent' => 'plausible-admin-bar',
119+
];
120+
}
121+
122+
// Add link to Plausible Settings page.
123+
$args[] = [
124+
'id' => 'settings',
125+
'title' => esc_html__( 'Settings', 'plausible-analytics' ),
126+
'href' => admin_url( 'options-general.php?page=plausible_analytics' ),
127+
'parent' => 'plausible-admin-bar',
128+
];
129+
foreach ( $args as $arg ) {
130+
$admin_bar->add_node( $arg );
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)