/** * Redirects user after login * * @param string $redirect_to URL to redirect to. * @param string $request URL the user is coming from. * @param object $user Logged user's data * @return string */ function palo_filter_login_redirect($redirect_to, $request, $user) { global $palo_options; /** * Exlude adminsistrators * * TODO: Mimic exact WP default behaviour */ if ($user && is_object($user) && is_a($user, 'WP_User') && $user->has_cap('administrator')) { if ($redirect_to) { palo_redirect($redirect_to); } else { palo_redirect(admin_url()); } } $palo_login_behavior = assign_if_exists('palo_login_behavior', $palo_options, 'PALO_REDIRECT_DEFAULT'); $palo_login_url = assign_if_exists('palo_login_url', $palo_options, home_url()); $redirect_to_value = assign_if_exists('redirect_to', $_GET); if ($redirect_to_value) { $referer = $redirect_to_value; } else { $referer = assign_if_exists('HTTP_REFERER', $_SERVER, $redirect_to_value); } $referer_no_query_string = preg_replace('/\\?.*/', '', $referer); /** * Perform the redirect depending on the option */ switch ($palo_login_behavior) { case 'PALO_REDIRECT_HOME': if (!is_a($user, 'WP_Error')) { wp_redirect(home_url()); exit; } break; case 'PALO_REDIRECT_URL': if (is_a($user, 'WP_User')) { palo_redirect(esc_url_raw($palo_login_url)); } break; case 'PALO_REDIRECT_CURRENT': /* Todo */ /* Todo */ default: return $redirect_to; } }
function wp_new_user_notification($user_id, $plaintext_pass = '') { global $palo_options; $password_on_registration_enabled = (bool) assign_if_exists('palo_password_on_registration', $palo_options); $custom_subject = trim(assign_if_exists('palo_setting_registration_email_subject', $palo_options)); $custom_message = trim(assign_if_exists('palo_registration_email_message', $palo_options)); $user = get_userdata($user_id); if ($password_on_registration_enabled) { $plaintext_pass = $_POST['palo_password']; } // The blogname option is escaped with esc_html on the way into the database in sanitize_option // we want to reverse this for the plain text arena of emails. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $subject = sprintf(__('[%s] New User Registration', 'pressapps'), $blogname); $message = sprintf(__('New user registration on your site %s:', 'pressapps'), $blogname) . "\r\n\r\n"; $message .= sprintf(__('Username: %s', 'pressapps'), $user->user_login) . "\r\n\r\n"; $message .= sprintf(__('E-mail: %s', 'pressapps'), $user->user_email) . "\r\n"; @wp_mail(get_option('admin_email'), $subject, $message); if (empty($plaintext_pass)) { return; } if ($custom_subject) { $subject = $custom_subject; } else { $subject = sprintf(__('[%s] Your username and password', 'pressapps'), $blogname); } if ($custom_message) { $message = $custom_message; $message = str_replace(array('%username%', '%password%', '%loginlink%'), array($user->user_login, $plaintext_pass, wp_login_url()), $message); } else { $message = sprintf(__('Username: %s', 'pressapps'), $user->user_login) . "\r\n"; $message .= sprintf(__('Password: %s', 'pressapps'), $plaintext_pass) . "\r\n"; $message .= wp_login_url() . "\r\n"; } @wp_mail($user->user_email, $subject, $message); /** * Login after registration */ if ($password_on_registration_enabled) { $creds['user_login'] = $_POST['user_login']; $creds['user_password'] = $_POST['palo_password']; $creds['remember'] = true; wp_signon($creds, false); } /** * Redirect after login */ if ($password_on_registration_enabled) { /** * Where to redirect, replace empty URLs with home_url(); */ $palo_login_behavior = assign_if_exists('palo_logout_behavior', $palo_options, 'PALO_REDIRECT_DEFAULT'); $palo_login_url = trim(assign_if_exists('palo_login_url', $palo_options)); $palo_login_url = $palo_login_url ? esc_url_raw($palo_login_url) : home_url(); /** * Redirect */ switch ($palo_login_behavior) { case 'PALO_REDIRECT_URL': palo_redirect($palo_login_url); break; case 'PALO_REDIRECT_CURRENT': /* Todo */ break; default: palo_redirect(home_url()); } } }
/** * Redirect if this page is restricted */ function palo_action_frontend_access_control() { /** * Do not check access on non-posts */ if (!is_singular()) { return; } /** * Do not check access for logged in users */ if (is_user_logged_in()) { return; } global $palo_options, $post; $action = assign_if_exists('palo_access_action', $palo_options); $excluded = false; $post_type = $post->post_type; $post_type_taxonomies = get_object_taxonomies($post_type); $post_type_exceptions = assign_if_exists('palo_access_exceptions_' . $post_type, $palo_options, array()); foreach ($post_type_taxonomies as $taxonomy) { $post_terms[$taxonomy] = get_the_terms($post->ID, $taxonomy); if (!empty($post_terms[$taxonomy])) { foreach ($post_terms[$taxonomy] as $term) { $post_terms[$taxonomy][$term->term_id] = $term->name; } } } // Check if "All" excluded if (in_array('_all_', $post_type_exceptions)) { $excluded = true; } // If the post type is not excluded, check if post is excuded by ID if (!$excluded) { if (in_array($post->ID, $post_type_exceptions)) { $excluded = true; } } // If the post type is not excluded, check if post is excuded by taxonomy term if (!$excluded) { if (!empty($post_terms)) { foreach ($post_terms as $taxonomy => $terms) { if (!empty($terms)) { foreach ($terms as $term_id => $term_name) { if (in_array("{$taxonomy}:{$term_id}", $post_type_exceptions)) { $excluded = true; } } } } } } /** * Allow or block * * This is how it works * - Block if: * - action != block AND post == excluded * - action == block * - Allow if: * - action == block AND post == excluded * - action != block * */ if ('PALO_ACCESS_ACTION_BLOCK' !== $action && $excluded || 'PALO_ACCESS_ACTION_BLOCK' === $action && !$excluded) { // Where to redirect if ('PALO_REDIRECT_URL' === assign_if_exists('palo_access_behavior', $palo_options)) { $access_url = assign_if_exists('palo_access_url', $palo_options); // If URL is empty, use login URL if (!$access_url) { $access_url = wp_login_url(); } } else { $access_url = wp_login_url(); } // Redirect palo_redirect($access_url); } }