| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Data; |
| 4 |
|
| 5 |
use GraphQL\Type\Definition\ResolveInfo; |
| 6 |
use WPGraphQL\AppContext; |
| 7 |
use WPGraphQL\Utils\Utils; |
| 8 |
use WP_Post_Type; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class MediaItemMutation |
| 12 |
* |
| 13 |
* @package WPGraphQL\Type\MediaItem |
| 14 |
*/ |
| 15 |
class MediaItemMutation { |
| 16 |
|
| 17 |
/** |
| 18 |
* This prepares the media item for insertion |
| 19 |
* |
| 20 |
* @param array<string,mixed> $input The input for the mutation from the GraphQL request |
| 21 |
* @param \WP_Post_Type $post_type_object The post_type_object for the mediaItem (attachment) |
| 22 |
* @param string $mutation_name The name of the mutation being performed (create, update, etc.) |
| 23 |
* @param array<string,mixed>|false $file The mediaItem (attachment) file |
| 24 |
* |
| 25 |
* @return array<string,mixed> |
| 26 |
*/ |
| 27 |
public static function prepare_media_item( array $input, WP_Post_Type $post_type_object, string $mutation_name, $file ) { |
| 28 |
$insert_post_args = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Set the post_type (attachment) for the insert |
| 32 |
*/ |
| 33 |
$insert_post_args['post_type'] = $post_type_object->name; |
| 34 |
|
| 35 |
/** |
| 36 |
* Prepare the data for inserting the mediaItem |
| 37 |
* NOTE: These are organized in the same order as: http://v2.wp-api.org/reference/media/#schema-meta |
| 38 |
*/ |
| 39 |
if ( ! empty( $input['date'] ) && false !== strtotime( $input['date'] ) ) { |
| 40 |
$insert_post_args['post_date'] = gmdate( 'Y-m-d H:i:s', strtotime( $input['date'] ) ); |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! empty( $input['dateGmt'] ) && false !== strtotime( $input['dateGmt'] ) ) { |
| 44 |
$insert_post_args['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', strtotime( $input['dateGmt'] ) ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! empty( $input['slug'] ) ) { |
| 48 |
$insert_post_args['post_name'] = $input['slug']; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! empty( $input['status'] ) ) { |
| 52 |
$insert_post_args['post_status'] = $input['status']; |
| 53 |
} else { |
| 54 |
$insert_post_args['post_status'] = 'inherit'; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! empty( $input['title'] ) ) { |
| 58 |
$insert_post_args['post_title'] = $input['title']; |
| 59 |
} elseif ( ! empty( $file['file'] ) ) { |
| 60 |
$insert_post_args['post_title'] = basename( $file['file'] ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! empty( $input['authorId'] ) ) { |
| 64 |
$insert_post_args['post_author'] = Utils::get_database_id_from_id( $input['authorId'] ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! empty( $input['commentStatus'] ) ) { |
| 68 |
$insert_post_args['comment_status'] = $input['commentStatus']; |
| 69 |
} |
| 70 |
|
| 71 |
if ( ! empty( $input['pingStatus'] ) ) { |
| 72 |
$insert_post_args['ping_status'] = $input['pingStatus']; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! empty( $input['caption'] ) ) { |
| 76 |
$insert_post_args['post_excerpt'] = $input['caption']; |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! empty( $input['description'] ) ) { |
| 80 |
$insert_post_args['post_content'] = $input['description']; |
| 81 |
} else { |
| 82 |
$insert_post_args['post_content'] = ''; |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! empty( $file['type'] ) ) { |
| 86 |
$insert_post_args['post_mime_type'] = $file['type']; |
| 87 |
} elseif ( ! empty( $input['fileType'] ) ) { |
| 88 |
$insert_post_args['post_mime_type'] = $input['fileType']; |
| 89 |
} |
| 90 |
|
| 91 |
if ( ! empty( $input['parentId'] ) ) { |
| 92 |
$insert_post_args['post_parent'] = Utils::get_database_id_from_id( $input['parentId'] ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Filter the $insert_post_args |
| 97 |
* |
| 98 |
* @param array<string,mixed> $insert_post_args The array of $input_post_args that will be passed to wp_insert_attachment |
| 99 |
* @param array<string,mixed> $input The data that was entered as input for the mutation |
| 100 |
* @param \WP_Post_Type $post_type_object The post_type_object that the mutation is affecting |
| 101 |
* @param string $mutation_type The type of mutation being performed (create, update, delete) |
| 102 |
* |
| 103 |
* @hookGroup models |
| 104 |
* @since 0.0.5 |
| 105 |
*/ |
| 106 |
$insert_post_args = apply_filters( 'graphql_media_item_insert_post_args', $insert_post_args, $input, $post_type_object, $mutation_name ); |
| 107 |
|
| 108 |
return $insert_post_args; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* This updates additional data related to a mediaItem, such as postmeta. |
| 113 |
* |
| 114 |
* @param int $media_item_id The ID of the media item being mutated |
| 115 |
* @param array<string,mixed> $input The input on the mutation |
| 116 |
* @param \WP_Post_Type $post_type_object The Post Type Object for the item being mutated |
| 117 |
* @param string $mutation_name The name of the mutation |
| 118 |
* @param \WPGraphQL\AppContext $context The AppContext that is passed down the resolve tree |
| 119 |
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo that is passed down the resolve tree |
| 120 |
* |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public static function update_additional_media_item_data( int $media_item_id, array $input, WP_Post_Type $post_type_object, string $mutation_name, AppContext $context, ResolveInfo $info ) { |
| 124 |
|
| 125 |
/** |
| 126 |
* Update alt text postmeta for the mediaItem |
| 127 |
*/ |
| 128 |
if ( ! empty( $input['altText'] ) ) { |
| 129 |
update_post_meta( $media_item_id, '_wp_attachment_image_alt', $input['altText'] ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Run an action after the additional data has been updated. This is a great spot to hook into to |
| 134 |
* update additional data related to mediaItems, such as updating additional postmeta, |
| 135 |
* or sending emails to Kevin. . .whatever you need to do with the mediaItem. |
| 136 |
* |
| 137 |
* @param int $media_item_id The ID of the mediaItem being mutated |
| 138 |
* @param array<string,mixed> $input The input for the mutation |
| 139 |
* @param \WP_Post_Type $post_type_object The Post Type Object for the type of post being mutated |
| 140 |
* @param string $mutation_name The name of the mutation (ex: create, update, delete) |
| 141 |
* @param \WPGraphQL\AppContext $context The AppContext that is passed down the resolve tree |
| 142 |
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo that is passed down the resolve tree |
| 143 |
* |
| 144 |
* @hookGroup models |
| 145 |
* @since 0.0.5 |
| 146 |
*/ |
| 147 |
do_action( 'graphql_media_item_mutation_update_additional_data', $media_item_id, $input, $post_type_object, $mutation_name, $context, $info ); |
| 148 |
} |
| 149 |
} |
| 150 |
|