Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/wp-scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Dec 2, 2022
2 parents 9d9cd54 + fec28ff commit 9ff8d6f
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .deployignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ output.log
tests
bin
composer.lock
phpcs.xml
.phpcs.xml
phpunit.xml
configure.php
DOCKER_ENV
Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Via WPCS.
#
/.github export-ignore
/phpcs.xml export-ignore
/.phpcs.xml export-ignore
/.phpcs export-ignore
/phpunit.xml export-ignore
/tests export-ignore
Expand Down
File renamed without changes.
105 changes: 100 additions & 5 deletions configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
/**
* Configure the WordPress Plugin interactively.
*
* Supports arguments to set the values directly.
*
* [--author_name=<author_name>]
* : The author name.
*
* [--author_email=<author_email>]
* : The author email.
*
* phpcs:disable
*/

Expand All @@ -14,6 +22,34 @@
die( 'Not supported in Windows. 🪟' );
}

if ( version_compare( PHP_VERSION, '8.0.0', '<' ) ) {
die( 'PHP 8.0.0 or greater is required.' );
}

// Parse the command line arguments from $argv.
$args = [];
$previous_key = null;

foreach ( $argv as $value ) {
if ( str_starts_with( $value, '--' ) ) {
if ( false !== strpos( $value, '=' ) ) {
[ $arg, $value ] = explode( '=', substr( $value, 2 ), 2 );

$args[ $arg ] = trim( $value );

$previous_key = null;
} else {
$args[ substr( $value, 2 ) ] = true;

$previous_key = substr( $value, 2 );
}
} elseif ( ! empty( $previous_key ) ) {
$args[ $previous_key ] = trim( $value );
} else {
$previous_key = trim( $value );
}
}

function ask( string $question, string $default = '', bool $allow_empty = true ): string {
$answer = readline(
$question . ( $default ? " [{$default}]" : '' ) . ': '
Expand Down Expand Up @@ -136,6 +172,38 @@ function remove_project_files() {
echo 'Removed .buddy, buddy.yml, CHANGELOG.md, .deployignore, .editorconfig, .gitignore, .gitattributes, .github and LICENSE files.' . PHP_EOL;
}

function rollup_phpcs_to_parent( string $parent_file, string $local_file, string $plugin_name, string $plugin_domain ) {
$config = '<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="' . $plugin_name . ' Configuration" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>PHP_CodeSniffer standard for ' . $plugin_name . '</description>
<!-- DO NOT ADD ADDITIONAL RULES TO THIS FILE. Modifications belong in the root-level configuration. -->
<!-- Include Root Rules -->
<rule ref="' . $parent_file . '" />
<rule ref="WordPress.WP.I18n">
<properties>
<!--
Verify that the text_domain is set to the desired text-domain.
Multiple valid text domains can be provided as a comma-delimited list.
-->
<property name="text_domain" type="array" value="' . $plugin_domain . '" />
</properties>
</rule>
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array" value="' . $plugin_domain . '" />
</properties>
</rule>
</ruleset>';

if ( file_put_contents( $local_file, $config ) ) {
echo "Updated {$local_file}.\n";
}
}

function remove_assets_readme( bool $keep_contents, string $file = 'README.md' ) {
$contents = file_get_contents( $file );

Expand Down Expand Up @@ -198,13 +266,13 @@ function delete_files( string|array $paths ) {

$author_name = ask(
question: 'Author name?',
default: run( 'git config user.name' ),
default: $args['author_name'] ?? run( 'git config user.name' ),
allow_empty: false,
);

$author_email = ask(
question: 'Author email?',
default: run( 'git config user.email' ),
default: $args['author_email'] ?? run( 'git config user.email' ),
allow_empty: false,
);

Expand Down Expand Up @@ -342,8 +410,8 @@ function delete_files( string|array $paths ) {
remove_assets_buddy();
}

if ( confirm( 'Will this plugin be using Composer? (WordPress Composer Autoloader already included!)', true ) ) {
$uses_composer = true;
if ( confirm( 'Will this plugin be using Composer? (WordPress Composer Autoloader already included! phpcs and phpunit also rely on Composer being installed for testing.)', true ) ) {
$uses_composer = true;
$needs_built_assets = true;

if ( confirm( 'Do you want to run `composer install`?', true ) ) {
Expand All @@ -368,7 +436,9 @@ function delete_files( string|array $paths ) {
$standalone = true;

// Check if the plugin will be use standalone (as a single repository) or as a
// part of larger project (such as a wp-content-rooted project).
// part of larger project (such as a wp-content-rooted project). Assumes that
// the parent project is located at /wp-content/ and this plugin is located at
// /wp-content/plugins/:plugin/.
if (
file_exists( '../../.git/index' )
&& ! confirm(
Expand Down Expand Up @@ -417,6 +487,31 @@ function delete_files( string|array $paths ) {
echo "\n\n";
}
}

$parent_files = [
$parent_folder . '/phpcs.xml',
$parent_folder . '/phpcs.xml.dist',
$parent_folder . '/.phpcs.xml',
];

if ( file_exists( __DIR__ . '/.phpcs.xml' ) ) {
foreach ( $parent_files as $parent_file ) {
if ( ! file_exists( $parent_file ) ) {
continue;
}

if ( confirm( "Do you want to roll up the phpcs configuration to the parent? (This will change the plugin's phpcs configuration to inherit the parent configuration from {$parent_file}.)" ) ) {
rollup_phpcs_to_parent(
parent_file: '../../' . basename( $parent_file ),
local_file: __DIR__ . '/.phpcs.xml',
plugin_name: $plugin_name,
plugin_domain: $plugin_name_slug,
);

break;
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function register_meta_helper(
* @param string $meta_key The meta key to register.
*/
$args = apply_filters(
'ai_register_meta_helper_args', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
'create_wordpress_plugin_register_meta_helper_args', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
wp_parse_args(
$args,
[
Expand Down

0 comments on commit 9ff8d6f

Please sign in to comment.