WordPress.org

Forums

Email Users
[resolved] preferences to receive emails revert to 'false' (fix provided) (7 posts)

  1. maximinime
    Member
    Posted 2 years ago #

    I notice you put in a kind of fix for this here, but it is insufficient. The problem occurs because any call to update a user will check if the preference is POSTed and, if not, set it to false. Users can be updated in many ways without using the default interface, so your field isn't always present, and the preference goes to default.

    This kind of problem (with checkboxes in general) can be easily solved in forms processed by php. Simply put a hidden input with the same name as the checkbox before the checkbox. If the box isn't ticked, the hidden value is used. If it is ticked the checkbox's value overwrites the hidden value in the $_POST array. Basically, avoid using isset() with checkboxes.

    in mailusers_edit_any_user_profile_form() change this:

    <input 	type="checkbox"
    						name="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>"
    						id="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>"
    						value="true"
    						<?php if (get_user_meta($uid, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, true)=='true') echo 'checked="checked"'; ?> ></input>
    				<?php _e('Accept to receive post or page notification emails', MAILUSERS_I18N_DOMAIN); ?><br/>
    				<input 	type="checkbox"
    						name="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>"
    						id="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>"
    						value="true"
    						<?php if (get_user_meta($uid, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, true)=='true') echo 'checked="checked"'; ?> ></input>

    to this:

    <input type="hidden" name="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>" value="false">
    				<input 	type="checkbox"
    						name="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>"
    						id="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>"
    						value="true"
    						<?php if (get_user_meta($uid, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, true)=='true') echo 'checked="checked"'; ?> ></input>
    				<?php _e('Accept to receive post or page notification emails', MAILUSERS_I18N_DOMAIN); ?><br/>
    				<input type="hidden" name="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>" value="false">
    				<input 	type="checkbox"
    						name="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>"
    						id="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>"
    						value="true"
    						<?php if (get_user_meta($uid, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, true)=='true') echo 'checked="checked"'; ?> ></input>

    remove this entirely:

    else {
    ?>
    <input 	type="hidden" name="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>" id="<?php echo MAILUSERS_ACCEPT_NOTIFICATION_USER_META; ?>" value="<?php echo (get_user_meta($uid, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, true) === 'true') ? "true" : "false"; ?>"></input>
    <input 	type="hidden" name="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>" id="<?php echo MAILUSERS_ACCEPT_MASS_EMAIL_USER_META; ?>" value="<?php echo (get_user_meta($uid, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, true) === 'true') ? "true" : "false"; ?>"></input>
    <?php
        }

    then in mailusers_any_user_profile_update() remove:

    } else {
    		update_usermeta($uid, MAILUSERS_ACCEPT_NOTIFICATION_USER_META, 'false');

    and remove:

    } else {
    		update_usermeta($uid, MAILUSERS_ACCEPT_MASS_EMAIL_USER_META, 'false');

    Thanks!

    http://wordpress.org/extend/plugins/email-users/

  2. maximinime
    Member
    Posted 2 years ago #

    Better still, in mailusers_any_user_profile_update(), instead of removing the elses, change the lines like

    update_usermeta($uid, MAILUSERS_ACCEPT_*_META, 'false');

    to:

    add_user_meta($uid,  MAILUSERS_ACCEPT_*_META, get_option('mailusers_default_*'), true);

    obviously, replacing the *. Then the default is only set if it doesn't already exist.

    Also, update_usermeta() is deprecated.

  3. Mike Walsh
    Member
    Plugin Author

    Posted 2 years ago #

    Thank you for the solution. I have implemented in my latest beta update (beta 4). I hope to push this update out tomorrow.

  4. Andy Fragen
    Member
    Posted 2 years ago #

    This fix in latest update (beta 5) is working. Yeah!

  5. Mike Walsh
    Member
    Plugin Author

    Posted 2 years ago #

    Excellent news thank @maximinime for the solution.

  6. Andy Fragen
    Member
    Posted 2 years ago #

    Thanks @maximinime

    BTW, while update_usermeta is deprecated, update_user_meta exists.

  7. maximinime
    Member
    Posted 2 years ago #

    No worries Andy. Yes, my point was he should be using the newer function.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Email Users
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic