| 1 |
<?php |
| 2 |
namespace WPGraphQL\Mutation; |
| 3 |
|
| 4 |
use GraphQL\Error\UserError; |
| 5 |
use GraphQL\Type\Definition\ResolveInfo; |
| 6 |
use WPGraphQL\AppContext; |
| 7 |
use WPGraphQL\Data\TermObjectMutation; |
| 8 |
use WPGraphQL\Utils\Utils; |
| 9 |
use WP_Taxonomy; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class TermObjectUpdate |
| 13 |
* |
| 14 |
* @package WPGraphQL\Mutation |
| 15 |
*/ |
| 16 |
class TermObjectUpdate { |
| 17 |
/** |
| 18 |
* Registers the TermObjectUpdate mutation. |
| 19 |
* |
| 20 |
* @param \WP_Taxonomy $taxonomy The Taxonomy the mutation is registered for. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public static function register_mutation( WP_Taxonomy $taxonomy ) { |
| 25 |
$mutation_name = 'update' . ucwords( $taxonomy->graphql_single_name ); |
| 26 |
register_graphql_mutation( |
| 27 |
$mutation_name, |
| 28 |
[ |
| 29 |
'inputFields' => self::get_input_fields( $taxonomy ), |
| 30 |
'outputFields' => self::get_output_fields( $taxonomy ), |
| 31 |
'mutateAndGetPayload' => self::mutate_and_get_payload( $taxonomy, $mutation_name ), |
| 32 |
] |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Defines the mutation input field configuration. |
| 38 |
* |
| 39 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 40 |
* |
| 41 |
* @return array<string,array<string,mixed>> |
| 42 |
*/ |
| 43 |
public static function get_input_fields( WP_Taxonomy $taxonomy ) { |
| 44 |
return array_merge( |
| 45 |
TermObjectCreate::get_input_fields( $taxonomy ), |
| 46 |
[ |
| 47 |
'name' => [ |
| 48 |
'type' => 'String', |
| 49 |
'description' => static function () use ( $taxonomy ) { |
| 50 |
// Translators: The placeholder is the name of the taxonomy for the object being mutated |
| 51 |
return sprintf( __( 'The name of the %1$s object to mutate', 'wp-graphql' ), $taxonomy->name ); |
| 52 |
}, |
| 53 |
], |
| 54 |
'id' => [ |
| 55 |
'type' => [ |
| 56 |
'non_null' => 'ID', |
| 57 |
], |
| 58 |
'description' => static function () use ( $taxonomy ) { |
| 59 |
// Translators: The placeholder is the taxonomy of the term being updated |
| 60 |
return sprintf( __( 'The ID of the %1$s object to update', 'wp-graphql' ), $taxonomy->graphql_single_name ); |
| 61 |
}, |
| 62 |
], |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Defines the mutation output field configuration. |
| 69 |
* |
| 70 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 71 |
* |
| 72 |
* @return array<string,array<string,mixed>> |
| 73 |
*/ |
| 74 |
public static function get_output_fields( WP_Taxonomy $taxonomy ) { |
| 75 |
return TermObjectCreate::get_output_fields( $taxonomy ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Defines the mutation data modification closure. |
| 80 |
* |
| 81 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 82 |
* @param string $mutation_name The name of the mutation. |
| 83 |
* |
| 84 |
* @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed> |
| 85 |
*/ |
| 86 |
public static function mutate_and_get_payload( WP_Taxonomy $taxonomy, $mutation_name ) { |
| 87 |
return static function ( $input, AppContext $context, ResolveInfo $info ) use ( $taxonomy, $mutation_name ) { |
| 88 |
$term_id = Utils::get_database_id_from_id( $input['id'] ); |
| 89 |
|
| 90 |
/** |
| 91 |
* Ensure the type for the Global ID matches the type being mutated |
| 92 |
*/ |
| 93 |
if ( empty( $term_id ) ) { |
| 94 |
// Translators: The placeholder is the name of the taxonomy for the term being edited |
| 95 |
throw new UserError( esc_html( sprintf( __( 'The ID passed is not for a %1$s object', 'wp-graphql' ), $taxonomy->graphql_single_name ) ) ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get the existing term |
| 100 |
*/ |
| 101 |
$existing_term = get_term( $term_id, $taxonomy->name ); |
| 102 |
|
| 103 |
/** |
| 104 |
* If there was an error getting the existing term, return the error message |
| 105 |
*/ |
| 106 |
if ( ! $existing_term instanceof \WP_Term ) { |
| 107 |
if ( is_wp_error( $existing_term ) ) { |
| 108 |
$error_message = $existing_term->get_error_message(); |
| 109 |
if ( ! empty( $error_message ) ) { |
| 110 |
throw new UserError( esc_html( $error_message ) ); |
| 111 |
} else { |
| 112 |
// Translators: The placeholder is the name of the taxonomy for the term being deleted |
| 113 |
throw new UserError( esc_html( sprintf( __( 'The %1$s node failed to update', 'wp-graphql' ), $taxonomy->name ) ) ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Translators: The placeholder is the name of the taxonomy for the term being deleted |
| 118 |
throw new UserError( esc_html( sprintf( __( 'The %1$s node failed to update', 'wp-graphql' ), $taxonomy->name ) ) ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( $taxonomy->name !== $existing_term->taxonomy ) { |
| 122 |
// translators: The first placeholder is an ID and the second placeholder is the name of the post type being edited |
| 123 |
throw new UserError( esc_html( sprintf( __( 'The id %1$d is not of the type "%2$s"', 'wp-graphql' ), $term_id, $taxonomy->name ) ) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Ensure the user has permission to edit terms |
| 128 |
*/ |
| 129 |
if ( ! current_user_can( 'edit_term', $existing_term->term_id ) ) { |
| 130 |
// Translators: The placeholder is the name of the taxonomy for the term being deleted |
| 131 |
throw new UserError( esc_html( sprintf( __( 'You do not have permission to update %1$s', 'wp-graphql' ), $taxonomy->graphql_plural_name ) ) ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Prepare the $args for mutation |
| 136 |
*/ |
| 137 |
$args = TermObjectMutation::prepare_object( $input, $taxonomy, $mutation_name ); |
| 138 |
|
| 139 |
if ( ! empty( $args ) ) { |
| 140 |
|
| 141 |
/** |
| 142 |
* Update the term |
| 143 |
*/ |
| 144 |
$update = wp_update_term( $existing_term->term_id, $taxonomy->name, wp_slash( (array) $args ) ); |
| 145 |
|
| 146 |
/** |
| 147 |
* Respond with any errors |
| 148 |
*/ |
| 149 |
if ( is_wp_error( $update ) ) { |
| 150 |
// Translators: the placeholder is the name of the taxonomy |
| 151 |
throw new UserError( esc_html( sprintf( __( 'The %1$s failed to update', 'wp-graphql' ), $taxonomy->name ) ) ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Fires an action when a term is updated via a GraphQL Mutation |
| 157 |
* |
| 158 |
* @param int $term_id The ID of the term object that was mutated |
| 159 |
* @param \WP_Taxonomy $taxonomy The taxonomy of the term being updated |
| 160 |
* @param array<string,mixed> $args The args used to update the term |
| 161 |
* @param string $mutation_name The name of the mutation being performed (create, update, delete, etc) |
| 162 |
* @param \WPGraphQL\AppContext $context The AppContext passed down the resolve tree |
| 163 |
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down the resolve tree |
| 164 |
* |
| 165 |
* @hookGroup models |
| 166 |
* @since 0.0.5 |
| 167 |
*/ |
| 168 |
do_action( 'graphql_update_term', $existing_term->term_id, $taxonomy, $args, $mutation_name, $context, $info ); |
| 169 |
|
| 170 |
/** |
| 171 |
* Fires an action when a term is updated via a GraphQL Mutation |
| 172 |
* |
| 173 |
* @param int $term_id The ID of the term object that was mutated |
| 174 |
* @param array<string,mixed> $args The args used to update the term |
| 175 |
* @param string $mutation_name The name of the mutation being performed (create, update, delete, etc) |
| 176 |
* @param \WPGraphQL\AppContext $context The AppContext passed down the resolve tree |
| 177 |
* @param \GraphQL\Type\Definition\ResolveInfo $info The ResolveInfo passed down the resolve tree |
| 178 |
* |
| 179 |
* @hookGroup models |
| 180 |
* @since 0.0.5 |
| 181 |
*/ |
| 182 |
do_action( "graphql_update_{$taxonomy->name}", $existing_term->term_id, $args, $mutation_name, $context, $info ); |
| 183 |
|
| 184 |
/** |
| 185 |
* Return the payload |
| 186 |
*/ |
| 187 |
return [ |
| 188 |
'termId' => $existing_term->term_id, |
| 189 |
]; |
| 190 |
}; |
| 191 |
} |
| 192 |
} |
| 193 |
|