Registering elements in PHP

Plugin.php file should have the following structure to load the php template files of the newly added elements

namespace MyPlugin;

use MyPlugin\Editor;
use MyPlugin\Frontend;
// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    return;
}
class Plugin {
    
    public static $instance = null;
    
    private $version = null;
    
    private $project_root_path = null;
   
    private $project_root_url = null;

    public $plugin_data = [];

    public $plugin_file = null;

    public function __construct( $path ) {
        $this->plugin_file       = $path;
        $this->project_root_path = trailingslashit( dirname( $path ) );
        $this->project_root_url  = plugin_dir_url( $path );
        $this->plugin_data       = get_plugin_data( $path );
        $this->version           = $this->plugin_data['Version'];

        self::$instance = $this;
    }

    public function register_elements( $elements_manager ) {      
        $default_elements = [
            'MyElement',            
        ];

        foreach ( $default_elements as $element_name ) {
            $file_path = Utils::get_file_path( 'includes/Elements/' . trailingslashit( $element_name ) . $element_name . '.php' );

            if ( is_file( $file_path ) ) {
                include $file_path;

                // Normalize class name
                $class_name = str_replace( '-', '_', $element_name );
                $class_name = __NAMESPACE__ . '\\Elements\\' . $class_name;
                $elements_manager->register_element( new $class_name() );
            }
        }
    }   
    public $elements_manager = null;
    
    public function add_elements_categories( $categories ) {
        $my_plugin_category = [
          [
             'id'       => 'category_id',
             'name'     => __( 'My Category', 'text-domain' ),
             'priority' => 10,
          ],
        ]

        return array_merge( $categories, $my_plugin_category  );
    }

    public function init() {
        add_filter( 'zionbuilder/elements/categories', [ $this, 'add_elements_categories' ] );
        add_action( 'zionbuilder/elements_manager/register_elements', [ $this, 'register_elements' ] );
        add_action( 'zionbuilder/editor/before_scripts', [ $this, 'enqueue_scripts' ] );

        $this->editor                  = new Editor();
        $this->frontend                = new Frontend();
    }   
    public function __clone() {
        // Cloning instances of the class is forbidden.
        _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'text-domain' ), '2.0.0' );
    }
    
    public function __wakeup() {
        _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'text-domain' ), '2.0.0' );
    }
    
    public static function instance() {
        return self::$instance;
    }

    public function enqueue_scripts() {
        wp_localize_script(
            'my-plugin-companion-elements-script',
            'MyPluginRestConfig',
            [
                'nonce'     => wp_create_nonce( 'wp_rest' ),
                'rest_root' => esc_url_raw( rest_url() ),
            ]
        );
    }
    public function get_root_path() {
        return $this->project_root_path;
    }
    
    public function get_plugin_file() {
        return $this->plugin_file;
    }

    public function get_root_url() {
        return $this->project_root_url;
    }

    public function get_version() {
        return $this->version;
    }
    
    public function get_plugin_data( $type ) {
        if ( isset( $this->plugin_data[$type] ) ) {
            return $this->plugin_data[$type];
        }

        return null;
    }
}

Was this article helpful ?

100 people consider this article helpful
Thank you! We will improve this article.

Didn’t find what you’re looking for? Please let us know

Submit a ticket