PluginProbe
Depicter — Popup & Slider Builder / 1.3.3
Depicter — Popup & Slider Builder v1.3.3
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Front / Render.php

Render.php in Depicter — Popup & Slider Builder 1.3.3, at app/src/Front/Render.php

212 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 . '<span hidden>Depicter cache hit.</span>';
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'] ){
89 $output .= $this->getUnPublishedNoticeMarkup( $documentID );
90 } elseif( \Depicter::document()->repository()->isNotPublished( $documentID, $where ) ) {
91 throw new DocumentNotPublished( __( 'Slider is not published yet and saved as "draft"', 'depicter' ), 0, $where );
92 }
93
94 if( $documentModel = \Depicter::document()->getModel( $documentID, $where ) ){
95
96 $output .= $documentModel->prepare()->render();
97 $documentModel->styleGenerator( $styleGeneratorArgs );
98
99 if( $args['isPrivilegedUser'] && \Depicter::document()->getStatus( $documentID ) == 'publish' ){
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 //----------
114
115 $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' );
116
117 $this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS );
118
119 // sanitize the output before caching
120 $output = Sanitize::html( $output, null, 'depicter/output' );
121
122 if( $args['useCache'] ){
123 $this->setDocumentCache( $documentID, $output, $args );
124 } else {
125 $this->deleteDocumentCache( $documentID, $args );
126 }
127 }
128
129 } catch( \Exception $exception ){
130 $output = Sanitize::html( $exception->getMessage() );
131 }
132
133 return $output;
134 }
135
136
137 /**
138 * Retrieves the cached markup and enqueues custom styles
139 *
140 * @param int $documentID
141 * @param array $args
142 *
143 * @return bool|mixed
144 */
145 protected function getDocumentCache( $documentID, $args = [] ){
146 if( ( false !== $cacheOutput = $this->cache->get( $documentID ."_". $args['loadStyleMode'] ) ) && !empty( $cacheOutput ) ){
147 return $cacheOutput;
148 }
149 return false;
150 }
151
152 /**
153 * Cache markup and styles
154 *
155 * @param int $documentID
156 * @param string $content
157 * @param array $args
158 *
159 * @return bool
160 */
161 protected function setDocumentCache( $documentID, $content, $args = [] ){
162 return $this->cache->set( $documentID ."_". $args['loadStyleMode'], $content, WEEK_IN_SECONDS );
163 }
164
165 /**
166 * Flushes a document cache
167 *
168 * @param int $documentID
169 * @param array $args
170 *
171 * @return bool
172 */
173 protected function deleteDocumentCache( $documentID, $args = [] ){
174 return $this->cache->delete( $documentID ."_". $args['loadStyleMode'] );
175 }
176
177 /**
178 * Flushes entire document cache
179 *
180 * @param int $documentID
181 */
182 public function flushDocumentCache( $documentID ){
183 $this->cache->delete( $documentID ."_auto" );
184 $this->cache->delete( $documentID ."_inline" );
185 $this->cache->delete( $documentID ."_file" );
186 }
187
188 /**
189 * Get unpublished notice markup if document is not published
190 *
191 * @param int $documentID
192 * @param array $args
193 *
194 * @return string
195 */
196 public function getUnPublishedNoticeMarkup( $documentID, $args = [] ){
197 if( $document = \Depicter::document()->repository()->findById( $documentID ) ){
198 $document = $document->toArray();
199
200 if ( $document['status'] == 'draft' ) {
201 return \Depicter::view('admin/notices/slider-draft-notice')->with( 'view_args', [
202 'isNotPublished' => true,
203 'editUrl' => \Depicter::editor()->getEditUrl( $documentID )
204 ])->toString();
205 }
206 }
207
208 return '';
209 }
210
211 }
212