PluginProbe
WP Ultimate Post Grid / 2.6.0
WP Ultimate Post Grid v2.6.0
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / addons / custom-templates / templates / block.php

block.php in WP Ultimate Post Grid 2.6.0, at addons/custom-templates/templates/block.php

596 lines 23.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WPUPG_Template_Block {
4
5 public $type;
6 public $children = array();
7 public $settings = array();
8 public $style = array();
9 public $conditions = array();
10 public $classes = array();
11
12 // Relative block position
13 public $parent;
14 public $row;
15 public $column;
16 public $order;
17
18 // Max block size
19 public $max_width;
20 public $max_height;
21
22 // Hover conditions
23 public $show_on_hover = false;
24 public $hide_on_hover = false;
25 public $hover_in_transition = 'instant';
26 public $hover_out_transition = 'instant';
27 public $hover_in_time = 0;
28 public $hover_out_time = 0;
29
30 // Responsive condition
31 protected $show_on_desktop = true;
32 protected $show_on_mobile = true;
33
34 // Special cases
35 protected $link_color = false;
36 protected $background_preset = false;
37 protected $cut_off = false;
38
39 public function __construct( $type )
40 {
41 $this->type = $type;
42 }
43
44 /*
45 * Children
46 */
47
48 public function add_child( $block )
49 {
50 $this->children[$block->row][$block->column][] = $block;
51 }
52
53 public function output_children_string( $post, $row = 0, $column = 0, $args = array() )
54 {
55 $output = '';
56
57 if( isset( $this->children[$row][$column] ) ) {
58 foreach( $this->children[$row][$column] as $child )
59 {
60 $output .= $child->output( $post, $args );
61 }
62 }
63
64 return $output;
65 }
66
67 public function output_children( $post, $row = 0, $column = 0, $args = array() )
68 {
69 echo $this->output_children_string( $post, $row, $column, $args );
70 }
71
72 /*
73 * Settings
74 */
75
76 public function add_settings( $block )
77 {
78 $this->settings = $block;
79
80 /*
81 * Styling
82 */
83
84 // Positioning
85 if( $this->present( $block, 'float' ) && $block->float != 'none' ) $this->add_style( 'float', $block->float );
86 if( $this->present( $block, 'center' ) && $block->center ) $this->add_style( 'margin', '0 auto' );
87 if( $this->present( $block, 'marginTop' ) ) $this->add_style( 'margin-top', $block->marginTop . 'px' );
88 if( $this->present( $block, 'marginBottom' ) ) $this->add_style( 'margin-bottom', $block->marginBottom . 'px' );
89 if( $this->present( $block, 'marginLeft' ) ) $this->add_style( 'margin-left', $block->marginLeft . 'px' );
90 if( $this->present( $block, 'marginRight' ) ) $this->add_style( 'margin-right', $block->marginRight . 'px' );
91
92 if( $this->present( $block, 'paddingTop' ) ) $this->add_style( 'padding-top', $block->paddingTop . 'px' );
93 if( $this->present( $block, 'paddingBottom' ) ) $this->add_style( 'padding-bottom', $block->paddingBottom . 'px' );
94 if( $this->present( $block, 'paddingLeft' ) ) $this->add_style( 'padding-left', $block->paddingLeft . 'px' );
95 if( $this->present( $block, 'paddingRight' ) ) $this->add_style( 'padding-right', $block->paddingRight . 'px' );
96 if( $this->present( $block, 'paddingTop' ) ) $this->add_style( 'padding-top', $block->paddingTop . 'px', 'td' );
97 if( $this->present( $block, 'paddingBottom' ) ) $this->add_style( 'padding-bottom', $block->paddingBottom . 'px', 'td' );
98 if( $this->present( $block, 'paddingLeft' ) ) $this->add_style( 'padding-left', $block->paddingLeft . 'px', 'td' );
99 if( $this->present( $block, 'paddingRight' ) ) $this->add_style( 'padding-right', $block->paddingRight . 'px', 'td' );
100
101 if( $this->present( $block, 'width' ) ) $this->add_style( 'width', $block->width . $block->widthType );
102 if( $this->present( $block, 'height' ) ) $this->add_style( 'height', $block->height . $block->heightType );
103 if( $this->present( $block, 'maxWidth' ) ) $this->add_style( 'max-width', $block->maxWidth . $block->maxWidthType );
104 if( $this->present( $block, 'maxHeight' ) ) $this->add_style( 'max-height', $block->maxHeight . $block->maxHeightType );
105 if( $this->present( $block, 'minWidth' ) ) $this->add_style( 'min-width', $block->minWidth . $block->minWidthType );
106 if( $this->present( $block, 'minHeight' ) ) $this->add_style( 'min-height', $block->minHeight . $block->minHeightType );
107
108 if( $this->present( $block, 'position' ) ) {
109 $this->add_style( 'position', $block->position );
110
111 if( $block->position != 'static' ) {
112 if( $this->present( $block, 'positionTop' ) ) $this->add_style( 'top', $block->positionTop . 'px' );
113 if( $this->present( $block, 'positionBottom' ) ) $this->add_style( 'bottom', $block->positionBottom . 'px' );
114 if( $this->present( $block, 'positionLeft' ) ) $this->add_style( 'left', $block->positionLeft . 'px' );
115 if( $this->present( $block, 'positionRight' ) ) $this->add_style( 'right', $block->positionRight . 'px' );
116 }
117 }
118
119 // Block Style
120 if( $this->present( $block, 'backgroundPreset' ) ) { $this->background_preset = $block->backgroundPreset; }
121 if( $this->present( $block, 'backgroundImage' ) ) $this->add_style( 'background', 'url(' . $block->backgroundImage . ')' );
122 if( $this->present( $block, 'backgroundColor' ) ) $this->add_style( 'background-color', $block->backgroundColor );
123
124 if( $this->present( $block, 'borderWidth' ) ) {
125 $borderTop = $this->present( $block, 'borderTop' ) ? $block->borderWidth . 'px' : '0';
126 $borderBottom = $this->present( $block, 'borderBottom' ) ? $block->borderWidth . 'px' : '0';
127 $borderLeft = $this->present( $block, 'borderLeft' ) ? $block->borderWidth . 'px' : '0';
128 $borderRight = $this->present( $block, 'borderRight' ) ? $block->borderWidth . 'px' : '0';
129
130 $this->add_style( 'border-width', $borderTop . ' ' . $borderRight . ' ' .$borderBottom . ' ' . $borderLeft );
131 if( $this->present( $block, 'borderColor' ) ) $this->add_style( 'border-color', $block->borderColor );
132 if( $this->present( $block, 'borderStyle' ) ) $this->add_style( 'border-style', $block->borderStyle );
133
134 $this->add_style( 'border-width', $block->borderWidth . 'px', 'td' );
135 if( $this->present( $block, 'borderColor' ) ) $this->add_style( 'border-color', $block->borderColor, 'td' );
136 if( $this->present( $block, 'borderStyle' ) ) $this->add_style( 'border-style', $block->borderStyle, 'td' );
137 }
138
139 if( $this->present( $block, 'shadowColor' ) && $this->present( $block, 'shadowHorizontal' ) && $this->present( $block, 'shadowVertical' ) )
140 {
141 $blur = $this->present( $block, 'shadowBlur' ) ? $block->shadowBlur . 'px ' : ' ';
142 $spread = $this->present( $block, 'shadowSpread' ) ? $block->shadowSpread . 'px ' : ' ';
143
144 $shadow = $block->shadowHorizontal . 'px ' . $block->shadowVertical . 'px ' . $blur . $spread . $block->shadowColor . ' ' . $block->shadowType;
145
146 $this->add_style( '-webkit-box-shadow', $shadow );
147 $this->add_style( '-moz-box-shadow', $shadow );
148 $this->add_style( 'box-shadow', $shadow );
149 }
150
151 // Text Style
152 if( $this->present( $block, 'textAlign' ) && $this->type != 'container' ) {
153 $this->add_style( 'text-align', $block->textAlign );
154 $this->add_style( 'text-align', $block->textAlign, 'td' );
155 }
156 if( $this->present( $block, 'verticalAlign' ) ) {
157 $this->add_style( 'vertical-align', $block->verticalAlign );
158 }
159
160 if( $this->present( $block, 'fontBold' ) && $block->fontBold ) {
161 $this->add_style( 'font-weight', 'bold' );
162 $this->add_style( 'font-weight', 'bold', 'td' );
163 }
164 if( $this->present( $block, 'fontSmallCaps' ) && $block->fontSmallCaps ) $this->add_style( 'font-variant', 'small-caps' );
165
166 $fontSizeUnit = $this->present( $block, 'fontSizeUnit' ) ? $block->fontSizeUnit : 'px';
167 $lineHeightUnit = $this->present( $block, 'lineHeightUnit' ) ? $block->lineHeightUnit : 'px';
168
169 if( $this->present( $block, 'fontSize' ) ) $this->add_style( 'font-size', $block->fontSize . $fontSizeUnit );
170 if( $this->present( $block, 'lineHeight' ) ) $this->add_style( 'line-height', $block->lineHeight . $lineHeightUnit );
171 if( $this->present( $block, 'fontColor' ) ) $this->add_style( 'color', $block->fontColor );
172 if( $this->present( $block, 'linkColor' ) ) $this->link_color = $block->linkColor;
173
174 if( $this->present( $block, 'fontFamilyType' ) && $block->fontFamilyType == 'manual' ) {
175 if( $this->present( $block, 'fontFamilyManual' ) ) $this->add_style( 'font-family', $block->fontFamilyManual );
176 }
177 if( $this->present( $block, 'fontFamilyType' ) && $block->fontFamilyType == 'gwf' ) {
178 if( $this->present( $block, 'fontFamilyGWF' ) ) {
179 $font = str_replace( '+',' ', $block->fontFamilyGWF );
180 $font .= ', sans-serif';
181 $this->add_style( 'font-family', $font );
182 }
183 }
184
185 /*
186 * Hover
187 */
188 if( $this->present( $block, 'hover' ) ) {
189 if( $block->hover == 'show' ) $this->show_on_hover = true;
190 if( $block->hover == 'hide' ) $this->hide_on_hover = true;
191 }
192 if( $this->present( $block, 'hoverInTransition' ) ) $this->hover_in_transition = $block->hoverInTransition;
193 if( $this->present( $block, 'hoverOutTransition' ) ) $this->hover_out_transition = $block->hoverOutTransition;
194 if( $this->present( $block, 'hoverInTime' ) ) $this->hover_in_time = $block->hoverInTime;
195 if( $this->present( $block, 'hoverOutTime' ) ) $this->hover_out_time = $block->hoverOutTime;
196
197 /*
198 * Conditions
199 */
200 if( isset( $block->conditions ) ) {
201 foreach( $block->conditions as $condition ) {
202 if( ( $condition->condition_type == 'field' || $condition->condition_type == 'custom_field' ) && $this->present( $condition, 'field' ) ) {
203 $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'field', 'field' => $condition->field, 'when' => $condition->when ), $condition->target );
204 } else if( $condition->condition_type == 'sub_field' && $this->present( $condition, 'field' ) ) {
205 $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'sub_field', 'field' => $condition->field, 'when' => $condition->when ), $condition->target );
206 } else if( $condition->condition_type == 'setting' && $this->present( $condition, 'setting' ) ) {
207 $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'setting', 'setting' => $condition->setting, 'when' => $condition->when ), $condition->target );
208 } else if( $condition->condition_type == 'responsive' ) {
209 $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'responsive', 'when' => $condition->when ), $condition->target );
210 }
211 }
212 }
213
214 /*
215 * Max block size
216 */
217 if( $this->present( $block, 'maxWidth' ) && $block->maxWidthType == 'px' ) $this->max_width = intval( $block->maxWidth );
218 if( $this->present( $block, 'maxHeight' ) && $block->maxHeightType == 'px' ) $this->max_height = intval( $block->maxHeight );
219 if( $this->present( $block, 'width' ) && $block->widthType == 'px' ) $this->max_width = intval( $block->width );
220 if( $this->present( $block, 'height' ) && $block->heightType == 'px' ) $this->max_height = intval( $block->height );
221
222 /*
223 * Cut off
224 */
225 if( $this->present( $block, 'shortenText') && $block->shortenText != 'none' ) {
226 $this->cut_off = array(
227 'type' => $block->shortenText,
228 'number' => intval( $block->shortenTextNumber ),
229 'after_text' => $block->shortenTextAfter,
230 );
231 }
232 }
233
234 protected function present( $block, $field )
235 {
236 if( is_array( $block ) ) {
237 return isset( $block[$field] ) && !is_null( $block[$field] ) && $block[$field] != '';
238 } else {
239 return isset( $block->{$field} ) && !is_null( $block->{$field} ) && $block->{$field} != '';
240 }
241 }
242
243 /*
244 * Styling
245 */
246
247 public function add_class( $class )
248 {
249 $this->classes[] = $class;
250 }
251
252 public function add_style( $property, $value, $name = 'default' )
253 {
254 $this->style[$name][$property] = str_replace( '"', "'", $value );
255 }
256
257 private function get_style_string( $name )
258 {
259 $output = '';
260
261 foreach( $this->style[$name] as $property => $value )
262 {
263 if( WPUltimatePostGrid::option( 'grid_template_force_style', '0' ) == '1' ) {
264 $output .= $property . ':' . $value . ' !important;';
265 } else {
266 $output .= $property . ':' . $value . ';';
267 }
268 }
269
270 return $output;
271 }
272
273 protected function style( $names = 'default' )
274 {
275 if( !is_array( $names ) ) {
276 $names = array( $names );
277 }
278
279 $style = '';
280 $class = '';
281
282 // Only add inline style if setting is enabled
283 if( WPUltimatePostGrid::option( 'grid_template_inline_css', '1' ) == '1' ) {
284 foreach( $names as $name )
285 {
286 if( isset( $this->style[$name] ) ) {
287 $style .= $this->get_style_string( $name );
288 }
289 }
290 }
291
292 // Special Custom styles
293 if( in_array( $this->type, array( 'recipe-ingredient-container', 'recipe-instruction-container' ) ) ) {
294 if( in_array( 'li', $names ) && $this->present( $this->settings, 'customStyleItem' ) ) {
295 $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleItem ) );
296 }
297 if( in_array( 'li-odd', $names ) && $this->present( $this->settings, 'customStyleOdd' ) ) {
298 $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleOdd ) );
299 }
300 if( in_array( 'li-even', $names ) && $this->present( $this->settings, 'customStyleEven' ) ) {
301 $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleEven ) );
302 }
303 if( in_array( 'li-first', $names ) && $this->present( $this->settings, 'customStyleFirst' ) ) {
304 $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleFirst ) );
305 }
306 if( in_array( 'li-last', $names ) && $this->present( $this->settings, 'customStyleLast' ) ) {
307 $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleLast ) );
308 }
309 }
310
311 if( in_array( 'default', $names ) ) {
312 // Custom inline CSS
313 if( $this->present( $this->settings, 'customStyle' ) ) {
314 $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyle ) );
315 }
316
317 // Class name
318 $classes = $this->classes;
319 $classes[] = 'wpupg-' . $this->type;
320
321 if( $this->present( $this->settings, 'customClass' ) ) {
322 $classes[] = esc_attr( $this->settings->customClass );
323 }
324
325 if( $this->hide_on_hover || $this->show_on_hover ) {
326 $classes[] = $this->hide_on_hover ? 'wpupg-hide-on-hover' : 'wpupg-show-on-hover';
327 }
328
329 $classes = implode( ' ', $classes );
330
331 $class = ' class="' . $classes . '"';
332
333 if( $this->hide_on_hover || $this->show_on_hover ) {
334 $class .= ' data-hover-in="' . $this->hover_in_transition . '" data-hover-out="' . $this->hover_out_transition . '"';
335 $class .= ' data-hover-in-duration="' . $this->hover_in_time . '" data-hover-out-duration="' . $this->hover_out_time . '"';
336 }
337 }
338
339
340 if( $style == '' ) {
341 return $class;
342 } else {
343 return $class . ' style="' . $style . '"';
344 }
345 }
346
347 /*
348 * Conditions
349 */
350
351 public function add_condition( $condition, $target = 'block' )
352 {
353 if( $condition['condition_type'] == 'responsive' ) {
354 if( $condition['when'] == 'mobile' ) {
355 $this->show_on_mobile = false;
356 } else if ( $condition['when'] == 'desktop' ) {
357 $this->show_on_desktop = false;
358 }
359 }
360
361 if( !isset( $this->conditions[$target] ) ) {
362 $this->conditions[$target] = array();
363 }
364
365 $this->conditions[$target][] = $condition;
366 }
367
368 private function condition( $post, $condition, $args = array() )
369 {
370 $show = true;
371
372 if( $condition['condition_type'] == 'field' ) {
373 switch( $condition['field'] ) {
374 case 'post_image':
375 if( isset( $post->term ) && $post->term ) {
376 $post_image_id = get_term_meta( $post->ID, 'wpupg_custom_image', true );
377 } elseif( $post->post_type == 'attachment' ) {
378 $post_image_id = $post->ID;
379 } else {
380 $post_image_id = get_post_thumbnail_id( $post->ID );
381 if( !$post_image_id ) {
382 $post_image_id = get_post_meta( $post->ID, 'wpupg_custom_image', true );
383 }
384 }
385
386 $present = $post_image_id == '' ? false : true;
387 break;
388 case 'post_content':
389 $present = $post->post_content == '' ? false : true;
390 break;
391 case 'post_excerpt':
392 $present = $post->post_excerpt == '' ? false : true;
393 break;
394 case 'post_title':
395 $present = $post->post_title == '' ? false : true;
396 break;
397 default:
398 $present = trim( get_post_meta( $post->ID, $condition['field'], true ) );
399 break;
400 }
401
402 if( isset( $condition['when'] ) && $condition['when'] == 'present' ) {
403 $show = $show && !$present; // Hide when present
404 } else {
405 $show = $show && $present; // Hide when missing
406 }
407 } else if( $condition['condition_type'] == 'sub_field' && isset( $args[$condition['field']] ) ) {
408 $present = $args[$condition['field']] == '' ? false : true;
409
410 if( isset( $condition['when'] ) && $condition['when'] == 'present' ) {
411 $show = $show && !$present; // Hide when present
412 } else {
413 $show = $show && $present; // Hide when missing
414 }
415 } else if( $condition['condition_type'] == 'setting' ) {
416
417 if( in_array( $condition['setting'], array(
418 'partners_integrations_foodfanatic_enable',
419 'partners_integrations_bigoven_enable' ) ) ) {
420 // Default 0
421 $val = WPUltimatePostGrid::option( $condition['setting'], '0' );
422 } else {
423 // Default 1
424 $val = WPUltimatePostGrid::option( $condition['setting'], '1' );
425 }
426
427 if( isset( $condition['when'] ) && $condition['when'] == 'enabled' ) {
428 $show = $show && $val != '1';
429 } else {
430 $show = $show && $val == '1';
431 }
432 }
433
434 return $show;
435 }
436
437 protected function show( $post, $target = 'block', $args = array() )
438 {
439 if( isset( $this->conditions[$target] ) ) {
440 foreach( $this->conditions[$target] as $condition ) {
441 if( !$this->condition( $post, $condition, $args ) ) {
442 return false;
443 }
444 }
445 }
446
447 return true;
448 }
449
450 /*
451 * Cut off text
452 */
453 protected function cut_off( $text )
454 {
455 if( $this->cut_off ) {
456 $limit = $this->cut_off['number'];
457
458 if( $this->cut_off['type'] == 'words' && str_word_count( $text, 0 ) > $limit ) {
459 // Limit to X words
460 $words = str_word_count( $text, 2 );
461 $pos = array_keys( $words );
462 $text = substr( $text, 0, $pos[$limit] );
463
464 $text = rtrim( $text ) . $this->cut_off['after_text'];
465 } elseif( $this->cut_off['type'] == 'characters' && strlen( $text ) > $limit ) {
466 // Limit to X characters
467 $text = substr( $text, 0, $limit );
468
469 $text = rtrim( $text ) . $this->cut_off['after_text'];
470 }
471
472 if( $text ) {
473 $text = $this->clean_html( $text );
474 }
475 }
476
477 return $text;
478 }
479
480 protected function clean_html( $html )
481 {
482 $doc = new DOMDocument();
483 libxml_use_internal_errors( true );
484 $doc->loadHTML( '<?xml version="1.0" encoding="UTF-8"?><html_tags>' . $html . '</html_tags>' );
485 libxml_clear_errors();
486 return substr( $doc->saveXML( $doc->getElementsByTagName( 'html_tags' )->item( 0 ) ), strlen( '<html_tags>' ), -strlen( '</html_tags>' ) );
487 }
488
489 /*
490 * Output block, called before output of child.
491 * Return false to not output the child.
492 */
493 protected function output_block( $post, $args )
494 {
495 return $this->show( $post, 'block', $args );
496 }
497
498 protected function before_output()
499 {
500 $output = '';
501
502 // Responsive
503 if( !$this->show_on_desktop ) {
504 $output = '<div class="wpupg-responsive-mobile">';
505 } else if( !$this->show_on_mobile ) {
506 $output = '<div class="wpupg-responsive-desktop">';
507 }
508
509 // Background presets
510 if( $this->background_preset )
511 {
512 switch( $this->background_preset ) {
513 case 'default':
514 $img = WPUltimatePostGrid::addon( 'custom-templates' )->addonUrl . '/img/default.png';
515 break;
516 default:
517 $img = WPUltimatePostGrid::addon( 'template-editor' )->addonUrl . '/img/' . $this->background_preset . '.png';
518 }
519
520 if( isset( $img ) ) $this->add_style( 'background', 'url(' . $img . ')' );
521 }
522
523 return $output;
524 }
525
526 protected function after_output( $output, $post )
527 {
528 if( !$this->show_on_desktop || !$this->show_on_mobile ) {
529 $output .= '</div>';
530 }
531
532 // TODO Better way of doing this?
533 if( $this->link_color ) {
534
535 if( WPUltimatePostGrid::option( 'grid_template_force_style', '0' ) == '1' ) {
536 $important = ' !important';
537 } else {
538 $important = '';
539 }
540
541
542 preg_match_all("/<a [^><]*>/i", $output, $links);
543
544 foreach( $links[0] as $link )
545 {
546 $new_link = preg_replace('/( style=")([^"]*")/i', '$1color: ' . $this->link_color . $important .';$2', $link);
547
548 if( $new_link == $link ) {
549 $new_link = str_ireplace('<a ', '<a style="color: ' . $this->link_color . $important . ';" ', $link);
550 }
551
552 $output = str_ireplace( $link, $new_link, $output );
553 }
554 }
555
556 return apply_filters( 'wpupg_output_grid_block_' . $this->type, $output, $post, $this );
557 }
558
559 /*
560 * Quick Access
561 */
562
563 public function loc( $parent, $row, $column, $order )
564 {
565 $this->parent = $parent;
566 $this->row = $row;
567 $this->column = $column;
568 $this->order = $order;
569
570 return $this;
571 }
572
573 public function parent( $parent )
574 {
575 $this->parent = $parent;
576 return $this;
577 }
578
579 public function row( $row )
580 {
581 $this->row = $row;
582 return $this;
583 }
584
585 public function column( $column )
586 {
587 $this->column = $column;
588 return $this;
589 }
590
591 public function order( $order )
592 {
593 $this->order = $order;
594 return $this;
595 }
596 }