Convert JW Player shortcodes to [fvplayer]:
convert__process('jwplayer');
}
}
function convert__process( $type = false ) {
echo 'Running the conversion process for ' . esc_html( $type ) . '. If anything fails, remember to restore your backup or revert the change of the post to the previous revision.
';
echo 'Scroll down to the end of the list to see the status.
';
$sType = sanitize_title($type);
global $wpdb;
$aPosts = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_status != 'inherit' AND post_content LIKE %s ORDER BY post_date DESC",
'%' . $wpdb->esc_like( $sType ) . '%'
)
);
$tMax = ini_get('max_execution_time') ? ini_get('max_execution_time') : 30;
$tStart = microtime(true);
$iCount = 0;
$iFound = 0;
echo "\n";
foreach( $aPosts AS $objPost ) {
if( microtime(true) - $tStart > ($tMax - 2) ) {
break;
}
echo "" . esc_html( $objPost->post_title ) . ' (' . intval( $objPost->ID ) . ') ';
$method = 'convert__'.$sType.'_callback';
$new_content = $this->$method($objPost->post_content);
if( strlen($new_content) != strlen($objPost->post_content) || $new_content != $objPost->post_content ) {
$iFound++;
$post_id = wp_update_post( array( 'ID' => $objPost->ID, 'post_content' => $new_content ) );
if( is_wp_error($post_id) ) {
$errors = $post_id->get_error_messages();
echo "Error: ";
foreach ($errors as $error) {
echo esc_html( $error );
}
echo " ";
} else {
$iCount++;
echo "".$objPost->post_title." updated ok\n";
}
}
}
echo " \n";
if( $iFound == 0 ) {
echo "No more posts with " . esc_html( $type )." embeds found!
\n";
} else {
echo "Updated " . intval( $iCount ) . " posts out of " . intval( $iFound ) . " posts with " . esc_html( $type ) . " embeds.
\n";
}
if( microtime(true) - $tStart > ($tMax - 5) ) {
echo "Execution terminated : PHP max_execution_time reached, run this process again to process the remaining posts!
\n";
} else {
echo "All done!
\n";
}
die();
}
function convert__jwplayer_callback( $content ) {
$content = preg_replace_callback( '~\[jwplayer.*?\]~', array( $this, 'convert__jwplayer_callback_parse' ), $content );
return $content;
}
function convert__jwplayer_callback_parse( $aMatch ) {
echo 'Replacing ' . esc_html( $aMatch[0] ) . '
';
$bGotSomething = false;
$aFVPlayer = array();
$shortcode = rtrim($aMatch[0],']');
$aJW = shortcode_parse_atts($shortcode);
if( !empty($aJW['playlist']) ) {
echo "
playlist argument not supported!
";
return $aMatch[0];
}
if( !empty($aJW['file']) ) {
if( stripos($aJW['file'],'.xml') !== false ) {
echo "XML MRSS not supported!
";
return $aMatch[0];
}
$bGotSomething = true;
$aFVPlayer['src'] = $aJW['file'];
}
if( !empty($aJW['image']) ) $aFVPlayer['splash'] = $aJW['image'];
if( !empty($aJW['playlistid']) ) {
$sPlaylistItems = get_post_meta( $aJW['playlistid'], 'jwplayermodule_playlist_items', true );
if( !$sPlaylistItems ) $sPlaylistItems = get_post_meta( $aJW['playlistid'], 'jwplayermodule_playlist_items', true );
$aAttachments = get_posts( array( 'include' => $sPlaylistItems, 'orderby' => 'post__in', 'post_type' => 'attachment' ) );
if( count($aAttachments) > 0 ) {
$iCount = 0;
$aFVPlaylist = array();
$aFVCaptions = array();
foreach( $aAttachments AS $objAttachment ) {
$src = get_post_meta($objAttachment->ID,'_wp_attached_file',true);
$src = $this->get_full_url($src);
$splash = $this->parse_jwplayer_image($objAttachment->ID);
if( $iCount == 0 ) {
$aFVPlayer['src'] = $src;
if( $splash ) $aFVPlayer['splash'] = $splash;
}
else {
$item = $src;
if( $splash ) {
$src .= ','.$splash;
}
$aFVPlaylist[] = $src;
}
$aFVCaptions[] = str_replace( array('"',';'), '', flowplayer::esc_caption($objAttachment->post_title) );
$iCount++;
}
$aFVPlayer['playlist'] = implode(';',$aFVPlaylist);
$aFVPlayer['caption'] = implode(';',$aFVCaptions);
$bGotSomething = true;
}
} else if( !empty($aJW['mediaid']) ) {
$objAttachment = get_post($aJW['mediaid']);
if( $objAttachment ) {
$src = get_post_meta($objAttachment->ID,'_wp_attached_file',true);
$src = $this->get_full_url($src);
$splash = $this->parse_jwplayer_image($objAttachment->ID);
$aFVPlayer['src'] = $src;
if( $splash ) $aFVPlayer['splash'] = $splash;
$aFVPlayer['caption'] = flowplayer::esc_caption($objAttachment->post_title);
$bGotSomething = true;
}
}
if( !$bGotSomething ) {
echo "No arguments recognized!
";
return $aMatch[0];
}
$aShortcode = array();
foreach( $aFVPlayer AS $k => $v ) {
$aShortcode[] = $k.'="'.$v.'"';
}
$out = '[fvplayer '.implode(' ',$aShortcode).']';
echo 'With ' . esc_html( $out ) . '
';
return $out;
}
function get_full_url( $url ) {
if( stripos($url,'://') === false && stripos($url,'/') !== 0 ) {
$aUploads = wp_upload_dir();
$url = trailingslashit($aUploads['baseurl']).$url;
}
return $url;
}
function parse_jwplayer_image( $attachment_id ) {
$splash_id = get_post_meta($attachment_id,'jwplayermodule_thumbnail',true);
$splash_url = false;
if( is_numeric($splash_id) ) {
if( $featured_image = wp_get_attachment_image_src( $splash_id, 'large' ) ) {
$splash_url = $featured_image[0];
}
} else if( !$splash_id ) {
if( $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($attachment_id), 'large' ) ) {
$splash_url = $featured_image[0];
}
} else {
$splash_url = $this->get_full_url($splash_id);
}
return $splash_url;
}
}
$FV_Player_Conversion = new FV_Player_Conversion();