/** * Sets the right href and class attributes for the modal link in menus */ function palo_filter_frontend_modal_link_atts($atts, $item, $args) { /** * Ony apply to modal login or register */ if (in_array($atts['href'], array('#pa_modal_login', '#pa_modal_register'))) { /** * Add trigger if not logged in */ if (!is_user_logged_in()) { $atts['class'] = assign_if_exists('class', $atts) . ' palo-modal-login-trigger'; } /** * Modify links */ if ('#pa_modal_login' == $atts['href']) { if (is_user_logged_in()) { $atts['href'] = wp_logout_url(); } else { $atts['href'] = wp_login_url(); $atts['data-palo-modal'] = palo_append_qs(wp_login_url(), 'palo-login=1'); } } else { if ('#pa_modal_register' == $atts['href']) { $atts['href'] = wp_registration_url(); $atts['data-palo-modal'] = palo_append_qs(wp_registration_url(), 'palo-login=1'); } } } return $atts; }
/** * 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()); } } }
/** * Generates and outputs the HTML for the "Access Control" group * * The function is used as a callback function in add_settings_field(), */ function palo_setting_group_access_control_html($args) { global $palo_options, $palo_textdomain; $option_id = 'palo_access_action'; $name = "palo_options[{$option_id}]"; $post_types = get_post_types(array('public' => true)); /** * Exclude Media as it's most of the time * not used directly */ unset($post_types['attachment']); $radios = array('PALO_ACCESS_ACTION_DEFAULT' => __('Authorize All Content', $palo_textdomain), 'PALO_ACCESS_ACTION_BLOCK' => __('Block All Content', $palo_textdomain)); if (!empty($palo_options[$option_id])) { $checked = $palo_options[$option_id]; } else { $checked = false; } echo '<h4 class="palo-field-label">' . __('Restrict access for non logged in users', $palo_textdomain) . '</h4>'; echo '<p>'; foreach ($radios as $value => $label) { /** * We should we check this radio if the condition is met * * At least one of these conditions is met: * - This choice has been set it the past * - (or) No choices has been set *and* this choice ends with "_DEFAULT" */ $check_it = $value === $checked || !$checked && preg_match('/_DEFAULT$/', $value) ? 'checked' : ''; printf('<label class="button"><span class="hidden"><input type="radio" name="%s" value="%s" %s /></span>%s</label> ', $name, $value, $check_it, $label); } echo '</p>'; /** * Print one dropdown of exception for each post types (including custom) */ echo '<h4>' . __('Except', $palo_textdomain) . '</h4>'; foreach ($post_types as $post_type => $post_type_args) { $post_type_obj = get_post_type_object($post_type); $post_type_name = $post_type_obj->labels->singular_name; $exceptions = array(); $saved_exceptions = assign_if_exists('palo_access_exceptions_' . $post_type, $palo_options, array()); /** * Add taxonomy/terms options */ $taxonomies = get_object_taxonomies($post_type); foreach ($taxonomies as $taxonomy) { $taxonomy_obj = get_taxonomy($taxonomy); $taxonomy_name = $taxonomy_obj->labels->singular_name; $terms = get_categories("taxonomy={$taxonomy}&type={$post_type}"); foreach ($terms as $term) { $exceptions['taxonomies']["{$taxonomy}:{$term->term_id}"] = "[{$taxonomy_name}] {$term->name}"; } } /** * Add posts options */ $the_query = new WP_Query("post_type={$post_type}&posts_per_page=-1"); if ($the_query->have_posts()) { while ($the_query->have_posts()) { $the_query->the_post(); $exceptions['posts'][get_the_ID()] = get_the_ID() . ': ' . (get_the_title() ? get_the_title() : __('[Untitled]', $palo_textdomain)); } } wp_reset_postdata(); /** * Output <select> */ echo '<p>'; printf('<label for="%s"><strong>%s</strong> <em>(%s)</em></label><br />', "palo_access_exceptions_{$post_type}", $post_type_name, $post_type); reset($exceptions); printf('<select multiple autocomplete="off" name="%s" id="%s" class="palo_select2" style="width:%s;">', "palo_options[palo_access_exceptions_{$post_type}][]", "palo_access_exceptions_{$post_type}", "400px"); printf('<option value="_all_" %s >%s</option>', in_array('_all_', $saved_exceptions) ? 'selected' : '', __('All', $palo_textdomain)); if (!empty($exceptions['taxonomies'])) { printf('<optgroup label="%s">', __('Taxonomies', $palo_textdomain)); foreach ($exceptions['taxonomies'] as $k => $v) { $selected = in_array($k, $saved_exceptions) ? 'selected="selected"' : ''; printf('<option value="%s" %s >%s</option>', $k, $selected, $v); } printf('</optgroup>'); } if (!empty($exceptions['posts'])) { printf('<optgroup label="%s">', __('Posts', $palo_textdomain)); foreach ($exceptions['posts'] as $k => $v) { $selected = in_array($k, $saved_exceptions) ? 'selected="selected"' : ''; printf('<option value="%s" %s >%s</option>', $k, $selected, $v); } printf('</optgroup>'); } echo '</select>'; echo '</p>'; } }
/** * Output the CSS of the frontend for profile and forms shortcodes */ function palo_action_front_css() { global $palo_helper, $palo_options; if (!empty($palo_helper['enqueue_front_css']) && ($css = assign_if_exists('palo_setting_front_custom_css_code', $palo_options))) { echo "<style>{$css}</style>"; } }
/** * 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); } }