| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPFunnels\Rest\Controllers; |
| 4 |
|
| 5 |
use Error; |
| 6 |
use WPFunnels\Export\Wpfnl_Export; |
| 7 |
use WPFunnels\Import\Wpfnl_Import; |
| 8 |
use WP_Error; |
| 9 |
use WP_REST_Request; |
| 10 |
use WPFunnels\Wpfnl; |
| 11 |
use WPFunnels\Wpfnl_functions; |
| 12 |
|
| 13 |
|
| 14 |
class ImportExportController extends Wpfnl_REST_Controller |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* Endpoint namespace. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
* @since 1.9.3 |
| 22 |
*/ |
| 23 |
protected $namespace = 'wpfunnels/v1'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Route base. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
* @since 1.9.3 |
| 30 |
*/ |
| 31 |
protected $rest_base = 'import-export'; |
| 32 |
|
| 33 |
/** |
| 34 |
* check if user has valid permission |
| 35 |
* |
| 36 |
* @param $request |
| 37 |
* @return bool|WP_Error |
| 38 |
* @since 1.9.3 |
| 39 |
*/ |
| 40 |
public function update_items_permissions_check($request) |
| 41 |
{ |
| 42 |
if (!Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'steps', 'edit' )) { |
| 43 |
return new WP_Error('wpfunnels_rest_cannot_edit', __('Sorry, you cannot edit this resource.', 'wpfnl'), ['status' => rest_authorization_required_code()]); |
| 44 |
} |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Check read permission for this resource. |
| 51 |
* |
| 52 |
* @param WP_REST_Request $request |
| 53 |
* |
| 54 |
* @return bool|WP_Error |
| 55 |
* @since 2.5.16 |
| 56 |
*/ |
| 57 |
public function get_items_permissions_check($request) |
| 58 |
{ |
| 59 |
if (!Wpfnl_functions::wpfnl_rest_check_manager_permissions( 'funnels' )) { |
| 60 |
return new WP_Error('wpfunnels_rest_cannot_view', __('Sorry, you cannot list resources.', 'wpfnl'), ['status' => rest_authorization_required_code()]); |
| 61 |
} |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* register rest routes |
| 68 |
* |
| 69 |
* @since 1.9.3 |
| 70 |
*/ |
| 71 |
public function register_routes() |
| 72 |
{ |
| 73 |
register_rest_route($this->namespace, '/' . $this->rest_base . '/bulk-export-funnel', [ |
| 74 |
[ |
| 75 |
'methods' => \WP_REST_Server::EDITABLE, |
| 76 |
'callback' => [ |
| 77 |
$this, |
| 78 |
'bulk_export_funnel' |
| 79 |
], |
| 80 |
'permission_callback' => [ |
| 81 |
$this, |
| 82 |
'update_items_permissions_check' |
| 83 |
], |
| 84 |
], |
| 85 |
]); |
| 86 |
|
| 87 |
register_rest_route($this->namespace, '/' . $this->rest_base . '/import', [ |
| 88 |
[ |
| 89 |
'methods' => \WP_REST_Server::CREATABLE, |
| 90 |
'callback' => [ |
| 91 |
$this, |
| 92 |
'import_funnels' |
| 93 |
], |
| 94 |
'permission_callback' => [ |
| 95 |
$this, |
| 96 |
'update_items_permissions_check' |
| 97 |
], |
| 98 |
], |
| 99 |
]); |
| 100 |
|
| 101 |
register_rest_route($this->namespace, '/' . $this->rest_base . '/import-status', [ |
| 102 |
[ |
| 103 |
'methods' => \WP_REST_Server::READABLE, |
| 104 |
'callback' => [ |
| 105 |
$this, |
| 106 |
'get_import_status' |
| 107 |
], |
| 108 |
'permission_callback' => [ |
| 109 |
$this, |
| 110 |
'get_items_permissions_check' |
| 111 |
], |
| 112 |
'args' => [ |
| 113 |
'run_id' => [ |
| 114 |
'type' => 'string', |
| 115 |
'required' => false, |
| 116 |
'default' => '', |
| 117 |
'sanitize_callback' => 'sanitize_text_field', |
| 118 |
], |
| 119 |
], |
| 120 |
], |
| 121 |
]); |
| 122 |
|
| 123 |
register_rest_route($this->namespace, '/' . $this->rest_base . '/import-reset', [ |
| 124 |
[ |
| 125 |
'methods' => \WP_REST_Server::EDITABLE, |
| 126 |
'callback' => [ |
| 127 |
$this, |
| 128 |
'reset_import' |
| 129 |
], |
| 130 |
'permission_callback' => [ |
| 131 |
$this, |
| 132 |
'update_items_permissions_check' |
| 133 |
], |
| 134 |
], |
| 135 |
]); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* Queue an uploaded funnel export for import. |
| 141 |
* |
| 142 |
* The REST twin of the `wpfnl_import_funnels` admin-ajax action, added for the React |
| 143 |
* funnel list — the file rides in as multipart form data under `uploaded_file`, exactly |
| 144 |
* the field the shared uploader reads, and the response carries the run id the import |
| 145 |
* modal then polls `import-status` with. |
| 146 |
* |
| 147 |
* @param WP_REST_Request $request |
| 148 |
* |
| 149 |
* @return \WP_REST_Response|WP_Error |
| 150 |
* @since 3.13.4 |
| 151 |
*/ |
| 152 |
public function import_funnels($request) |
| 153 |
{ |
| 154 |
$importer = Wpfnl::$instance->import_funnel; |
| 155 |
|
| 156 |
if (!$importer instanceof Wpfnl_Import) { |
| 157 |
$importer = new Wpfnl_Import(); |
| 158 |
} |
| 159 |
|
| 160 |
$result = $importer->schedule_import(); |
| 161 |
|
| 162 |
if (is_wp_error($result)) { |
| 163 |
return $result; |
| 164 |
} |
| 165 |
|
| 166 |
return rest_ensure_response(array_merge(['success' => true], $result)); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Report the progress of the running (or most recent) funnel import. |
| 172 |
* |
| 173 |
* Polled by the import modal. The heavy lifting — including stall detection, which has to |
| 174 |
* be derived by a reader because the process that died cannot report on itself — lives in |
| 175 |
* Wpfnl_Import so the MCP tools and the admin UI agree on one definition of "running". |
| 176 |
* |
| 177 |
* @param WP_REST_Request $request |
| 178 |
* |
| 179 |
* @return \WP_REST_Response |
| 180 |
* @since 2.5.16 |
| 181 |
*/ |
| 182 |
public function get_import_status($request) |
| 183 |
{ |
| 184 |
$run_id = $request->get_param('run_id'); |
| 185 |
|
| 186 |
return rest_ensure_response(Wpfnl_Import::get_status_payload(is_string($run_id) ? $run_id : '')); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Clear a stuck import. |
| 192 |
* |
| 193 |
* Before this existed, one dead job left `as_has_scheduled_action()` returning true forever |
| 194 |
* and every later import was refused with "Import already scheduled", with no way out from |
| 195 |
* the UI. |
| 196 |
* |
| 197 |
* @param WP_REST_Request $request |
| 198 |
* |
| 199 |
* @return \WP_REST_Response |
| 200 |
* @since 2.5.16 |
| 201 |
*/ |
| 202 |
public function reset_import($request) |
| 203 |
{ |
| 204 |
return rest_ensure_response(Wpfnl_Import::reset_import()); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Export selected funnels |
| 209 |
* |
| 210 |
* @param array $payload Funnel ids and status. |
| 211 |
* |
| 212 |
* @return array|\WP_Rest_Response|\WP_Error |
| 213 |
* @since 1.9.3 |
| 214 |
*/ |
| 215 |
public function bulk_export_funnel ( $payload ){ |
| 216 |
|
| 217 |
$controller_class = Wpfnl_Export::getInstance(); |
| 218 |
|
| 219 |
return $controller_class->bulk_export_funnel( $payload ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/** |
| 224 |
* Prepare a single setting object for response. |
| 225 |
* |
| 226 |
* @param object $item Setting object. |
| 227 |
* @param WP_REST_Request $request Request object. |
| 228 |
* @return \WP_REST_Response $response Response data. |
| 229 |
* @since 1.9.3 |
| 230 |
*/ |
| 231 |
public function prepare_item_for_response($item, $request) |
| 232 |
{ |
| 233 |
$data = $this->add_additional_fields_to_object($item, $request); |
| 234 |
return rest_ensure_response($data); |
| 235 |
} |
| 236 |
} |
| 237 |
|