| 1 |
<?php |
| 2 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 3 |
|
| 4 |
class WP_Members_CLI_Filesystem_Upgrade { |
| 5 |
|
| 6 |
function __construct() { |
| 7 |
// Need the admin api for some CLI commands. |
| 8 |
global $wpmem; |
| 9 |
require_once $wpmem->path . 'includes/admin/api.php'; |
| 10 |
require_once $wpmem->path . 'includes/class-wp-members-filesystem.php'; |
| 11 |
$wpmem->filesystem = new WP_Members_Update_Filesystem_Class; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* List files that need to be moved. |
| 16 |
* |
| 17 |
* ## OPTIONS |
| 18 |
* |
| 19 |
* [--page=<page>] |
| 20 |
* : Current page of results to display. Default is 1. |
| 21 |
* |
| 22 |
* [--per_page=<per_page>] |
| 23 |
* : Number of results to display per page. Default is 20. |
| 24 |
* |
| 25 |
* ## EXAMPLES |
| 26 |
* |
| 27 |
* wp mem fs-upgrade list --page=1 --per_page=10 |
| 28 |
*/ |
| 29 |
public function list( $args, $assoc_args ) { |
| 30 |
global $wpmem; |
| 31 |
$files_to_move = $wpmem->filesystem->get_file_list(); |
| 32 |
if ( empty( $files_to_move ) ) { |
| 33 |
WP_CLI::line( WP_CLI::colorize( '%gThere are no files to move.%n' ) ); |
| 34 |
} else { |
| 35 |
|
| 36 |
$current_page = WP_CLI\Utils\get_flag_value( $assoc_args, 'page', 1 ); |
| 37 |
$per_page = WP_CLI\Utils\get_flag_value( $assoc_args, 'per_page', 20 ); |
| 38 |
|
| 39 |
$query_args = array( |
| 40 |
'number' => intval( $per_page ), |
| 41 |
'offset' => intval( $current_page ) |
| 42 |
); |
| 43 |
|
| 44 |
$total_items = count( $files_to_move ); |
| 45 |
$total_pages = ceil( $total_items/$per_page ); |
| 46 |
|
| 47 |
$offset = ($current_page-1) * $per_page; |
| 48 |
|
| 49 |
// paginate with array_slice. |
| 50 |
$paged_files_to_move = array_slice( $files_to_move, $offset, $per_page ); |
| 51 |
|
| 52 |
// Indicate paginated results. |
| 53 |
WP_CLI::line( 'Page ' . $current_page . ' of ' . $total_pages . ' (' . $total_items . ' records, ' . $per_page . ' per page)' ); |
| 54 |
|
| 55 |
foreach( $paged_files_to_move as $file ) { |
| 56 |
$user = get_userdata( $file->post_author ); |
| 57 |
$list[] = array( |
| 58 |
'user id' => $file->post_author, |
| 59 |
'user_email' => $user->user_email, |
| 60 |
'file id' => $file->ID, |
| 61 |
'path (in wp-content/uploads)' => wpmem_get_sub_str( '/wpmembers/user_files', get_attached_file( $file->ID ) ) |
| 62 |
); |
| 63 |
} |
| 64 |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'user id', 'user_email', 'file id', 'path (in wp-content/uploads)' ) ); |
| 65 |
$formatter->display_items( $list ); |
| 66 |
|
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Move files to new directory structure. |
| 72 |
* |
| 73 |
* ## EXAMPLES |
| 74 |
* |
| 75 |
* wp mem fs-upgrade move |
| 76 |
*/ |
| 77 |
public function move( $args, $assoc_args ) { |
| 78 |
|
| 79 |
WP_CLI::line( 'This will move all files in the WP-Members uploads directory to a new directory structure.' ); |
| 80 |
WP_CLI::line( 'Files are not deleted but the attachment data is updated in the database.' ); |
| 81 |
WP_CLI::line( 'Make sure you have backed up your database.' ); |
| 82 |
WP_CLI::confirm( 'Are you sure you want to continue?' ); |
| 83 |
|
| 84 |
global $wpmem; |
| 85 |
$wpmem->filesystem->update_filesystem(); |
| 86 |
if ( ! empty( $wpmem->filesystem->get_errors() ) ) { |
| 87 |
WP_CLI::error( 'There were errors' ); |
| 88 |
foreach ( $wpmem->filesystem->get_errors() as $user_id => $error ) { |
| 89 |
$user = get_userdata( $user_id ); |
| 90 |
$list[] = array( |
| 91 |
'user_id' => $user_id, |
| 92 |
'user_email' => $user->user_email, |
| 93 |
'error' => $error |
| 94 |
); |
| 95 |
} |
| 96 |
WP_CLI::line( 'Attempts to move uploads for the following user IDs returned an error:' ); |
| 97 |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'user_id', 'user_email', 'error' ) ); |
| 98 |
$formatter->display_items( $list ); |
| 99 |
} else { |
| 100 |
WP_CLI::line( WP_CLI::colorize( '%gNo errors during move.%n' ) ); |
| 101 |
WP_CLI::line( 'Run <wp mem fs-upgrade list> to confirm all files were moved.' ); |
| 102 |
WP_CLI::line( 'Run <wp mem fs-upgrade delete> to remove the old files and directories.' ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Delete old uploads directory. |
| 108 |
* |
| 109 |
* ## EXAMPLES |
| 110 |
* |
| 111 |
* wp mem fs-upgrade delete |
| 112 |
*/ |
| 113 |
public function delete( $args, $assoc_args ) { |
| 114 |
global $wpmem; |
| 115 |
WP_CLI::confirm( 'This will delete all files in uploads/wpmembers/user_files/ and cannot be undone. Are you sure?' ); |
| 116 |
$rmdir = $wpmem->filesystem->delete_directory( trailingslashit( $wpmem->filesystem->basedir ) . 'wpmembers/user_files/' ); |
| 117 |
if ( $rmdir ) { |
| 118 |
WP_CLI::success( 'Deletion of old directory successful.' ); |
| 119 |
} else { |
| 120 |
WP_CLI::error( 'Deletion processing returned an error. You may need to check directory permissions and/or delete the folder manually' ); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
WP_CLI::add_command( 'mem fs-upgrade', 'WP_Members_CLI_Filesystem_Upgrade' ); |