'FV Player Shortcode2Database Conversion',
'slug' => 'shortcode2db',
) );
$this->conversion_limit = 1;
// atts for video that are supported
$this->supported_video_atts = array(
'src',
'src1',
'src2',
'splash',
);
// atts for player that are supported
$this->supported_player_atts = array(
'width',
'height',
'autoplay',
'lightbox',
'playlist',
'caption',
'subtitles',
'transcript',
'original_formatting',
'ab',
'chapters',
'controlbar',
'speed'
);
$this->supported_atts = array_merge( $this->supported_video_atts, $this->supported_player_atts );
$this->screen_fields = array(
'ID',
'Title',
'Post Type',
'Shortcode',
'Result',
'Error'
);
if( isset($_GET['fv-conversion-export']) && !empty($_GET['page']) && sanitize_key( $_GET['page'] ) === $this->screen ) {
add_action('admin_init', array( $this, 'csv_export' ) );
}
}
public function get_text( $type ) {
if ( 'help' === $type ) {
return __( "This converts the [fvplayer src=...] and [flowplayer src=...] shortcodes into database [fvplayer id=...] shortcodes.", 'fv-player' );
} else if ( 'start_warning' === $type ) {
return __( 'Please make sure you backup your database before continuing. You can use post revisions to get back to previous version of your posts as well.', 'fv-player' );
}
return parent::get_text( $type );
}
/**
* Get posts with [fvplayer/flowplayer src...] shortcodes
*
* @return object|null $result
*/
function get_items($offset, $limit) {
global $wpdb;
// Each row is the matching wp_posts row or wp_posts row with matching meta_value
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT SQL_CALC_FOUND_ROWS ID, post_author, post_date_gmt, post_status, post_title, post_type, post_content FROM {$wpdb->posts} AS p JOIN {$wpdb->postmeta} AS m ON p.ID = m.post_id WHERE post_status NOT IN ('inherit','trash') AND (post_content LIKE %s OR post_content LIKE %s) AND post_type NOT IN ('topic','reply') OR (meta_value LIKE %s OR meta_value LIKE %s) AND meta_key NOT LIKE %s AND meta_key NOT LIKE %s GROUP BY ID ORDER BY post_date_gmt DESC LIMIT %d, %d",
'%' . $wpdb->esc_like( "[fvplayer src=" ) . '%',
'%' . $wpdb->esc_like( "[flowplayer src=" ) . '%',
'%' . $wpdb->esc_like( "[fvplayer src=" ) . '%',
'%' . $wpdb->esc_like( "[flowplayer src=" ) . '%',
'%' . $wpdb->esc_like( "_fv_player_%_backup_" ) . '%',
'%' . $wpdb->esc_like( "_fv_player_%_failed_" ) . '%',
$offset,
$limit
)
);
return $results;
}
/**
* Converts all shortcodes to DB
*
* @param WP_Post $post
*
* @return arrray
*/
function convert_one( $post ) {
$content_updated = false; // track if content was updated
$new_content = $post->post_content; // copy of content for update
$output_data = array(); // output for html
$errors = array(); // all errors for export
if( !empty( $post->post_content) ) {
// match shortcodes in post_content, ignore shotcodes in double brackets like [[fvplayer src="https://your-website.com/videos/video.mp4"]]
preg_match_all( '~(?post_content, $matched_shortcodes );
if( !empty($matched_shortcodes) && !empty($matched_shortcodes[0]) ) {
foreach( $matched_shortcodes[0] as $shortcode ) {
$result = $this->worker( $shortcode, $post, $new_content );
if( $result ) {
$new_content = $result['new_content'];
if( $result['content_updated'] ) {
$content_updated = $result['content_updated'];
}
$errors = array_merge( $errors, $result['errors'] );
$output_data[] = $result['data'];
}
}
}
}
// match shortcodes also in post meta
$meta = get_post_custom( $post->ID );
if( is_array($meta) ) {
foreach( $meta AS $meta_key => $meta_values ) {
// Skip meta keys created by the conversion
if(
preg_match( '~^_fv_player_.*?_backup_~', $meta_key ) ||
preg_match( '~^_fv_player_.*?_failed$~', $meta_key )
) {
continue;
}
foreach( $meta_values AS $meta_value ) {
// Skip serialized meta values, it would be tricky to update
if( is_serialized($meta_value) ) {
continue;
}
$original_meta_value = $meta_value;
$meta_updated = false;
preg_match_all( '~(?worker( $shortcode, $post, $meta_value, $meta_key );
if( $result ) {
if( $result['content_updated'] ) {
$meta_updated = $result['content_updated'];
$meta_value = $result['new_content'];
}
$errors = array_merge( $errors, $result['errors'] );
$output_data[] = $result['data'];
}
}
}
if( $this->is_live() && $meta_updated ) {
update_post_meta( $post->ID, '_fv_player_'.$this->slug.'_backup_'.$meta_key, $original_meta_value );
update_post_meta( $post->ID, $meta_key, $meta_value, $original_meta_value );
}
}
}
}
return array(
'new_content' => $new_content,
'content_updated' => $content_updated,
'output_data' => $output_data,
'errors' => $errors
);
}
function conversion_button() {
?>
';
// var_export($import_player_atts);
// echo '';
// die();
if( $this->is_live() ) {
global $FV_Player_Db;
$player_id = $FV_Player_Db->import_player_data(false, false, $import_player_atts);
if( $player_id > 0 ) {
// echo "Inserted player #".$player_id."\n";
$new_content = str_replace( $shortcode , '[fvplayer id="'.$player_id.'"]', $new_content );
$content_updated = true;
$output_msg = "New FV Player #" . $player_id ;
} else {
$failed_msg = "Error saving FV Player instance";
$errors[] = array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'post_link' => get_permalink( $post->ID ),
'post_edit' => get_edit_post_link( $post->ID ),
'shortcode' => $shortcode,
'message' => $failed_msg
);
}
} else {
$output_msg = "Would create new FV Player";
}
}
$type = $post->post_type;
if( $meta_key ) {
$type .= ''.$meta_key.'';
}
return array(
'errors' => $errors,
'data' => array(
'ID' => $post->ID,
'title' => $post->post_title,
'type' => $type,
'shortcode' => $shortcode,
'output' => $output_msg,
'error' => $failed_msg
),
'new_content' => $new_content,
'content_updated' => $content_updated
);
}
function build_output_html($data, $percent_done) {
$html = array();
foreach( $data as $output_data ) {
$html[] = "