| @@ -2,10 +2,9 @@ | ||
| 2 | 2 | namespace Depicter\Front; |
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | use Averta\Core\Utility\Arr; |
| 6 | -use Averta\WordPress\Cache\WPCache; | |
| 7 | -use Averta\WordPress\Utility\JSON; | |
| 6 | +use Averta\WordPress\Models\WPOptions; | |
| 8 | 7 | use Depicter\Exception\DocumentNotPublished; |
| 9 | 8 | use Depicter\Utility\Sanitize; |
| 10 | 9 | |
| 11 | 10 | class Render |
| @@ -10,9 +9,9 @@ | ||
| 10 | 9 | |
| 11 | 10 | class Render |
| 12 | 11 | { |
| 13 | 12 | /** |
| 14 | - * @var WPCache | |
| 13 | + * @var WPOptions | |
| 15 | 14 | */ |
| 16 | 15 | private $cache; |
| 17 | 16 | |
| 18 | 17 | public function __construct(){ |
| @@ -29,29 +28,31 @@ | ||
| 29 | 28 | */ |
| 30 | 29 | public function document( $documentID = 0, $args = [] ){ |
| 31 | 30 | $isPrivilegedUser = \Depicter::authorization()->currentUserCanPublishDocument(); |
| 32 | 31 | |
| 33 | - $args = Arr::merge( $args, [ | |
| 32 | + $defaults = [ | |
| 34 | 33 | 'loadStyleMode' => 'auto',// ["auto", "inline", "file"]."auto" loads custom css if available, otherwise prints styles inline |
| 35 | 34 | 'useCache' => true, |
| 36 | 35 | 'echo' => true, |
| 37 | 36 | 'status' => 'publish', |
| 38 | - 'showAdminNotice' => $isPrivilegedUser | |
| 39 | - ]); | |
| 37 | + 'showUnpublishedNotice' => $isPrivilegedUser | |
| 38 | + ]; | |
| 40 | 39 | |
| 41 | - if ( $args['isPrivilegedUser'] = $isPrivilegedUser ) { | |
| 42 | - $args['status'] = ['publish', 'draft', 'future']; | |
| 43 | - $args['useCache'] = false; | |
| 40 | + $args = Arr::merge( $args, $defaults ); | |
| 41 | + | |
| 42 | + $args['isPrivilegedUser'] = $isPrivilegedUser; | |
| 43 | + | |
| 44 | + if ( $isPrivilegedUser ) { | |
| 45 | + $args['status'] = ['publish', 'draft']; | |
| 46 | + $args['useCache'] = false; | |
| 44 | 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 | - if ( !empty( $output ) ) { | |
| 50 | - // Fallback to load missing asset files of a document | |
| 51 | - \Depicter::front()->assets()->enqueueAssets( $documentID, $isPrivilegedUser ); | |
| 52 | - } | |
| 53 | - | |
| 54 | 55 | if( $args['echo'] ){ |
| 55 | 56 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 56 | 57 | } else { |
| 57 | 58 | return $output; |
| @@ -70,13 +71,8 @@ | ||
| 70 | 71 | { |
| 71 | 72 | if ( empty( $documentID ) ) { |
| 72 | 73 | return esc_html__( 'Slider ID is required.', 'depicter' ); |
| 73 | 74 | } |
| 74 | - | |
| 75 | - if( ! \Depicter::document()->displayRules( $documentID )->isVisible( $args ) ){ | |
| 76 | - return ''; | |
| 77 | - } | |
| 78 | - | |
| 79 | 75 | if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID, $args ) ) ){ |
| 80 | 76 | return $cacheOutput; |
| 81 | 77 | } |
| 82 | 78 | |
| @@ -81,9 +77,8 @@ | ||
| 81 | 77 | } |
| 82 | 78 | |
| 83 | 79 | $output = ''; |
| 84 | 80 | $where = [ 'status' => $args['status'] ]; |
| 85 | - | |
| 86 | 81 | $styleGeneratorArgs = isset( $args['addImportant'] ) ? [ 'addImportant' => $args['addImportant'] ] : []; |
| 87 | 82 | |
| 88 | 83 | try{ |
| 89 | 84 | if( ! $documentID = \Depicter::document()->getID( $documentID ) ){ |
| @@ -89,16 +84,15 @@ | ||
| 89 | 84 | if( ! $documentID = \Depicter::document()->getID( $documentID ) ){ |
| 90 | 85 | return esc_html__( 'Slider alias not found.', 'depicter' ); |
| 91 | 86 | } |
| 92 | 87 | |
| 93 | - if( ! $args['showAdminNotice'] && ! \Depicter::document()->repository()->isPublishedBefore( $documentID ) ) { | |
| 88 | + if( ! $args['showUnpublishedNotice'] && ! \Depicter::document()->repository()->isPublishedBefore( $documentID ) ) { | |
| 94 | 89 | throw new DocumentNotPublished( __( 'Slider is not published yet and saved as "draft"', 'depicter' ), 0, $where ); |
| 95 | 90 | } |
| 96 | 91 | |
| 97 | 92 | if( $documentModel = \Depicter::document()->getModel( $documentID, $where ) ){ |
| 93 | + $documentModel->setUnpublishedNotice( $args['showUnpublishedNotice'] ); | |
| 98 | 94 | |
| 99 | - $documentModel->setShowAdminNotice( $args['showAdminNotice'] ); | |
| 100 | - | |
| 101 | 95 | $output .= $documentModel->prepare()->render(); |
| 102 | 96 | |
| 103 | 97 | $documentModel->styleGenerator( $styleGeneratorArgs ); |
| 104 | 98 | |
| @@ -145,8 +139,9 @@ | ||
| 145 | 139 | |
| 146 | 140 | return $output; |
| 147 | 141 | } |
| 148 | 142 | |
| 143 | + | |
| 149 | 144 | /** |
| 150 | 145 | * Retrieves the cached markup and enqueues custom styles |
| 151 | 146 | * |
| 152 | 147 | * @param int $documentID |
| @@ -154,9 +149,9 @@ | ||
| 154 | 149 | * |
| 155 | 150 | * @return bool|mixed |
| 156 | 151 | */ |
| 157 | 152 | protected function getDocumentCache( $documentID, $args = [] ){ |
| 158 | - if( ( false !== $cacheOutput = $this->cache->get( $this->getDocumentCacheKey( $documentID, $args ) ) ) && !empty( $cacheOutput ) ){ | |
| 153 | + if( ( false !== $cacheOutput = $this->cache->get( $documentID ."_". $args['loadStyleMode'] ) ) && !empty( $cacheOutput ) ){ | |
| 159 | 154 | return $cacheOutput; |
| 160 | 155 | } |
| 161 | 156 | return false; |
| 162 | 157 | } |
| @@ -169,10 +164,10 @@ | ||
| 169 | 164 | * @param array $args |
| 170 | 165 | * |
| 171 | 166 | * @return bool |
| 172 | 167 | */ |
| 173 | - protected function setDocumentCache( $documentID, $content, $args = [] ): bool{ | |
| 174 | - return $this->cache->set( $this->getDocumentCacheKey( $documentID, $args ), $content, 4 * DAY_IN_SECONDS ); | |
| 168 | + protected function setDocumentCache( $documentID, $content, $args = [] ){ | |
| 169 | + return $this->cache->set( $documentID ."_". $args['loadStyleMode'], $content, WEEK_IN_SECONDS ); | |
| 175 | 170 | } |
| 176 | 171 | |
| 177 | 172 | /** |
| 178 | 173 | * Flushes a document cache |
| @@ -182,9 +177,9 @@ | ||
| 182 | 177 | * |
| 183 | 178 | * @return bool |
| 184 | 179 | */ |
| 185 | 180 | protected function deleteDocumentCache( $documentID, $args = [] ){ |
| 186 | - return $this->cache->delete( $this->getDocumentCacheKey( $documentID, $args ) ); | |
| 181 | + return $this->cache->delete( $documentID ."_". $args['loadStyleMode'] ); | |
| 187 | 182 | } |
| 188 | 183 | |
| 189 | 184 | /** |
| 190 | 185 | * Flushes entire document cache |
| @@ -193,23 +188,8 @@ | ||
| 193 | 188 | */ |
| 194 | 189 | public function flushDocumentCache( $documentID ){ |
| 195 | 190 | $this->cache->delete( $documentID ."_auto" ); |
| 196 | 191 | $this->cache->delete( $documentID ."_inline" ); |
| 197 | - $this->cache->delete( $documentID ."_css_files" ); | |
| 192 | + $this->cache->delete( $documentID ."_file" ); | |
| 198 | 193 | } |
| 199 | 194 | |
| 200 | - protected function getDocumentCacheKey( $documentID = '', $args = [] ){ | |
| 201 | - // type of loading style | |
| 202 | - $loadStyleMode = $args['loadStyleMode'] ?? 'auto'; | |
| 203 | - // whether addImportant option is enabled for styles or not | |
| 204 | - $addImportant = !empty( $args['addImportant'] ) ? '1' : '0'; | |
| 205 | - // If the cache is for logged-in users or not (visitors) | |
| 206 | - $cacheFor = !empty( $args['isPrivilegedUser'] ) ? 'admin_' : ''; | |
| 207 | - // Include admin notice in document markup or not | |
| 208 | - $isAdminNoticeEnabled = !empty( $args['showAdminNotice'] ) ? 'notice_' : ''; | |
| 209 | - // The last modified date of main document | |
| 210 | - $modifiedAt = \Depicter::documentRepository()->getFieldValue( $documentID, 'modified_at'); | |
| 211 | - $documentModifiedTime = $modifiedAt ? strtotime( $modifiedAt ): '0'; | |
| 212 | - | |
| 213 | - return "{$documentID}_{$loadStyleMode}_{$cacheFor}{$isAdminNoticeEnabled}{$addImportant}{$documentModifiedTime}"; | |
| 214 | - } | |
| 215 | 195 | } |