PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.0
Elementor Website Builder – more than just a page builder v3.0.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / frontend / render-mode-manager.php

render-mode-manager.php in Elementor Website Builder – more than just a page builder 3.0.0, at core/frontend/render-mode-manager.php

143 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Frontend;
3
4 use Elementor\Core\Frontend\RenderModes\Render_Mode_Base;
5 use Elementor\Core\Frontend\RenderModes\Render_Mode_Normal;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Render_Mode_Manager {
12 const QUERY_STRING_PARAM_NAME = 'render_mode';
13 const QUERY_STRING_POST_ID = 'post_id';
14 const QUERY_STRING_NONCE_PARAM_NAME = 'render_mode_nonce';
15 const NONCE_ACTION_PATTERN = 'render_mode_{post_id}';
16
17 /**
18 * @var Render_Mode_Base
19 */
20 private $current;
21
22 /**
23 * @var Render_Mode_Base[]
24 */
25 private $render_modes = [];
26
27 /**
28 * @param $post_id
29 * @param $render_mode_name
30 *
31 * @return string
32 */
33 public static function get_base_url( $post_id, $render_mode_name ) {
34 return add_query_arg( [
35 self::QUERY_STRING_POST_ID => $post_id,
36 self::QUERY_STRING_PARAM_NAME => $render_mode_name,
37 self::QUERY_STRING_NONCE_PARAM_NAME => wp_create_nonce( self::get_nonce_action( $post_id ) ),
38 'ver' => time(),
39 ], get_permalink( $post_id ) );
40 }
41
42 /**
43 * @param $post_id
44 *
45 * @return string
46 */
47 public static function get_nonce_action( $post_id ) {
48 return str_replace( '{post_id}', $post_id, self::NONCE_ACTION_PATTERN );
49 }
50
51 /**
52 * Register a new render mode into the render mode manager.
53 *
54 * @param $class_name
55 *
56 * @return $this
57 * @throws \Exception
58 */
59 public function register_render_mode( $class_name ) {
60 if ( ! is_subclass_of( $class_name, Render_Mode_Base::class ) ) {
61 throw new \Exception( "'{$class_name}' must extends 'Render_Mode_Base'" );
62 }
63
64 $this->render_modes[ $class_name::get_name() ] = $class_name;
65
66 return $this;
67 }
68
69 /**
70 * Get the current render mode.
71 *
72 * @return Render_Mode_Base
73 */
74 public function get_current() {
75 return $this->current;
76 }
77
78 /**
79 * Set render mode.
80 *
81 * @return $this
82 */
83 private function set_current_render_mode() {
84 $post_id = null;
85 $key = null;
86 $nonce = null;
87
88 if ( isset( $_GET[ self::QUERY_STRING_POST_ID ] ) ) {
89 $post_id = $_GET[ self::QUERY_STRING_POST_ID ]; // phpcs:ignore -- Nonce will be checked next line.
90 }
91
92 if ( isset( $_GET[ self::QUERY_STRING_NONCE_PARAM_NAME ] ) ) {
93 $nonce = $_GET[ self::QUERY_STRING_NONCE_PARAM_NAME ]; // phpcs:ignore -- Nonce will be checked next line.
94 }
95
96 if ( isset( $_GET[ self::QUERY_STRING_PARAM_NAME ] ) ) {
97 $key = $_GET[ self::QUERY_STRING_PARAM_NAME ]; // phpcs:ignore -- Nonce will be checked next line.
98 }
99
100 if (
101 $post_id &&
102 $nonce &&
103 wp_verify_nonce( $nonce, self::get_nonce_action( $post_id ) ) &&
104 $key &&
105 array_key_exists( $key, $this->render_modes )
106 ) {
107 $this->current = new $this->render_modes[ $key ]( $post_id );
108 } else {
109 $this->current = new Render_Mode_Normal( $post_id );
110 }
111
112 return $this;
113 }
114
115 /**
116 * Add actions base on the current render.
117 *
118 * @throws \Requests_Exception_HTTP_403
119 */
120 private function add_current_actions() {
121 if ( ! $this->current->get_permissions_callback() ) {
122 throw new \Requests_Exception_HTTP_403();
123 }
124
125 // Run when 'template-redirect' actually because the the class is instantiate when 'template-redirect' run.
126 $this->current->prepare_render();
127 }
128
129 /**
130 * Render_Mode_Manager constructor.
131 *
132 * @throws \Exception
133 */
134 public function __construct() {
135 $this->register_render_mode( Render_Mode_Normal::class );
136
137 do_action( 'elementor/frontend/render_mode/register', $this );
138
139 $this->set_current_render_mode();
140 $this->add_current_actions();
141 }
142 }
143