| 1 |
<?php |
| 2 |
namespace Depicter\Front; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\Core\Utility\Arr; |
| 6 |
use Averta\WordPress\Cache\WPCache; |
| 7 |
use Averta\WordPress\Utility\JSON; |
| 8 |
use Depicter\Exception\DocumentNotPublished; |
| 9 |
use Depicter\Utility\Sanitize; |
| 10 |
|
| 11 |
class Render |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var WPCache |
| 15 |
*/ |
| 16 |
private $cache; |
| 17 |
|
| 18 |
public function __construct(){ |
| 19 |
$this->cache = \Depicter::cache('document'); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Retrieves or renders a document markup |
| 24 |
* |
| 25 |
* @param int|string $documentID Document ID or alias |
| 26 |
* @param array $args Retrieve params |
| 27 |
* |
| 28 |
* @return string|void |
| 29 |
*/ |
| 30 |
public function document( $documentID = 0, $args = [] ){ |
| 31 |
$isPrivilegedUser = \Depicter::authorization()->currentUserCanPublishDocument(); |
| 32 |
|
| 33 |
$args = Arr::merge( $args, [ |
| 34 |
'loadStyleMode' => 'auto',// ["auto", "inline", "file"]."auto" loads custom css if available, otherwise prints styles inline |
| 35 |
'useCache' => true, |
| 36 |
'echo' => true, |
| 37 |
'status' => 'publish', |
| 38 |
'showAdminNotice' => $isPrivilegedUser |
| 39 |
]); |
| 40 |
|
| 41 |
if ( $args['isPrivilegedUser'] = $isPrivilegedUser ) { |
| 42 |
$args['status'] = ['publish', 'draft', 'future']; |
| 43 |
$args['useCache'] = false; |
| 44 |
$args['loadStyleMode'] = 'inline'; |
| 45 |
} |
| 46 |
|
| 47 |
$output = $this->getDocument( $documentID, $args ); // retrieves escaped slider markup |
| 48 |
|
| 49 |
if ( !empty( $output ) ) { |
| 50 |
// Fallback to load missing asset files of a document |
| 51 |
\Depicter::front()->assets()->enqueueAssets( $documentID, $isPrivilegedUser ); |
| 52 |
} |
| 53 |
|
| 54 |
if( $args['echo'] ){ |
| 55 |
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 56 |
} else { |
| 57 |
return $output; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Retrieves a document markup |
| 63 |
* |
| 64 |
* @param int|string $documentID Document ID or alias |
| 65 |
* @param array $args Retrieve params |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
protected function getDocument( $documentID = '', $args = [] ) |
| 70 |
{ |
| 71 |
if ( empty( $documentID ) ) { |
| 72 |
return esc_html__( 'Slider ID is required.', 'depicter' ); |
| 73 |
} |
| 74 |
|
| 75 |
if( ! \Depicter::document()->displayRules( $documentID )->isVisible( $args ) ){ |
| 76 |
return ''; |
| 77 |
} |
| 78 |
|
| 79 |
if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID, $args ) ) ){ |
| 80 |
return $cacheOutput; |
| 81 |
} |
| 82 |
|
| 83 |
$output = ''; |
| 84 |
$where = [ 'status' => $args['status'] ]; |
| 85 |
|
| 86 |
$styleGeneratorArgs = isset( $args['addImportant'] ) ? [ 'addImportant' => $args['addImportant'] ] : []; |
| 87 |
|
| 88 |
try{ |
| 89 |
if( ! $documentID = \Depicter::document()->getID( $documentID ) ){ |
| 90 |
return esc_html__( 'Slider alias not found.', 'depicter' ); |
| 91 |
} |
| 92 |
|
| 93 |
if( ! $args['showAdminNotice'] && ! \Depicter::document()->repository()->isPublishedBefore( $documentID ) ) { |
| 94 |
throw new DocumentNotPublished( __( 'Slider is not published yet and saved as "draft"', 'depicter' ), 0, $where ); |
| 95 |
} |
| 96 |
|
| 97 |
if( $documentModel = \Depicter::document()->getModel( $documentID, $where ) ){ |
| 98 |
|
| 99 |
$documentModel->setShowAdminNotice( $args['showAdminNotice'] ); |
| 100 |
|
| 101 |
$output .= $documentModel->prepare()->render(); |
| 102 |
|
| 103 |
$documentModel->styleGenerator( $styleGeneratorArgs ); |
| 104 |
|
| 105 |
if( $args['isPrivilegedUser'] && \Depicter::document()->repository()->isPublished( $documentID ) ){ |
| 106 |
$documentModel->saveCss(); |
| 107 |
} |
| 108 |
|
| 109 |
if( ( $args['loadStyleMode'] == 'inline' ) ){ |
| 110 |
$output = $documentModel->getInlineCssTag() . $output; |
| 111 |
|
| 112 |
// fallback to inline if css file cannot be generated |
| 113 |
} elseif( in_array( $args['loadStyleMode'], [ 'auto', 'file' ] ) ) { |
| 114 |
if( ! $documentModel->getCssFileUrl() ){ |
| 115 |
$output = $documentModel->getInlineCssTag() . $output; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$output = $documentModel->getBeforeInitCssAndTag() . $output; |
| 120 |
|
| 121 |
//---------- |
| 122 |
|
| 123 |
$cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' ); |
| 124 |
|
| 125 |
$this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS ); |
| 126 |
|
| 127 |
if( $firstSection = $documentModel->getSectionNth(1) ){ |
| 128 |
$preloadTags = $firstSection->getPreloadTags(); |
| 129 |
$this->cache->set( $documentID . '_preload_tags', $preloadTags, WEEK_IN_SECONDS ); |
| 130 |
} |
| 131 |
|
| 132 |
// sanitize the output before caching |
| 133 |
$output = Sanitize::html( $output, null, 'depicter/output' ); |
| 134 |
|
| 135 |
if( $args['useCache'] ){ |
| 136 |
$this->setDocumentCache( $documentID, $output, $args ); |
| 137 |
} else { |
| 138 |
$this->deleteDocumentCache( $documentID, $args ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} catch( \Exception $exception ){ |
| 143 |
$output = Sanitize::html( $exception->getMessage() ); |
| 144 |
} |
| 145 |
|
| 146 |
return $output; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Retrieves the cached markup and enqueues custom styles |
| 151 |
* |
| 152 |
* @param int $documentID |
| 153 |
* @param array $args |
| 154 |
* |
| 155 |
* @return bool|mixed |
| 156 |
*/ |
| 157 |
protected function getDocumentCache( $documentID, $args = [] ){ |
| 158 |
if( ( false !== $cacheOutput = $this->cache->get( $this->getDocumentCacheKey( $documentID, $args ) ) ) && !empty( $cacheOutput ) ){ |
| 159 |
return $cacheOutput; |
| 160 |
} |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Cache markup and styles |
| 166 |
* |
| 167 |
* @param int $documentID |
| 168 |
* @param string $content |
| 169 |
* @param array $args |
| 170 |
* |
| 171 |
* @return bool |
| 172 |
*/ |
| 173 |
protected function setDocumentCache( $documentID, $content, $args = [] ): bool{ |
| 174 |
return $this->cache->set( $this->getDocumentCacheKey( $documentID, $args ), $content, 4 * DAY_IN_SECONDS ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Flushes a document cache |
| 179 |
* |
| 180 |
* @param int $documentID |
| 181 |
* @param array $args |
| 182 |
* |
| 183 |
* @return bool |
| 184 |
*/ |
| 185 |
protected function deleteDocumentCache( $documentID, $args = [] ){ |
| 186 |
return $this->cache->delete( $this->getDocumentCacheKey( $documentID, $args ) ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Flushes entire document cache |
| 191 |
* |
| 192 |
* @param int $documentID |
| 193 |
*/ |
| 194 |
public function flushDocumentCache( $documentID ){ |
| 195 |
$this->cache->delete( $documentID ."_auto" ); |
| 196 |
$this->cache->delete( $documentID ."_inline" ); |
| 197 |
$this->cache->delete( $documentID ."_css_files" ); |
| 198 |
} |
| 199 |
|
| 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 |
} |
| 216 |
|