• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / #951

12 Feb 2025 08:05PM UTC coverage: 83.023% (-0.005%) from 83.028%
#951

push

php-coveralls

web-flow
Merge pull request #3310 from wp-graphql/release/v1.32.1

release: v1.32.1

17 of 20 new or added lines in 3 files covered. (85.0%)

13067 of 15739 relevant lines covered (83.02%)

300.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/Data/TermObjectMutation.php
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 = [];
9✔
25

26
                /**
27
                 * Set the taxonomy for insert
28
                 */
29
                $insert_args['taxonomy'] = $taxonomy->name;
9✔
30

31
                /**
32
                 * Prepare the data for inserting the term
33
                 */
34
                if ( ! empty( $input['aliasOf'] ) ) {
9✔
35
                        $insert_args['alias_of'] = $input['aliasOf'];
×
36
                }
37

38
                if ( ! empty( $input['name'] ) ) {
9✔
39
                        $insert_args['name'] = $input['name'];
6✔
40
                }
41

42
                if ( ! empty( $input['description'] ) ) {
9✔
43
                        $insert_args['description'] = $input['description'];
5✔
44
                }
45

46
                if ( ! empty( $input['slug'] ) ) {
9✔
47
                        $insert_args['slug'] = $input['slug'];
1✔
48
                }
49

50
                /**
51
                 * If both parentId and parentDatabaseId are provided, throw an error
52
                 */
53
                if ( isset( $input['parentId'] ) && isset( $input['parentDatabaseId'] ) ) {
9✔
54
                        throw new UserError( esc_html__( 'Please provide only one of parentId or parentDatabaseId', 'wp-graphql' ) );
1✔
55
                }
56

57
                /**
58
                 * Handle parentId input
59
                 */
60
                if ( isset( $input['parentId'] ) ) {
8✔
61
                        /**
62
                         * Convert parent ID to WordPress ID
63
                         */
64
                        $parent_id = Utils::get_database_id_from_id( $input['parentId'] );
2✔
65

66
                        if ( false === $parent_id ) {
2✔
67
                                throw new UserError( esc_html__( 'The parent ID is not a valid ID', 'wp-graphql' ) );
1✔
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 );
2✔
74

75
                        if ( 0 !== $parent_id && ! $parent_term instanceof \WP_Term ) {
2✔
76
                                throw new UserError( esc_html__( 'The parent does not exist', 'wp-graphql' ) );
1✔
77
                        }
78

79
                        $insert_args['parent'] = 0 !== $parent_id ? $parent_term->term_id : 0;
2✔
80
                }
81

82
                /**
83
                 * Handle parentDatabaseId input
84
                 */
85
                if ( isset( $input['parentDatabaseId'] ) ) {
8✔
86
                        $parent_id = absint( $input['parentDatabaseId'] );
1✔
87

88
                        // If parent_id is not 0, verify the term exists
89
                        if ( 0 !== $parent_id ) {
1✔
NEW
90
                                $parent_term = get_term( $parent_id, $taxonomy->name );
×
91

NEW
92
                                if ( ! $parent_term instanceof \WP_Term ) {
×
NEW
93
                                        throw new UserError( esc_html__( 'The parent does not exist', 'wp-graphql' ) );
×
94
                                }
95
                        }
96

97
                        $insert_args['parent'] = $parent_id;
1✔
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
                return apply_filters( 'graphql_term_object_insert_term_args', $insert_args, $input, $taxonomy, $mutation_name );
8✔
109
        }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc