Skip to content

Commit

Permalink
Merge branch 'trunk' into feature/image-loading-optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter authored Mar 22, 2024
2 parents ea44fcf + 6e8f8c3 commit 4352e58
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 26 deletions.
56 changes: 53 additions & 3 deletions bin/plugin/commands/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const { getChangelog } = require( './changelog' );
* @typedef WPReadmeCommandOptions
*
* @property {string=} milestone Optional milestone title, to update the changelog in the readme.
* @property {string=} path Optional path to the readme.txt file to update. If omitted, it will be detected via --milestone.
* @property {string=} token Optional personal GitHub access token, only relevant for changelog updates.
*/

Expand All @@ -24,6 +25,7 @@ const { getChangelog } = require( './changelog' );
* @property {string} owner GitHub repository owner.
* @property {string} repo GitHub repository name.
* @property {string=} milestone Optional milestone title, to update the changelog in the readme.
* @property {string=} path Optional path to the readme.txt file to update.
* @property {string=} token Optional personal GitHub access token, only relevant for changelog updates.
*/

Expand All @@ -32,6 +34,11 @@ exports.options = [
argname: '-m, --milestone <milestone>',
description: 'Milestone title, to update the changelog',
},
{
argname: '-p, --path <path>',
description:
'Path to the readme.txt file to update; if omitted, it will be detected via --milestone',
},
{
argname: '-t, --token <token>',
description: 'GitHub token',
Expand All @@ -48,20 +55,57 @@ exports.handler = async ( opt ) => {
owner: config.githubRepositoryOwner,
repo: config.githubRepositoryName,
milestone: opt.milestone,
path: opt.path,
token: opt.token,
} );
};

/**
* Detects the path to the readme.txt file to update based on the milestone title.
*
* @param {string} milestone Milestone title.
*
* @return {string} Detected readme.txt path.
*/
function detectReadmePath( milestone ) {
const slug = milestone.match( /^([a-z0-9-]+) / );
if ( ! slug ) {
throw new Error(
`The ${ milestone } milestone does not start with a valid plugin slug.`
);
}

if ( 'performance-lab' === slug[ 1 ] ) {
return 'readme.txt';
}

if ( ! fs.existsSync( path.join( '.', `plugins/${ slug[ 1 ] }` ) ) ) {
throw new Error( `Unknown plugin with slug '${ slug[ 1 ] }'` );
}

return `plugins/${ slug[ 1 ] }/readme.txt`;
}

/**
* Updates the `readme.txt` file with the given changelog.
*
* @param {string} changelog Changelog in markdown, with trailing newline.
* @param {WPReadmeSettings} settings Readme settings.
*/
function updateReadmeChangelog( changelog, settings ) {
const regex = new RegExp( `= ${ settings.milestone } =[^=]+` );
// Detect the version number to replace it in readme changelog, if already present.
const version = settings.milestone.match(
/\d+\.\d+(\.\d+)?(-[A-Za-z0-9.]+)?$/
);
if ( ! version ) {
throw new Error(
`The ${ settings.milestone } milestone does not end with a version number.`
);
}

const readmeFile = path.join( '.', 'readme.txt' );
const regex = new RegExp( `= ${ version[ 0 ] } =[^=]+` );

const readmeFile = path.join( '.', settings.path );
const fileContent = fs.readFileSync( readmeFile, 'utf8' );

let newContent;
Expand Down Expand Up @@ -95,6 +139,10 @@ async function updateReadme( settings ) {
);

try {
if ( ! settings.path ) {
settings.path = detectReadmePath( settings.milestone );
}

const changelog = await getChangelog( {
owner: settings.owner,
repo: settings.repo,
Expand All @@ -108,6 +156,8 @@ async function updateReadme( settings ) {
return;
}
}
log( formats.success( `\n💃readme.txt successfully updated\n\n` ) );
log(
formats.success( `\n💃${ settings.path } successfully updated\n\n` )
);
}
}
14 changes: 7 additions & 7 deletions includes/admin/js/perflab-plugin-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
typeof settings.data === 'string' &&
settings.data.includes( 'action=install-plugin' )
) {
var params = new URLSearchParams( settings.data );
var slug = params.get( 'slug' );
const params = new URLSearchParams( settings.data );
const slug = params.get( 'slug' );

// Check if 'slug' was found and output the value.
if ( ! slug ) {
return;
}

var target_element = $(
const target_element = $(
'.wpp-standalone-plugins a[data-slug="' + slug + '"]'
);
if ( ! target_element ) {
Expand All @@ -30,15 +30,15 @@
* the core changes have taken place.
*/
setTimeout( function () {
var plugin_url = target_element.attr( 'href' );
const plugin_url = target_element.attr( 'href' );
if ( ! plugin_url ) {
return;
}
var nonce = target_element.attr(
const nonce = target_element.attr(
'data-plugin-activation-nonce'
);
var plugin_slug = target_element.attr( 'data-slug' );
var url = new URL( plugin_url );
const plugin_slug = target_element.attr( 'data-slug' );
const url = new URL( plugin_url );
url.searchParams.set( 'action', 'perflab_activate_plugin' );
url.searchParams.set( '_wpnonce', nonce );
url.searchParams.set( 'plugin', plugin_slug );
Expand Down
24 changes: 10 additions & 14 deletions includes/admin/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ function perflab_render_pointer( $pointer_id = 'perflab-admin-pointer', $args =
<script id="<?php echo esc_attr( $pointer_id ); ?>" type="text/javascript">
jQuery( function() {
// Pointer Options.
var options = {
content: '<h3><?php echo esc_js( $args['heading'] ); ?></h3><p><?php echo wp_kses( $args['content'], $wp_kses_options ); ?></p>',
const options = {
content: <?php echo wp_json_encode( '<h3>' . esc_html( $args['heading'] ) . '</h3><p>' . wp_kses( $args['content'], $wp_kses_options ) . '</p>' ); ?>,
position: {
edge: 'left',
align: 'right',
Expand All @@ -143,7 +143,7 @@ function perflab_render_pointer( $pointer_id = 'perflab-admin-pointer', $args =
jQuery.post(
window.ajaxurl,
{
pointer: '<?php echo esc_js( $pointer_id ); ?>',
pointer: <?php echo wp_json_encode( $pointer_id ); ?>,
action: 'dismiss-wp-pointer',
_wpnonce: <?php echo wp_json_encode( wp_create_nonce( 'dismiss_pointer' ) ); ?>,
}
Expand Down Expand Up @@ -305,17 +305,13 @@ function perflab_deactivate_plugin() {
}

// Deactivate the plugin in question and return to prior screen.
$do_plugin_deactivation = deactivate_plugins( $plugin );
$referer = wp_get_referer();
if ( ! is_wp_error( $do_plugin_deactivation ) ) {
$referer = add_query_arg(
array(
'deactivate' => true,
),
$referer
);
}

deactivate_plugins( $plugin );
$referer = add_query_arg(
array(
'deactivate' => true,
),
wp_get_referer()
);
if ( wp_safe_redirect( $referer ) ) {
exit;
}
Expand Down
4 changes: 2 additions & 2 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function perflab_maybe_set_object_cache_dropin() {
* Previous versions of the Performance Lab plugin were renaming the
* original object-cache.php file and then loading both. However, due
* to other plugins eagerly checking file headers, this caused too many
* problems across sites so it was decided to remove this layer.
* problems across sites, so it was decided to remove this layer.
* Only placing the drop-in file if no other one exists yet is the
* safest solution.
*/
Expand Down Expand Up @@ -249,7 +249,7 @@ function perflab_maybe_remove_object_cache_dropin() {
* override the Performance Lab file. This is only relevant for
* backward-compatibility with previous Performance Lab versions
* which were backing up the file and then loading both.
* Otherwise just delete the Performance Lab file.
* Otherwise, just delete the Performance Lab file.
*/
if ( $wp_filesystem->exists( $dropin_backup_path ) ) {
$wp_filesystem->move( $dropin_backup_path, $dropin_path, true );
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ parameters:
- uninstall.php
scanDirectories:
- vendor/wp-phpunit/wp-phpunit/
dynamicConstantNames:
- PERFLAB_OBJECT_CACHE_DROPIN_VERSION

0 comments on commit 4352e58

Please sign in to comment.