Пример #1
0
/**
 * 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.
 * @return string
 */
function siteorigin_panels_render($post_id = false, $enqueue_css = true, $panels_data = false)
{
    if (empty($post_id)) {
        $post_id = get_the_ID();
    }
    global $siteorigin_panels_current_post;
    $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)) {
        if (strpos($post_id, 'prebuilt:') === 0) {
            list($null, $prebuilt_id) = explode(':', $post_id, 2);
            $layouts = apply_filters('siteorigin_panels_prebuilt_layouts', array());
            $panels_data = !empty($layouts[$prebuilt_id]) ? $layouts[$prebuilt_id] : array();
        } else {
            if ($post_id == 'home') {
                $page_id = get_option('page_on_front');
                if (empty($page_id)) {
                    $page_id = get_option('siteorigin_panels_home_page_id');
                }
                $panels_data = !empty($page_id) ? get_post_meta($page_id, 'panels_data', true) : null;
                if (is_null($panels_data)) {
                    // Load the default layout
                    $layouts = apply_filters('siteorigin_panels_prebuilt_layouts', array());
                    $prebuilt_id = siteorigin_panels_setting('home-page-default') ? siteorigin_panels_setting('home-page-default') : 'home';
                    $panels_data = !empty($layouts[$prebuilt_id]) ? $layouts[$prebuilt_id] : current($layouts);
                }
            } else {
                if (post_password_required($post_id)) {
                    return false;
                }
                $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) || empty($panels_data['grids'])) {
        return '';
    }
    // Filter the widgets to add indexes
    if (!empty($panels_data['widgets'])) {
        $last_gi = 0;
        $last_ci = 0;
        $last_wi = 0;
        foreach ($panels_data['widgets'] as $wid => &$widget_info) {
            if ($widget_info['panels_info']['grid'] != $last_gi) {
                $last_gi = $widget_info['panels_info']['grid'];
                $last_ci = 0;
                $last_wi = 0;
            } elseif ($widget_info['panels_info']['cell'] != $last_ci) {
                $last_ci = $widget_info['panels_info']['cell'];
                $last_wi = 0;
            }
            $widget_info['panels_info']['cell_index'] = $last_wi++;
        }
    }
    if (is_rtl()) {
        $panels_data = siteorigin_panels_make_rtl($panels_data);
    }
    // Create the skeleton of the grids
    $grids = array();
    if (!empty($panels_data['grids']) && !empty($panels_data['grids'])) {
        foreach ($panels_data['grids'] as $gi => $grid) {
            $gi = intval($gi);
            $grids[$gi] = array();
            for ($i = 0; $i < $grid['cells']; $i++) {
                $grids[$gi][$i] = array();
            }
        }
    }
    // We need this to migrate from the old $panels_data that put widget meta into the "info" key instead of "panels_info"
    if (!empty($panels_data['widgets']) && is_array($panels_data['widgets'])) {
        foreach ($panels_data['widgets'] as $i => $widget) {
            if (empty($panels_data['widgets'][$i]['panels_info'])) {
                $panels_data['widgets'][$i]['panels_info'] = $panels_data['widgets'][$i]['info'];
                unset($panels_data['widgets'][$i]['info']);
            }
        }
    }
    if (!empty($panels_data['widgets']) && is_array($panels_data['widgets'])) {
        foreach ($panels_data['widgets'] as $widget) {
            // Put the widgets in the grids
            $grids[intval($widget['panels_info']['grid'])][intval($widget['panels_info']['cell'])][] = $widget;
        }
    }
    ob_start();
    // Add the panel layout wrapper
    $panel_layout_classes = apply_filters('siteorigin_panels_layout_classes', array(), $post_id, $panels_data);
    $panel_layout_attributes = apply_filters('siteorigin_panels_layout_attributes', array('class' => implode(' ', $panel_layout_classes), 'id' => 'pl-' . $post_id), $post_id, $panels_data);
    echo '<div';
    foreach ($panel_layout_attributes as $name => $value) {
        if ($value) {
            echo ' ' . $name . '="' . esc_attr($value) . '"';
        }
    }
    echo '>';
    global $siteorigin_panels_inline_css;
    if (empty($siteorigin_panels_inline_css)) {
        $siteorigin_panels_inline_css = array();
    }
    if ($enqueue_css && !isset($siteorigin_panels_inline_css[$post_id])) {
        wp_enqueue_style('siteorigin-panels-front');
        $siteorigin_panels_inline_css[$post_id] = siteorigin_panels_generate_css($post_id, $panels_data);
    }
    echo apply_filters('siteorigin_panels_before_content', '', $panels_data, $post_id);
    foreach ($grids as $gi => $cells) {
        $grid_classes = apply_filters('siteorigin_panels_row_classes', array('panel-grid'), $panels_data['grids'][$gi]);
        $grid_attributes = apply_filters('siteorigin_panels_row_attributes', array('class' => implode(' ', $grid_classes), 'id' => 'pg-' . $post_id . '-' . $gi), $panels_data['grids'][$gi]);
        // This allows other themes and plugins to add html before the row
        echo apply_filters('siteorigin_panels_before_row', '', $panels_data['grids'][$gi], $grid_attributes);
        echo '<div ';
        foreach ($grid_attributes as $name => $value) {
            echo $name . '="' . esc_attr($value) . '" ';
        }
        echo '>';
        $style_attributes = array();
        if (!empty($panels_data['grids'][$gi]['style']['class'])) {
            $style_attributes['class'] = array('panel-row-style-' . $panels_data['grids'][$gi]['style']['class']);
        }
        // Themes can add their own attributes to the style wrapper
        $row_style_wrapper = siteorigin_panels_start_style_wrapper('row', $style_attributes, !empty($panels_data['grids'][$gi]['style']) ? $panels_data['grids'][$gi]['style'] : array());
        if (!empty($row_style_wrapper)) {
            echo $row_style_wrapper;
        }
        foreach ($cells as $ci => $widgets) {
            // Themes can add their own styles to cells
            $cell_classes = apply_filters('siteorigin_panels_row_cell_classes', array('panel-grid-cell'), $panels_data);
            $cell_attributes = apply_filters('siteorigin_panels_row_cell_attributes', array('class' => implode(' ', $cell_classes), 'id' => 'pgc-' . $post_id . '-' . $gi . '-' . $ci), $panels_data);
            echo '<div ';
            foreach ($cell_attributes as $name => $value) {
                echo $name . '="' . esc_attr($value) . '" ';
            }
            echo '>';
            $cell_style_wrapper = siteorigin_panels_start_style_wrapper('cell', array(), !empty($panels_data['grids'][$gi]['style']) ? $panels_data['grids'][$gi]['style'] : array());
            if (!empty($cell_style_wrapper)) {
                echo $cell_style_wrapper;
            }
            foreach ($widgets as $pi => $widget_info) {
                // TODO this wrapper should go in the before/after widget arguments
                $widget_style_wrapper = siteorigin_panels_start_style_wrapper('widget', array(), !empty($widget_info['panels_info']['style']) ? $widget_info['panels_info']['style'] : array());
                siteorigin_panels_the_widget($widget_info['panels_info'], $widget_info, $gi, $ci, $pi, $pi == 0, $pi == count($widgets) - 1, $post_id, $widget_style_wrapper);
            }
            if (empty($widgets)) {
                echo '&nbsp;';
            }
            if (!empty($cell_style_wrapper)) {
                echo '</div>';
            }
            echo '</div>';
        }
        echo '</div>';
        // Close the
        if (!empty($row_style_wrapper)) {
            echo '</div>';
        }
        // This allows other themes and plugins to add html after the row
        echo apply_filters('siteorigin_panels_after_row', '', $panels_data['grids'][$gi], $grid_attributes);
    }
    echo apply_filters('siteorigin_panels_after_content', '', $panels_data, $post_id);
    echo '</div>';
    $html = ob_get_clean();
    // Reset the current post
    $siteorigin_panels_current_post = $old_current_post;
    return apply_filters('siteorigin_panels_render', $html, $post_id, !empty($post) ? $post : null);
}
 /**
  * This is a rewrite of siteorigin_panels_render function in the SiteOrigin Page Builder plugin
  *
  * @return String - HTML of page builder field
  */
 function acf_siteorigin_panels_render($panel_id, $panels_data)
 {
     $GLOBALS['panel_id'] = $panel_id;
     if (is_string($panels_data)) {
         $panels_data = json_decode($panels_data, true);
     }
     if (empty($post_id)) {
         $post_id = get_the_ID();
     }
     $panels_data = apply_filters('siteorigin_panels_data', $panels_data, $post_id, $panel_id);
     if (empty($panels_data) || empty($panels_data['grids'])) {
         return '';
     }
     // Filter the widgets to add indexes
     if (!empty($panels_data['widgets'])) {
         $last_gi = 0;
         $last_ci = 0;
         $last_wi = 0;
         foreach ($panels_data['widgets'] as $wid => &$widget_info) {
             if ($widget_info['panels_info']['grid'] != $last_gi) {
                 $last_gi = $widget_info['panels_info']['grid'];
                 $last_ci = 0;
                 $last_wi = 0;
             } elseif ($widget_info['panels_info']['cell'] != $last_ci) {
                 $last_ci = $widget_info['panels_info']['cell'];
                 $last_wi = 0;
             }
             $widget_info['panels_info']['cell_index'] = $last_wi++;
         }
     }
     if (is_rtl()) {
         $panels_data = siteorigin_panels_make_rtl($panels_data);
     }
     // Create the skeleton of the grids
     $grids = array();
     if (!empty($panels_data['grids']) && !empty($panels_data['grids'])) {
         foreach ($panels_data['grids'] as $gi => $grid) {
             $gi = intval($gi);
             $grids[$gi] = array();
             for ($i = 0; $i < $grid['cells']; $i++) {
                 $grids[$gi][$i] = array();
             }
         }
     }
     // We need this to migrate from the old $panels_data that put widget meta into the "info" key instead of "panels_info"
     if (!empty($panels_data['widgets']) && is_array($panels_data['widgets'])) {
         foreach ($panels_data['widgets'] as $i => $widget) {
             if (empty($panels_data['widgets'][$i]['panels_info'])) {
                 $panels_data['widgets'][$i]['panels_info'] = $panels_data['widgets'][$i]['info'];
                 unset($panels_data['widgets'][$i]['info']);
             }
         }
     }
     if (!empty($panels_data['widgets']) && is_array($panels_data['widgets'])) {
         foreach ($panels_data['widgets'] as $widget) {
             // Put the widgets in the grids
             $grids[intval($widget['panels_info']['grid'])][intval($widget['panels_info']['cell'])][] = $widget;
         }
     }
     ob_start();
     // Add the panel layout wrapper
     $panel_layout_classes = apply_filters('siteorigin_panels_layout_classes', array(), $post_id, $panels_data, $panel_id);
     $panel_layout_attributes = apply_filters('siteorigin_panels_layout_attributes', array('class' => implode(' ', $panel_layout_classes), 'id' => 'pl-' . $post_id), $post_id, $panels_data);
     echo '<div';
     foreach ($panel_layout_attributes as $name => $value) {
         if ($value) {
             echo ' ' . $name . '="' . esc_attr($value) . '"';
         }
     }
     echo '>';
     global $acf_siteorigin_panels_inline_css;
     if (empty($acf_siteorigin_panels_inline_css)) {
         $acf_siteorigin_panels_inline_css = array();
     }
     if (!isset($acf_siteorigin_panels_inline_css[$post_id])) {
         wp_enqueue_style('siteorigin-panels-front');
         $acf_siteorigin_panels_inline_css[$panel_id] = siteorigin_panels_generate_css($post_id, $panels_data);
     }
     $this->page_styles = $acf_siteorigin_panels_inline_css;
     echo apply_filters('siteorigin_panels_before_content', '', $panels_data, $post_id);
     foreach ($grids as $gi => $cells) {
         $grid_classes = apply_filters('siteorigin_panels_row_classes', array('panel-grid'), $panels_data['grids'][$gi]);
         $grid_attributes = apply_filters('siteorigin_panels_row_attributes', array('class' => implode(' ', $grid_classes), 'id' => 'pg-' . $post_id . '-' . $gi), $panels_data['grids'][$gi]);
         // This allows other themes and plugins to add html before the row
         echo apply_filters('siteorigin_panels_before_row', '', $panels_data['grids'][$gi], $grid_attributes);
         echo '<div ';
         foreach ($grid_attributes as $name => $value) {
             echo $name . '="' . esc_attr($value) . '" ';
         }
         echo '>';
         $style_attributes = array();
         if (!empty($panels_data['grids'][$gi]['style']['class'])) {
             $style_attributes['class'] = array('panel-row-style-' . $panels_data['grids'][$gi]['style']['class']);
         }
         // Themes can add their own attributes to the style wrapper
         $row_style_wrapper = siteorigin_panels_start_style_wrapper('row', $style_attributes, !empty($panels_data['grids'][$gi]['style']) ? $panels_data['grids'][$gi]['style'] : array());
         if (!empty($row_style_wrapper)) {
             echo $row_style_wrapper;
         }
         foreach ($cells as $ci => $widgets) {
             // Themes can add their own styles to cells
             $cell_classes = apply_filters('siteorigin_panels_row_cell_classes', array('panel-grid-cell'), $panels_data);
             $cell_attributes = apply_filters('siteorigin_panels_row_cell_attributes', array('class' => implode(' ', $cell_classes), 'id' => 'pgc-' . $post_id . '-' . $gi . '-' . $ci), $panels_data);
             echo '<div ';
             foreach ($cell_attributes as $name => $value) {
                 echo $name . '="' . esc_attr($value) . '" ';
             }
             echo '>';
             $cell_style_wrapper = siteorigin_panels_start_style_wrapper('cell', array(), !empty($panels_data['grids'][$gi]['style']) ? $panels_data['grids'][$gi]['style'] : array());
             if (!empty($cell_style_wrapper)) {
                 echo $cell_style_wrapper;
             }
             $widget_index = 0;
             foreach ($widgets as $pi => $widget_info) {
                 $widget_info['panels_info']['widget_index'] = $widget_index;
                 $widget_index += 1;
                 // TODO this wrapper should go in the before/after widget arguments
                 $widget_style_wrapper = siteorigin_panels_start_style_wrapper('widget', array(), !empty($widget_info['panels_info']['style']) ? $widget_info['panels_info']['style'] : array());
                 siteorigin_panels_the_widget($widget_info['panels_info'], $widget_info, $gi, $ci, $pi, $pi == 0, $pi == count($widgets) - 1, $post_id, $widget_style_wrapper);
             }
             if (empty($widgets)) {
                 echo '&nbsp;';
             }
             if (!empty($cell_style_wrapper)) {
                 echo '</div>';
             }
             echo '</div>';
         }
         echo '</div>';
         // Close the
         if (!empty($row_style_wrapper)) {
             echo '</div>';
         }
         // This allows other themes and plugins to add html after the row
         echo apply_filters('siteorigin_panels_after_row', '', $panels_data['grids'][$gi], $grid_attributes);
     }
     echo apply_filters('siteorigin_panels_after_content', '', $panels_data, $post_id);
     echo '</div>';
     $html = ob_get_clean();
     return apply_filters('siteorigin_panels_render', $html, $post_id, !empty($post) ? $post : null);
 }