Skip to content

Commit e10785f

Browse files
Closes #7574: [Prealpha][Performance Monitoring] Need to have the page title in dashboard (PR #7583)
Co-authored-by: WordPressFan <ahmed@wp-media.me>
1 parent 4ab63f9 commit e10785f

File tree

9 files changed

+93
-31
lines changed

9 files changed

+93
-31
lines changed

assets/js/wpr-admin.js

Lines changed: 3 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/wpr-admin.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/wpr-admin.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/Engine/Admin/PerformanceMonitoring/AJAX/Controller.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,72 @@ public function add_new_page(): void {
6464
wp_send_json_error( $payload );
6565
}
6666

67-
$row_id = $this->manager->add_url_to_the_queue( $url, true );
68-
$row_data = $this->query->get_row_by_id( $row_id );
67+
$page_title = $this->get_page_title( $payload['message'] );
68+
69+
$row_id = $this->manager->add_url_to_the_queue(
70+
$url,
71+
true,
72+
[
73+
'title' => $page_title,
74+
]
75+
);
76+
77+
if ( empty( $row_id ) ) {
78+
wp_send_json_error(
79+
[
80+
'error' => true,
81+
'message' => esc_html__( 'Not valid inputs', 'rocket' ),
82+
]
83+
);
84+
}
85+
86+
$row_data = $this->query->get_row_by_id( (int) $row_id );
87+
88+
// Remove message from the response payload.
89+
unset( $payload['message'] );
6990

7091
$payload['id'] = $row_id;
7192
$payload['html'] = $this->generate( 'partials/performance-monitoring-row', $row_data );
7293

7394
wp_send_json_success( $payload );
7495
}
7596

97+
/**
98+
* Extracts and sanitizes the page title from the provided HTML string.
99+
*
100+
* This method attempts to find the <title> tag in the given HTML, decodes any HTML entities,
101+
* strips all tags, sanitizes the text, and then trims the title at common separators
102+
* (such as " | ", " - ", " – ", " » ") to return a clean, concise page title.
103+
*
104+
* @param string $html The HTML content from which to extract the page title.
105+
*
106+
* @return string The sanitized and trimmed page title, or an empty string if not found.
107+
*/
108+
public function get_page_title( string $html ): string {
109+
$title = '';
110+
111+
if ( empty( $html ) ) {
112+
return $title;
113+
}
114+
115+
// Extract title from title tag.
116+
if ( ! preg_match( '/<title[^>]*>(.*?)<\/title>/is', $html, $matches ) ) {
117+
return $title;
118+
}
119+
120+
// Clean up and sanitize the title.
121+
$title = html_entity_decode( trim( $matches[1] ), ENT_QUOTES, 'UTF-8' );
122+
123+
if ( empty( $title ) ) {
124+
return $title;
125+
}
126+
127+
$title = wp_strip_all_tags( $title );
128+
$title = sanitize_text_field( $title );
129+
130+
return $title;
131+
}
132+
76133
/**
77134
* Validates a given URL for performance monitoring eligibility.
78135
*
@@ -123,9 +180,15 @@ protected function get_url_validation_payload( string $url ): array {
123180
}
124181

125182
// Check if url is a valid url.
126-
$response = wp_remote_head( $url );
183+
$user_agent = 'WP Rocket/Fetch Page Buffer for Performance Monitoring Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
184+
$args = [
185+
'user-agent' => $user_agent,
186+
'timeout' => 60,
187+
];
127188

128-
if ( is_wp_error( $response ) ) {
189+
$response = wp_safe_remote_get( $url, $args );
190+
191+
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
129192
$payload['error'] = true;
130193
$payload['message'] = 'Url does not resolve to a valid page.';
131194

@@ -152,6 +215,9 @@ protected function get_url_validation_payload( string $url ): array {
152215

153216
// TODO: Check if page is cached.
154217

218+
// Fetch url body and send to payload.
219+
$payload['message'] = wp_remote_retrieve_body( $response );
220+
155221
return $payload;
156222
}
157223

inc/Engine/Common/Database/Queries/AbstractQuery.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,11 @@ public function get_on_submit_jobs( int $count = 100 ) {
144144
* @param string $job_id API job_id.
145145
* @param string $queue_name API Queue name.
146146
* @param bool $is_mobile if the request is for mobile page.
147+
* @param array $additional_details Additional details to be saved into DB.
147148
*
148149
* @return bool
149150
*/
150-
public function create_new_job( string $url, string $job_id = '', string $queue_name = '', bool $is_mobile = false ) {
151+
public function create_new_job( string $url, string $job_id = '', string $queue_name = '', bool $is_mobile = false, array $additional_details = [] ) {
151152
if ( ! self::$table_exists && ! $this->table_exists() ) {
152153
return false;
153154
}
@@ -162,6 +163,10 @@ public function create_new_job( string $url, string $job_id = '', string $queue_
162163
'last_accessed' => current_time( 'mysql', true ),
163164
];
164165

166+
if ( ! empty( $additional_details ) ) {
167+
$item = array_merge( $item, $additional_details );
168+
}
169+
165170
$result = $this->add_item( $item );
166171

167172
/**

inc/Engine/Common/JobManager/Managers/AbstractManager.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@ public function get_optimization_type(): string {
4646
*
4747
* @param string $url page URL.
4848
* @param bool $is_mobile page is for mobile.
49+
* @param array $additional_details Additional details to be saved into DB.
4950
*
50-
* @return mixed
51+
* @return bool|void
5152
*/
52-
public function add_url_to_the_queue( string $url, bool $is_mobile ) {
53+
public function add_url_to_the_queue( string $url, bool $is_mobile, array $additional_details = [] ) {
5354
if ( ! $this->is_allowed() ) {
5455
return;
5556
}
5657

5758
$row = $this->query->get_row( $url, (bool) $is_mobile );
5859

5960
if ( empty( $row ) ) {
60-
return $this->query->create_new_job( $url, '', '', $is_mobile );
61+
return $this->query->create_new_job( $url, '', '', $is_mobile, $additional_details );
6162
}
6263
$this->query->reset_job( (int) $row->id );
6364
}

src/js/global/ajax.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,11 +348,6 @@ document.addEventListener('DOMContentLoaded', function() {
348348
e.preventDefault();
349349
const pageUrl = $pageUrlInput.val().trim();
350350

351-
if (!pageUrl) {
352-
alert('No page address was added');
353-
return;
354-
}
355-
356351
if (!isValidUrl(pageUrl)) {
357352
alert('Please enter a valid URL with an extension');
358353
return;

src/js/global/fields.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ $(document).ready(function(){
421421
} );
422422
}
423423

424-
$('[data-wpr_onclick]').each(function() {
425-
$(this).attr('onclick', $(this).data('wpr_onclick'));
426-
$(this).removeAttr('data-wpr_onclick');
427-
});
424+
$(document).on( 'click', '.wpr-confirm-delete', function (e) {
425+
return confirm( $(this).data('wpr_confirm_msg') );
426+
} );
427+
428428
});

views/settings/partials/performance-monitoring-row.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@
6969
'label' => '',
7070
'url' => $data->delete_url(),
7171
'attributes' => [
72-
'class' => 'wpr-icon-trash',
73-
'title' => __( 'Delete', 'rocket' ),
74-
'aria-label' => __( 'Delete', 'rocket' ),
75-
'data-wpr_onclick' => 'return confirm("' . esc_js( __( 'Are you sure you want to delete this item?', 'rocket' ) ) . '")',
72+
'class' => 'wpr-icon-trash wpr-confirm-delete',
73+
'title' => __( 'Delete', 'rocket' ),
74+
'aria-label' => __( 'Delete', 'rocket' ),
75+
'data-wpr_confirm_msg' => esc_html__( 'Are you sure you want to delete this item?', 'rocket' ),
7676
],
7777
]
7878
);

0 commit comments

Comments
 (0)