| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor integration for the WpStream plugin. |
| 4 |
* |
| 5 |
* One class owns the whole Elementor layer: it verifies on plugins_loaded that |
| 6 |
* Elementor is active and meets the minimum Elementor/PHP versions, and then |
| 7 |
* loads the plugin's widget classes from widgets/ and registers them with |
| 8 |
* Elementor under the "WpStream Widgets" category. |
| 9 |
* |
| 10 |
* Elementor is optional. A site without it stays silent: no widgets, no |
| 11 |
* notice. Only a site that does run Elementor is warned when its Elementor or |
| 12 |
* PHP version is too old, because there the warning is actionable. |
| 13 |
* |
| 14 |
* The widget files are required here rather than autoloaded because they |
| 15 |
* extend Elementor\Widget_Base, which only exists once Elementor has loaded. |
| 16 |
* |
| 17 |
* @package Wpstream |
| 18 |
* @subpackage Wpstream/includes |
| 19 |
*/ |
| 20 |
|
| 21 |
// Block direct access: ABSPATH is only defined when running inside WordPress. |
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Requirement gate and widget registrar for Elementor. |
| 28 |
*/ |
| 29 |
final class Wpstream_Elementor_Widgets { |
| 30 |
|
| 31 |
/** |
| 32 |
* Elementor must be at least this version. 3.5.0 introduced the |
| 33 |
* `elementor/widgets/register` hook and `Widgets_Manager::register()` |
| 34 |
* that this class relies on. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
const MINIMUM_ELEMENTOR_VERSION = '3.5.0'; |
| 39 |
|
| 40 |
/** |
| 41 |
* PHP must be at least this version (matches the plugin header). |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
const MINIMUM_PHP_VERSION = '7.1'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Defer the requirement checks until every plugin has had a chance to load. |
| 49 |
*/ |
| 50 |
public function __construct() { |
| 51 |
add_action( 'plugins_loaded', array( $this, 'init' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Check the requirements; schedule a notice on failure, wire the widgets on success. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function init() { |
| 60 |
// Elementor not active: 'elementor/loaded' never fired. Elementor is an |
| 61 |
// optional integration, so a site without it gets no widgets and no notice. |
| 62 |
if ( ! did_action( 'elementor/loaded' ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
// Elementor too old for the registration API used below. |
| 67 |
if ( ! version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' ) ) { |
| 68 |
add_action( 'admin_notices', array( $this, 'admin_notice_minimum_elementor_version' ) ); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
// PHP too old. |
| 73 |
if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) { |
| 74 |
add_action( 'admin_notices', array( $this, 'admin_notice_minimum_php_version' ) ); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
// Requirements met: add the panel category and register the widgets when Elementor asks for them. |
| 79 |
add_action( 'elementor/elements/categories_registered', array( $this, 'add_widget_category' ) ); |
| 80 |
add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Add the "WpStream Widgets" category to the Elementor editor panel. |
| 85 |
* |
| 86 |
* @param \Elementor\Elements_Manager $elements_manager Elementor's category registry. |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function add_widget_category( $elements_manager ) { |
| 90 |
$elements_manager->add_category( |
| 91 |
'wpstream', |
| 92 |
array( |
| 93 |
'title' => __( 'WpStream Widgets', 'wpstream' ), |
| 94 |
'icon' => 'fa fa-home', |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Load the widget classes and hand one instance of each to Elementor. |
| 101 |
* |
| 102 |
* @param \Elementor\Widgets_Manager $widgets_manager Elementor's widget registry. |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function register_widgets( $widgets_manager ) { |
| 106 |
$widgets_dir = dirname( __DIR__ ) . '/widgets/'; |
| 107 |
|
| 108 |
// Widget files extend Elementor\Widget_Base, so they are only safe to include here. |
| 109 |
require_once $widgets_dir . 'player.php'; |
| 110 |
require_once $widgets_dir . 'start_streaming.php'; |
| 111 |
require_once $widgets_dir . 'media_list.php'; |
| 112 |
|
| 113 |
$widgets_manager->register( new \ElementorWpStream\Widgets\Wpstream_Player_Base() ); |
| 114 |
$widgets_manager->register( new \ElementorWpStream\Widgets\Wpstream_Start_Streaming_Base() ); |
| 115 |
$widgets_manager->register( new \ElementorWpStream\Widgets\Wpstream_Media_List_Channel() ); |
| 116 |
$widgets_manager->register( new \ElementorWpStream\Widgets\Wpstream_Media_List_Vod() ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Admin notice: the installed Elementor is older than the minimum. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function admin_notice_minimum_elementor_version() { |
| 125 |
$message = sprintf( |
| 126 |
/* translators: 1: Plugin name 2: Elementor 3: Required Elementor version */ |
| 127 |
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'wpstream' ), |
| 128 |
'<strong>' . esc_html__( 'WpStream Elementor', 'wpstream' ) . '</strong>', |
| 129 |
'<strong>' . esc_html__( 'Elementor', 'wpstream' ) . '</strong>', |
| 130 |
esc_html( self::MINIMUM_ELEMENTOR_VERSION ) |
| 131 |
); |
| 132 |
$this->print_notice( $message ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Admin notice: the running PHP is older than the minimum. |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function admin_notice_minimum_php_version() { |
| 141 |
$message = sprintf( |
| 142 |
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */ |
| 143 |
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'wpstream' ), |
| 144 |
'<strong>' . esc_html__( 'WpStream Elementor', 'wpstream' ) . '</strong>', |
| 145 |
'<strong>' . esc_html__( 'PHP', 'wpstream' ) . '</strong>', |
| 146 |
esc_html( self::MINIMUM_PHP_VERSION ) |
| 147 |
); |
| 148 |
$this->print_notice( $message ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Print an already-escaped message as a dismissible warning notice. |
| 153 |
* |
| 154 |
* @param string $message Escaped HTML message. |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
private function print_notice( $message ) { |
| 158 |
// Hide WordPress's "Plugin activated" message so it is not shown beside the warning. |
| 159 |
unset( $_GET['activate'] ); |
| 160 |
printf( '<div class="notice notice-warning is-dismissible"><p>%s</p></div>', $message ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped when built. |
| 161 |
} |
| 162 |
} |
| 163 |
|