diff --git a/controller/editor.php b/controller/editor.php index fa452ba4b..542c2e1d3 100755 --- a/controller/editor.php +++ b/controller/editor.php @@ -537,18 +537,78 @@ function getTitleFromUrl($url) { ); } - header('Content-Type: application/json'); + header('Content-Type: application/json'); echo json_encode($jsonReturn); - wp_die(); + wp_die(); } -/* -Elementor support -*/ +add_filter( 'save_post', 'fv_wp_flowplayer_convert_to_db', 9, 3 ); + +/** + * Convert shortcodes and links to DB on save + * + * @param int $post_id + * @param WP_Post $post + * @param bool $update + * + * @return void + */ +function fv_wp_flowplayer_convert_to_db($post_id, $post, $update) { + global $wp, $wp_embed, $fv_fp, $FV_Player_Shortcode2Database_Conversion; + + $is_classic_editor_save = !empty($_POST['action']) && $_POST['action'] === 'editpost' && !empty($_POST['post_ID']) && $_POST['post_ID'] == $post_id; + $is_gutenberg_post_save = !empty($wp->query_vars['rest_route']) && $wp->query_vars['rest_route'] == '/wp/v2/posts/'.$post_id; + + if( !$is_classic_editor_save && !$is_gutenberg_post_save ) { + return; + } + + // check if option is enabled + if( !$fv_fp->_get_option('convert_db_save') ) return; + + // ignore revision + if ( wp_is_post_revision( $post_id ) ) return; + + // ignore trash + if ( $post->post_status === 'trash' ) return; + + $new_content = $post->post_content; + + // if( is_serialized($new_content) ) return; // TODO: is something serializing content? + + // convert links to embed + $new_content = $wp_embed->autoembed( $new_content ); + + // convert iframe/video tags to src shortcodes + $new_content = fv_player_handle_video_tags($new_content); + $new_content = fv_player_handle_youtube_links($new_content); + $new_content = fv_player_handle_vimeo_links($new_content); + + $post->post_content = $new_content; + + $FV_Player_Shortcode2Database_Conversion->set_live = true; + + // convert src shortcodes to db + $new_content_data = $FV_Player_Shortcode2Database_Conversion->convert_one($post); + if( $new_content_data['content_updated'] ) { + $new_content = $new_content_data['new_content']; + } + + // remove current action to prevent infinite loop when using wp_update_post + remove_action( 'save_post', 'fv_wp_flowplayer_convert_to_db', 9 ); + wp_update_post( array( 'ID' => $post->ID, 'post_content' => $new_content ) ); +} + + + + +/** + * Elementor support + */ add_action( 'elementor/editor/wp_head', 'fv_player_shortcode_editor_scripts_enqueue' ); add_action( 'elementor/editor/wp_head', 'fv_wp_flowplayer_edit_form_after_editor' ); add_action( 'elementor/editor/wp_head', 'flowplayer_prepare_scripts' ); diff --git a/controller/shortcodes.php b/controller/shortcodes.php index bcdb5053c..7515676dc 100644 --- a/controller/shortcodes.php +++ b/controller/shortcodes.php @@ -315,7 +315,7 @@ function fv_flowplayer_optimizepress_bridge( $input ) { function fv_player_time( $args = array() ) { global $post, $fv_fp; - + if( !empty($args['id']) ) { $player = new FV_Player_Db_Player($args['id']); if( $player->getIsValid() ) { @@ -335,210 +335,202 @@ function fv_player_time( $args = array() ) { } -global $fv_fp; -if( ( empty($_POST['action']) || $_POST['action'] != 'parse-media-shortcode' ) && ( empty($_GET['action']) || $_GET['action'] != 'edit' ) && !empty($fv_fp->conf['integrations']['wp_core_video']) && $fv_fp->conf['integrations']['wp_core_video'] == 'true' ) { - - function fv_flowplayer_shortcode_video( $output ) { - $aArgs = func_get_args(); - $atts = $aArgs[1]; - - $bridge_atts = array(); - if( isset($atts['src']) ) { - $bridge_atts['src'] = $atts['src']; - } - - $count = 0; - foreach( array('mp4','webm','ogv','mov','flv','wmv','m4v','mp3') AS $key => $value ) { - if( !empty($atts[$value]) ) { - $src = 'src'.(( $count > 0 ) ? $count : ''); - $bridge_atts[$src] = $atts[$value]; - $count++; - } - } - - if( isset($atts['poster']) ) { - $bridge_atts['splash'] = $atts['poster']; - } - - if( isset($atts['loop']) && $atts['loop'] == 'on' ) { - $bridge_atts['loop'] = 'true'; - } else if( isset($atts['loop']) && $atts['loop'] == 'off' ) { - $bridge_atts['loop'] = 'false'; - } - - if( isset($atts['autoplay']) && $atts['autoplay'] == 'on' ) { - $bridge_atts['autoplay'] = 'true'; - } else if( isset($atts['loop']) && $atts['loop'] == 'off' ) { - $bridge_atts['autoplay'] = 'false'; - } - - if( isset($atts['width']) ) { - $bridge_atts['width'] = $atts['width']; - } - if( isset($atts['height']) ) { - $bridge_atts['height'] = $atts['height']; - } - - if( count($bridge_atts) == 0 ) { - return ""; +function fv_player_handle_vimeo_links( $html ) { + $html = preg_replace( '~]*?vimeo\.com/video/(\d+)[^>]*?(\?h=[a-z0-9]+)?[^>]*?>~', '[fvplayer src="http://vimeo.com/$1$2"]', $html ); + return $html; +} + +function fv_player_handle_youtube_links( $html ) { + $html = preg_replace( '~]*?youtube(?:-nocookie)?\.com/(?:embed|v)/(.*?)[\'"&#\?][^>]*?>~', '[fvplayer src="http://youtube.com/watch?v=$1"]', $html ); + return $html; +} + +function fv_player_handle_video_tags( $html ) { + $html = preg_replace( '~
]*?src\s*=\s*"(.+?)"[^>]*?>
~', '
[fvplayer src="$1"]
' , $html); + return $html; +} + +function fv_flowplayer_shortcode_video( $output ) { + $aArgs = func_get_args(); + $atts = $aArgs[1]; + + $bridge_atts = array(); + if( isset($atts['src']) ) { + $bridge_atts['src'] = $atts['src']; + } + + $count = 0; + foreach( array('mp4','webm','ogv','mov','flv','wmv','m4v','mp3') AS $key => $value ) { + if( !empty($atts[$value]) ) { + $src = 'src'.(( $count > 0 ) ? $count : ''); + $bridge_atts[$src] = $atts[$value]; + $count++; } - return flowplayer_content_handle( $bridge_atts, false, 'video' ); } - add_filter( 'wp_video_shortcode_override', 'fv_flowplayer_shortcode_video', 10, 4 ); - add_filter( 'wp_audio_shortcode_override', 'fv_flowplayer_shortcode_video', 10, 4 ); + if( isset($atts['poster']) ) { + $bridge_atts['splash'] = $atts['poster']; + } + if( isset($atts['loop']) && $atts['loop'] == 'on' ) { + $bridge_atts['loop'] = 'true'; + } else if( isset($atts['loop']) && $atts['loop'] == 'off' ) { + $bridge_atts['loop'] = 'false'; + } + if( isset($atts['autoplay']) && $atts['autoplay'] == 'on' ) { + $bridge_atts['autoplay'] = 'true'; + } else if( isset($atts['loop']) && $atts['loop'] == 'off' ) { + $bridge_atts['autoplay'] = 'false'; + } + if( isset($atts['width']) ) { + $bridge_atts['width'] = $atts['width']; + } + if( isset($atts['height']) ) { + $bridge_atts['height'] = $atts['height']; + } - function fv_flowplayer_shortcode_playlist( $output ) { - $aArgs = func_get_args(); - $atts = $aArgs[1]; + if( count($bridge_atts) == 0 ) { + return ""; + } + return flowplayer_content_handle( $bridge_atts, false, 'video' ); +} - // copy from wp-includes/media.php wp_playlist_shortcode() - global $post; - if ( ! empty( $attr['ids'] ) ) { - // 'ids' is explicitly ordered, unless you specify otherwise. - if ( empty( $attr['orderby'] ) ) { - $attr['orderby'] = 'post__in'; - } - $attr['include'] = $attr['ids']; - } - - $atts = shortcode_atts( array( - 'type' => 'audio', - 'order' => 'ASC', - 'orderby' => 'menu_order ID', - 'id' => $post ? $post->ID : 0, - 'include' => '', - 'exclude' => '', - 'style' => 'light', - 'tracklist' => true, - 'tracknumbers' => true, - 'images' => true, - 'artists' => true - ), $atts, 'playlist' ); - - $args = array( - 'post_status' => 'inherit', - 'post_type' => 'attachment', - 'post_mime_type' => $atts['type'], - 'order' => $atts['order'], - 'orderby' => $atts['orderby'] - ); +function fv_flowplayer_shortcode_playlist( $output ) { + $aArgs = func_get_args(); + $atts = $aArgs[1]; + + // copy from wp-includes/media.php wp_playlist_shortcode() + global $post; + if ( ! empty( $attr['ids'] ) ) { + // 'ids' is explicitly ordered, unless you specify otherwise. + if ( empty( $attr['orderby'] ) ) { + $attr['orderby'] = 'post__in'; + } + $attr['include'] = $attr['ids']; + } + + $atts = shortcode_atts( array( + 'type' => 'audio', + 'order' => 'ASC', + 'orderby' => 'menu_order ID', + 'id' => $post ? $post->ID : 0, + 'include' => '', + 'exclude' => '', + 'style' => 'light', + 'tracklist' => true, + 'tracknumbers' => true, + 'images' => true, + 'artists' => true + ), $atts, 'playlist' ); - if( !empty($atts['include']) ) { - $args['include'] = $atts['include']; - $_attachments = get_posts( $args ); - if( !count($_attachments) ) { - return false; - } + $args = array( + 'post_status' => 'inherit', + 'post_type' => 'attachment', + 'post_mime_type' => $atts['type'], + 'order' => $atts['order'], + 'orderby' => $atts['orderby'] + ); - $attachments = array(); - foreach( $_attachments as $key => $val ) { - $attachments[$val->ID] = $_attachments[$key]; - } - } else { + if( !empty($atts['include']) ) { + $args['include'] = $atts['include']; + $_attachments = get_posts( $args ); + if( !count($_attachments) ) { return false; } - - - $bridge_atts = array(); - $aPlaylistItems = array(); - $aPlaylistImages = array(); - $aPlaylistCaptions = array(); - $aPlaylistDurations = array(); - $i = 0; - foreach ( $attachments as $attachment ) { - $i++; - - $url = wp_get_attachment_url( $attachment->ID ); - if( $i == 1 ) { - $bridge_atts['src'] = $url; - } else { - $aPlaylistItems[] = $url; - } - - $thumb_id = get_post_thumbnail_id( $attachment->ID ); - $src = false; - if( !empty( $thumb_id ) ) { - list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); - } - if( $i == 1 ) { - $bridge_atts['splash'] = $src; - } else { - $aPlaylistImages[] = $src; - } - - $aPlaylistCaptions[] = str_replace( array('"',';'), '', flowplayer::esc_caption($attachment->post_title) ); - - $meta = wp_get_attachment_metadata( $attachment->ID ); - if( !empty($meta) ) { - if( !empty($meta['length']) ) { - $aPlaylistDurations[] = $meta['length']; - } - } + $attachments = array(); + foreach( $_attachments as $key => $val ) { + $attachments[$val->ID] = $_attachments[$key]; } + } else { + return false; + } + + $bridge_atts = array(); + $aPlaylistItems = array(); + $aPlaylistImages = array(); + $aPlaylistCaptions = array(); + $aPlaylistDurations = array(); + $i = 0; + foreach ( $attachments as $attachment ) { + $i++; - - $bridge_atts['playlist'] = ''; - foreach( $aPlaylistItems AS $key => $src ) { - $bridge_atts['playlist'] .= $src; - if( $aPlaylistImages[$key] ) { - $bridge_atts['playlist'] .= ','.$aPlaylistImages[$key]; - } - $bridge_atts['playlist'] .= ';'; + $url = wp_get_attachment_url( $attachment->ID ); + if( $i == 1 ) { + $bridge_atts['src'] = $url; + } else { + $aPlaylistItems[] = $url; } - $bridge_atts['playlist'] = trim($bridge_atts['playlist'],';'); - if( count($aPlaylistCaptions) > 1 || $atts['tracklist'] ) $bridge_atts['caption'] = implode(';',$aPlaylistCaptions); - $bridge_atts['durations'] = implode(';',$aPlaylistDurations); - - if( $atts['tracklist'] ) { - $bridge_atts['listshow'] = true; + + $thumb_id = get_post_thumbnail_id( $attachment->ID ); + $src = false; + if( !empty( $thumb_id ) ) { + list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); } + if( $i == 1 ) { + $bridge_atts['splash'] = $src; + } else { + $aPlaylistImages[] = $src; + } - if( isset($atts['width']) ) { - $bridge_atts['width'] = $atts['width']; - } - if( isset($atts['height']) ) { - $bridge_atts['height'] = $atts['height']; - } + $aPlaylistCaptions[] = str_replace( array('"',';'), '', flowplayer::esc_caption($attachment->post_title) ); - if( count($bridge_atts) == 0 ) { - return ""; + $meta = wp_get_attachment_metadata( $attachment->ID ); + if( !empty($meta) ) { + if( !empty($meta['length']) ) { + $aPlaylistDurations[] = $meta['length']; + } } - - if( !empty($atts['type']) && $atts['type'] == 'audio' ) { - $bridge_atts['liststyle'] = 'horizontal'; + + } + + + $bridge_atts['playlist'] = ''; + foreach( $aPlaylistItems AS $key => $src ) { + $bridge_atts['playlist'] .= $src; + if( $aPlaylistImages[$key] ) { + $bridge_atts['playlist'] .= ','.$aPlaylistImages[$key]; } - - return flowplayer_content_handle( $bridge_atts, false, 'video' ); + $bridge_atts['playlist'] .= ';'; + } + $bridge_atts['playlist'] = trim($bridge_atts['playlist'],';'); + if( count($aPlaylistCaptions) > 1 || $atts['tracklist'] ) $bridge_atts['caption'] = implode(';',$aPlaylistCaptions); + $bridge_atts['durations'] = implode(';',$aPlaylistDurations); + + if( $atts['tracklist'] ) { + $bridge_atts['listshow'] = true; } - add_filter( 'post_playlist', 'fv_flowplayer_shortcode_playlist', 10, 2 ); + if( isset($atts['width']) ) { + $bridge_atts['width'] = $atts['width']; + } + if( isset($atts['height']) ) { + $bridge_atts['height'] = $atts['height']; + } + + if( count($bridge_atts) == 0 ) { + return ""; + } + if( !empty($atts['type']) && $atts['type'] == 'audio' ) { + $bridge_atts['liststyle'] = 'horizontal'; + } + return flowplayer_content_handle( $bridge_atts, false, 'video' ); +} + +global $fv_fp; +if( ( empty($_POST['action']) || $_POST['action'] != 'parse-media-shortcode' ) && ( empty($_GET['action']) || $_GET['action'] != 'edit' ) && !empty($fv_fp->conf['integrations']['wp_core_video']) && $fv_fp->conf['integrations']['wp_core_video'] == 'true' ) { + add_filter( 'wp_video_shortcode_override', 'fv_flowplayer_shortcode_video', 10, 4 ); + add_filter( 'wp_audio_shortcode_override', 'fv_flowplayer_shortcode_video', 10, 4 ); + add_filter( 'post_playlist', 'fv_flowplayer_shortcode_playlist', 10, 2 ); add_filter( 'the_content', 'fv_player_handle_youtube_links' ); add_filter( 'the_content', 'fv_player_handle_vimeo_links' ); add_filter( 'embed_oembed_html', 'fv_player_handle_vimeo_links' ); add_filter( 'embed_oembed_html', 'fv_player_handle_youtube_links' ); - - function fv_player_handle_vimeo_links( $html ) { - $html = preg_replace( '~]*?vimeo\.com/video/(\d+)[^>]*?(\?h=[a-z0-9]+)?[^>]*?>~', '[fvplayer src="http://vimeo.com/$1$2"]', $html ); - return $html; - } - - function fv_player_handle_youtube_links( $html ) { - $html = preg_replace( '~]*?youtube(?:-nocookie)?\.com/(?:embed|v)/(.*?)[\'"&#\?][^>]*?>~', '[fvplayer src="http://youtube.com/watch?v=$1"]', $html ); - return $html; - } - add_filter( 'the_content', 'fv_player_handle_video_tags' ); - function fv_player_handle_video_tags( $html ) { - $html = preg_replace( '~
]*?src\s*=\s*"(.+?)"[^>]*?>
~', '
[fvplayer src="$1"]
' , $html); - return $html; - } } diff --git a/flowplayer.php b/flowplayer.php index 78679ca78..e8caacf72 100644 --- a/flowplayer.php +++ b/flowplayer.php @@ -85,7 +85,8 @@ global $fv_fp; $fv_fp = new flowplayer_frontend(); -if( is_admin() ) { +// If it's in wp-admin or if it's Gutenberg post saving +if( is_admin() || $_SERVER['REQUEST_METHOD'] == "POST" && preg_match( '~/wp-json/wp/v2/posts/\d+~', $_SERVER['REQUEST_URI'] ) ) { include_once( dirname( __FILE__ ) . '/controller/backend.php' ); include_once( dirname( __FILE__ ) . '/controller/editor.php' ); include_once( dirname( __FILE__ ) . '/controller/settings.php' ); diff --git a/models/conversion/conversion-base.class.php b/models/conversion/conversion-base.class.php index 8eaece204..89b13ca64 100644 --- a/models/conversion/conversion-base.class.php +++ b/models/conversion/conversion-base.class.php @@ -2,6 +2,8 @@ abstract class FV_Player_Conversion_Base { + public $set_live = false; + protected $matchers = array(); protected $title = 'Conversion'; protected $slug = 'conversion-slug'; @@ -216,7 +218,7 @@ function conversion_screen() { } function is_live() { - return !empty($_POST['make-changes']) && $_POST['make-changes'] == 'true'; + return (!empty($_POST['make-changes']) && $_POST['make-changes'] == 'true') || $this->set_live ; } } \ No newline at end of file diff --git a/models/conversion/shortcode2DB.class.php b/models/conversion/shortcode2DB.class.php index 9ca1f9d4c..432be0622 100644 --- a/models/conversion/shortcode2DB.class.php +++ b/models/conversion/shortcode2DB.class.php @@ -41,6 +41,8 @@ function __construct() { 'playlist', 'caption', 'subtitles', + 'transcript', + 'original_formatting', 'ab', 'chapters', 'controlbar', @@ -184,8 +186,10 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { $atts = shortcode_parse_atts( trim(rtrim($shortcode,']')) ); $import_video_atts = array(); $import_player_atts = array(); + $errors = array(); $import_atts = array(); + $errors = array(); unset( $atts[0] ); // remove [fvplayer or [flowplayer @@ -311,6 +315,19 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { $video_index++; } + } else if(strcmp( $player_att, 'transcript' ) == 0) { // transcript text + $transcript_text = $import_atts[$player_att]; + if( !empty($transcript_text) ) { + $import_video_atts[$video_index]['meta'][] = array( 'meta_key' => 'transcript_text', 'meta_value' => $transcript_text ); + } + + } else if(strcmp( $player_att, 'original_formatting' ) == 0) { // transcript original formatting + $transcript_original_formatting = $import_atts[$player_att]; + + if( $transcript_original_formatting === 'true' ) { + $import_video_atts[$video_index]['meta'][] = array( 'meta_key' => 'transcript_original_formatting', 'meta_value' => $transcript_original_formatting ); + } + } else if(strcmp( $player_att, 'ab' ) == 0) { // subtitles if( $import_atts[$player_att] == 'true' ) { $import_player_atts[$player_att] = 'on'; @@ -352,7 +369,7 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { $import_player_atts['status'] = 'published'; // add date_created - $import_player_atts['date_created'] = $post->post_date_gmt; + $import_player_atts['date_created'] = strtotime($post->post_date_gmt) > 0 ? $post->post_date_gmt : current_time( 'mysql' ); // add player_name // $import_player_atts['player_name'] = 'player_name'; @@ -391,7 +408,7 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { $output_msg = "Would create new FV Player"; } } - + $type = $post->post_type; if( $meta_key ) { $type .= '
meta_key: '.$meta_key.''; @@ -414,4 +431,5 @@ function worker( $shortcode, $post, $new_content, $meta_key = false ) { } -new FV_Player_Shortcode2Database_Conversion; \ No newline at end of file +global $FV_Player_Shortcode2Database_Conversion; +$FV_Player_Shortcode2Database_Conversion = new FV_Player_Shortcode2Database_Conversion; \ No newline at end of file diff --git a/view/admin.php b/view/admin.php index 0dabcbc5d..b98254e92 100644 --- a/view/admin.php +++ b/view/admin.php @@ -603,6 +603,7 @@ function fv_flowplayer_admin_integrations() { _get_checkbox(__('Always use fixed size player', 'fv-wordpress-flowplayer'), 'fixed_size', __('Enable to force video size at cost of loosing the video responsiveness.', 'fv-wordpress-flowplayer') ); ?> + _get_checkbox(__('Database conversion', 'fv-wordpress-flowplayer'), 'convert_db_save', __('Convert [fvplayer src="..."] shortcodes, [video] shortcodes, Vimeo and YouTube links to database-driven FV Player when post is saved.', 'fv-wordpress-flowplayer') ); ?> _get_checkbox(__('Disable saving skin CSS to a static file', 'fv-wordpress-flowplayer'), 'css_disable', __('Normally the player CSS configuration is stored in wp-content/fv-player-custom/style-{blog_id}.css.', 'fv-wordpress-flowplayer'), __('We do this to avoid a big style tag in your site <head>. Don\'t edit this file though, as it will be overwritten by plugin update or saving its options!','fv-wordpress-flowplayer' )); ?>