CTF_Elementor_Base.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TwitterFeed\Integrations\Elementor; |
| 4 | |
| 5 | use Smashballoon\TwitterFeed\Vendor\Smashballoon\Framework\Packages\Blocks\RecommendedElementorWidgets; |
| 6 | use Smashballoon\TwitterFeed\Vendor\Smashballoon\Framework\Packages\Blocks\SB_Feed_Blocks_Registry; |
| 7 | use Smashballoon\TwitterFeed\Vendor\Smashballoon\Framework\Packages\Blocks\SB_Block_Utils; |
| 8 | use Smashballoon\TwitterFeed\Vendor\Smashballoon\Framework\Packages\Blocks\SB_Elementor_Editor_Assets; |
| 9 | use TwitterFeed\Builder\CTF_Db; |
| 10 | |
| 11 | if (! defined('ABSPATH')) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps -- Follows codebase CTF_ prefix convention. |
| 16 | class CTF_Elementor_Base |
| 17 | { |
| 18 | /** |
| 19 | * Singleton instance. |
| 20 | * |
| 21 | * @var self|null |
| 22 | */ |
| 23 | private static $instance = null; |
| 24 | |
| 25 | /** |
| 26 | * Register and return the singleton instance. |
| 27 | * |
| 28 | * @return self |
| 29 | */ |
| 30 | public static function register() |
| 31 | { |
| 32 | if (null === self::$instance) { |
| 33 | self::$instance = new self(); |
| 34 | self::$instance->init(); |
| 35 | } |
| 36 | return self::$instance; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the singleton instance. |
| 41 | * |
| 42 | * @return self |
| 43 | */ |
| 44 | public static function instance() |
| 45 | { |
| 46 | return self::register(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Initialize the Elementor integration hooks. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | private function init() |
| 55 | { |
| 56 | if (doing_action('init') || did_action('init')) { |
| 57 | $this->init_elementor_integration(); |
| 58 | } else { |
| 59 | add_action('init', array( $this, 'init_elementor_integration' ), 4); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set up Elementor widget registration, scripts, and categories. |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public function init_elementor_integration() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 69 | { |
| 70 | if (! did_action('elementor/loaded')) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $recommended = new RecommendedElementorWidgets('twitter'); |
| 75 | $recommended->setup(); |
| 76 | |
| 77 | $registry = SB_Feed_Blocks_Registry::instance(); |
| 78 | $registry->register_elementor_widget(array( |
| 79 | 'blockId' => 'twitter', |
| 80 | 'widgetName' => 'sb-twitter-feed', |
| 81 | 'globalVar' => 'ctfElementorData', |
| 82 | 'feedInitFn' => 'ctf_init', |
| 83 | )); |
| 84 | |
| 85 | add_action('elementor/widgets/register', array( $this, 'register_widgets' )); |
| 86 | add_action('elementor/frontend/after_register_scripts', array( $this, 'register_frontend_scripts' )); |
| 87 | add_action('elementor/elements/categories_registered', array( $this, 'add_smashballoon_categories' )); |
| 88 | add_action('elementor/editor/after_enqueue_scripts', array( $this, 'enqueue_editor_scripts' )); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Register the Elementor widget. |
| 93 | * |
| 94 | * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager. |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | public function register_widgets($widgets_manager) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 99 | { |
| 100 | $widgets_manager->register(new CTF_Modern_Elementor_Widget()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Register and localize frontend scripts for Elementor. |
| 105 | * |
| 106 | * @return void |
| 107 | */ |
| 108 | public function register_frontend_scripts() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 109 | { |
| 110 | ctf_scripts_and_styles(true); |
| 111 | |
| 112 | $feeds = CTF_Db::elementor_feeds_list(); |
| 113 | |
| 114 | $data = array( |
| 115 | 'feeds' => ! empty($feeds) ? $feeds : array(), |
| 116 | 'feed_url' => admin_url('admin.php?page=ctf-feed-builder'), |
| 117 | 'is_pro_active' => ctf_is_pro_version(), |
| 118 | ); |
| 119 | |
| 120 | wp_localize_script('ctf_scripts', 'ctfElementorData', $data); |
| 121 | |
| 122 | SB_Feed_Blocks_Registry::instance()->enqueue_elementor_assets(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Add the Smash Balloon widget category to Elementor. |
| 127 | * |
| 128 | * @param \Elementor\Elements_Manager $elements_manager Elementor elements manager. |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | public function add_smashballoon_categories($elements_manager) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 133 | { |
| 134 | $elements_manager->add_category( |
| 135 | SB_Block_Utils::CATEGORY_SLUG, |
| 136 | array( |
| 137 | 'title' => esc_html__('Smash Balloon', 'custom-twitter-feeds'), |
| 138 | 'icon' => 'fa fa-plug', |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Enqueue shared Elementor editor styles. |
| 145 | * |
| 146 | * @return void |
| 147 | */ |
| 148 | public function enqueue_editor_scripts() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 149 | { |
| 150 | SB_Elementor_Editor_Assets::enqueue_shared_elementor_styles(CTF_VERSION); |
| 151 | } |
| 152 | } |
| 153 |