inline_css = null; } public static function single() { static $single; return empty( $single ) ? $single = new self() : $single; } /** * Add CSS that needs to go inline. * * @param $post_id * @param $css */ public function add_inline_css( $post_id, $css ) { if ( is_null( $this->inline_css ) ) { // Initialize the inline CSS array and add actions to handle printing. $this->inline_css = array(); $css_output_set = false; $output_css = siteorigin_panels_setting( 'output-css-header'); if ( is_admin() || SiteOrigin_Panels_Admin::is_block_editor() || $output_css == 'auto' ) { add_action( 'wp_head', array( $this, 'print_inline_css' ), 12 ); add_action( 'wp_footer', array( $this, 'print_inline_css' ) ); $css_output_set = true; } // The CSS can only be output in the header if the page is powered by the Classic Editor. // $post_id won't be a number if the current page is powered by the Block Editor. if ( ! $css_output_set && $output_css == 'header' && is_numeric( $post_id ) ) { add_action( 'wp_head', array( $this, 'print_inline_css' ), 12 ); $css_output_set = true; } if ( ! $css_output_set ) { add_action( 'wp_footer', array( $this, 'print_inline_css' ) ); } } $this->inline_css[ $post_id ] = $css; // Enqueue the front styles, if they haven't already been enqueued if ( ! wp_style_is( 'siteorigin-panels-front', 'enqueued' ) ) { wp_enqueue_style( 'siteorigin-panels-front' ); } } /** * Generate the CSS for the page layout. * * @param $post_id * @param $panels_data * @param $layout_data * * @return string */ public function generate_css( $post_id, $panels_data = false, $layout_data = false ) { // Exit if we don't have panels data if ( empty( $panels_data ) ) { $panels_data = get_post_meta( $post_id, 'panels_data', true ); $panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, $post_id ); if ( empty( $panels_data ) ) { return ''; } } if ( empty( $layout_data ) ) { $layout_data = $this->get_panels_layout_data( $panels_data ); $layout_data = apply_filters( 'siteorigin_panels_layout_data', $layout_data, $post_id ); } if ( empty( $this->container ) ) { $this->container = SiteOrigin_Panels::container_settings(); } // Get some of the default settings $settings = siteorigin_panels_setting(); $panels_tablet_width = $settings['tablet-width']; $panels_mobile_width = $settings['mobile-width']; $panels_margin_bottom_last_row = $settings['margin-bottom-last-row']; $css = new SiteOrigin_Panels_Css_Builder(); $ci = 0; foreach ( $layout_data as $ri => $row ) { // Filter the bottom margin for this row with the arguments $panels_margin_bottom = apply_filters( 'siteorigin_panels_css_row_margin_bottom', $settings['margin-bottom'] . 'px', $row, $ri, $panels_data, $post_id ); $panels_mobile_margin_bottom = apply_filters( 'siteorigin_panels_css_row_mobile_margin_bottom', $settings['row-mobile-margin-bottom'] . 'px', $row, $ri, $panels_data, $post_id ); if ( empty( $row['cells'] ) ) { continue; } // Let other themes and plugins change the gutter. $gutter = apply_filters( 'siteorigin_panels_css_row_gutter', $settings['margin-sides'] . 'px', $row, $ri, $panels_data ); preg_match( '/([0-9\.,]+)(.*)/', $gutter, $gutter_parts ); $cell_count = count( $row['cells'] ); // If the CSS Container Breaker is enabled, and this row is using it, // we need to remove the cell widths on mobile. $css_container_cutoff = $this->container['css_override'] && isset( $row['style']['row_stretch'] ) && $row['style']['row_stretch'] == 'full' ? ':' . ( $panels_mobile_width + 1 ) : 1920; if ( $this->container['css_override'] && ! $this->container['full_width'] && ! empty( $row['style'] ) && ! empty( $row['style']['row_stretch'] ) && ( $row['style']['row_stretch'] == 'full' || $row['style']['row_stretch'] == 'full-stretched' || $row['style']['row_stretch'] == 'full-stretched-padded' ) ) { $this->container['full_width'] = true; } // Add the cell sizing foreach ( $row['cells'] as $ci => $cell ) { $weight = apply_filters( 'siteorigin_panels_css_cell_weight', $cell['weight'], $row, $ri, $cell, $ci - 1, $panels_data, $post_id ); $rounded_width = round( $weight * 100, 4 ) . '%'; $calc_width = 'calc(' . $rounded_width . ' - ( ' . ( 1 - $weight ) . ' * ' . $gutter . ' ) )'; // Add the width and ensure we have correct formatting for CSS. $css->add_cell_css( $post_id, $ri, $ci, '', array( 'width' => array( // For some locales PHP uses ',' for decimal separation. // This seems to happen when a plugin calls `setlocale(LC_ALL, 'de_DE');` or `setlocale(LC_NUMERIC, 'de_DE');` // This should prevent issues with column sizes in these cases. str_replace( ',', '.', $rounded_width ), str_replace( ',', '.', (int) $gutter ? $calc_width : '' ), // Exclude if there's a zero gutter ) ), $css_container_cutoff ); // Add in any widget specific CSS foreach ( $cell['widgets'] as $wi => $widget ) { $widget_style_data = ! empty( $widget['panels_info']['style'] ) ? $widget['panels_info']['style'] : array(); $widget_css = apply_filters( 'siteorigin_panels_css_widget_css', array(), $widget_style_data, $row, $ri, $cell, $ci - 1, $widget, $wi, $panels_data, $post_id ); $css->add_widget_css( $post_id, $ri, $ci, $wi, '', $widget_css, 1920, true ); $panels_mobile_widget_mobile_margin = apply_filters( 'siteorigin_panels_css_widget_mobile_margin', ! empty( $widget['panels_info']['style']['mobile_margin'] ) ? $widget['panels_info']['style']['mobile_margin'] : false, $widget, $wi, $panels_data, $post_id ); if ( empty( $panels_mobile_widget_mobile_margin ) && ! empty( $settings['widget-mobile-margin-bottom'] ) ) { $panels_mobile_widget_mobile_margin = '0 0 ' . $settings[ 'widget-mobile-margin-bottom'] . 'px'; } if ( ! empty( $panels_mobile_widget_mobile_margin ) ) { $css->add_widget_css( $post_id, $ri, $ci, $wi, '', array( 'margin' => $panels_mobile_widget_mobile_margin ), $panels_mobile_width, true ); } } } if ( $ri != count( $layout_data ) - 1 || ! empty( $row['style']['bottom_margin'] ) || ! empty( $panels_margin_bottom_last_row ) ) { $css->add_row_css( $post_id, $ri, '', array( 'margin-bottom' => $panels_margin_bottom ) ); } $collapse_order = ! empty( $row['style']['collapse_order'] ) ? $row['style']['collapse_order'] : ( ! is_rtl() ? 'left-top' : 'right-top' ); // Let other themes and plugins change the row collapse point. $collapse_point = apply_filters( 'siteorigin_panels_css_row_collapse_point', '', $row, $ri, $panels_data ); if ( $settings['responsive'] && empty( $row['style']['collapse_behaviour'] ) ) { // The default collapse behaviour if ( $settings['tablet-layout'] && $cell_count >= 3 && $panels_tablet_width > $panels_mobile_width && empty( $collapse_point ) ) { // Tablet responsive css for the row $css->add_row_css( $post_id, $ri, array( '.panel-no-style', '.panel-has-style > .panel-row-style' ), array( '-ms-flex-wrap' => $collapse_order == 'left-top' ? 'wrap' : 'wrap-reverse', '-webkit-flex-wrap' => $collapse_order == 'left-top' ? 'wrap' : 'wrap-reverse', 'flex-wrap' => $collapse_order == 'left-top' ? 'wrap' : 'wrap-reverse', ), $panels_tablet_width . ':' . ( $panels_mobile_width + 1 ) ); $css->add_cell_css( $post_id, $ri, false, '', array( '-ms-flex' => '0 1 50%', '-webkit-flex' => '0 1 50%', 'flex' => '0 1 50%', 'margin-right' => '0', 'margin-bottom' => $panels_margin_bottom, ), $panels_tablet_width . ':' . ( $panels_mobile_width + 1 ) ); $remove_bottom_margin = ':nth-'; if ( $collapse_order == 'left-top' ) { $remove_bottom_margin .= 'last-child(' . ( count( $row['cells'] ) % 2 == 0 ? '-n+2' : '1' ) . ')'; } else { $remove_bottom_margin .= 'child(-n+2)'; } if ( ! empty( $gutter_parts[1] ) ) { // Tablet responsive css for cells $css->add_cell_css( $post_id, $ri, false, ':nth-child(even)', array( 'padding-left' => ( (float) $gutter_parts[1] / 2 . $gutter_parts[2] ), ), $panels_tablet_width . ':' . ( $panels_mobile_width + 1 ) ); $css->add_cell_css( $post_id, $ri, false, ':nth-child(odd)', array( 'padding-right' => ( (float) $gutter_parts[1] / 2 . $gutter_parts[2] ), ), $panels_tablet_width . ':' . ( $panels_mobile_width + 1 ) ); } } // Mobile Responsive $collapse_point = ! empty( $collapse_point ) ? $collapse_point : $panels_mobile_width; // Uses rows custom collapse point or sets mobile collapse point set on settings page. $css->add_row_css( $post_id, $ri, array( '.panel-no-style', '.panel-has-style > .panel-row-style', // When CSS override is enabled, a full width row has a special wrapper so need to account for that. $this->container['css_override'] && isset( $row['style']['row_stretch'] ) && $row['style']['row_stretch'] == 'full' ? ' .so-panels-full-wrapper' : '', ), array( '-webkit-flex-direction' => $collapse_order == 'left-top' ? 'column' : 'column-reverse', '-ms-flex-direction' => $collapse_order == 'left-top' ? 'column' : 'column-reverse', 'flex-direction' => $collapse_order == 'left-top' ? 'column' : 'column-reverse', ), $collapse_point ); // Uses rows custom collapse point or sets mobile collapse point set on settings page. $css->add_cell_css( $post_id, $ri, false, '', array( 'width' => '100%', 'margin-right' => 0, ), $collapse_point ); foreach ( $row['cells'] as $ci => $cell ) { if ( ( $collapse_order == 'left-top' && $ci != $cell_count - 1 ) || ( $collapse_order == 'right-top' && $ci !== 0 ) ) { $css->add_cell_css( $post_id, $ri, $ci, '', array( 'margin-bottom' => apply_filters( 'siteorigin_panels_css_cell_mobile_margin_bottom', $settings['mobile-cell-margin'] . 'px', $cell, $ci, $row, $ri, $panels_data, $post_id ) ), $collapse_point ); } } if ( $settings['tablet-layout'] && $panels_tablet_width > $collapse_point && ! empty( $row['style']['tablet_bottom_margin'] ) ) { $css->add_row_css( $post_id, $ri, '', array( 'margin-bottom' => $row['style']['tablet_bottom_margin'] ), "$panels_tablet_width:$collapse_point" ); } if( $panels_mobile_margin_bottom != $panels_margin_bottom && ! empty( $panels_mobile_margin_bottom ) ) { $css->add_row_css( $post_id, $ri, '', array( 'margin-bottom' => $panels_mobile_margin_bottom ), $collapse_point ); } } // End of responsive code } // Add the bottom margins $css->add_widget_css( $post_id, false, false, false, '', array( 'margin-bottom' => apply_filters( 'siteorigin_panels_css_cell_margin_bottom', $settings['margin-bottom'] . 'px', false, false, $panels_data, $post_id ) ) ); $css->add_widget_css( $post_id, false, false, false, ':last-of-type', array( 'margin-bottom' => apply_filters( 'siteorigin_panels_css_cell_last_margin_bottom', '0px', false, false, $panels_data, $post_id ) ) ); if ( $settings['responsive'] ) { $css->add_cell_css( $post_id, false, false, '', array( 'padding' => 0, ), $panels_mobile_width ); // Hide empty cells on mobile $css->add_row_css( $post_id, false, ' .panel-grid-cell-empty', array( 'display' => 'none', ), $panels_mobile_width ); // Hide empty cells on mobile $css->add_row_css( $post_id, false, ' .panel-grid-cell-mobile-last', array( 'margin-bottom' => '0px', ), $panels_mobile_width ); } // Do we need to remove the theme container on this page? if ( $this->container['css_override'] && $this->container['full_width'] && // Does this layout have full width layouts? ! defined( 'siteorigin_css_override' ) ) { // Prevent this CSS from being added again. define( 'siteorigin_css_override', true ); $css->add_css( esc_html( $this->container['selector'] ), array( 'max-width' => 'none', // Clear horizontal spacing from container to prevent any indents. 'padding-right' => '0', 'padding-left' => '0', 'margin-right' => '0', 'margin-left' => '0', ), 1920 ); $css->add_css( '.so-panels-full-wrapper, .panel-grid.panel-no-style, .panel-row-style:not([data-stretch-type])', array( 'max-width' => esc_attr( $this->container['width'] ), 'margin' => '0 auto', ), 1920 ); // Allow .so-panels-full-wrapper to handle columns correctly. $css->add_css( '.so-panels-full-wrapper', array( 'display' => 'flex', 'flex-wrap' => 'nowrap', 'justify-content' => 'space-between', 'align-items' => 'flex-start', 'width' => '100%', ), 1920 ); // Ensure cells inside of .so-panels-full-wrapper are full width when collapsed. $css->add_css( '.so-panels-full-wrapper .panel-grid-cell', array( 'width' => '100%', ), siteorigin_panels_setting( 'mobile-width' ) ); } // Let other plugins and components filter the CSS object. $css = apply_filters( 'siteorigin_panels_css_object', $css, $panels_data, $post_id, $layout_data ); return $css->get_css(); } /** * Render the panels. * * @param int|string|bool $post_id The Post ID or 'home'. * @param bool $enqueue_css Should we also enqueue the layout CSS. * @param array|bool $panels_data Existing panels data. By default load from settings or post meta. * @param array $layout_data Reformatted panels_data that includes data about the render. * * @return string */ function render( $post_id = false, $enqueue_css = true, $panels_data = false, & $layout_data = array(), $is_preview = false ) { if ( empty( $post_id ) ) { $post_id = get_the_ID(); if ( class_exists( 'WooCommerce' ) && is_shop() ) { $post_id = wc_get_page_id( 'shop' ); } } global $siteorigin_panels_current_post; // If $panels_data is empty, and the current post being processed is the same as the last one, don't process it. if ( empty( $panels_data ) && ! empty( $siteorigin_panels_current_post ) && apply_filters( 'siteorigin_panels_renderer_current_post_check', true ) && $siteorigin_panels_current_post == $post_id ) { trigger_error( __( 'Prevented SiteOrigin layout from repeated rendering.', 'siteorigin-panels' ) ); return; } $old_current_post = $siteorigin_panels_current_post; $siteorigin_panels_current_post = $post_id; // Try get the cached panel from in memory cache. global $siteorigin_panels_cache; if ( ! empty( $siteorigin_panels_cache ) && ! empty( $siteorigin_panels_cache[ $post_id ] ) ) { return $siteorigin_panels_cache[ $post_id ]; } if ( empty( $panels_data ) ) { $panels_data = $this->get_panels_data_for_post( $post_id ); if ( $panels_data === false ) { return false; } } $panels_data = apply_filters( 'siteorigin_panels_data', $panels_data, $post_id ); if ( empty( $panels_data ) || empty( $panels_data['grids'] ) ) { return ''; } if ( empty( $this->container ) ) { $this->container = SiteOrigin_Panels::container_settings(); } if ( $is_preview ) { $GLOBALS[ 'SITEORIGIN_PANELS_PREVIEW_RENDER' ] = true; } $layout_data = $this->get_panels_layout_data( $panels_data ); $layout_data = apply_filters( 'siteorigin_panels_layout_data', $layout_data, $post_id ); ob_start(); // Add the panel layout wrapper $layout_classes = apply_filters( 'siteorigin_panels_layout_classes', array( 'panel-layout' ), $post_id, $panels_data ); if ( is_rtl() ) { $layout_classes[] = 'panel-is-rtl'; } $layout_attributes = apply_filters( 'siteorigin_panels_layout_attributes', array( 'id' => 'pl-' . $post_id, 'class' => implode( ' ', $layout_classes ), ), $post_id, $panels_data ); $this->render_element( 'div', $layout_attributes ); echo apply_filters( 'siteorigin_panels_before_content', '', $panels_data, $post_id ); foreach ( $layout_data as $ri => & $row ) { if ( apply_filters( 'siteorigin_panels_output_row', true, $row, $ri, $panels_data, $post_id ) ) { $this->render_row( $post_id, $ri, $row, $panels_data ); } } echo apply_filters( 'siteorigin_panels_after_content', '', $panels_data, $post_id ); echo ''; do_action( 'siteorigin_panels_after_render', $panels_data, $post_id ); $html = ob_get_clean(); if ( $enqueue_css && ! isset( $this->inline_css[ $post_id ] ) ) { wp_enqueue_style( 'siteorigin-panels-front' ); $this->add_inline_css( $post_id, $this->generate_css( $post_id, $panels_data, $layout_data ) ); } // Reset the current post $siteorigin_panels_current_post = $old_current_post; $rendered_layout = apply_filters( 'siteorigin_panels_render', $html, $post_id, ! empty( $post ) ? $post : null ); if ( $is_preview ) { $widget_css = '@import url(' . SiteOrigin_Panels::front_css_url() . '); '; $widget_css .= SiteOrigin_Panels::renderer()->generate_css( $post_id, $panels_data, $layout_data ); $widget_css = preg_replace( '/\s+/', ' ', $widget_css ); $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; $rendered_layout .= "\n\n" . ""; } unset( $GLOBALS[ 'SITEORIGIN_PANELS_PREVIEW_RENDER' ] ); return $rendered_layout; } /** * Echo the style wrapper and return if there was a wrapper * * @param string $name The name of the style wrapper * @param array $style The style wrapper args. Used as an argument for siteorigin_panels_{$name}_style_attributes * @param string|bool $for An identifier of what this style wrapper is for * * @return bool Is there a style wrapper */ private function start_style_wrapper( $name, $style = array(), $for = false ) { $attributes = array(); if ( empty( $attributes['class'] ) ) { $attributes['class'] = array(); } if ( empty( $attributes['style'] ) ) { $attributes['style'] = ''; } // Get everything related to the style wrapper $attributes = apply_filters( 'siteorigin_panels_' . $name . '_style_attributes', $attributes, $style ); $attributes = apply_filters( 'siteorigin_panels_general_style_attributes', $attributes, $style ); $standard_css = array(); $standard_css = apply_filters( 'siteorigin_panels_' . $name . '_style_css', $standard_css, $style ); $standard_css = apply_filters( 'siteorigin_panels_general_style_css', $standard_css, $style ); $tablet_css = array(); $tablet_css = siteorigin_panels_setting( 'tablet-layout' ) ? apply_filters( 'siteorigin_panels_' . $name . '_style_tablet_css', $tablet_css, $style ) : ''; $tablet_css = apply_filters( 'siteorigin_panels_general_style_tablet_css', $tablet_css, $style ); $mobile_css = array(); $mobile_css = apply_filters( 'siteorigin_panels_' . $name . '_style_mobile_css', $mobile_css, $style ); $mobile_css = apply_filters( 'siteorigin_panels_general_style_mobile_css', $mobile_css, $style ); // Remove anything we didn't actually use if ( empty( $attributes['class'] ) ) { unset( $attributes['class'] ); } if ( empty( $attributes['style'] ) ) { unset( $attributes['style'] ); } $style_wrapper = ''; if ( ! empty( $attributes ) || ! empty( $standard_css ) || ! empty( $tablet_css )|| ! empty( $mobile_css ) ) { if ( empty( $attributes['class'] ) ) { $attributes['class'] = array(); } $attributes['class'][] = 'panel-' . $name . '-style'; if ( ! empty( $for ) ) { $attributes['class'][] = 'panel-' . $name . '-style-for-' . sanitize_html_class( $for ); } $attributes['class'] = array_unique( $attributes['class'] ); // Filter and sanitize the classes $attributes['class'] = apply_filters( 'siteorigin_panels_' . $name . '_style_classes', $attributes['class'], $attributes, $style ); $attributes['class'] = array_map( 'sanitize_html_class', $attributes['class'] ); $style_wrapper = '