PluginProbe
Plausible Analytics / trunk
Plausible Analytics vtrunk
2.6.1 2.6.0 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 52 releases
plausible-analytics / src / EnhancedMeasurements.php

EnhancedMeasurements.php in Plausible Analytics trunk, at src/EnhancedMeasurements.php

97 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Plausible\Analytics\WP;
4
5 /**
6 * This class behaves like an enum, which we can't really support until WP drops support for PHP 8.0 and lower.
7 *
8 * @codeCoverageIgnore
9 */
10 final class EnhancedMeasurements {
11 const FOUR_O_FOUR = '404';
12
13 const FILE_DOWNLOADS = 'file-downloads';
14
15 const OUTBOUND_LINKS = 'outbound-links';
16
17 /** @var string Legacy since v2.6.0 because it moved to its own settings section. */
18 const CLOAKED_AFFILIATE_LINKS = 'affiliate-links';
19
20 const PAGEVIEW_PROPS = 'pageview-props';
21
22 const ECOMMERCE_REVENUE = 'revenue';
23
24 const FORM_COMPLETIONS = 'form-completions';
25
26 const LOGGED_IN_USER_STATUS = 'user-logged-in';
27
28 /** @var string Legacy since v2.6.0 because it moved to its own settings section. */
29 const QUERY_PARAMS = 'query-params';
30
31 const SEARCH_QUERIES = 'search';
32
33 const HASH_BASED_ROUTING = 'hash';
34
35 /**
36 * Source of Truth for Enhanced Measurements.
37 */
38 private const AVAILABLE_OPTIONS = [
39 self::FOUR_O_FOUR,
40 self::FILE_DOWNLOADS,
41 self::OUTBOUND_LINKS,
42 self::CLOAKED_AFFILIATE_LINKS,
43 self::PAGEVIEW_PROPS,
44 self::ECOMMERCE_REVENUE,
45 self::FORM_COMPLETIONS,
46 self::LOGGED_IN_USER_STATUS,
47 self::QUERY_PARAMS,
48 self::SEARCH_QUERIES,
49 self::HASH_BASED_ROUTING,
50 ];
51
52 /**
53 * Check if a certain Enhanced Measurement is enabled.
54 *
55 * @TODO: Refactor $name to enum (introduced in PHP 8.1) when WordPress drops support for PHP 8.0 and lower.
56 *
57 * @param string $name Name of the option to check, valid values are defined in @var self::AVAILABLE_OPTIONS
58 * @param array $enhanced_measurements Allows checking against a different set of options.
59 *
60 * @return bool
61 */
62 public static function is_enabled( $name, $enhanced_measurements = [] ) {
63 self::is_valid( $name );
64
65 if ( empty( $enhanced_measurements ) ) {
66 $settings = Helpers::get_settings();
67
68 $enhanced_measurements = $settings['enhanced_measurements'] ?? [];
69 }
70
71 if ( ! is_array( $enhanced_measurements ) ) {
72 return false; // @codeCoverageIgnore
73 }
74
75 return apply_filters( 'plausible_analytics_enhanced_measurements_is_enabled', in_array( $name, $enhanced_measurements ) );
76 }
77
78 /**
79 * Validate if requested Enhanced Measurement is valid.
80 *
81 * @param $name
82 *
83 * @return void
84 */
85 private static function is_valid( $name ) {
86 if ( ! in_array( $name, self::AVAILABLE_OPTIONS, true ) ) {
87 throw new \InvalidArgumentException(
88 sprintf(
89 'Invalid enhanced measurement "%s". Allowed values: %s',
90 $name,
91 implode( ', ', self::AVAILABLE_OPTIONS )
92 )
93 );
94 }
95 }
96 }
97