PluginProbe
WP Ultimate Post Grid / 1.7.2
WP Ultimate Post Grid v1.7.2
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 1.7.2, at addons/custom-templates/templates/block.php

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