Skip Navigation

[Resolved] Split: Messaging to the group/Individual user. – connect message

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 0 replies, has 0 voices.

Last updated by jum 1 week, 3 days ago.

Assisted by: Christopher Amirian.

Author
Posts
#2788873

Christopher Amirian
Supporter

Languages: English (English )

Hi,

This is Christopher. Minesh is on vacation and I will reply till he comes back.

From what I understand, The main problem is that the conditional logic in the "Message Single Item" template prevents users from seeing the reply form for group messages. This happens because the condition checks whether the current user’s ID matches either the message-to or message-from fields. Since message-to refers to the Group ID, not individual user IDs, the condition does not evaluate as true for all group members.

If that is the case, I suggest that you do as follows:

1. Update Conditional Logic:

Use the custom field email-group-users (which stores group member IDs) to check if the current user is a part of the group. Here’s the revised conditional:

[wpv-conditional if="( '[wpv-current-user info='id']' eq '[types field='message-to' output='raw'][/types]' ) 
OR ( '[wpv-current-user info='id']' eq '[types field='message-from' output='raw'][/types]' ) 
OR ( strpos('[types field="email-group-users" output="raw"]', '[wpv-current-user info="id"]') !== false )" debug="true"]

[cred_form form="Reply message"]
[/wpv-conditional]

2. Ensure email-group-users Updates Correctly:

Modify the custom code that updates email-group-users to ensure it properly captures all group members. Here’s a refined version of the code:

add_action( 'cred_save_data_192677', 'set_post_id_in_field_group_email', 10, 2 );
function set_post_id_in_field_group_email( $post_id, $form_data ) {
    $parent_id = get_post_meta($post_id, 'wpcf-message-to', true);  
    $relationship_slug = 'uni-group-user-group';
    $related_posts = toolset_get_related_posts(
        $parent_id,
        $relationship_slug,
        'parent',
        999,
        0,
        [],
        'post_id',
        'child'
    );
    if (!empty($related_posts)) {
        $child_ids = implode(',', $related_posts); // Convert IDs to a comma-separated string
        update_post_meta($post_id, 'wpcf-email-group-users', $child_ids);  
    }
}

Now test it with the testuser. If that I did not understand the issue correctly, I'd appreciate it if you expand further in details.

Thanks.

#2788904

jum
user.png

Hi Christopher,

Thanks for supporting

yes the logic you mentioned is right. That's is what i am trying to do

Condition to check whether the current user is in the group then show the reply form.

I tried with the above message single item code the condition its not working seems like the third condition is not taking the custom field value. It taking as an empty string for comparision

FYI: the custom field email-group-users is numeric

Does this numeric field support to check the data stored like 123,4534,23434 ?

[toolset_access role="Guest" operator="deny" raw="true"]
[wpv-conditional if="( '[wpv-current-user info='id']' eq '[types field='message-to' output='raw'][/types]' )
OR ( '[wpv-current-user info='id']' eq '[types field='message-from' output='raw'][/types]' )
OR ( strpos('[types field="email-group-users" output="raw"]', '[wpv-current-user info="id"]') !== false )" debug="true"]

[wpv-post-title id="[types field='first-message-id' output='raw'][/types]"]

[wpv-view name="Related messages" message="[types field='first-message-id' raw='true'][/types]"]

[cred_form form="Reply message"]

[/wpv-conditional]
[wpv-conditional if="( '[wpv-current-user info='id']' ne '[types field='message-to' output='raw'][/types]' ) AND ( '[wpv-current-user info='id']' ne '[types field='message-from' output='raw'][/types]' )"]

You cannot view this message.

[/wpv-conditional] [/toolset_access]

Debug:
####################
wpv-conditional attributes
####################
Array
(
[if] => ( '2680' = '191242' )
OR ( '2680' = '10827' )
OR ( strpos('', '2680') !== false )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( '2680' = '191242' )
OR ( '2680' = '10827' )
OR ( strpos('', '2680') !== false )
--------------------
After replacing 1 general variables and comparing strings: ( 2680 = 191242 )
OR ( 2680 = '10827' )
OR ( strpos('', 2680) !== false )
Comparing 2680 to 191242
After replacing 2 general variables and comparing strings: ( 2680 = 191242 )
OR ( 2680 = 10827 )
OR ( strpos('', 2680) !== false )
Comparing 2680 to 10827

Regarding the email-group-users custom field update the user fields are updating correctly. i used the below code

As user is a post type i take the author of the child user post id so it will match with the current user id in the wordpress.

add_action( 'cred_save_data_192677', 'set_post_id_in_field_group_email', 10, 2 );
function set_post_id_in_field_group_email( $post_id, $form_data ) {

// Retrieve the parent post ID from the 'wpcf-message-to' custom field
$parent_id = get_post_meta($post_id, 'wpcf-message-to', true); // Get the parent ID from the meta field

$relationship_slug = 'uni-group-user-group';

// Get all child posts related to the parent group
$related_posts = toolset_get_related_posts(
$parent_id, // The ID of the parent post
$relationship_slug, // The relationship slug
'parent', // 'parent' means get child posts
100, // Limit (100 as an example)
0, // Offset (no offset)
[], // No additional filters
'post_id', // Get the post IDs
'child' // We're looking for child posts
);

if (!empty($related_posts)) {

$author_ids = []; // Initialize an array to store author IDs

// Loop through each related post (child post)
foreach ($related_posts as $child_id) {
// Get the author ID of the child post
$author_id = get_post_field( 'post_author', $child_id );

// Add each author ID to the array
$author_ids[] = $author_id;
}

// If there are any author IDs, update the custom field 'wpcf-email-group-users'
if (!empty($author_ids)) {
$author_ids_string = implode(',', $author_ids); // Convert the array to a comma-separated string
update_post_meta($post_id, 'wpcf-email-group-users', $author_ids_string); // Save the author IDs in the custom field
}

}
}

please guide me how to set this condition ( strpos('[types field="email-group-users" output="raw"]', '[wpv-current-user info="id"]') !== false )" debug="true"]

Thanks for the support.

#2788943

jum

Hi Christopher,

I have changed the custom field to single line and gave the custom function in settings to check the comparison.

the function gave me the result yes.

when i gave the condition here its not taking the function shortcode [check_user_in_email_group]

[wpv-conditional if="( '[wpv-current-user info='id']' eq '[types field='message-to' output='raw'][/types]' ) OR ( '[wpv-current-user info='id']' eq '[types field='message-from' output='raw'][/types]' ) OR ('[check_user_in_email_group]' eq 'yes' )" debug="true"]

please guide how to give the function shortcode in the condition.

Thanks in advance,

#2789013

Christopher Amirian
Supporter

Languages: English (English )

Hi,

I am not sure if this fact has been mentioned before in the replies or not but you need to register the custom shortcode you used in:

Toolset > Settings > Front-end content

That way, Toolset will understand the shortcode and will wait for the rendering of the shortcode result.

Thanks.

#2789134

jum

Hi christopher,

I gave the condition in custom code and gave the shortcode in the template and form working as expected.

Thanks for the support