PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev2
Elementor Website Builder – more than just a page builder v3.32.0-dev2
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 3.32.0-dev2, at core/common/modules/connect/apps/library.php

168 lines 4.5 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
69 return array_replace_recursive( $settings, [
70 'library_connect' => [
71 'is_connected' => $is_connected,
72 'user_id' => $user_id,
73 'subscription_plans' => $connect->get_subscription_plans( 'template-library' ),
74 // TODO: Remove `base_access_level`.
75 'base_access_level' => ConnectModule::ACCESS_LEVEL_CORE,
76 'base_access_tier' => ConnectModule::ACCESS_TIER_FREE,
77 'current_access_level' => ConnectModule::ACCESS_LEVEL_CORE,
78 'current_access_tier' => ConnectModule::ACCESS_TIER_FREE,
79 'plan_type' => ConnectModule::ACCESS_TIER_FREE,
80 ],
81 ] );
82 }
83
84 public function library_connect_popup_seen() {
85 User::set_introduction_viewed( [
86 'introductionKey' => 'library_connect',
87 ] );
88 }
89
90 /**
91 * @param \Elementor\Core\Common\Modules\Ajax\Module $ajax_manager
92 */
93 public function register_ajax_actions( $ajax_manager ) {
94 $ajax_manager->register_ajax_action( 'library_connect_popup_seen', [ $this, 'library_connect_popup_seen' ] );
95 }
96
97 private function get_user_id() {
98 $token = $this->get( 'access_token' );
99
100 if ( ! is_string( $token ) ) {
101 return null;
102 }
103
104 $parts = explode( '.', $token );
105
106 if ( count( $parts ) !== 3 ) {
107 return null;
108 }
109
110 try {
111 $payload_encoded = $parts[1];
112
113 $payload_encoded = str_pad( $payload_encoded, strlen( $payload_encoded ) + ( 4 - strlen( $payload_encoded ) % 4 ) % 4, '=' );
114
115 $payload_json = base64_decode( strtr( $payload_encoded, '-_', '+/' ), true );
116
117 $payload = json_decode( $payload_json, true );
118
119 if ( ! isset( $payload['sub'] ) ) {
120 return null;
121 }
122
123 return $payload['sub'];
124 } catch ( Exception $e ) {
125 error_log( 'JWT Decoding Error: ' . $e->getMessage() );
126 return null;
127 }
128 }
129
130 /**
131 * After Connect
132 *
133 * After Connecting to the library, re-fetch the library data to get it up to date.
134 *
135 * @since 3.7.0
136 */
137 protected function after_connect() {
138 Api::get_library_data( true );
139 }
140
141 protected function get_app_info() {
142 return [
143 'user_common_data' => [
144 'label' => 'User Common Data',
145 'value' => get_user_option( $this->get_option_name(), get_current_user_id() ),
146 ],
147 'connect_site_key' => [
148 'label' => 'Site Key',
149 'value' => get_option( self::OPTION_CONNECT_SITE_KEY ),
150 ],
151 ];
152 }
153
154 protected function get_popup_success_event_data() {
155 return [
156 'access_level' => ConnectModule::ACCESS_LEVEL_CORE,
157 'access_tier' => ConnectModule::ACCESS_TIER_FREE,
158 'plan_type' => ConnectModule::ACCESS_TIER_FREE,
159 ];
160 }
161
162 protected function init() {
163 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
164 add_filter( 'elementor/common/localize_settings', [ $this, 'localize_settings' ] );
165 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
166 }
167 }
168