Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
167 lines (149 loc) · 4.1 KB
/
Copy pathPlugin.php
File metadata and controls
167 lines (149 loc) · 4.1 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Plausible\Analytics\WP;
/**
* Loads and registers plugin functionality through WordPress hooks.
*
* @since 1.0.0
*/
final class Plugin {
/**
* Load @see Integrations()
*
* @return void
*
* @codeCoverageIgnore
*/
public function load_integrations() {
$this->load_service( Integrations::class );
}
/**
* Instantiates a service class without taking down the site when its file can't be loaded.
*
* WordPress' updater deletes the plugin directory before copying the new version into place, so a
* request landing in that window finds part of our source tree missing. Because
* @see register_services() runs on plugins_loaded for every request, an unguarded `new` turns that
* into a fatal error on the front end instead of a single disabled feature.
*
* Note that `::class` is resolved at compile time and doesn't trigger the autoloader, so passing a
* missing class here is safe.
*
* @since 2.6.1
*
* @param string $class_name Fully qualified class name.
*
* @return object|null Null when the class couldn't be loaded.
*/
private function load_service( $class_name ) {
if ( ! class_exists( $class_name ) ) {
$this->log_missing_service( $class_name );
return null;
}
return new $class_name();
}
/**
* Logs a service that couldn't be loaded by @see load_service().
*
* Only logged when WP_DEBUG is enabled: during a plugin update this is expected and transient, and
* we don't want to fill production logs with it.
*
* @since 2.6.1
*
* @param string $class_name Fully qualified class name.
*
* @return void
*
* @codeCoverageIgnore
*/
private function log_missing_service( $class_name ) {
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
return;
}
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( sprintf( 'Plausible Analytics: %s could not be loaded and was skipped.', $class_name ) );
}
/**
* Loads the plugin's translated strings.
*
* @since 1.0.0
* @access public
* @return void
*
* @codeCoverageIgnore
*/
public function load_plugin_textdomain() {
load_plugin_textdomain(
'plausible-analytics',
false,
dirname( plugin_basename( PLAUSIBLE_ANALYTICS_PLUGIN_FILE ) ) . '/languages/'
);
}
/**
* Load @see Admin\Provisioning()
*
* @return void
*
* @codeCoverageIgnore
*/
public function load_provisioning() {
$this->load_service( Admin\Provisioning::class );
$this->load_service( Admin\Provisioning\Integrations::class );
}
/**
* Load @see Admin\Settings\Page()
*
* @return void
*
* @codeCoverageIgnore
*/
public function load_settings() {
$this->load_service( Admin\Settings\Page::class );
}
/**
* Registers functionality with WordPress hooks.
*
* @since 1.0.0
* @access public
* @return void
*/
public function register() {
$this->setup();
// Register services used throughout the plugin. (WP Rocket runs at priority 10)
add_action( 'plugins_loaded', [ $this, 'register_services' ], 9 );
// Load text domain.
add_action( 'init', [ $this, 'load_plugin_textdomain' ], 1000 );
}
/**
* Register plugin (de)activation hooks and cron job.
*
* @return void
*/
public function setup() {
$this->load_service( Setup::class );
}
/**
* Registers the individual services of the plugin.
*
* @since 1.0.0
* @access public
* @return void
*/
public function register_services() {
if ( is_admin() ) {
add_action( 'init', [ $this, 'load_settings' ] );
add_action( 'init', [ $this, 'load_provisioning' ] );
$this->load_service( Admin\Upgrades::class );
$this->load_service( Admin\Filters::class );
$this->load_service( Admin\Actions::class );
$this->load_service( Admin\Module::class );
$this->load_service( Admin\PrivacyPolicy::class );
}
add_action( 'init', [ $this, 'load_integrations' ] );
$this->load_service( AdminBar::class );
$this->load_service( Assets::class );
$this->load_service( Ajax::class );
$this->load_service( Compatibility::class );
$this->load_service( InitOptions::class );
$this->load_service( Proxy::class );
$this->load_service( Verification::class );
}
}