Skip to content

Commit

Permalink
Merge branch 'trunk' into fix/content-distribution-block-editor-compo…
Browse files Browse the repository at this point in the history
…nents
  • Loading branch information
miguelpeixe authored Jan 21, 2025
2 parents 720730c + f1b52ec commit 968e655
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 75 deletions.
2 changes: 1 addition & 1 deletion includes/class-user-manual-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function manual_sync_user( $user_data ) {

return [
'email' => $user_data->user_email,
'role' => array_shift( $user_data->roles ),
'role' => $user_data->roles,
'user_id' => $user_data->ID,
'meta' => $synced_metadata,
'prop' => $synced_props,
Expand Down
28 changes: 23 additions & 5 deletions includes/content-distribution/class-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,34 @@ public function __construct( $payload ) {
/**
* Log a message.
*
* If "newspack_log" is available, we'll use it. Otherwise, we'll fallback to
* the Network's debugger.
*
* @param string $message The message to log.
* @param string $type The log type. Either 'error' or 'debug'.
* Default is 'error'.
*
* @return void
*/
protected function log( $message ) {
$prefix = '[Incoming Post]';
if ( ! empty( $this->payload ) ) {
$prefix .= ' ' . $this->payload['network_post_id'];
protected function log( $message, $type = 'error' ) {
if ( method_exists( 'Newspack\Logger', 'newspack_log' ) ) {
\Newspack\Logger::newspack_log(
'newspack_network_incoming_post',
$message,
[
'network_post_id' => $this->network_post_id,
'post_id' => $this->ID,
'payload' => $this->payload,
],
$type
);
} else {
$prefix = '[Incoming Post]';
if ( ! empty( $this->payload ) ) {
$prefix .= ' ' . $this->payload['network_post_id'];
}
Debugger::log( $prefix . ' ' . $message );
}
Debugger::log( $prefix . ' ' . $message );
}

/**
Expand Down
33 changes: 23 additions & 10 deletions includes/incoming-events/class-user-manually-synced.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,29 @@ public function maybe_sync_user() {
$data = $this->get_data();

// Update user role if changed.
$user_current_role = $user->roles;
$new_role = $data->role ?? '';

if ( ! empty( $new_role ) && $user_current_role !== $new_role ) {
wp_update_user(
[
'ID' => $user->ID,
'role' => $new_role,
]
);
$user_current_roles = $user->roles;
$user_new_roles = $data->role ?? [];
$remove_roles = array_diff( $user_current_roles, $user_new_roles );
$add_roles = array_diff( $user_new_roles, $user_current_roles );

// If the old and new role arrays aren't the same, update the roles.
if ( $remove_roles || $add_roles ) {
// Get the user object.
$current_user = new \WP_User( $user->ID );

// Get rid of any roles that aren't being pushed.
if ( $remove_roles ) {
foreach ( $remove_roles as $role ) {
$current_user->remove_role( $role );
}
}

// Assign each new role.
if ( $add_roles ) {
foreach ( $add_roles as $role ) {
$current_user->add_role( $role );
}
}
}

// Loop through user props and update.
Expand Down
59 changes: 0 additions & 59 deletions tests/unit-tests/test-content-distribution-admin.php

This file was deleted.

0 comments on commit 968e655

Please sign in to comment.