Elementor.php
121 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Integrates WPP with Elementor. |
| 4 | * |
| 5 | * @package WordPressPopularPosts |
| 6 | * @subpackage WordPressPopularPosts/Compatibility |
| 7 | * @author Hector Cabrera <me@cabrerahector.com> |
| 8 | */ |
| 9 | |
| 10 | namespace WordPressPopularPosts\Compatibility\Elementor; |
| 11 | |
| 12 | use WordPressPopularPosts\Compatibility\Compat; |
| 13 | use WordPressPopularPosts\{Image, Themer}; |
| 14 | |
| 15 | class Elementor extends Compat |
| 16 | { |
| 17 | /** |
| 18 | * Image object. |
| 19 | * |
| 20 | * @var \WordPressPopularPosts\Image |
| 21 | * @access private |
| 22 | */ |
| 23 | private $thumbnail; |
| 24 | |
| 25 | /** |
| 26 | * Themer object. |
| 27 | * |
| 28 | * @var \WordPressPopularPosts\Themer $themer |
| 29 | * @access private |
| 30 | */ |
| 31 | private $themer; |
| 32 | |
| 33 | /** |
| 34 | * Construct. |
| 35 | * |
| 36 | * @param array $settings |
| 37 | */ |
| 38 | public function __construct($settings, Image $image, Themer $themer) |
| 39 | { |
| 40 | $this->thumbnail = $image; |
| 41 | $this->themer = $themer; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Registers various WPPxElementor things. |
| 46 | * |
| 47 | * @since 7.3.0 |
| 48 | */ |
| 49 | public function init() |
| 50 | { |
| 51 | if ( defined('ELEMENTOR_VERSION') && version_compare(ELEMENTOR_VERSION, '3.5.0', '>=') ) { |
| 52 | // Registers flame icon |
| 53 | add_action('elementor/editor/after_enqueue_scripts', [$this, 'elementor_icon_css']); |
| 54 | // Registers WPP widget |
| 55 | add_action('elementor/widgets/register', [$this, 'register_widget']); |
| 56 | // Disable [wpp] shortcode AJAX loading in editor mode |
| 57 | add_filter('shortcode_atts_wpp', [$this, 'disable_ajax_in_editor'], 10, 4); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Registers WPP's icon for Elementor. |
| 63 | * |
| 64 | * @since 7.3.0 |
| 65 | */ |
| 66 | public function elementor_icon_css() { |
| 67 | $icon_file = esc_url(plugin_dir_url(dirname(__FILE__, 3))) . 'assets/images/flame.svg'; |
| 68 | |
| 69 | echo '<style> |
| 70 | .wpp-eicon { |
| 71 | display: inline-block; |
| 72 | width: 24px; |
| 73 | height: 24px; |
| 74 | background: url("' . $icon_file . '") center center /contain no-repeat; |
| 75 | } |
| 76 | </style>'; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Registers WPP widget. |
| 81 | * |
| 82 | * @since 7.3.0 |
| 83 | * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager. |
| 84 | */ |
| 85 | public function register_widget($widgets_manager) { |
| 86 | $registered_themes = $this->themer->get_themes(); |
| 87 | ksort($registered_themes); |
| 88 | |
| 89 | require_once(__DIR__ . '/widgets/widget.php'); |
| 90 | $widgets_manager->register( |
| 91 | new \Elementor_WPP_Widget( |
| 92 | [], |
| 93 | [ |
| 94 | 'image' => $this->thumbnail, |
| 95 | 'themer' => $this->themer |
| 96 | ] |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Disables [wpp] AJAX loading in editor mode. |
| 103 | * |
| 104 | * @since 7.4.0 |
| 105 | * @param array $out The output array of shortcode attributes. |
| 106 | * @param array $pairs The supported attributes and their defaults. |
| 107 | * @param array $atts The user defined shortcode attributes. |
| 108 | * @param string $shortcode The shortcode name. |
| 109 | */ |
| 110 | public function disable_ajax_in_editor(array $out, array $pairs, array $atts, string $shortcode) { |
| 111 | if ( |
| 112 | 'wpp' === $shortcode && |
| 113 | \Elementor\Plugin::$instance->editor->is_edit_mode() |
| 114 | ) { |
| 115 | $out['ajaxify'] = 0; |
| 116 | } |
| 117 | |
| 118 | return $out; |
| 119 | } |
| 120 | } |
| 121 |