| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor widget: WpStream Player. |
| 4 |
* |
| 5 |
* Registers the "WpStream Player" Elementor widget under the plugin's own |
| 6 |
* widget category. It exposes a product/free-product id plus an optional user |
| 7 |
* id and, on render, delegates to the main plugin object to print the player. |
| 8 |
* |
| 9 |
* @package Wpstream |
| 10 |
* @subpackage Wpstream/widgets |
| 11 |
*/ |
| 12 |
namespace ElementorWpStream\Widgets; |
| 13 |
|
| 14 |
use Elementor\Widget_Base; |
| 15 |
use Elementor\Controls_Manager; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Elementor widget class for the WpStream player. |
| 22 |
* |
| 23 |
* Implements the standard Elementor widget contract: identity (name/title/icon), |
| 24 |
* editor controls, frontend render, and the editor preview template. |
| 25 |
*/ |
| 26 |
class Wpstream_Player_Base extends Widget_Base { |
| 27 |
|
| 28 |
/** |
| 29 |
* Retrieve the widget name. |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
* |
| 33 |
* @access public |
| 34 |
* |
| 35 |
* @return string Widget name. |
| 36 |
*/ |
| 37 |
public function get_name() { |
| 38 |
// Internal identifier Elementor uses to reference this widget. |
| 39 |
return 'Wpstream_Player'; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Retrieve the widget categories. |
| 44 |
* |
| 45 |
* @return array List of Elementor category slugs this widget belongs to. |
| 46 |
*/ |
| 47 |
public function get_categories() { |
| 48 |
// Group this widget under the plugin's own "wpstream" category. |
| 49 |
return [ 'wpstream' ]; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Retrieve the widget title. |
| 55 |
* |
| 56 |
* @since 1.0.0 |
| 57 |
* |
| 58 |
* @access public |
| 59 |
* |
| 60 |
* @return string Widget title. |
| 61 |
*/ |
| 62 |
public function get_title() { |
| 63 |
// Wrap the translated label in the theme's widget-title markup. |
| 64 |
return '<div class="wpestate_elementor_widget_title">'.__( 'WpStream Player', 'wpstream' ).'</div>'; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Retrieve the widget icon. |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* |
| 72 |
* @access public |
| 73 |
* |
| 74 |
* @return string Widget icon. |
| 75 |
*/ |
| 76 |
public function get_icon() { |
| 77 |
// Elementor icon-font class shown for this widget in the panel. |
| 78 |
return 'eicon-play-o'; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Retrieve the list of scripts the widget depended on. |
| 85 |
* |
| 86 |
* Used to set scripts dependencies required to run the widget. |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
* |
| 90 |
* @access public |
| 91 |
* |
| 92 |
* @return array Widget scripts dependencies. |
| 93 |
*/ |
| 94 |
public function get_script_depends() { |
| 95 |
// No extra script handles are registered as dependencies. |
| 96 |
return [ '' ]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Register the widget controls. |
| 101 |
* |
| 102 |
* Adds different input fields to allow the user to change and customize the widget settings. |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @access protected |
| 107 |
*/ |
| 108 |
|
| 109 |
/** |
| 110 |
* Flatten a list of {value,label} pairs into a value => label map. |
| 111 |
* |
| 112 |
* @param array $input List of arrays each holding 'value' and 'label'. |
| 113 |
* @return array Map keyed by each item's 'value' with its 'label'. |
| 114 |
*/ |
| 115 |
public function elementor_transform($input){ |
| 116 |
// Accumulator for the resulting value => label map. |
| 117 |
$output=array(); |
| 118 |
// Only iterate when we were actually handed an array. |
| 119 |
if( is_array($input) ){ |
| 120 |
// Re-key each entry so the option value becomes the map key. |
| 121 |
foreach ($input as $key=>$tax){ |
| 122 |
$output[$tax['value']]=$tax['label']; |
| 123 |
} |
| 124 |
} |
| 125 |
return $output; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Register the widget's editor controls. |
| 130 |
* |
| 131 |
* @access protected |
| 132 |
*/ |
| 133 |
protected function _register_controls() { |
| 134 |
// Theme-wide taxonomy list kept in scope (unused by this widget). |
| 135 |
global $all_tax; |
| 136 |
|
| 137 |
// Open the "Content" settings section. |
| 138 |
$this->start_controls_section( |
| 139 |
'section_content', |
| 140 |
[ |
| 141 |
'label' => __( 'Content', 'wpstream' ), |
| 142 |
] |
| 143 |
); |
| 144 |
|
| 145 |
// Text field: product / free-product id to build the player for. |
| 146 |
$this->add_control( |
| 147 |
'item_id', |
| 148 |
[ |
| 149 |
'label' => __( 'Product/Free Product id', 'wpstream' ), |
| 150 |
'label_block'=>true, |
| 151 |
'type' => Controls_Manager::TEXT, |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
|
| 156 |
// Text field: optional user id; its first channel overrides the product id. |
| 157 |
$this->add_control( |
| 158 |
'user_id', |
| 159 |
[ |
| 160 |
'label' => __( 'User Id', 'wpstream' ), |
| 161 |
'label_block'=>true, |
| 162 |
'type' => Controls_Manager::TEXT, |
| 163 |
'description' => esc_html__( "We will use the first channel of this user id(product id will be ignored.).","wpestate") |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
// Close the "Content" settings section. |
| 168 |
$this->end_controls_section(); |
| 169 |
|
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Render the widget output on the frontend. |
| 175 |
* |
| 176 |
* Written in PHP and used to generate the final HTML. |
| 177 |
* |
| 178 |
* @since 1.0.0 |
| 179 |
* |
| 180 |
* @access protected |
| 181 |
*/ |
| 182 |
|
| 183 |
/** |
| 184 |
* Join array values into a single comma-separated string. |
| 185 |
* |
| 186 |
* @param array|string $input Values to concatenate; '' short-circuits to empty. |
| 187 |
* @return string Comma-separated list, or empty string when nothing to join. |
| 188 |
*/ |
| 189 |
public function wpresidence_send_to_shortcode($input){ |
| 190 |
// Start with an empty result buffer. |
| 191 |
$output=''; |
| 192 |
// Nothing to concatenate for an empty input. |
| 193 |
if($input!==''){ |
| 194 |
// Track the total so we can skip the trailing separator. |
| 195 |
$numItems = count($input); |
| 196 |
$i = 0; |
| 197 |
|
| 198 |
// Append each value, comma-separating all but the last. |
| 199 |
foreach ($input as $key=>$value){ |
| 200 |
$output.=$value; |
| 201 |
if(++$i !== $numItems) { |
| 202 |
$output.=', '; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
return $output; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Render the widget on the frontend. |
| 211 |
* |
| 212 |
* Collects the control values and delegates to the plugin to echo the player. |
| 213 |
* |
| 214 |
* @access protected |
| 215 |
*/ |
| 216 |
protected function render() { |
| 217 |
// Pull the saved control values for this widget instance. |
| 218 |
$settings = $this->get_settings_for_display(); |
| 219 |
|
| 220 |
// Map the control values onto the attribute array the player expects. |
| 221 |
$attributes['id'] = $settings['item_id'] ; |
| 222 |
$attributes['user_id'] = $settings['user_id'] ; |
| 223 |
|
| 224 |
|
| 225 |
// Main plugin instance that knows how to build the player markup. |
| 226 |
global $wpstream_plugin; |
| 227 |
// Echo the assembled player for the chosen product/user. |
| 228 |
echo $wpstream_plugin->wpstream_insert_player_elementor($attributes); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Render the widget output in the editor. |
| 233 |
* |
| 234 |
* Written as a Backbone JavaScript template and used to generate the live preview. |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
* |
| 238 |
* @access protected |
| 239 |
*/ |
| 240 |
protected function content_template() { |
| 241 |
?> |
| 242 |
<div class="title"> |
| 243 |
<!-- Backbone/Underscore template token echoed as the live editor preview. --> |
| 244 |
{{{ settings.title }}} |
| 245 |
</div> |
| 246 |
<?php |
| 247 |
} |
| 248 |
} |
| 249 |
|