-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathinfo.php
More file actions
433 lines (397 loc) · 11.9 KB
/
info.php
File metadata and controls
433 lines (397 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
/**
* Packages Admin Information.
*
* @package FAIR
*/
namespace FAIR\Packages\Admin\Info;
use FAIR\Packages;
use FAIR\Packages\Admin;
use FAIR\Packages\MetadataDocument;
use FAIR\Packages\ReleaseDocument;
/**
* Sanitize HTML content for plugin information.
*
* @param string $html The HTML content to sanitize.
* @return string Sanitized HTML content.
*/
function sanitize_html( string $html ) : string {
static $allowed = [
'a' => [
'href' => [],
'title' => [],
'target' => [],
],
'abbr' => [
'title' => [],
],
'acronym' => [
'title' => [],
],
'code' => [],
'pre' => [],
'em' => [],
'strong' => [],
'div' => [
'class' => [],
],
'span' => [
'class' => [],
],
'p' => [],
'br' => [],
'ul' => [],
'ol' => [],
'li' => [],
'h1' => [],
'h2' => [],
'h3' => [],
'h4' => [],
'h5' => [],
'h6' => [],
'img' => [
'src' => [],
'class' => [],
'alt' => [],
],
'blockquote' => [
'cite' => true,
],
];
return wp_kses( $html, $allowed );
}
/**
* Get section title.
*
* @param string $id DID.
*
* @return string
*/
function get_section_title( string $id ) {
switch ( $id ) {
case 'description':
return _x( 'Description', 'Plugin installer section title', 'fair' );
case 'installation':
return _x( 'Installation', 'Plugin installer section title', 'fair' );
case 'faq':
return _x( 'FAQ', 'Plugin installer section title', 'fair' );
case 'screenshots':
return _x( 'Screenshots', 'Plugin installer section title', 'fair' );
case 'changelog':
return _x( 'Changelog', 'Plugin installer section title', 'fair' );
case 'reviews':
return _x( 'Reviews', 'Plugin installer section title', 'fair' );
case 'other_notes':
return _x( 'Other Notes', 'Plugin installer section title', 'fair' );
default:
return ucwords( str_replace( '_', ' ', $id ) );
}
}
/**
* Render page.
*
* @param MetadataDocument $metadata Metadata for page render.
* @param string $tab Page tab.
* @param string $section Page section.
*
* @return void
*/
function render_page( MetadataDocument $metadata, string $tab, string $section ) {
iframe_header( __( 'Plugin Installation', 'fair' ) );
render( $metadata, $tab, $section );
wp_print_request_filesystem_credentials_modal();
wp_print_admin_notice_templates();
iframe_footer();
}
/**
* Displays plugin information in dialog box form.
*
* @since 2.7.0
*
* @global string $tab
* @param MetadataDocument $doc Metadata for page render.
* @param string $tab Page tab.
* @param string $section Page section.
*/
function render( MetadataDocument $doc, string $tab, string $section ) {
$sections = (array) $doc->sections;
if ( ! isset( $sections[ $section ] ) ) {
$section = array_keys( $sections )[0];
}
$releases = array_values( $doc->releases );
usort( $releases, fn ( $a, $b ) => version_compare( $b->version, $a->version ) );
$latest = ! empty( $releases ) ? reset( $releases ) : null;
// Add banners, if available.
$_with_banner = '';
if ( ! empty( $latest->artifacts->banner ) ) {
$_with_banner = 'with-banner';
render_banner( $latest );
}
?>
<div id="plugin-information-scrollable">
<div id="<?= esc_attr( $tab ); ?>-title" class="<?= esc_attr( $_with_banner ); ?>">
<div class='vignette'></div>
<h2><?= esc_html( $doc->name ); ?></h2>
</div>
<div id="<?= esc_attr( $tab ); ?>-tabs" class="<?= esc_attr( $_with_banner ); ?>">
<?php
foreach ( $sections as $section_id => $content ) :
$href = add_query_arg( [
'tab' => $tab,
'section' => $section_id,
] );
?>
<a
name="<?= esc_attr( $section_id ); ?>"
href="<?= esc_url( $href ); ?>"
<?= ( $section_id === $section ) ? ' class="current"' : ''; ?>
><?= esc_html( get_section_title( $section_id ) ); ?></a>
<?php
endforeach;
?>
</div>
<div id="<?= esc_attr( $tab ); ?>-content" class="<?= esc_attr( $_with_banner ); ?>">
<?php render_fyi( $doc, $latest ) ?>
<div id="section-holder">
<?php
add_requirement_notices( $latest );
foreach ( $sections as $section_id => $content ) {
$prepared = sanitize_html( $content );
$prepared = links_add_target( $prepared, '_blank' );
printf(
'<div id="section-%s" class="section" style="display: %s;">%s</div>',
esc_attr( $section_id ),
( $section_id === $section ) ? 'block' : 'none',
// phpcs:ignore HM.Security.EscapeOutput.OutputNotEscaped -- sanitize_html() is a custom wrapper for wp_kses().
sanitize_html( $prepared )
);
}
?>
</div>
</div>
</div>
<div id="<?= esc_attr( $tab ); ?>-footer">
<?php
if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
$data = [];
$button = get_action_button( $doc, $latest );
$button = str_replace( 'class="', 'class="right ', $button );
if ( ! str_contains( $button, _x( 'Activate', 'plugin', 'fair' ) ) ) {
// todo: requires changes to the JS to catch the DID.
// phpcs:ignore Squiz.Commenting.InlineComment.InvalidEndChar
// $button = str_replace( 'class="', 'id="plugin_install_from_iframe" class="', $button );
}
echo wp_kses_post( $button );
}
?>
</div>
<?php
}
/**
* Render banner.
*
* @param ReleaseDocument $release Release document.
*
* @return void
*/
function render_banner( ReleaseDocument $release ) {
if ( empty( $release->artifacts->banner ) ) {
return;
}
$banners = $release->artifacts->banner;
$regular = array_find( $banners, fn ( $banner ) => $banner->width === 772 && $banner->height === 250 );
$high_res = array_find( $banners, fn ( $banner ) => $banner->width === 1544 && $banner->height === 500 );
if ( empty( $regular ) && empty( $high_res ) ) {
return;
}
?>
<style type="text/css">
#plugin-information-title.with-banner {
background-image: url( <?php echo esc_url( $regular->url ?? $high_res->url ); ?> );
}
@media only screen and ( -webkit-min-device-pixel-ratio: 1.5 ) {
#plugin-information-title.with-banner {
background-image: url( <?php echo esc_url( $high_res->url ?? $regular->url ); ?> );
}
}
</style>
<?php
}
/**
* Name requirement.
*
* @param string $requirement The requirement.
*
* @return string
*/
function name_requirement( string $requirement ) : string {
switch ( true ) {
case ( $requirement === 'env:wp' ):
return __( 'WordPress', 'fair' );
case ( $requirement === 'env:php' ):
return __( 'PHP', 'fair' );
case str_starts_with( $requirement, 'env:php-' ):
return substr( $requirement, 8 );
default:
return $requirement;
}
}
/**
* Render FYI.
*
* @param MetadataDocument $doc Metadata document.
* @param ReleaseDocument $release Release document.
*
* @return void
*/
function render_fyi( MetadataDocument $doc, ReleaseDocument $release ) : void {
?>
<div class="fyi">
<ul>
<?php if ( ! empty( $release ) ) : ?>
<li><strong><?= __( 'Version:', 'fair' ); ?></strong> <?= esc_attr( $release->version ); ?></li>
<?php endif; ?>
<?php if ( ! empty( $doc->slug ) ) : ?>
<li><strong><?= __( 'Slug:', 'fair' ); ?></strong> <?= esc_attr( $doc->slug ); ?></li>
<?php endif; ?>
<?php if ( ! empty( $release->requires ) ) : ?>
<li>
<strong><?= __( 'Requires:', 'fair' ); ?></strong>
<ul>
<?php foreach ( (array) $release->requires as $type => $constraint ) : ?>
<li><?= esc_html( name_requirement( $type ) ); ?> <?= esc_html( $constraint ); ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>
<?php if ( ! empty( $release->suggests ) ) : ?>
<li>
<strong><?= __( 'Suggests:', 'fair' ); ?></strong>
<ul>
<?php foreach ( (array) $release->suggests as $type => $constraint ) : ?>
<li><?= esc_html( name_requirement( $type ) ); ?> <?= esc_html( $constraint ); ?></li>
<?php endforeach; ?>
</ul>
</li>
<?php endif; ?>
</ul>
<?php
if ( ! empty( $doc->authors ) ) :
?>
<h3><?= __( 'Authors', 'fair' ); ?></h3>
<ul class="contributors">
<?php
foreach ( (array) $doc->authors as $author ) {
if ( empty( $author->name ) ) {
continue;
}
$url = $author->url ?? ( isset( $author->email ) ? 'mailto:' . $author->email : null );
printf(
'<a href="%s" target="_blank">%s</a>',
esc_url( $url ),
esc_html( $author->name )
);
}
?>
</ul>
<?php endif ?>
</div>
<?php
}
/**
* Check requirements, and add notices if not met.
*
* @param ReleaseDocument $release Release document.
* @return void
*/
function add_requirement_notices( ReleaseDocument $release ) : void {
$unmet_requires = Packages\get_unmet_requirements( (array) $release->requires );
$unmet_suggests = Packages\get_unmet_requirements( (array) $release->suggests );
if ( empty( $unmet_requires ) && empty( $unmet_suggests ) ) {
return;
}
if ( isset( $unmet_requires['env:php'] ) ) {
$compatible_php_notice_message = '<p>';
$compatible_php_notice_message .= __( '<strong>Error:</strong> This plugin <strong>requires a newer version of PHP</strong>.', 'fair' );
if ( current_user_can( 'update_php' ) ) {
$compatible_php_notice_message .= sprintf(
/* translators: %s: URL to Update PHP page. */
' ' . __( '<a href="%s" target="_blank">Click here to learn more about updating PHP</a>.', 'fair' ),
esc_url( wp_get_update_php_url() )
) . wp_update_php_annotation( '</p><p><em>', '</em>', false );
} else {
$compatible_php_notice_message .= '</p>';
}
wp_admin_notice(
$compatible_php_notice_message,
[
'type' => 'error',
'additional_classes' => [ 'notice-alt' ],
'paragraph_wrap' => false,
]
);
}
$is_dev = (bool) preg_match( '/alpha|beta|RC/', get_bloginfo( 'version' ) );
if ( isset( $unmet_suggests['env:wp'] ) && ! $is_dev ) {
wp_admin_notice(
__( '<strong>Warning:</strong> This plugin <strong>has not been tested</strong> with your current version of WordPress.', 'fair' ),
[
'type' => 'warning',
'additional_classes' => [ 'notice-alt' ],
]
);
} elseif ( isset( $unmet_requires['env:wp'] ) ) {
$compatible_wp_notice_message = __( '<strong>Error:</strong> This plugin <strong>requires a newer version of WordPress</strong>.', 'fair' );
if ( current_user_can( 'update_core' ) ) {
$compatible_wp_notice_message .= sprintf(
/* translators: %s: URL to WordPress Updates screen. */
' ' . __( '<a href="%s" target="_parent">Click here to update WordPress</a>.', 'fair' ),
esc_url( self_admin_url( 'update-core.php' ) )
);
}
wp_admin_notice(
$compatible_wp_notice_message,
[
'type' => 'error',
'additional_classes' => [ 'notice-alt' ],
]
);
}
}
/**
* Gets the markup for the plugin install action button.
*
* @param MetadataDocument $doc Metadata document.
* @param ReleaseDocument $release Release document.
*
* @return string The markup for the dependency row button. An empty string if the user does not have capabilities.
*/
function get_action_button( MetadataDocument $doc, ReleaseDocument $release ) {
if ( ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) {
// How did you get here, pal?
return '';
}
// Do we actually meet the requirements?
$compatible = Packages\check_requirements( $release );
$status = 'install'; // todo.
switch ( $status ) {
case 'install':
if ( ! $compatible ) {
return sprintf(
'<button type="button" class="z_install-now button button-disabled" disabled="disabled">%s</button>',
esc_html_x( 'Install Now', 'plugin', 'fair' )
);
}
return sprintf(
'<a class="z_install-now button" data-id="%s" href="%s" aria-label="%s" data-name="%s" role="button">%s</a>',
esc_attr( $doc->id ),
esc_url( Admin\get_direct_install_url( $doc, $release ) ),
/* translators: %s: Plugin name and version. */
esc_attr( sprintf( _x( 'Install %s now', 'plugin', 'fair' ), $doc->name ) ),
esc_attr( $doc->name ),
esc_html_x( 'Install Now', 'plugin', 'fair' )
);
default:
// todo.
}
}