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