From d14388668b14c67447b08c1171e7e45688ce377b Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 28 Nov 2023 12:54:05 -0500 Subject: [PATCH 1/3] Updates to support phpstan --- inc/assets.php | 16 ++++++++++++---- inc/class-archiveless.php | 35 ++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/inc/assets.php b/inc/assets.php index 77adf60c..042e4ed9 100644 --- a/inc/assets.php +++ b/inc/assets.php @@ -67,6 +67,16 @@ function get_asset_dependencies( string $asset ): array { return $dependencies['dependencies']; } +/** + * Get the asset map. + * + * @return array>> The asset map. + */ +function get_asset_map(): array { + // @phpstan-ignore-next-line return + return ARCHIVELESS_ASSET_MAP; +} + /** * Get the contentHash for a given asset. * @@ -74,9 +84,7 @@ function get_asset_dependencies( string $asset ): array { * @return string The asset's hash. */ function get_asset_hash( string $asset ): string { - return get_asset_property( $asset, 'hash' ) - ?? ARCHIVELESS_ASSET_MAP['hash'] - ?? '1.0.0'; + return get_asset_property( $asset, 'hash' ) ?? '1.0.0'; } /** @@ -115,7 +123,7 @@ function get_asset_property( string $asset, string $prop ): ?string { */ list( $entrypoint, $type ) = explode( '.', "$asset." ); - return ARCHIVELESS_ASSET_MAP[ $entrypoint ][ $type ][ $prop ] ?? null; + return get_asset_map()[ $entrypoint ][ $type ][ $prop ] ?? null; } /** diff --git a/inc/class-archiveless.php b/inc/class-archiveless.php index ee42469e..b29bd54b 100644 --- a/inc/class-archiveless.php +++ b/inc/class-archiveless.php @@ -205,7 +205,7 @@ public function add_ui(): void { * @param int $meta_id ID of updated metadata entry. * @param int $object_id ID of the object metadata is for. * @param string $meta_key Metadata key. - * @param mixed $meta_value Metadata value. Serialized if non-scalar. + * @param string $meta_value Metadata value. Serialized if non-scalar. */ public function updated_post_meta( $meta_id, $object_id, $meta_key, $meta_value ): void { // Only handle updates to this plugin's meta key. @@ -241,7 +241,12 @@ public function updated_post_meta( $meta_id, $object_id, $meta_key, $meta_value } // Update the post with the new status. - wp_update_post( $post_object ); + wp_update_post( + [ + 'ID' => $object_id, + 'post_status' => $post_object->post_status, + ] + ); } /** @@ -260,9 +265,9 @@ public function filter_rest_response(): void { /** * Filter the REST API query, with the edit context, to include archiveless posts. * - * @param array $args Array of arguments for WP_Query. - * @param WP_REST_Request $request The REST API request. - * @return array + * @param array $args Array of arguments for WP_Query. + * @param WP_REST_Request> $request The REST API request. + * @return array */ public function filter__rest_post_type_query( $args, $request ): array { /** @@ -294,7 +299,7 @@ public function filter__rest_post_type_query( $args, $request ): array { */ public function filter__rest_prepare_post_data( $response ) { // Override the post status if it is 'archiveless'. - if ( ! empty( $response->data['status'] ) && self::$status === $response->data['status'] ) { + if ( is_array( $response->data ) && ! empty( $response->data['status'] ) && self::$status === $response->data['status'] ) { $response->data['status'] = 'publish'; } @@ -315,14 +320,22 @@ public function transition_post_status( $new_status, $old_status, $post ): void } // Only fire if archiveless postmeta is set to true. - if ( 1 !== (int) get_post_meta( $post->ID, self::$meta_key, true ) ) { + if ( empty( get_post_meta( $post->ID, self::$meta_key, true ) ) ) { return; } - // Change the post status to `archiveless` and update. - $post->post_status = self::$status; + // Prevent updating if the post status is already `archiveless`. + if ( self::$status === $post->post_status ) { + return; + } - wp_update_post( $post ); + // Update the post's status to `archiveless`. + wp_update_post( + [ + 'ID' => $post->ID, + 'post_status' => self::$status, + ] + ); } /** @@ -437,7 +450,7 @@ public function get_default_post_statuses( $query ): array { $post_statuses = $query->get( 'post_status', [] ); if ( ! is_array( $post_statuses ) ) { - $post_statuses = explode( ',', $post_statuses ); + $post_statuses = is_string( $post_statuses ) ? explode( ',', $post_statuses ) : []; } $post_statuses = array_merge( From a5f5dbee4bed8eeee870cbd369e16318ba966019 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 28 Nov 2023 12:57:11 -0500 Subject: [PATCH 2/3] Ensure that when an archiveless post is created manually the meta is still set --- inc/class-archiveless.php | 4 ++++ tests/test-general.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/inc/class-archiveless.php b/inc/class-archiveless.php index b29bd54b..c8ce6e30 100644 --- a/inc/class-archiveless.php +++ b/inc/class-archiveless.php @@ -348,6 +348,10 @@ public function save_post( $post_id ): void { if ( isset( $_POST[ self::$meta_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing update_post_meta( $post_id, self::$meta_key, intval( $_POST[ self::$meta_key ] ) ); + } elseif ( static::$status === get_post_status( $post_id ) ) { + // If the post status is `archiveless`, ensure the post's + // archiveless meta is set to true. + update_post_meta( $post_id, self::$meta_key, 1 ); } } diff --git a/tests/test-general.php b/tests/test-general.php index 7c6ce59f..db1a6298 100644 --- a/tests/test-general.php +++ b/tests/test-general.php @@ -322,6 +322,23 @@ public function test_post_meta_hooks() { $this->assertEquals( 'archiveless', get_post_status( $post_id ) ); } + public function test_post_meta_applied_when_manually_created() { + $post_id = static::factory()->post->create(); + + $this->assertEmpty( get_post_meta( $post_id, 'archiveless', true ) ); + + wp_update_post( + [ + 'ID' => $post_id, + 'post_status' => 'archiveless', + ], + true, + ); + + $this->assertEquals( 'archiveless', get_post_status( $post_id ) ); + $this->assertEquals( '1', get_post_meta( $post_id, 'archiveless', true ) ); + } + public function test_post_preview() { $post_id = static::factory()->post->create( [ From 0054826e64ebaef67f587cde6318b8bdc65b5309 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 28 Nov 2023 12:58:25 -0500 Subject: [PATCH 3/3] Remove schedule --- .github/workflows/unit-test.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index e784ff48..c308fd28 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -5,8 +5,6 @@ on: branches: - main pull_request: - schedule: - - cron: '0 0 * * *' jobs: php-tests: