PluginProbe
WPGraphQL / 2.22.1
WPGraphQL v2.22.1
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Data / TermObjectMutation.php

TermObjectMutation.php in WPGraphQL 2.22.1, at src/Data/TermObjectMutation.php

114 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPGraphQL\Data;
4
5 use GraphQL\Error\UserError;
6 use WPGraphQL\Utils\Utils;
7 use WP_Taxonomy;
8
9 class TermObjectMutation {
10
11 /**
12 * This prepares the object to be mutated - ensures data is safe to be saved,
13 * and mapped from input args to WordPress $args
14 *
15 * @throws \GraphQL\Error\UserError User error for invalid term.
16 *
17 * @param array<string,mixed> $input The input from the GraphQL Request
18 * @param \WP_Taxonomy $taxonomy The Taxonomy object for the type of term being mutated
19 * @param string $mutation_name The name of the mutation (create, update, etc)
20 *
21 * @return array<string,mixed>
22 */
23 public static function prepare_object( array $input, WP_Taxonomy $taxonomy, string $mutation_name ) {
24 $insert_args = [];
25
26 /**
27 * Set the taxonomy for insert
28 */
29 $insert_args['taxonomy'] = $taxonomy->name;
30
31 /**
32 * Prepare the data for inserting the term
33 */
34 if ( ! empty( $input['aliasOf'] ) ) {
35 $insert_args['alias_of'] = $input['aliasOf'];
36 }
37
38 if ( ! empty( $input['name'] ) ) {
39 $insert_args['name'] = $input['name'];
40 }
41
42 if ( ! empty( $input['description'] ) ) {
43 $insert_args['description'] = $input['description'];
44 }
45
46 if ( ! empty( $input['slug'] ) ) {
47 $insert_args['slug'] = $input['slug'];
48 }
49
50 /**
51 * If both parentId and parentDatabaseId are provided, throw an error
52 */
53 if ( isset( $input['parentId'] ) && isset( $input['parentDatabaseId'] ) ) {
54 throw new UserError( esc_html__( 'Please provide only one of parentId or parentDatabaseId', 'wp-graphql' ) );
55 }
56
57 /**
58 * Handle parentId input
59 */
60 if ( isset( $input['parentId'] ) ) {
61 /**
62 * Convert parent ID to WordPress ID
63 */
64 $parent_id = Utils::get_database_id_from_id( $input['parentId'] );
65
66 if ( false === $parent_id ) {
67 throw new UserError( esc_html__( 'The parent ID is not a valid ID', 'wp-graphql' ) );
68 }
69
70 /**
71 * Ensure there's actually a parent term to be associated with
72 */
73 $parent_term = get_term( absint( $parent_id ), $taxonomy->name );
74
75 if ( 0 !== $parent_id && ! $parent_term instanceof \WP_Term ) {
76 throw new UserError( esc_html__( 'The parent does not exist', 'wp-graphql' ) );
77 }
78
79 $insert_args['parent'] = 0 !== $parent_id ? $parent_term->term_id : 0;
80 }
81
82 /**
83 * Handle parentDatabaseId input
84 */
85 if ( isset( $input['parentDatabaseId'] ) ) {
86 $parent_id = absint( $input['parentDatabaseId'] );
87
88 // If parent_id is not 0, verify the term exists
89 if ( 0 !== $parent_id ) {
90 $parent_term = get_term( $parent_id, $taxonomy->name );
91
92 if ( ! $parent_term instanceof \WP_Term ) {
93 throw new UserError( esc_html__( 'The parent does not exist', 'wp-graphql' ) );
94 }
95 }
96
97 $insert_args['parent'] = $parent_id;
98 }
99
100 /**
101 * Filter the $insert_args
102 *
103 * @param array<string,mixed> $insert_args The array of input args that will be passed to the functions that insert terms
104 * @param array<string,mixed> $input The data that was entered as input for the mutation
105 * @param \WP_Taxonomy $taxonomy The taxonomy object of the term being mutated
106 * @param string $mutation_name The name of the mutation being performed (create, edit, etc)
107 *
108 * @hookGroup models
109 * @since 0.0.5
110 */
111 return apply_filters( 'graphql_term_object_insert_term_args', $insert_args, $input, $taxonomy, $mutation_name );
112 }
113 }
114