| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Video Player |
| 5 |
* |
| 6 |
* @since 4.0.0 |
| 7 |
*/ |
| 8 |
class Optml_Video_Player { |
| 9 |
|
| 10 |
/** |
| 11 |
* Block attributes |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $block_attributes = [ |
| 16 |
'url' => [ |
| 17 |
'type' => 'string', |
| 18 |
], |
| 19 |
'primaryColor' => [ |
| 20 |
'type' => 'string', |
| 21 |
'default' => '#577BF9', |
| 22 |
], |
| 23 |
'aspectRatio' => [ |
| 24 |
'type' => 'string', |
| 25 |
'default' => 'auto', |
| 26 |
], |
| 27 |
'loop' => [ |
| 28 |
'type' => 'boolean', |
| 29 |
'default' => false, |
| 30 |
], |
| 31 |
'hideControls' => [ |
| 32 |
'type' => 'boolean', |
| 33 |
'default' => false, |
| 34 |
], |
| 35 |
]; |
| 36 |
|
| 37 |
const LOCALIZATION_VAR = 'OMVideoPlayerBlock'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Icons |
| 41 |
* |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
private $icons = [ |
| 45 |
'play' => '<symbol id="optml-play-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-play"><polygon points="6 3 20 12 6 21 6 3"/></symbol>', |
| 46 |
'volume-mute' => '<symbol id="optml-volume-mute-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-volume-x"><path d="M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z"/><line x1="22" x2="16" y1="9" y2="15"/><line x1="16" x2="22" y1="9" y2="15"/></symbol>', |
| 47 |
'volume-high' => '<symbol id="optml-volume-high-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-volume-2"><path d="M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z"/><path d="M16 9a5 5 0 0 1 0 6"/><path d="M19.364 18.364a9 9 0 0 0 0-12.728"/></symbol>', |
| 48 |
'volume-low' => '<symbol id="optml-volume-low-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-volume-1"><path d="M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z"/><path d="M16 9a5 5 0 0 1 0 6"/></symbol>', |
| 49 |
'spinner' => '<symbol id="optml-loader-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-loader"><path d="M12 2v4"/><path d="m16.2 7.8 2.9-2.9"/><path d="M18 12h4"/><path d="m16.2 16.2 2.9 2.9"/><path d="M12 18v4"/><path d="m4.9 19.1 2.9-2.9"/><path d="M2 12h4"/><path d="m4.9 4.9 2.9 2.9"/></symbol>', |
| 50 |
'maximize' => '<symbol id="optml-maximize-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize"><path d="M8 3H5a2 2 0 0 0-2 2v3"/><path d="M21 8V5a2 2 0 0 0-2-2h-3"/><path d="M3 16v3a2 2 0 0 0 2 2h3"/><path d="M16 21h3a2 2 0 0 0 2-2v-3"/></symbol>', |
| 51 |
'minimize' => '<symbol id="optml-minimize-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-minimize"><path d="M8 3v3a2 2 0 0 1-2 2H3"/><path d="M21 8h-3a2 2 0 0 1-2-2V3"/><path d="M3 16h3a2 2 0 0 1 2 2v3"/><path d="M16 21v-3a2 2 0 0 1 2-2h3"/></symbol>', |
| 52 |
'pause' => '<symbol id="optml-pause-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--optml-ctrl-ico-col, #fff)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-pause"><rect x="14" y="4" width="4" height="16" rx="1"/><rect x="6" y="4" width="4" height="16" rx="1"/></symbol>', |
| 53 |
]; |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor. |
| 57 |
* |
| 58 |
* @since 4.0.0 |
| 59 |
*/ |
| 60 |
public function __construct() { |
| 61 |
$settings = new Optml_Settings(); |
| 62 |
|
| 63 |
if ( ! $settings->is_connected() ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_admin_video_player_assets' ] ); |
| 68 |
add_action( 'admin_head', [ $this, 'add_editor_player_icons' ] ); |
| 69 |
add_action( 'wp_enqueue_scripts', [ $this, 'maybe_enqueue_video_player_script' ] ); |
| 70 |
add_action( 'init', [ $this, 'register_video_player_block' ] ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Enqueue the admin video player assets. |
| 75 |
* |
| 76 |
* @since 4.0.0 |
| 77 |
*/ |
| 78 |
public function enqueue_admin_video_player_assets() { |
| 79 |
$asset_file = include OPTML_PATH . 'assets/build/video-player/editor/editor.asset.php'; |
| 80 |
|
| 81 |
if ( empty( $asset_file ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$handle = 'optimole-video-player-editor'; |
| 86 |
|
| 87 |
wp_register_script( |
| 88 |
$handle, |
| 89 |
OPTML_URL . 'assets/build/video-player/editor/editor.js', |
| 90 |
array_merge( $asset_file['dependencies'] ), |
| 91 |
$asset_file['version'], |
| 92 |
true |
| 93 |
); |
| 94 |
|
| 95 |
wp_localize_script( $handle, 'OMVideoPlayerBlock', $this->get_localization( true ) ); |
| 96 |
wp_enqueue_script( $handle ); |
| 97 |
|
| 98 |
wp_enqueue_style( $handle, OPTML_URL . 'assets/build/video-player/frontend/style-frontend.css', [], $asset_file['version'] ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Add the video player icons to the editor. |
| 103 |
* |
| 104 |
* @since 4.0.0 |
| 105 |
*/ |
| 106 |
public function add_editor_player_icons() { |
| 107 |
$screen = get_current_screen(); |
| 108 |
if ( ! $screen || ! $screen->is_block_editor() ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$this->render_player_icons(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Maybe enqueue the video player component script. |
| 117 |
* |
| 118 |
* @since 4.0.0 |
| 119 |
*/ |
| 120 |
public function maybe_enqueue_video_player_script() { |
| 121 |
if ( ! has_block( 'optimole/video-player' ) ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$this->enqueue_video_player_component_script(); |
| 126 |
$this->render_player_icons(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Enqueue the video player component script. |
| 131 |
* |
| 132 |
* @since 4.0.0 |
| 133 |
*/ |
| 134 |
private function enqueue_video_player_component_script() { |
| 135 |
$asset_file = include OPTML_PATH . 'assets/build/video-player/frontend/frontend.asset.php'; |
| 136 |
|
| 137 |
if ( empty( $asset_file ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
wp_register_script( |
| 142 |
'optimole-video-player-component', |
| 143 |
OPTML_URL . 'assets/build/video-player/frontend/frontend.js', |
| 144 |
$asset_file['dependencies'], |
| 145 |
); |
| 146 |
|
| 147 |
wp_localize_script( 'optimole-video-player-component', self::LOCALIZATION_VAR, $this->get_localization() ); |
| 148 |
wp_enqueue_script( 'optimole-video-player-component' ); |
| 149 |
|
| 150 |
wp_enqueue_style( |
| 151 |
'optimole-video-player-component', |
| 152 |
OPTML_URL . 'assets/build/video-player/frontend/style-frontend.css', |
| 153 |
[], |
| 154 |
$asset_file['version'] |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Render the video player icons |
| 160 |
* |
| 161 |
* @since 4.0.0 |
| 162 |
*/ |
| 163 |
private function render_player_icons() { |
| 164 |
?> |
| 165 |
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> |
| 166 |
<?php foreach ( $this->icons as $icon_id => $icon_svg ) : ?> |
| 167 |
<?php echo $icon_svg; ?> |
| 168 |
<?php endforeach; ?> |
| 169 |
</svg> |
| 170 |
<?php |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Register the video player block. |
| 175 |
* |
| 176 |
* @since 4.0.0 |
| 177 |
*/ |
| 178 |
public function register_video_player_block() { |
| 179 |
register_block_type( |
| 180 |
'optimole/video-player', |
| 181 |
[ |
| 182 |
'render_callback' => [ $this, 'render_video_player_block' ], |
| 183 |
'attributes' => $this->block_attributes, |
| 184 |
'supports' => [ |
| 185 |
'align' => true, |
| 186 |
], |
| 187 |
] |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Render the video player block. |
| 193 |
* |
| 194 |
* @param array $attributes The block attributes. |
| 195 |
* @return string The video player block markup. |
| 196 |
* |
| 197 |
* @since 4.0.0 |
| 198 |
*/ |
| 199 |
public function render_video_player_block( $attributes, $content, $block ) { |
| 200 |
$attributes = wp_parse_args( $attributes, $this->block_attributes ); |
| 201 |
|
| 202 |
$style = [ |
| 203 |
'--om-primary-color' => $attributes['primaryColor'], |
| 204 |
'--om-aspect-ratio' => $attributes['aspectRatio'], |
| 205 |
]; |
| 206 |
|
| 207 |
if ( isset( $attributes['style'] ) ) { |
| 208 |
$style = array_merge( $style, $this->block_style_attributes_to_css_array( $attributes['style'] ) ); |
| 209 |
} |
| 210 |
|
| 211 |
$css = array_map( |
| 212 |
function ( $key, $value ) { |
| 213 |
return $key . ': ' . $value; |
| 214 |
}, |
| 215 |
array_keys( $style ), |
| 216 |
$style |
| 217 |
); |
| 218 |
|
| 219 |
$tag_attributes = [ |
| 220 |
'video-src' => esc_url( $attributes['url'] ), |
| 221 |
'loop' => $attributes['loop'] ? 'true' : 'false', |
| 222 |
'hide-controls' => $attributes['hideControls'] ? 'true' : 'false', |
| 223 |
'style' => esc_attr( implode( ';', $css ) ), |
| 224 |
]; |
| 225 |
|
| 226 |
$tag_attributes = array_map( |
| 227 |
function ( $key, $value ) { |
| 228 |
return $key . '="' . $value . '"'; |
| 229 |
}, |
| 230 |
array_keys( $tag_attributes ), |
| 231 |
$tag_attributes |
| 232 |
); |
| 233 |
|
| 234 |
$wrapper_attributes = array_filter( |
| 235 |
$attributes, |
| 236 |
function ( $key ) { |
| 237 |
return ! in_array( $key, array_keys( $this->block_attributes ), true ) && $key !== 'style'; |
| 238 |
}, |
| 239 |
ARRAY_FILTER_USE_KEY |
| 240 |
); |
| 241 |
|
| 242 |
return sprintf( |
| 243 |
'<div %s><optimole-video-player %s></optimole-video-player></div>', |
| 244 |
get_block_wrapper_attributes( $wrapper_attributes ), |
| 245 |
implode( ' ', $tag_attributes ), |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Get the localization strings. |
| 251 |
* |
| 252 |
* @param boolean $editor Whether to get the localization for the editor. |
| 253 |
* @return array The localization strings. |
| 254 |
* |
| 255 |
* @since 4.0.0 |
| 256 |
*/ |
| 257 |
private function get_localization( $editor = false ) { |
| 258 |
$strings = [ |
| 259 |
'play' => __( 'Play', 'optimole-wp' ), |
| 260 |
'pause' => __( 'Pause', 'optimole-wp' ), |
| 261 |
'mute' => __( 'Mute', 'optimole-wp' ), |
| 262 |
'unmute' => __( 'Unmute', 'optimole-wp' ), |
| 263 |
'fullscreen' => __( 'Fullscreen', 'optimole-wp' ), |
| 264 |
'exitFullscreen' => __( 'Exit Fullscreen', 'optimole-wp' ), |
| 265 |
]; |
| 266 |
|
| 267 |
if ( ! $editor ) { |
| 268 |
return $strings; |
| 269 |
} |
| 270 |
|
| 271 |
$options = new Optml_Settings(); |
| 272 |
$cdn_url = $options->get_cdn_url(); |
| 273 |
|
| 274 |
return array_merge( |
| 275 |
$strings, |
| 276 |
[ |
| 277 |
'domain' => $cdn_url, |
| 278 |
'blockTitle' => __( 'Optimole Video', 'optimole-wp' ), |
| 279 |
'blockDescription' => __( 'Optimole Video', 'optimole-wp' ), |
| 280 |
'aspectRatioLabel' => __( 'Aspect Ratio', 'optimole-wp' ), |
| 281 |
'urlLabel' => __( 'Video URL', 'optimole-wp' ), |
| 282 |
'urlHelp' => __( 'Enter the URL of the video you want to display.', 'optimole-wp' ), |
| 283 |
'editLabel' => __( 'Change URL', 'optimole-wp' ), |
| 284 |
'loopLabel' => __( 'Loop Video', 'optimole-wp' ), |
| 285 |
'hideControlsLabel' => __( 'Hide Video Controls Bar', 'optimole-wp' ), |
| 286 |
// translators: %s is 'Optimole Dashboard'. |
| 287 |
'invalidUrlError' => sprintf( __( 'Invalid URL. Please enter a valid video URL from %s', 'optimole-wp' ), '<a target="_blank" href="https://dashboard.optimole.com">' . __( 'Optimole Dashboard', 'optimole-wp' ) . '</a>' ), |
| 288 |
'auto' => __( 'Auto', 'optimole-wp' ), |
| 289 |
'save' => __( 'Save', 'optimole-wp' ), |
| 290 |
'primaryColorLabel' => __( 'Controls color', 'optimole-wp' ), |
| 291 |
] |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Convert the block style attributes to a css array. |
| 297 |
* |
| 298 |
* @param array $attributes The block attributes. |
| 299 |
* @return array The css array. |
| 300 |
* |
| 301 |
* @since 4.0.0 |
| 302 |
*/ |
| 303 |
private function block_style_attributes_to_css_array( $attributes ) { |
| 304 |
$css = []; |
| 305 |
|
| 306 |
if ( isset( $attributes['spacing'] ) ) { |
| 307 |
$spacing = $attributes['spacing']; |
| 308 |
|
| 309 |
foreach ( $spacing as $css_prop_prefix => $values ) { |
| 310 |
foreach ( $values as $direction => $value ) { |
| 311 |
$css[ $css_prop_prefix . '-' . $direction ] = $this->core_var_to_css_var( $value ); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
return $css; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Convert a core var to a css var. |
| 321 |
* e.g.: var:preset|spacing|50 -> var(--wp--preset--spacing--50) |
| 322 |
* |
| 323 |
* @param string $css_value The css value. |
| 324 |
* @return string The css var. |
| 325 |
* |
| 326 |
* @since 4.0.0 |
| 327 |
*/ |
| 328 |
private function core_var_to_css_var( $css_value ) { |
| 329 |
if ( strpos( $css_value, 'var:' ) !== 0 ) { |
| 330 |
return $css_value; |
| 331 |
} |
| 332 |
|
| 333 |
$css_value = str_replace( 'var:', '', $css_value ); |
| 334 |
$css_value = str_replace( '|', '--', $css_value ); |
| 335 |
|
| 336 |
return 'var(--wp--' . $css_value . ')'; |
| 337 |
} |
| 338 |
} |
| 339 |
|