Skip to content

Commit 74f7396

Browse files
authored
Merge pull request elementor#884 from WP2Static/feature/fix-code-quality-issues
WIP fix code quality issues
2 parents 8990204 + 0bc4d3a commit 74f7396

19 files changed

+548
-168
lines changed

phpstan.neon

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,23 @@ parameters:
2525
count: 25
2626
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
2727
path: src/ViewRenderer.php
28-
count: 32
29-
- message: '#^In method "WP2Static\\\S+::\S+", you should not use the \$_(GET|POST) superglobal#'
30-
path: src/WordPressAdmin.php
28+
count: 10
29+
- message: "#^Cannot access property \\$value on mixed\\.$#"
30+
path: views/options-page.php
31+
count: 4
32+
- message: "#^Cannot access property \\$name on mixed\\.$#"
33+
path: views/options-page.php
3134
count: 2
35+
- message: "#^Cannot access property \\$time on string\\.$#"
36+
path: views/logs-page.php
37+
count: 1
38+
- message: "#^Cannot access property \\$log on string\\.$#"
39+
path: views/logs-page.php
40+
count: 1
41+
- message: "#^Cannot access property \\$\\S+ on mixed\\.$#"
42+
path: views/jobs-page.php
43+
count: 12
44+
- message: "#^Cannot access property \\$\\S+ on mixed\\.$#"
45+
path: views/addons-page.php
46+
count: 7
3247
- '/^Parameter #2 \$callable of static method WP_CLI::add_command\(\) expects callable\(\): mixed, \S+ given\.$/'
33-

src/CrawlCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static function rmUrl( string $url ) : void {
158158
/**
159159
* Remove multiple URLs at once
160160
*
161-
* @param array<int> $ids
161+
* @param array<string> $ids
162162
* @return void
163163
*/
164164
public static function rmUrlsById( array $ids ) : void {

src/Paginator.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ public function records() : array {
8080
return $this->records;
8181
}
8282

83-
public function render() : void {
84-
require __DIR__ . '/../views/_paginator.php';
85-
}
86-
8783
/**
8884
* Extract only the records for our current page.q
8985
* Because array_slice doesn't preserve integer keys, we need to split

src/ViewRenderer.php

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,37 @@ public static function renderCrawlQueue() : void {
6464
die( 'Forbidden' );
6565
}
6666

67-
if ( ! empty( $_GET['action'] ) && ! empty( $_GET['id'] ) && is_array( $_GET['id'] ) ) {
68-
switch ( $_GET['action'] ) {
69-
case 'remove':
70-
CrawlQueue::rmUrlsById( $_GET['id'] );
71-
break;
72-
}
67+
$action = filter_input( INPUT_GET, 'action' );
68+
/**
69+
* @var string[] $url_id
70+
*/
71+
$url_id = filter_input( INPUT_GET, 'id' );
72+
73+
if ( $action === 'remove' && is_array( $url_id ) ) {
74+
CrawlQueue::rmUrlsById( $url_id );
7375
}
7476

7577
$urls = CrawlQueue::getCrawlablePaths();
7678
// Apply search
77-
if ( ! empty( $_GET['s'] ) ) {
78-
$s = $_GET['s'];
79+
$search_term = strval( filter_input( INPUT_GET, 's' ) );
80+
if ( $search_term !== '' ) {
7981
$urls = array_filter(
8082
$urls,
81-
function ( $url ) use ( $s ) {
82-
return stripos( $url, $s ) !== false;
83+
function ( $url ) use ( $search_term ) {
84+
return stripos( $url, $search_term ) !== false;
8385
}
8486
);
8587
}
8688

8789
$page_size = 200;
8890
$page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
91+
$paginator = new Paginator( $urls, $page_size, $page );
8992
$view = [
90-
'paginator' => new Paginator( $urls, $page_size, $page ),
93+
'paginatorFirstPage' => $paginator->firstPage(),
94+
'paginatorLastPage' => $paginator->lastPage(),
95+
'paginatorPage' => $paginator->page(),
96+
'paginatorRecords' => $paginator->records(),
97+
'paginatorTotalRecords' => $paginator->totalRecords(),
9198
];
9299

93100
require_once WP2STATIC_PATH . 'views/crawl-queue-page.php';
@@ -99,31 +106,37 @@ public static function renderCrawlCache() : void {
99106
die( 'Forbidden' );
100107
}
101108

102-
if ( ! empty( $_GET['action'] ) && ! empty( $_GET['id'] ) && is_array( $_GET['id'] ) ) {
103-
switch ( $_GET['action'] ) {
104-
case 'remove':
105-
CrawlCache::rmUrlsById( $_GET['id'] );
106-
break;
107-
}
109+
$action = filter_input( INPUT_GET, 'action' );
110+
/**
111+
* @var string[] $url_id
112+
*/
113+
$url_id = filter_input( INPUT_GET, 'id' );
114+
115+
if ( $action === 'remove' && is_array( $url_id ) ) {
116+
CrawlCache::rmUrlsById( $url_id );
108117
}
109118

110119
$urls = CrawlCache::getURLs();
111-
112120
// Apply search
113-
if ( ! empty( $_GET['s'] ) ) {
114-
$s = $_GET['s'];
121+
$search_term = strval( filter_input( INPUT_GET, 's' ) );
122+
if ( $search_term !== '' ) {
115123
$urls = array_filter(
116124
$urls,
117-
function ( $url ) use ( $s ) {
118-
return stripos( isset( $url->url ) ? $url->url : '', $s ) !== false;
125+
function ( $url ) use ( $search_term ) {
126+
return stripos( $url->url ?? '', $search_term ) !== false;
119127
}
120128
);
121129
}
122130

123131
$page_size = 200;
124132
$page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
133+
$paginator = new Paginator( $urls, $page_size, $page );
125134
$view = [
126-
'paginator' => new Paginator( $urls, $page_size, $page ),
135+
'paginatorFirstPage' => $paginator->firstPage(),
136+
'paginatorLastPage' => $paginator->lastPage(),
137+
'paginatorPage' => $paginator->page(),
138+
'paginatorRecords' => $paginator->records(),
139+
'paginatorTotalRecords' => $paginator->totalRecords(),
127140
];
128141

129142
require_once WP2STATIC_PATH . 'views/crawl-cache-page.php';
@@ -138,20 +151,25 @@ public static function renderPostProcessedSitePaths() : void {
138151
$paths = ProcessedSite::getPaths();
139152

140153
// Apply search
141-
if ( ! empty( $_GET['s'] ) ) {
142-
$s = $_GET['s'];
154+
$search_term = strval( filter_input( INPUT_GET, 's' ) );
155+
if ( $search_term !== '' ) {
143156
$paths = array_filter(
144157
$paths,
145-
function ( $path ) use ( $s ) {
146-
return stripos( $path, $s ) !== false;
158+
function ( $path ) use ( $search_term ) {
159+
return stripos( $path, $search_term ) !== false;
147160
}
148161
);
149162
}
150163

151164
$page_size = 200;
152165
$page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
166+
$paginator = new Paginator( $paths, $page_size, $page );
153167
$view = [
154-
'paginator' => new Paginator( $paths, $page_size, $page ),
168+
'paginatorFirstPage' => $paginator->firstPage(),
169+
'paginatorLastPage' => $paginator->lastPage(),
170+
'paginatorPage' => $paginator->page(),
171+
'paginatorRecords' => $paginator->records(),
172+
'paginatorTotalRecords' => $paginator->totalRecords(),
155173
];
156174

157175
require_once WP2STATIC_PATH . 'views/post-processed-site-paths-page.php';
@@ -166,20 +184,25 @@ public static function renderStaticSitePaths() : void {
166184
$paths = StaticSite::getPaths();
167185

168186
// Apply search
169-
if ( ! empty( $_GET['s'] ) ) {
170-
$s = $_GET['s'];
187+
$search_term = strval( filter_input( INPUT_GET, 's' ) );
188+
if ( $search_term !== '' ) {
171189
$paths = array_filter(
172190
$paths,
173-
function ( $path ) use ( $s ) {
174-
return stripos( $path, $s ) !== false;
191+
function ( $path ) use ( $search_term ) {
192+
return stripos( $path, $search_term ) !== false;
175193
}
176194
);
177195
}
178196

179197
$page_size = 200;
180198
$page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
199+
$paginator = new Paginator( $paths, $page_size, $page );
181200
$view = [
182-
'paginator' => new Paginator( $paths, $page_size, $page ),
201+
'paginatorFirstPage' => $paginator->firstPage(),
202+
'paginatorLastPage' => $paginator->lastPage(),
203+
'paginatorPage' => $paginator->page(),
204+
'paginatorRecords' => $paginator->records(),
205+
'paginatorTotalRecords' => $paginator->totalRecords(),
183206
];
184207

185208
require_once WP2STATIC_PATH . 'views/static-site-paths-page.php';
@@ -191,25 +214,31 @@ public static function renderDeployCache() : void {
191214
die( 'Forbidden' );
192215
}
193216

194-
$paths = isset( $_GET['deploy_namespace'] )
195-
? DeployCache::getPaths( $_GET['deploy_namespace'] )
217+
$deploy_namespace = strval( filter_input( INPUT_GET, 'deploy_namespace' ) );
218+
$paths = $deploy_namespace !== ''
219+
? DeployCache::getPaths( $deploy_namespace )
196220
: DeployCache::getPaths();
197221

198222
// Apply search
199-
if ( ! empty( $_GET['s'] ) ) {
200-
$s = $_GET['s'];
223+
$search_term = strval( filter_input( INPUT_GET, 's' ) );
224+
if ( $search_term !== '' ) {
201225
$paths = array_filter(
202226
$paths,
203-
function ( $path ) use ( $s ) {
204-
return stripos( $path, $s ) !== false;
227+
function ( $path ) use ( $search_term ) {
228+
return stripos( $path, $search_term ) !== false;
205229
}
206230
);
207231
}
208232

209233
$page_size = 200;
210234
$page = isset( $_GET['paged'] ) ? max( 1, intval( $_GET['paged'] ) ) : 1;
235+
$paginator = new Paginator( $paths, $page_size, $page );
211236
$view = [
212-
'paginator' => new Paginator( $paths, $page_size, $page ),
237+
'paginatorFirstPage' => $paginator->firstPage(),
238+
'paginatorLastPage' => $paginator->lastPage(),
239+
'paginatorPage' => $paginator->page(),
240+
'paginatorRecords' => $paginator->records(),
241+
'paginatorTotalRecords' => $paginator->totalRecords(),
213242
];
214243

215244
require_once WP2STATIC_PATH . 'views/deploy-cache-page.php';
@@ -262,6 +291,9 @@ public static function renderCachesPage() : void {
262291
);
263292

264293
foreach ( $files as $file ) {
294+
/**
295+
* @var \SplFileInfo $file
296+
*/
265297
$disk_space += $file->getSize();
266298
}
267299
}
@@ -294,6 +326,9 @@ public static function renderCachesPage() : void {
294326
);
295327

296328
foreach ( $files as $file ) {
329+
/**
330+
* @var \SplFileInfo $file
331+
*/
297332
$disk_space += $file->getSize();
298333
}
299334
}

src/WPCron.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,14 @@ public static function wp2static_cron_with_http_basic_auth( array $cron_request
8686
return $cron_request;
8787
}
8888

89-
$headers = [
89+
$auth_headers = [
9090
'Authorization' =>
9191
sprintf( 'Basic %s', base64_encode( $auth_user . ':' . $auth_password ) ),
9292
];
9393

94-
$cron_request['args']['headers'] =
95-
isset( $cron_request['args']['headers'] ) ?
96-
array_merge( $cron_request['args']['headers'], $headers ) :
97-
$headers;
94+
$cron_request_args = (array) ( $cron_request['args'] ?? [] );
95+
$cron_request_headers = (array) ( $cron_request_args['headers'] ?? [] );
9896

99-
return $cron_request;
97+
return array_merge( $cron_request_headers, $auth_headers );
10098
}
10199
}

src/WordPressAdmin.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,15 @@ public static function addAdminUIElements() : void {
335335
* Do security checks before calling Controller::wp2staticProcessQueue
336336
*/
337337
public static function adminPostProcessQueue() : void {
338-
$method = $_SERVER['REQUEST_METHOD'];
339-
if ( 'POST' !== $method ) {
338+
$method = filter_input( INPUT_SERVER, 'REQUEST_METHOD' );
339+
if ( ! $method ) {
340+
$msg = 'Empty method in request to admin-post.php (wp2static_process_queue)';
341+
} elseif ( 'POST' !== $method ) {
342+
$method = strval( $method );
340343
$msg = "Invalid method in request to admin-post.php (wp2static_process_queue): $method";
341344
}
342-
343-
$nonce = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : false;
344-
$nonce_valid = $nonce && wp_verify_nonce( $nonce, 'wp2static_process_queue' );
345+
$nonce = filter_input( INPUT_POST, '_wpnonce' );
346+
$nonce_valid = $nonce && wp_verify_nonce( strval( $nonce ), 'wp2static_process_queue' );
345347
if ( ! $nonce_valid ) {
346348
$msg = 'Invalid nonce in request to admin-post.php (wpstatic_process_queue)';
347349
}

views/_paginator.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

views/addons-page.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
/**
66
* @var mixed[] $view
77
*/
8+
9+
/**
10+
* @var mixed[] $addons
11+
*/
12+
$addons = $view['addons'];
813
?>
914

1015
<div class="wrap">
@@ -21,22 +26,22 @@
2126
</tr>
2227
</thead>
2328
<tbody>
24-
<?php if ( ! $view['addons'] ) : ?>
29+
<?php if ( ! $addons ) : ?>
2530
<tr>
2631
<td colspan="4">No addons are installed. <a href="https://wp2static.com/download">Get Add-Ons</a></td>
2732
</tr>
2833
<?php endif; ?>
2934

3035

31-
<?php foreach ( $view['addons'] as $addon ) : ?>
36+
<?php foreach ( $addons as $addon ) : ?>
3237
<tr>
3338
<td>
3439
<form
3540
name="wp2static-crawl-queue-delete"
3641
method="POST"
3742
action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
3843

39-
<?php wp_nonce_field( $view['nonce_action'] ); ?>
44+
<?php wp_nonce_field( strval( $view['nonce_action'] ) ); ?>
4045
<input name="action" type="hidden" value="wp2static_toggle_addon" />
4146
<input name="addon_slug" type="hidden" value="<?php echo $addon->slug; ?>" />
4247

0 commit comments

Comments
 (0)