Overview
  • Namespace
  • Class

Namespaces

  • WPGraphQL
    • Data
    • Type
      • Avatar
      • Comment
        • Connection
      • CommentAuthor
      • EditLock
      • Enum
      • MediaItem
        • Mutation
      • Plugin
        • Connection
      • PostObject
        • Connection
        • Mutation
      • PostType
      • Setting
      • Taxonomy
      • TermObject
        • Connection
        • Mutation
      • Theme
        • Connection
      • Union
      • User
        • Connection
        • Mutation
    • Utils

Classes

  • WPGraphQL\AppContext
  • WPGraphQL\Data\Config
  • WPGraphQL\Data\ConnectionResolver
  • WPGraphQL\Data\DataSource
  • WPGraphQL\Data\Loader
  • WPGraphQL\Router
  • WPGraphQL\Type\Avatar\AvatarType
  • WPGraphQL\Type\Comment\CommentQuery
  • WPGraphQL\Type\Comment\CommentType
  • WPGraphQL\Type\Comment\Connection\CommentConnectionArgs
  • WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition
  • WPGraphQL\Type\Comment\Connection\CommentConnectionResolver
  • WPGraphQL\Type\CommentAuthor\CommentAuthorQuery
  • WPGraphQL\Type\CommentAuthor\CommentAuthorType
  • WPGraphQL\Type\EditLock\EditLockType
  • WPGraphQL\Type\Enum\MediaItemStatusEnumType
  • WPGraphQL\Type\Enum\MimeTypeEnumType
  • WPGraphQL\Type\Enum\PostObjectFieldFormatEnumType
  • WPGraphQL\Type\Enum\PostStatusEnumType
  • WPGraphQL\Type\Enum\PostTypeEnumType
  • WPGraphQL\Type\Enum\RelationEnumType
  • WPGraphQL\Type\Enum\TaxonomyEnumType
  • WPGraphQL\Type\MediaItem\MediaItemType
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemCreate
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemDelete
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemMutation
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemUpdate
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionDefinition
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionResolver
  • WPGraphQL\Type\Plugin\PluginQuery
  • WPGraphQL\Type\Plugin\PluginType
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgs
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgsDateQuery
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionDefinition
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionResolver
  • WPGraphQL\Type\PostObject\Mutation\PostObjectCreate
  • WPGraphQL\Type\PostObject\Mutation\PostObjectDelete
  • WPGraphQL\Type\PostObject\Mutation\PostObjectMutation
  • WPGraphQL\Type\PostObject\Mutation\PostObjectUpdate
  • WPGraphQL\Type\PostObject\Mutation\TermObjectDelete
  • WPGraphQL\Type\PostObject\PostObjectQuery
  • WPGraphQL\Type\PostObject\PostObjectType
  • WPGraphQL\Type\PostType\PostTypeType
  • WPGraphQL\Type\RootMutationType
  • WPGraphQL\Type\RootQueryType
  • WPGraphQL\Type\Setting\SettingQuery
  • WPGraphQL\Type\Setting\SettingType
  • WPGraphQL\Type\Taxonomy\TaxonomyType
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionArgs
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionDefinition
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionResolver
  • WPGraphQL\Type\TermObject\Mutation\TermObjectCreate
  • WPGraphQL\Type\TermObject\Mutation\TermObjectMutation
  • WPGraphQL\Type\TermObject\Mutation\TermObjectUpdate
  • WPGraphQL\Type\TermObject\TermObjectQuery
  • WPGraphQL\Type\TermObject\TermObjectType
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionDefinition
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionResolver
  • WPGraphQL\Type\Theme\ThemeType
  • WPGraphQL\Type\Union\CommentAuthorUnionType
  • WPGraphQL\Type\Union\PostObjectUnionType
  • WPGraphQL\Type\Union\TermObjectUnionType
  • WPGraphQL\Type\User\Connection\UserConnectionArgs
  • WPGraphQL\Type\User\Connection\UserConnectionDefinition
  • WPGraphQL\Type\User\Connection\UserConnectionResolver
  • WPGraphQL\Type\User\Mutation\UserCreate
  • WPGraphQL\Type\User\Mutation\UserDelete
  • WPGraphQL\Type\User\Mutation\UserMutation
  • WPGraphQL\Type\User\Mutation\UserUpdate
  • WPGraphQL\Type\User\UserQuery
  • WPGraphQL\Type\User\UserType
  • WPGraphQL\Type\WPEnumType
  • WPGraphQL\Type\WPInputObjectType
  • WPGraphQL\Type\WPObjectType
  • WPGraphQL\Types
  • WPGraphQL\Utils\InstrumentSchema
  • WPGraphQL\WPSchema

Interfaces

  • WPGraphQL\Data\ConnectionResolverInterface
  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
 *
 * @package WPGraphQL\Type\PostObject
 */
class PostObjectMutation {

    /**
     * Holds the input_fields configuration
     *
     * @var array
     */
    private static $input_fields = [];

    /**
     * @param $post_type_object
     *
     * @return mixed|array|null $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' ),
                ],
            ];

            /**
             * Filters the mutation input fields for the object type
             *
             * @param array         $input_fields     The array of input fields
             * @param \WP_Post_Type $post_type_object The post_type object for the type of Post being mutated
             */
            self::$input_fields[ $post_type_object->graphql_single_name ] = apply_filters( 'graphql_post_object_mutation_input_fields', $input_fields, $post_type_object );


        } // End if().

        return ! empty( self::$input_fields[ $post_type_object->graphql_single_name ] ) ? self::$input_fields[ $post_type_object->graphql_single_name ] : null;

    }

    /**
     * This handles inserting the post object
     *
     * @param array         $input            The input for the mutation
     * @param \WP_Post_Type $post_type_object The post_type_object for the type of post being mutated
     * @param string        $mutation_name    The name of the mutation being performed
     *
     * @return array $insert_post_args
     * @throws \Exception
     */
    public static function prepare_post_object( $input, $post_type_object, $mutation_name ) {

        /**
         * Set the post_type for the insert
         */
        $insert_post_args['post_type'] = $post_type_object->name;

        /**
         * Prepare the data for inserting the post
         * NOTE: These are organized in the same order as: https://developer.wordpress.org/reference/functions/wp_insert_post/
         */
        $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'];
        }

        /**
         * Filter the $insert_post_args
         *
         * @param array         $insert_post_args The array of $input_post_args that will be passed to wp_insert_post
         * @param array         $input            The data that was entered as input for the mutation
         * @param \WP_Post_Type $post_type_object The post_type_object that the mutation is affecting
         * @param string        $mutation_type    The type of mutation being performed (create, edit, etc)
         */
        $insert_post_args = apply_filters( 'graphql_post_object_insert_post_args', $insert_post_args, $input, $post_type_object, $mutation_name );

        /**
         * Return the $args
         */
        return $insert_post_args;

    }

    /**
     * This updates additional data related to a post object, such as postmeta, term relationships, etc.
     *
     * @param $new_post_id
     * @param $input
     * @param $post_type_object
     * @param $mutation_name
     */
    public static function update_additional_post_object_data( $new_post_id, $input, $post_type_object, $mutation_name ) {

        /**
         * Set the post_lock for the $new_post_id
         */
        self::set_edit_lock( $new_post_id );

        /**
         * Update the _edit_last field
         */
        update_post_meta( $new_post_id, '_edit_last', get_current_user_id() );

        /**
         * Update the postmeta fields
         */
        if ( ! empty( $input['desiredSlug'] ) ) {
            update_post_meta( $new_post_id, '_wp_desired_post_slug', $input['desiredSlug'] );
        }

        /**
         * Run an action after the additional data has been updated. This is a great spot to hook into to
         * update additional data related to postObjects, such as setting relationships, updating additional postmeta,
         * or sending emails to Kevin. . .whatever you need to do with the postObject.
         *
         * @param int           $new_post_id      The ID of the postObject being mutated
         * @param array         $input            The input for the mutation
         * @param \WP_Post_Type $post_type_object The Post Type Object for the type of post being mutated
         * @param string        $mutation_name    The name of the mutation (ex: create, update, delete)
         */
        do_action( 'graphql_post_object_mutation_update_additional_data', $new_post_id, $input, $post_type_object, $mutation_name );

    }

    /**
     * This is a copy of the wp_set_post_lock function that exists in WordPress core, but is not
     * accessible because that part of WordPress is never loaded for WPGraphQL executions
     *
     * Mark the post as currently being edited by the current user
     *
     * @param int $post_id ID of the post being edited.
     *
     * @return array|false Array of the lock time and user ID. False if the post does not exist, or
     *                     there is no current user.
     */
    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 ];

    }

}
API documentation generated by ApiGen