-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1821 from WordPress/add/track-tag-method
Introduce `OD_Tag_Visitor_Context::track_tag()` method as alternative for returning `true` in tag visitor callback
- Loading branch information
Showing
9 changed files
with
197 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
plugins/optimization-detective/class-od-visited-tag-state.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Optimization Detective: OD_Visited_Tag_State class | ||
* | ||
* @package optimization-detective | ||
* @since n.e.x.t | ||
*/ | ||
|
||
// @codeCoverageIgnoreStart | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} | ||
// @codeCoverageIgnoreEnd | ||
|
||
/** | ||
* State for a tag visitation when visited by tag visitors while walking over a document. | ||
* | ||
* @since n.e.x.t | ||
* @access private | ||
*/ | ||
final class OD_Visited_Tag_State { | ||
|
||
/** | ||
* Whether the tag should be tracked among the elements in URL Metrics. | ||
* | ||
* @since n.e.x.t | ||
* @var bool | ||
*/ | ||
private $should_track_tag; | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
$this->reset(); | ||
} | ||
|
||
/** | ||
* Marks the tag for being tracked in URL Metrics. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public function track_tag(): void { | ||
$this->should_track_tag = true; | ||
} | ||
|
||
/** | ||
* Whether the tag should be tracked among the elements in URL Metrics. | ||
* | ||
* @since n.e.x.t | ||
* @return bool Whether tracked. | ||
*/ | ||
public function is_tag_tracked(): bool { | ||
return $this->should_track_tag; | ||
} | ||
|
||
/** | ||
* Resets state. | ||
* | ||
* This should be called after tag visitors have been invoked on a tag. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
public function reset(): void { | ||
$this->should_track_tag = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
plugins/optimization-detective/tests/test-cases/tag-track-opt-in/buffer.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>...</title> | ||
</head> | ||
<body> | ||
<div id="page"> | ||
<div id="not-tracked-by-any"></div> | ||
<div id="tracked-by-return-true"></div> | ||
<div id="tracked-by-track-tag-method"></div> | ||
<div id="tracked-by-return-value-and-track-tag-method"></div> | ||
</div> | ||
</body> | ||
</html> |
15 changes: 15 additions & 0 deletions
15
plugins/optimization-detective/tests/test-cases/tag-track-opt-in/expected.html
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
plugins/optimization-detective/tests/test-cases/tag-track-opt-in/set-up.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
return static function (): void { | ||
add_action( | ||
'od_register_tag_visitors', | ||
static function ( OD_Tag_Visitor_Registry $registry ): void { | ||
$registry->register( | ||
'not-tracking-anything-return-false', | ||
static function (): bool { | ||
return false; | ||
} | ||
); | ||
|
||
$registry->register( | ||
'not-tracking-anything-return-void', | ||
static function ( OD_Tag_Visitor_Context $context ): void {} | ||
); | ||
|
||
$registry->register( | ||
'track-by-return-true', | ||
static function ( OD_Tag_Visitor_Context $context ): bool { | ||
return in_array( | ||
$context->processor->get_attribute( 'id' ), | ||
array( | ||
'tracked-by-return-true', | ||
'tracked-by-return-value-and-track-tag-method', | ||
), | ||
true | ||
); | ||
} | ||
); | ||
|
||
$registry->register( | ||
'track-by-track-tag-method', | ||
static function ( OD_Tag_Visitor_Context $context ): void { | ||
$should_track = in_array( | ||
$context->processor->get_attribute( 'id' ), | ||
array( | ||
'tracked-by-track-tag-method', | ||
'tracked-by-return-value-and-track-tag-method', | ||
), | ||
true | ||
); | ||
if ( $should_track ) { | ||
$context->track_tag(); | ||
} | ||
} | ||
); | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters