| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models\Options; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Depicter\Document\Helper\Helper; |
| 7 |
use Depicter\Document\Models\Document; |
| 8 |
|
| 9 |
class Script |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* @param Document $document |
| 14 |
* |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
public function getDocumentInitScript( $document ) |
| 18 |
{ |
| 19 |
$width = array_values( $document->options->getSizes( 'width' ) ); |
| 20 |
$height = array_values( $document->options->getSizes( 'height') ); |
| 21 |
|
| 22 |
$attributes = [ |
| 23 |
'width' => $width, |
| 24 |
'height' => $height, |
| 25 |
'view' => 'basic', |
| 26 |
'keepAspectRatio' => $document->options->general->keepAspect ?? false, |
| 27 |
'preload' => isset( $document->options->loading ) ? $document->options->loading->getValue() : 0, |
| 28 |
'layout' => $document->options->getLayout(), |
| 29 |
'rtl' => $document->options->navigation->rtl ?? false, |
| 30 |
'initAfterAppear' => isset( $document->options->loading ) && !empty( $document->options->loading->initAfterAppear ), |
| 31 |
'ajaxApiUrl' => esc_url( admin_url( 'admin-ajax.php' ) ) |
| 32 |
]; |
| 33 |
|
| 34 |
if ( $document->isBuildWithAI ) { |
| 35 |
$attributes['disableAnimations'] = true; |
| 36 |
} |
| 37 |
|
| 38 |
if ( ! empty( $document->options->sectionTransition->type ) ) { |
| 39 |
if ( \Depicter::auth()->isPaid() ) { |
| 40 |
$attributes['view'] = $document->options->sectionTransition->type; |
| 41 |
} else { |
| 42 |
$attributes['view'] = $document->options->sectionTransition->type == 'fade' ? 'fade' : 'basic'; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// viewOptions property |
| 47 |
$viewOptions = []; |
| 48 |
if( isset( $document->options->navigation->loop ) ){ |
| 49 |
$viewOptions['loop'] = $document->options->navigation->loop; |
| 50 |
} |
| 51 |
if( $attributes['view'] === 'mask' ){ |
| 52 |
if( isset( $document->options->sectionTransition->options->mask->maskParallax ) ){ |
| 53 |
$viewOptions['maskParallax'] = $document->options->sectionTransition->options->mask->maskParallax; |
| 54 |
} |
| 55 |
} elseif( $attributes['view'] === 'transform' ){ |
| 56 |
if( isset( $document->options->sectionTransition->options->transform->transformType ) ){ |
| 57 |
$viewOptions['transformStyle'] = $document->options->sectionTransition->options->transform->transformType; |
| 58 |
} |
| 59 |
} elseif( $attributes['view'] === 'cube' ){ |
| 60 |
if( isset( $document->options->sectionTransition->options->cube->shadow ) ){ |
| 61 |
$viewOptions['shadow'] = $document->options->sectionTransition->options->cube->shadow; |
| 62 |
} |
| 63 |
if( isset( $document->options->sectionTransition->options->cube->dolly ) ){ |
| 64 |
$viewOptions['dolly'] = $document->options->sectionTransition->options->cube->dolly; |
| 65 |
} |
| 66 |
} |
| 67 |
if( $attributes['view'] !== 'fade' ) { |
| 68 |
if ( isset( $document->options->sectionTransition->options->basic->space ) ) { |
| 69 |
$viewOptions['space'] = $document->options->sectionTransition->options->basic->space; |
| 70 |
} |
| 71 |
if ( isset( $document->options->sectionTransition->options->basic->direction ) ) { |
| 72 |
$viewOptions['dir'] = $document->options->sectionTransition->options->basic->direction; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if( in_array( $attributes['view'], ['basic', 'transform'] ) ){ |
| 77 |
if ( isset( $document->options->sectionTransition->options->basic->nearbyVisibility ) ) { |
| 78 |
$viewOptions['nearbyVisibility'] = $document->options->sectionTransition->options->basic->nearbyVisibility; |
| 79 |
} |
| 80 |
if ( isset( $document->options->sectionTransition->options->basic->nearbyVisibilityAmount->value ) ) { |
| 81 |
$viewOptions['nearbyVisibilityAmount'] = $document->options->sectionTransition->options->basic->nearbyVisibilityAmount->value . $document->options->sectionTransition->options->basic->nearbyVisibilityAmount->unit; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if( $viewOptions ){ |
| 86 |
$attributes['viewOptions'] = $viewOptions; |
| 87 |
} |
| 88 |
|
| 89 |
if( isset( $document->options->stretch ) ){ |
| 90 |
$attributes['stretch'] = $document->options->stretch ?? true; |
| 91 |
} |
| 92 |
|
| 93 |
// slideShow property |
| 94 |
if( ! empty( $document->options->navigation->slideshow->enable ) ){ |
| 95 |
$slideShow = []; |
| 96 |
if( isset( $document->options->navigation->slideshow->duration ) ){ |
| 97 |
$slideShow['duration'] = $document->options->navigation->slideshow->duration; |
| 98 |
} |
| 99 |
if( isset( $document->options->navigation->slideshow->pauseOnLastSlide ) ){ |
| 100 |
$slideShow['pauseAtEnd'] = $document->options->navigation->slideshow->pauseOnLastSlide; |
| 101 |
} |
| 102 |
if( isset( $document->options->navigation->slideshow->pauseOnHover ) ){ |
| 103 |
$slideShow['pauseOnHover'] = $document->options->navigation->slideshow->pauseOnHover; |
| 104 |
} |
| 105 |
|
| 106 |
if( isset( $document->options->navigation->slideshow->resetTimerOnBlur ) ){ |
| 107 |
$slideShow['resetTimerOnBlur'] = $document->options->navigation->slideshow->resetTimerOnBlur; |
| 108 |
} |
| 109 |
|
| 110 |
$slideShow['autostart'] = $document->options->navigation->slideshow->enable; |
| 111 |
|
| 112 |
$attributes['slideshow'] = $slideShow; |
| 113 |
} |
| 114 |
|
| 115 |
if( ! empty ( $document->options->navigation->nativeScrollNavigation ) ){ |
| 116 |
$attributes['nativeScrollNavigation'] = $document->options->navigation->nativeScrollNavigation; |
| 117 |
} |
| 118 |
|
| 119 |
if( !empty( $document->options->navigation->swipe->enable ) ){ |
| 120 |
if( isset( $document->options->navigation->swipe->mouseSwipe ) ){ |
| 121 |
$attributes['mouseSwipe'] = $document->options->navigation->swipe->mouseSwipe; |
| 122 |
} |
| 123 |
if( isset( $document->options->navigation->swipe->touchSwipe ) ){ |
| 124 |
$attributes['touchSwipe'] = $document->options->navigation->swipe->touchSwipe; |
| 125 |
} |
| 126 |
if( isset( $document->options->navigation->swipe->direction ) ){ |
| 127 |
$attributes['swipeDir'] = $document->options->navigation->swipe->direction; |
| 128 |
} |
| 129 |
} else { |
| 130 |
$attributes['mouseSwipe'] = false; |
| 131 |
$attributes['touchSwipe'] = false; |
| 132 |
} |
| 133 |
|
| 134 |
if( isset( $document->options->navigation->mouseWheel ) ){ |
| 135 |
$attributes['mouseWheel'] = $document->options->navigation->mouseWheel; |
| 136 |
} |
| 137 |
|
| 138 |
if( isset( $document->options->navigation->keyboardNavigation ) ){ |
| 139 |
$attributes['keyboard'] = $document->options->navigation->keyboardNavigation; |
| 140 |
} |
| 141 |
|
| 142 |
if( isset( $document->options->general->fullscreenMargin ) ){ |
| 143 |
$attributes['fullscreenMargin'] = $document->options->general->fullscreenMargin; |
| 144 |
} |
| 145 |
|
| 146 |
// navigator property |
| 147 |
$navigator = []; |
| 148 |
|
| 149 |
if( !empty( $document->options->navigator ) ){ |
| 150 |
$navigator = (array) $document->options->navigator; |
| 151 |
} |
| 152 |
if( !empty( $document->options->navigator->duration->value ) ){ |
| 153 |
$navigator['duration'] = $document->options->navigator->duration->value; |
| 154 |
} |
| 155 |
if( !empty( $document->startSection ) ){ |
| 156 |
$navigator['start'] = $document->startSection; |
| 157 |
} |
| 158 |
|
| 159 |
if( $navigator ){ |
| 160 |
$attributes['navigator'] = $navigator; |
| 161 |
} |
| 162 |
|
| 163 |
$playerName = 'dpPlayer'; |
| 164 |
$displayExtensionScript = $this->generateDisplayExtensionScript( $document, $playerName ); |
| 165 |
|
| 166 |
if ( ! empty( $displayExtensionScript ) ) { |
| 167 |
$attributes['detachBeforeInit'] = true; |
| 168 |
} |
| 169 |
|
| 170 |
if ( isset ( $document->options->navigation->autoScroll ) ) { |
| 171 |
$attributes['autoScroll'] = $document->options->navigation->autoScroll; |
| 172 |
} |
| 173 |
|
| 174 |
if ( isset( $document->options->documentTypeOptions->carousel ) ) { |
| 175 |
foreach( $document->options->documentTypeOptions->carousel as $key => $value ) { |
| 176 |
if ( $key == 'styles' ) { |
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
$attributes['carouselOptions'][ $key ] = $value; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if ( ! empty( $document->options->navigation->deepLink->enable ) ) { |
| 185 |
$attributes['deepLink'] = $document->options->navigation->deepLink; |
| 186 |
} |
| 187 |
|
| 188 |
$attributes['useWatermark'] = \Depicter::auth()->isSubscriptionExpired(); |
| 189 |
|
| 190 |
if ( $document->hasTeaser() ) { |
| 191 |
$attributes['teaser'] = [ |
| 192 |
'enabled' => true, |
| 193 |
'placement' => $document->options->documentTypeOptions->teaser->placement, |
| 194 |
'behavior' => $document->options->documentTypeOptions->teaser->behavior ?? "always", |
| 195 |
'vSpace' => [ |
| 196 |
'value' => $document->options->documentTypeOptions->teaser->vSpace->value, |
| 197 |
'unit' => $document->options->documentTypeOptions->teaser->vSpace->unit |
| 198 |
], |
| 199 |
'hSpace' => [ |
| 200 |
'value' => $document->options->documentTypeOptions->teaser->hSpace->value, |
| 201 |
'unit' => $document->options->documentTypeOptions->teaser->hSpace->unit |
| 202 |
] |
| 203 |
]; |
| 204 |
} |
| 205 |
|
| 206 |
$attributes = apply_filters( 'depicter/player/script/attributes', $attributes ); |
| 207 |
|
| 208 |
$basePath = \Depicter::core()->assets()->getUrl() . '/resources/scripts/player/'; |
| 209 |
|
| 210 |
$script = "\n(window.depicterSetups = window.depicterSetups || []).push(function(){"; |
| 211 |
$script .= "\n\tDepicter.basePath = '{$basePath}';"; |
| 212 |
$script .= "\n\tconst $playerName = Depicter.setup('.{$document->getSelector()}',\n\t\t"; |
| 213 |
|
| 214 |
$attributesString = JSON::encode( $attributes ); |
| 215 |
|
| 216 |
$script .= "{$attributesString}\n\t);\n"; |
| 217 |
|
| 218 |
$script .= $document->options->getCallbacks( $playerName ); |
| 219 |
|
| 220 |
$script .= $displayExtensionScript; |
| 221 |
|
| 222 |
$script .= $this->generateCustomJSActionsScript( $document ); |
| 223 |
|
| 224 |
$script .= "});\n"; |
| 225 |
|
| 226 |
$this->enqueueRecaptchaScript( $document ); |
| 227 |
|
| 228 |
return $script; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Generate display extension script |
| 233 |
* |
| 234 |
* @param Document $document |
| 235 |
* @param string $playerName |
| 236 |
* |
| 237 |
* @return string |
| 238 |
*/ |
| 239 |
public function generateDisplayExtensionScript( $document, $playerName ) { |
| 240 |
|
| 241 |
if ( ! $document->isDisplayExtension() ) { |
| 242 |
return ''; |
| 243 |
} |
| 244 |
if( empty( $document->options->documentTypeOptions->displayOptions ) ){ |
| 245 |
return ''; |
| 246 |
} |
| 247 |
$displayOptions = $document->options->documentTypeOptions->displayOptions; |
| 248 |
|
| 249 |
unset( $displayOptions->animation ); |
| 250 |
unset( $displayOptions->backdropColor ); |
| 251 |
unset( $displayOptions->backdropBlur ); |
| 252 |
|
| 253 |
$extensionParams = [ |
| 254 |
"type" => $document->getType(), |
| 255 |
"id" => $document->getCssId(), |
| 256 |
"className" => $document->getDisplayStyleSelector(), |
| 257 |
"displayOptions" => $displayOptions |
| 258 |
]; |
| 259 |
|
| 260 |
$triggerParams = ''; |
| 261 |
|
| 262 |
if ( ! \Depicter::front()->preview()->isPreview() ){ |
| 263 |
$displayAgain = Helper::getDisplayAgainProperties( $document->getDocumentID() ); |
| 264 |
$extensionParams = Arr::merge( $displayAgain, $extensionParams ); |
| 265 |
|
| 266 |
if( $triggers = Helper::getDisplayRuleTriggers( $document->getDocumentID() ) ){ |
| 267 |
$triggerParams = ",\n\t\t" . JSON::encode( $triggers ); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
return "\n\tDepicter.display( $playerName, \n\t\t". JSON::encode( $extensionParams ). "{$triggerParams}\n\t);\n"; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Generate custom JS Actions defined by user |
| 276 |
* |
| 277 |
* @param object $document |
| 278 |
* @return string $script |
| 279 |
*/ |
| 280 |
public function generateCustomJSActionsScript( $document ) { |
| 281 |
$script = ''; |
| 282 |
foreach ( $document->elements as $key => $element ) { |
| 283 |
if ( ! empty( $element->actions ) ) { |
| 284 |
foreach ( $element->actions as $actionKey => $action ) { |
| 285 |
if ( $action->type == 'customJS' ) { |
| 286 |
$script .= "\n\tDepicter.jsActions['" . $actionKey . "'] = function(){\n\t\t" . $action->options->value . "\n\t}\n"; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return $script; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Undocumented function |
| 297 |
* |
| 298 |
* @param Document $document |
| 299 |
* |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
public function enqueueRecaptchaScript( $document ) { |
| 303 |
if ( ! $document->isRecaptchaEnabled() ) { |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
$clientKey = \Depicter::options()->get('google_recaptcha_client_key', false ); |
| 308 |
$secretKey = \Depicter::options()->get('google_recaptcha_secret_key', false ); |
| 309 |
|
| 310 |
if ( $clientKey && $secretKey ) { |
| 311 |
wp_enqueue_script( 'google-recaptcha', 'https://www.google.com/recaptcha/api.js?render=' . $clientKey ); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|