| 1 |
<?php |
| 2 |
|
| 3 |
namespace Plausible\Analytics\WP; |
| 4 |
|
| 5 |
/** |
| 6 |
* Loads and registers plugin functionality through WordPress hooks. |
| 7 |
* |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
final class Plugin { |
| 11 |
/** |
| 12 |
* Load @see Integrations() |
| 13 |
* |
| 14 |
* @return void |
| 15 |
* |
| 16 |
* @codeCoverageIgnore |
| 17 |
*/ |
| 18 |
public function load_integrations() { |
| 19 |
$this->load_service( Integrations::class ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Instantiates a service class without taking down the site when its file can't be loaded. |
| 24 |
* |
| 25 |
* WordPress' updater deletes the plugin directory before copying the new version into place, so a |
| 26 |
* request landing in that window finds part of our source tree missing. Because |
| 27 |
* @see register_services() runs on plugins_loaded for every request, an unguarded `new` turns that |
| 28 |
* into a fatal error on the front end instead of a single disabled feature. |
| 29 |
* |
| 30 |
* Note that `::class` is resolved at compile time and doesn't trigger the autoloader, so passing a |
| 31 |
* missing class here is safe. |
| 32 |
* |
| 33 |
* @since 2.6.1 |
| 34 |
* |
| 35 |
* @param string $class_name Fully qualified class name. |
| 36 |
* |
| 37 |
* @return object|null Null when the class couldn't be loaded. |
| 38 |
*/ |
| 39 |
private function load_service( $class_name ) { |
| 40 |
if ( ! class_exists( $class_name ) ) { |
| 41 |
$this->log_missing_service( $class_name ); |
| 42 |
|
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
return new $class_name(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Logs a service that couldn't be loaded by @see load_service(). |
| 51 |
* |
| 52 |
* Only logged when WP_DEBUG is enabled: during a plugin update this is expected and transient, and |
| 53 |
* we don't want to fill production logs with it. |
| 54 |
* |
| 55 |
* @since 2.6.1 |
| 56 |
* |
| 57 |
* @param string $class_name Fully qualified class name. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
* |
| 61 |
* @codeCoverageIgnore |
| 62 |
*/ |
| 63 |
private function log_missing_service( $class_name ) { |
| 64 |
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 69 |
error_log( sprintf( 'Plausible Analytics: %s could not be loaded and was skipped.', $class_name ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Loads the plugin's translated strings. |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* @access public |
| 77 |
* @return void |
| 78 |
* |
| 79 |
* @codeCoverageIgnore |
| 80 |
*/ |
| 81 |
public function load_plugin_textdomain() { |
| 82 |
load_plugin_textdomain( |
| 83 |
'plausible-analytics', |
| 84 |
false, |
| 85 |
dirname( plugin_basename( PLAUSIBLE_ANALYTICS_PLUGIN_FILE ) ) . '/languages/' |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Load @see Admin\Provisioning() |
| 91 |
* |
| 92 |
* @return void |
| 93 |
* |
| 94 |
* @codeCoverageIgnore |
| 95 |
*/ |
| 96 |
public function load_provisioning() { |
| 97 |
$this->load_service( Admin\Provisioning::class ); |
| 98 |
$this->load_service( Admin\Provisioning\Integrations::class ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Load @see Admin\Settings\Page() |
| 103 |
* |
| 104 |
* @return void |
| 105 |
* |
| 106 |
* @codeCoverageIgnore |
| 107 |
*/ |
| 108 |
public function load_settings() { |
| 109 |
$this->load_service( Admin\Settings\Page::class ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Registers functionality with WordPress hooks. |
| 114 |
* |
| 115 |
* @since 1.0.0 |
| 116 |
* @access public |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
public function register() { |
| 120 |
$this->setup(); |
| 121 |
|
| 122 |
// Register services used throughout the plugin. (WP Rocket runs at priority 10) |
| 123 |
add_action( 'plugins_loaded', [ $this, 'register_services' ], 9 ); |
| 124 |
|
| 125 |
// Load text domain. |
| 126 |
add_action( 'init', [ $this, 'load_plugin_textdomain' ], 1000 ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Register plugin (de)activation hooks and cron job. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function setup() { |
| 135 |
$this->load_service( Setup::class ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Registers the individual services of the plugin. |
| 140 |
* |
| 141 |
* @since 1.0.0 |
| 142 |
* @access public |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function register_services() { |
| 146 |
if ( is_admin() ) { |
| 147 |
add_action( 'init', [ $this, 'load_settings' ] ); |
| 148 |
add_action( 'init', [ $this, 'load_provisioning' ] ); |
| 149 |
|
| 150 |
$this->load_service( Admin\Upgrades::class ); |
| 151 |
$this->load_service( Admin\Filters::class ); |
| 152 |
$this->load_service( Admin\Actions::class ); |
| 153 |
$this->load_service( Admin\Module::class ); |
| 154 |
$this->load_service( Admin\PrivacyPolicy::class ); |
| 155 |
} |
| 156 |
|
| 157 |
add_action( 'init', [ $this, 'load_integrations' ] ); |
| 158 |
|
| 159 |
$this->load_service( AdminBar::class ); |
| 160 |
$this->load_service( Assets::class ); |
| 161 |
$this->load_service( Ajax::class ); |
| 162 |
$this->load_service( Compatibility::class ); |
| 163 |
$this->load_service( InitOptions::class ); |
| 164 |
$this->load_service( Proxy::class ); |
| 165 |
$this->load_service( Verification::class ); |
| 166 |
} |
| 167 |
} |
| 168 |
|