get_settings();
if (empty($s['enabled'])) { return; }
add_action('add_meta_boxes', array($this, 'add_meta_box'));
add_action('save_post_product', array($this, 'save_meta_box'));
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue'));
add_action('wp_enqueue_scripts', array($this, 'register_scripts'));
add_filter('woocommerce_post_class', array($this, 'add_video_class'), 10, 2);
add_filter('woocommerce_single_product_image_thumbnail_html', array($this, 'add_featured_video'), 5, 2);
add_filter('woocommerce_single_product_image_thumbnail_html', array($this, 'add_gallery_videos'), 10, 2);
add_filter('render_block_woocommerce/product-gallery', array($this, 'inject_block_gallery'), 10, 2);
if ( class_exists( 'AIEO_Theme_Adapters' ) && method_exists( 'AIEO_Theme_Adapters', 'draw' ) ) { AIEO_Theme_Adapters::draw( 'product_video', 'woocommerce_single_product_image_thumbnail_html, product-gallery block' ); }
}
public function get_settings() {
$stored = get_option(self::OPTION_KEY, array());
if (!is_array($stored)) { $stored = array(); }
$legacy = get_option('om_ppe_product-video', array());
if (!is_array($legacy)) { $legacy = array(); }
return array_merge(array(
'enabled' => false,
'default_autoplay' => false,
'default_aspect_ratio' => '16-9',
), $legacy, $stored);
}
public function ajax_save() {
AIEO_DMM::verify_ajax_request();
$valid_ratios = array('16-9','9-16','4-3','3-2','1-1','auto');
$ar = isset($_POST['default_aspect_ratio']) ? sanitize_key(wp_unslash($_POST['default_aspect_ratio'])) : '16-9';
update_option(self::OPTION_KEY, array(
'enabled' => !empty($_POST['enabled']),
'default_autoplay' => !empty($_POST['default_autoplay']),
'default_aspect_ratio' => in_array($ar, $valid_ratios, true) ? $ar : '16-9',
), false);
wp_send_json_success(array('message' => __('Product Video settings saved.', 'ai-eshop-optimizer')));
}
public function register_scripts() {
$version = defined('AIEO_VERSION') ? AIEO_VERSION : '1.0';
wp_register_style(
'aieo-product-video',
plugins_url('assets/css/product-display/product-video.css', AIEO_FILE),
array(), $version
);
wp_register_script(
'aieo-product-video',
plugins_url('assets/js/product-display/product-video.js', AIEO_FILE),
array('jquery'), $version, true
);
if (function_exists('is_product') && is_product()) {
wp_enqueue_style('aieo-product-video');
}
}
public function admin_enqueue($hook) {
if (!in_array($hook, array('post.php','post-new.php'), true)) { return; }
$screen = function_exists('get_current_screen') ? get_current_screen() : null;
if (!$screen || $screen->post_type !== 'product') { return; }
}
public function add_meta_box() {
add_meta_box('aieo_product_video', __('Product Video', 'ai-eshop-optimizer'), array($this, 'render_meta_box'), 'product', 'side', 'default');
}
public function render_meta_box($post) {
wp_nonce_field('aieo_product_video', 'aieo_product_video_nonce');
$enable_featured = get_post_meta($post->ID, '_merchant_enable_featured_video', true);
$video_url = get_post_meta($post->ID, '_merchant_product_video_url', true);
$autoplay = get_post_meta($post->ID, '_merchant_product_video_autoplay', true);
$aspect_ratio = get_post_meta($post->ID, '_merchant_product_video_aspect_ratio', true);
if ($aspect_ratio === '') { $aspect_ratio = (string) $this->get_settings()['default_aspect_ratio']; }
$enable_gallery = get_post_meta($post->ID, '_merchant_enable_gallery', true);
$gallery_videos = get_post_meta($post->ID, '_merchant_videos_in_gallery', true);
$video_position = get_post_meta($post->ID, '_merchant_video_position', true);
?>
get_id() === get_queried_object_id();
if ($is_main) { return $classes; }
$url = (string) get_post_meta($product->get_id(), '_merchant_product_video_url', true);
if ($url !== '') { $classes[] = 'om-ppe-product-has-video'; }
return $classes;
}
public function add_featured_video($html, $post_thumbnail_id) {
global $product;
if (!$product) { return $html; }
$enable = (string) get_post_meta($product->get_id(), '_merchant_enable_featured_video', true);
$url = (string) get_post_meta($product->get_id(), '_merchant_product_video_url', true);
if ($enable === '' || $url === '') { return $html; }
if (!has_post_thumbnail() || get_post_thumbnail_id() !== $post_thumbnail_id) { return $html; }
ob_start(); $this->render_video($product->get_id()); $video_html = (string) ob_get_clean();
$poster = $this->oembed_thumb($url) ?: plugins_url('assets/images/product-display/video-placeholder.svg', AIEO_FILE);
return '' . $video_html . '
';
}
public function add_gallery_videos($html, $post_thumbnail_id) {
global $product;
if (!$product) { return $html; }
$enable = (string) get_post_meta($product->get_id(), '_merchant_enable_gallery', true);
$videos = (array) get_post_meta($product->get_id(), '_merchant_videos_in_gallery', true);
$videos = array_filter($videos);
if ($enable === '' || empty($videos)) { return $html; }
if ($this->embedded) { return $html; }
$position = (string) get_post_meta($product->get_id(), '_merchant_video_position', true);
$ar = (string) get_post_meta($product->get_id(), '_merchant_product_video_aspect_ratio', true);
if ($ar === '') { $ar = '16-9'; }
$ar_class = $ar !== 'auto' ? 'om-ppe-video-ratio om-ppe-video-ratio-' . $ar : 'om-ppe-video-ratio-auto';
$video_html = '';
foreach ($videos as $url) {
if ($url === '') { continue; }
$ft = wp_check_filetype($url, wp_get_mime_types());
$type = wp_ext2type($ft['ext']);
if ($type === 'video') {
$poster = plugins_url('assets/images/product-display/video-placeholder.svg', AIEO_FILE);
$video_html .= '';
$video_html .= '
';
$video_html .= '';
$video_html .= '
';
} else {
$oembed = _wp_oembed_get_object()->get_data($url);
if (!empty($oembed)) {
$video_html .= '';
$video_html .= '
';
$video_html .= _wp_oembed_get_object()->data2html($oembed, $url);
$video_html .= '
';
}
}
}
if ($video_html !== '') {
$html = ($position === 'after') ? $html . $video_html : $video_html . $html;
$this->embedded = true;
wp_enqueue_script('aieo-product-video');
}
return $html;
}
public function inject_block_gallery($block_content, $block) {
if (!is_singular('product')) { return $block_content; }
$product = isset($GLOBALS['product']) && $GLOBALS['product'] instanceof WC_Product ? $GLOBALS['product'] : wc_get_product(get_queried_object_id());
if (!$product) { return $block_content; }
$pid = $product->get_id();
$html = '';
if ('' !== (string) get_post_meta($pid, '_merchant_enable_featured_video', true) && '' !== (string) get_post_meta($pid, '_merchant_product_video_url', true)) {
ob_start(); $this->render_video($pid); $html .= (string) ob_get_clean();
}
$html .= $this->add_gallery_videos('', 0);
if ($html === '') { return $block_content; }
wp_enqueue_style('aieo-product-video');
wp_enqueue_script('aieo-product-video');
$wrap = '' . $html . '
';
$position = (string) get_post_meta($pid, '_merchant_video_position', true);
return ('after' === $position) ? $block_content . $wrap : $wrap . $block_content;
}
private function render_video($product_id) {
$url = (string) get_post_meta($product_id, '_merchant_product_video_url', true);
$autoplay = (string) get_post_meta($product_id, '_merchant_product_video_autoplay', true);
$ar = (string) get_post_meta($product_id, '_merchant_product_video_aspect_ratio', true);
if ($url === '') { return; }
if ($ar === '') { $ar = (string) $this->get_settings()['default_aspect_ratio']; }
$ar_class = $ar !== 'auto' ? 'om-ppe-video-ratio om-ppe-video-ratio-' . $ar : 'om-ppe-video-ratio-auto';
$autoplay_attr = $autoplay !== '' ? ' autoplay muted loop playsinline' : ' controls';
$ft = wp_check_filetype($url, wp_get_mime_types());
$type = wp_ext2type($ft['ext']);
if ($type === 'video') {
echo '';
echo '';
echo '
';
} else {
$oembed = _wp_oembed_get_object()->get_data($url);
if (!empty($oembed)) {
$oembed_html = (string) $oembed->html;
if ($autoplay !== '' && in_array(strtolower((string) ($oembed->provider_name ?? '')), array('youtube','vimeo','dailymotion'), true)) {
if (preg_match('/<\/iframe>/isU', $oembed_html, $m) && !empty($m[1])) {
$oembed_html = str_replace($m[1], add_query_arg(array(
'autoplay' => '1', 'mute' => '1', 'muted' => '1', 'controls' => '0',
), $m[1]), $oembed_html);
}
}
echo '';
echo wp_kses($oembed_html, array('iframe' => array('src' => true,'width' => true,'height' => true,'frameborder' => true,'allow' => true,'allowfullscreen' => true,'title' => true)));
echo '
';
}
}
}
private function oembed_thumb($url) {
$oembed = _wp_oembed_get_object()->get_data($url);
return !empty($oembed->thumbnail_url) ? (string) $oembed->thumbnail_url : '';
}
public function render_subtab() {
$s = $this->get_settings();
$form_id = 'aieo-card-product-video-overview-form';
list($open, $close) = AIEO_DMM_UI::card(array(
'anchor' => 'product-video-overview',
'icon' => 'video',
'title' => __('Aspect ratio and autoplay', 'ai-eshop-optimizer'),
'description' => __('Embed a video into the product gallery (YouTube / Vimeo / self-hosted). Per-product config lives on the product edit screen → "Product Video" meta box. Same `_merchant_*` post meta keys as the legacy module.', 'ai-eshop-optimizer'),
'col' => 1,
'footer' => \Rooster\Admin\States::save_button($form_id),
));
echo $open;
$ratios = array();
foreach (array('16-9','9-16','4-3','3-2','1-1','auto') as $r) {
$ratios[$r] = $r === 'auto' ? __('Auto', 'ai-eshop-optimizer') : str_replace('-', ':', $r);
}
$fields = \Rooster\Admin\Ui::field_row(array(
'id' => 'aieo-pv-enabled',
'label' => __('Enable', 'ai-eshop-optimizer'),
'control' => \Rooster\Admin\Ui::toggle('enabled', !empty($s['enabled']), array('id' => 'aieo-pv-enabled', 'hidden' => false, 'label' => __('On', 'ai-eshop-optimizer'), 'label_hidden' => true)),
));
$fields .= \Rooster\Admin\Ui::field_row(array(
'id' => 'aieo-pv-default-autoplay',
'label' => __('Default autoplay (per-product overridable)', 'ai-eshop-optimizer'),
'control' => \Rooster\Admin\Ui::toggle('default_autoplay', !empty($s['default_autoplay']), array('id' => 'aieo-pv-default-autoplay', 'hidden' => false, 'label' => __('On', 'ai-eshop-optimizer'), 'label_hidden' => true)),
));
$fields .= \Rooster\Admin\Ui::field_row(array(
'id' => 'aieo-pv-default-aspect-ratio',
'label' => __('Default aspect ratio', 'ai-eshop-optimizer'),
'control' => \Rooster\Admin\Ui::select('default_aspect_ratio', $ratios, (string) $s['default_aspect_ratio'], array('id' => 'aieo-pv-default-aspect-ratio')),
));
echo AIEO_DMM_UI::card_form('aieo_dmm_product_video_save', 'product_video', $form_id, '' . $fields . '
');
echo $close;
}
}