From c7ff805ee1967c9676d661ff6962964649d4cbfa Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 3 Jun 2022 16:58:45 +0530 Subject: [PATCH 1/6] enhancement/bbPress The support for the bbPress plugin! --- assets/js/simple-local-avatars.js | 10 ++++----- includes/class-simple-local-avatars.php | 28 ++++++++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/assets/js/simple-local-avatars.js b/assets/js/simple-local-avatars.js index 071910d0..0d5548d3 100644 --- a/assets/js/simple-local-avatars.js +++ b/assets/js/simple-local-avatars.js @@ -129,7 +129,7 @@ jQuery(document).ready(function ($) { if (avatar_working) return; avatar_lock('lock'); - $.get(ajaxurl, { + $.get(i10n_SimpleLocalAvatars.ajaxurl, { action: 'remove_simple_local_avatar', user_id: i10n_SimpleLocalAvatars.user_id, _wpnonce: i10n_SimpleLocalAvatars.deleteNonce, @@ -163,7 +163,7 @@ jQuery(document).ready(function ($) { $( document.getElementById('simple-local-avatars-migrate-from-wp-user-avatar') ).on( 'click', function(event) { event.preventDefault(); - jQuery.post( ajaxurl, { action: 'migrate_from_wp_user_avatar', migrateFromWpUserAvatarNonce: i10n_SimpleLocalAvatars.migrateFromWpUserAvatarNonce } ) + jQuery.post( i10n_SimpleLocalAvatars.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); @@ -224,7 +224,7 @@ jQuery(document).ready(function ($) { function processStep( step, data ) { data.step = step; $.ajax( { - url: ajaxurl, + url: i10n_SimpleLocalAvatars.ajaxurl, dataType: 'json', data: data, method: 'POST', @@ -398,7 +398,7 @@ function simple_local_avatar_set_image_from_url(url, attachmentId, width, height avatar_lock('lock'); jQuery - .post(ajaxurl, { + .post(i10n_SimpleLocalAvatars.ajaxurl, { action: 'assign_simple_local_avatar_media', media_id: attachmentId, user_id: i10n_SimpleLocalAvatars.user_id, @@ -424,7 +424,7 @@ function simple_local_avatar_set_image_from_url(url, attachmentId, width, height function simple_local_avatar_set_image_from_attachment(attachment) { avatar_lock('lock'); jQuery - .post(ajaxurl, { + .post(i10n_SimpleLocalAvatars.ajaxurl, { action: 'assign_simple_local_avatar_media', media_id: attachment.id, user_id: i10n_SimpleLocalAvatars.user_id, diff --git a/includes/class-simple-local-avatars.php b/includes/class-simple-local-avatars.php index 050c84a4..8feb0654 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -107,7 +107,10 @@ public function add_hooks() { add_action( 'admin_init', array( $this, 'admin_init' ) ); - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); + // Load the JS on BE & FE both, in order to support third party plugins like bbPress. + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + add_action( 'show_user_profile', array( $this, 'edit_user_profile' ) ); add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); @@ -675,7 +678,7 @@ public static function save_network_settings() { * * @param string $hook_suffix Page hook */ - public function admin_enqueue_scripts( $hook_suffix ) { + public function enqueue_scripts( $hook_suffix ) { /** * Filter the admin screens where we enqueue our scripts. @@ -684,7 +687,12 @@ public function admin_enqueue_scripts( $hook_suffix ) { * @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 ); + $screens = apply_filters( 'simple_local_avatars_enqueue_scripts', array( 'profile.php', 'user-edit.php', 'options-discussion.php' ), $hook_suffix ); + + // Allow SLA actions on a bbPress profile edit page at FE. + if ( function_exists( 'bbp_is_user_home_edit' ) && bbp_is_user_home_edit() ) { + $hook_suffix = 'profile.php'; + } if ( ! in_array( $hook_suffix, $screens, true ) ) { return; @@ -704,6 +712,7 @@ public function admin_enqueue_scripts( $hook_suffix ) { 'simple-local-avatars', 'i10n_SimpleLocalAvatars', array( + 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'user_id' => $user_id, 'insertIntoPost' => __( 'Set as avatar', 'simple-local-avatars' ), 'selectCrop' => __( 'Select avatar and Crop', 'simple-local-avatars' ), @@ -834,9 +843,10 @@ public function edit_user_profile( $profileuser ) { -

+


@@ -944,6 +954,14 @@ public function edit_user_profile_update( $user_id ) { include_once ABSPATH . 'wp-admin/includes/media.php'; } + // front end (plugin bbPress etc) support + if ( ! function_exists( 'wp_handle_upload' ) ) { + include_once ABSPATH . 'wp-admin/includes/file.php'; + } + if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) { + include_once ABSPATH . 'wp-admin/includes/image.php'; + } + // allow developers to override file size upload limit for avatars add_filter( 'upload_size_limit', array( $this, 'upload_size_limit' ) ); From 92debf2abb0f9c3c2ee8f1f1b1c2324578cc3c10 Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 3 Jun 2022 17:05:34 +0530 Subject: [PATCH 2/6] fix/bbPress reverting the `td` width --- 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 8feb0654..592d8c2e 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -846,7 +846,7 @@ public function edit_user_profile( $profileuser ) { // At FE, show the file input field regardless of the caps. if ( ! is_admin() || ! current_user_can( 'upload_files' ) ) { ?> -

+


From 5cea5cfa62aa9f170672cee49b71413b794f58dd Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 3 Jun 2022 17:09:17 +0530 Subject: [PATCH 3/6] fix/bbPress updating the funciton name in the tests --- tests/phpunit/SimpleLocalAvatarsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/phpunit/SimpleLocalAvatarsTest.php b/tests/phpunit/SimpleLocalAvatarsTest.php index dc24b0ad..728bc3dd 100644 --- a/tests/phpunit/SimpleLocalAvatarsTest.php +++ b/tests/phpunit/SimpleLocalAvatarsTest.php @@ -86,7 +86,7 @@ public function test_add_hooks() { WP_Mock::expectActionAdded( 'admin_init', [ $this->instance, 'admin_init' ] ); - WP_Mock::expectActionAdded( 'admin_enqueue_scripts', [ $this->instance, 'admin_enqueue_scripts' ] ); + WP_Mock::expectActionAdded( 'enqueue_scripts', [ $this->instance, 'enqueue_scripts' ] ); WP_Mock::expectActionAdded( 'show_user_profile', [ $this->instance, 'edit_user_profile' ] ); WP_Mock::expectActionAdded( 'edit_user_profile', [ $this->instance, 'edit_user_profile' ] ); @@ -250,9 +250,9 @@ public function test_load_discussion_page() { $this->instance->load_discussion_page(); } - public function test_admin_enqueue_scripts_wrong_screen() { + public function test_enqueue_scripts_wrong_screen() { WP_Mock::userFunction( 'current_user_can' )->never(); - $this->instance->admin_enqueue_scripts( 'index.php' ); + $this->instance->enqueue_scripts( 'index.php' ); } public function test_sanitize_options() { From d5821af0b91e6c6d8cf9c897103461f15bdbd424 Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 3 Jun 2022 17:12:23 +0530 Subject: [PATCH 4/6] fix/bbPress fixing `is_admin` undefined issue in the test --- tests/phpunit/SimpleLocalAvatarsTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/phpunit/SimpleLocalAvatarsTest.php b/tests/phpunit/SimpleLocalAvatarsTest.php index 728bc3dd..46177bea 100644 --- a/tests/phpunit/SimpleLocalAvatarsTest.php +++ b/tests/phpunit/SimpleLocalAvatarsTest.php @@ -313,6 +313,9 @@ public function test_edit_user_profile() { WP_Mock::userFunction( 'add_query_arg' ); WP_Mock::userFunction( 'disabled' ); + WP_Mock::userFunction( 'is_admin' ) + ->andReturn( true ); + WP_Mock::userFunction( 'get_simple_local_avatar' ) ->with( 1 ) ->andReturn( '' ); From 6a939c917a5973acaf7d3626ad3c99718743637e Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 3 Jun 2022 17:15:24 +0530 Subject: [PATCH 5/6] fix/bbPress hooking proper hooks :) --- tests/phpunit/SimpleLocalAvatarsTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/SimpleLocalAvatarsTest.php b/tests/phpunit/SimpleLocalAvatarsTest.php index 46177bea..1395a75c 100644 --- a/tests/phpunit/SimpleLocalAvatarsTest.php +++ b/tests/phpunit/SimpleLocalAvatarsTest.php @@ -86,7 +86,9 @@ public function test_add_hooks() { WP_Mock::expectActionAdded( 'admin_init', [ $this->instance, 'admin_init' ] ); - WP_Mock::expectActionAdded( 'enqueue_scripts', [ $this->instance, 'enqueue_scripts' ] ); + WP_Mock::expectActionAdded( 'wp_enqueue_scripts', [ $this->instance, 'enqueue_scripts' ] ); + WP_Mock::expectActionAdded( 'admin_enqueue_scripts', [ $this->instance, 'enqueue_scripts' ] ); + WP_Mock::expectActionAdded( 'show_user_profile', [ $this->instance, 'edit_user_profile' ] ); WP_Mock::expectActionAdded( 'edit_user_profile', [ $this->instance, 'edit_user_profile' ] ); From 4ae43015f49ad3ca04b66acd09668208c5fb35a4 Mon Sep 17 00:00:00 2001 From: Faisal Alvi Date: Fri, 10 Jun 2022 15:34:59 +0530 Subject: [PATCH 6/6] fix/bbPress reverting the filter name --- 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 592d8c2e..0d3bd46e 100644 --- a/includes/class-simple-local-avatars.php +++ b/includes/class-simple-local-avatars.php @@ -687,7 +687,7 @@ public function enqueue_scripts( $hook_suffix ) { * @param string $hook_suffix Page hook. * @return array */ - $screens = apply_filters( 'simple_local_avatars_enqueue_scripts', array( 'profile.php', 'user-edit.php', 'options-discussion.php' ), $hook_suffix ); + $screens = apply_filters( 'simple_local_avatars_admin_enqueue_scripts', array( 'profile.php', 'user-edit.php', 'options-discussion.php' ), $hook_suffix ); // Allow SLA actions on a bbPress profile edit page at FE. if ( function_exists( 'bbp_is_user_home_edit' ) && bbp_is_user_home_edit() ) {