@@ 18-80 (lines=63) @@ | ||
15 | use WPGraphQL\AppContext; |
|
16 | use GraphQL\Error\UserError; |
|
17 | ||
18 | class DatetimeDelete { |
|
19 | ||
20 | /** |
|
21 | * Defines the mutation data modification closure. |
|
22 | * |
|
23 | * @param EEM_Datetime $model |
|
24 | * @param Datetime $type |
|
25 | * @return callable |
|
26 | */ |
|
27 | public static function mutateAndGetPayload(EEM_Datetime $model, Datetime $type) |
|
28 | { |
|
29 | /** |
|
30 | * Deletes an entity. |
|
31 | * |
|
32 | * @param array $input The input for the mutation |
|
33 | * @param AppContext $context The AppContext passed down to all resolvers |
|
34 | * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
|
35 | * |
|
36 | * @throws UserError |
|
37 | * @throws ReflectionException |
|
38 | * @throws InvalidArgumentException |
|
39 | * @throws InvalidInterfaceException |
|
40 | * @throws InvalidDataTypeException |
|
41 | * @throws EE_Error |
|
42 | */ |
|
43 | return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) |
|
44 | { |
|
45 | /** |
|
46 | * Stop now if a user isn't allowed to create a datetime. |
|
47 | */ |
|
48 | if (! current_user_can('ee_edit_events')) { |
|
49 | // translators: the %1$s is the name of the object being mutated |
|
50 | throw new UserError(sprintf(__('Sorry, you are not allowed to edit %1$s', 'event_espresso' ), $type->name())); |
|
51 | } |
|
52 | ||
53 | $id = ! empty($input['id']) ? absint($input['id']) : 0; |
|
54 | $entity = null; |
|
55 | ||
56 | if ($id) { |
|
57 | $entity = $model->get_one_by_ID($id); |
|
58 | } |
|
59 | ||
60 | /** |
|
61 | * If there's no existing datetime, throw an exception |
|
62 | */ |
|
63 | if (! $id || ! ($entity instanceof EE_Datetime)) { |
|
64 | // translators: the placeholder is the name of the type being updated |
|
65 | throw new UserError(sprintf(__( 'No %1$s could be found to delete', 'event_espresso' ), $type->name())); |
|
66 | } |
|
67 | ||
68 | // Delete the entity |
|
69 | $result = ! empty($input['deletePermanently']) ? $entity->delete_permanently() : $entity->delete(); |
|
70 | ||
71 | if (empty($result)) { |
|
72 | throw new UserError(__( 'The object failed to delete but no error was provided', 'event_espresso')); |
|
73 | } |
|
74 | ||
75 | return [ |
|
76 | 'deleted' => $entity, |
|
77 | ]; |
|
78 | }; |
|
79 | } |
|
80 | } |
|
81 |
@@ 18-80 (lines=63) @@ | ||
15 | use WPGraphQL\AppContext; |
|
16 | use GraphQL\Error\UserError; |
|
17 | ||
18 | class TicketDelete { |
|
19 | ||
20 | /** |
|
21 | * Defines the mutation data modification closure. |
|
22 | * |
|
23 | * @param EEM_Ticket $model |
|
24 | * @param Ticket $type |
|
25 | * @return callable |
|
26 | */ |
|
27 | public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type) |
|
28 | { |
|
29 | /** |
|
30 | * Deletes an entity. |
|
31 | * |
|
32 | * @param array $input The input for the mutation |
|
33 | * @param AppContext $context The AppContext passed down to all resolvers |
|
34 | * @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
|
35 | * |
|
36 | * @throws UserError |
|
37 | * @throws ReflectionException |
|
38 | * @throws InvalidArgumentException |
|
39 | * @throws InvalidInterfaceException |
|
40 | * @throws InvalidDataTypeException |
|
41 | * @throws EE_Error |
|
42 | */ |
|
43 | return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) |
|
44 | { |
|
45 | /** |
|
46 | * Stop now if a user isn't allowed to create a datetime. |
|
47 | */ |
|
48 | if (! current_user_can('ee_edit_events')) { |
|
49 | // translators: the %1$s is the name of the object being mutated |
|
50 | throw new UserError(sprintf(__('Sorry, you are not allowed to edit %1$s', 'event_espresso' ), $type->name())); |
|
51 | } |
|
52 | ||
53 | $id = ! empty($input['id']) ? absint($input['id']) : 0; |
|
54 | $entity = null; |
|
55 | ||
56 | if ($id) { |
|
57 | $entity = $model->get_one_by_ID($id); |
|
58 | } |
|
59 | ||
60 | /** |
|
61 | * If there's no existing datetime, throw an exception |
|
62 | */ |
|
63 | if (! $id || ! ($entity instanceof EE_Ticket)) { |
|
64 | // translators: the placeholder is the name of the type being updated |
|
65 | throw new UserError(sprintf(__( 'No %1$s could be found to delete', 'event_espresso' ), $type->name())); |
|
66 | } |
|
67 | ||
68 | // Delete the entity |
|
69 | $result = ! empty($input['deletePermanently']) ? $entity->delete_permanently() : $entity->delete(); |
|
70 | ||
71 | if (empty($result)) { |
|
72 | throw new UserError(__( 'The object failed to delete but no error was provided', 'event_espresso')); |
|
73 | } |
|
74 | ||
75 | return [ |
|
76 | 'deleted' => $entity, |
|
77 | ]; |
|
78 | }; |
|
79 | } |
|
80 | } |
|
81 |