| 1 |
<?php |
| 2 |
namespace Depicter\Editor; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Data; |
| 5 |
use Averta\WordPress\Utility\Escape; |
| 6 |
use Averta\WordPress\Utility\JSON; |
| 7 |
use Averta\WordPress\Utility\Plugin; |
| 8 |
use Averta\WordPress\Utility\Sanitize; |
| 9 |
use Depicter\Security\CSRF; |
| 10 |
use Depicter\Services\UserAPIService; |
| 11 |
|
| 12 |
class EditorAssets |
| 13 |
{ |
| 14 |
public function bootstrap(){ |
| 15 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueueAdminAssets' ] ); |
| 16 |
} |
| 17 |
|
| 18 |
public function enqueueAdminAssets(){ |
| 19 |
global $wp_version; |
| 20 |
|
| 21 |
// This will enqueue the Media Uploader script |
| 22 |
wp_enqueue_media(); |
| 23 |
|
| 24 |
\Depicter::core()->assets()->enqueueScript( |
| 25 |
'depicter-admin', |
| 26 |
\Depicter::core()->assets()->getUrl() . '/resources/scripts/admin/index.js', |
| 27 |
['jquery'], |
| 28 |
true |
| 29 |
); |
| 30 |
\Depicter::core()->assets()->enqueueStyle( |
| 31 |
'depicter-editor-style', |
| 32 |
\Depicter::core()->assets()->getUrl() . '/resources/scripts/editor/depicter-editor-styles.css' |
| 33 |
); |
| 34 |
|
| 35 |
wp_enqueue_style('common'); |
| 36 |
|
| 37 |
// Enqueue scripts. |
| 38 |
\Depicter::core()->assets()->enqueueScriptModule( |
| 39 |
'depicter-editor-js', |
| 40 |
\Depicter::core()->assets()->getUrl() . '/resources/scripts/editor/depicter-editor.js' |
| 41 |
); |
| 42 |
|
| 43 |
$currentUser = wp_get_current_user(); |
| 44 |
$documentID = ! empty( $_GET['document'] ) ? Data::cast( Sanitize::key( wp_unslash( $_GET['document'] ) ), 'int' ) : null; |
| 45 |
|
| 46 |
try { |
| 47 |
$googleClientId = UserAPIService::googleClientID()['clientId'] ?? ''; |
| 48 |
} catch ( \Exception $e ) { |
| 49 |
$googleClientId = ''; |
| 50 |
} |
| 51 |
|
| 52 |
$upgradeLink = add_query_arg([ |
| 53 |
'action' => 'upgrade-plugin', |
| 54 |
'plugin' => 'depicter/depicter.php', |
| 55 |
'_wpnonce' => wp_create_nonce( 'upgrade-plugin_depicter/depicter.php') |
| 56 |
], self_admin_url('update.php') ); |
| 57 |
|
| 58 |
$envData = [ |
| 59 |
'wpVersion' => $wp_version, |
| 60 |
'wpHomepage' => home_url(), |
| 61 |
"scriptsPath" => \Depicter::core()->assets()->getUrl(). '/resources/scripts/editor/', |
| 62 |
'pluginAPI' => admin_url( 'admin-ajax.php' ), |
| 63 |
'clientKey' => \Depicter::auth()->getClientKey(), |
| 64 |
'csrfToken' => \Depicter::csrf()->getToken( CSRF::EDITOR_ACTION ), |
| 65 |
'updateInfo' => [ |
| 66 |
'from' => \Depicter::options()->get('version_previous') ?: null, |
| 67 |
'to' => \Depicter::options()->get('version'), |
| 68 |
'url' => $upgradeLink, |
| 69 |
], |
| 70 |
"assetsAPI" => Escape::url('https://api.wp.depicter.com/' ), |
| 71 |
"wpRestAPI" => Escape::url( get_rest_url() ), |
| 72 |
"dashboardURL"=> Escape::url( menu_page_url('depicter-dashboard', false) ), |
| 73 |
"documentId" => $documentID, |
| 74 |
'documentType' => \Depicter::documentRepository()->getFieldValue( $documentID, 'type' ), |
| 75 |
'user' => [ |
| 76 |
'tier' => $this->getUserTier( $documentID ), |
| 77 |
'name' => Escape::html( $currentUser->display_name ), |
| 78 |
'email' => Escape::html( $currentUser->user_email ), |
| 79 |
'joinedNewsletter' => !! \Depicter::options()->get('has_subscribed'), |
| 80 |
'dataCollectionConsent' => \Depicter::options()->get('data_collect_consent', 'not-set') |
| 81 |
], |
| 82 |
'activation' => [ |
| 83 |
'status' => \Depicter::auth()->getActivationStatus(), |
| 84 |
'errorMessage' => \Depicter::options()->get('activation_error_message', ''), |
| 85 |
'expiresAt' => \Depicter::options()->get('subscription_expires_at' , ''), |
| 86 |
'isNew' => isset( $_GET['depicter_upgraded'] ) |
| 87 |
], |
| 88 |
'subscription' => [ |
| 89 |
'id' => \Depicter::options()->get('subscription_id', null), |
| 90 |
'status' => \Depicter::auth()->getSubscriptionStatus(), |
| 91 |
'overdue' => \Depicter::auth()->isSubscriptionExpired() |
| 92 |
], |
| 93 |
'integrations' => [ |
| 94 |
'unfilteredUploadAllowed' => \Depicter::options()->get('allow_unfiltered_data_upload' ) === 'on', |
| 95 |
'woocommerce' => [ |
| 96 |
'label' => __( 'WooCommerce Plugin', 'depicter' ), |
| 97 |
'enabled' => Plugin::isActive( 'woocommerce/woocommerce.php' ) |
| 98 |
], |
| 99 |
'googleReviews' => \Depicter::dataSource()->googlePlaces()->hasValidApiKey() |
| 100 |
], |
| 101 |
'googleClientId' => $googleClientId, |
| 102 |
'tokens' => [ |
| 103 |
'idToken' => \Depicter::cache('base')->get( 'id_token' , null ), |
| 104 |
'accessToken' => \Depicter::cache('base')->get( 'access_token' , null ), |
| 105 |
'refreshToken' => \Depicter::cache('base')->get( 'refresh_token', null ) |
| 106 |
], |
| 107 |
'routes'=>[ |
| 108 |
'settingPage' => Escape::url( add_query_arg( [ 'page' => 'depicter-settings', ], self_admin_url( 'admin.php' ) ) ) |
| 109 |
] |
| 110 |
]; |
| 111 |
|
| 112 |
$useGoogleFonts = \Depicter::options()->get('use_google_fonts', 'on'); |
| 113 |
if ( $useGoogleFonts == 'off' ) { |
| 114 |
$envData['disabledFontServices'] = ['googleFont']; |
| 115 |
} elseif ( $useGoogleFonts == 'save_locally' ) { |
| 116 |
$envData['hostedFonts'] = \Depicter::googleFontsService()->getListOfHostedGoogleFonts(); |
| 117 |
} |
| 118 |
|
| 119 |
// Add Environment variables |
| 120 |
wp_print_inline_script_tag( 'window.depicterEnv = '. JSON::encode( $envData ) ); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get user tier |
| 126 |
* |
| 127 |
* @param $documentID |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function getUserTier( $documentID ): string{ |
| 132 |
$tier = \Depicter::auth()->getTier(); |
| 133 |
if ( $tier == 'free-user' ) { |
| 134 |
$createdAt = \Depicter::documentRepository()->getFieldValue( $documentID, 'created_at' ); |
| 135 |
if ( strtotime( $createdAt ) < strtotime('2024-01-01') ) { |
| 136 |
return $tier . '+'; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $tier; |
| 141 |
} |
| 142 |
|
| 143 |
} |
| 144 |
|