Elementor 开发者文档

title: "Deprecated Function" post_status: publish comment_status: open taxonomy: category: - elementor-developers-docs post_tag: - Deprecations - Src - Repos


Deprecated Function

Elementor offers the Deprecation::deprecated_function() method to handle the deprecation process for functions and methods inside Elementor code.

Use Cases

Previously, Elementor protected methods by using the _ prefix. However, this convention is outdated. For instance, _register_controls() would now just be register_controls().

Unifying components is another example of this change. Elementor 3.5 renamed many methods in order to unify the registration process. This entailed using managers, renaming register_widget_type(), register_control(), register_tag(), add_category(), and others to simply register().

How To Deprecate Functions

In the following example we need to rename the add_item() method and replace it with register().

For example, take the following code:

class Items {

    function add_item( $item_instance ): void {
        /* ... */
    }

}

The new code, with the deprecation handler, will look like this:

use Elementor\Plugin;

class Items {

    function register( $item_instance ): void {
        /* ... */
    }

    function add_item( $item_instance ): void {
        // Deprecation handler
        Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function(
            'add_item()',
            '3.5.0',
            'register()'
        );

        // Backwards compatibility
        $this->register( $item_instance );
    }

}

The original method was renamed to register(). But, we didn't remove the add_item() method, we simply added a deprecation handler to inform developers still using this method to replace it with the new name.

After eight major versions, the code will look like this:

class Items {

    function register( $item_instance ): void {
        /* ... */
    }

}

Updating Deprecated Functions

Addon developers who use the old add_item() method should simply rename the method to register(), as follows:

class MyItem extends Items {

-   protected function add_item(): void {
+   protected function register(): void {
        /* ... */
    }

}