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

Ensure that when an archiveless post is created manually the meta is still set #66

Merged
merged 3 commits into from
Nov 28, 2023
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches:
- main
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
php-tests:
Expand Down
16 changes: 12 additions & 4 deletions inc/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,24 @@ function get_asset_dependencies( string $asset ): array {
return $dependencies['dependencies'];
}

/**
* Get the asset map.
*
* @return array<string, array<string, array<string, string>>> The asset map.
*/
function get_asset_map(): array {
// @phpstan-ignore-next-line return
return ARCHIVELESS_ASSET_MAP;
}

/**
* Get the contentHash for a given asset.
*
* @param string $asset Entry point and asset type separated by a '.'.
* @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';
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down
39 changes: 28 additions & 11 deletions inc/class-archiveless.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
]
);
}

/**
Expand All @@ -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<mixed> $args Array of arguments for WP_Query.
* @param WP_REST_Request<array<string>> $request The REST API request.
* @return array<mixed>
*/
public function filter__rest_post_type_query( $args, $request ): array {
/**
Expand Down Expand Up @@ -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';
}

Expand All @@ -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,
]
);
}

/**
Expand All @@ -335,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 );
}
}

Expand Down Expand Up @@ -437,7 +454,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(
Expand Down
17 changes: 17 additions & 0 deletions tests/test-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down