Created
May 17, 2019 11:41
-
-
Save danyj/317e0fdc9e18d2857f331245f2cedd1d to your computer and use it in GitHub Desktop.
Pull any Elementor options outside of Elementor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @package Thz Framework | |
* @author Themezly | |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only | |
* @websites http://www.themezly.com | http://www.youjoomla.com | http://www.yjsimplegrid.com | |
*/ | |
class ThzelGetElementSettings { | |
public function __construct( $postid, $widget_id, $widget_type ) { | |
$this->postid = $postid; | |
$this->widget_id = $widget_id; | |
$this->widget_type = $widget_type; | |
$this->widget = null; | |
$this->parse(); | |
} | |
public function elementor(){ | |
return \Elementor\Plugin::$instance; | |
} | |
public function get_settings () { | |
$widget = $this->elementor()->elements_manager->create_element_instance( $this->widget ); | |
return $widget->get_settings_for_display(); | |
} | |
private function parse() { | |
$data = $this->read_data(); | |
$this->parse_options($data); | |
} | |
private function read_data () { | |
return $this->elementor()->documents->get( $this->postid )->get_elements_data(); | |
} | |
private function parse_options($data) { | |
if(!is_array($data) || empty($data)){ | |
return; | |
} | |
foreach ( $data as $item ) { | |
if(empty($item)){ | |
continue; | |
} | |
if ( 'section' === $item['elType'] || 'column' === $item['elType']) { | |
$this->parse_options($item['elements']); | |
} else { | |
$this->parse_options_simple($item); | |
} | |
} | |
} | |
private function parse_options_simple($item) { | |
if ( | |
$item['id'] === $this->widget_id && | |
$item['widgetType'] === $this->widget_type | |
) { | |
$this->widget = $item; | |
} | |
} | |
} |
Thanks!
Updated code to include dynamic field data
use Elementor\Plugin; use Elementor\Utils; /** * Class ElementorWidgetSettings * * This class is responsible for retrieving and managing settings of Elementor widgets. */ class ElementorWidgetSettings { /** * The post ID where the Elementor widget is located. * * @var int */ public int $post_id; /** * The ID of the Elementor widget. * * @var string */ public string $widget_id; /** * The data of the widget retrieved from Elementor. * * @var mixed */ public mixed $widget_data = null; /** * The default settings for the widget if none are found in the document. * * @var array */ public array $default_settings = array(); /** * ElementorWidgetSettings constructor. * * Initializes the class with the post ID, widget ID, and default settings. It also fetches the widget data. * * @param int $post_id The ID of the post/page. * @param string $widget_id The ID of the widget. * @param array $default_settings The default settings for the widget. */ public function __construct( int $post_id, string $widget_id, array $default_settings ) { // Initialize class properties. $this->post_id = $post_id; $this->widget_id = $widget_id; $this->default_settings = $default_settings; // Fetch the widget data. $this->set_widget_data(); } /** * Retrieve the Elementor Plugin instance. * * @return Plugin */ public function elementor(): Plugin { return Plugin::$instance; } /** * Set widget data for the given post and widget ID. * * This method fetches the Elementor document and finds the specific widget data within it. */ public function set_widget_data(): void { // Get the Elementor document for the specified post. $document = $this->elementor()->documents->get( $this->post_id ); if ( $document ) { // Find widget data recursively in the document. $widget_data = Utils::find_element_recursive( $document->get_elements_data( 'draft' ), $this->widget_id ); // Set widget data if found. if ( $widget_data ) { $this->widget_data = $widget_data; } } } /** * Retrieve the settings of the widget for display. * * This method switches to the specified post, retrieves the widget's settings, and restores the post context. * * @return array The settings of the widget or the default settings if widget data is not found. */ public function get_settings(): array { // Return default settings if no widget data is set. if ( ! $this->widget_data ) { return $this->default_settings; } // Switch to the post context for the Elementor document. $this->elementor()->db->switch_to_post( $this->post_id ); // Create the widget instance and retrieve its settings. $widget = $this->elementor()->elements_manager->create_element_instance( $this->widget_data ); // Get settings prepared for display. $settings = $widget->get_settings_for_display(); // Restore the original post context. $this->elementor()->db->restore_current_post(); return is_array( $settings ) ? $settings : $this->default_settings; } } $post_id = 6; $widget_id = '6bf487'; $default_settings = array(); // default settings incase no data this data will return in get_settings(); $get_settings = new ElementorWidgetSettings( $post_id, $widget_id, $default_settings ); $settings = $get_settings->get_settings();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage