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