PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
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 / Document / Helper / Helper.php

Helper.php in Depicter — Popup & Slider Builder trunk, at app/src/Document/Helper/Helper.php

368 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Document\Helper;
3
4
5 use Averta\WordPress\Utility\JSON;
6 use Depicter\Document\CSS\Breakpoints;
7 use Depicter\Document\CSS\Selector;
8
9 class Helper
10 {
11 /**
12 * Generate data-actions value for DOM
13 *
14 * @param array $actions
15 *
16 * @return string
17 */
18 public static function getActions( $actions ) {
19 if ( !is_array( $actions ) || empty( $actions ) ) {
20 return '';
21 }
22
23 $output = [];
24 foreach ( $actions as $key => $action ) {
25 $action = (array) $action;
26
27 $action_params = [];
28 $action_params[] = $action['type'];
29 $action_params[] = $action['trigger'];
30 $action_params[] = (int) ! empty( $action['delay'] ) ? $action['delay'] : 0;
31
32 if( !empty( $action['options'] ) ){
33 if ( $action['type'] == 'customJS' ) {
34 $customJS = $action['options']->value;
35 $action['options']->value = $key;
36 }
37
38 if ( $action['type'] == 'openURL' ) {
39 $action['options']->path = esc_url( $action['options']->path );
40 }
41
42 $action_params[] = clone $action['options'];
43
44 if ( $action['type'] == 'customJS' ) {
45 $action['options']->value = $customJS;
46 }
47
48 }
49
50 $output[] = $action_params;
51 }
52
53 return JSON::encode( $output );
54 }
55
56
57 /**
58 * Extract section ID from slug
59 *
60 * @param string $sectionSlug
61 *
62 * @return string
63 */
64 public static function getSectionIdFromSlug( $sectionSlug = '' ){
65 return ltrim( $sectionSlug, Selector::SECTION_PREFIX . '-' );
66 }
67
68 /**
69 * Extract element ID from slug
70 *
71 * @param string $elementSlug
72 *
73 * @return string
74 */
75 public static function getElementIdFromSlug( $elementSlug = '' ){
76 return ltrim( $elementSlug, Selector::ELEMENT_PREFIX . '-' );
77 }
78
79 public static function getSectionCssId( $documentId, $sectionId ){
80 return Selector::getFullSelectorPath( $documentId, $sectionId );
81 }
82
83 public static function sectionsSlugsListToCssIDsList( $sectionsSlugList, $documentId ){
84
85 if( empty( $sectionsSlugList ) || ! is_array( $sectionsSlugList ) ){
86 return [];
87 }
88
89 $sectionsCssIDsList = [];
90
91 foreach( $sectionsSlugList as $sectionSlug ){
92 $sectionIdNumber = self::getSectionIdFromSlug( $sectionSlug );
93 $sectionsCssIDsList[] = self::getSectionCssId( $documentId, $sectionIdNumber );
94 }
95
96 return $sectionsCssIDsList;
97 }
98
99 /**
100 * extract third party and selfhosted asset IDs from document content
101 * @param $content
102 * @param bool $includeNumericalIds // get selfhosted asset IDs with numerical values
103 *
104 * @return array|void
105 */
106 public static function extractAssetIds( $content, $includeNumericalIds = false) {
107
108 if ( empty( $content ) ) {
109 return;
110 }
111
112 $assetIDs = [];
113
114 $patterns = [
115 '"source":"([^"]*)"',
116 '"src":"([^"]*)"',
117 '"src":{([^}]+)}'
118 ];
119
120 foreach ( $patterns as $pattern ) {
121 preg_match_all( '/' . $pattern . '/', $content, $sources , PREG_SET_ORDER );
122 if ( !empty( $sources ) ) {
123 foreach ( $sources as $source ) {
124 // check if assetID if for third party libraries or not
125 if ($includeNumericalIds && !empty( $source[1] ) && false === strpos( $source[1], 'https' ) && is_numeric($source[1])) {
126 $assetIDs[] = $source[1];
127 } else if ( !empty( $source[1] ) && strpos( $source[1], '@') === 0 && false === strpos( $source[1], 'https' ) ) {
128 $assetIDs[] = $source[1];
129 }
130
131 if ( !empty( $source[1] ) ) {
132 $backgroundPatterns = ':"([^"]*)"';
133 preg_match_all( '/' . $backgroundPatterns . '/', $source[1], $backgroundSources , PREG_SET_ORDER );
134 if ( !empty( $backgroundSources ) ) {
135 foreach ( $backgroundSources as $backgroundSource ) {
136 if ($includeNumericalIds && !empty( $backgroundSource[1] ) && false === strpos( $backgroundSource[1], 'https' ) && is_numeric($backgroundSource[1])) {
137 $assetIDs[] = $backgroundSource[1];
138 } else if ( !empty( $backgroundSource[1] ) && strpos( $backgroundSource[1], '@') === 0 && false === strpos( $backgroundSource[1], 'https' ) ) {
139 $assetIDs[] = $backgroundSource[1];
140 }
141 }
142 }
143 }
144 }
145 }
146 }
147
148 return $assetIDs;
149 }
150
151
152 /**
153 * Get list of class which has specific elementId
154 *
155 * @param $hideOnSections
156 * @param int $documentId
157 * @param array $allSections
158 *
159 * @return string
160 */
161 public static function getVisibleSectionsCssIdList( $hideOnSections, $documentId, $allSections = [] ) {
162
163 if ( ! empty( $hideOnSections ) ) {
164 foreach ( $allSections as $key => $section ) {
165 if ( in_array( $section, $hideOnSections ) ) {
166 unset( $allSections[ $key ] );
167 }
168 }
169 }
170
171 return implode( ',', Helper::sectionsSlugsListToCssIDsList( $allSections, $documentId ) );
172 }
173
174 /**
175 * Get list of class which has specific elementId
176 *
177 * @param $hideOnSections
178 * @param $documentId
179 *
180 * @return string
181 */
182 public static function getInvisibleSectionsCssIdList( $hideOnSections, $documentId ) {
183 if( !empty( $hideOnSections ) ){
184 return implode( ',', Helper::sectionsSlugsListToCssIDsList( $hideOnSections, $documentId ) );
185 }
186 return '';
187 }
188
189 /**
190 * Get parent device value for a css variable
191 *
192 * @param $cssVariableInstance
193 * @param $variable
194 * @param $device
195 * @param $default
196 *
197 * @return mixed
198 */
199 public static function getParentValue( $cssVariableInstance, $variable, $device, $default ) {
200 $parentDevice = Breakpoints::getParentDevice( $device );
201 if ( !empty( $parentDevice ) ) {
202
203 if ( strpos( $variable, 'enable' ) === false ) {
204 return $cssVariableInstance->{$parentDevice}->{$variable} ?? self::getParentValue( $cssVariableInstance, $variable, $parentDevice, $default );
205 } else {
206 return $cssVariableInstance->{$parentDevice}->{$variable} ?? true;
207 }
208
209 }
210
211 return $default;
212 }
213
214 /**
215 * Check if style is enabled for specific device or not
216 *
217 * @param $cssVariableInstance
218 * @param string $device
219 * @param string $enabledParam
220 * @return boolean
221 */
222 public static function isStyleEnabled( $cssVariableInstance, $device, $enableParam = 'enable' ) {
223 if ( $device == 'default' ) {
224 return isset( $cssVariableInstance->default->{$enableParam} ) ? !empty( $cssVariableInstance->default->{$enableParam} ) : !empty( $cssVariableInstance->default ) ;
225 }
226
227 return $cssVariableInstance->{$device}->{$enableParam} ?? self::getParentValue( $cssVariableInstance, $enableParam, $device, true );
228 }
229
230 /**
231 * Check if hover style is enable for specific device
232 *
233 * @param $hoverInstance
234 * @param string $device
235 * @return boolean
236 */
237 public static function isHoverStyleEnabled( $hoverInstance, $device ) {
238 if ( $device == 'default' ) {
239 return !empty( $hoverInstance->enable->{$device} );
240 }
241
242 $parentDevice = Breakpoints::getParentDevice( $device );
243 return $hoverInstance->enable->{$device} ?? self::isHoverStyleEnabled( $hoverInstance, $parentDevice );
244 }
245
246 /**
247 * Get triggers from display rule meta for a document ID
248 *
249 * @param $documentID
250 *
251 * @return array
252 */
253 public static function getDisplayRuleTriggers( $documentID ): array{
254
255 $rule = \Depicter::metaRepository()->get( $documentID, 'rules', '' );
256 $displayRule = JSON::isJson( $rule ) ? JSON::decode( $rule, true ) : [];
257
258 if ( empty( $displayRule['triggers'] ) ) {
259 return [];
260 }
261
262 $triggersList = array_filter( $displayRule['triggers'], function ( $triggerObject ) {
263 return $triggerObject['enable'];
264 });
265
266 return array_values( $triggersList );
267 }
268
269 /**
270 * Get display again properties
271 *
272 * @param $documentID
273 *
274 * @return array
275 */
276 public static function getDisplayAgainProperties( $documentID ) {
277 $rule = \Depicter::metaRepository()->get( $documentID, 'rules', '' );
278 $displayRule = JSON::isJson( $rule ) ? JSON::decode( $rule, true ) : [];
279
280 if ( empty( $displayRule ) || empty( $displayRule['afterClose'] ) ) {
281 return [];
282 }
283
284 if ( $displayRule['afterClose']['displayAgain'] == 'afterPeriod' ) {
285 $periodType = $displayRule['afterClose']['displayAgainPeriodType'];
286 switch( $periodType ) {
287 case 's':
288 $period = $displayRule['afterClose']['displayAgainPeriod'] / 60;
289 break;
290 case 'h':
291 $period = $displayRule['afterClose']['displayAgainPeriod'] * 60;
292 break;
293 case 'd':
294 $period = $displayRule['afterClose']['displayAgainPeriod'] * 24 * 60;
295 break;
296 default:
297 // minutes
298 $period = $displayRule['afterClose']['displayAgainPeriod'];
299 break;
300 }
301
302 return [
303 'displayAgain' => 'afterPeriod',
304 'displayAgainPeriod' => $period
305 ];
306 }
307
308 return [
309 'displayAgain' => $displayRule['afterClose']['displayAgain']
310 ];
311 }
312
313 /**
314 * change a string from camelCase style to hyphenated style
315 * @param $inputString
316 *
317 * @return string
318 */
319 public static function camelCaseToHyphenated( $inputString ): string
320 {
321 $inputString = strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $inputString));
322 // Some times the second part of original hyphenated text that converted to camel case starts with numbber so when converting it back the hyphen character missed and we need to add it again here
323 if (strpos($inputString, 'depicter') !== false && strpos($inputString, 'depicter-') === false ) {
324 $inputString = str_replace('depicter', 'depicter-', $inputString);
325 }
326
327 return $inputString;
328 }
329
330 /**
331 * change a string from camelCase style to snake case style
332 * @param $inputString
333 *
334 * @return string
335 */
336 public static function camelCaseToSnakeCase( $inputString ): string
337 {
338 $inputString = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $inputString));
339 // Some times the second part of original hyphenated text that converted to camel case starts with numbber so when converting it back the underline character missed and we need to add it again here
340 if (strpos($inputString, 'depicter') !== false && strpos($inputString, 'depicter_') === false ) {
341 $inputString = str_replace('depicter', 'depicter_', $inputString);
342 }
343
344 return $inputString;
345 }
346
347 /**
348 * Return label for a document type
349 *
350 */
351 public static function getDocumentTypeLabel( string $type ): string
352 {
353 $typesDictionary = [
354 'slider' => __( 'Slider', 'depicter' ),
355 'custom' => __( 'Slider', 'depicter' ),
356 'popup' => __( 'Popup', 'depicter' ),
357 'post-slider' => __( 'Post Slider', 'depicter' ),
358 'woo-slider' => __( 'Product Slider', 'depicter' ),
359 'banner-bar' => __( 'Notification Bar', 'depicter' ),
360 'carousel' => __( 'Carousel', 'depicter' ),
361 'hero-section'=> __( 'Hero Section', 'depicter' ),
362 'survey' => __( 'Survey', 'depicter' )
363 ];
364
365 return !empty( $typesDictionary[ $type ] ) ? $typesDictionary[ $type ] : __('Slider', 'depicter' );
366 }
367 }
368