PluginProbe
Depicter — Popup & Slider Builder / 1.3.0
Depicter — Popup & Slider Builder v1.3.0
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.0, at app/src/Front/Render.php

198 lines 5.3 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()->currentUserCan( [ 'manage_options', 'publish_depicter' ] );
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 if ( $isPrivilegedUser ) {
43 $args['status'] = ['publish', 'draft'];
44 $args['useCache'] = false;
45 $args['loadStyleMode'] = 'inline';
46 }
47
48 $args['isPrivilegedUser'] = $isPrivilegedUser;
49
50 $output = $this->getDocument( $documentID, $args ); // retrieves escaped slider markup
51
52 if( $args['echo'] ){
53 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
54 } else {
55 return $output;
56 }
57 }
58
59 /**
60 * Retrieves a document markup
61 *
62 * @param int|string $documentID Document ID or alias
63 * @param array $args Retrieve params
64 *
65 * @return string
66 */
67 protected function getDocument( $documentID = '', $args = [] )
68 {
69 if ( empty( $documentID ) ) {
70 return esc_html__( 'Slider ID is required.', 'depicter' );
71 }
72 if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID, $args ) ) ){
73 return $cacheOutput . '<span hidden>Depicter cache hit.</span>';
74 }
75
76 $output = '';
77 $where = [ 'status' => $args['status'] ];
78 $styleGeneratorArgs = isset( $args['addImportant'] ) ? [ 'addImportant' => $args['addImportant'] ] : [];
79
80 try{
81 if( ! $documentID = \Depicter::document()->getID( $documentID ) ){
82 return esc_html__( 'Slider alias not found.', 'depicter' );
83 }
84
85 if( $args['showUnpublishedNotice'] ){
86 $output .= $this->getUnPublishedNoticeMarkup( $documentID );
87 } elseif( \Depicter::document()->repository()->isNotPublished( $documentID, $where ) ) {
88 throw new DocumentNotPublished( __( 'Slider is not published yet and saved as "draft"', 'depicter' ), 0, $where );
89 }
90
91 if( $documentModel = \Depicter::document()->getModel( $documentID, $where ) ){
92
93 $output .= $documentModel->prepare()->render();
94 $documentModel->styleGenerator( $styleGeneratorArgs );
95
96 if( $args['isPrivilegedUser'] ){
97 $documentModel->saveCss();
98 }
99
100 if( ( $args['loadStyleMode'] == 'inline' ) ){
101 $output = $documentModel->getInlineCssTag() . $output;
102
103 // fallback to inline if css file cannot be generated
104 } elseif( in_array( $args['loadStyleMode'], [ 'auto', 'file' ] ) ) {
105 if( ! $documentModel->getCssFileUrl() ){
106 $output = $documentModel->getInlineCssTag() . $output;
107 }
108 }
109
110 //----------
111
112 $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' );
113
114 $this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS );
115
116 // sanitize the output before caching
117 $output = Sanitize::html( $output, null, 'depicter/output' );
118
119 if( $args['useCache'] ){
120 $this->setDocumentCache( $documentID, $output, $args );
121 } else {
122 $this->deleteDocumentCache( $documentID, $args );
123 }
124 }
125
126 } catch( \Exception $exception ){
127 $output = Sanitize::html( $exception->getMessage() );
128 }
129
130 return $output;
131 }
132
133
134 /**
135 * Retrieves the cached markup and enqueues custom styles
136 *
137 * @param int $documentID
138 * @param array $args
139 *
140 * @return bool|mixed
141 */
142 protected function getDocumentCache( $documentID, $args = [] ){
143 if( ( false !== $cacheOutput = $this->cache->get( $documentID ."_". $args['loadStyleMode'] ) ) && !empty( $cacheOutput ) ){
144 return $cacheOutput;
145 }
146 return false;
147 }
148
149 /**
150 * Cache markup and styles
151 *
152 * @param int $documentID
153 * @param string $content
154 * @param array $args
155 *
156 * @return bool
157 */
158 protected function setDocumentCache( $documentID, $content, $args = [] ){
159 return $this->cache->set( $documentID ."_". $args['loadStyleMode'], $content, WEEK_IN_SECONDS );
160 }
161
162 /**
163 * Flushes a document cache
164 *
165 * @param int $documentID
166 * @param array $args
167 *
168 * @return bool
169 */
170 protected function deleteDocumentCache( $documentID, $args = [] ){
171 return $this->cache->delete( $documentID ."_". $args['loadStyleMode'] );
172 }
173
174 /**
175 * Get unpublished notice markup if document is not published
176 *
177 * @param int $documentID
178 * @param array $args
179 *
180 * @return string
181 */
182 public function getUnPublishedNoticeMarkup( $documentID, $args = [] ){
183 if( $document = \Depicter::document()->repository()->findById( $documentID ) ){
184 $document = $document->toArray();
185
186 if ( $document['status'] == 'draft' ) {
187 return \Depicter::view('admin/notices/slider-draft-notice')->with( 'view_args', [
188 'isNotPublished' => true,
189 'editUrl' => \Depicter::editor()->getEditUrl( $documentID )
190 ])->toString();
191 }
192 }
193
194 return '';
195 }
196
197 }
198