| 1 |
<?php |
| 2 |
|
| 3 |
namespace ElementorOne\Admin\Components; |
| 4 |
|
| 5 |
use ElementorOne\Admin\Helpers\Utils; |
| 6 |
use ElementorOne\Connect\Facade; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Assets |
| 14 |
* Handles script and style enqueuing for the admin area |
| 15 |
*/ |
| 16 |
class Assets { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* @var Assets|null |
| 21 |
*/ |
| 22 |
private static ?Assets $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get instance |
| 26 |
* @return Assets|null |
| 27 |
*/ |
| 28 |
public static function instance(): ?Assets { |
| 29 |
if ( ! self::$instance ) { |
| 30 |
self::$instance = new self(); |
| 31 |
} |
| 32 |
return self::$instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get admin page |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
private function get_admin_page(): string { |
| 40 |
return Utils::get_one_connect()->get_config( 'admin_page' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Enqueue scripts |
| 45 |
* @param string $hook |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function enqueue_scripts( $hook ) { |
| 49 |
$package_version = Utils::get_latest_package_version(); |
| 50 |
|
| 51 |
// Enqueue fonts |
| 52 |
wp_enqueue_style( |
| 53 |
'elementor-one-admin-fonts', |
| 54 |
'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap', |
| 55 |
[], |
| 56 |
'1.0.0' |
| 57 |
); |
| 58 |
|
| 59 |
// Enqueue common assets |
| 60 |
$this->enqueue_common_assets( $package_version ); |
| 61 |
|
| 62 |
// Enqueue app assets only on Elementor Home admin page |
| 63 |
if ( "toplevel_page_{$this->get_admin_page()}" === $hook ) { |
| 64 |
$this->enqueue_app_assets( $package_version ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get plugin env |
| 70 |
* @return string |
| 71 |
*/ |
| 72 |
private static function get_plugin_env(): string { |
| 73 |
return apply_filters( 'elementor_one/package_env', 'production' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Enqueue app assets |
| 78 |
* @param string $package_version |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
private function enqueue_app_assets( string $package_version ) { |
| 82 |
wp_enqueue_script( 'elementor-one-admin', ELEMENTOR_ONE_CLIENT_APP_URL, [], $package_version, true ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Enqueue common assets |
| 87 |
* @param string $package_version |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
private function enqueue_common_assets( string $package_version ) { |
| 91 |
// Load the asset file to get dependencies and version |
| 92 |
$asset_file = ELEMENTOR_ONE_ASSETS_PATH . 'common.asset.php'; |
| 93 |
$asset = file_exists( $asset_file ) ? include $asset_file : [ |
| 94 |
'dependencies' => [], |
| 95 |
'version' => $package_version, |
| 96 |
]; |
| 97 |
|
| 98 |
wp_enqueue_script( 'elementor-one-admin-common', ELEMENTOR_ONE_ASSETS_URL . 'common.js', $asset['dependencies'], $asset['version'], true ); |
| 99 |
wp_enqueue_style( 'elementor-one-admin-common', ELEMENTOR_ONE_ASSETS_URL . 'common.css', [], $package_version ); |
| 100 |
|
| 101 |
wp_add_inline_script( |
| 102 |
'elementor-one-admin-common', |
| 103 |
'window.elementorOneSettingsData = ' . wp_json_encode( [ |
| 104 |
'wpRestNonce' => wp_create_nonce( 'wp_rest' ), |
| 105 |
'wpRestUrl' => rest_url(), |
| 106 |
'pluginEnv' => self::get_plugin_env(), |
| 107 |
'packageVersion' => Utils::get_latest_package_version(), |
| 108 |
'canUserManageOptions' => current_user_can( 'manage_options' ), |
| 109 |
'elementorNewPostNonce' => wp_create_nonce( 'elementor_action_new_post' ), |
| 110 |
'elementorSiteSettingsRedirectNonce' => wp_create_nonce( 'elementor_action_site_settings_redirect' ), |
| 111 |
'elementorEditSiteNonce' => wp_create_nonce( 'elementor_action_edit_website' ), |
| 112 |
'manageSiteOverviewRedirectNonce' => wp_create_nonce( 'manage_site_overview_redirect' ), |
| 113 |
'shareUsageData' => $this->should_share_usage_data(), |
| 114 |
'assetsUIRootUrl' => ELEMENTOR_ONE_UI_ASSETS_ROOT_URL, |
| 115 |
] ) . ';' |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Should share usage data |
| 121 |
* @return bool |
| 122 |
*/ |
| 123 |
private function should_share_usage_data(): bool { |
| 124 |
global $plugin_page; |
| 125 |
|
| 126 |
$sa_app_connect = $plugin_page ? Facade::get_by_plugin_page( $plugin_page ) : null; |
| 127 |
|
| 128 |
if ( $sa_app_connect && $sa_app_connect->utils()->is_connected() ) { |
| 129 |
return 'yes' === $sa_app_connect->data()->get_share_usage_data(); |
| 130 |
} |
| 131 |
|
| 132 |
$is_editor_admin_page = is_callable( '\Elementor\Core\Admin\Admin::is_elementor_admin_page' ) |
| 133 |
&& \Elementor\Core\Admin\Admin::is_elementor_admin_page(); |
| 134 |
|
| 135 |
if ( $is_editor_admin_page && ! $sa_app_connect ) { |
| 136 |
$editor_tracking_allowed = is_callable( '\Elementor\Tracker::is_allow_track' ) |
| 137 |
&& \Elementor\Tracker::is_allow_track(); |
| 138 |
|
| 139 |
$editor_tracking_updated = is_callable( '\Elementor\Tracker::get_last_update_time' ) |
| 140 |
&& \Elementor\Tracker::get_last_update_time(); |
| 141 |
|
| 142 |
if ( $editor_tracking_updated ) { |
| 143 |
return $editor_tracking_allowed; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
return 'yes' === Utils::get_one_connect()->data()->get_share_usage_data(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Assets constructor |
| 152 |
* @return void |
| 153 |
*/ |
| 154 |
private function __construct() { |
| 155 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 156 |
} |
| 157 |
} |
| 158 |
|