From 573bcfaeed4c6c64a817b50239f0dfe103eebaa1 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Sat, 11 Sep 2021 12:10:52 -0500 Subject: [PATCH 01/23] Create wp-cli command to migrate avatars from WP User Avatars plugin --- includes/class-simple-local-avatars.php | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 85100fd2..bf4d37b1 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -42,6 +42,10 @@ public function add_hooks() { add_action( 'user_edit_form_tag', array( $this, 'user_edit_form_tag' ) ); add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) ); + + if ( defined( 'WP_CLI' ) && WP_CLI ) { + WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'migrate_wp_user_avatar' ) ); + } } /** @@ -678,4 +682,68 @@ public function get_avatar_rest( $user ) { public function set_avatar_rest( $input, $user ) { $this->assign_new_user_avatar( $input['media_id'], $user->ID ); } + + /** + * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the command line. + * + * This function creates a new option in the wp_options table to store the processed user IDs + * so that we can run this command multiple times without processing the same user over and over again. + * + * Credit to Philip John for the Gist + * + * @see https://gist.github.com/philipjohn/822d3521a95481f6ad7e118a7106fbc7 + * + * ## EXAMPLES + * + * $ wp simple-local-avatars migrate wp-user-avatar + * Success: Migrated the avatar for user: 1234 + */ + public function migrate_wp_user_avatar() { + + // Get the name of the meta key for WP User Avatar. + global $blog_id, $wpdb; + write_log( 'blog id: ' . $blog_id ); + $meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar'; + write_log( 'meta key: ' . $meta_key ); + + // Get processed users from database. + $migrations = get_option( 'simple_local_avatars_migrations', array() ); + $processed_users = isset( $migrations['wp_user_avatar'] ) ? $migrations['wp_user_avatar'] : array(); + + // Grab all users that have a local avatar. + $users = get_users( + array( + 'exclude' => $processed_users, + 'meta_key' => $meta_key, + 'meta_compare' => 'EXISTS', + ) + ); + + // Bail early if we don't find users. + if ( empty( $users ) ) { + WP_CLI::error( 'Did not find users with an avatar associated with WP User Avatar.' ); + return; + } + + foreach ( $users as $user ) { + // Get the existing avatar media ID. + $avatar_id = get_user_meta( $user->ID, $meta_key, true ); + + // Attach the user and media to Simple Local Avatars. + $sla = new Simple_Local_Avatars(); + $sla->assign_new_user_avatar( (int) $avatar_id, $user->ID ); + + // Check that it worked. + $is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true ); + + // Record the user ID so we don't process a second time. + if ( ! empty( $is_migrated ) ) { + $migrations['wp_user_avatar'][] = $user->ID; + $is_saved = update_option( 'simple_local_avatars_migrations', $migrations ); + if ( ! empty( $is_saved ) ) { + WP_CLI::success( "Migrated the avatar for user: {$user->ID}" ); + } + } + } + } } From 45ecb2f29e79e11e964e00931752cc349eb36d28 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Sat, 11 Sep 2021 12:11:14 -0500 Subject: [PATCH 02/23] Clean up the migrations option if the plugin is deactivated --- simple-local-avatars.php | 1 + 1 file changed, 1 insertion(+) diff --git a/simple-local-avatars.php b/simple-local-avatars.php index 172b5f97..26146a3b 100644 --- a/simple-local-avatars.php +++ b/simple-local-avatars.php @@ -57,4 +57,5 @@ function simple_local_avatars_uninstall() { endforeach; delete_option( 'simple_local_avatars' ); + delete_option( 'simple_local_avatars_migrations' ); } From 4e4d4be798cfe584af78e67f50c45a2448a10ca3 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Sat, 11 Sep 2021 12:47:35 -0500 Subject: [PATCH 03/23] Make compatible with WP Multisite --- includes/class-simple-local-avatars.php | 83 ++++++++++++++----------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index bf4d37b1..a3a0c60d 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -700,48 +700,61 @@ public function set_avatar_rest( $input, $user ) { */ public function migrate_wp_user_avatar() { - // Get the name of the meta key for WP User Avatar. - global $blog_id, $wpdb; - write_log( 'blog id: ' . $blog_id ); - $meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar'; - write_log( 'meta key: ' . $meta_key ); - - // Get processed users from database. - $migrations = get_option( 'simple_local_avatars_migrations', array() ); - $processed_users = isset( $migrations['wp_user_avatar'] ) ? $migrations['wp_user_avatar'] : array(); - - // Grab all users that have a local avatar. - $users = get_users( - array( - 'exclude' => $processed_users, - 'meta_key' => $meta_key, - 'meta_compare' => 'EXISTS', - ) - ); + global $wpdb; + + $sites = get_sites(); - // Bail early if we don't find users. - if ( empty( $users ) ) { - WP_CLI::error( 'Did not find users with an avatar associated with WP User Avatar.' ); + // Bail early if we don't find sites. + if ( empty( $sites ) ) { + WP_CLI::error( esc_html__( 'Did not find sites to run the migration.', 'simple-local-avatars' ) ); return; } - foreach ( $users as $user ) { - // Get the existing avatar media ID. - $avatar_id = get_user_meta( $user->ID, $meta_key, true ); + foreach ( $sites as $site ) { + // Get the blog ID to use in the meta key and user query. + $blog_id = isset( $site->blog_id ) ? $site->blog_id : 1; + + // Get the name of the meta key for WP User Avatar. + $meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar'; + + // Get processed users from database. + $migrations = get_option( 'simple_local_avatars_migrations', array() ); + $processed_users = isset( $migrations['wp_user_avatar'] ) ? $migrations['wp_user_avatar'] : array(); + + // Get all users that have a local avatar. + $users = get_users( + array( + 'blog_id' => $blog_id, + 'exclude' => $processed_users, + 'meta_key' => $meta_key, + 'meta_compare' => 'EXISTS', + ) + ); - // Attach the user and media to Simple Local Avatars. - $sla = new Simple_Local_Avatars(); - $sla->assign_new_user_avatar( (int) $avatar_id, $user->ID ); + // Bail early if we don't find users. + if ( empty( $users ) ) { + WP_CLI::warning( esc_html__( 'Did not find users with an avatar associated with WP User Avatar for site: ', 'simple-local-avatars' ) . esc_html( $blog_id ) ); + continue; + } - // Check that it worked. - $is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true ); + foreach ( $users as $user ) { + // Get the existing avatar media ID. + $avatar_id = get_user_meta( $user->ID, $meta_key, true ); - // Record the user ID so we don't process a second time. - if ( ! empty( $is_migrated ) ) { - $migrations['wp_user_avatar'][] = $user->ID; - $is_saved = update_option( 'simple_local_avatars_migrations', $migrations ); - if ( ! empty( $is_saved ) ) { - WP_CLI::success( "Migrated the avatar for user: {$user->ID}" ); + // Attach the user and media to Simple Local Avatars. + $sla = new Simple_Local_Avatars(); + $sla->assign_new_user_avatar( (int) $avatar_id, $user->ID ); + + // Check that it worked. + $is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true ); + + // Record the user ID so we don't process a second time. + if ( ! empty( $is_migrated ) ) { + $migrations['wp_user_avatar'][] = $user->ID; + $is_saved = update_option( 'simple_local_avatars_migrations', $migrations ); + if ( ! empty( $is_saved ) ) { + WP_CLI::success( esc_html__( 'Migrated the avatar for user: ', 'simple-local-avatars' ) . esc_html( $user->user_login ) ); + } } } } From 26413b820bb78de1fa6442b6633c5bcad7f62a18 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Mon, 18 Oct 2021 21:14:54 -0500 Subject: [PATCH 04/23] Rename cli method name --- includes/class-simple-local-avatars.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index a3a0c60d..6baaf1ed 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -44,7 +44,7 @@ public function add_hooks() { add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) ); if ( defined( 'WP_CLI' ) && WP_CLI ) { - WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'migrate_wp_user_avatar' ) ); + WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'migrate_from_wp_user_avatar' ) ); } } @@ -698,7 +698,7 @@ public function set_avatar_rest( $input, $user ) { * $ wp simple-local-avatars migrate wp-user-avatar * Success: Migrated the avatar for user: 1234 */ - public function migrate_wp_user_avatar() { + public function migrate_from_wp_user_avatar() { global $wpdb; From 8fb01fdc52a6d5f469288335b1093c1ae1d1ca6a Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Mon, 18 Oct 2021 21:15:44 -0500 Subject: [PATCH 05/23] Add button to settings fields --- includes/class-simple-local-avatars.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 6baaf1ed..a73ddd43 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -255,6 +255,13 @@ public function admin_init() { 'desc' => __( 'Only allow users with file upload capabilities to upload local avatars (Authors and above)', 'simple-local-avatars' ), ) ); + add_settings_field( + 'simple-local-avatars-migration', + __( 'Migrations', 'simple-local-avatars' ), + array( $this, 'migrate_from_wp_user_avatar_settings_field' ), + 'discussion', + 'avatars' + ); } /** @@ -328,6 +335,16 @@ public function avatar_settings_field( $args ) { '; } + /** + * Settings field for migrating avatars away from WP User Avatars + */ + public function migrate_from_wp_user_avatar_settings_field() { + printf( + '', + esc_html__( 'Migrate avatars from WP User Avatar to Simple Local Avatars', 'simple-local-avatars' ) + ); + } + /** * Output new Avatar fields to user editing / profile screen * From f3f468e2e48b2949931151a45955de822c62585c Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Mon, 18 Oct 2021 21:17:44 -0500 Subject: [PATCH 06/23] Add migration with wp ajax from dashboard --- includes/class-simple-local-avatars.php | 89 +++++++++++++++++++++++-- simple-local-avatars.dev.js | 10 +++ 2 files changed, 92 insertions(+), 7 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index a73ddd43..b6580b78 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -43,6 +43,8 @@ public function add_hooks() { add_action( 'rest_api_init', array( $this, 'register_rest_fields' ) ); + add_action( 'wp_ajax_migrate_from_wp_user_avatar', array( $this, 'ajax_migrate_from_wp_user_avatar' ) ); + if ( defined( 'WP_CLI' ) && WP_CLI ) { WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'migrate_from_wp_user_avatar' ) ); } @@ -270,7 +272,7 @@ public function admin_init() { * @param string $hook_suffix Page hook */ public function admin_enqueue_scripts( $hook_suffix ) { - if ( 'profile.php' !== $hook_suffix && 'user-edit.php' !== $hook_suffix ) { + if ( 'profile.php' !== $hook_suffix && 'user-edit.php' !== $hook_suffix && 'options-discussion.php' !== $hook_suffix ) { return; } @@ -278,7 +280,8 @@ public function admin_enqueue_scripts( $hook_suffix ) { wp_enqueue_media(); } - $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) $_GET['user_id']; + $param_user_id = isset( $_GET['user_id'] ) ? $_GET['user_id'] : 0; + $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) $param_user_id; $this->remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' ); @@ -288,11 +291,12 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'simple-local-avatars', 'i10n_SimpleLocalAvatars', array( - 'user_id' => $user_id, - 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), - 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), - 'deleteNonce' => $this->remove_nonce, - 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), + 'user_id' => $user_id, + 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), + 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), + 'deleteNonce' => $this->remove_nonce, + 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), + 'wpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), ) ); } @@ -700,6 +704,77 @@ public function set_avatar_rest( $input, $user ) { $this->assign_new_user_avatar( $input['media_id'], $user->ID ); } + /** + * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the dashboard. + * + * This function creates a new option in the wp_options table to store the processed user IDs + * so that we can run this command multiple times without processing the same user over and over again. + * + * Credit to Philip John for the Gist + * + * @see https://gist.github.com/philipjohn/822d3521a95481f6ad7e118a7106fbc7 + */ + public function ajax_migrate_from_wp_user_avatar() { + // Check required information. + if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'migrate_from_wp_user_avatar_nonce' ) ) { + die; + } + + global $wpdb; + + $sites = get_sites(); + + // Bail early if we don't find sites. + if ( empty( $sites ) ) { + return; + } + + foreach ( $sites as $site ) { + // Get the blog ID to use in the meta key and user query. + $blog_id = isset( $site->blog_id ) ? $site->blog_id : 1; + + // Get the name of the meta key for WP User Avatar. + $meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar'; + + // Get processed users from database. + $migrations = get_option( 'simple_local_avatars_migrations', array() ); + $processed_users = isset( $migrations['wp_user_avatar'] ) ? $migrations['wp_user_avatar'] : array(); + + // Get all users that have a local avatar. + $users = get_users( + array( + 'blog_id' => $blog_id, + 'exclude' => $processed_users, + 'meta_key' => $meta_key, + 'meta_compare' => 'EXISTS', + ) + ); + + // Bail early if we don't find users. + if ( empty( $users ) ) { + continue; + } + + foreach ( $users as $user ) { + // Get the existing avatar media ID. + $avatar_id = get_user_meta( $user->ID, $meta_key, true ); + + // Attach the user and media to Simple Local Avatars. + $sla = new Simple_Local_Avatars(); + $sla->assign_new_user_avatar( (int) $avatar_id, $user->ID ); + + // Check that it worked. + $is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true ); + + // Record the user ID so we don't process a second time. + if ( ! empty( $is_migrated ) ) { + $migrations['wp_user_avatar'][] = $user->ID; + update_option( 'simple_local_avatars_migrations', $migrations ); + } + } + } + } + /** * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the command line. * diff --git a/simple-local-avatars.dev.js b/simple-local-avatars.dev.js index ce51f6c0..ddb2df9b 100644 --- a/simple-local-avatars.dev.js +++ b/simple-local-avatars.dev.js @@ -77,6 +77,16 @@ jQuery(document).ready(function($){ avatar_preview.attr( 'src', current_avatar ); } } ); + + $( document.getElementById('simple-local-avatar-migrate-from-wp-user-avatars') ).on( 'click', function(event) { + event.preventDefault(); + $('.spinner').addClass('is-active'); + jQuery.post( ajaxurl, { action: 'migrate_from_wp_user_avatar', _wpnonce: i10n_SimpleLocalAvatars.wpUserAvatarNonce }, function(data) { + if ( data != '' ) { + $('.spinner').removeClass('is-active'); + } + }); + }); }); function avatar_lock( lock_or_unlock ) { From 681f17d0d649926496c408e22ff4b0821ebc8088 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 22 Oct 2021 10:22:35 -0500 Subject: [PATCH 07/23] Switch admin enqueue scripts guard clause to in-array and allow filtering of screens --- includes/class-simple-local-avatars.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index b6580b78..34307fe0 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -272,7 +272,17 @@ public function admin_init() { * @param string $hook_suffix Page hook */ public function admin_enqueue_scripts( $hook_suffix ) { - if ( 'profile.php' !== $hook_suffix && 'user-edit.php' !== $hook_suffix && 'options-discussion.php' !== $hook_suffix ) { + + /** + * Filter the admin screens where we enqueue our scripts. + * + * @param array $screens Array of admin screens. + * @param string $hook_suffix Page hook. + * @return array + */ + $screens = apply_filters( 'simple_local_avatars_admin_enqueue_scripts', array( 'profile.php', 'user-edit.php', 'options-discussion.php' ), $hook_suffix ); + + if ( ! in_array( $hook_suffix, $screens, true ) ) { return; } From a29c240904abb25db26b0ed7fd2364ca26c29bd5 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 22 Oct 2021 10:24:01 -0500 Subject: [PATCH 08/23] Change plural and singlar names --- includes/class-simple-local-avatars.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 34307fe0..9027b08f 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -350,11 +350,11 @@ public function avatar_settings_field( $args ) { } /** - * Settings field for migrating avatars away from WP User Avatars + * Settings field for migrating avatars away from WP User Avatar */ public function migrate_from_wp_user_avatar_settings_field() { printf( - '', + '', esc_html__( 'Migrate avatars from WP User Avatar to Simple Local Avatars', 'simple-local-avatars' ) ); } From 8514dc93feb52f6b50f2c52986b7bb3733378a3d Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 22 Oct 2021 11:41:56 -0500 Subject: [PATCH 09/23] Fix nonce verification by using get_query_var --- includes/class-simple-local-avatars.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 9027b08f..e4fd98bc 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -290,8 +290,7 @@ public function admin_enqueue_scripts( $hook_suffix ) { wp_enqueue_media(); } - $param_user_id = isset( $_GET['user_id'] ) ? $_GET['user_id'] : 0; - $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) $param_user_id; + $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) get_query_var( 'user_id' ); $this->remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' ); From 77f743a60bf7bf2bec49925034c37acad9247627 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 22 Oct 2021 11:42:40 -0500 Subject: [PATCH 10/23] Update class names to be singular or plural --- includes/class-simple-local-avatars.php | 14 +++++++------- simple-local-avatars.dev.js | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index e4fd98bc..a7b6b391 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -300,12 +300,12 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'simple-local-avatars', 'i10n_SimpleLocalAvatars', array( - 'user_id' => $user_id, - 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), - 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), - 'deleteNonce' => $this->remove_nonce, - 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), - 'wpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), + 'user_id' => $user_id, + 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), + 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), + 'deleteNonce' => $this->remove_nonce, + 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), + 'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), ) ); } @@ -353,7 +353,7 @@ public function avatar_settings_field( $args ) { */ public function migrate_from_wp_user_avatar_settings_field() { printf( - '', + '', esc_html__( 'Migrate avatars from WP User Avatar to Simple Local Avatars', 'simple-local-avatars' ) ); } diff --git a/simple-local-avatars.dev.js b/simple-local-avatars.dev.js index ddb2df9b..b66df772 100644 --- a/simple-local-avatars.dev.js +++ b/simple-local-avatars.dev.js @@ -78,12 +78,12 @@ jQuery(document).ready(function($){ } } ); - $( document.getElementById('simple-local-avatar-migrate-from-wp-user-avatars') ).on( 'click', function(event) { + $( document.getElementById('simple-local-avatars-migrate-from-wp-user-avatar') ).on( 'click', function(event) { event.preventDefault(); - $('.spinner').addClass('is-active'); - jQuery.post( ajaxurl, { action: 'migrate_from_wp_user_avatar', _wpnonce: i10n_SimpleLocalAvatars.wpUserAvatarNonce }, function(data) { + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').addClass('is-active'); + jQuery.post( ajaxurl, { action: 'migrate_from_wp_user_avatar', migrateFromWpUserAvatarNonce: i10n_SimpleLocalAvatars.migrateFromWpUserAvatarNonce }, function(data) { if ( data != '' ) { - $('.spinner').removeClass('is-active'); + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').removeClass('is-active'); } }); }); From 5a306c584952938051f7f54fb29716ed6e994ab8 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 22 Oct 2021 11:43:00 -0500 Subject: [PATCH 11/23] Refactor to DRY out code --- includes/class-simple-local-avatars.php | 131 +++++++++++------------- 1 file changed, 58 insertions(+), 73 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index a7b6b391..23869c3b 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -46,7 +46,7 @@ public function add_hooks() { add_action( 'wp_ajax_migrate_from_wp_user_avatar', array( $this, 'ajax_migrate_from_wp_user_avatar' ) ); if ( defined( 'WP_CLI' ) && WP_CLI ) { - WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'migrate_from_wp_user_avatar' ) ); + WP_CLI::add_command( 'simple-local-avatars migrate wp-user-avatar', array( $this, 'wp_cli_migrate_from_wp_user_avatar' ) ); } } @@ -714,7 +714,7 @@ public function set_avatar_rest( $input, $user ) { } /** - * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the dashboard. + * Migrate the user's avatar data from WP User Avatar/ProfilePress * * This function creates a new option in the wp_options table to store the processed user IDs * so that we can run this command multiple times without processing the same user over and over again. @@ -722,20 +722,19 @@ public function set_avatar_rest( $input, $user ) { * Credit to Philip John for the Gist * * @see https://gist.github.com/philipjohn/822d3521a95481f6ad7e118a7106fbc7 + * + * @return int */ - public function ajax_migrate_from_wp_user_avatar() { - // Check required information. - if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'migrate_from_wp_user_avatar_nonce' ) ) { - die; - } + public function migrate_from_wp_user_avatar() { global $wpdb; + $count = 0; $sites = get_sites(); // Bail early if we don't find sites. if ( empty( $sites ) ) { - return; + return $count; } foreach ( $sites as $site ) { @@ -775,89 +774,75 @@ public function ajax_migrate_from_wp_user_avatar() { // Check that it worked. $is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true ); - // Record the user ID so we don't process a second time. if ( ! empty( $is_migrated ) ) { + // Build array of user IDs. $migrations['wp_user_avatar'][] = $user->ID; - update_option( 'simple_local_avatars_migrations', $migrations ); + + // Record the user IDs so we don't process a second time. + $is_saved = update_option( 'simple_local_avatars_migrations', $migrations ); + + // Record how many avatars we migrate to be used in our messaging. + if ( $is_saved ) { + $count++; + } } } } + + return $count; + } /** - * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the command line. - * - * This function creates a new option in the wp_options table to store the processed user IDs - * so that we can run this command multiple times without processing the same user over and over again. - * - * Credit to Philip John for the Gist - * - * @see https://gist.github.com/philipjohn/822d3521a95481f6ad7e118a7106fbc7 - * - * ## EXAMPLES - * - * $ wp simple-local-avatars migrate wp-user-avatar - * Success: Migrated the avatar for user: 1234 + * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the dashboard. */ - public function migrate_from_wp_user_avatar() { - - global $wpdb; + public function ajax_migrate_from_wp_user_avatar() { + // Check required information. + if ( empty( $_POST['migrateFromWpUserAvatarNonce'] ) || ! wp_verify_nonce( $_POST['migrateFromWpUserAvatarNonce'], 'migrate_from_wp_user_avatar_nonce' ) ) { + die; + } - $sites = get_sites(); + $count = $this->migrate_from_wp_user_avatar(); - // Bail early if we don't find sites. - if ( empty( $sites ) ) { - WP_CLI::error( esc_html__( 'Did not find sites to run the migration.', 'simple-local-avatars' ) ); - return; + if ( is_integer( $count ) && $count > 0 ) { + // WP_CLI::success( + // sprintf( + // '%s %s %s', + // esc_html__( 'Successfully migrated', 'simple-local-avatars' ), + // esc_html( $count ), + // esc_html__( 'avatars.', 'simple-local-avatars' ) + // ) + // ); + } else { + // WP_CLI::warning( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) ); } + } - foreach ( $sites as $site ) { - // Get the blog ID to use in the meta key and user query. - $blog_id = isset( $site->blog_id ) ? $site->blog_id : 1; + /** + * Migrate the user's avatar data from WP User Avatar/ProfilePress via the command line. + * + * ## EXAMPLES + * + * $ wp simple-local-avatars migrate wp-user-avatar + * Success: Successfully migrated 5 avatars. + */ + public function wp_cli_migrate_from_wp_user_avatar() { - // Get the name of the meta key for WP User Avatar. - $meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar'; + WP_CLI::confirm( esc_html__( 'Do you want to migrate avatars from WP User Avatar?', 'simple-local-avatars' ) ); - // Get processed users from database. - $migrations = get_option( 'simple_local_avatars_migrations', array() ); - $processed_users = isset( $migrations['wp_user_avatar'] ) ? $migrations['wp_user_avatar'] : array(); + $count = $this->migrate_from_wp_user_avatar(); - // Get all users that have a local avatar. - $users = get_users( - array( - 'blog_id' => $blog_id, - 'exclude' => $processed_users, - 'meta_key' => $meta_key, - 'meta_compare' => 'EXISTS', + if ( is_integer( $count ) && $count > 0 ) { + WP_CLI::success( + sprintf( + '%s %s %s', + esc_html__( 'Successfully migrated', 'simple-local-avatars' ), + esc_html( $count ), + esc_html__( 'avatars.', 'simple-local-avatars' ) ) ); - - // Bail early if we don't find users. - if ( empty( $users ) ) { - WP_CLI::warning( esc_html__( 'Did not find users with an avatar associated with WP User Avatar for site: ', 'simple-local-avatars' ) . esc_html( $blog_id ) ); - continue; - } - - foreach ( $users as $user ) { - // Get the existing avatar media ID. - $avatar_id = get_user_meta( $user->ID, $meta_key, true ); - - // Attach the user and media to Simple Local Avatars. - $sla = new Simple_Local_Avatars(); - $sla->assign_new_user_avatar( (int) $avatar_id, $user->ID ); - - // Check that it worked. - $is_migrated = get_user_meta( $user->ID, 'simple_local_avatar', true ); - - // Record the user ID so we don't process a second time. - if ( ! empty( $is_migrated ) ) { - $migrations['wp_user_avatar'][] = $user->ID; - $is_saved = update_option( 'simple_local_avatars_migrations', $migrations ); - if ( ! empty( $is_saved ) ) { - WP_CLI::success( esc_html__( 'Migrated the avatar for user: ', 'simple-local-avatars' ) . esc_html( $user->user_login ) ); - } - } - } + } else { + WP_CLI::warning( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) ); } } } From 44ffa4d8e4a55b06515b7b50b1a260f1729f7778 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 5 Nov 2021 07:43:17 -0500 Subject: [PATCH 12/23] Add acceptance test for new settings fields --- tests/wpa/BasicTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/wpa/BasicTest.php b/tests/wpa/BasicTest.php index 3a897efa..476aac47 100644 --- a/tests/wpa/BasicTest.php +++ b/tests/wpa/BasicTest.php @@ -59,6 +59,10 @@ public function testAdminSettingShows() { $I->seeText( 'Local Avatars Only' ); + $I->seeText( 'Local Upload Permissions' ); + + $I->seeText( 'Migrations' ); + $I->moveTo( 'wp-admin/profile.php' ); $I->seeText( 'Upload Avatar' ); From 8949b2a73e1340901af9ff8a7b428449452dc393 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 5 Nov 2021 08:05:02 -0500 Subject: [PATCH 13/23] Simplify CLI command. Add --yes flag to skip confirmation for automated systems. --- includes/class-simple-local-avatars.php | 39 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 23869c3b..d7f6d7b8 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -821,28 +821,43 @@ public function ajax_migrate_from_wp_user_avatar() { /** * Migrate the user's avatar data from WP User Avatar/ProfilePress via the command line. * + * ## OPTIONS + * + * [--yes] + * : Skips the confirmations (for automated systems). + * * ## EXAMPLES * * $ wp simple-local-avatars migrate wp-user-avatar * Success: Successfully migrated 5 avatars. + * + * @param array|null $args The arguments. + * @param array|null $assoc_args The associative arguments. + * + * @return void */ - public function wp_cli_migrate_from_wp_user_avatar() { + public function wp_cli_migrate_from_wp_user_avatar( $args = null, $assoc_args = null ) { - WP_CLI::confirm( esc_html__( 'Do you want to migrate avatars from WP User Avatar?', 'simple-local-avatars' ) ); + // Argument --yes to prevent confirmation (for automated systems). + if ( ! isset( $assoc_args['yes'] ) ) { + WP_CLI::confirm( esc_html__( 'Do you want to migrate avatars from WP User Avatar?', 'simple-local-avatars' ) ); + } + // Run the migration script and store the number of avatars processed. $count = $this->migrate_from_wp_user_avatar(); - if ( is_integer( $count ) && $count > 0 ) { - WP_CLI::success( - sprintf( - '%s %s %s', - esc_html__( 'Successfully migrated', 'simple-local-avatars' ), - esc_html( $count ), - esc_html__( 'avatars.', 'simple-local-avatars' ) - ) - ); - } else { + // Error out if we don't process any avatars. + if ( 0 === absint( $count ) ) { WP_CLI::warning( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) ); } + + WP_CLI::success( + sprintf( + '%s %s %s', + esc_html__( 'Successfully migrated', 'simple-local-avatars' ), + esc_html( $count ), + esc_html__( 'avatars.', 'simple-local-avatars' ) + ) + ); } } From f6fe4ea7a3c0f9814056efd9aabd35d0771b82b0 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Sat, 6 Nov 2021 15:42:58 -0500 Subject: [PATCH 14/23] Error out and stop execution on WP CLI migration script --- includes/class-simple-local-avatars.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index d7f6d7b8..1bece30e 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -848,7 +848,7 @@ public function wp_cli_migrate_from_wp_user_avatar( $args = null, $assoc_args = // Error out if we don't process any avatars. if ( 0 === absint( $count ) ) { - WP_CLI::warning( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) ); + WP_CLI::error( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) ); } WP_CLI::success( @@ -856,7 +856,7 @@ public function wp_cli_migrate_from_wp_user_avatar( $args = null, $assoc_args = '%s %s %s', esc_html__( 'Successfully migrated', 'simple-local-avatars' ), esc_html( $count ), - esc_html__( 'avatars.', 'simple-local-avatars' ) + esc_html__( 'avatars from WP User Avatar.', 'simple-local-avatars' ) ) ); } From c3ca8225ff29adbb22a2617863467117888287d6 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Sat, 6 Nov 2021 15:43:39 -0500 Subject: [PATCH 15/23] Refactor admin button to use paragraphs --- includes/class-simple-local-avatars.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 1bece30e..3e1db1dc 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -353,7 +353,8 @@ public function avatar_settings_field( $args ) { */ public function migrate_from_wp_user_avatar_settings_field() { printf( - '', + '

', + esc_attr( 'simple-local-avatars-migrate-from-wp-user-avatar' ), esc_html__( 'Migrate avatars from WP User Avatar to Simple Local Avatars', 'simple-local-avatars' ) ); } From 6df622aa8f5e9e694a87709fe2398542e6789391 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Sat, 6 Nov 2021 15:44:52 -0500 Subject: [PATCH 16/23] Add dynamic messaging to AJAX version of migration script --- includes/class-simple-local-avatars.php | 51 +++++++++++++++---------- simple-local-avatars.dev.js | 29 +++++++++++--- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 3e1db1dc..72a254c0 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -300,12 +300,16 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'simple-local-avatars', 'i10n_SimpleLocalAvatars', array( - 'user_id' => $user_id, - 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), - 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), - 'deleteNonce' => $this->remove_nonce, - 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), - 'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), + 'user_id' => $user_id, + 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), + 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), + 'deleteNonce' => $this->remove_nonce, + 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), + 'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), + 'migrateFromWpUserAvatarSuccessStart' => __( 'Successfully migrated', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarSuccessEnd' => __( 'avatars from WP User Avatar.', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarProgress' => __( 'Migration in progress...', 'simple-local-avatars' ), ) ); } @@ -796,27 +800,34 @@ public function migrate_from_wp_user_avatar() { /** * Migrate the user's avatar data away from WP User Avatar/ProfilePress via the dashboard. + * + * Sends the number of avatars processed back to the AJAX response before stopping execution. + * + * @return void */ public function ajax_migrate_from_wp_user_avatar() { - // Check required information. - if ( empty( $_POST['migrateFromWpUserAvatarNonce'] ) || ! wp_verify_nonce( $_POST['migrateFromWpUserAvatarNonce'], 'migrate_from_wp_user_avatar_nonce' ) ) { + // Bail early if nonce is not available. + if ( empty( $_POST['migrateFromWpUserAvatarNonce'] ) ) { die; } + // Bail early if nonce is invalid. + if ( ! wp_verify_nonce( $_POST['migrateFromWpUserAvatarNonce'], 'migrate_from_wp_user_avatar_nonce' ) ) { + die(); + } + + // Run the migration script and store the number of avatars processed. $count = $this->migrate_from_wp_user_avatar(); - if ( is_integer( $count ) && $count > 0 ) { - // WP_CLI::success( - // sprintf( - // '%s %s %s', - // esc_html__( 'Successfully migrated', 'simple-local-avatars' ), - // esc_html( $count ), - // esc_html__( 'avatars.', 'simple-local-avatars' ) - // ) - // ); - } else { - // WP_CLI::warning( esc_html__( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ) ); - } + // Create the array we send back to javascript here. + $array_we_send_back = array( 'count' => $count ); + + // Make sure to json encode the output because that's what it is expecting. + echo wp_json_encode( $array_we_send_back ); + + // Make sure you die when finished doing ajax output. + wp_die(); + } /** diff --git a/simple-local-avatars.dev.js b/simple-local-avatars.dev.js index b66df772..06d0808c 100644 --- a/simple-local-avatars.dev.js +++ b/simple-local-avatars.dev.js @@ -80,12 +80,29 @@ jQuery(document).ready(function($){ $( document.getElementById('simple-local-avatars-migrate-from-wp-user-avatar') ).on( 'click', function(event) { event.preventDefault(); - $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').addClass('is-active'); - jQuery.post( ajaxurl, { action: 'migrate_from_wp_user_avatar', migrateFromWpUserAvatarNonce: i10n_SimpleLocalAvatars.migrateFromWpUserAvatarNonce }, function(data) { - if ( data != '' ) { - $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').removeClass('is-active'); - } - }); + jQuery.post( ajaxurl, { action: 'migrate_from_wp_user_avatar', migrateFromWpUserAvatarNonce: i10n_SimpleLocalAvatars.migrateFromWpUserAvatarNonce } ) + .always( function() { + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').empty(); + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').text(i10n_SimpleLocalAvatars.migrateFromWpUserAvatarProgress); + }) + .done( function( response ) { + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').empty(); + const data = $.parseJSON(response); + const count = data.count; + if ( 0 === count ) { + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').text( + i10n_SimpleLocalAvatars.migrateFromWpUserAvatarFailure + ); + } + if ( count > 0 ) { + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').text( + i10n_SimpleLocalAvatars.migrateFromWpUserAvatarSuccessStart + ' ' + count + ' ' + i10n_SimpleLocalAvatars.migrateFromWpUserAvatarSuccessEnd + ); + } + setTimeout(function() { + $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').empty(); + }, 5000); + }); }); }); From 7a765efc876079c3c1e5e7fb7639a6fbf3a5f756 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Wed, 1 Dec 2021 08:56:43 -0600 Subject: [PATCH 17/23] Change admin table heading to be more descriptive --- includes/class-simple-local-avatars.php | 2 +- tests/wpa/BasicTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 72a254c0..e6f311e3 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -259,7 +259,7 @@ public function admin_init() { ); add_settings_field( 'simple-local-avatars-migration', - __( 'Migrations', 'simple-local-avatars' ), + __( 'Migrate Other Local Avatars', 'simple-local-avatars' ), array( $this, 'migrate_from_wp_user_avatar_settings_field' ), 'discussion', 'avatars' diff --git a/tests/wpa/BasicTest.php b/tests/wpa/BasicTest.php index 476aac47..ce9bd574 100644 --- a/tests/wpa/BasicTest.php +++ b/tests/wpa/BasicTest.php @@ -61,7 +61,7 @@ public function testAdminSettingShows() { $I->seeText( 'Local Upload Permissions' ); - $I->seeText( 'Migrations' ); + $I->seeText( 'Migrate Other Local Avatars' ); $I->moveTo( 'wp-admin/profile.php' ); From 0c2133209f2c09edc6a8539b1e1cd117f268cac6 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Wed, 1 Dec 2021 09:18:37 -0600 Subject: [PATCH 18/23] Change wording on success message to be single string --- includes/class-simple-local-avatars.php | 14 ++++++-------- simple-local-avatars.dev.js | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index e6f311e3..3345359c 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -306,10 +306,9 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'deleteNonce' => $this->remove_nonce, 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), 'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), - 'migrateFromWpUserAvatarSuccessStart' => __( 'Successfully migrated', 'simple-local-avatars' ), - 'migrateFromWpUserAvatarSuccessEnd' => __( 'avatars from WP User Avatar.', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarSuccess' => __( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ), 'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ), - 'migrateFromWpUserAvatarProgress' => __( 'Migration in progress...', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarProgress' => __( 'Migration in progress.', 'simple-local-avatars' ), ) ); } @@ -841,7 +840,7 @@ public function ajax_migrate_from_wp_user_avatar() { * ## EXAMPLES * * $ wp simple-local-avatars migrate wp-user-avatar - * Success: Successfully migrated 5 avatars. + * Success: Number of avatars successfully migrated from WP User Avatar: 5 * * @param array|null $args The arguments. * @param array|null $assoc_args The associative arguments. @@ -865,10 +864,9 @@ public function wp_cli_migrate_from_wp_user_avatar( $args = null, $assoc_args = WP_CLI::success( sprintf( - '%s %s %s', - esc_html__( 'Successfully migrated', 'simple-local-avatars' ), - esc_html( $count ), - esc_html__( 'avatars from WP User Avatar.', 'simple-local-avatars' ) + '%s: %s', + esc_html__( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ), + esc_html( $count ) ) ); } diff --git a/simple-local-avatars.dev.js b/simple-local-avatars.dev.js index 06d0808c..ef0c4bea 100644 --- a/simple-local-avatars.dev.js +++ b/simple-local-avatars.dev.js @@ -96,7 +96,7 @@ jQuery(document).ready(function($){ } if ( count > 0 ) { $('.simple-local-avatars-migrate-from-wp-user-avatar-progress').text( - i10n_SimpleLocalAvatars.migrateFromWpUserAvatarSuccessStart + ' ' + count + ' ' + i10n_SimpleLocalAvatars.migrateFromWpUserAvatarSuccessEnd + i10n_SimpleLocalAvatars.migrateFromWpUserAvatarSuccess + ': ' + count ); } setTimeout(function() { From 0213020755636e740454b6f205ff48886ae9945c Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 10 Dec 2021 14:21:43 -0600 Subject: [PATCH 19/23] Remove extra spaces in array --- includes/class-simple-local-avatars.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 3345359c..4116d3df 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -300,15 +300,15 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'simple-local-avatars', 'i10n_SimpleLocalAvatars', array( - 'user_id' => $user_id, - 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), - 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), - 'deleteNonce' => $this->remove_nonce, - 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), - 'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), - 'migrateFromWpUserAvatarSuccess' => __( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ), - 'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ), - 'migrateFromWpUserAvatarProgress' => __( 'Migration in progress.', 'simple-local-avatars' ), + 'user_id' => $user_id, + 'insertMediaTitle' => __( 'Choose an Avatar', 'simple-local-avatars' ), + 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), + 'deleteNonce' => $this->remove_nonce, + 'mediaNonce' => wp_create_nonce( 'assign_simple_local_avatar_nonce' ), + 'migrateFromWpUserAvatarNonce' => wp_create_nonce( 'migrate_from_wp_user_avatar_nonce' ), + 'migrateFromWpUserAvatarSuccess' => __( 'Number of avatars successfully migrated from WP User Avatar', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarFailure' => __( 'No avatars were migrated from WP User Avatar.', 'simple-local-avatars' ), + 'migrateFromWpUserAvatarProgress' => __( 'Migration in progress.', 'simple-local-avatars' ), ) ); } From b6cc21d03da1d4b5f6c89c9b890a9f3a90f827e1 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 10 Dec 2021 14:26:04 -0600 Subject: [PATCH 20/23] Do not set default parameter values --- includes/class-simple-local-avatars.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 4116d3df..38df13ea 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -842,12 +842,12 @@ public function ajax_migrate_from_wp_user_avatar() { * $ wp simple-local-avatars migrate wp-user-avatar * Success: Number of avatars successfully migrated from WP User Avatar: 5 * - * @param array|null $args The arguments. - * @param array|null $assoc_args The associative arguments. + * @param array $args The arguments. + * @param array $assoc_args The associative arguments. * * @return void */ - public function wp_cli_migrate_from_wp_user_avatar( $args = null, $assoc_args = null ) { + public function wp_cli_migrate_from_wp_user_avatar( $args, $assoc_args ) { // Argument --yes to prevent confirmation (for automated systems). if ( ! isset( $assoc_args['yes'] ) ) { From d3768cd65f9a8b80876f5e8cc923b0af25156948 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 10 Dec 2021 14:30:05 -0600 Subject: [PATCH 21/23] Sanitize data --- includes/class-simple-local-avatars.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 38df13ea..2df91b25 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -806,12 +806,12 @@ public function migrate_from_wp_user_avatar() { */ public function ajax_migrate_from_wp_user_avatar() { // Bail early if nonce is not available. - if ( empty( $_POST['migrateFromWpUserAvatarNonce'] ) ) { + if ( empty( sanitize_text_field( $_POST['migrateFromWpUserAvatarNonce'] ) ) ) { die; } // Bail early if nonce is invalid. - if ( ! wp_verify_nonce( $_POST['migrateFromWpUserAvatarNonce'], 'migrate_from_wp_user_avatar_nonce' ) ) { + if ( ! wp_verify_nonce( sanitize_text_field( $_POST['migrateFromWpUserAvatarNonce'] ), 'migrate_from_wp_user_avatar_nonce' ) ) { die(); } From a2dc5462dcf016951aed028d7d4a4c9e8c759fc4 Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Fri, 17 Dec 2021 09:55:34 -0600 Subject: [PATCH 22/23] Add support for single site and multisite installs --- includes/class-simple-local-avatars.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 2df91b25..255247ae 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -734,7 +734,17 @@ public function migrate_from_wp_user_avatar() { global $wpdb; $count = 0; - $sites = get_sites(); + + // Support single site and multisite installs. + // Use WordPress function if running multisite. + // Create generic class if running single site. + if ( is_multisite() ) { + $sites = get_sites(); + } else { + $site = new stdClass(); + $site->blog_id = 1; + $sites = array( $site ); + } // Bail early if we don't find sites. if ( empty( $sites ) ) { From 3235c371b6190be151e201ea577ed22c913c521c Mon Sep 17 00:00:00 2001 From: Clayton Collie Date: Thu, 23 Dec 2021 11:58:43 -0600 Subject: [PATCH 23/23] revert change to how we get user ID on user-edit screen --- includes/class-simple-local-avatars.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 255247ae..244cb82e 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -290,7 +290,7 @@ public function admin_enqueue_scripts( $hook_suffix ) { wp_enqueue_media(); } - $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) get_query_var( 'user_id' ); + $user_id = ( 'profile.php' === $hook_suffix ) ? get_current_user_id() : (int) $_GET['user_id']; $this->remove_nonce = wp_create_nonce( 'remove_simple_local_avatar_nonce' );