| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Integration class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers blocks as Elementor Widgets. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Elementor { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 1.9.7.2 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Enqueue CSS when using the Elementor editor. |
| 25 |
add_action( 'elementor/editor/after_enqueue_styles', array( $this, 'enqueue_styles' ) ); |
| 26 |
|
| 27 |
// Register Widget Category. |
| 28 |
add_action( 'elementor/elements/categories_registered', array( $this, 'register_elementor_widget_categories' ) ); |
| 29 |
|
| 30 |
// Register Blocks as Elementor Widgets. |
| 31 |
add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) ); |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue styles for widget icons. |
| 37 |
* |
| 38 |
* @since 1.9.7.2 |
| 39 |
*/ |
| 40 |
public function enqueue_styles() { |
| 41 |
|
| 42 |
// Don't load stylesheets if not in editor mode. |
| 43 |
if ( ! filter_has_var( INPUT_GET, 'action' ) || filter_input( INPUT_GET, 'action', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) !== 'elementor' ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
// Enqueue styles for block icons. |
| 48 |
wp_enqueue_style( |
| 49 |
'convertkit-elementor', |
| 50 |
CONVERTKIT_PLUGIN_URL . 'resources/backend/css/elementor.css', |
| 51 |
array(), |
| 52 |
CONVERTKIT_PLUGIN_VERSION |
| 53 |
); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers this Plugin's Name as a Category for Elementor Widgets registered |
| 59 |
* by this Plugin. |
| 60 |
* |
| 61 |
* @since 1.9.7.2 |
| 62 |
* |
| 63 |
* @param object $elements_manager Elements Manager. |
| 64 |
*/ |
| 65 |
public function register_elementor_widget_categories( $elements_manager ) { |
| 66 |
|
| 67 |
$elements_manager->add_category( |
| 68 |
'convertkit', |
| 69 |
array( |
| 70 |
'title' => __( 'Kit', 'convertkit' ), |
| 71 |
'icon' => 'fa fa-plug', |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Registers Blocks as Elementor Widgets. |
| 79 |
* |
| 80 |
* @since 1.9.7.2 |
| 81 |
* |
| 82 |
* @param Elementor\Widgets_Manager $widgets_manager Widgets Manager, used to register/unregister Elementor Widgets. |
| 83 |
*/ |
| 84 |
public function register_widgets( $widgets_manager ) { |
| 85 |
|
| 86 |
// Get blocks. |
| 87 |
$blocks = convertkit_get_blocks(); |
| 88 |
|
| 89 |
// Bail if no blocks are available. |
| 90 |
if ( ! count( $blocks ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Load widget class here, as we know that Elementor is active. |
| 95 |
require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/elementor/class-convertkit-elementor-widget.php'; |
| 96 |
|
| 97 |
// Iterate through blocks, registering them. |
| 98 |
foreach ( $blocks as $block => $properties ) { |
| 99 |
// Skip if this block doesn't have an Elementor Widget class. |
| 100 |
if ( ! file_exists( CONVERTKIT_PLUGIN_PATH . '/includes/integrations/elementor/class-convertkit-elementor-widget-' . $block . '.php' ) ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
// Load widget class for this block. |
| 105 |
require_once CONVERTKIT_PLUGIN_PATH . '/includes/integrations/elementor/class-convertkit-elementor-widget-' . $block . '.php'; |
| 106 |
$class_name = 'ConvertKit_Elementor_Widget_' . str_replace( '-', '_', $block ); |
| 107 |
|
| 108 |
// Register Widget, using applicable function depending on the Elementor version. |
| 109 |
if ( method_exists( $widgets_manager, 'register' ) ) { // @phpstan-ignore-line - older versions of Elementor don't have this method. |
| 110 |
// Use register() function, available in Elementor 3.5.0. |
| 111 |
// Required per https://developers.elementor.com/docs/managers/registering-widgets/. |
| 112 |
$widgets_manager->register( new $class_name() ); |
| 113 |
} else { |
| 114 |
// Fallback to register_widget_type(), available in Elementor < 3.5.0. |
| 115 |
$widgets_manager->register_widget_type( new $class_name() ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|