Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Created August 28, 2024 11:26
Show Gist options
  • Save Crocoblock/ed5054b0ee02c3048a22d86e4e7dff18 to your computer and use it in GitHub Desktop.
Save Crocoblock/ed5054b0ee02c3048a22d86e4e7dff18 to your computer and use it in GitHub Desktop.
JetEngine Output component with PHP
<?php
class JE_Component_Shortcode {
public function __construct() {
add_action( 'init', array( $this, 'init' ), 100 );
}
public function init() {
if ( ! function_exists( 'jet_engine' ) ) {
return;
}
add_shortcode( 'je_component', array( $this, 'render_component' ) );
}
public function render_component( $attrs ) {
$component_id = $attrs['component_id'] ?? 0;
unset( $attrs['component_id'] );
if ( ! $component_id ) {
return;
}
$component = jet_engine()->listings->components->get( $component_id, 'id' );
if ( ! $component ) {
return;
}
$component->set_component_context( $attrs['component_context'] ?? 'default_object' );
$content = $component->get_content( $attrs, false );
if ( 'elementor' === $component->get_render_view() ) {
return $content;
} else {
if ( ! empty( $attrs['__globals__'] ) ) {
$attrs = array_merge( $attrs, \Jet_Engine\Elementor_Views\Components\Document::get_global_values( [
'control_type' => 'color',
], $attrs['__globals__'] ) );
}
return sprintf(
'<div class="jet-listing-grid--%1$s" style="%2$s">%3$s</div>',
$component->get_id(),
$component->css_variables_string( $attrs ),
$content
);
}
}
}
new JE_Component_Shortcode();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment