| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | * |
| 27 | 27 | * @return string|void |
| 28 | 28 | */ |
| 29 | 29 | public function document( $documentID = 0, $args = [] ){ |
| 30 | - $isPrivilegedUser = \Depicter::authorization()->currentUserCan( [ 'manage_options', 'publish_depicter' ] ); | |
| 30 | + $isPrivilegedUser = \Depicter::authorization()->currentUserCanPublishDocument(); | |
| 31 | 31 | |
| 32 | 32 | $defaults = [ |
| 33 | 33 | 'loadStyleMode' => 'auto',// ["auto", "inline", "file"]."auto" loads custom css if available, otherwise prints styles inline |
| 34 | 34 | 'useCache' => true, |
| @@ -38,8 +38,10 @@ | ||
| 38 | 38 | ]; |
| 39 | 39 | |
| 40 | 40 | $args = Arr::merge( $args, $defaults ); |
| 41 | 41 | |
| 42 | + $args['isPrivilegedUser'] = $isPrivilegedUser; | |
| 43 | + | |
| 42 | 44 | if ( $isPrivilegedUser ) { |
| 43 | 45 | $args['status'] = ['publish', 'draft']; |
| 44 | 46 | $args['useCache'] = false; |
| 45 | 47 | $args['loadStyleMode'] = 'inline'; |
| @@ -44,9 +46,10 @@ | ||
| 44 | 46 | $args['useCache'] = false; |
| 45 | 47 | $args['loadStyleMode'] = 'inline'; |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | - $args['isPrivilegedUser'] = $isPrivilegedUser; | |
| 50 | + // Fallback to load missing asset files of a document | |
| 51 | + \Depicter::front()->assets()->enqueueAssets( $documentID, $isPrivilegedUser ); | |
| 49 | 52 | |
| 50 | 53 | $output = $this->getDocument( $documentID, $args ); // retrieves escaped slider markup |
| 51 | 54 | |
| 52 | 55 | if( $args['echo'] ){ |
| @@ -69,9 +72,9 @@ | ||
| 69 | 72 | if ( empty( $documentID ) ) { |
| 70 | 73 | return esc_html__( 'Slider ID is required.', 'depicter' ); |
| 71 | 74 | } |
| 72 | 75 | if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID, $args ) ) ){ |
| 73 | - return $cacheOutput . '<span hidden>Depicter cache hit.</span>'; | |
| 76 | + return $cacheOutput; | |
| 74 | 77 | } |
| 75 | 78 | |
| 76 | 79 | $output = ''; |
| 77 | 80 | $where = [ 'status' => $args['status'] ]; |
| @@ -81,20 +84,20 @@ | ||
| 81 | 84 | if( ! $documentID = \Depicter::document()->getID( $documentID ) ){ |
| 82 | 85 | return esc_html__( 'Slider alias not found.', 'depicter' ); |
| 83 | 86 | } |
| 84 | 87 | |
| 85 | - if( $args['showUnpublishedNotice'] ){ | |
| 86 | - $output .= $this->getUnPublishedNoticeMarkup( $documentID ); | |
| 87 | - } elseif( \Depicter::document()->repository()->isNotPublished( $documentID, $where ) ) { | |
| 88 | + if( ! $args['showUnpublishedNotice'] && ! \Depicter::document()->repository()->isPublishedBefore( $documentID ) ) { | |
| 88 | 89 | throw new DocumentNotPublished( __( 'Slider is not published yet and saved as "draft"', 'depicter' ), 0, $where ); |
| 89 | 90 | } |
| 90 | 91 | |
| 91 | 92 | if( $documentModel = \Depicter::document()->getModel( $documentID, $where ) ){ |
| 93 | + $documentModel->setUnpublishedNotice( $args['showUnpublishedNotice'] ); | |
| 92 | 94 | |
| 93 | 95 | $output .= $documentModel->prepare()->render(); |
| 96 | + | |
| 94 | 97 | $documentModel->styleGenerator( $styleGeneratorArgs ); |
| 95 | 98 | |
| 96 | - if( $args['isPrivilegedUser'] ){ | |
| 99 | + if( $args['isPrivilegedUser'] && \Depicter::document()->repository()->isPublished( $documentID ) ){ | |
| 97 | 100 | $documentModel->saveCss(); |
| 98 | 101 | } |
| 99 | 102 | |
| 100 | 103 | if( ( $args['loadStyleMode'] == 'inline' ) ){ |
| @@ -106,8 +109,10 @@ | ||
| 106 | 109 | $output = $documentModel->getInlineCssTag() . $output; |
| 107 | 110 | } |
| 108 | 111 | } |
| 109 | 112 | |
| 113 | + $output = $documentModel->getBeforeInitCssAndTag() . $output; | |
| 114 | + | |
| 110 | 115 | //---------- |
| 111 | 116 | |
| 112 | 117 | $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' ); |
| 113 | 118 | |
| @@ -112,8 +117,13 @@ | ||
| 112 | 117 | $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' ); |
| 113 | 118 | |
| 114 | 119 | $this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS ); |
| 115 | 120 | |
| 121 | + if( $firstSection = $documentModel->getSectionNth(1) ){ | |
| 122 | + $preloadTags = $firstSection->getPreloadTags(); | |
| 123 | + $this->cache->set( $documentID . '_preload_tags', $preloadTags, WEEK_IN_SECONDS ); | |
| 124 | + } | |
| 125 | + | |
| 116 | 126 | // sanitize the output before caching |
| 117 | 127 | $output = Sanitize::html( $output, null, 'depicter/output' ); |
| 118 | 128 | |
| 119 | 129 | if( $args['useCache'] ){ |
| @@ -171,27 +181,15 @@ | ||
| 171 | 181 | return $this->cache->delete( $documentID ."_". $args['loadStyleMode'] ); |
| 172 | 182 | } |
| 173 | 183 | |
| 174 | 184 | /** |
| 175 | - * Get unpublished notice markup if document is not published | |
| 185 | + * Flushes entire document cache | |
| 176 | 186 | * |
| 177 | - * @param int $documentID | |
| 178 | - * @param array $args | |
| 179 | - * | |
| 180 | - * @return string | |
| 187 | + * @param int $documentID | |
| 181 | 188 | */ |
| 182 | - public function getUnPublishedNoticeMarkup( $documentID, $args = [] ){ | |
| 183 | - if( $document = \Depicter::document()->repository()->findById( $documentID ) ){ | |
| 184 | - $document = $document->toArray(); | |
| 185 | - | |
| 186 | - if ( $document['status'] == 'draft' ) { | |
| 187 | - return \Depicter::view('admin/notices/slider-draft-notice')->with( 'view_args', [ | |
| 188 | - 'isNotPublished' => true, | |
| 189 | - 'editUrl' => \Depicter::editor()->getEditUrl( $documentID ) | |
| 190 | - ])->toString(); | |
| 191 | - } | |
| 192 | - } | |
| 193 | - | |
| 194 | - return ''; | |
| 189 | + public function flushDocumentCache( $documentID ){ | |
| 190 | + $this->cache->delete( $documentID ."_auto" ); | |
| 191 | + $this->cache->delete( $documentID ."_inline" ); | |
| 192 | + $this->cache->delete( $documentID ."_file" ); | |
| 195 | 193 | } |
| 196 | 194 | |
| 197 | 195 | } |