| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( ! class_exists( 'ewduwpmUserManager' ) ) { |
| 5 |
/** |
| 6 |
* Class to handle subscription/unsubscription of users |
| 7 |
* |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
class ewduwpmUserManager { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
add_action( 'register_form', array( $this, 'add_registration_checkboxes' ), 99 ); |
| 15 |
add_action( 'user_register', array( $this, 'save_registration_checkboxes' ) ); |
| 16 |
|
| 17 |
add_action( 'personal_options', array( $this, 'edit_profile_checkboxes' ) ); |
| 18 |
add_action( 'personal_options_update', array( $this, 'save_edit_profile_checkboxes' ) ); |
| 19 |
add_action( 'edit_user_profile_update', array( $this, 'save_edit_profile_checkboxes' ) ); |
| 20 |
|
| 21 |
add_filter( 'the_content', array( $this, 'append_subscription_interests' ) ); |
| 22 |
add_action( 'the_content', array( $this, 'process_unsubscribe_link_click' ) ); |
| 23 |
|
| 24 |
add_filter( 'user_contactmethods', array( $this, 'maybe_add_phone_to_contact_methods' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Add subscribe/unsubscribe checkboxes to the registration form, if selected |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
public function add_registration_checkboxes() { |
| 32 |
global $ewd_uwpm_controller; |
| 33 |
|
| 34 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-subscribe-checkbox' ) ) { |
| 35 |
|
| 36 |
?> |
| 37 |
|
| 38 |
<style>.login input[type=checkbox] {width:auto;}</style> |
| 39 |
<label for="ewd_uwpm_subscribe"><?php _e( 'Subscribe to Update Notifications', 'ultimate-wp-mail' ) ?><br /> |
| 40 |
<input type="checkbox" name="ewd_uwpm_subscribe" id="ewd_uwpm_subscribe" class="input" value="<?php echo esc_attr( wp_unslash( $ewd_uwpm_subscribe ) ); ?>" size="25" /></label> |
| 41 |
|
| 42 |
<?php |
| 43 |
} |
| 44 |
|
| 45 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-unsubscribe-checkbox' ) ) { |
| 46 |
|
| 47 |
?> |
| 48 |
|
| 49 |
<style>.login input[type=checkbox] {width:auto;}</style> |
| 50 |
<label for="ewd_uwpm_unsubscribe"><?php _e( 'Unsubscribe from Update Notifications', 'ultimate-wp-mail' ) ?><br /> |
| 51 |
<input type="checkbox" name="ewd_uwpm_unsubscribe" id="ewd_uwpm_unsubscribe" class="input" value="<?php echo esc_attr( wp_unslash( $ewd_uwpm_unsubscribe ) ); ?>" size="25" /></label> |
| 52 |
|
| 53 |
<?php |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Save the selections for subscribe/unsubscribe checkboxes, if enabled |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
public function save_registration_checkboxes( $user_id ) { |
| 62 |
global $ewd_uwpm_controller; |
| 63 |
|
| 64 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-subscribe-checkbox' ) ) { |
| 65 |
|
| 66 |
if ( isset( $_POST['ewd_uwpm_subscribe'] ) ) { update_user_meta( $user_id, 'EWD_UWPM_User_Subscribe', 'Yes' ); } |
| 67 |
else { update_user_meta( $user_id, 'EWD_UWPM_User_Subscribe', 'No' ); } |
| 68 |
} |
| 69 |
|
| 70 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-unsubscribe-checkbox' ) ) { |
| 71 |
|
| 72 |
if ( isset( $_POST['ewd_uwpm_unsubscribe'] ) ) { update_user_meta( $user_id, 'EWD_UWPM_User_Unsubscribe', 'Yes' ); } |
| 73 |
else { update_user_meta( $user_id, 'EWD_UWPM_User_Unsubscribe', 'No' ); } |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Add subscribe/unsubscribe checkboxes to the edit profile form, if selected |
| 79 |
* @since 1.0.0 |
| 80 |
*/ |
| 81 |
public function edit_profile_checkboxes( $user ) { |
| 82 |
global $ewd_uwpm_controller; |
| 83 |
|
| 84 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-subscribe-checkbox' ) ) { |
| 85 |
|
| 86 |
?> |
| 87 |
|
| 88 |
<tr class="show-admin-bar user-admin-bar-front-wrap"> |
| 89 |
<th scope="row"><?php _e( 'Subscribe to Updates', 'ultimate-wp-mail' ); ?></th> |
| 90 |
<td> |
| 91 |
<fieldset> |
| 92 |
<label for="ewd_uwpm_subscribe"> |
| 93 |
<input name="ewd_uwpm_subscribe" type="checkbox" id="ewd_uwpm_subscribe" value="1" <?php echo ( get_user_meta( $user->ID, 'EWD_UWPM_User_Subscribe', true) == "Yes" ) ? 'checked' : ''; ?> /> |
| 94 |
</label> |
| 95 |
</fieldset> |
| 96 |
</td> |
| 97 |
</tr> |
| 98 |
|
| 99 |
<?php |
| 100 |
} |
| 101 |
|
| 102 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-unsubscribe-checkbox' ) ) { |
| 103 |
|
| 104 |
?> |
| 105 |
|
| 106 |
<tr class="show-admin-bar user-admin-bar-front-wrap"> |
| 107 |
<th scope="row"><?php _e( 'Unsubscribe from Updates', 'ultimate-wp-mail' ); ?></th> |
| 108 |
<td> |
| 109 |
<fieldset> |
| 110 |
<label for="ewd_uwpm_subscribe"> |
| 111 |
<input name="ewd_uwpm_unsubscribe" type="checkbox" id="ewd_uwpm_unsubscribe" value="1" <?php echo ( get_user_meta( $user->ID, 'EWD_UWPM_User_Unsubscribe', true ) == "Yes" ) ? 'checked' : ''; ?> /> |
| 112 |
</label> |
| 113 |
</fieldset> |
| 114 |
</td> |
| 115 |
</tr> |
| 116 |
|
| 117 |
<?php |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Save the selections for subscribe/unsubscribe checkboxes, if enabled |
| 123 |
* @since 1.0.0 |
| 124 |
*/ |
| 125 |
public function save_edit_profile_checkboxes( $user_id ) { |
| 126 |
global $ewd_uwpm_controller; |
| 127 |
|
| 128 |
if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-subscribe-checkbox' ) ) { |
| 134 |
|
| 135 |
if ( isset( $_POST['ewd_uwpm_subscribe'] ) ) { update_user_meta( $user_id, 'EWD_UWPM_User_Subscribe', 'Yes' ); } |
| 136 |
else { update_user_meta( $user_id, 'EWD_UWPM_User_Subscribe', 'No' ); } |
| 137 |
} |
| 138 |
|
| 139 |
if ( $ewd_uwpm_controller->settings->get_setting( 'add-unsubscribe-checkbox' ) ) { |
| 140 |
|
| 141 |
if ( isset( $_POST['ewd_uwpm_unsubscribe'] ) ) { update_user_meta( $user_id, 'EWD_UWPM_User_Unsubscribe', 'Yes' ); } |
| 142 |
else { update_user_meta( $user_id, 'EWD_UWPM_User_Unsubscribe', 'No' ); } |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Sets a user to 'unsubscribed' when they click on the unsubscribe email link |
| 148 |
* @since 1.0.0 |
| 149 |
*/ |
| 150 |
public function process_unsubscribe_link_click( $content ) { |
| 151 |
global $ewd_uwpm_controller; |
| 152 |
|
| 153 |
if ( empty( $_GET['action'] ) or $_GET['action'] != 'ewd_uwpm_unsubscribe' ) { return $content; } |
| 154 |
|
| 155 |
$user_id = intval( substr( $_GET['code'], 0, strpos( $_GET['code'], 'pl' ) ) ); |
| 156 |
|
| 157 |
if ( empty( $user_id ) ) { return $content; } |
| 158 |
|
| 159 |
$user = get_user_by( 'id', $user_id ); |
| 160 |
|
| 161 |
if ( $user->user_email != $_GET['email'] ) { return $content; } |
| 162 |
|
| 163 |
update_user_meta( $user->ID, 'EWD_UWPM_User_Unsubscribe', 'Yes' ); |
| 164 |
|
| 165 |
if( ! empty( $ewd_uwpm_controller->settings->get_setting( 'unsubscribe-redirect-url' ) ) ) { |
| 166 |
wp_redirect( $ewd_uwpm_controller->settings->get_setting( 'unsubscribe-redirect-url' ) ); |
| 167 |
exit(0); |
| 168 |
} |
| 169 |
else { |
| 170 |
return '<p>' . __( 'You have been successully unsubscribed.', 'ultimate-wp-mail' ) . '</p>' . $content; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Adds the post interest checkboxes to the top/bottom of a post, if selected |
| 176 |
* @since 1.0.0 |
| 177 |
*/ |
| 178 |
public function append_subscription_interests( $content ) { |
| 179 |
global $ewd_uwpm_controller; |
| 180 |
|
| 181 |
$post = get_post(); |
| 182 |
|
| 183 |
if ( empty( $post ) or $post->post_type != 'post' ) { return $content; } |
| 184 |
|
| 185 |
$categories = wp_get_post_categories( $post->ID ); |
| 186 |
|
| 187 |
$content = $ewd_uwpm_controller->settings->get_setting( 'display-post-interests' ) == 'before' ? do_shortcode( '[subscription-interests display_interests="post_categories" post_categories="' . implode( ',', $categories ) . '"]' ) . $content : $content; |
| 188 |
|
| 189 |
$content = $ewd_uwpm_controller->settings->get_setting( 'display-post-interests' ) == 'after' ? $content . do_shortcode( '[subscription-interests display_interests="post_categories" post_categories="' . implode( ',', $categories ) . '"]' ) : $content; |
| 190 |
|
| 191 |
return $content; |
| 192 |
} |
| 193 |
|
| 194 |
public function maybe_add_phone_to_contact_methods( $methods ) { |
| 195 |
global $ewd_uwpm_controller; |
| 196 |
|
| 197 |
if ( empty( $ewd_uwpm_controller->permissions->check_permission( 'sms' ) ) ) { return $methods; } |
| 198 |
|
| 199 |
return array_merge( array( 'ewd_uwpm_phone_number' => __( 'Phone Number', 'ultimate-wp-mail' ) ), $methods ); |
| 200 |
} |
| 201 |
} |
| 202 |
} |