| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Elementor Integration Manager |
| 5 |
* |
| 6 |
* Registers ThinkRank's Elementor widgets (FAQ, HowTo, Table of Contents) so |
| 7 |
* Elementor-built pages get the same content patterns and structured data as |
| 8 |
* the Gutenberg blocks. Notably, RankMath ships no such widgets — Elementor |
| 9 |
* users there get schema only by decorating Elementor's own accordion. |
| 10 |
* |
| 11 |
* All hooks are Elementor-fired: when Elementor is not active, none of this |
| 12 |
* runs and no Elementor classes are referenced. |
| 13 |
* |
| 14 |
* @package ThinkRank |
| 15 |
* @subpackage Editor |
| 16 |
* @since 1.18.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
declare(strict_types=1); |
| 20 |
|
| 21 |
namespace ThinkRank\Editor; |
| 22 |
|
| 23 |
// Prevent direct access |
| 24 |
if (!defined('ABSPATH')) { |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Elementor Manager. |
| 30 |
* |
| 31 |
* @since 1.18.0 |
| 32 |
*/ |
| 33 |
class Elementor_Manager { |
| 34 |
|
| 35 |
/** |
| 36 |
* Wire up hooks. Safe without Elementor — the hooks simply never fire. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function init(): void { |
| 41 |
add_action('elementor/elements/categories_registered', [$this, 'register_category']); |
| 42 |
add_action('elementor/widgets/register', [$this, 'register_widgets']); |
| 43 |
add_action('wp_enqueue_scripts', [$this, 'register_widget_styles']); |
| 44 |
add_action('elementor/editor/after_enqueue_styles', [$this, 'register_widget_styles']); |
| 45 |
add_action('elementor/preview/enqueue_styles', [$this, 'enqueue_preview_styles']); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Add the ThinkRank widget category. |
| 50 |
* |
| 51 |
* @param object $elements_manager Elementor elements manager. |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
public function register_category($elements_manager): void { |
| 55 |
$elements_manager->add_category('thinkrank', [ |
| 56 |
'title' => __('ThinkRank', 'thinkrank'), |
| 57 |
'icon' => 'eicon-search', |
| 58 |
]); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Register the widgets. |
| 63 |
* |
| 64 |
* @param object $widgets_manager Elementor widgets manager. |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function register_widgets($widgets_manager): void { |
| 68 |
$widgets_manager->register(new Elementor\FAQ_Widget()); |
| 69 |
$widgets_manager->register(new Elementor\Howto_Widget()); |
| 70 |
$widgets_manager->register(new Elementor\TOC_Widget()); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Register (not enqueue) the shared block stylesheets so widgets can |
| 75 |
* declare them via get_style_depends() — Elementor then loads them only |
| 76 |
* on pages that actually use the widget. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function register_widget_styles(): void { |
| 81 |
foreach (['faq-block', 'howto-block', 'toc-block'] as $handle) { |
| 82 |
$css = THINKRANK_PLUGIN_DIR . "assets/{$handle}.css"; |
| 83 |
if (!file_exists($css)) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
$asset_path = THINKRANK_PLUGIN_DIR . "assets/{$handle}.asset.php"; |
| 88 |
$asset = file_exists($asset_path) ? include $asset_path : []; |
| 89 |
|
| 90 |
wp_register_style( |
| 91 |
"thinkrank-{$handle}", |
| 92 |
THINKRANK_PLUGIN_URL . "assets/{$handle}.css", |
| 93 |
[], |
| 94 |
$asset['version'] ?? THINKRANK_VERSION |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* In the Elementor editor preview all styles must load up front (the |
| 101 |
* preview doesn't know which widgets will be dropped in). |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function enqueue_preview_styles(): void { |
| 106 |
$this->register_widget_styles(); |
| 107 |
foreach (['faq-block', 'howto-block', 'toc-block'] as $handle) { |
| 108 |
wp_enqueue_style("thinkrank-{$handle}"); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|