PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.9
Elementor Website Builder – more than just a page builder v4.0.9
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 / common / modules / connect / apps / library.php

library.php in Elementor Website Builder – more than just a page builder 4.0.9, at core/common/modules/connect/apps/library.php

179 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Common\Modules\Connect\Apps;
3
4 use Elementor\Api;
5 use Elementor\User;
6 use Elementor\Plugin;
7 use Elementor\Core\Common\Modules\Connect\Module as ConnectModule;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Library extends Common_App {
14
15 public function get_title() {
16 return esc_html__( 'Library', 'elementor' );
17 }
18
19 /**
20 * @since 2.3.0
21 * @access protected
22 */
23 protected function get_slug() {
24 return 'library';
25 }
26
27 public function get_template_content( $id ) {
28 if ( ! $this->is_connected() ) {
29 return new \WP_Error( '401', esc_html__( 'Connecting to the Library failed. Please try reloading the page and try again', 'elementor' ) );
30 }
31
32 $body_args = [
33 'id' => $id,
34
35 // Which API version is used.
36 'api_version' => ELEMENTOR_VERSION,
37 // Which language to return.
38 'site_lang' => get_bloginfo( 'language' ),
39 ];
40
41 /**
42 * API: Template body args.
43 *
44 * Filters the body arguments send with the GET request when fetching the content.
45 *
46 * @since 1.0.0
47 *
48 * @param array $body_args Body arguments.
49 */
50 $body_args = apply_filters( 'elementor/api/get_templates/body_args', $body_args );
51
52 $template_content = $this->request( 'get_template_content', $body_args, true );
53
54 if ( is_wp_error( $template_content ) && 401 === $template_content->get_error_code() ) {
55 // Normalize 401 message
56 return new \WP_Error( 401, esc_html__( 'Connecting to the Library failed. Please try reloading the page and try again', 'elementor' ) );
57 }
58
59 return $template_content;
60 }
61
62 public function localize_settings( $settings ) {
63 $is_connected = $this->is_connected();
64
65 /** @var ConnectModule $connect */
66 $connect = Plugin::$instance->common->get_component( 'connect' );
67 $user_id = $this->get_user_id();
68 $user_roles = $this->get_user_roles();
69 $user = $this->get( 'user' );
70
71 return array_replace_recursive( $settings, [
72 'library_connect' => [
73 'is_connected' => $is_connected,
74 'user_id' => $user_id,
75 'user_roles' => $user_roles,
76 'subscription_plans' => $connect->get_subscription_plans( 'template-library' ),
77 // TODO: Remove `base_access_level`.
78 'base_access_level' => ConnectModule::ACCESS_LEVEL_CORE,
79 'base_access_tier' => ConnectModule::ACCESS_TIER_FREE,
80 'current_access_level' => ConnectModule::ACCESS_LEVEL_CORE,
81 'current_access_tier' => ConnectModule::ACCESS_TIER_FREE,
82 'plan_type' => ConnectModule::ACCESS_TIER_FREE,
83 'user_email' => $user->email ?? null,
84 ],
85 ] );
86 }
87
88 public function library_connect_popup_seen() {
89 User::set_introduction_viewed( [
90 'introductionKey' => 'library_connect',
91 ] );
92 }
93
94 /**
95 * @param \Elementor\Core\Common\Modules\Ajax\Module $ajax_manager
96 */
97 public function register_ajax_actions( $ajax_manager ) {
98 $ajax_manager->register_ajax_action( 'library_connect_popup_seen', [ $this, 'library_connect_popup_seen' ] );
99 }
100
101 private function get_user_id() {
102 $token = $this->get( 'access_token' );
103
104 if ( ! is_string( $token ) ) {
105 return null;
106 }
107
108 $parts = explode( '.', $token );
109
110 if ( count( $parts ) !== 3 ) {
111 return null;
112 }
113
114 try {
115 $payload_encoded = $parts[1];
116
117 $payload_encoded = str_pad( $payload_encoded, strlen( $payload_encoded ) + ( 4 - strlen( $payload_encoded ) % 4 ) % 4, '=' );
118
119 $payload_json = base64_decode( strtr( $payload_encoded, '-_', '+/' ), true );
120
121 $payload = json_decode( $payload_json, true );
122
123 if ( ! isset( $payload['sub'] ) ) {
124 return null;
125 }
126
127 return $payload['sub'];
128 } catch ( Exception $e ) {
129 error_log( 'JWT Decoding Error: ' . $e->getMessage() );
130 return null;
131 }
132 }
133
134 private function get_user_roles() {
135 $user = wp_get_current_user();
136 return $user->roles ?? [];
137 }
138
139 /**
140 * After Connect
141 *
142 * After Connecting to the library, re-fetch the library data to get it up to date.
143 *
144 * @since 3.7.0
145 */
146 protected function after_connect() {
147 Api::get_library_data( true );
148 }
149
150 protected function get_app_info() {
151 return [
152 'user_common_data' => [
153 'label' => 'User Common Data',
154 'value' => get_user_option( $this->get_option_name(), get_current_user_id() ),
155 ],
156 'connect_site_key' => [
157 'label' => 'Site Key',
158 'value' => get_option( self::OPTION_CONNECT_SITE_KEY ),
159 ],
160 ];
161 }
162
163 protected function get_popup_success_event_data() {
164 return [
165 'access_level' => ConnectModule::ACCESS_LEVEL_CORE,
166 'access_tier' => ConnectModule::ACCESS_TIER_FREE,
167 'plan_type' => ConnectModule::ACCESS_TIER_FREE,
168 'tracking_opted_in' => $this->get( 'data_share_opted_in' ) ?? false,
169 'user_id' => $this->get_user_id(),
170 ];
171 }
172
173 protected function init() {
174 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
175 add_filter( 'elementor/common/localize_settings', [ $this, 'localize_settings' ] );
176 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
177 }
178 }
179