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

maybe auto select user profile #56

Merged
merged 8 commits into from
Oct 15, 2021
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
22 changes: 8 additions & 14 deletions client/src/data/modules/hydrate/getHydrateProfiles.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
/**
* Hydrate profiles for use in this React app. The data is saved in another
* format, so we will need to trandform the data back when saving to post meta.
* format, so we will need to transform the data back when saving to post meta.
*
* @param {Array} items Array of profiles from post meta.
* @return {Promise} Promise from an API request to get hydrated profiles.
*/
const getHydrateProfiles = async (items = []) => {
if (0 >= items.length) {
return Promise.resolve([]);
}

return wp.apiFetch({
path: '/byline-manager/v1/hydrateProfiles/',
method: 'POST',
data: {
profiles: items,
},
}).catch(() => []);
};
const getHydrateProfiles = async (items = []) => wp.apiFetch({
path: '/byline-manager/v1/hydrateProfiles/',
method: 'POST',
data: {
profiles: items,
},
}).catch(() => []);

export default getHydrateProfiles;
18 changes: 17 additions & 1 deletion inc/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,21 @@ function rest_profile_search( \WP_REST_Request $request ) {
function rest_hydrate_profiles( \WP_REST_Request $request ) {
$byline_profiles = $request['profiles'] ?? [];
$profiles = [];

// Check to see if the current user has profile associated with their user.
$current_user_profile_id = get_user_meta( get_current_user_id(), 'profile_id', true );
$current_user_profile = Profile::get_by_post( $current_user_profile_id );

/**
* Determine wether to auto set byline if a user object has a byline associated with it.
*
* @param boolean wether or not to auto set profile.
*/
$auto_set_user_profile = apply_filters( 'byline_manager_auto_set_user_profile', true );

/**
* If we have bylines, hydrate them with meta values.
* Else return the user's associated profile if one was found and $auto_set_user_profile === true.
*/
if ( ! empty( $byline_profiles ) ) {
$index = 0;

Expand Down Expand Up @@ -118,6 +132,8 @@ function rest_hydrate_profiles( \WP_REST_Request $request ) {
}

$byline_profiles = $profiles;
} elseif ( $current_user_profile instanceof Profile && $auto_set_user_profile ) {
$profiles[] = get_profile_data_for_meta_box( $current_user_profile );
}

// Send the response.
Expand Down