Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordPress build: Add newlines after PHP annotations #986

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions packages/playground/wordpress/build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ RUN cd wordpress && \
# PHP >= 8.0 is needed to preserve PHP 8.0 attributes,
# like #[Attribute] and #[Pure]. In PHP 7.4, they are
# treated as comments and removed by the whitespace stripping.
COPY ./build-assets/add-newlines-after-annotations.php /root/
RUN cd wordpress && \
for phpfile in $(\
find ./ -type f -name '*.php' \
Expand All @@ -170,6 +171,8 @@ RUN cd wordpress && \
# remove set_time_limit function calls as they invoke the
# setitimer system call unsupported by emscripten
perl -pe 's/@?set_time_limit\([^)]+\)//g' $phpfile.small > $phpfile && \
# Add newlines after PHP 8.0 attributes
php /root/add-newlines-after-annotations.php $phpfile && \
rm $phpfile.small; \
done

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Playground uses `php -w` used to minify WordPress by removing whitespace.
* However, this causes issues with annotations.
* `php8 -w file.php` may output code like this:
*
* ```php
* <?php
* #[AllowDynamicProperties] final class WP_Recovery_Mode_Email_Service { /* ... code ... * / $message = __( 'Howdy!', '
*
* WordPress has a built-in feature ...
* ```
*
* The class declaration is treated as a comment by PHP 7.4.
*
* However, PHP 7.4 would remove that annotation entirely.
*
* This script is used to postprocess the minified PHP files to add newlines after
* annotations.
*
* @see https://github.com/WordPress/wordpress-playground/issues/985
*/

function token_to_string($token) {
if(is_array($token)) {
return $token[1];
} else {
return $token;
}
}

$text = file_get_contents( $argv[1] );
$output = '';
$inside_annotation = false;
foreach(token_get_all($text) as $token) {
if ($inside_annotation) {
if ($token === '[') {
++$square_brace_balance;
} else if ($token === ']') {
--$square_brace_balance;
}

$output .= token_to_string($token);
if($square_brace_balance === 0) {
$output .= "\n";
$inside_annotation = false;
}
continue;
} else if(is_array($token)) {
if($token[0] === T_ATTRIBUTE) {
$square_brace_balance = 1;
$inside_annotation = true;
}
}

$output .= token_to_string($token);
}

file_put_contents($argv[1], $output);
1 change: 1 addition & 0 deletions packages/playground/wordpress/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ await asyncSpawn(
[
'build',
'.',
'--progress=plain',
'--tag=wordpress-playground',
'--build-arg',
`WP_ZIP_URL=${versionInfo.url}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,7 @@ div.action-links {
}

@media print,
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {

#TB_window.plugin-details-modal.thickbox-loading:before {
Expand Down Expand Up @@ -3176,6 +3177,7 @@ img {
font-family: Consolas, Monaco, monospace;
font-size: 13px;
background: #f6f7f7;
-o-tab-size: 4;
tab-size: 4;
}

Expand Down Expand Up @@ -3764,6 +3766,7 @@ img {
* HiDPI Displays
*/
@media print,
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {
/* Back-compat for pre-3.8 */
div.star-holder,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,7 @@ div.action-links {
}

@media print,
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {

#TB_window.plugin-details-modal.thickbox-loading:before {
Expand Down Expand Up @@ -3175,6 +3176,7 @@ img {
font-family: Consolas, Monaco, monospace;
font-size: 13px;
background: #f6f7f7;
-o-tab-size: 4;
tab-size: 4;
}

Expand Down Expand Up @@ -3763,6 +3765,7 @@ img {
* HiDPI Displays
*/
@media print,
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {
/* Back-compat for pre-3.8 */
div.star-holder,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,7 @@ p.customize-section-description {
font-family: Consolas, Monaco, monospace;
font-size: 12px;
padding: 6px 8px;
-o-tab-size: 2;
tab-size: 2;
}
.customize-control-code_editor textarea,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,7 @@ p.customize-section-description {
font-family: Consolas, Monaco, monospace;
font-size: 12px;
padding: 6px 8px;
-o-tab-size: 2;
tab-size: 2;
}
.customize-control-code_editor textarea,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ table.not-image tr.image-only {
* HiDPI Displays
*/
@media print,
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {

.image-align-none-label {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ table.not-image tr.image-only {
* HiDPI Displays
*/
@media print,
(-webkit-min-device-pixel-ratio: 1.25),
(min-resolution: 120dpi) {

.image-align-none-label {
Expand Down
Loading
Loading