File "class-element.php"
Full path: /home/dora/public_html/wp-content/plugins/wp-grid-builder-bricks/includes/class-element.php
File size: 3.2 KB
MIME-type: --
Charset: utf-8
<?php
/**
* Element
*
* @package WP Grid Builder - Bricks
* @author Loïc Blascos
* @copyright 2019-2022 Loïc Blascos
*/
namespace WP_Grid_Builder_Bricks\Includes;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Element
*
* @class WP_Grid_Builder_Bricks\Includes\Element
* @since 1.0.0
*/
final class Element {
use Helpers;
/**
* Holds template attributes
*
* @since 1.0.0
* @access protected
* @var array
*/
protected $template = [];
/**
* Holds elements to render
*
* @since 1.0.0
* @access protected
* @var array
*/
protected $elements = [];
/**
* Constructor
*
* @since 1.0.0
* @access public
*
* @param array $template Holds template attributes.
*/
public function __construct( $template ) {
$this->template = $template;
}
/**
* Parse Bricks elements to match the one filterable
*
* @since 1.0.0
* @access public
*
* @return string
*/
public function get_content() {
$content = '';
$this->get_elements();
foreach ( $this->elements as $element ) {
$content = $this->get_element( $element );
if ( ! empty( $content ) ) {
break;
}
}
return $content;
}
/**
* Get content from Bricks
*
* @since 1.0.0
* @access public
*
* @return array
*/
public function get_elements() {
// If we render facets in the editor.
if ( ! empty( $this->template['elements'] ) ) {
$this->elements = $this->template['elements'];
return;
}
if ( empty( $this->template['post_id'] ) ) {
return;
}
foreach ( $this->template['post_id'] as $post_id ) {
$this->elements = array_merge(
$this->elements,
$this->get_bricks_data( $post_id )
);
}
$this->elements = array_filter( $this->elements );
}
/**
* Get filterable element
*
* @since 1.0.0
* @access public
*
* @param array $element Holds element attributes.
* @return array
*/
public function get_element( $element ) {
if ( ! isset( $element['id'], $element['settings'] ) ) {
return '';
}
if ( 'bricks-element-' . $element['id'] !== $this->template['id'] ) {
return '';
}
// We setup element to filter to handle it correctly.
apply_filters( 'wp_grid_builder_bricks/setup_element', $this->template['id'] );
$element['settings'] = array_merge( $element['settings'], $this->template );
// Render only the element without parent context.
unset( $element['parent'] );
$elements = array_merge( [ $element ], $this->get_children( $element ) );
// Not properly rendered because of bricks_is_builder_call() that returns true for any Ajax or REST API request.
return \Bricks\Frontend::render_data( array_filter( $elements ) );
}
/**
* Get children element from parent element
*
* @since 1.0.0
* @access public
*
* @param array $parent Holds parent element attributes.
* @return array
*/
public function get_children( $parent ) {
$children = [];
if ( empty( $parent['children'] ) ) {
return $children;
}
foreach ( $this->elements as $element ) {
if ( in_array( $element['id'], $parent['children'], true ) ) {
$children[] = $element;
$children = array_merge( $children, $this->get_children( $element ) );
}
}
return $children;
}
}