| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | * |
| 27 | 27 | * @return string|void |
| 28 | 28 | */ |
| 29 | 29 | public function document( $documentID = 0, $args = [] ){ |
| 30 | - $isPrivilegedUser = current_user_can('manage_options') || current_user_can('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,13 +38,19 @@ | ||
| 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; |
| 47 | + $args['loadStyleMode'] = 'inline'; | |
| 45 | 48 | } |
| 46 | 49 | |
| 50 | + // Fallback to load missing asset files of a document | |
| 51 | + \Depicter::front()->assets()->enqueueAssets( $documentID, $isPrivilegedUser ); | |
| 52 | + | |
| 47 | 53 | $output = $this->getDocument( $documentID, $args ); // retrieves escaped slider markup |
| 48 | 54 | |
| 49 | 55 | if( $args['echo'] ){ |
| 50 | 56 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| @@ -65,10 +71,10 @@ | ||
| 65 | 71 | { |
| 66 | 72 | if ( empty( $documentID ) ) { |
| 67 | 73 | return esc_html__( 'Slider ID is required.', 'depicter' ); |
| 68 | 74 | } |
| 69 | - if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID ) ) ){ | |
| 70 | - return $cacheOutput . '<span hidden>Depicter Cache hit!</span>'; | |
| 75 | + if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID, $args ) ) ){ | |
| 76 | + return $cacheOutput; | |
| 71 | 77 | } |
| 72 | 78 | |
| 73 | 79 | $output = ''; |
| 74 | 80 | $where = [ 'status' => $args['status'] ]; |
| @@ -74,62 +80,57 @@ | ||
| 74 | 80 | $where = [ 'status' => $args['status'] ]; |
| 75 | 81 | $styleGeneratorArgs = isset( $args['addImportant'] ) ? [ 'addImportant' => $args['addImportant'] ] : []; |
| 76 | 82 | |
| 77 | 83 | try{ |
| 78 | - if ( ! is_numeric( $documentID ) && is_string( $documentID ) ) { | |
| 79 | - if ( ! $document = \Depicter::document()->repository()->findOne( null, ['slug' => $documentID] ) ) { | |
| 80 | - return esc_html__( 'Slider alias not found.', 'depicter' ); | |
| 81 | - } | |
| 82 | - $documentID = $document->getID(); | |
| 83 | - } | |
| 84 | + if( ! $documentID = \Depicter::document()->getID( $documentID ) ){ | |
| 85 | + return esc_html__( 'Slider alias not found.', 'depicter' ); | |
| 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 | |
| 99 | + if( $args['isPrivilegedUser'] && \Depicter::document()->repository()->isPublished( $documentID ) ){ | |
| 100 | + $documentModel->saveCss(); | |
| 101 | + } | |
| 102 | + | |
| 96 | 103 | if( ( $args['loadStyleMode'] == 'inline' ) ){ |
| 97 | 104 | $output = $documentModel->getInlineCssTag() . $output; |
| 98 | 105 | |
| 99 | - } elseif( ( $args['loadStyleMode'] == 'auto' ) ) { | |
| 100 | - // fallback to inline if css file cannot be generated | |
| 106 | + // fallback to inline if css file cannot be generated | |
| 107 | + } elseif( in_array( $args['loadStyleMode'], [ 'auto', 'file' ] ) ) { | |
| 101 | 108 | if( ! $documentModel->getCssFileUrl() ){ |
| 102 | 109 | $output = $documentModel->getInlineCssTag() . $output; |
| 103 | 110 | } |
| 104 | 111 | } |
| 105 | 112 | |
| 113 | + $output = $documentModel->getBeforeInitCssAndTag() . $output; | |
| 114 | + | |
| 106 | 115 | //---------- |
| 107 | 116 | |
| 108 | - $cssLinksToEnqueue = $documentModel->getCustomCssFiles( [ 'google-font' ] ); | |
| 117 | + $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' ); | |
| 109 | 118 | |
| 110 | - if( ( $args['loadStyleMode'] == 'auto' ) ) { | |
| 111 | - if( $documentModel->getCssFileUrl() ){ | |
| 112 | - $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' ); | |
| 113 | - } | |
| 119 | + $this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS ); | |
| 114 | 120 | |
| 115 | - } elseif( $args['loadStyleMode'] == 'file' ) { | |
| 116 | - $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' ); | |
| 121 | + if( $firstSection = $documentModel->getSectionNth(1) ){ | |
| 122 | + $preloadTags = $firstSection->getPreloadTags(); | |
| 123 | + $this->cache->set( $documentID . '_preload_tags', $preloadTags, WEEK_IN_SECONDS ); | |
| 117 | 124 | } |
| 118 | 125 | |
| 119 | - if( $args['useCache'] ){ | |
| 120 | - $this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS ); | |
| 121 | - } | |
| 122 | - | |
| 123 | - $this->enqueueCustomStyles( $cssLinksToEnqueue ); | |
| 124 | - | |
| 125 | 126 | // sanitize the output before caching |
| 126 | 127 | $output = Sanitize::html( $output, null, 'depicter/output' ); |
| 127 | 128 | |
| 128 | 129 | if( $args['useCache'] ){ |
| 129 | - $this->cache->set( $documentID, $output, WEEK_IN_SECONDS ); | |
| 130 | + $this->setDocumentCache( $documentID, $output, $args ); | |
| 130 | 131 | } else { |
| 131 | - $this->cache->delete( $documentID ); | |
| 132 | + $this->deleteDocumentCache( $documentID, $args ); | |
| 132 | 133 | } |
| 133 | 134 | } |
| 134 | 135 | |
| 135 | 136 | } catch( \Exception $exception ){ |
| @@ -142,50 +143,53 @@ | ||
| 142 | 143 | |
| 143 | 144 | /** |
| 144 | 145 | * Retrieves the cached markup and enqueues custom styles |
| 145 | 146 | * |
| 146 | - * @param $documentId | |
| 147 | + * @param int $documentID | |
| 148 | + * @param array $args | |
| 147 | 149 | * |
| 148 | 150 | * @return bool|mixed |
| 149 | 151 | */ |
| 150 | - protected function getDocumentCache( $documentId ){ | |
| 151 | - if( ( false !== $cacheOutput = $this->cache->get( $documentId ) ) && !empty( $cacheOutput ) ){ | |
| 152 | - if( false !== $cssLinksToEnqueue = $this->cache->get( $documentId . '_css_files' ) ){ | |
| 153 | - $this->enqueueCustomStyles( $cssLinksToEnqueue ); | |
| 154 | - } | |
| 152 | + protected function getDocumentCache( $documentID, $args = [] ){ | |
| 153 | + if( ( false !== $cacheOutput = $this->cache->get( $documentID ."_". $args['loadStyleMode'] ) ) && !empty( $cacheOutput ) ){ | |
| 155 | 154 | return $cacheOutput; |
| 156 | 155 | } |
| 157 | - | |
| 158 | 156 | return false; |
| 159 | 157 | } |
| 160 | 158 | |
| 161 | 159 | /** |
| 162 | - * Enqueues css links in footer | |
| 160 | + * Cache markup and styles | |
| 163 | 161 | * |
| 164 | - * @param $cssLinksToEnqueue | |
| 162 | + * @param int $documentID | |
| 163 | + * @param string $content | |
| 164 | + * @param array $args | |
| 165 | + * | |
| 166 | + * @return bool | |
| 165 | 167 | */ |
| 166 | - protected function enqueueCustomStyles( $cssLinksToEnqueue ){ | |
| 167 | - if( $cssLinksToEnqueue && is_array( $cssLinksToEnqueue ) ){ | |
| 168 | - add_action( 'wp_footer', function() use ( $cssLinksToEnqueue ){ | |
| 169 | - foreach( $cssLinksToEnqueue as $cssId => $cssLink ){ | |
| 170 | - \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink ); | |
| 171 | - } | |
| 172 | - } ); | |
| 173 | - } | |
| 168 | + protected function setDocumentCache( $documentID, $content, $args = [] ){ | |
| 169 | + return $this->cache->set( $documentID ."_". $args['loadStyleMode'], $content, WEEK_IN_SECONDS ); | |
| 174 | 170 | } |
| 175 | 171 | |
| 176 | - public function getUnPublishedNoticeMarkup( $documentID, $args = [] ){ | |
| 177 | - if( $document = \Depicter::document()->repository()->findById( $documentID ) ){ | |
| 178 | - $document = $document->toArray(); | |
| 172 | + /** | |
| 173 | + * Flushes a document cache | |
| 174 | + * | |
| 175 | + * @param int $documentID | |
| 176 | + * @param array $args | |
| 177 | + * | |
| 178 | + * @return bool | |
| 179 | + */ | |
| 180 | + protected function deleteDocumentCache( $documentID, $args = [] ){ | |
| 181 | + return $this->cache->delete( $documentID ."_". $args['loadStyleMode'] ); | |
| 182 | + } | |
| 179 | 183 | |
| 180 | - if ( $document['status'] == 'draft' ) { | |
| 181 | - return \Depicter::view('admin/notices/slider-draft-notice')->with( 'view_args', [ | |
| 182 | - 'isNotPublished' => true, | |
| 183 | - 'editUrl' => \Depicter::editor()->getEditUrl( $documentID ) | |
| 184 | - ])->toString(); | |
| 185 | - } | |
| 186 | - } | |
| 187 | - | |
| 188 | - return ''; | |
| 184 | + /** | |
| 185 | + * Flushes entire document cache | |
| 186 | + * | |
| 187 | + * @param int $documentID | |
| 188 | + */ | |
| 189 | + public function flushDocumentCache( $documentID ){ | |
| 190 | + $this->cache->delete( $documentID ."_auto" ); | |
| 191 | + $this->cache->delete( $documentID ."_inline" ); | |
| 192 | + $this->cache->delete( $documentID ."_file" ); | |
| 189 | 193 | } |
| 190 | 194 | |
| 191 | 195 | } |