/wp-content/plugins/elementor/data/v2/manager.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 401 lines · 173 code · 69 blank · 159 comment · 17 complexity · 0afba3344c6f8bc5815f9b29d39db582 MD5 · raw file

  1. <?php
  2. namespace Elementor\Data\V2;
  3. use Elementor\Core\Base\Module as BaseModule;
  4. use Elementor\Data\V2\Base\Processor;
  5. use Elementor\Data\V2\Base\Controller;
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly
  8. }
  9. /**
  10. * @method static \Elementor\Data\V2\Manager instance()
  11. */
  12. class Manager extends BaseModule {
  13. const ROOT_NAMESPACE = 'elementor';
  14. const VERSION = '1';
  15. /**
  16. * @var \WP_REST_Server
  17. */
  18. private $server;
  19. /**
  20. * @var boolean
  21. */
  22. private $is_internal = false;
  23. /**
  24. * @var array
  25. */
  26. private $cache = [];
  27. /**
  28. * Loaded controllers.
  29. *
  30. * @var \Elementor\Data\V2\Base\Controller[]
  31. */
  32. public $controllers = [];
  33. /**
  34. * Loaded command(s) format.
  35. *
  36. * @var string[]
  37. */
  38. public $command_formats = [];
  39. public function get_name() {
  40. return 'data-manager-v2';
  41. }
  42. /**
  43. * @return \Elementor\Data\V2\Base\Controller[]
  44. */
  45. public function get_controllers() {
  46. return $this->controllers;
  47. }
  48. /**
  49. * @param string $name
  50. *
  51. * @return \Elementor\Data\V2\Base\Controller|false
  52. */
  53. public function get_controller( $name ) {
  54. if ( isset( $this->controllers[ $name ] ) ) {
  55. return $this->controllers[ $name ];
  56. }
  57. return false;
  58. }
  59. private function get_cache( $key ) {
  60. return self::get_items( $this->cache, $key );
  61. }
  62. private function set_cache( $key, $value ) {
  63. $this->cache[ $key ] = $value;
  64. }
  65. /**
  66. * Register controller.
  67. *
  68. * @param \Elementor\Data\V2\Base\Controller $controller_instance
  69. *
  70. * @return \Elementor\Data\V2\Base\Controller
  71. */
  72. public function register_controller( Controller $controller_instance ) {
  73. $this->controllers[ $controller_instance->get_name() ] = $controller_instance;
  74. return $controller_instance;
  75. }
  76. /**
  77. * Register endpoint format.
  78. *
  79. * @param string $command
  80. * @param string $format
  81. *
  82. */
  83. public function register_endpoint_format( $command, $format ) {
  84. $this->command_formats[ $command ] = untrailingslashit( $format );
  85. }
  86. /**
  87. * Find controller instance.
  88. *
  89. * By given command name.
  90. *
  91. * @param string $command
  92. *
  93. * @return false|\Elementor\Data\V2\Base\Controller
  94. */
  95. public function find_controller_instance( $command ) {
  96. $command_parts = explode( '/', $command );
  97. $assumed_command_parts = [];
  98. foreach ( $command_parts as $command_part ) {
  99. $assumed_command_parts [] = $command_part;
  100. foreach ( $this->controllers as $controller_name => $controller ) {
  101. $assumed_command = implode( '/', $assumed_command_parts );
  102. if ( $assumed_command === $controller_name ) {
  103. return $controller;
  104. }
  105. }
  106. }
  107. return false;
  108. }
  109. /**
  110. * Command extract args.
  111. *
  112. * @param string $command
  113. * @param array $args
  114. *
  115. * @return \stdClass
  116. */
  117. public function command_extract_args( $command, $args = [] ) {
  118. $result = new \stdClass();
  119. $result->command = $command;
  120. $result->args = $args;
  121. if ( false !== strpos( $command, '?' ) ) {
  122. $command_parts = explode( '?', $command );
  123. $pure_command = $command_parts[0];
  124. $query_string = $command_parts[1];
  125. parse_str( $query_string, $temp );
  126. $result->command = untrailingslashit( $pure_command );
  127. $result->args = array_merge( $args, $temp );
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Command to endpoint.
  133. *
  134. * Format is required otherwise $command will returned.
  135. *
  136. * @param string $command
  137. * @param string $format
  138. * @param array $args
  139. *
  140. * @return string endpoint
  141. */
  142. public function command_to_endpoint( $command, $format, $args ) {
  143. $endpoint = $command;
  144. if ( $format ) {
  145. $formatted = $format;
  146. array_walk( $args, function ( $val, $key ) use ( &$formatted ) {
  147. $formatted = str_replace( '{' . $key . '}', $val, $formatted );
  148. } );
  149. // Remove remaining format if not requested via `$args`.
  150. if ( strstr( $formatted, '/{' ) ) {
  151. /**
  152. * Example:
  153. * $command = 'example/documents';
  154. * $format = 'example/documents/{document_id}/elements/{element_id}';
  155. * $formatted = 'example/documents/1618/elements/{element_id}';
  156. * Result:
  157. * $formatted = 'example/documents/1618/elements';
  158. */
  159. $formatted = substr( $formatted, 0, strpos( $formatted, '/{' ) );
  160. }
  161. $endpoint = $formatted;
  162. }
  163. return $endpoint;
  164. }
  165. /**
  166. * Run server.
  167. *
  168. * Init WordPress reset api.
  169. *
  170. * @return \WP_REST_Server
  171. */
  172. public function run_server() {
  173. /**
  174. * If run_server() called means, that rest api is simulated from the backend.
  175. */
  176. $this->is_internal = true;
  177. if ( ! $this->server ) {
  178. // Remove all 'rest_api_init' actions.
  179. remove_all_actions( 'rest_api_init' );
  180. // Call custom reset api loader.
  181. do_action( 'elementor_rest_api_before_init' );
  182. $this->server = rest_get_server(); // Init API.
  183. }
  184. return $this->server;
  185. }
  186. /**
  187. * Kill server.
  188. *
  189. * Free server and controllers.
  190. */
  191. public function kill_server() {
  192. global $wp_rest_server;
  193. $this->controllers = [];
  194. $this->command_formats = [];
  195. $this->server = false;
  196. $this->is_internal = false;
  197. $this->cache = [];
  198. $wp_rest_server = false;
  199. }
  200. /**
  201. * Run processor.
  202. *
  203. * @param \Elementor\Data\V2\Base\Processor $processor
  204. * @param array $data
  205. *
  206. * @return mixed
  207. */
  208. public function run_processor( $processor, $data ) {
  209. if ( call_user_func_array( [ $processor, 'get_conditions' ], $data ) ) {
  210. return call_user_func_array( [ $processor, 'apply' ], $data );
  211. }
  212. return null;
  213. }
  214. /**
  215. * Run processors.
  216. *
  217. * Filter them by class.
  218. *
  219. * @param \Elementor\Data\V2\Base\Processor[] $processors
  220. * @param string $filter_by_class
  221. * @param array $data
  222. *
  223. * @return false|array
  224. */
  225. public function run_processors( $processors, $filter_by_class, $data ) {
  226. foreach ( $processors as $processor ) {
  227. if ( $processor instanceof $filter_by_class ) {
  228. if ( Processor\Before::class === $filter_by_class ) {
  229. $this->run_processor( $processor, $data );
  230. } elseif ( Processor\After::class === $filter_by_class ) {
  231. $result = $this->run_processor( $processor, $data );
  232. if ( $result ) {
  233. $data[1] = $result;
  234. }
  235. } else {
  236. trigger_error( "Invalid processor filter: '${ $filter_by_class }'" ); // phpcs:ignore
  237. break;
  238. }
  239. }
  240. }
  241. return isset( $data[1] ) ? $data[1] : false;
  242. }
  243. /**
  244. * Run request.
  245. *
  246. * Simulate rest API from within the backend.
  247. * Use args as query.
  248. *
  249. * @param string $endpoint
  250. * @param array $args
  251. * @param string $method
  252. * @param string $namespace (optional)
  253. * @param string $version (optional)
  254. *
  255. * @return \WP_REST_Response
  256. */
  257. public function run_request( $endpoint, $args = [], $method = \WP_REST_Server::READABLE, $namespace = self::ROOT_NAMESPACE, $version = self::VERSION ) {
  258. $this->run_server();
  259. $endpoint = '/' . $namespace . '/v' . $version . '/' . trim( $endpoint, '/' );
  260. // Run reset api.
  261. $request = new \WP_REST_Request( $method, $endpoint );
  262. if ( 'GET' === $method ) {
  263. $request->set_query_params( $args );
  264. } else {
  265. $request->set_body_params( $args );
  266. }
  267. return rest_do_request( $request );
  268. }
  269. /**
  270. * Run endpoint.
  271. *
  272. * Wrapper for `$this->run_request` return `$response->getData()` instead of `$response`.
  273. *
  274. * @param string $endpoint
  275. * @param array $args
  276. * @param string $method
  277. *
  278. * @return array
  279. */
  280. public function run_endpoint( $endpoint, $args = [], $method = 'GET' ) {
  281. // The method become public since it used in `Elementor\Data\V2\Base\Endpoint\Index\AllChildren`.
  282. $response = $this->run_request( $endpoint, $args, $method );
  283. return $response->get_data();
  284. }
  285. /**
  286. * Run ( simulated reset api ).
  287. *
  288. * Do:
  289. * Init reset server.
  290. * Run before processors.
  291. * Run command as reset api endpoint from internal.
  292. * Run after processors.
  293. *
  294. * @param string $command
  295. * @param array $args
  296. * @param string $method
  297. *
  298. * @return array|false processed result
  299. */
  300. public function run( $command, $args = [], $method = 'GET' ) {
  301. $key = crc32( $command . '-' . wp_json_encode( $args ) . '-' . $method );
  302. $cache = $this->get_cache( $key );
  303. if ( $cache ) {
  304. return $cache;
  305. }
  306. $this->run_server();
  307. $controller_instance = $this->find_controller_instance( $command );
  308. if ( ! $controller_instance ) {
  309. $this->set_cache( $key, [] );
  310. return [];
  311. }
  312. $extracted_command = $this->command_extract_args( $command, $args );
  313. $command = $extracted_command->command;
  314. $args = $extracted_command->args;
  315. $format = isset( $this->command_formats[ $command ] ) ? $this->command_formats[ $command ] : false;
  316. $command_processors = $controller_instance->get_processors( $command );
  317. $endpoint = $this->command_to_endpoint( $command, $format, $args );
  318. $this->run_processors( $command_processors, Processor\Before::class, [ $args ] );
  319. $response = $this->run_request( $endpoint, $args, $method );
  320. $result = $response->get_data();
  321. if ( $response->is_error() ) {
  322. $this->set_cache( $key, [] );
  323. return [];
  324. }
  325. $result = $this->run_processors( $command_processors, Processor\After::class, [ $args, $result ] );
  326. $this->set_cache( $key, $result );
  327. return $result;
  328. }
  329. public function is_internal() {
  330. return $this->is_internal;
  331. }
  332. }