| 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_connect_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_roles() { |
| 102 |
$user = wp_get_current_user(); |
| 103 |
return $user->roles ?? []; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* After Connect |
| 108 |
* |
| 109 |
* After Connecting to the library, re-fetch the library data to get it up to date. |
| 110 |
* |
| 111 |
* @since 3.7.0 |
| 112 |
*/ |
| 113 |
protected function after_connect() { |
| 114 |
Api::get_library_data( true ); |
| 115 |
} |
| 116 |
|
| 117 |
protected function get_app_info() { |
| 118 |
return [ |
| 119 |
'user_common_data' => [ |
| 120 |
'label' => 'User Common Data', |
| 121 |
'value' => get_user_option( $this->get_option_name(), get_current_user_id() ), |
| 122 |
], |
| 123 |
'connect_site_key' => [ |
| 124 |
'label' => 'Site Key', |
| 125 |
'value' => get_option( self::OPTION_CONNECT_SITE_KEY ), |
| 126 |
], |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
protected function get_popup_success_event_data() { |
| 131 |
return [ |
| 132 |
'access_level' => ConnectModule::ACCESS_LEVEL_CORE, |
| 133 |
'access_tier' => ConnectModule::ACCESS_TIER_FREE, |
| 134 |
'plan_type' => ConnectModule::ACCESS_TIER_FREE, |
| 135 |
'tracking_opted_in' => $this->get( 'data_share_opted_in' ) ?? false, |
| 136 |
'user_id' => $this->get_connect_user_id(), |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
protected function init() { |
| 141 |
add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] ); |
| 142 |
add_filter( 'elementor/common/localize_settings', [ $this, 'localize_settings' ] ); |
| 143 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 144 |
} |
| 145 |
} |
| 146 |
|