Elementor Errore Fatale: Classe 'Elementor\Widget_Base' non trovata

17 mag 2018, 04:11:24
Visualizzazioni: 15.5K
Voti: 5

Sto cercando di creare un widget di base in Elementor. Quando seguo la documentazione per sviluppatori per creare un nuovo widget, viene generato il seguente errore:

Fatal error: Class 'Elementor\Widget_Base' not found

Ecco il mio codice (principalmente copiato dalla documentazione ufficiale)

Classe principale dell'estensione del plugin:

<?php
/**
 * Plugin Name: Deo Elementor Extension
 * Description: Estensione personalizzata per Elementor.
 * Plugin URI:  https://elementor.com/
 * Version:     1.0.0
 * Author:      DeoThemes
 * Author URI:  https://elementor.com/
 * Text Domain: deo-elementor-extension
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Esci se accesso diretto
}

/**
 * Classe principale dell'estensione di test per Elementor
 *
 * La classe principale che avvia e gestisce il plugin.
 *
 * @since 1.0.0
 */
final class Elementor_Test_Extension {

    /**
     * Versione del plugin
     *
     * @since 1.0.0
     *
     * @var string La versione del plugin.
     */
    const VERSION = '1.0.0';

    /**
     * Versione minima di Elementor
     *
     * @since 1.0.0
     *
     * @var string Versione minima di Elementor richiesta per eseguire il plugin.
     */
    const MINIMUM_ELEMENTOR_VERSION = '2.0.0';

    /**
     * Versione minima di PHP
     *
     * @since 1.0.0
     *
     * @var string Versione minima di PHP richiesta per eseguire il plugin.
     */
    const MINIMUM_PHP_VERSION = '7.0';

    /**
     * Istanza
     *
     * @since 1.0.0
     *
     * @access private
     * @static
     *
     * @var Elementor_Test_Extension L'istanza singola della classe.
     */
    private static $_instance = null;

    /**
     * Istanza
     *
     * Assicura che solo un'istanza della classe venga caricata o possa essere caricata.
     *
     * @since 1.0.0
     *
     * @access public
     * @static
     *
     * @return Elementor_Test_Extension Un'istanza della classe.
     */
    public static function instance() {

        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;

    }

    /**
     * Costruttore
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function __construct() {

        add_action( 'init', [ $this, 'i18n' ] );
        add_action( 'plugins_loaded', [ $this, 'init' ] );

    }

    /**
     * Carica il dominio di testo
     *
     * Carica i file di localizzazione del plugin.
     *
     * Attivato dall'hook `init`.
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function i18n() {

        load_plugin_textdomain( 'elementor-test-extension' );

    }

    /**
     * Inizializza il plugin
     *
     * Carica il plugin solo dopo che Elementor (e altri plugin) sono stati caricati.
     * Controlla i requisiti di base del plugin, se un controllo fallisce non continua,
     * se tutti i controlli hanno superato carica i file necessari per eseguire il plugin.
     *
     * Attivato dall'hook `plugins_loaded`.
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function init() {

        // Controlla se Elementor è installato e attivato
        if ( ! did_action( 'elementor/loaded' ) ) {
            add_action( 'admin_notices', [ $this, 'admin_notice_missing_main_plugin' ] );
            return;
        }

        // Controlla la versione richiesta di Elementor
        if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) {
            add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] );
            return;
        }

        // Controlla la versione richiesta di PHP
        if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
            add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
            return;
        }

        // Includi i file del plugin
        $this->includes();

        // Registra i widget
        add_action( 'elementor/widgets/widgets_registered', [ $this, 'register_widgets' ] );

    }

    /**
     * Avviso di amministrazione
     *
     * Avviso quando il sito non ha Elementor installato o attivato.
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function admin_notice_missing_main_plugin() {

        if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );

        $message = sprintf(
            /* traduttori: 1: Nome del plugin 2: Elementor */
            esc_html__( '"%1$s" richiede "%2$s" per essere installato e attivato.', 'elementor-test-extension' ),
            '<strong>' . esc_html__( 'Elementor Test Extension', 'elementor-test-extension' ) . '</strong>',
            '<strong>' . esc_html__( 'Elementor', 'elementor-test-extension' ) . '</strong>'
        );

        printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );

    }

    /**
     * Avviso di amministrazione
     *
     * Avviso quando il sito non ha la versione minima richiesta di Elementor.
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function admin_notice_minimum_elementor_version() {

        if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );

        $message = sprintf(
            /* traduttori: 1: Nome del plugin 2: Elementor 3: Versione richiesta di Elementor */
            esc_html__( '"%1$s" richiede la versione %3$s o superiore di "%2$s".', 'elementor-test-extension' ),
            '<strong>' . esc_html__( 'Elementor Test Extension', 'elementor-test-extension' ) . '</strong>',
            '<strong>' . esc_html__( 'Elementor', 'elementor-test-extension' ) . '</strong>',
            self::MINIMUM_ELEMENTOR_VERSION
        );

        printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );

    }

    /**
     * Avviso di amministrazione
     *
     * Avviso quando il sito non ha la versione minima richiesta di PHP.
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function admin_notice_minimum_php_version() {

        if ( isset( $_GET['activate'] ) ) unset( $_GET['activate'] );

        $message = sprintf(
            /* traduttori: 1: Nome del plugin 2: PHP 3: Versione richiesta di PHP */
            esc_html__( '"%1$s" richiede la versione %3$s o superiore di "%2$s".', 'elementor-test-extension' ),
            '<strong>' . esc_html__( 'Elementor Test Extension', 'elementor-test-extension' ) . '</strong>',
            '<strong>' . esc_html__( 'PHP', 'elementor-test-extension' ) . '</strong>',
            self::MINIMUM_PHP_VERSION
        );

        printf( '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message );

    }

    /**
     * Includi file
     *
     * Carica i file core richiesti dal plugin.
     *
     * @since 1.0.0
     *
     * @access public
     */
    public function includes() {

        require_once( __DIR__ . '/widgets/test-widget.php' );
        //require_once( __DIR__ . '/controls/test-control.php' );

    }

    public function register_widgets() {

        \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_oEmbed_Widget() );

    }

}

Elementor_Test_Extension::instance();

Classe del widget di test:

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Esci se accesso diretto
}

class Elementor_oEmbed_Widget extends \Elementor\Widget_Base {

    /**
     * Ottieni il nome del widget.
     *
     * Recupera il nome del widget oEmbed.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Nome del widget.
     */
    public function get_name() {
        return 'oembed';
    }

    /**
     * Ottieni il titolo del widget.
     *
     * Recupera il titolo del widget oEmbed.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Titolo del widget.
     */
    public function get_title() {
        return __( 'oEmbed', 'plugin-name' );
    }

    /**
     * Ottieni l'icona del widget.
     *
     * Recupera l'icona del widget oEmbed.
     *
     * @since 1.0.0
     * @access public
     *
     * @return string Icona del widget.
     */
    public function get_icon() {
        return 'fa fa-code';
    }

    /**
     * Ottieni le categorie del widget.
     *
     * Recupera l'elenco delle categorie a cui appartiene il widget oEmbed.
     *
     * @since 1.0.0
     * @access public
     *
     * @return array Categorie del widget.
     */
    public function get_categories() {
        return [ 'general' ];
    }

    /**
     * Registra i controlli del widget oEmbed.
     *
     * Aggiunge diversi campi di input per consentire all'utente di modificare e personalizzare le impostazioni del widget.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function _register_controls() {

        $this->start_controls_section(
            'content_section',
            [
                'label' => __( 'Contenuto', 'plugin-name' ),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'url',
            [
                'label' => __( 'URL da incorporare', 'plugin-name' ),
                'type' => \Elementor\Controls_Manager::TEXT,
                'input_type' => 'url',
                'placeholder' => __( 'https://your-link.com', 'plugin-name' ),
            ]
        );

        $this->end_controls_section();

    }

    /**
     * Renderizza l'output del widget oEmbed sul frontend.
     *
     * Scritto in PHP e utilizzato per generare l'HTML finale.
     *
     * @since 1.0.0
     * @access protected
     */
    protected function render() {

        $settings = $this->get_settings_for_display();

        $html = wp_oembed_get( $settings['url'] );

        echo '<div class="oembed-elementor-widget">';

        echo ( $html ) ? $html : $settings['url'];

        echo '</div>';

    }

}
0
Tutte le risposte alla domanda 2
1

Ho avuto lo stesso problema. Sposta $this->includes(); fuori dal metodo init e inseriscilo nel metodo register_widgets:

public function init() {

   ...

   // Includi i file del plugin
   // $this->includes(); // <-- rimuovi questa riga

}

public function register_widgets() {

    $this->includes(); // <- inseriscilo qui
    \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Elementor_oEmbed_Widget() );

}
21 mag 2018 07:57:58
Commenti

Grazie mille Frank! È esattamente quello che ho scoperto ieri. Ho creato un metodo separato e ci ho inserito $this->includes e ha funzionato. Ho pubblicato il problema sul repository Github di Elementor. Non capisco perché altri abbiano votato negativamente questo post, c'è chiaramente qualcosa di sbagliato nel codice della documentazione o nel core. Ma il repository Github è al momento disabilitato a causa del DMCA :) Comunque la tua risposta è corretta.

Alexander Alexander
21 mag 2018 08:33:30
1

vai a /var/www/wordpress/wp-content/themes/ReservoirLabs_v2

e apri il file functions.php

e commenta require( get_template_directory()

20 mag 2020 22:12:58
Commenti

Non significherebbe modificare i file del tema principale ReservoirLabs_v2 e quindi non poter aggiornare il core senza perdere le modifiche, nel migliore dei casi, e potenzialmente compromettere altre funzionalità nel peggiore? Per favore cerca anche di includere una spiegazione sul perché la tua risposta funziona e cosa sta cercando di ottenere.

Tony Djukic Tony Djukic
20 mag 2020 23:11:10