Created
May 26, 2022 14:42
-
-
Save chriscoyier/51e231b0a6b3631feb669a6dd06290ad to your computer and use it in GitHub Desktop.
One-Time Metadata Migration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Migrate Podcast Files to S3 | |
* Text Domain: cp-podcast-migrate | |
* Version: 1.0.0 | |
* | |
*/ | |
/** | |
* Add menu item | |
*/ | |
add_action( 'admin_menu', 'ssp_powerpress_add_menu_item' ); | |
function ssp_powerpress_add_menu_item() { | |
add_submenu_page( | |
'edit.php?post_type=podcast', | |
__( 'Migrate to S3', 'seriously-simple-podcasting' ), | |
__( 'Migrate to S3', 'seriously-simple-podcasting' ), | |
'manage_podcast', | |
'powerpress', | |
'ssp_powerpress_migrate_episodes' | |
); | |
} | |
function parse_file_path( $podcast ) { | |
/** | |
* Case insensitive match for any valid file url that ends with .mp3 | |
*/ | |
$title_parts = explode(":", $podcast->post_title); | |
$show_num = $title_parts[0]; | |
if ($show_num[0] == "#") { | |
// Strip it (not all shows start with this) | |
$show_num = substr($show_num, 1); | |
} | |
// strip leading zeros | |
$show_num = ltrim($show_num, "0"); | |
// Correct format should be: | |
// https://podcast.codepen.io/cpr-1.mp3 | |
// https://podcast.codepen.io/cpr-245.mp3 | |
return "https://podcast.codepen.io/cpr-" . $show_num . ".mp3"; | |
} | |
/** | |
* Migrate | |
*/ | |
function ssp_powerpress_migrate_episodes() { | |
/* | |
1860 | |
001: The Big Idea | |
http://codepen-podcast.s3.amazonaws.com/001.mp3 | |
*/ | |
$args = array( | |
// 'p' => 1860, | |
'post_type' => 'post', | |
'posts_per_page' => 299, | |
'post_status' => 'any', | |
'orderby' => 'post_date', | |
'order' => 'ASC', | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => 'audio_file', | |
'compare' => 'EXISTS', | |
), | |
array( | |
'key' => 'audio_file', | |
'value' => '', | |
'compare' => '!=', | |
), | |
), | |
); | |
$podcast_query = new WP_Query( $args ); | |
$podcasts = $podcast_query->get_posts(); | |
$html = '<div class="wrap">'; | |
$html .= '<h2>Power Press conversion completed.</h2>'; | |
if ( empty( $podcasts ) ) { | |
$html .= '<p>No podcasts to update.</p>'; | |
} | |
$inner_html = ''; | |
foreach ( $podcasts as $podcast ) { | |
$audio_file = get_post_meta( $podcast->ID, 'audio_file', true ); | |
$renamed_audio_file = parse_file_path( $podcast ); | |
if ( $audio_file === $renamed_audio_file ) { | |
continue; | |
} | |
$inner_html .= '<p>Original Audio file: ' . $audio_file . '</p>'; | |
update_post_meta( $podcast->ID, 'audio_file', $renamed_audio_file ); | |
$inner_html .= '<p>Renamed Audio file: ' . $renamed_audio_file . '</p>'; | |
} | |
if ( empty( $inner_html ) ) { | |
$html .= '<p>No audio files to rename</p>'; | |
} else { | |
$html .= $inner_html; | |
} | |
$html .= '</div>'; | |
echo $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment