| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Depicter\Document\CSS\Selector; |
| 6 |
use Depicter\Document\Helper\Helper; |
| 7 |
use Depicter\Document\Models\Elements\Form; |
| 8 |
use Depicter\Document\Models\Traits\HasDataSheetTrait; |
| 9 |
use Depicter\Document\Models\Traits\HasDocumentIdTrait; |
| 10 |
use Depicter\Document\Models\Traits\HasDocumentTypeTrait; |
| 11 |
use Depicter\Html\Html; |
| 12 |
|
| 13 |
class Section |
| 14 |
{ |
| 15 |
use HasDocumentIdTrait; |
| 16 |
use HasDataSheetTrait; |
| 17 |
use HasDocumentTypeTrait; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $id; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $name; |
| 28 |
|
| 29 |
/** |
| 30 |
* Index|order of the section |
| 31 |
* |
| 32 |
* @var int|null |
| 33 |
*/ |
| 34 |
public $index; |
| 35 |
|
| 36 |
/** |
| 37 |
* List of belonging element ids (assigns with jsonMapper) |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
public $elements; |
| 42 |
|
| 43 |
/** |
| 44 |
* List of belonging elements |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
public $elementObjects; |
| 49 |
|
| 50 |
/** |
| 51 |
* @var Common\Size\States |
| 52 |
*/ |
| 53 |
public $wrapperSize; |
| 54 |
|
| 55 |
/** |
| 56 |
* @var Common\Background |
| 57 |
*/ |
| 58 |
public $background; |
| 59 |
|
| 60 |
/** |
| 61 |
* @var object |
| 62 |
*/ |
| 63 |
public $options; |
| 64 |
|
| 65 |
/** |
| 66 |
* @var \Depicter\Document\Models\Common\Animation|null |
| 67 |
*/ |
| 68 |
public $animation; |
| 69 |
|
| 70 |
/** |
| 71 |
* @var Common\Parallax|null |
| 72 |
*/ |
| 73 |
public $parallax; |
| 74 |
|
| 75 |
/** |
| 76 |
* @var Common\KenBurns|null |
| 77 |
*/ |
| 78 |
public $kenBurns; |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* @var Common\DataSourceOptions|null |
| 83 |
*/ |
| 84 |
public $dataSource = null; |
| 85 |
|
| 86 |
/** |
| 87 |
* @var array|null |
| 88 |
*/ |
| 89 |
public $actions; |
| 90 |
|
| 91 |
/** |
| 92 |
* @var string |
| 93 |
*/ |
| 94 |
public $className = ''; |
| 95 |
|
| 96 |
/** |
| 97 |
* @var array |
| 98 |
*/ |
| 99 |
protected $styleList = []; |
| 100 |
|
| 101 |
/** |
| 102 |
* @var string |
| 103 |
*/ |
| 104 |
protected $preloadTags = ''; |
| 105 |
|
| 106 |
/** |
| 107 |
* @var string |
| 108 |
*/ |
| 109 |
public $visibility = 'visible'; |
| 110 |
|
| 111 |
/** |
| 112 |
* @var object|null |
| 113 |
*/ |
| 114 |
public $visibilitySchedule; |
| 115 |
|
| 116 |
/** |
| 117 |
* @var string |
| 118 |
*/ |
| 119 |
public $type = 'section'; |
| 120 |
|
| 121 |
/** |
| 122 |
* get section ID |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public function getID() { |
| 127 |
return Helper::getSectionIdFromSlug( $this->id ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* get section slug |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function getSlug() { |
| 136 |
return $this->id; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get section name |
| 141 |
* |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
public function getName() { |
| 145 |
return $this->name ? $this->name : $this->id; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get section name |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public function getType() { |
| 154 |
return $this->type ?? "section"; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Gets belonging element ids |
| 159 |
* |
| 160 |
* @return array |
| 161 |
*/ |
| 162 |
public function getElementIds() |
| 163 |
{ |
| 164 |
return $this->elements; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Sets belonging elements |
| 169 |
* |
| 170 |
* @param array $elementIds |
| 171 |
*/ |
| 172 |
public function setElementIds( $elementIds ) |
| 173 |
{ |
| 174 |
$this->elements = $elementIds; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Sets belonging elements |
| 179 |
* |
| 180 |
* @param array $elementObjects |
| 181 |
*/ |
| 182 |
public function setElementObjects( $elementObjects ) |
| 183 |
{ |
| 184 |
$this->elementObjects = $elementObjects; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Renders the Section |
| 189 |
*/ |
| 190 |
public function render() { |
| 191 |
if ( $this->visibility == 'hidden' ) { |
| 192 |
return ''; |
| 193 |
} |
| 194 |
|
| 195 |
if ( $this->visibility == 'scheduled' && !is_null( $this->visibilitySchedule ) ) { |
| 196 |
if ( !empty( $this->visibilitySchedule->start ) && !\Depicter::schedule()->isDatePassed( $this->visibilitySchedule->start ) ) { |
| 197 |
\Depicter::schedule()->scheduleClearCacheEvent( $this->getDocumentID(), $this->visibilitySchedule->start ); |
| 198 |
return ''; |
| 199 |
} |
| 200 |
|
| 201 |
if ( !empty( $this->visibilitySchedule->end ) && ! \Depicter::schedule()->isDatePassed( $this->visibilitySchedule->end ) ) { |
| 202 |
\Depicter::schedule()->scheduleClearCacheEvent( $this->getDocumentID(), $this->visibilitySchedule->end ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( !empty( $this->visibilitySchedule->end ) && \Depicter::schedule()->isDatePassed( $this->visibilitySchedule->end ) ) { |
| 206 |
return ''; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// render section by dataSource if exists |
| 211 |
if( $this->dataSource && $sectionMarkup = $this->renderDataSource() ){ |
| 212 |
return $sectionMarkup; |
| 213 |
} |
| 214 |
return $this->renderStatic(); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Renders the Section from dataSource |
| 219 |
*/ |
| 220 |
protected function renderDataSource() { |
| 221 |
$sectionMarkup = ''; |
| 222 |
$dataSourceInstance = \Depicter::dataSource()->getByType( $this->dataSource->type ); |
| 223 |
if( $dataSourceInstance ){ |
| 224 |
$dataSheets = $dataSourceInstance->getDataSheetArgs( $this->dataSource->params ); |
| 225 |
foreach( $dataSheets as $dataSheet ){ |
| 226 |
$this->setDataSheet( $dataSheet ); |
| 227 |
$sectionMarkup .= $this->renderStatic(); |
| 228 |
} |
| 229 |
} |
| 230 |
return $sectionMarkup; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Renders the Section |
| 236 |
*/ |
| 237 |
protected function renderStatic() { |
| 238 |
|
| 239 |
// default Section properties |
| 240 |
$args = [ |
| 241 |
'id' => $this->getCssID(), |
| 242 |
'class' => $this->getClassNames(), |
| 243 |
'data-name' => $this->getName(), |
| 244 |
'data-type' => $this->getType(), |
| 245 |
'data-local-id' => $this->id |
| 246 |
]; |
| 247 |
|
| 248 |
if ( !empty( $this->wrapperSize ) ) { |
| 249 |
$sectionWidth = $this->wrapperSize->getResponsiveSizes( "width" ); |
| 250 |
if ( ! $this->wrapperSize->hasNoResponsiveSize( $sectionWidth ) ) { |
| 251 |
$args[ 'data-wrapper-width'] = implode( ',', $sectionWidth ); |
| 252 |
$args[ 'data-wrapper-width'] = str_replace( 'auto', '', $args[ 'data-wrapper-width'] ); |
| 253 |
if ( $args[ 'data-wrapper-width'] == ',,' ) { |
| 254 |
unset( $args[ 'data-wrapper-width'] ); |
| 255 |
} |
| 256 |
} |
| 257 |
$sectionHeight = $this->wrapperSize->getResponsiveSizes( "height" ); |
| 258 |
if ( ! $this->wrapperSize->hasNoResponsiveSize( $sectionHeight ) ) { |
| 259 |
$args['data-wrapper-height'] = implode( ',', $sectionHeight ); |
| 260 |
$args[ 'data-wrapper-height'] = str_replace( 'auto', '', $args[ 'data-wrapper-height'] ); |
| 261 |
if ( $args[ 'data-wrapper-height'] == ',,' ) { |
| 262 |
unset( $args[ 'data-wrapper-height'] ); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if ( !empty( $this->options ) ) { |
| 268 |
// check for autoplay option |
| 269 |
if ( !empty( $this->options->slideshowDuration ) ) { |
| 270 |
$args['data-slideshow-duration'] = $this->options->slideshowDuration; |
| 271 |
} |
| 272 |
// check for autoplay option |
| 273 |
if ( !empty( $this->options->pauseSlideshow ) ) { |
| 274 |
$args['data-slideshow-pause'] = $this->options->pauseSlideshow ? "true" : "false"; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if ( $this->background->hasBackground() && $this->parallax && false !== $parallaxData = $this->parallax->getParallaxAttrs() ) { |
| 279 |
$args = Arr::merge( $args, $parallaxData ); |
| 280 |
} |
| 281 |
|
| 282 |
$actions = Helper::getActions( $this->actions ); |
| 283 |
if ( !empty( $actions ) ) { |
| 284 |
$args = Arr::merge( $args, [ |
| 285 |
'data-actions' => $actions |
| 286 |
]); |
| 287 |
} |
| 288 |
|
| 289 |
if ( $this->animation && false !== $animationData = $this->animation->getAnimationAttrs() ) { |
| 290 |
$args = Arr::merge( $args, $animationData ); |
| 291 |
} |
| 292 |
|
| 293 |
$div = Html::div($args); |
| 294 |
|
| 295 |
// check if section has link |
| 296 |
if ( $this->hasEnabledLink() ) { |
| 297 |
// Section link anchor element should always have target="_black" unless openInNewTab is false. |
| 298 |
$urlArgs = isset( $this->options->url->openInNewTab ) && ! $this->options->url->openInNewTab ? [] : ['target' => '_blank']; |
| 299 |
$urlArgs['class'] = Selector::prefixify('section-link'); |
| 300 |
|
| 301 |
if ( $urlPath = $this->getSectionUrl() ) { |
| 302 |
$div->nest( Html::a( '', $urlPath, $urlArgs ) . "\n" ); |
| 303 |
} |
| 304 |
} |
| 305 |
if( $sectionBackground = $this->background->render() ){ |
| 306 |
$this->background->setDataSheet( $this->getDataSheet() ); |
| 307 |
|
| 308 |
if ( $this->kenBurns && false !== $kenBurnsData = $this->kenBurns->getKenBurnsAttrs() ) { |
| 309 |
$this->background->setKenBurnsData( $kenBurnsData ); |
| 310 |
} |
| 311 |
|
| 312 |
$div->nest( "\n" . $this->background->render() . "\n\n" ); |
| 313 |
} |
| 314 |
|
| 315 |
$this->styleList = Arr::merge( $this->styleList, $this->getSelectorAndCssList() ); |
| 316 |
|
| 317 |
if ( !empty( $this->elementObjects ) ) { |
| 318 |
|
| 319 |
// adds a form tag on first section for survey document type |
| 320 |
if ( $this->index === 0 && $this->isDocumentType('survey') ) { |
| 321 |
$form = $this->renderSurveyForm(); |
| 322 |
$div->nest( "\n" . $form ); |
| 323 |
} |
| 324 |
|
| 325 |
foreach ( $this->elementObjects as $elementObject ) { |
| 326 |
// if dataSheet is available for current section, assign it elements of this section as well |
| 327 |
if( $this->getDataSheet() ){ |
| 328 |
$elementObject->prepare()->setDataSheet( $this->getDataSheet() ); |
| 329 |
} |
| 330 |
// Get element style |
| 331 |
$this->styleList = Arr::merge( $this->styleList, $elementObject->prepare()->getSelectorAndCssList() ); |
| 332 |
|
| 333 |
$div->nest( "\n" . $elementObject->prepare()->render() ); |
| 334 |
|
| 335 |
// generate and retrieve media files of elements for preloading |
| 336 |
$this->preloadTags .= $elementObject->prepare()->getPreloadTags(); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
return "\n" . $div; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Whether linking slide ro url is enabled or not |
| 345 |
* |
| 346 |
* @return bool |
| 347 |
*/ |
| 348 |
public function hasEnabledLink(){ |
| 349 |
return $this->isLinkedToDataSheet() || !empty( $this->options->url->enable ); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Retrieves markup for preloading media files belonging to this section |
| 354 |
* |
| 355 |
* @return string |
| 356 |
*/ |
| 357 |
public function getPreloadTags(){ |
| 358 |
return $this->preloadTags; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Retrieves section url if exists |
| 363 |
* |
| 364 |
* @return string |
| 365 |
*/ |
| 366 |
public function getSectionUrl(){ |
| 367 |
if( $this->isLinkedToDataSheet() && $url = $this->getDataSheetUrl() ){ |
| 368 |
return $url; |
| 369 |
} elseif( ! empty( $this->options->url->path ) ){ |
| 370 |
return $this->options->url->path; |
| 371 |
} |
| 372 |
return ''; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Collect element font and add it to document font service |
| 377 |
*/ |
| 378 |
public function collectElementFonts(){ |
| 379 |
if ( !empty( $this->elementObjects ) ) { |
| 380 |
foreach ( $this->elementObjects as $elementObject ) { |
| 381 |
$elementObject->prepare()->getFontsList(); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Get section class names. |
| 388 |
* |
| 389 |
* @return string |
| 390 |
*/ |
| 391 |
protected function getClassNames(){ |
| 392 |
$classes = []; |
| 393 |
|
| 394 |
$classes[] = Selector::prefixify(Selector::SECTION_PREFIX ); |
| 395 |
$classes[] = $this->getSelector(); |
| 396 |
$classes[] = $this->getDataSheetClassName(); |
| 397 |
$classes[] = $this->getCustomClassName(); |
| 398 |
|
| 399 |
return trim( implode(' ', $classes) ); |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Get section CSS ID for data sheet if dataSource is assigned |
| 404 |
* |
| 405 |
* @return string |
| 406 |
*/ |
| 407 |
public function getDataSheetClassName() { |
| 408 |
if( $this->getDataSheetID() ){ |
| 409 |
return $this->getCssID(); |
| 410 |
} |
| 411 |
return ''; |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Retrieves custom class name of document |
| 416 |
* |
| 417 |
* @return string |
| 418 |
*/ |
| 419 |
protected function getCustomClassName(){ |
| 420 |
if ( ! empty( $this->className ) ) { |
| 421 |
return $this->className; |
| 422 |
} |
| 423 |
return ''; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Retrieves custom styles of document |
| 428 |
* |
| 429 |
* @return string |
| 430 |
*/ |
| 431 |
protected function getCustomStyles(){ |
| 432 |
if ( ! empty( $this->options->customStyle ) ) { |
| 433 |
$customStyles = $this->options->customStyle; |
| 434 |
// replace "selector" with unique selector of section |
| 435 |
return str_replace('selector', '.'.$this->getStyleSelector(), $customStyles ); |
| 436 |
} |
| 437 |
return ''; |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* Get Elements Style that presented in this section. |
| 442 |
* |
| 443 |
* @return array |
| 444 |
*/ |
| 445 |
public function getCss() { |
| 446 |
return $this->styleList; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Get section CSS ID |
| 451 |
* |
| 452 |
* @return string |
| 453 |
*/ |
| 454 |
public function getCssID() { |
| 455 |
$cssID = Helper::getSectionCssId( $this->getDocumentID(), $this->getID() ); |
| 456 |
if( $dataSheetId = $this->getDataSheetID() ){ |
| 457 |
$cssID .= "-{$dataSheetId}"; |
| 458 |
} |
| 459 |
|
| 460 |
return $cssID; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Get section selector |
| 465 |
* |
| 466 |
* @return string |
| 467 |
*/ |
| 468 |
public function getSelector() { |
| 469 |
return Selector::getUniqueSelector( $this->getDocumentID(), $this->getID(), null, Selector::SECTION_PREFIX ); |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Get section style selector |
| 474 |
* |
| 475 |
* @return string |
| 476 |
*/ |
| 477 |
public function getStyleSelector() { |
| 478 |
return Selector::PREFIX_CSS . " ." . $this->getSelector(); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Get list of selector and CSS for section |
| 483 |
* |
| 484 |
* @return array |
| 485 |
*/ |
| 486 |
protected function getSelectorAndCssList(){ |
| 487 |
$styleList = []; |
| 488 |
|
| 489 |
$styleList[ '.' . $this->getStyleSelector() . ' .' . $this->background->getContainerClassName() ] = array_merge_recursive( |
| 490 |
$this->background->getColor(), |
| 491 |
$this->background->getSectionBackgroundFilter() |
| 492 |
); |
| 493 |
$styleList[ '.' . $this->getStyleSelector() . ' .' . $this->background->getContainerClassName() . '::after' ] = $this->background->getOverlayStyles(); |
| 494 |
|
| 495 |
if ( $this->getCustomStyles() ) { |
| 496 |
$styleList[ '.' . $this->getStyleSelector() ]['customStyle'] = $this->getCustomStyles(); |
| 497 |
} |
| 498 |
|
| 499 |
return $styleList; |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Check if this section has shortcode element or not |
| 504 |
* |
| 505 |
* @return boolean |
| 506 |
*/ |
| 507 |
public function hasShortcode() { |
| 508 |
if ( ! empty( $this->elementObjects ) ) { |
| 509 |
foreach( $this->elementObjects as $elementObject ) { |
| 510 |
if ( $elementObject->type == 'wpShortcode' ) { |
| 511 |
return true; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Check if this section has shortcode element or not |
| 521 |
* |
| 522 |
* @return bool|array |
| 523 |
*/ |
| 524 |
public function hasForm() { |
| 525 |
$formIDs = []; |
| 526 |
if ( ! empty( $this->elementObjects ) ) { |
| 527 |
foreach( $this->elementObjects as $elementObject ) { |
| 528 |
/** |
| 529 |
* @var Element $elementObject |
| 530 |
*/ |
| 531 |
if ( $elementObject->type == 'form' ) { |
| 532 |
$formIDs[] = [ |
| 533 |
'id' => $elementObject->getCssID(), |
| 534 |
'captcha' => !empty( $elementObject->options->captcha ) |
| 535 |
]; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
return ! empty( $formIDs ) ? $formIDs : false; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Render survey form |
| 545 |
* |
| 546 |
* @return string |
| 547 |
*/ |
| 548 |
public function renderSurveyForm() { |
| 549 |
$args = [ |
| 550 |
'data-wrap' => "false", |
| 551 |
"data-responsive-scale" => "true,,", |
| 552 |
"data-name" => "", |
| 553 |
"data-local-id" => "" |
| 554 |
]; |
| 555 |
$args['id'] = 'depicter-survey-form-' . $this->getDocumentID(); |
| 556 |
$args['class'] = "depicter-element depicter-layer depicter-label-top depicter-survey-form"; |
| 557 |
$args['class'] = str_replace('depicter- ', '', $args['class']); |
| 558 |
$args['data-type'] = 'survey:form'; |
| 559 |
|
| 560 |
$formContent = Html::input( 'hidden', 'action', 'depicter-lead-submit') . "\n"; |
| 561 |
$formContent .= Html::input( 'hidden', '_sourceId', $this->getDocumentID() ) . "\n"; |
| 562 |
|
| 563 |
$formContent .= Html::input( 'hidden', '_contentId', $this->getID() ) . "\n"; |
| 564 |
$formContent .= Html::input( 'hidden', '_contentName', $this->getName() ) . "\n"; |
| 565 |
|
| 566 |
$formContent .= Html::input( 'hidden', '_csrfToken', wp_create_nonce( 'depicter-csrf-lead-' . $this->getDocumentID() ) ) . "\n\n"; |
| 567 |
|
| 568 |
$form = Html::form( trim( wp_parse_url( self_admin_url( 'admin-ajax.php' ), PHP_URL_PATH ) ), 'post', $args, "\n" . $formContent ); |
| 569 |
|
| 570 |
return $form; |
| 571 |
} |
| 572 |
} |
| 573 |
|