Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
ETH Web Vitals
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP Plugins
ETH Web Vitals
Commits
600a2677
Commit
600a2677
authored
5 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
Simplify
parent
1b8abeb7
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
classes/class-web-vitals.php
+0
-57
0 additions, 57 deletions
classes/class-web-vitals.php
classes/trait-singleton.php
+0
-78
0 additions, 78 deletions
classes/trait-singleton.php
pmc-performance-metrics.php
+23
-5
23 additions, 5 deletions
pmc-performance-metrics.php
with
23 additions
and
140 deletions
classes/class-web-vitals.php
deleted
100644 → 0
+
0
−
57
View file @
1b8abeb7
<?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
}
}
This diff is collapsed.
Click to expand it.
classes/trait-singleton.php
deleted
100644 → 0
+
0
−
78
View file @
1b8abeb7
<?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
];
}
}
This diff is collapsed.
Click to expand it.
pmc-performance-metrics.php
+
23
−
5
View file @
600a2677
...
@@ -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
);
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment