Created
April 4, 2019 19:43
-
-
Save lgedeon/7358c5cbece8268ca5bd0de1b60b3c0a to your computer and use it in GitHub Desktop.
Discarded code that may someday be relevant.
This file contains hidden or 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 | |
/** | |
* Content Cleanup Filter Commands. | |
*/ | |
class IOneContentClean extends WPCOM_VIP_CLI_Command { | |
/** | |
* Run selected the_content filters and save back to post content. | |
* | |
* ## OPTIONS | |
* | |
* [--dry-run] | |
* : Set dry-run to false to make changes permanent. | |
* | |
* ## EXAMPLES | |
* | |
* $ wp ione-content-cleanup filter | |
* $ wp ione-content-cleanup filter --dry-run=false | |
*/ | |
public function filter( $args, $assoc_args ) { | |
$dry_run = $this->is_dry_run( $args ); | |
// Filters that do not require setup. | |
$filters = [ | |
'globalgrind_404_filter_the_content', | |
'network_partial_url_filter_replace', | |
]; | |
if ( function_exists( 'ione_handle_legacy_shortcodes' ) ) { | |
$filters[] = 'ione_handle_legacy_shortcodes'; | |
} | |
// Set up for other filters. | |
if ( class_exists( 'IOne3_Https' ) ) { | |
$class_https = ione_singleton( 'IOne3_Https' ); | |
$class_https->init_hooks(); | |
$filters[] = [ $class_https, 'adjust_link_scheme' ]; | |
} | |
if ( class_exists( 'iOne3_Image_Date_Filter' ) ) { | |
$class_IDF = ione_singleton( 'iOne3_Image_Date_Filter' ); | |
$filters[] = [ $class_IDF, 'image_date_check_remove' ]; | |
} | |
if ( class_exists( 'iOne_Migrate_Subdomains' ) ) { | |
$class_migrate = iOne_Migrate_Subdomains::get_instance(); | |
$class_migrate->init(); | |
$filters[] = [ $class_migrate, 'replace_content_urls' ]; | |
} | |
if ( class_exists( 'iOne_Strip_Styles' ) ) { | |
$class_strip = ione_singleton( 'iOne_Strip_Styles' ); | |
$class_strip->init_hooks(); | |
$filters[] = [ $class_strip, 'filter_the_content' ]; | |
} | |
if ( class_exists( 'Ione3_Check_Body_Headings' ) ) { | |
$class_headings = ione_singleton( 'Ione3_Check_Body_Headings' ); | |
$filters[] = [ $class_headings, 'filter_the_content' ]; | |
} | |
if ( class_exists( 'iOne_Media_Playlist' ) ) { | |
add_action( 'ione3-content-clean-cli-post', [ $this, 'remove_attachments' ], 10, 2 ); | |
} | |
// Image removal reporting. | |
add_action( 'ione3_image_date_filter_removed', function( $img_url ) use ( $dry_run ) { | |
if ( $dry_run ) { | |
WP_CLI::log( sprintf( 'Image: %s would be removed from post id: %d', $img_url, get_the_ID() ) ); | |
} else { | |
WP_CLI::log( sprintf( 'Image: %s removed from post id: %d', $img_url, get_the_ID() ) ); | |
} | |
} ); | |
WP_CLI::log( sprintf( 'Running the following filters on the content of: %s', home_url() ) ); | |
WP_CLI::log( implode( "\n", $this->list_callbacks( $filters ) ) ); | |
$this->apply_filters( $filters, $dry_run ); | |
} | |
/** | |
* Convert a list of callables into human readable references. | |
* | |
* @param array $callbacks List of callables. | |
* | |
* @return array | |
*/ | |
protected function list_callbacks( $callbacks ) { | |
return array_map( function( $callback ) { | |
if ( is_array( $callback ) && ! empty( $callback[1] ) ) { | |
return sprintf( '%s::%s', get_class( $callback[0] ), (string) $callback[1] ); | |
} | |
return (string) $callback; | |
}, $callbacks ); | |
} | |
/** | |
* Apply the given filters to the content of all posts on the site. | |
* | |
* @param array $filters Array of callables. | |
* @param bool $dry_run True if not actually making any changes. | |
*/ | |
private function apply_filters( $filters, $dry_run ) { | |
if ( $dry_run ) { | |
WP_CLI::warning( 'This is a dry-run. Add --dry-run=false to run the actual thing.' ); | |
} | |
$paged = 1; | |
$post_types = get_post_types(); // Because 'any' doesn't get all. | |
$this->start_bulk_operation(); | |
do { | |
$query = new WP_Query( [ | |
'posts_per_page' => 100, | |
'post_status' => 'any', | |
'post_type' => $post_types, | |
'paged' => $paged, | |
'cache_results' => false, | |
'ignore_sticky_posts' => true, | |
'update_post_term_cache' => false, | |
'update_post_meta_cache' => false, | |
'suppress_filters' => true // phpcs:ignore WordPressVIPMinimum.VIP.WPQueryParams.suppressFiltersTrue -- Okay for CLI. | |
] ); | |
while ( $query->have_posts() ) { | |
$query->the_post(); | |
$post = get_post(); | |
$content = $post->post_content; | |
foreach ( $filters as $filter ) { | |
$content = call_user_func( $filter, $content ); | |
} | |
if ( $content !== $post->post_content ) { | |
if ( $dry_run ) { | |
WP_CLI::log( sprintf( 'Post ID: %d would have the content updated to: %s', $post->ID, $content ) ); | |
} elseif ( $this->update_content( $post, $content ) ) { | |
WP_CLI::log( sprintf( 'Post ID: %d updated.', $post->ID ) ); | |
} else { | |
WP_CLI::log( sprintf( 'Post ID: %d failed to update.', $post->ID ) ); | |
} | |
} | |
// Clean-up other fields of a post. | |
do_action( 'ione3-content-clean-cli-post', $post, $dry_run ); | |
} | |
WP_CLI::log( 'Preparing next batch' ); | |
sleep( 1 ); | |
$this->stop_the_insanity(); | |
$paged++; | |
} while ( $query->post_count ); | |
WP_CLI::success( 'Complete' ); | |
$this->end_bulk_operation(); | |
} | |
/** | |
* @param WP_Post $post Post object. | |
* @param string $content New content. | |
* | |
* @return bool True on success | |
*/ | |
private function update_content( $post, $content ) { | |
$update = [ | |
'ID' => $post->ID, | |
'post_content' => $content, | |
]; | |
return ( wp_update_post( $update ) === $post->ID ); | |
} | |
/** | |
* Remove any out of date media. | |
* | |
* @param WP_Post $post Post that may have attachments that are out of date. | |
* @param boolean $dry_run Whether to really delete or not. | |
*/ | |
public function remove_attachments( $post, $dry_run ) { | |
$class_media_playlist = ione_singleton( 'iOne_Media_Playlist' ); | |
// If this is a playlist item that is no longer available, delete it. | |
if ( in_array( $post->post_type, $class_media_playlist->item_post_types, true ) ) { | |
$image_src = get_post_meta( $post->ID, $class_media_playlist->src_meta_key, true ); | |
if ( ! $this->image_available( $image_src ) ) { | |
if ( $dry_run ) { | |
WP_CLI::log( sprintf( 'Playlist item: %d with media %s would have been removed.', $post->ID, $image_src ) ); | |
} elseif ( $this->delete_post_with_attachments( $post->ID ) ) { | |
WP_CLI::log( sprintf( 'Playlist item: %d with media %s removed.', $post->ID, $image_src ) ); | |
} else { | |
WP_CLI::log( sprintf( 'Playlist item: %d with media %s failed to remove.', $post->ID, $image_src ) ); | |
} | |
} | |
} | |
// If this is a playlist, clean up the items. | |
if ( $class_media_playlist->post_type === $post->post_type ) { | |
$items = $class_media_playlist->playlist_items( $post->ID ); | |
$filtered_items = []; | |
foreach ( $items as $item ) { | |
if ( isset( $item['src'] ) && $this->image_available( $item['src'] ) ) { | |
$filtered_items[] = $item; | |
} | |
} | |
if ( empty( $filtered_items ) && '2014' > $post->post_date ) { | |
if ( $dry_run ) { | |
WP_CLI::log( sprintf( 'Playlist: %d with items %s would have been removed.', $post->ID, wp_json_encode( $items ) ) ); | |
} elseif ( $this->four_ten_playlist( $post->ID ) ) { | |
WP_CLI::log( sprintf( 'Playlist: %d removed.', $post->ID ) ); | |
} else { | |
WP_CLI::log( sprintf( 'Playlist: %d failed to remove.', $post->ID ) ); | |
} | |
} elseif ( $filtered_items !== $items ) { | |
if ( $dry_run ) { | |
WP_CLI::log( sprintf( 'Playlist: %d would have all items except %s removed.', $post->ID, wp_json_encode( $filtered_items ) ) ); | |
} elseif ( update_post_meta( $post->ID, 'playlist_items', $filtered_items ) ) { | |
WP_CLI::log( sprintf( 'Playlist: %d has all items except %s removed.', $post->ID, wp_json_encode( $filtered_items ) ) ); | |
} else { | |
WP_CLI::log( sprintf( 'Playlist: %d failed to update.', $post->ID ) ); | |
} | |
} | |
} | |
// If featured image is not available, remove it. | |
$image_src = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); | |
if ( ! $this->image_available( $image_src ) ) { | |
if ( $dry_run ) { | |
WP_CLI::log( sprintf( 'Thumbnail: %s would have been removed from post %d.', $image_src, $post->ID ) ); | |
} elseif ( delete_post_thumbnail( $post->ID ) ) { | |
WP_CLI::log( sprintf( 'Thumbnail: %s removed from post %d.', $image_src, $post->ID ) ); | |
} else { | |
WP_CLI::log( sprintf( 'Thumbnail: %s failed to remove from post %d.', $image_src, $post->ID ) ); | |
} | |
} | |
} | |
/** | |
* Images are available unless the ione3_helper_image swaps it for the image named ImageUnavailable. | |
* | |
* @param string $image_src Url of image. | |
* | |
* @return bool | |
*/ | |
private function image_available( $image_src ) { | |
$check = apply_filters( 'ione3_helper_image', $image_src ); | |
return ( false === strpos( $check, 'ImageUnavailable.' ) ); | |
} | |
/** | |
* Delete a post and all of its attachments. | |
* | |
* @param integer $post_id Post to remove permanently. | |
* @param bool $force_delete Permanently delete or just send to trash. | |
* | |
* @return false|WP_Post|null | |
*/ | |
private function delete_post_with_attachments( $post_id, $force_delete = true ) { | |
$attachments = get_attached_media( '', $post_id ); | |
foreach ( $attachments as $attachment ) { | |
WP_CLI::log( sprintf( 'Image: %s removed from post id: %d', wp_get_attachment_url( $attachment->ID ) , $post_id ) ); | |
wp_delete_attachment( $attachment->ID, $force_delete ); | |
} | |
return wp_delete_post( $post_id, $force_delete ); | |
} | |
/** | |
* Mark a playlist as 410 removed permanently. | |
* | |
* Doesn't delete from db but reports to search engines that it has been permanently removed. | |
* | |
* @param integer $playlist_id Playlist to remove permanently. | |
* | |
* @return false|WP_Post|null | |
*/ | |
private function four_ten_playlist( $playlist_id ) { | |
$class_four_ten = new IOne3_Image_Four_Ten(); | |
return update_post_meta( $playlist_id, $class_four_ten::POST_META, $class_four_ten::UNAVAILABLE ); | |
} | |
/** | |
* Check if the current request is a dry run. | |
* | |
* @param array $args Arguments passed to a command. | |
* @param string $ref Dry-run argument key. | |
* | |
* @return boolean | |
*/ | |
protected function is_dry_run( $args, $ref = 'dry-run' ) { | |
if ( isset( $args[ $ref ] ) && 'false' === $args[ $ref ] ) { | |
return false; | |
} | |
return true; | |
} | |
} | |
WP_CLI::add_command( 'ione-content-cleanup', 'IOneContentClean' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment