-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathFilters.php
More file actions
122 lines (100 loc) · 3.23 KB
/
Filters.php
File metadata and controls
122 lines (100 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Plausible Analytics | Filters.
*
* @since 1.0.0
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace Plausible\Analytics\WP;
use WP_Term;
use Exception;
// Bailout, if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Filters {
/**
* Constructor.
*
* @since 1.0.0
* @access public
* @return void
*/
public function __construct() {
add_filter( 'script_loader_tag', [ $this, 'add_plausible_attributes' ], 10, 2 );
add_filter( 'plausible_analytics_script_params', [ $this, 'maybe_add_custom_params' ] );
}
/**
* Add Plausible Analytics attributes.
*
* @since 1.0.0
* @access public
*
* @param string $handle Script handle.
* @param string $tag Script tag.
*
* @return string
*/
public function add_plausible_attributes( $tag, $handle ) {
// Bail if it's not our script.
if ( 'plausible-analytics' !== $handle ) {
return $tag; // @codeCoverageIgnore
}
$settings = Helpers::get_settings();
$api_url = Helpers::get_data_api_url();
$domain_name = Helpers::get_domain();
// We need the correct id attribute for IE compatibility.
$tag = preg_replace( "/\sid=(['\"])plausible-analytics-js(['\"])/", " id=$1plausible$2", $tag );
/**
* the data-cfasync ensures this script isn't processed by CF Rocket Loader @see https://developers.cloudflare.com/speed/optimization/content/rocket-loader/ignore-javascripts/
*/
$params = "data-domain='{$domain_name}' data-api='{$api_url}' data-cfasync='false'";
// Triggered when exclude pages is enabled.
if ( ! empty( $settings[ 'excluded_pages' ] ) && $settings[ 'excluded_pages' ] ) {
$excluded_pages = $settings[ 'excluded_pages' ]; // @codeCoverageIgnore
$params .= " data-exclude='{$excluded_pages}'"; // @codeCoverageIgnore
}
$params = apply_filters( 'plausible_analytics_script_params', $params );
return str_replace( ' src', " {$params} src", $tag );
}
/**
* Adds custom parameters Author and Category if Custom Pageview Properties is enabled.
*
* @param $params
*
* @return mixed|void
*/
public function maybe_add_custom_params( $params ) {
$settings = Helpers::get_settings();
if ( ! is_array( $settings[ 'enhanced_measurements' ] ) || ! in_array( 'pageview-props', $settings[ 'enhanced_measurements' ] ) ) {
return $params; // @codeCoverageIgnore
}
global $post;
if ( ! $post instanceof \WP_Post ) {
return $params; // @codeCoverageIgnore
}
$author = $post->post_author;
if ( $author ) {
$author_name = get_the_author_meta( 'display_name', $author );
$params .= " event-author='$author_name'";
}
// Add support for post category and tags along with custom taxonomies.
$taxonomies = get_object_taxonomies( $post->post_type );
// Loop through existing taxonomies.
foreach ( $taxonomies as $taxonomy ) {
$terms = get_the_terms( $post->ID, $taxonomy );
// Skip the iteration, if `$terms` is not array.
if ( ! is_array( $terms ) ) {
continue; // @codeCoverageIgnore;
}
// Loop through the terms.
foreach ( $terms as $term ) {
if ( $term instanceof WP_Term ) {
$params .= " event-{$taxonomy}=\"{$term->name}\"";
}
}
}
return apply_filters( 'plausible_analytics_pageview_properties', $params );
}
}