Skip to content

Commit

Permalink
Merge branch 'develop' into trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
faisal-alvi committed Jun 24, 2022
2 parents e254656 + 9d66f67 commit d0d8472
Show file tree
Hide file tree
Showing 22 changed files with 2,072 additions and 79 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Cypress

on:
pull_request:
branches:
- develop
jobs:
cypress:
name: ${{ matrix.core.name }}
runs-on: ubuntu-latest
strategy:
matrix:
core:
- {name: 'WP latest', version: 'latest'}
- {name: 'WP minimum', version: 'WordPress/WordPress#4.9'}
- {name: 'WP trunk', version: 'WordPress/WordPress#master'}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Composer (optional)
run: composer install
continue-on-error: true
- name: Build (optional)
run: npm run build
continue-on-error: true
- name: Set the core version
run: ./tests/bin/set-core-version.js ${{ matrix.core.version }}
- name: Set up WP environment
run: npm run env:start
- name: Test
run: npm run cypress:run
10 changes: 10 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": ["."],
"env": {
"tests": {
"mappings": {
"wp-cli.yml": "./tests/bin/wp-cli.yml"
}
}
}
}
42 changes: 10 additions & 32 deletions assets/js/simple-local-avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,13 @@ jQuery(document).ready(function ($) {
const cropControl = {
id: 'control-id',
params: {
flex_width: false, // set to true if the width of the cropped image can be different to the width defined here
flex_height: false, // set to true if the height of the cropped image can be different to the height defined here
flex_width: true, // set to true if the width of the cropped image can be different to the width defined here
flex_height: true, // set to true if the height of the cropped image can be different to the height defined here
width: 200, // set the desired width of the destination image here
height: 200, // set the desired height of the destination image here
},
};

/**
* Return whether the image must be cropped, based on required dimensions.
*
* @param {boolean} flexW
* @param {boolean} flexH
* @param {number} distW
* @param {number} distH
* @param {number} imgW
* @param {number} imgH
* @returns {boolean}
*/
cropControl.mustBeCropped = function (flexW, flexH, distW, distH, imgW, imgH) {
// Skip cropping if the image matches the crop dimension.
if (imgW === distW && imgH === distH) {
return false;
}

return true;
};

/**
* Create a media modal select frame, we need to set this up every time instead of reusing if already there
* as the toolbar button does not get reset when doing the following:
Expand Down Expand Up @@ -149,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 @@ -183,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 @@ -244,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 @@ -354,10 +334,8 @@ function simple_local_avatar_calculate_image_select_options(attachment, controll

const ratio = xInit / yInit;

controller.set(
'canSkipCrop',
!control.mustBeCropped(false, false, xInit, yInit, realWidth, realHeight),
);
// Enable skip cropping button.
controller.set('canSkipCrop', true);

const xImg = xInit;
const yImg = yInit;
Expand Down Expand Up @@ -421,7 +399,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 @@ -440,14 +418,14 @@ function simple_local_avatar_set_image_from_url(url, attachmentId, width, height
}

/**
* Set the avatar image, once it is selected from the media library.
* Set the avatar image, once it is selected from the media library.
*
* @param {object} attachment
*/
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
112 changes: 100 additions & 12 deletions includes/class-simple-local-avatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function __construct() {
&& (
( // And either an ajax request not in the network admin.
defined( 'DOING_AJAX' ) && DOING_AJAX
&& ! preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] )
&& isset( $_SERVER['HTTP_REFERER'] ) && ! preg_match( '#^' . network_admin_url() . '#i', $_SERVER['HTTP_REFERER'] )
)
||
( // Or normal request not in the network admin.
Expand All @@ -99,6 +99,7 @@ public function __construct() {
* Register actions and filters.
*/
public function add_hooks() {
global $pagenow;

add_filter( 'plugin_action_links_' . SLA_PLUGIN_BASENAME, array( $this, 'plugin_filter_action_links' ) );

Expand All @@ -107,7 +108,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' ) );

Expand All @@ -130,6 +134,14 @@ public function add_hooks() {

add_filter( 'avatar_defaults', array( $this, 'add_avatar_default_field' ) );
add_action( 'wpmu_new_blog', array( $this, 'set_defaults' ) );

if ( 'profile.php' === $pagenow ) {
add_filter( 'media_view_strings', function ( $strings ) {
$strings['skipCropping'] = esc_html__( 'Default Crop', 'simple-local-avatars' );

return $strings;
}, 10, 1 );
}
}

/**
Expand Down Expand Up @@ -262,21 +274,28 @@ public function get_avatar_data( $args, $id_or_email ) {

if ( ! empty( $args['url'] ) ) {
$args['found_avatar'] = true;

// If custom alt text isn't passed, pull alt text from the local image.
if ( empty( $args['alt'] ) ) {
$args['alt'] = $this->get_simple_local_avatar_alt( $id_or_email );
}
}

return $args;
}

/**
* Get local avatar url.
* Get a user ID from certain possible values.
*
* @since 2.2.0
* @since 2.5.0
*
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
* @param int $size Requested avatar size.
* @return int|false
*/
public function get_simple_local_avatar_url( $id_or_email, $size ) {
public function get_user_id( $id_or_email ) {
$user_id = false;

if ( is_numeric( $id_or_email ) ) {
$user_id = (int) $id_or_email;
} elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
Expand All @@ -288,6 +307,21 @@ public function get_simple_local_avatar_url( $id_or_email, $size ) {
$user_id = $user ? $user->ID : '';
}

return $user_id;
}

/**
* Get local avatar url.
*
* @since 2.2.0
*
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
* @param int $size Requested avatar size.
*/
public function get_simple_local_avatar_url( $id_or_email, $size ) {
$user_id = $this->get_user_id( $id_or_email );

if ( empty( $user_id ) ) {
return '';
}
Expand Down Expand Up @@ -354,7 +388,15 @@ public function get_simple_local_avatar_url( $id_or_email, $size ) {
$dest_file = $editor->generate_filename();
$saved = $editor->save( $dest_file );
if ( ! is_wp_error( $saved ) ) {
$local_avatars[ $size ] = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
// Transform the destination file path into URL.
$dest_file_url = '';
if ( false !== strpos( $dest_file, $upload_path['basedir'] ) ) {
$dest_file_url = str_replace( $upload_path['basedir'], $upload_path['baseurl'], $dest_file );
} else if ( is_multisite() && false !== strpos( $dest_file, ABSPATH . 'wp-content/uploads' ) ) {
$dest_file_url = str_replace( ABSPATH . 'wp-content/uploads', network_site_url( '/wp-content/uploads' ), $dest_file );
}

$local_avatars[ $size ] = $dest_file_url;
}
}
}
Expand All @@ -372,6 +414,31 @@ public function get_simple_local_avatar_url( $id_or_email, $size ) {
return esc_url( $local_avatars[ $size ] );
}

/**
* Get local avatar alt text.
*
* @since 2.5.0
*
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
* @return string
*/
public function get_simple_local_avatar_alt( $id_or_email ) {
$user_id = $this->get_user_id( $id_or_email );

if ( empty( $user_id ) ) {
return '';
}

// Fetch local avatar from meta and make sure we have a media ID.
$local_avatars = get_user_meta( $user_id, 'simple_local_avatar', true );
if ( empty( $local_avatars['media_id'] ) ) {
return '';
}

return esc_attr( get_post_meta( $local_avatars['media_id'], '_wp_attachment_image_alt', true ) );
}

/**
* Get default avatar url
*
Expand Down Expand Up @@ -620,7 +687,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 @@ -631,6 +698,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 );

// 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 @@ -649,6 +721,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 @@ -779,7 +852,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 @@ -889,6 +963,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 Expand Up @@ -1248,7 +1330,13 @@ public function sla_clear_user_cache() {
*/
private function clear_user_avatar_cache( $local_avatars, $user_id, $media_id ) {
if ( ! empty( $media_id ) ) {
$file_name_data = pathinfo( wp_get_original_image_path( $media_id ) );
// In order to support WP 4.9.
if ( function_exists( 'wp_get_original_image_path' ) ) {
$file_name_data = pathinfo( wp_get_original_image_path( $media_id ) );
} else {
$file_name_data = pathinfo( get_attached_file( $media_id ) );
}

$file_dir_name = $file_name_data['dirname'];
$file_name = $file_name_data['filename'];
$file_ext = $file_name_data['extension'];
Expand Down Expand Up @@ -1306,10 +1394,10 @@ private function save_default_avatar_file_id() {
update_option( 'simple_local_avatar_default', $file_id );
}
}

/**
* 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.
*
Expand Down
Loading

0 comments on commit d0d8472

Please sign in to comment.