| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Mutation; |
| 4 |
|
| 5 |
use GraphQL\Error\UserError; |
| 6 |
use GraphQLRelay\Relay; |
| 7 |
use WPGraphQL\Model\Term; |
| 8 |
use WPGraphQL\Utils\Utils; |
| 9 |
use WP_Taxonomy; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class TermObjectDelete |
| 13 |
* |
| 14 |
* @package WPGraphQL\Mutation |
| 15 |
*/ |
| 16 |
class TermObjectDelete { |
| 17 |
/** |
| 18 |
* Registers the TermObjectDelete mutation. |
| 19 |
* |
| 20 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public static function register_mutation( WP_Taxonomy $taxonomy ) { |
| 25 |
$mutation_name = 'delete' . ucfirst( $taxonomy->graphql_single_name ); |
| 26 |
|
| 27 |
register_graphql_mutation( |
| 28 |
$mutation_name, |
| 29 |
[ |
| 30 |
'inputFields' => self::get_input_fields( $taxonomy ), |
| 31 |
'outputFields' => self::get_output_fields( $taxonomy ), |
| 32 |
'mutateAndGetPayload' => self::mutate_and_get_payload( $taxonomy, $mutation_name ), |
| 33 |
] |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Defines the mutation input field configuration. |
| 39 |
* |
| 40 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 41 |
* |
| 42 |
* @return array<string,array<string,mixed>> |
| 43 |
*/ |
| 44 |
public static function get_input_fields( WP_Taxonomy $taxonomy ) { |
| 45 |
return [ |
| 46 |
'id' => [ |
| 47 |
'type' => [ |
| 48 |
'non_null' => 'ID', |
| 49 |
], |
| 50 |
'description' => static function () use ( $taxonomy ) { |
| 51 |
// translators: The placeholder is the name of the taxonomy for the term being deleted |
| 52 |
return sprintf( __( 'The ID of the %1$s to delete', 'wp-graphql' ), $taxonomy->graphql_single_name ); |
| 53 |
}, |
| 54 |
], |
| 55 |
]; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Defines the mutation output field configuration. |
| 60 |
* |
| 61 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 62 |
* |
| 63 |
* @return array<string,array<string,mixed>> |
| 64 |
*/ |
| 65 |
public static function get_output_fields( WP_Taxonomy $taxonomy ) { |
| 66 |
return [ |
| 67 |
'deletedId' => [ |
| 68 |
'type' => 'ID', |
| 69 |
'description' => static function () { |
| 70 |
return __( 'The ID of the deleted object', 'wp-graphql' ); |
| 71 |
}, |
| 72 |
'resolve' => static function ( $payload ) { |
| 73 |
$deleted = (object) $payload['termObject']; |
| 74 |
|
| 75 |
return ! empty( $deleted->term_id ) ? Relay::toGlobalId( 'term', $deleted->term_id ) : null; |
| 76 |
}, |
| 77 |
], |
| 78 |
$taxonomy->graphql_single_name => [ |
| 79 |
'type' => $taxonomy->graphql_single_name, |
| 80 |
'description' => static function () { |
| 81 |
return __( 'The deleted term object', 'wp-graphql' ); |
| 82 |
}, |
| 83 |
'resolve' => static function ( $payload ) { |
| 84 |
return new Term( $payload['termObject'] ); |
| 85 |
}, |
| 86 |
], |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Defines the mutation data modification closure. |
| 92 |
* |
| 93 |
* @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation. |
| 94 |
* @param string $mutation_name The name of the mutation. |
| 95 |
* |
| 96 |
* @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed> |
| 97 |
*/ |
| 98 |
public static function mutate_and_get_payload( WP_Taxonomy $taxonomy, string $mutation_name ) { |
| 99 |
return static function ( $input ) use ( $taxonomy ) { |
| 100 |
// Get the database ID for the comment. |
| 101 |
$term_id = Utils::get_database_id_from_id( $input['id'] ); |
| 102 |
|
| 103 |
if ( empty( $term_id ) ) { |
| 104 |
// Translators: The placeholder is the name of the taxonomy for the term being deleted |
| 105 |
throw new UserError( esc_html( sprintf( __( 'The ID for the %1$s was not valid', 'wp-graphql' ), $taxonomy->graphql_single_name ) ) ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get the term before deleting it |
| 110 |
*/ |
| 111 |
$term_object = get_term( $term_id, $taxonomy->name ); |
| 112 |
|
| 113 |
if ( ! $term_object instanceof \WP_Term ) { |
| 114 |
throw new UserError( esc_html__( 'The ID passed is invalid', 'wp-graphql' ) ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Ensure the type for the Global ID matches the type being mutated |
| 119 |
*/ |
| 120 |
if ( $taxonomy->name !== $term_object->taxonomy ) { |
| 121 |
// Translators: The placeholder is the name of the taxonomy for the term being edited |
| 122 |
throw new UserError( esc_html( sprintf( __( 'The ID passed is not for a %1$s object', 'wp-graphql' ), $taxonomy->graphql_single_name ) ) ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Ensure the user can delete terms of this taxonomy |
| 127 |
*/ |
| 128 |
if ( ! current_user_can( 'delete_term', $term_object->term_id ) ) { |
| 129 |
// Translators: The placeholder is the name of the taxonomy for the term being deleted |
| 130 |
throw new UserError( esc_html( sprintf( __( 'You do not have permission to delete %1$s', 'wp-graphql' ), $taxonomy->graphql_plural_name ) ) ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Delete the term and get the response |
| 135 |
*/ |
| 136 |
$taxonomy_name = $term_object->taxonomy; |
| 137 |
|
| 138 |
// A term always belongs to a taxonomy; this guards the unreachable empty |
| 139 |
// case so the taxonomy name is provably non-empty. |
| 140 |
if ( '' === $taxonomy_name ) { |
| 141 |
throw new UserError( esc_html__( 'The ID passed is invalid', 'wp-graphql' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
$deleted = wp_delete_term( $term_id, $taxonomy_name ); |
| 145 |
|
| 146 |
/** |
| 147 |
* If there was an error deleting the term, get the error message and return it |
| 148 |
*/ |
| 149 |
if ( is_wp_error( $deleted ) ) { |
| 150 |
$error_message = $deleted->get_error_message(); |
| 151 |
if ( ! empty( $error_message ) ) { |
| 152 |
throw new UserError( esc_html( $error_message ) ); |
| 153 |
} else { |
| 154 |
// Translators: The placeholder is the name of the taxonomy for the term being deleted |
| 155 |
throw new UserError( esc_html( sprintf( __( 'The %1$s failed to delete but no error was provided', 'wp-graphql' ), $taxonomy->name ) ) ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Return the term object that was retrieved prior to deletion |
| 161 |
*/ |
| 162 |
return [ |
| 163 |
'termObject' => $term_object, |
| 164 |
]; |
| 165 |
}; |
| 166 |
} |
| 167 |
} |
| 168 |
|