Skip to content

Commit

Permalink
change the way we add rocket_head to be inside rocket_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Feb 18, 2025
1 parent e572c35 commit fcd2721
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
7 changes: 0 additions & 7 deletions inc/Engine/Optimization/Buffer/Optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ public function maybe_process_buffer( $buffer ) {
return $buffer;
}

$filtered_buffer = preg_replace(
'#</title>#iU',
'</title>' . wpm_apply_filters_typed( 'string', 'rocket_head', '' ),
$filtered_buffer,
1
);

$this->log( 'Page optimized.', [], 'info' );

/**
Expand Down
26 changes: 26 additions & 0 deletions inc/Engine/Optimization/Buffer/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct( Optimization $optimizer ) {
public static function get_subscribed_events() {
return [
'template_redirect' => [ 'start_content_process', 2 ],
'rocket_buffer' => [ 'insert_rocket_head', 100000 ],
];
}

Expand All @@ -44,4 +45,29 @@ public static function get_subscribed_events() {
public function start_content_process() {
return $this->optimizer->maybe_init_process();
}

/**
* Insert rocket_head into the buffer HTML
*
* @param string $html Buffer HTML.
* @return string
*/
public function insert_rocket_head( $html ) {
if ( empty( $html ) ) {
return $html;
}

$filtered_buffer = preg_replace(
'#</title>#iU',
'</title>' . wpm_apply_filters_typed( 'string', 'rocket_head', '' ),
$html,
1
);

if ( empty( $filtered_buffer ) ) {
return $html;
}

return $filtered_buffer;
}
}

0 comments on commit fcd2721

Please sign in to comment.