-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-source-wordpress): fetch referenced media items during sin…
…gle node updates (#32381)
- Loading branch information
1 parent
9e96088
commit 1645965
Showing
11 changed files
with
305 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...source-wordpress/docker/wordpress/plugins/wp-gatsby-test-plugin/wp-gatsby-test-plugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
// Plugin Name: WPGatsby Test Plugin (for tests only) | ||
|
||
add_action( 'graphql_register_types', 'add_test_mutations' ); | ||
|
||
function add_test_mutations() { | ||
register_graphql_mutation( "attachFeaturedImageToNodeById", [ | ||
'description' => 'Attaches a MediaItem database ID to a post featured image by database ID.', | ||
'inputFields' => [ | ||
'mediaItemId' => [ | ||
'type' => 'Int' | ||
], | ||
'postId' => [ | ||
'type' => 'Int' | ||
] | ||
], | ||
'outputFields' => [ | ||
'success' => [ | ||
'type' => 'Boolean' | ||
] | ||
], | ||
'mutateAndGetPayload' => function( $input ) { | ||
|
||
if ( empty( $input['mediaItemId'] ) || empty( $input['postId'] ) ) { | ||
return [ | ||
'success' => false | ||
]; | ||
} | ||
|
||
$selectedPost = get_post( $input['postId'] ); | ||
$selectedMediaItem = get_post( $input['mediaItemId'] ); | ||
|
||
|
||
if ( ! $selectedPost || ! $selectedMediaItem ) { | ||
return [ | ||
'success' => false, | ||
]; | ||
} | ||
|
||
set_post_thumbnail( $selectedPost->ID, $selectedMediaItem->ID ); | ||
|
||
wp_update_post( [ | ||
'ID' => $selectedPost->ID, | ||
] ); | ||
|
||
return [ | ||
'success' => true, | ||
]; | ||
} | ||
] ); | ||
|
||
register_graphql_mutation( "removeFeaturedImageFromNodeById", [ | ||
'description' => 'Removes a post featured image by post database ID.', | ||
'inputFields' => [ | ||
'postId' => [ | ||
'type' => 'Int' | ||
] | ||
], | ||
'outputFields' => [ | ||
'success' => [ | ||
'type' => 'Boolean' | ||
] | ||
], | ||
'mutateAndGetPayload' => function( $input ) { | ||
|
||
if ( empty( $input['postId'] ) ) { | ||
return [ | ||
'success' => false | ||
]; | ||
} | ||
|
||
$selectedPost = get_post( $input['postId'] ); | ||
|
||
|
||
if ( ! $selectedPost ) { | ||
return [ | ||
'success' => false, | ||
]; | ||
} | ||
|
||
delete_post_meta( $selectedPost->ID, '_thumbnail_id' ); | ||
|
||
wp_update_post( [ | ||
'ID' => $selectedPost->ID, | ||
] ); | ||
|
||
return [ | ||
'success' => true, | ||
]; | ||
} | ||
] ); | ||
|
||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.