1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
<?php
namespace WPGraphQL\Type\PostObject\Mutation;
use GraphQLRelay\Relay;
use WPGraphQL\Types;
class PostObjectMutation {
private static $input_fields = [];
public static function input_fields( $post_type_object ) {
if ( ! empty( $post_type_object->graphql_single_name ) && empty( self::$input_fields[ $post_type_object->graphql_single_name ] ) ) {
$input_fields = [
'authorId' => [
'type' => Types::id(),
'description' => __( 'The userId to assign as the author of the post', 'wp-graphql' ),
],
'commentCount' => [
'type' => Types::int(),
'description' => __( 'The number of comments. Even though WPGraphQL denotes this field as an integer, in WordPress this field should be saved as a numeric string for compatability.', 'wp-graphql' ),
],
'commentStatus' => [
'type' => Types::string(),
'description' => __( 'The comment status for the object', 'wp-graphql' ),
],
'content' => [
'type' => Types::string(),
'description' => __( 'The content of the object', 'wp-graphql' ),
],
'date' => [
'type' => Types::string(),
'description' => __( 'The date of the object', 'wp-graphql' ),
],
'dateGmt' => [
'type' => Types::string(),
'description' => __( 'The date (in GMT zone) of the object', 'wp-graphql' ),
],
'excerpt' => [
'type' => Types::string(),
'description' => __( 'The excerpt of the object', 'wp-graphql' ),
],
'menuOrder' => [
'type' => Types::int(),
'description' => __( 'A field used for ordering posts. This is typically used with nav menu items or for special ordering of hierarchical content types.', 'wp-graphql' ),
],
'mimeType' => [
'type' => Types::mime_type_enum(),
'description' => __( 'If the post is an attachment or a media file, this field will carry the corresponding MIME type. This field is equivalent to the value of WP_Post->post_mime_type and the post_mime_type column in the `post_objects` database table.', 'wp-graphql' ),
],
'modified' => [
'type' => Types::string(),
'description' => __( 'The local modified time for a post. If a post was recently updated the modified field will change to match the corresponding time.', 'wp-graphql' ),
],
'modifiedGmt' => [
'type' => Types::string(),
'description' => __( 'The GMT modified time for a post. If a post was recently updated the modified field will change to match the corresponding time in GMT.', 'wp-graphql' ),
],
'parentId' => [
'type' => Types::id(),
'description' => __( 'The ID of the parent object', 'wp-graphql' ),
],
'password' => [
'type' => Types::string(),
'description' => __( 'The password used to protect the content of the object', 'wp-graphql' ),
],
'pinged' => [
'type' => Types::list_of( Types::string() ),
'description' => __( 'URLs that have been pinged.', 'wp-graphql' ),
],
'pingStatus' => [
'type' => Types::string(),
'description' => __( 'The ping status for the object', 'wp-graphql' ),
],
'slug' => [
'type' => Types::string(),
'description' => __( 'The slug of the object', 'wp-graphql' ),
],
'status' => [
'type' => Types::post_status_enum(),
'description' => __( 'The status of the object', 'wp-graphql' ),
],
'title' => [
'type' => Types::string(),
'description' => __( 'The title of the post', 'wp-graphql' ),
],
'toPing' => [
'type' => Types::list_of( Types::string() ),
'description' => __( 'URLs queued to be pinged.', 'wp-graphql' ),
],
];
self::$input_fields[ $post_type_object->graphql_single_name ] = apply_filters( 'graphql_post_object_mutation_input_fields', $input_fields, $post_type_object );
}
return ! empty( self::$input_fields[ $post_type_object->graphql_single_name ] ) ? self::$input_fields[ $post_type_object->graphql_single_name ] : null;
}
public static function prepare_post_object( $input, $post_type_object, $mutation_name ) {
$insert_post_args['post_type'] = $post_type_object->name;
$author_id_parts = ! empty( $input['authorId'] ) ? Relay::fromGlobalId( $input['authorId'] ) : null;
if ( is_array( $author_id_parts ) && ! empty( $author_id_parts['id'] ) && is_int( $author_id_parts['id'] ) ) {
$insert_post_args['post_author'] = absint( $author_id_parts['id'] );
}
if ( ! empty( $input['date'] ) && false !== strtotime( $input['date'] ) ) {
$insert_post_args['post_date'] = strtotime( $input['date'] );
}
if ( ! empty( $input['dateGmt'] ) && false !== strtotime( $input['dateGmt'] ) ) {
$insert_post_args['post_date_gmt'] = strtotime( $input['dateGmt'] );
}
if ( ! empty( $input['content'] ) ) {
$insert_post_args['post_content'] = $input['content'];
}
if ( ! empty( $input['title'] ) ) {
$insert_post_args['post_title'] = $input['title'];
}
if ( ! empty( $input['excerpt'] ) ) {
$insert_post_args['post_excerpt'] = $input['excerpt'];
}
if ( ! empty( $input['status'] ) ) {
$insert_post_args['post_status'] = $input['status'];
}
if ( ! empty( $input['commentStatus'] ) ) {
$insert_post_args['comment_status'] = $input['commentStatus'];
}
if ( ! empty( $input['pingStatus'] ) ) {
$insert_post_args['ping_status'] = $input['pingStatus'];
}
if ( ! empty( $input['password'] ) ) {
$insert_post_args['post_password'] = $input['password'];
}
if ( ! empty( $input['slug'] ) ) {
$insert_post_args['post_name'] = $input['slug'];
}
if ( ! empty( $input['toPing'] ) ) {
$insert_post_args['to_ping'] = $input['toPing'];
}
if ( ! empty( $input['pinged'] ) ) {
$insert_post_args['pinged'] = $input['pinged'];
}
if ( ! empty( $input['postModified'] ) && false !== strtotime( $input['postModified'] ) ) {
$insert_post_args['modified'] = strtotime( $input['postModified'] );
}
if ( ! empty( $input['postModifiedGmt'] ) && false !== strtotime( $input['postModifiedGmt'] ) ) {
$insert_post_args['post_modified_gmt'] = strtotime( $input['postModifiedGmt'] );
}
$parent_id_parts = ! empty( $input['parentId'] ) ? Relay::fromGlobalId( $input['parentId'] ) : null;
if ( is_array( $parent_id_parts ) && ! empty( $parent_id_parts['id'] ) && is_int( $parent_id_parts['id'] ) ) {
$insert_post_args['post_parent'] = absint( $parent_id_parts['id'] );
}
if ( ! empty( $input['menuOrder'] ) ) {
$insert_post_args['menu_order'] = $input['menuOrder'];
}
if ( ! empty( $input['mimeType'] ) ) {
$insert_post_args['post_mime_type'] = $input['mimeType'];
}
if ( ! empty( $input['commentCount'] ) ) {
$insert_post_args['comment_count'] = $input['commentCount'];
}
$insert_post_args = apply_filters( 'graphql_post_object_insert_post_args', $insert_post_args, $input, $post_type_object, $mutation_name );
return $insert_post_args;
}
public static function update_additional_post_object_data( $new_post_id, $input, $post_type_object, $mutation_name ) {
self::set_edit_lock( $new_post_id );
update_post_meta( $new_post_id, '_edit_last', get_current_user_id() );
if ( ! empty( $input['desiredSlug'] ) ) {
update_post_meta( $new_post_id, '_wp_desired_post_slug', $input['desiredSlug'] );
}
do_action( 'graphql_post_object_mutation_update_additional_data', $new_post_id, $input, $post_type_object, $mutation_name );
}
public static function set_edit_lock( $post_id ) {
$post = get_post( $post_id );
$user_id = get_current_user_id();
if ( empty( $post ) ) {
return false;
}
if ( 0 === $user_id ) {
return false;
}
$now = time();
$lock = "$now:$user_id";
update_post_meta( $post->ID, '_edit_lock', $lock );
return [ $now, $user_id ];
}
}