Support » Plugin: Email Users » preferences to receive emails revert to 'false' (fix provided)

  • Resolved maximinime

    (@maximinime)


    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/

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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.

    Plugin Author Mike Walsh

    (@mpwalsh8)

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

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

    Plugin Author Mike Walsh

    (@mpwalsh8)

    Excellent news thank @maximinime for the solution.

    Thanks @maximinime

    BTW, while update_usermeta is deprecated, update_user_meta exists.

    maximinime

    (@maximinime)

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘preferences to receive emails revert to 'false' (fix provided)’ is closed to new replies.