| 1 |
<?php |
| 2 |
namespace Elementor\App\Modules\ImportExportCustomization\Data\Routes; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\App\Modules\ImportExportCustomization\Data\Response; |
| 6 |
use Elementor\Core\Base\Document; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Manager_Url extends Base_Route { |
| 13 |
private const ALLOWED_PANELS = [ |
| 14 |
'global-classes-manager', |
| 15 |
'variables-manager', |
| 16 |
]; |
| 17 |
|
| 18 |
protected function get_route(): string { |
| 19 |
return 'manager-url'; |
| 20 |
} |
| 21 |
|
| 22 |
protected function get_method(): string { |
| 23 |
return \WP_REST_Server::READABLE; |
| 24 |
} |
| 25 |
|
| 26 |
protected function callback( $request ): \WP_REST_Response { |
| 27 |
$panel = $request->get_param( 'panel' ); |
| 28 |
|
| 29 |
if ( ! in_array( $panel, self::ALLOWED_PANELS, true ) ) { |
| 30 |
return Response::error( 'Invalid panel parameter', 'invalid_panel' ); |
| 31 |
} |
| 32 |
|
| 33 |
$url = $this->get_editor_url_with_panel( $panel ); |
| 34 |
|
| 35 |
return Response::success( [ |
| 36 |
'url' => $url, |
| 37 |
] ); |
| 38 |
} |
| 39 |
|
| 40 |
private function get_editor_url_with_panel( string $panel ): string { |
| 41 |
$elementor_page = $this->get_elementor_page(); |
| 42 |
|
| 43 |
if ( $elementor_page ) { |
| 44 |
$document = Plugin::$instance->documents->get( $elementor_page->ID ); |
| 45 |
|
| 46 |
if ( $document && $document->is_built_with_elementor() ) { |
| 47 |
return add_query_arg( 'active-panel', $panel, $document->get_edit_url() ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return add_query_arg( |
| 52 |
'active-panel', |
| 53 |
$panel, |
| 54 |
Plugin::$instance->documents->get_create_new_post_url( 'page' ) |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
private function get_elementor_page() { |
| 59 |
$pages = get_pages( [ |
| 60 |
'post_status' => [ 'publish', 'draft' ], |
| 61 |
'meta_key' => Document::BUILT_WITH_ELEMENTOR_META_KEY, |
| 62 |
'sort_order' => 'desc', |
| 63 |
'sort_column' => 'post_modified', |
| 64 |
'number' => 1, |
| 65 |
] ); |
| 66 |
|
| 67 |
return $pages[0] ?? null; |
| 68 |
} |
| 69 |
|
| 70 |
protected function get_args(): array { |
| 71 |
return [ |
| 72 |
'panel' => [ |
| 73 |
'type' => 'string', |
| 74 |
'description' => 'Panel ID to open in the editor', |
| 75 |
'required' => true, |
| 76 |
'enum' => self::ALLOWED_PANELS, |
| 77 |
], |
| 78 |
]; |
| 79 |
} |
| 80 |
} |
| 81 |
|