diff --git a/classes/class-web-vitals.php b/classes/class-web-vitals.php deleted file mode 100644 index 513a81b3db335d318fa4b9fc974c875b4205e603..0000000000000000000000000000000000000000 --- a/classes/class-web-vitals.php +++ /dev/null @@ -1,57 +0,0 @@ -<?php -/** - * Google's Web Vitals measurements. - * - * @link https://github.com/GoogleChrome/web-vitals - * - * @package eth-web-vitals - */ - -namespace ETH\Web_Vitals; - -/** - * Class Web_Vitals. - */ -class Web_Vitals { - use Singleton; - - /** - * Reporting category for analytics provider. - */ - protected const EVENT_CATEGORY = 'Web Vitals'; - - /** - * Output priority. - * - * Load early to capture as soon as possible. - */ - protected const OUTPUT_PRIORITY = 0; - - /** - * Web_Vitals constructor. - */ - protected function __construct() { - add_action( 'wp_head', [ $this, 'render' ], static::OUTPUT_PRIORITY ); - } - - /** - * Output the Web Vitals script. - */ - public function render(): void { - $data = [ - 'config' => [ - 'eventCategory' => static::EVENT_CATEGORY, - ], - 'queue' => [], - ]; - - ?> - <script> - window.ethWebVitals = window.ethWebVitals || {}; - window.ethWebVitals = <?php echo wp_json_encode( $data ); ?>; - - <?php echo file_get_contents( PLUGIN_PATH . 'assets/build/web-vitals.js' ); ?> - </script> - <?php - } -} diff --git a/classes/trait-singleton.php b/classes/trait-singleton.php deleted file mode 100644 index bbbb0283c73a180a9d9da944c3f2fa42f814f542..0000000000000000000000000000000000000000 --- a/classes/trait-singleton.php +++ /dev/null @@ -1,78 +0,0 @@ -<?php -/** - * Singleton trait which implements Singleton pattern in any class in which this trait is used. - * - * Dependent items can use the `pmc_singleton_init_{$called_class}` hook to - * execute code immediately after _init() is called. - * - * Using the singleton pattern in WordPress is an easy way to protect against - * mistakes caused by creating multiple objects or multiple initialization - * of classes which need to be initialized only once. - * - * With complex plugins, there are many cases where multiple copies of - * the plugin would load, and action hooks would load (and trigger) multiple - * times. - * - * If you're planning on using a global variable, then you should implement - * this trait. Singletons are a way to safely use globals; they let you - * access and set the global from anywhere, without risk of collision. - * - * If any method in a class needs to be aware of "state", then you should - * implement this trait in that class. - * - * If any method in the class need to "talk" to another or be aware of what - * another method has done, then you should implement this trait in that class. - * - * If you specifically need multiple objects, then use a normal class. - * - * If you're unsure, ask in engineering chat. - * - * @since 2017-06-19 Amit Gupta - */ - -namespace ETH\Web_Vitals; - -trait Singleton { - protected static $_instance = array(); - - /** - * Protected class constructor to prevent direct object creation - * - * This is meant to be overridden in the classes which implement - * this trait. This is ideal for doing stuff that you only want to - * do once, such as hooking into actions and filters, etc. - */ - protected function __construct() { } - - /** - * Prevent object cloning - */ - final protected function __clone() { } - - /** - * This method returns new or existing Singleton instance - * of the class for which it is called. This method is set - * as final intentionally, it is not meant to be overridden. - * - * @return object|static Singleton instance of the class. - */ - final public static function get_instance() { - /** - * If this trait is implemented in a class which has multiple - * sub-classes then static::$_instance will be overwritten with the most recent - * sub-class instance. Thanks to late static binding - * we use get_called_class() to grab the called class name, and store - * a key=>value pair for each `classname => instance` in self::$_instance - * for each sub-class. - */ - $called_class = get_called_class(); - - if ( ! isset( static::$_instance[ $called_class ] ) ) { - static::$_instance[ $called_class ] = new $called_class(); - } - - return static::$_instance[ $called_class ]; - - } - -} diff --git a/pmc-performance-metrics.php b/pmc-performance-metrics.php index 4ebff0dd5e5af31a7347e1c4e7c62e7fd98dcfea..c520492155c24bb4ae6e41b81b38d7629e123988 100644 --- a/pmc-performance-metrics.php +++ b/pmc-performance-metrics.php @@ -4,14 +4,32 @@ * Plugin URI: https://ethitter.com * Description: Record performance metrics in analytics platforms. * Version: 1.0 - * License: PMC Proprietary. All rights reserved. - * Author: PMC + * License: GPLv2 + * Author: Erick Hitter */ namespace ETH\Web_Vitals; define( 'ETH\\Web_Vitals\\PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); -require_once __DIR__ . '/classes/trait-singleton.php'; -require_once __DIR__ . '/classes/class-web-vitals.php'; -Web_Vitals::get_instance(); +/** + * Output the Web Vitals script. + */ +function render(): void { + $data = [ + 'config' => [ + 'eventCategory' => 'Web Vitals', + ], + 'queue' => [], + ]; + + ?> + <script> + window.ethWebVitals = window.ethWebVitals || {}; + window.ethWebVitals = <?php echo wp_json_encode( $data ); ?>; + <?php echo file_get_contents( PLUGIN_PATH . 'assets/build/web-vitals.js' ); ?> + </script> + <?php +} + +add_action( 'wp_head', __NAMESPACE__ . '\render', 0 );