Skip to content

Commit c63f37b

Browse files
committed
feat: add HTML comment generation for profiling data and improve parameter validation in REST API
1 parent ec89c6e commit c63f37b

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

inc/manager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ public function replace_content( $html, $partial = false ) {
432432
}
433433
if ( self::should_load_profiler( $partial ) ) {
434434
$profile_id = Profile::generate_id( $html );
435+
$should_show_comment = false;
435436
// We disable the optimizer for logged in users.
436437
if ( ! is_user_logged_in() || ! apply_filters( 'optml_force_page_profiler', false ) !== true ) {
437438
$js_optimizer = Optml_Admin::get_optimizer_script( false );
@@ -449,11 +450,16 @@ public function replace_content( $html, $partial = false ) {
449450
if ( ! headers_sent() ) {
450451
header( 'Cache-Control: max-age=300' ); // Attempt to cache the page just for 5 mins until the optimizer is done. Once the optimizer is done, the page will load optimized.
451452
}
453+
} else {
454+
$should_show_comment = isset( $_GET['optml_debug'] ) && $_GET['optml_debug'] === 'true';
452455
}
453456
}
454457

455458
Profile::set_current_profile_id( $profile_id );
456459
$this->page_profiler->set_current_profile_data();
460+
if ( $should_show_comment ) {
461+
$html = str_replace( '</html>', '</html>' . $this->page_profiler->get_current_profile_html_comment(), $html );
462+
}
457463
}
458464
if ( ! $partial ) {
459465
$html = $this->add_html_class( $html );

inc/rest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ public function optimizations( WP_REST_Request $request ) {
950950
if ( empty( $origin ) || ! is_allowed_http_origin( $origin ) ) {
951951
return $this->response( 'Invalid origin', 'error' );
952952
}
953-
if ( empty( $device_type ) || empty( $above_fold_images ) || empty( $url ) || ! is_array( $above_fold_images ) ) {
953+
if ( empty( $device_type ) || empty( $url ) || ! is_array( $above_fold_images ) ) {
954954
return $this->response( 'Missing required parameters', 'error' );
955955
}
956956
if ( count( $above_fold_images ) > 20 ) {

inc/v2/PageProfiler/Profile.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,4 +455,50 @@ public function check_data_availability(): bool {
455455

456456
return $has_data;
457457
}
458+
459+
/**
460+
* Generate HTML comment with profile data and performance metrics.
461+
*
462+
* @return string HTML comment with profile data and metrics.
463+
*/
464+
public function get_current_profile_html_comment(): string {
465+
$profile_data = self::get_current_profile_data();
466+
467+
if ( empty( $profile_data ) ) {
468+
return '<!-- plugin=optimole-wp: No profile ID available -->';
469+
}
470+
471+
// Format the HTML comment
472+
$comment_parts = [
473+
'plugin=optimole-wp',
474+
];
475+
476+
// Add device-specific metrics
477+
foreach ( $profile_data as $device_type => $device_data ) {
478+
$device_name = $this->get_device_name( $device_type );
479+
$comment_parts[] = 'measurement#device-' . $device_name . '#' . json_encode( $device_data );
480+
}
481+
482+
return '<!-- ' . implode( ' ', $comment_parts ) . ' -->';
483+
}
484+
485+
486+
/**
487+
* Get device name from device type constant.
488+
*
489+
* @param int $device_type The device type constant.
490+
* @return string Device name.
491+
*/
492+
private function get_device_name( int $device_type ): string {
493+
switch ( $device_type ) {
494+
case self::DEVICE_TYPE_MOBILE:
495+
return 'mobile';
496+
case self::DEVICE_TYPE_DESKTOP:
497+
return 'desktop';
498+
case self::DEVICE_TYPE_GLOBAL:
499+
return 'global';
500+
default:
501+
return 'unknown';
502+
}
503+
}
458504
}

0 commit comments

Comments
 (0)