Skip to content

Commit

Permalink
Merge pull request #1540 from tomusborne/bug/dynamic-tags-user-author…
Browse files Browse the repository at this point in the history
…-meta

Bug/dynamic tags user author meta
  • Loading branch information
tomusborne authored Dec 4, 2024
2 parents 3765126 + cd5de03 commit 9ef1ffc
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 84 deletions.
14 changes: 2 additions & 12 deletions includes/class-meta-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class GenerateBlocks_Meta_Handler extends GenerateBlocks_Singleton {
const DISALLOWED_KEYS = [
'post_password',
'password',
'user_pass',
];

/**
Expand Down Expand Up @@ -214,18 +215,7 @@ public static function get_meta( $id, $key, $single_only = true, $callable = nul
// Some user meta is stored as user data.
// If we're looking for user meta and can't find it, let's check for user data as well.
if ( ! $meta && 'get_user_meta' === $callable ) {
$user_data_keys = [
'user_nicename',
'user_email',
'display_name',
'ID',
];

if ( in_array( $key, $user_data_keys, true ) ) {
$userdata = get_userdata( $id )->data ?? new stdClass();

$meta = $userdata->{$key} ?? '';
}
$meta = get_the_author_meta( $parent_name, $id );
}

$meta = apply_filters(
Expand Down
161 changes: 145 additions & 16 deletions includes/dynamic-tags/class-dynamic-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,26 @@ public function register_rest_routes() {
),
)
);

register_rest_route(
'generateblocks/v1',
'/get-user-record',
array(
'methods' => 'GET',
'callback' => [ $this, 'get_user_record' ],
'permission_callback' => function() {
return current_user_can( 'edit_posts' );
},
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => function( $param ) {
return is_numeric( $param );
},
),
),
)
);
}

/**
Expand Down Expand Up @@ -529,6 +549,49 @@ public function get_dynamic_tag_replacements( $request ) {
return rest_ensure_response( $replacements );
}


/**
* Add meta fields to a user record.
*
* @param WP_User $user The user object to update.
* @return WP_User The updated user object.
*/
public function add_meta_to_user_record( $user ) {
if ( ! $user ) {
return $user;
}

$data = get_object_vars( $user->data );
$meta = array_filter(
get_user_meta( $user->ID ),
function ( $key ) {
$hidden = generateblocks_str_starts_with( $key, '_' );
$is_rest = $this->is_rest_field( $key );

return ! $hidden && $is_rest;
},
ARRAY_FILTER_USE_KEY
);

$user_meta = array_merge(
$data,
$meta
);

// Remove all hidden or disallowed meta fields.
$user->meta = array_filter(
$user_meta,
function( $key ) {
$disallowed = in_array( $key, GenerateBlocks_Meta_Handler::DISALLOWED_KEYS, true );

return ! $disallowed;
},
ARRAY_FILTER_USE_KEY
);

return $user;
}

/**
* Get our post record based on the requested load and post ID.
*
Expand Down Expand Up @@ -558,22 +621,6 @@ function ( $key ) {
$response->meta = $post_meta;
}

// Fetch author data if requested.
if ( in_array( 'author', $load, true ) ) {
$author = get_user_by( 'ID', $post->post_author );

// Remove all hidden meta fields.
$author->meta = array_filter(
get_user_meta( $post->post_author ),
function( $key ) {
return ! generateblocks_str_starts_with( $key, '_' );
},
ARRAY_FILTER_USE_KEY
);

$response->author = $author->data;
}

// Fetch comments if requested.
if ( in_array( 'comments', $load, true ) ) {
$comments = get_comments( array( 'post_id' => $id ) );
Expand Down Expand Up @@ -616,6 +663,53 @@ function( $key ) {
return rest_ensure_response( $filtered_response );
}

/**
* Get the record for a specific user by ID.
*
* @param WP_REST_Request $request Full data about the request.
*/
public function get_user_record( WP_REST_Request $request ) {
$id = $request->get_param( 'id' );

if ( ! $id ) {
return rest_ensure_response(
new WP_Error(
'Invalid user ID',
'User ID is required',
[ 'status' => 400 ]
)
);
}

$response = $this->add_meta_to_user_record(
get_user_by( 'ID', $id )
)->data;

if ( ! $response ) {
$response = new WP_Error(
'User not found',
'User not found',
[ 'status' => 400 ]
);
}

/**
* Allows filtering of the post record response data to add or alter data.
*
* @since 2.0.0
* @param array $response Array of response data.
* @param int $id ID of the user record.
*
* @return \WP_REST_Response|\WP_Error Response object.
*/
$filtered_response = apply_filters(
'generateblocks_dynamic_tags_user_record_response',
$response,
$id
);

return rest_ensure_response( $filtered_response );
}

/**
* Before tag replace.
Expand Down Expand Up @@ -730,6 +824,41 @@ public static function expand_allowed_html( $tags, $context ) {

return $tags;
}

/**
* Check if a given key is registered as a REST field.
*
* @param string $field_name The name of the field to check.
* @param string|array $types The type(s) to check against. Default is 'post', 'term', 'comment', 'user', and 'settings'.
*
* @return boolean
*/
public function is_rest_field(
$field_name,
$types = [
'post',
'term',
'comment',
'user',
'settings',
'page',
]
) {

if ( is_string( $types ) ) {
$types = [ $types ];
}

foreach ( $types as $type ) {
$fields = get_registered_meta_keys( $type );

if ( isset( $fields[ $field_name ] ) ) {
return true;
}
}

return false;
}
}

GenerateBlocks_Dynamic_Tags::get_instance()->init();
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"dependencies": {
"@edge22/block-styles": "^1.1.31",
"@edge22/components": "^1.1.43",
"@edge22/components": "^1.1.45",
"@edge22/styles-builder": "^1.2.45",
"@wordpress/block-editor": "^12.12.0",
"@wordpress/blocks": "^12.21.0",
Expand Down
9 changes: 7 additions & 2 deletions src/dynamic-tags/components/DynamicTagModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ export function DynamicTagModal( {
} ) {
const [ isOpen, setOpen ] = useState( false );
const [ tagToEdit, setTagToEdit ] = useState( null );
const currentPost = useSelect( ( select ) => {
const { currentPost, currentUser } = useSelect( ( select ) => {
const { getCurrentPost } = select( editorStore );
const { getCurrentUser } = select( 'core' );

return getCurrentPost ? getCurrentPost() : null;
return {
currentPost: getCurrentPost ? getCurrentPost() : null,
currentUser: getCurrentUser ? getCurrentUser() : null,
};
} );

function onToggle() {
Expand Down Expand Up @@ -145,6 +149,7 @@ export function DynamicTagModal( {
selectedText={ tagToEdit || selectedText }
tagToReplace={ tagToEdit }
currentPost={ currentPost }
currentUser={ currentUser }
context={ context }
/>
) }
Expand Down
Loading

0 comments on commit 9ef1ffc

Please sign in to comment.