type = $type; } /* * Children */ public function add_child( $block ) { $this->children[$block->row][$block->column][] = $block; } public function output_children_string( $post, $row = 0, $column = 0, $args = array() ) { $output = ''; if( isset( $this->children[$row][$column] ) ) { foreach( $this->children[$row][$column] as $child ) { $output .= $child->output( $post, $args ); } } return $output; } public function output_children( $post, $row = 0, $column = 0, $args = array() ) { echo $this->output_children_string( $post, $row, $column, $args ); } /* * Settings */ public function add_settings( $block ) { $this->settings = $block; /* * Styling */ // Positioning if( $this->present( $block, 'float' ) && $block->float != 'none' ) $this->add_style( 'float', $block->float ); if( $this->present( $block, 'center' ) && $block->center ) $this->add_style( 'margin', '0 auto' ); if( $this->present( $block, 'marginTop' ) ) $this->add_style( 'margin-top', $block->marginTop . 'px' ); if( $this->present( $block, 'marginBottom' ) ) $this->add_style( 'margin-bottom', $block->marginBottom . 'px' ); if( $this->present( $block, 'marginLeft' ) ) $this->add_style( 'margin-left', $block->marginLeft . 'px' ); if( $this->present( $block, 'marginRight' ) ) $this->add_style( 'margin-right', $block->marginRight . 'px' ); if( $this->present( $block, 'paddingTop' ) ) $this->add_style( 'padding-top', $block->paddingTop . 'px' ); if( $this->present( $block, 'paddingBottom' ) ) $this->add_style( 'padding-bottom', $block->paddingBottom . 'px' ); if( $this->present( $block, 'paddingLeft' ) ) $this->add_style( 'padding-left', $block->paddingLeft . 'px' ); if( $this->present( $block, 'paddingRight' ) ) $this->add_style( 'padding-right', $block->paddingRight . 'px' ); if( $this->present( $block, 'paddingTop' ) ) $this->add_style( 'padding-top', $block->paddingTop . 'px', 'td' ); if( $this->present( $block, 'paddingBottom' ) ) $this->add_style( 'padding-bottom', $block->paddingBottom . 'px', 'td' ); if( $this->present( $block, 'paddingLeft' ) ) $this->add_style( 'padding-left', $block->paddingLeft . 'px', 'td' ); if( $this->present( $block, 'paddingRight' ) ) $this->add_style( 'padding-right', $block->paddingRight . 'px', 'td' ); if( $this->present( $block, 'width' ) ) $this->add_style( 'width', $block->width . $block->widthType ); if( $this->present( $block, 'height' ) ) $this->add_style( 'height', $block->height . $block->heightType ); if( $this->present( $block, 'maxWidth' ) ) $this->add_style( 'max-width', $block->maxWidth . $block->maxWidthType ); if( $this->present( $block, 'maxHeight' ) ) $this->add_style( 'max-height', $block->maxHeight . $block->maxHeightType ); if( $this->present( $block, 'minWidth' ) ) $this->add_style( 'min-width', $block->minWidth . $block->minWidthType ); if( $this->present( $block, 'minHeight' ) ) $this->add_style( 'min-height', $block->minHeight . $block->minHeightType ); if( $this->present( $block, 'position' ) ) { $this->add_style( 'position', $block->position ); if( $block->position != 'static' ) { if( $this->present( $block, 'positionTop' ) ) $this->add_style( 'top', $block->positionTop . 'px' ); if( $this->present( $block, 'positionBottom' ) ) $this->add_style( 'bottom', $block->positionBottom . 'px' ); if( $this->present( $block, 'positionLeft' ) ) $this->add_style( 'left', $block->positionLeft . 'px' ); if( $this->present( $block, 'positionRight' ) ) $this->add_style( 'right', $block->positionRight . 'px' ); } } // Block Style if( $this->present( $block, 'backgroundPreset' ) ) { $this->background_preset = $block->backgroundPreset; } if( $this->present( $block, 'backgroundImage' ) ) $this->add_style( 'background', 'url(' . $block->backgroundImage . ')' ); if( $this->present( $block, 'backgroundColor' ) ) $this->add_style( 'background-color', $block->backgroundColor ); if( $this->present( $block, 'borderWidth' ) ) { $borderTop = $this->present( $block, 'borderTop' ) ? $block->borderWidth . 'px' : '0'; $borderBottom = $this->present( $block, 'borderBottom' ) ? $block->borderWidth . 'px' : '0'; $borderLeft = $this->present( $block, 'borderLeft' ) ? $block->borderWidth . 'px' : '0'; $borderRight = $this->present( $block, 'borderRight' ) ? $block->borderWidth . 'px' : '0'; $this->add_style( 'border-width', $borderTop . ' ' . $borderRight . ' ' .$borderBottom . ' ' . $borderLeft ); if( $this->present( $block, 'borderColor' ) ) $this->add_style( 'border-color', $block->borderColor ); if( $this->present( $block, 'borderStyle' ) ) $this->add_style( 'border-style', $block->borderStyle ); $this->add_style( 'border-width', $block->borderWidth . 'px', 'td' ); if( $this->present( $block, 'borderColor' ) ) $this->add_style( 'border-color', $block->borderColor, 'td' ); if( $this->present( $block, 'borderStyle' ) ) $this->add_style( 'border-style', $block->borderStyle, 'td' ); } if( $this->present( $block, 'shadowColor' ) && $this->present( $block, 'shadowHorizontal' ) && $this->present( $block, 'shadowVertical' ) ) { $blur = $this->present( $block, 'shadowBlur' ) ? $block->shadowBlur . 'px ' : ' '; $spread = $this->present( $block, 'shadowSpread' ) ? $block->shadowSpread . 'px ' : ' '; $shadow = $block->shadowHorizontal . 'px ' . $block->shadowVertical . 'px ' . $blur . $spread . $block->shadowColor . ' ' . $block->shadowType; $this->add_style( '-webkit-box-shadow', $shadow ); $this->add_style( '-moz-box-shadow', $shadow ); $this->add_style( 'box-shadow', $shadow ); } // Text Style if( $this->present( $block, 'textAlign' ) && $this->type != 'container' ) { $this->add_style( 'text-align', $block->textAlign ); $this->add_style( 'text-align', $block->textAlign, 'td' ); } if( $this->present( $block, 'verticalAlign' ) ) { $this->add_style( 'vertical-align', $block->verticalAlign ); } if( $this->present( $block, 'fontBold' ) && $block->fontBold ) { $this->add_style( 'font-weight', 'bold' ); $this->add_style( 'font-weight', 'bold', 'td' ); } if( $this->present( $block, 'fontSmallCaps' ) && $block->fontSmallCaps ) $this->add_style( 'font-variant', 'small-caps' ); $fontSizeUnit = $this->present( $block, 'fontSizeUnit' ) ? $block->fontSizeUnit : 'px'; $lineHeightUnit = $this->present( $block, 'lineHeightUnit' ) ? $block->lineHeightUnit : 'px'; if( $this->present( $block, 'fontSize' ) ) $this->add_style( 'font-size', $block->fontSize . $fontSizeUnit ); if( $this->present( $block, 'lineHeight' ) ) $this->add_style( 'line-height', $block->lineHeight . $lineHeightUnit ); if( $this->present( $block, 'fontColor' ) ) $this->add_style( 'color', $block->fontColor ); if( $this->present( $block, 'linkColor' ) ) $this->link_color = $block->linkColor; if( $this->present( $block, 'fontFamilyType' ) && $block->fontFamilyType == 'manual' ) { if( $this->present( $block, 'fontFamilyManual' ) ) $this->add_style( 'font-family', $block->fontFamilyManual ); } if( $this->present( $block, 'fontFamilyType' ) && $block->fontFamilyType == 'gwf' ) { if( $this->present( $block, 'fontFamilyGWF' ) ) { $font = str_replace( '+',' ', $block->fontFamilyGWF ); $font .= ', sans-serif'; $this->add_style( 'font-family', $font ); } } /* * Conditions */ if( isset( $block->conditions ) ) { foreach( $block->conditions as $condition ) { if( ( $condition->condition_type == 'field' || $condition->condition_type == 'custom_field' ) && $this->present( $condition, 'field' ) ) { $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'field', 'field' => $condition->field, 'when' => $condition->when ), $condition->target ); } else if( $condition->condition_type == 'sub_field' && $this->present( $condition, 'field' ) ) { $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'sub_field', 'field' => $condition->field, 'when' => $condition->when ), $condition->target ); } else if( $condition->condition_type == 'setting' && $this->present( $condition, 'setting' ) ) { $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'setting', 'setting' => $condition->setting, 'when' => $condition->when ), $condition->target ); } else if( $condition->condition_type == 'responsive' ) { $this->add_condition( array( 'type' => 'hide', 'condition_type' => 'responsive', 'when' => $condition->when ), $condition->target ); } } } /* * Max block size */ if( $this->present( $block, 'maxWidth' ) && $block->maxWidthType == 'px' ) $this->max_width = intval( $block->maxWidth ); if( $this->present( $block, 'maxHeight' ) && $block->maxHeightType == 'px' ) $this->max_height = intval( $block->maxHeight ); if( $this->present( $block, 'width' ) && $block->widthType == 'px' ) $this->max_width = intval( $block->width ); if( $this->present( $block, 'height' ) && $block->heightType == 'px' ) $this->max_height = intval( $block->height ); /* * Cut off */ if( $this->present( $block, 'shortenText') && $block->shortenText != 'none' ) { $this->cut_off = array( 'type' => $block->shortenText, 'number' => intval( $block->shortenTextNumber ), 'after_text' => $block->shortenTextAfter, ); } } protected function present( $block, $field ) { if( is_array( $block ) ) { return isset( $block[$field] ) && !is_null( $block[$field] ) && $block[$field] != ''; } else { return isset( $block->{$field} ) && !is_null( $block->{$field} ) && $block->{$field} != ''; } } /* * Styling */ public function add_class( $class ) { $this->classes[] = $class; } public function add_style( $property, $value, $name = 'default' ) { $this->style[$name][$property] = str_replace( '"', "'", $value ); } private function get_style_string( $name ) { $output = ''; foreach( $this->style[$name] as $property => $value ) { if( WPUltimatePostGrid::option( 'grid_template_force_style', '1' ) == '1' ) { $output .= $property . ':' . $value . ' !important;'; } else { $output .= $property . ':' . $value . ';'; } } return $output; } protected function style( $names = 'default' ) { if( !is_array( $names ) ) { $names = array( $names ); } $style = ''; $class = ''; // Only add inline style if setting is enabled if( WPUltimatePostGrid::option( 'grid_template_inline_css', '1' ) == '1' ) { foreach( $names as $name ) { if( isset( $this->style[$name] ) ) { $style .= $this->get_style_string( $name ); } } } // Special Custom styles if( in_array( $this->type, array( 'recipe-ingredient-container', 'recipe-instruction-container' ) ) ) { if( in_array( 'li', $names ) && $this->present( $this->settings, 'customStyleItem' ) ) { $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleItem ) ); } if( in_array( 'li-odd', $names ) && $this->present( $this->settings, 'customStyleOdd' ) ) { $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleOdd ) ); } if( in_array( 'li-even', $names ) && $this->present( $this->settings, 'customStyleEven' ) ) { $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleEven ) ); } if( in_array( 'li-first', $names ) && $this->present( $this->settings, 'customStyleFirst' ) ) { $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleFirst ) ); } if( in_array( 'li-last', $names ) && $this->present( $this->settings, 'customStyleLast' ) ) { $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyleLast ) ); } } if( in_array( 'default', $names ) ) { // Custom inline CSS if( $this->present( $this->settings, 'customStyle' ) ) { $style .= esc_attr( preg_replace( "/\r|\n/", '', $this->settings->customStyle ) ); } // Class name $classes = $this->classes; $classes[] = 'wpupg-' . $this->type; if( $this->present( $this->settings, 'customClass' ) ) { $classes[] = esc_attr( $this->settings->customClass ); } $classes = implode( ' ', $classes ); $class = ' class="' . $classes . '"'; } if( $style == '' ) { return $class; } else { return $class . ' style="' . $style . '"'; } } /* * Conditions */ public function add_condition( $condition, $target = 'block' ) { if( $condition['condition_type'] == 'responsive' ) { if( $condition['when'] == 'mobile' ) { $this->show_on_mobile = false; } else if ( $condition['when'] == 'desktop' ) { $this->show_on_desktop = false; } } if( !isset( $this->conditions[$target] ) ) { $this->conditions[$target] = array(); } $this->conditions[$target][] = $condition; } private function condition( $post, $condition, $args = array() ) { $show = true; if( $condition['condition_type'] == 'field' ) { switch( $condition['field'] ) { case 'post_image': $post_image_id = get_post_thumbnail_id( $post->ID ); $present = $post_image_id == '' ? false : true; break; case 'post_content': $present = $post->post_content == '' ? false : true; break; case 'post_excerpt': $present = $post->post_excerpt == '' ? false : true; break; case 'post_title': $present = $post->post_title == '' ? false : true; break; default: $present = trim( get_post_meta( $post->ID, $condition['field'], true ) ); break; } if( isset( $condition['when'] ) && $condition['when'] == 'present' ) { $show = $show && !$present; // Hide when present } else { $show = $show && $present; // Hide when missing } } else if( $condition['condition_type'] == 'sub_field' && isset( $args[$condition['field']] ) ) { $present = $args[$condition['field']] == '' ? false : true; if( isset( $condition['when'] ) && $condition['when'] == 'present' ) { $show = $show && !$present; // Hide when present } else { $show = $show && $present; // Hide when missing } } else if( $condition['condition_type'] == 'setting' ) { if( in_array( $condition['setting'], array( 'partners_integrations_foodfanatic_enable', 'partners_integrations_bigoven_enable' ) ) ) { // Default 0 $val = WPUltimatePostGrid::option( $condition['setting'], '0' ); } else { // Default 1 $val = WPUltimatePostGrid::option( $condition['setting'], '1' ); } if( isset( $condition['when'] ) && $condition['when'] == 'enabled' ) { $show = $show && $val != '1'; } else { $show = $show && $val == '1'; } } return $show; } protected function show( $post, $target = 'block', $args = array() ) { if( isset( $this->conditions[$target] ) ) { foreach( $this->conditions[$target] as $condition ) { if( !$this->condition( $post, $condition, $args ) ) { return false; } } } return true; } /* * Cut off text */ protected function cut_off( $text ) { if( $this->cut_off ) { $limit = $this->cut_off['number']; if( $this->cut_off['type'] == 'words' && str_word_count( $text, 0 ) > $limit ) { // Limit to X words $words = str_word_count( $text, 2 ); $pos = array_keys( $words ); $text = substr( $text, 0, $pos[$limit] ); $text = rtrim( $text ) . $this->cut_off['after_text']; } elseif( $this->cut_off['type'] == 'characters' && strlen( $text ) > $limit ) { // Limit to X characters $text = substr( $text, 0, $limit ); $text = rtrim( $text ) . $this->cut_off['after_text']; } } return $text; } /* * Output block, called before output of child. * Return false to not output the child. */ protected function output_block( $post, $args ) { return $this->show( $post, 'block', $args ); } protected function before_output() { $output = ''; // Responsive if( !$this->show_on_desktop ) { $output = '