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

192 lines 5.4 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 = current_user_can('manage_options') || current_user_can('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 }
46
47 $output = $this->getDocument( $documentID, $args ); // retrieves escaped slider markup
48
49 if( $args['echo'] ){
50 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
51 } else {
52 return $output;
53 }
54 }
55
56 /**
57 * Retrieves a document markup
58 *
59 * @param int|string $documentID Document ID or alias
60 * @param array $args Retrieve params
61 *
62 * @return string
63 */
64 protected function getDocument( $documentID = '', $args = [] )
65 {
66 if ( empty( $documentID ) ) {
67 return esc_html__( 'Slider ID is required.', 'depicter' );
68 }
69 if( $args['useCache'] && ( false !== $cacheOutput = $this->getDocumentCache( $documentID ) ) ){
70 return $cacheOutput . '<span hidden>Depicter Cache hit!</span>';
71 }
72
73 $output = '';
74 $where = [ 'status' => $args['status'] ];
75 $styleGeneratorArgs = isset( $args['addImportant'] ) ? [ 'addImportant' => $args['addImportant'] ] : [];
76
77 try{
78 if ( ! is_numeric( $documentID ) && is_string( $documentID ) ) {
79 if ( ! $document = \Depicter::document()->repository()->findOne( null, ['slug' => $documentID] ) ) {
80 return esc_html__( 'Slider alias not found.', 'depicter' );
81 }
82 $documentID = $document->getID();
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['loadStyleMode'] == 'inline' ) ){
97 $output = $documentModel->getInlineCssTag() . $output;
98
99 } elseif( ( $args['loadStyleMode'] == 'auto' ) ) {
100 // fallback to inline if css file cannot be generated
101 if( ! $documentModel->getCssFileUrl() ){
102 $output = $documentModel->getInlineCssTag() . $output;
103 }
104 }
105
106 //----------
107
108 $cssLinksToEnqueue = $documentModel->getCustomCssFiles( [ 'google-font' ] );
109
110 if( ( $args['loadStyleMode'] == 'auto' ) ) {
111 if( $documentModel->getCssFileUrl() ){
112 $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' );
113 }
114
115 } elseif( $args['loadStyleMode'] == 'file' ) {
116 $cssLinksToEnqueue = $documentModel->getCustomCssFiles( 'all' );
117 }
118
119 if( $args['useCache'] ){
120 $this->cache->set( $documentID . '_css_files', $cssLinksToEnqueue, WEEK_IN_SECONDS );
121 }
122
123 $this->enqueueCustomStyles( $cssLinksToEnqueue );
124
125 // sanitize the output before caching
126 $output = Sanitize::html( $output, null, 'depicter/output' );
127
128 if( $args['useCache'] ){
129 $this->cache->set( $documentID, $output, WEEK_IN_SECONDS );
130 } else {
131 $this->cache->delete( $documentID );
132 }
133 }
134
135 } catch( \Exception $exception ){
136 $output = Sanitize::html( $exception->getMessage() );
137 }
138
139 return $output;
140 }
141
142
143 /**
144 * Retrieves the cached markup and enqueues custom styles
145 *
146 * @param $documentId
147 *
148 * @return bool|mixed
149 */
150 protected function getDocumentCache( $documentId ){
151 if( ( false !== $cacheOutput = $this->cache->get( $documentId ) ) && !empty( $cacheOutput ) ){
152 if( false !== $cssLinksToEnqueue = $this->cache->get( $documentId . '_css_files' ) ){
153 $this->enqueueCustomStyles( $cssLinksToEnqueue );
154 }
155 return $cacheOutput;
156 }
157
158 return false;
159 }
160
161 /**
162 * Enqueues css links in footer
163 *
164 * @param $cssLinksToEnqueue
165 */
166 protected function enqueueCustomStyles( $cssLinksToEnqueue ){
167 if( $cssLinksToEnqueue && is_array( $cssLinksToEnqueue ) ){
168 add_action( 'wp_footer', function() use ( $cssLinksToEnqueue ){
169 foreach( $cssLinksToEnqueue as $cssId => $cssLink ){
170 \Depicter::core()->assets()->enqueueStyle( $cssId, $cssLink );
171 }
172 } );
173 }
174 }
175
176 public function getUnPublishedNoticeMarkup( $documentID, $args = [] ){
177 if( $document = \Depicter::document()->repository()->findById( $documentID ) ){
178 $document = $document->toArray();
179
180 if ( $document['status'] == 'draft' ) {
181 return \Depicter::view('admin/notices/slider-draft-notice')->with( 'view_args', [
182 'isNotPublished' => true,
183 'editUrl' => \Depicter::editor()->getEditUrl( $documentID )
184 ])->toString();
185 }
186 }
187
188 return '';
189 }
190
191 }
192