Endpoint.php
4 years ago
FormActions.php
3 years ago
ListDonationForms.php
3 years ago
SwitchDonationFormView.php
4 years ago
FormActions.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Endpoints; |
| 4 | |
| 5 | use WP_Error; |
| 6 | use WP_REST_Request; |
| 7 | use WP_REST_Response; |
| 8 | |
| 9 | /** |
| 10 | * @since 2.19.0 |
| 11 | */ |
| 12 | class FormActions extends Endpoint |
| 13 | { |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $endpoint = 'admin/forms/(?P<action>[\S]+)'; |
| 18 | |
| 19 | /** |
| 20 | * @inheritDoc |
| 21 | */ |
| 22 | public function registerRoute() |
| 23 | { |
| 24 | register_rest_route( |
| 25 | 'give-api/v2', |
| 26 | $this->endpoint, |
| 27 | [ |
| 28 | [ |
| 29 | 'methods' => ['POST', 'UPDATE', 'DELETE'], |
| 30 | 'callback' => [$this, 'handleRequest'], |
| 31 | 'permission_callback' => [$this, 'permissionsCheck'], |
| 32 | ], |
| 33 | 'args' => [ |
| 34 | 'action' => [ |
| 35 | 'type' => 'string', |
| 36 | 'required' => true, |
| 37 | 'enum' => [ |
| 38 | 'trash', |
| 39 | 'restore', |
| 40 | 'delete', |
| 41 | 'duplicate', |
| 42 | 'edit', |
| 43 | ], |
| 44 | ], |
| 45 | 'ids' => [ |
| 46 | 'type' => 'string', |
| 47 | 'required' => true, |
| 48 | 'validate_callback' => function ($ids) { |
| 49 | foreach ($this->splitString($ids) as $id) { |
| 50 | if ( ! $this->validateInt($id)) { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | }, |
| 57 | ], |
| 58 | 'author' => [ |
| 59 | 'type' => 'string', |
| 60 | 'required' => 'false', |
| 61 | ], |
| 62 | 'status' => [ |
| 63 | 'type' => 'string', |
| 64 | 'required' => 'false', |
| 65 | ], |
| 66 | ], |
| 67 | ] |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @since 2.25.2 |
| 73 | * |
| 74 | * @inheritDoc |
| 75 | */ |
| 76 | public function permissionsCheck() |
| 77 | { |
| 78 | if ( ! current_user_can('edit_give_forms')) { |
| 79 | return new WP_Error( |
| 80 | 'rest_forbidden', |
| 81 | esc_html__('You don\'t have permission to edit Donation Forms', 'give'), |
| 82 | ['status' => $this->authorizationStatusCode()] |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @since 2.19.0 |
| 91 | * |
| 92 | * @param WP_REST_Request $request |
| 93 | * |
| 94 | * @return WP_REST_Response |
| 95 | */ |
| 96 | public function handleRequest(WP_REST_Request $request) |
| 97 | { |
| 98 | $ids = $this->splitString($request->get_param('ids')); |
| 99 | $errors = []; |
| 100 | $successes = []; |
| 101 | $form = false; |
| 102 | |
| 103 | switch ($request->get_param('action')) { |
| 104 | case 'trash': |
| 105 | foreach ($ids as $id) { |
| 106 | $form = wp_trash_post($id); |
| 107 | !empty($form) ? $successes[] = $id : $errors[] = $id; |
| 108 | } |
| 109 | |
| 110 | break; |
| 111 | |
| 112 | case 'restore': |
| 113 | foreach ($ids as $id) { |
| 114 | $form = wp_untrash_post($id); |
| 115 | !empty($form) ? $successes[] = $id : $errors[] = $id; |
| 116 | } |
| 117 | |
| 118 | break; |
| 119 | |
| 120 | |
| 121 | case 'delete': |
| 122 | foreach ($ids as $id) { |
| 123 | $form = wp_delete_post($id); |
| 124 | give()->form_meta->delete_all_meta($id); |
| 125 | !empty($form) ? $successes[] = $form : $errors[] = $form; |
| 126 | } |
| 127 | |
| 128 | break; |
| 129 | |
| 130 | case 'duplicate': |
| 131 | require_once(GIVE_PLUGIN_DIR . '/includes/admin/forms/class-give-form-duplicator.php'); |
| 132 | |
| 133 | foreach ($ids as $id) { |
| 134 | $form = \Give_Form_Duplicator::handler($id); |
| 135 | $form ? $successes[] = $form : $errors[] = $form; |
| 136 | } |
| 137 | |
| 138 | break; |
| 139 | |
| 140 | case 'edit': |
| 141 | $author = $request->get_param('author'); |
| 142 | $status = $request->get_param('status'); |
| 143 | $update_args = []; |
| 144 | $author ? $update_args['post_author'] = $author : null; |
| 145 | $status ? $update_args['post_status'] = $status : null; |
| 146 | foreach ($ids as $id) { |
| 147 | $form = wp_update_post(array_merge($update_args, ['ID' => $id])); |
| 148 | !empty($form) ? $successes[] = $id : $errors[] = $id; |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | return new WP_REST_Response(array('errors' => $errors, 'successes' => $successes)); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Split string |
| 159 | * |
| 160 | * @param string $ids |
| 161 | * |
| 162 | * @return string[] |
| 163 | */ |
| 164 | protected function splitString($ids) |
| 165 | { |
| 166 | if (strpos($ids, ',')) { |
| 167 | return array_map('trim', explode(',', $ids)); |
| 168 | } |
| 169 | |
| 170 | return [trim($ids)]; |
| 171 | } |
| 172 | } |
| 173 |