| 1 |
<?php |
| 2 |
namespace Elementor\Data\V2\Base; |
| 3 |
|
| 4 |
use Elementor\Data\V2\Manager; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
abstract class Endpoint extends Base_Route { |
| 11 |
/** |
| 12 |
* Current parent. |
| 13 |
* |
| 14 |
* @var \Elementor\Data\V2\Base\Controller|\Elementor\Data\V2\Base\Endpoint |
| 15 |
*/ |
| 16 |
protected $parent; |
| 17 |
|
| 18 |
/** |
| 19 |
* Loaded sub endpoint(s). |
| 20 |
* |
| 21 |
* @var \Elementor\Data\V2\Base\Endpoint[] |
| 22 |
*/ |
| 23 |
protected $sub_endpoints = []; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get endpoint name. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
abstract public function get_name(); |
| 31 |
|
| 32 |
/** |
| 33 |
* |
| 34 |
* Get endpoint format. |
| 35 |
* The formats that generated using this function, will be used only be `Data\Manager::run()`. |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
abstract public function get_format(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Get controller. |
| 43 |
* |
| 44 |
* @return \Elementor\Data\V2\Base\Controller |
| 45 |
*/ |
| 46 |
public function get_controller() { |
| 47 |
return $this->controller; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get current parent. |
| 52 |
* |
| 53 |
* @return \Elementor\Data\V2\Base\Controller|\Elementor\Data\V2\Base\Endpoint |
| 54 |
*/ |
| 55 |
public function get_parent() { |
| 56 |
return $this->parent; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get public name. |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function get_public_name() { |
| 65 |
return $this->get_name(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get full command name ( including index ). |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function get_full_command() { |
| 74 |
$parent = $this->get_parent(); |
| 75 |
|
| 76 |
if ( $parent instanceof Controller ) { |
| 77 |
return $this->controller->get_full_name() . '/' . $this->get_name(); |
| 78 |
} |
| 79 |
|
| 80 |
return $this->get_name_ancestry(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get name ancestry format, example: 'alpha/beta/delta'. |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function get_name_ancestry() { |
| 89 |
$ancestors = $this->get_ancestors(); |
| 90 |
$ancestors_names = []; |
| 91 |
|
| 92 |
foreach ( $ancestors as $ancestor ) { |
| 93 |
$ancestors_names [] = $ancestor->get_name(); |
| 94 |
} |
| 95 |
|
| 96 |
return implode( '/', $ancestors_names ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Register sub endpoint. |
| 101 |
* |
| 102 |
* @param \Elementor\Data\V2\Base\Endpoint $endpoint |
| 103 |
* |
| 104 |
* @return \Elementor\Data\V2\Base\Endpoint |
| 105 |
*/ |
| 106 |
public function register_sub_endpoint( Endpoint $endpoint ) { |
| 107 |
$command = $endpoint->get_full_command(); |
| 108 |
$format = $endpoint->get_format(); |
| 109 |
|
| 110 |
$this->sub_endpoints[ $command ] = $endpoint; |
| 111 |
|
| 112 |
Manager::instance()->register_endpoint_format( $command, $format ); |
| 113 |
|
| 114 |
return $endpoint; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get ancestors. |
| 119 |
* |
| 120 |
* @return \Elementor\Data\V2\Base\Endpoint[] |
| 121 |
*/ |
| 122 |
private function get_ancestors() { |
| 123 |
$ancestors = []; |
| 124 |
$current = $this; |
| 125 |
|
| 126 |
do { |
| 127 |
if ( $current ) { |
| 128 |
$ancestors [] = $current; |
| 129 |
} |
| 130 |
|
| 131 |
$current = $current->get_parent(); |
| 132 |
} while ( $current ); |
| 133 |
|
| 134 |
return array_reverse( $ancestors ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Endpoint constructor. |
| 139 |
* |
| 140 |
* @param \Elementor\Data\V2\Base\Controller|\Elementor\Data\V2\Base\Endpoint $parent |
| 141 |
* @param string $route |
| 142 |
*/ |
| 143 |
public function __construct( $parent, $route = '/' ) { |
| 144 |
$controller = $parent; |
| 145 |
$this->parent = $parent; |
| 146 |
|
| 147 |
// In case, its behave like sub-endpoint. |
| 148 |
if ( ! ( $parent instanceof Controller ) ) { |
| 149 |
$controller = $parent->get_controller(); |
| 150 |
} |
| 151 |
|
| 152 |
parent::__construct( $controller, $route ); |
| 153 |
} |
| 154 |
} |
| 155 |
|