@@ -186,26 +186,28 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor {
186
186
private $ bookmarked_open_stacks = array ();
187
187
188
188
/**
189
- * XPath for the current tag.
189
+ * Stored XPath for the current tag.
190
190
*
191
- * This is used so that repeated calls to {@see self::get_xpath()} won't needlessly reconstruct the string. This
192
- * gets cleared whenever {@see self::open_tags()} iterates to the next tag.
191
+ * This is used so that repeated calls to {@see self::get_stored_xpath()} won't needlessly reconstruct the string.
192
+ * This gets cleared whenever {@see self::open_tags()} iterates to the next tag.
193
+ *
194
+ * @todo Remove this once the XPath transitional period is over.
193
195
*
194
196
* @since 0.4.0
195
197
* @var string|null
196
198
*/
197
- private $ current_xpath = null ;
199
+ private $ current_stored_xpath = null ;
198
200
199
201
/**
200
- * Transitional XPath for the current tag.
202
+ * ( Transitional) XPath for the current tag.
201
203
*
202
204
* This is used to store the old XPath format in a transitional period until which new URL Metrics are expected to
203
205
* have been collected to purge out references to the old format.
204
206
*
205
207
* @since n.e.x.t
206
208
* @var string|null
207
209
*/
208
- private $ transitional_current_xpath = null ;
210
+ private $ current_xpath = null ;
209
211
210
212
/**
211
213
* Whether the previous tag does not expect a closer.
@@ -312,8 +314,8 @@ public function expects_closer( ?string $tag_name = null ): bool {
312
314
* @return bool Whether a token was parsed.
313
315
*/
314
316
public function next_token (): bool {
315
- $ this ->current_xpath = null ; // Clear cache.
316
- $ this ->transitional_current_xpath = null ; // Clear cache.
317
+ $ this ->current_stored_xpath = null ; // Clear cache.
318
+ $ this ->current_xpath = null ; // Clear cache.
317
319
++$ this ->cursor_move_count ;
318
320
if ( ! parent ::next_token () ) {
319
321
$ this ->open_stack_tags = array ();
@@ -639,40 +641,55 @@ private function is_foreign_element(): bool {
639
641
* index of the preceding node set. So it has to rather be written `.../*[1][self::DIV]/*[2][self::DIV]`.
640
642
* Note that the first three levels lack any node index whereas the third level includes a disambiguating
641
643
* attribute predicate (e.g. `/HTML/BODY/DIV[@id="page"]`) for the reasons explained in {@see self::XPATH_PATTERN}.
644
+ * This predicate will be included once the transitional period is over.
642
645
*
643
646
* @since 0.4.0
647
+ * @todo Replace the logic herein with what is in get_stored_xpath() once the transitional period is over.
644
648
*
645
- * @param bool $transitional_format Whether to use the transitional XPath format. Default true.
646
649
* @return string XPath.
647
650
*/
648
- public function get_xpath ( bool $ transitional_format = true ): string {
651
+ public function get_xpath (): string {
649
652
/*
650
653
* This transitional format is used by default for all extensions. The non-transitional format is used only in
651
654
* od_optimize_template_output_buffer() when setting the data-od-xpath attribute. This is so that the new format
652
655
* will replace the old format as new URL Metrics are collected. After a month of the new format being live, the
653
656
* transitional format can be eliminated. See the corresponding logic in OD_Element for normalizing both the
654
657
* old and new XPath formats to use the transitional format.
655
658
*/
656
- if ( $ transitional_format ) {
657
- if ( null === $ this ->transitional_current_xpath ) {
658
- $ this ->transitional_current_xpath = '' ;
659
- foreach ( $ this ->get_indexed_breadcrumbs () as $ i => list ( $ tag_name , $ index , $ attributes ) ) {
660
- if ( $ i < 2 || ( 2 === $ i && '/HTML/BODY ' === $ this ->transitional_current_xpath ) ) {
661
- $ this ->transitional_current_xpath .= "/ $ tag_name " ;
662
- } else {
663
- $ this ->transitional_current_xpath .= sprintf ( '/*[%d][self::%s] ' , $ index + 1 , $ tag_name );
664
- }
659
+ if ( null === $ this ->current_xpath ) {
660
+ $ this ->current_xpath = '' ;
661
+ foreach ( $ this ->get_indexed_breadcrumbs () as $ i => list ( $ tag_name , $ index , $ attributes ) ) {
662
+ if ( $ i < 2 || ( 2 === $ i && '/HTML/BODY ' === $ this ->current_xpath ) ) {
663
+ $ this ->current_xpath .= "/ $ tag_name " ;
664
+ } else {
665
+ $ this ->current_xpath .= sprintf ( '/*[%d][self::%s] ' , $ index + 1 , $ tag_name );
665
666
}
666
667
}
667
- return $ this ->transitional_current_xpath ;
668
668
}
669
+ return $ this ->current_xpath ;
670
+ }
669
671
670
- if ( null === $ this ->current_xpath ) {
671
- $ this ->current_xpath = '' ;
672
+ /**
673
+ * Gets stored XPath for the current open tag.
674
+ *
675
+ * This method is temporary for a transition period while new URL Metrics are collected for active installs. Once
676
+ * the transition period is over, the logic in this method can be moved to {@see self::get_xpath()} and this method
677
+ * can simply be an alias for that one. See related logic in {@see OD_Element::get_xpath()}. This function is only
678
+ * used internally by Optimization Detective in {@see od_optimize_template_output_buffer()}.
679
+ *
680
+ * @since n.e.x.t
681
+ * @todo Move the logic in this method to the get_xpath() method and let this be an alias for that method once the transitional period is over.
682
+ * @access private
683
+ *
684
+ * @return string XPath.
685
+ */
686
+ public function get_stored_xpath (): string {
687
+ if ( null === $ this ->current_stored_xpath ) {
688
+ $ this ->current_stored_xpath = '' ;
672
689
foreach ( $ this ->get_indexed_breadcrumbs () as $ i => list ( $ tag_name , $ index , $ attributes ) ) {
673
690
if ( $ i < 2 ) {
674
- $ this ->current_xpath .= "/ $ tag_name " ;
675
- } elseif ( 2 === $ i && '/HTML/BODY ' === $ this ->current_xpath ) {
691
+ $ this ->current_stored_xpath .= "/ $ tag_name " ;
692
+ } elseif ( 2 === $ i && '/HTML/BODY ' === $ this ->current_stored_xpath ) {
676
693
$ segment = "/ $ tag_name " ;
677
694
foreach ( $ attributes as $ attribute_name => $ attribute_value ) {
678
695
$ segment .= sprintf (
@@ -681,13 +698,13 @@ public function get_xpath( bool $transitional_format = true ): string {
681
698
$ attribute_value // Note: $attribute_value has already been validated to only contain safe characters /^[a-zA-Z0-9_.\s:-]*/ which do not need escaping.
682
699
);
683
700
}
684
- $ this ->current_xpath .= $ segment ;
701
+ $ this ->current_stored_xpath .= $ segment ;
685
702
} else {
686
- $ this ->current_xpath .= sprintf ( '/*[%d][self::%s] ' , $ index + 1 , $ tag_name );
703
+ $ this ->current_stored_xpath .= sprintf ( '/*[%d][self::%s] ' , $ index + 1 , $ tag_name );
687
704
}
688
705
}
689
706
}
690
- return $ this ->current_xpath ;
707
+ return $ this ->current_stored_xpath ;
691
708
}
692
709
693
710
/**
0 commit comments