• Resolved bluedogranch

    (@bluedogranch)


    Hi Ján,
    Thanks for your help! What you gave me in an earlier thread to show on the front end if the current logged-in user is a subscriber is this:

    $current_user_email = wp_get_current_user();
    $email = $current_user_email->user_email;
    $list_id = '4'; // You can get this ID from the URL when editing the list

    if ( class_exists( \MailPoet\API\API::class ) ) {
    $mailpoet_api = \MailPoet\API\API::MP( 'v1' );

    try {
    // Find subscriber
    $subscriber = $mailpoet_api->getSubscriber( $email );

    // Check if the subscriber is subscribed to the specific list
    $is_subscribed = false;
    foreach ( $subscriber[ 'subscriptions' ] as $subscription ) {
    if ( $subscription[ 'segment_id' ] === $list_id ) {
    $is_subscribed = true;
    break;
    }
    }

    if ( $is_subscribed ) {
    echo "The subscriber is subscribed to the list with ID $list_id.";
    } else {
    echo "The subscriber is NOT subscribed to the list with ID $list_id.";
    }
    } catch ( Exception $e ) {
    if ( $e->getCode() === 4 ) {
    echo 'Subscriber not found.';
    } else {
    echo "An error occurred: " . $e->getMessage();
    }
    }
    }

    and it mostly works, except for one issue.

    But what I found might be an edge case: some users who are unsubscribed still show as subscribed with the above function on the front end, i.e. this user in this screenshot https://ibb.co/VYB6sLvX that is from /wp-admin/admin.php?page=mailpoet-subscribers is shown to be subscribed.

    What is the difference between a user that is “Unsubscribed”, yet also shows to be in the New Post Emails list as shown in the screenshot?

    Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Ojoma a11n

    (@geraltrivia)

    Hello there @bluedogranch ,

    Thank you for reaching out to us.

    The reason an unsubscribed user may still appear as part of a list is that MailPoet doesn’t remove subscribers from lists when they unsubscribe; instead, their status is updated to “unsubscribed” while still being associated with the list. This allows you to keep a record of past subscribers while ensuring they no longer receive emails.

    Your function currently checks if a user is associated with a list, but it doesn’t filter based on their status. To accurately determine if they are actively subscribed, you’ll need to check that their status is “subscribed” within the list subscription details. You can update your code like this:

    $is_subscribed = false;
    foreach ( $subscriber['subscriptions'] as $subscription ) {
        if ( $subscription['segment_id'] === $list_id && $subscription['status'] === 'subscribed' ) {
            $is_subscribed = true;
            break;
        }
    }
    

    This ensures that only users who are actively subscribed will return as “subscribed” on the front end, and those who have unsubscribed won’t.

    Let me know if that helps or if you run into any other issues. I’d be happy to assist further!

    Thread Starter bluedogranch

    (@bluedogranch)

    Hi @geraltrivia ,

    Thanks, that makes sense to keep track of unsubscribed users so we know in the dashboard who was a past subscriber.

    I needed to add ( and ) to your new conditional if statement to prevent errors, but it still shows users as subscribed on the front end when they’re shown as unsubscribed in my screenshot of the backend.

    if (( $subscription[ 'segment_id' ] === $list_id) && ($subscription['status'] === 'subscribed'))

    Any ideas?

    Thanks

    Thread Starter bluedogranch

    (@bluedogranch)

    Hi Ján Miklá? @neosinner ,

    Can you help with this?

    Thanks!

    Thread Starter bluedogranch

    (@bluedogranch)

    Hi @lynnjat7 , can you help with my question?

    Thanks!

    Plugin Author Ján Miklá?

    (@neosinner)

    Hey @bluedogranch, sorry for the late reply.

    I forgot to mention this the first time, but you also need to do one other check (and it should be the first thing to do): check the global status of the subscriber. In the getSubscriber, there is a status property, which indicates the global subscriber’s status (regardless of specific list status). When this is not subscribed, the subscriber will not receive emails, even if their specific list status is subscribed.

    For example, you can do something like this:

    // Find subscriber
    $subscriber = $mailpoet_api->getSubscriber( $email );
    if ( $subscriber[ 'status'] !== 'subscribed` ) {
    echo 'User is not subscribed';
    }

    and if they are not globally subscribed, you don’t even need to loop through lists.

    Hope this helps.

    Thread Starter bluedogranch

    (@bluedogranch)

    Hi Jan @neosinner ,

    Thanks, that’s interesting; I did see the status property when I dumped the whole API output. But I want to be sure I’m targeting a particular list for currently logged in users who are subscribed/unsubscribed, not users “even if their specific list status is subscribed.”

    What I need is to get your original code to work with $list_id

    $email = $current_user_email->user_email;
    $list_id = '4';

    if ( class_exists( \MailPoet\API\API::class ) ) {
    $mailpoet_api = \MailPoet\API\API::MP( 'v1' );

    try {
    // Find subscriber
    $subscriber = $mailpoet_api->getSubscriber( $email );

    // Check if the subscriber is subscribed to the specific list
    $is_subscribed = false;
    foreach ( $subscriber[ 'subscriptions' ] as $subscription ) {
    if ( $subscription[ 'segment_id' ] === $list_id ) {
    $is_subscribed = true;
    break;
    }
    }

    if ( $is_subscribed ) {
    echo "The subscriber is subscribed to the list with ID $list_id.";
    } else {
    echo "The subscriber is NOT subscribed to the list with ID $list_id.";
    }
    } catch ( Exception $e ) {
    if ( $e->getCode() === 4 ) {
    echo 'Subscriber not found.';
    } else {
    echo "An error occurred: " . $e->getMessage();
    }
    }
    }

    Plugin Author Ján Miklá?

    (@neosinner)

    @bluedogranch I’ve updated the code a bit, and moved it to a function so it’s easier to understand what’s happening:

    /**
    * Check if a subscriber is subscribed to a specific list
    *
    * @param string $email The subscriber's email address
    * @param string $list_id The ID of the list to check
    * @return bool|string Returns true if subscribed, false if not subscribed or error message if there's an error
    */
    function is_subscribed_to_list($email, $list_id) {
    try {
    // Initialize MailPoet API
    if (!class_exists(\MailPoet\API\API::class)) {
    return 'MailPoet API not available';
    }

    $mailpoet_api = \MailPoet\API\API::MP('v1');

    // Get subscriber
    try {
    $subscriber = $mailpoet_api->getSubscriber($email);
    } catch (\Exception $e) {
    return 'Subscriber not found';
    }

    // Check subscriber's global status first
    if ($subscriber['status'] !== 'subscribed') {
    return false;
    }

    // Check subscription status for the specific list
    if (!empty($subscriber['subscriptions'])) {
    foreach ($subscriber['subscriptions'] as $subscription) {
    if ($subscription['segment_id'] === $list_id) {
    // Check if subscribed to a list
    return $subscription['status'] === 'subscribed';
    }
    }
    }

    return false; // Not subscribed to this list

    } catch (\Exception $e) {
    return 'Error: ' . $e->getMessage();
    }
    }
    Thread Starter bluedogranch

    (@bluedogranch)

    Thanks! That makes sense.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.