Skip to content
Snippets Groups Projects
Commit 600a2677 authored by Erick Hitter's avatar Erick Hitter
Browse files

Simplify

parent 1b8abeb7
No related branches found
No related tags found
No related merge requests found
<?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
}
}
<?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 ];
}
}
...@@ -4,14 +4,32 @@ ...@@ -4,14 +4,32 @@
* Plugin URI: https://ethitter.com * Plugin URI: https://ethitter.com
* Description: Record performance metrics in analytics platforms. * Description: Record performance metrics in analytics platforms.
* Version: 1.0 * Version: 1.0
* License: PMC Proprietary. All rights reserved. * License: GPLv2
* Author: PMC * Author: Erick Hitter
*/ */
namespace ETH\Web_Vitals; namespace ETH\Web_Vitals;
define( 'ETH\\Web_Vitals\\PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); 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'; * Output the Web Vitals script.
Web_Vitals::get_instance(); */
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 );
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment