| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class FV_Player_Conversion { |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_action( 'admin_notices', array( $this, 'convert__start') ); |
| 11 |
add_action( 'fv_player_conversion_buttons', array( $this, 'conversion_button') ); |
| 12 |
} |
| 13 |
|
| 14 |
public function conversion_button() { |
| 15 |
?> |
| 16 |
<tr> |
| 17 |
<td><label>Convert JW Player shortcodes to <code>[fvplayer]</code>:</label></td> |
| 18 |
<td> |
| 19 |
<p class="description"> |
| 20 |
<input type="button" class="button" value="<?php esc_attr_e('Convert JW Player shortcodes', 'fv-player-pro'); ?>" style="margin-top: 2ex;" onclick="if( confirm('<?php esc_attr_e('This converts the [jwplayer] shortcodes into [fvplayer] shortcodes.\n\n 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-pro'); ?>') ) location.href='<?php echo wp_nonce_url( site_url('wp-admin/admin.php?page=fvplayer'), 'convert_jwplayer', 'convert_jwplayer'); ?>'; "/> |
| 21 |
</p> |
| 22 |
</td> |
| 23 |
</tr> |
| 24 |
<?php |
| 25 |
} |
| 26 |
|
| 27 |
function convert__start() { |
| 28 |
if( current_user_can('manage_options') && isset($_GET['convert_jwplayer']) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['convert_jwplayer'] ) ),'convert_jwplayer') ) { |
| 29 |
$this->convert__process('jwplayer'); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
function convert__process( $type = false ) { |
| 34 |
echo '<p>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.</p>'; |
| 35 |
echo '<p>Scroll down to the end of the list to see the status.</p>'; |
| 36 |
|
| 37 |
$sType = sanitize_title($type); |
| 38 |
|
| 39 |
global $wpdb; |
| 40 |
$aPosts = $wpdb->get_results( |
| 41 |
$wpdb->prepare( |
| 42 |
"SELECT * FROM {$wpdb->posts} WHERE post_status != 'inherit' AND post_content LIKE %s ORDER BY post_date DESC", |
| 43 |
'%' . $wpdb->esc_like( $sType ) . '%' |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
$tMax = ini_get('max_execution_time') ? ini_get('max_execution_time') : 30; |
| 48 |
$tStart = microtime(true); |
| 49 |
$iCount = 0; |
| 50 |
$iFound = 0; |
| 51 |
|
| 52 |
echo "<ul>\n"; |
| 53 |
foreach( $aPosts AS $objPost ) { |
| 54 |
if( microtime(true) - $tStart > ($tMax - 2) ) { |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
echo "<li><strong>" . esc_html( $objPost->post_title ) . '</strong> (' . intval( $objPost->ID ) . ') '; |
| 59 |
|
| 60 |
$method = 'convert__'.$sType.'_callback'; |
| 61 |
$new_content = $this->$method($objPost->post_content); |
| 62 |
|
| 63 |
if( strlen($new_content) != strlen($objPost->post_content) || $new_content != $objPost->post_content ) { |
| 64 |
$iFound++; |
| 65 |
|
| 66 |
$post_id = wp_update_post( array( 'ID' => $objPost->ID, 'post_content' => $new_content ) ); |
| 67 |
if( is_wp_error($post_id) ) { |
| 68 |
$errors = $post_id->get_error_messages(); |
| 69 |
echo "Error: "; |
| 70 |
foreach ($errors as $error) { |
| 71 |
echo esc_html( $error ); |
| 72 |
} |
| 73 |
echo "</li>"; |
| 74 |
} else { |
| 75 |
$iCount++; |
| 76 |
echo "<a target='_blank' href='".get_permalink($objPost->ID)."'>".$objPost->post_title."</a> updated ok</li>\n"; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
} |
| 81 |
echo "</ul>\n"; |
| 82 |
|
| 83 |
if( $iFound == 0 ) { |
| 84 |
echo "<p>No more posts with " . esc_html( $type )." embeds found!</p>\n"; |
| 85 |
} else { |
| 86 |
echo "<p>Updated " . intval( $iCount ) . " posts out of " . intval( $iFound ) . " posts with " . esc_html( $type ) . " embeds.</p>\n"; |
| 87 |
} |
| 88 |
|
| 89 |
if( microtime(true) - $tStart > ($tMax - 5) ) { |
| 90 |
echo "<p><strong>Execution terminated</strong>: PHP max_execution_time reached, run this process again to process the remaining posts!</p>\n"; |
| 91 |
} else { |
| 92 |
echo "<p>All done!</p>\n"; |
| 93 |
} |
| 94 |
|
| 95 |
die(); |
| 96 |
} |
| 97 |
|
| 98 |
function convert__jwplayer_callback( $content ) { |
| 99 |
$content = preg_replace_callback( '~\[jwplayer.*?\]~', array( $this, 'convert__jwplayer_callback_parse' ), $content ); |
| 100 |
return $content; |
| 101 |
} |
| 102 |
|
| 103 |
function convert__jwplayer_callback_parse( $aMatch ) { |
| 104 |
|
| 105 |
echo '<p>Replacing <code>' . esc_html( $aMatch[0] ) . '</code><p>'; |
| 106 |
|
| 107 |
$bGotSomething = false; |
| 108 |
|
| 109 |
$aFVPlayer = array(); |
| 110 |
|
| 111 |
$shortcode = rtrim($aMatch[0],']'); |
| 112 |
|
| 113 |
$aJW = shortcode_parse_atts($shortcode); |
| 114 |
|
| 115 |
if( !empty($aJW['playlist']) ) { |
| 116 |
echo "<p><code>playlist</code> argument not supported!</p>"; |
| 117 |
return $aMatch[0]; |
| 118 |
} |
| 119 |
|
| 120 |
if( !empty($aJW['file']) ) { |
| 121 |
if( stripos($aJW['file'],'.xml') !== false ) { |
| 122 |
echo "<p><code>XML MRSS</code> not supported!</p>"; |
| 123 |
return $aMatch[0]; |
| 124 |
} |
| 125 |
|
| 126 |
$bGotSomething = true; |
| 127 |
$aFVPlayer['src'] = $aJW['file']; |
| 128 |
} |
| 129 |
if( !empty($aJW['image']) ) $aFVPlayer['splash'] = $aJW['image']; |
| 130 |
|
| 131 |
if( !empty($aJW['playlistid']) ) { |
| 132 |
$sPlaylistItems = get_post_meta( $aJW['playlistid'], 'jwplayermodule_playlist_items', true ); |
| 133 |
if( !$sPlaylistItems ) $sPlaylistItems = get_post_meta( $aJW['playlistid'], 'jwplayermodule_playlist_items', true ); |
| 134 |
|
| 135 |
$aAttachments = get_posts( array( 'include' => $sPlaylistItems, 'orderby' => 'post__in', 'post_type' => 'attachment' ) ); |
| 136 |
if( count($aAttachments) > 0 ) { |
| 137 |
$iCount = 0; |
| 138 |
$aFVPlaylist = array(); |
| 139 |
$aFVCaptions = array(); |
| 140 |
foreach( $aAttachments AS $objAttachment ) { |
| 141 |
$src = get_post_meta($objAttachment->ID,'_wp_attached_file',true); |
| 142 |
$src = $this->get_full_url($src); |
| 143 |
|
| 144 |
$splash = $this->parse_jwplayer_image($objAttachment->ID); |
| 145 |
|
| 146 |
if( $iCount == 0 ) { |
| 147 |
$aFVPlayer['src'] = $src; |
| 148 |
if( $splash ) $aFVPlayer['splash'] = $splash; |
| 149 |
} |
| 150 |
else { |
| 151 |
$item = $src; |
| 152 |
if( $splash ) { |
| 153 |
$src .= ','.$splash; |
| 154 |
} |
| 155 |
$aFVPlaylist[] = $src; |
| 156 |
} |
| 157 |
|
| 158 |
$aFVCaptions[] = str_replace( array('"',';'), '', flowplayer::esc_caption($objAttachment->post_title) ); |
| 159 |
$iCount++; |
| 160 |
} |
| 161 |
$aFVPlayer['playlist'] = implode(';',$aFVPlaylist); |
| 162 |
$aFVPlayer['caption'] = implode(';',$aFVCaptions); |
| 163 |
$bGotSomething = true; |
| 164 |
} |
| 165 |
|
| 166 |
} else if( !empty($aJW['mediaid']) ) { |
| 167 |
$objAttachment = get_post($aJW['mediaid']); |
| 168 |
if( $objAttachment ) { |
| 169 |
$src = get_post_meta($objAttachment->ID,'_wp_attached_file',true); |
| 170 |
$src = $this->get_full_url($src); |
| 171 |
|
| 172 |
$splash = $this->parse_jwplayer_image($objAttachment->ID); |
| 173 |
|
| 174 |
$aFVPlayer['src'] = $src; |
| 175 |
if( $splash ) $aFVPlayer['splash'] = $splash; |
| 176 |
$aFVPlayer['caption'] = flowplayer::esc_caption($objAttachment->post_title); |
| 177 |
$bGotSomething = true; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if( !$bGotSomething ) { |
| 182 |
echo "<p>No arguments recognized!</p>"; |
| 183 |
return $aMatch[0]; |
| 184 |
} |
| 185 |
|
| 186 |
$aShortcode = array(); |
| 187 |
foreach( $aFVPlayer AS $k => $v ) { |
| 188 |
$aShortcode[] = $k.'="'.$v.'"'; |
| 189 |
} |
| 190 |
|
| 191 |
$out = '[fvplayer '.implode(' ',$aShortcode).']'; |
| 192 |
echo '<p>With <code>' . esc_html( $out ) . '</code><p>'; |
| 193 |
return $out; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
function get_full_url( $url ) { |
| 198 |
if( stripos($url,'://') === false && stripos($url,'/') !== 0 ) { |
| 199 |
$aUploads = wp_upload_dir(); |
| 200 |
$url = trailingslashit($aUploads['baseurl']).$url; |
| 201 |
} |
| 202 |
return $url; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
function parse_jwplayer_image( $attachment_id ) { |
| 207 |
$splash_id = get_post_meta($attachment_id,'jwplayermodule_thumbnail',true); |
| 208 |
$splash_url = false; |
| 209 |
|
| 210 |
if( is_numeric($splash_id) ) { |
| 211 |
if( $featured_image = wp_get_attachment_image_src( $splash_id, 'large' ) ) { |
| 212 |
$splash_url = $featured_image[0]; |
| 213 |
} |
| 214 |
} else if( !$splash_id ) { |
| 215 |
if( $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($attachment_id), 'large' ) ) { |
| 216 |
$splash_url = $featured_image[0]; |
| 217 |
} |
| 218 |
} else { |
| 219 |
$splash_url = $this->get_full_url($splash_id); |
| 220 |
} |
| 221 |
return $splash_url; |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
$FV_Player_Conversion = new FV_Player_Conversion(); |
| 227 |
|