| 1 |
<?php |
| 2 |
namespace Elementor\Data\V2\Base\Endpoint\Index; |
| 3 |
|
| 4 |
use Elementor\Data\V2\Base\Endpoint\Index; |
| 5 |
use Elementor\Data\V2\Manager; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
/** |
| 11 |
* class AllChildren, is optional endpoint. |
| 12 |
* Used in cases where the endpoints are static & there no use of dynamic endpoints( alpha/{id} ), eg: |
| 13 |
* 'settings' - controller |
| 14 |
* 'settings/products' - endpoint |
| 15 |
* 'settings/partners' - endpoint |
| 16 |
* |
| 17 |
* When 'settings' is requested, it should return results of all endpoints ( except it self ): |
| 18 |
* 'settings/products |
| 19 |
* 'settings/partners' |
| 20 |
* By running 'get_items' of each endpoint. |
| 21 |
*/ |
| 22 |
class AllChildren extends Index { |
| 23 |
public function get_format() { |
| 24 |
return $this->controller->get_name() . '/index'; |
| 25 |
} |
| 26 |
|
| 27 |
/* |
| 28 |
* Retrieves a result(s) of all controller endpoint(s), items. |
| 29 |
* |
| 30 |
* Run overall endpoints of the current controller. |
| 31 |
* |
| 32 |
* Example, scenario: |
| 33 |
* 'settings' - controller |
| 34 |
* 'settings/products' - endpoint |
| 35 |
* 'settings/partners' - endpoint |
| 36 |
* Result: |
| 37 |
* [ |
| 38 |
* 'products' => [ |
| 39 |
* 0 => ... |
| 40 |
* 1 => ... |
| 41 |
* ], |
| 42 |
* 'partners' => [ |
| 43 |
* 0 => ... |
| 44 |
* 1 => ... |
| 45 |
* ], |
| 46 |
* ] |
| 47 |
*/ |
| 48 |
public function get_items( $request ) { |
| 49 |
$response = []; |
| 50 |
|
| 51 |
foreach ( $this->controller->get_sub_controllers() as $controller ) { |
| 52 |
$controller_route = $this->get_controller()->get_base_route() . '/' . $controller->get_name(); |
| 53 |
$result = Manager::instance()->run_request( $controller_route ); |
| 54 |
|
| 55 |
if ( ! $result->is_error() ) { |
| 56 |
$response[ $controller->get_name() ] = $result->get_data(); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $this->controller->endpoints as $endpoint ) { |
| 61 |
// Skip self. |
| 62 |
if ( $endpoint === $this ) { |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$result = Manager::instance()->run_request( $endpoint->get_base_route() ); |
| 67 |
|
| 68 |
if ( ! $result->is_error() ) { |
| 69 |
$response[ $endpoint->get_name() ] = $result->get_data(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return $response; |
| 74 |
} |
| 75 |
} |
| 76 |
|