Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/bbPress The support for the bbPress plugin! #134

Merged
merged 6 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions assets/js/simple-local-avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
24 changes: 21 additions & 3 deletions includes/class-simple-local-avatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
iamdharmesh marked this conversation as resolved.
Show resolved Hide resolved
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' ) );

Expand Down Expand Up @@ -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.
Expand All @@ -686,6 +689,11 @@ public function admin_enqueue_scripts( $hook_suffix ) {
*/
$screens = apply_filters( 'simple_local_avatars_admin_enqueue_scripts', array( 'profile.php', 'user-edit.php', 'options-discussion.php' ), $hook_suffix );
faisal-alvi marked this conversation as resolved.
Show resolved Hide resolved

// 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;
}
Expand All @@ -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' ),
Expand Down Expand Up @@ -834,7 +843,8 @@ public function edit_user_profile( $profileuser ) {
<?php
// if user is author and above hide the choose file option
// force them to use the WP Media Selector
if ( ! current_user_can( 'upload_files' ) ) {
// At FE, show the file input field regardless of the caps.
if ( ! is_admin() || ! current_user_can( 'upload_files' ) ) {
?>
<p style="display: inline-block; width: 26em;">
<span class="description"><?php esc_html_e( 'Choose an image from your computer:' ); ?></span><br />
Expand Down Expand Up @@ -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' ) );

Expand Down
11 changes: 8 additions & 3 deletions tests/phpunit/SimpleLocalAvatarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ 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( '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' ] );

Expand Down Expand Up @@ -250,9 +252,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() {
Expand Down Expand Up @@ -313,6 +315,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( '<img src="test-image-user-avatar"/>' );
Expand Down