diff --git a/.github/actions/code-quality/action.yml b/.github/actions/code-quality/action.yml new file mode 100644 index 00000000..d67ff17e --- /dev/null +++ b/.github/actions/code-quality/action.yml @@ -0,0 +1,36 @@ +name: 'PHP Code Quality' +description: 'Sets up PHP and runs code quality tools' +inputs: + working-directory: + description: 'Directory to run composer and quality tools in' + required: true +runs: + using: "composite" + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + tools: composer:v2 + coverage: none + + - name: Install dependencies + uses: ramsey/composer-install@v2 + with: + working-directory: ${{ inputs.working-directory }} + composer-options: "--no-progress" + + - name: Run PHPStan + working-directory: ${{ inputs.working-directory }} + run: composer run-script phpstan + shell: bash + + - name: Run Psalm + working-directory: ${{ inputs.working-directory }} + run: composer run-script php:psalm + shell: bash + + - name: Run PHP CodeSniffer + working-directory: ${{ inputs.working-directory }} + run: composer run-script check-cs + shell: bash \ No newline at end of file diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml new file mode 100644 index 00000000..80275d2d --- /dev/null +++ b/.github/workflows/code-quality.yml @@ -0,0 +1,27 @@ +name: Code Quality + +on: + pull_request: + paths: + - 'plugins/**' + +jobs: + run: + runs-on: ubuntu-latest + name: Check code quality + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Get changed plugin directory + id: plugin + run: | + git fetch --prune --unshallow + plugin=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' | head -1 | cut -d/ -f2) + echo "slug=$plugin" >> $GITHUB_OUTPUT + + - name: PHP Code Quality + uses: ./.github/actions/code-quality + with: + working-directory: plugins/${{ steps.plugin.outputs.slug }} \ No newline at end of file diff --git a/plugins/hwp-previews/ACTIONS_AND_FILTERS.md b/plugins/hwp-previews/ACTIONS_AND_FILTERS.md index 134fbcbf..492fc9e0 100644 --- a/plugins/hwp-previews/ACTIONS_AND_FILTERS.md +++ b/plugins/hwp-previews/ACTIONS_AND_FILTERS.md @@ -16,7 +16,7 @@ - `hwp_previews_settings_group_settings_group` - Filter to modify the settings group name. Default is HWP_PREVIEWS_SETTINGS_GROUP - `hwp_previews_settings_group_settings_config` - Filter to modify the settings array. See `Settings_Group` - `hwp_previews_settings_group_cache_groups` - Filter to modify cache groups for `Settings_Group` -- `hwp_preview_get_post_types_config` - Filter for generating the instance of `Post_Types_Config_Interface` +- `hwp_previews_get_post_types_config` - Filter for generating the instance of `Post_Types_Config_Interface` - `hwp_previews_hooks_post_type_config` - Filter for post type config service for the Hook class - `hwp_previews_hooks_post_status_config` - Filter for post status config service for the Hook class - `hwp_previews_hooks_preview_link_service` - Filter for preview link service for the Hook class diff --git a/plugins/hwp-previews/composer.json b/plugins/hwp-previews/composer.json index 993d4d80..801bc2bf 100644 --- a/plugins/hwp-previews/composer.json +++ b/plugins/hwp-previews/composer.json @@ -50,7 +50,7 @@ } }, "archive": { - "name": "hwp-previews", + "name": "hwp-previews", "exclude": [ "/.*", "bin", @@ -100,9 +100,9 @@ "phpstan": [ "vendor/bin/phpstan analyze --ansi --memory-limit=1G" ], - "psalm": [ - "vendor/bin/psalm --no-progress --show-info=false" - ] + "php:psalm": "psalm", + "php:psalm:info": "psalm --show-info=true", + "php:psalm:fix": "psalm --alter" }, "scripts-descriptions": { }, diff --git a/plugins/hwp-previews/composer.lock b/plugins/hwp-previews/composer.lock index 2aae8e21..bc8be856 100644 --- a/plugins/hwp-previews/composer.lock +++ b/plugins/hwp-previews/composer.lock @@ -1031,16 +1031,16 @@ }, { "name": "php-stubs/wordpress-stubs", - "version": "v6.8.0", + "version": "v6.8.1", "source": { "type": "git", "url": "https://github.com/php-stubs/wordpress-stubs.git", - "reference": "1824db4d1d00d32c0119175d2369d9425dbc4953" + "reference": "92e444847d94f7c30f88c60004648f507688acd5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-stubs/wordpress-stubs/zipball/1824db4d1d00d32c0119175d2369d9425dbc4953", - "reference": "1824db4d1d00d32c0119175d2369d9425dbc4953", + "url": "https://api.github.com/repos/php-stubs/wordpress-stubs/zipball/92e444847d94f7c30f88c60004648f507688acd5", + "reference": "92e444847d94f7c30f88c60004648f507688acd5", "shasum": "" }, "conflict": { @@ -1048,7 +1048,7 @@ }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^1.0", - "nikic/php-parser": "^4.13", + "nikic/php-parser": "^5.4", "php": "^7.4 || ^8.0", "php-stubs/generator": "^0.8.3", "phpdocumentor/reflection-docblock": "^5.4.1", @@ -1076,9 +1076,9 @@ ], "support": { "issues": "https://github.com/php-stubs/wordpress-stubs/issues", - "source": "https://github.com/php-stubs/wordpress-stubs/tree/v6.8.0" + "source": "https://github.com/php-stubs/wordpress-stubs/tree/v6.8.1" }, - "time": "2025-04-17T15:13:53+00:00" + "time": "2025-05-02T12:33:34+00:00" }, { "name": "php-stubs/wp-cli-stubs", diff --git a/plugins/hwp-previews/hwp-previews.php b/plugins/hwp-previews/hwp-previews.php index f4f16fae..f1a428e8 100644 --- a/plugins/hwp-previews/hwp-previews.php +++ b/plugins/hwp-previews/hwp-previews.php @@ -19,8 +19,9 @@ * @package HWP\Previews */ -declare( strict_types=1 ); +declare(strict_types=1); +use HWP\Previews\Autoloader; use HWP\Previews\Plugin; if ( ! defined( 'ABSPATH' ) ) { @@ -29,17 +30,19 @@ // Load the autoloader. require_once __DIR__ . '/src/Autoloader.php'; -if ( ! \HWP\Previews\Autoloader::autoload() ) { +if ( ! Autoloader::autoload() ) { return; } if ( file_exists( __DIR__ . '/activation.php' ) ) { require_once __DIR__ . '/activation.php'; + // @phpstan-ignore-next-line register_activation_hook( __FILE__, 'hwp_previews_activation_callback' ); } if ( file_exists( __DIR__ . '/deactivation.php' ) ) { require_once __DIR__ . '/deactivation.php'; + // @phpstan-ignore-next-line register_deactivation_hook( __FILE__, 'hwp_previews_deactivation_callback' ); } @@ -47,44 +50,23 @@ * Define plugin constants. */ function hwp_previews_constants(): void { - // Plugin version. - if ( ! defined( 'HWP_PREVIEWS_VERSION' ) ) { - define( 'HWP_PREVIEWS_VERSION', '0.0.1' ); - } - - // Plugin Folder Path. - if ( ! defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) { - define( 'HWP_PREVIEWS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); - } - - // Plugin Folder URL. - if ( ! defined( 'HWP_PREVIEWS_PLUGIN_URL' ) ) { - define( 'HWP_PREVIEWS_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); - } - - // Plugin Root File. - if ( ! defined( 'HWP_PREVIEWS_PLUGIN_FILE' ) ) { - define( 'HWP_PREVIEWS_PLUGIN_FILE', __FILE__ ); - } - - // Whether to autoload the files or not. - if ( ! defined( 'HWP_PREVIEWS_AUTOLOAD' ) ) { - define( 'HWP_PREVIEWS_AUTOLOAD', true ); - } - - // Text Domain - if ( ! defined( 'HWP_PREVIEWS_TEXT_DOMAIN' ) ) { - define( 'HWP_PREVIEWS_TEXT_DOMAIN', 'hwp-previews' ); - } - - // Plugin config settings group - if ( ! defined( 'HWP_PREVIEWS_SETTINGS_GROUP' ) ) { - define( 'HWP_PREVIEWS_SETTINGS_GROUP', 'hwp_previews_settings_group' ); - } - - // Plugin config settings key. - if ( ! defined( 'HWP_PREVIEWS_SETTINGS_KEY' ) ) { - define( 'HWP_PREVIEWS_SETTINGS_KEY', 'hwp_previews_settings' ); + $constants = [ + 'HWP_PREVIEWS_VERSION' => '0.0.1', + 'HWP_PREVIEWS_PLUGIN_DIR' => plugin_dir_path( __FILE__ ), + 'HWP_PREVIEWS_PLUGIN_URL' => plugin_dir_url( __FILE__ ), + 'HWP_PREVIEWS_PLUGIN_FILE' => __FILE__, + 'HWP_PREVIEWS_AUTOLOAD' => true, + 'HWP_PREVIEWS_SETTINGS_GROUP' => 'hwp_previews_settings_group', + 'HWP_PREVIEWS_SETTINGS_KEY' => 'hwp_previews_settings', + 'HWP_PREVIEWS_TEXT_DOMAIN' => 'hwp-previews', + ]; + + foreach ( $constants as $name => $value ) { + if ( ! defined( $name ) ) { + // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.VariableConstantNameFound + define( $name, $value ); + // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.VariableConstantNameFound + } } // Plugin Template Directory. @@ -99,7 +81,6 @@ function hwp_previews_constants(): void { function hwp_previews_init(): void { hwp_previews_constants(); - if ( defined( 'HWP_PREVIEWS_PLUGIN_DIR' ) ) { require_once HWP_PREVIEWS_PLUGIN_DIR . 'src/Plugin.php'; Plugin::instance(); @@ -110,14 +91,12 @@ function hwp_previews_init(): void { add_action( 'admin_notices', - static function () { + static function (): void { ?>
diff --git a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php
index 7a7c0fcd..c508b114 100644
--- a/plugins/hwp-previews/src/Hooks/Preview_Hooks.php
+++ b/plugins/hwp-previews/src/Hooks/Preview_Hooks.php
@@ -1,5 +1,7 @@
+ */
public static function get_post_statuses(): array {
$post_statuses = [
'publish',
@@ -123,7 +139,7 @@ public static function get_post_statuses(): array {
'draft',
'pending',
'private',
- 'auto-draft'
+ 'auto-draft',
];
return apply_filters( 'hwp_previews_hooks_post_statuses', $post_statuses );
@@ -134,14 +150,18 @@ public static function get_post_statuses(): array {
*
* @link https://developer.wordpress.org/reference/hooks/wp_insert_post_data/
*
- * @param array $data
- * @param array $postarr
+ * @param array