| @@ -3,19 +3,20 @@ | ||
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | use Averta\Core\Hydrate\HydratableInterface; |
| 6 | 6 | use Averta\Core\Utility\Arr; |
| 7 | +use Averta\Core\Utility\Data; | |
| 7 | 8 | use Averta\WordPress\Utility\Sanitize; |
| 8 | 9 | use Depicter\Document\CSS\Selector; |
| 9 | 10 | use Depicter\Document\Models\Options\Loading; |
| 10 | 11 | use Depicter\Document\Models\Options\Script; |
| 11 | -use Depicter\Document\Models\Traits\HasDocumentIdTrait; | |
| 12 | +use Depicter\Document\Models\Traits\UnPublishedNoticeTrait; | |
| 12 | 13 | use Depicter\Html\Html; |
| 13 | 14 | use Depicter\Services\StyleGeneratorService; |
| 14 | 15 | |
| 15 | 16 | class Document implements HydratableInterface |
| 16 | 17 | { |
| 17 | - use HasDocumentIdTrait; | |
| 18 | + use UnPublishedNoticeTrait; | |
| 18 | 19 | |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * @var array |
| @@ -52,8 +53,13 @@ | ||
| 52 | 53 | */ |
| 53 | 54 | public $meta; |
| 54 | 55 | |
| 55 | 56 | /** |
| 57 | + * @var array | |
| 58 | + */ | |
| 59 | + public $env = []; | |
| 60 | + | |
| 61 | + /** | |
| 56 | 62 | * Start from which section |
| 57 | 63 | * |
| 58 | 64 | * @var int |
| 59 | 65 | */ |
| @@ -94,8 +100,22 @@ | ||
| 94 | 100 | */ |
| 95 | 101 | private $fontLink; |
| 96 | 102 | |
| 97 | 103 | /** |
| 104 | + * Extra document args | |
| 105 | + * | |
| 106 | + * @var array | |
| 107 | + */ | |
| 108 | + private $args = []; | |
| 109 | + | |
| 110 | + /** | |
| 111 | + * Check if document is ai generated or not | |
| 112 | + * | |
| 113 | + * @var boolean | |
| 114 | + */ | |
| 115 | + public $isBuildWithAI = false; | |
| 116 | + | |
| 117 | + /** | |
| 98 | 118 | * Extract values for this class |
| 99 | 119 | * |
| 100 | 120 | * @return array |
| 101 | 121 | */ |
| @@ -150,8 +170,9 @@ | ||
| 150 | 170 | 'id' => $this->getCssId(), |
| 151 | 171 | 'class' => $this->getClassNames() |
| 152 | 172 | ]); |
| 153 | 173 | |
| 174 | + $this->renderNotice(); | |
| 154 | 175 | $this->renderLoadingSymbol(); |
| 155 | 176 | $this->renderForegroundElements(); |
| 156 | 177 | $this->renderSectionsAndElements(); |
| 157 | 178 | $this->collectAndSetFontsData(); |
| @@ -161,8 +182,18 @@ | ||
| 161 | 182 | return $this->html . $this->getInitScriptTag(); |
| 162 | 183 | } |
| 163 | 184 | |
| 164 | 185 | /** |
| 186 | + * Render markup for possible notices | |
| 187 | + * | |
| 188 | + * @return void | |
| 189 | + */ | |
| 190 | + protected function renderNotice(){ | |
| 191 | + $notice = $this->getUnpublishedChangesNotice(); | |
| 192 | + $this->html->nest( "\n" . $notice ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 165 | 196 | * Render markup for loading symbols |
| 166 | 197 | */ |
| 167 | 198 | protected function renderLoadingSymbol() { |
| 168 | 199 | if ( empty( $this->options->loading ) ) { |
| @@ -225,15 +256,25 @@ | ||
| 225 | 256 | |
| 226 | 257 | /** |
| 227 | 258 | * Collects fonts and generates a link for loading document fonts |
| 228 | 259 | */ |
| 229 | - protected function collectAndSetFontsData() | |
| 230 | - { | |
| 260 | + protected function collectAndSetFontsData(){ | |
| 261 | + if( !empty( $this->env['additionalFonts'] ) ){ | |
| 262 | + // convert to array recursively | |
| 263 | + $this->env['additionalFonts'] = Data::cast( $this->env['additionalFonts'], 'array' ); | |
| 264 | + | |
| 265 | + if( is_array( $this->env['additionalFonts'] ) ){ | |
| 266 | + foreach( $this->env['additionalFonts'] as $localFontName => $localFontInfo ){ | |
| 267 | + \Depicter::documentFonts()->addLocalFont( $this->getDocumentID(), $localFontName, $localFontInfo['variants'] ); | |
| 268 | + } | |
| 269 | + } | |
| 270 | + } | |
| 271 | + | |
| 231 | 272 | foreach ( $this->sections as $section ) { |
| 232 | 273 | // this method adds element fonts to documentFonts service |
| 233 | 274 | $section->collectElementFonts(); |
| 234 | 275 | } |
| 235 | - $this->fontLink = \Depicter::app()->documentFonts()->getFontsLink( $this->getDocumentID() ); | |
| 276 | + $this->fontLink = \Depicter::documentFonts()->getFontsLink( $this->getDocumentID() ); | |
| 236 | 277 | } |
| 237 | 278 | |
| 238 | 279 | /** |
| 239 | 280 | * Render document custom styles |
| @@ -248,11 +289,9 @@ | ||
| 248 | 289 | $this->stylesList[ '.'. $this->getSelector() ] = []; |
| 249 | 290 | } |
| 250 | 291 | |
| 251 | 292 | $documentStyles = [ |
| 252 | - '.'. $this->getStyleSelector() => $this->options->getStyles(), | |
| 253 | 293 | '.'. $this->getStyleSelector() . ' .depicter-section' => $this->options->getSectionGeneralStyles(), |
| 254 | - '.'. $this->getStyleSelector( true ) . ':not(.depicter-ready)' => $this->options->getBeforeInitStyles(), // styles to prevent FOUC. It should not have depicter-revert class in selector | |
| 255 | 294 | '.'. $this->getStyleSelector() . ' .depicter-layers-wrapper' => $this->options->getLayersWrapperStyles() |
| 256 | 295 | ]; |
| 257 | 296 | // prepend document styles at the beginning of the styles |
| 258 | 297 | $this->stylesList = $documentStyles + $this->stylesList; |
| @@ -257,8 +296,16 @@ | ||
| 257 | 296 | // prepend document styles at the beginning of the styles |
| 258 | 297 | $this->stylesList = $documentStyles + $this->stylesList; |
| 259 | 298 | |
| 260 | 299 | $this->stylesList[ '.'. $this->getStyleSelector() ]['customStyle'] = $this->getCustomStyles(); |
| 300 | + | |
| 301 | + // add before init styles separately to style list as well | |
| 302 | + $this->stylesList[ '.'. $this->getStyleSelector() ]['beforeInitStyle'] = [ | |
| 303 | + '.'. $this->getStyleSelector() => $this->options->getStyles(), | |
| 304 | + '.'. $this->getStyleSelector( true ) . ':not(.depicter-ready)' => $this->options->getBeforeInitStyles(), // styles to prevent FOUC. It should not have depicter-revert class in selector | |
| 305 | + ]; | |
| 306 | + | |
| 307 | + return $this->stylesList; | |
| 261 | 308 | } |
| 262 | 309 | |
| 263 | 310 | /** |
| 264 | 311 | * Retrieves StyleGeneratorService |
| @@ -302,8 +349,19 @@ | ||
| 302 | 349 | return $this->styleGenerator( ['forceRegenerateStyles' => $forceRegenerateStyles] )->getCss( $forceRegenerateStyles = false ); |
| 303 | 350 | } |
| 304 | 351 | |
| 305 | 352 | /** |
| 353 | + * Retrieves before init CSS | |
| 354 | + * | |
| 355 | + * @param bool $forceRegenerateStyles Whether regenerate before init styles or not | |
| 356 | + * | |
| 357 | + * @return string css styles | |
| 358 | + */ | |
| 359 | + public function getBeforeInitCssAndTag( $forceRegenerateStyles = false ){ | |
| 360 | + return $this->styleGenerator( ['forceRegenerateStyles' => $forceRegenerateStyles] )->getBeforeInitCssAndTag( $forceRegenerateStyles = false ); | |
| 361 | + } | |
| 362 | + | |
| 363 | + /** | |
| 306 | 364 | * Retrieves generated css wrapper in a style tag |
| 307 | 365 | * |
| 308 | 366 | * @param bool $forceRegenerateStyles Whether regenerate styles or not |
| 309 | 367 | * |
| @@ -415,9 +473,9 @@ | ||
| 415 | 473 | if( in_array('google-font', $fileKeysToInclude) && $fontLink = $this->getFontsLink() ){ |
| 416 | 474 | $documentCustomStyles[ "depicter-{$this->getDocumentID()}-google-font" ] = $fontLink; |
| 417 | 475 | } |
| 418 | 476 | if( in_array('custom', $fileKeysToInclude) && $customCssFileUrl = $this->getCssFileUrl() ){ |
| 419 | - $documentCustomStyles[ "depicter-{$this->getDocumentID()}-custom" ] = $customCssFileUrl; | |
| 477 | + $documentCustomStyles[ "depicter--{$this->getDocumentID()}-custom" ] = $customCssFileUrl; | |
| 420 | 478 | } |
| 421 | 479 | |
| 422 | 480 | return $documentCustomStyles; |
| 423 | 481 | } |
| @@ -571,8 +629,47 @@ | ||
| 571 | 629 | */ |
| 572 | 630 | public function getSectionsCount() |
| 573 | 631 | { |
| 574 | 632 | return is_array( $this->sectionsList ) ? count( $this->sectionsList ) : 0; |
| 633 | + } | |
| 634 | + | |
| 635 | + /** | |
| 636 | + * Gel list of section objects | |
| 637 | + * | |
| 638 | + * @return Section[] | |
| 639 | + */ | |
| 640 | + public function getSections() | |
| 641 | + { | |
| 642 | + return $this->sections ?? []; | |
| 643 | + } | |
| 644 | + | |
| 645 | + /** | |
| 646 | + * Get a section object by section ID | |
| 647 | + * | |
| 648 | + * @param string $sectionId The section ID | |
| 649 | + * | |
| 650 | + * @return Section|null | |
| 651 | + */ | |
| 652 | + public function getSectionById( $sectionId ) | |
| 653 | + { | |
| 654 | + return $this->sections[ $sectionId ] ?? null; | |
| 655 | + } | |
| 656 | + | |
| 657 | + /** | |
| 658 | + * Get a section by section number. starts from 1 | |
| 659 | + * | |
| 660 | + * @param int $sectionNumber The section number | |
| 661 | + * | |
| 662 | + * @return Section|null | |
| 663 | + */ | |
| 664 | + public function getSectionNth( $sectionNumber ) | |
| 665 | + { | |
| 666 | + if( ! $this->getSections() ){ | |
| 667 | + return null; | |
| 668 | + } | |
| 669 | + | |
| 670 | + $sectionIndex = $sectionNumber > 0 ? $sectionNumber - 1 : 1; | |
| 671 | + return array_values( $this->getSections() )[ $sectionIndex ] ?? null; | |
| 575 | 672 | } |
| 576 | 673 | |
| 577 | 674 | /** |
| 578 | 675 | * Get init script |