Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 74 additions & 12 deletions inc/classes/shortcodes/class-godam-player.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final protected function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
// Priority 20: must run after register_scripts() above has registered the handles.
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_lightbox_runtime' ), 20 );
// Priority 20: same reason — register_scripts() (priority 10) must have run first.
add_action( 'wp_enqueue_scripts', array( $this, 'preload_wpbakery_editor_assets' ), 20 );
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'wp_head', array( $this, 'godam_output_admin_player_css' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'godam_skin_styles_enqueue' ) );
Expand Down Expand Up @@ -285,6 +287,77 @@ public function maybe_enqueue_lightbox_runtime() {
wp_enqueue_style( 'godam-player-style' );
}

/**
* Preload the player runtime in the WPBakery inline editor.
*
* WPBakery adds/updates an element via AJAX and does NOT inject newly enqueued
* scripts into the running editor page. `render()` calls wp_enqueue_script()
* during that AJAX render, but the handle never reaches the live preview — so a
* GoDAM Video added to a page that had no player before stayed frozen on its
* poster/loading placeholder, and only turned into the real player after a full
* page reload (which re-renders the shortcode in content with the script present).
*
* Enqueue the player script (and its styles + the selected skin) up front in the
* inline editor so frontend.js — which already watches for builder-injected markup
* and re-initialises pending players — can bring a just-added video to life
* immediately, without a refresh. Editor-only; the published front end, which
* enqueues via render() as usual, is untouched. Mirrors the GoDAM Image element's
* preload_wpbakery_editor_assets().
*
* The analytics script (godam-player-analytics-script) is deliberately NOT
* preloaded here. Its only skip guard, shouldSkipAnalytics(), gates on is_admin()
* (false on the WPBakery frontend inline-editor page) and the video-preview query
* var (unset here), so it would not skip — every preview play/scrub while designing
* would send real view/engagement events and corrupt the video's stats. The player
* runs fine without it; the front end still loads analytics via render() as before.
*
* @since 2.2.2
*
* @return void
*/
public function preload_wpbakery_editor_assets() {
if ( ! function_exists( 'vc_is_inline' ) || ! vc_is_inline() ) {
return;
}
Comment thread
rudrakshi-gupta marked this conversation as resolved.
Resolved

// register_scripts() (priority 10) has already registered this handle.
if ( wp_script_is( 'godam-player-frontend-script', 'registered' ) ) {
wp_enqueue_script( 'godam-player-frontend-script' );
}

wp_enqueue_style( 'godam-player-style' );

// Match the active player skin so the editor preview looks like the front end.
$this->enqueue_active_skin_style();
}

/**
* Enqueue the stylesheet for the site's currently selected player skin, if any.
*
* Shared by render() (front end) and preload_wpbakery_editor_assets() (inline
* editor) so the skin map lives in one place and the editor preview can never
* silently diverge from the front end when a skin is added, renamed or removed.
* No-op when no skin (or the default base skin) is selected.
*
* @since 2.2.2
*
* @return void
*/
private function enqueue_active_skin_style() {
$godam_settings = get_option( 'rtgodam-settings', array() );
$selected_skin = isset( $godam_settings['video_player']['player_skin'] ) ? $godam_settings['video_player']['player_skin'] : '';
$skins = array(
'Minimal' => 'godam-player-minimal-skin',
'Pills' => 'godam-player-pills-skin',
'Bubble' => 'godam-player-bubble-skin',
'Classic' => 'godam-player-classic-skin',
);

if ( isset( $skins[ $selected_skin ] ) ) {
wp_enqueue_style( $skins[ $selected_skin ] );
}
}

/**
* Enqueue all player skin styles on the admin settings page.
*
Expand Down Expand Up @@ -448,18 +521,7 @@ public function render( $atts ) {
wp_enqueue_script( 'godam-player-analytics-script' );
wp_enqueue_style( 'godam-player-style' );

$godam_settings = get_option( 'rtgodam-settings', array() );
$selected_skin = isset( $godam_settings['video_player']['player_skin'] ) ? $godam_settings['video_player']['player_skin'] : '';
$skins = array(
'Minimal' => 'godam-player-minimal-skin',
'Pills' => 'godam-player-pills-skin',
'Bubble' => 'godam-player-bubble-skin',
'Classic' => 'godam-player-classic-skin',
);

if ( isset( $skins[ $selected_skin ] ) ) {
wp_enqueue_style( $skins[ $selected_skin ] );
}
$this->enqueue_active_skin_style();

ob_start();
require RTGODAM_PATH . 'inc/templates/godam-player.php';
Expand Down
Loading