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