Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/05/2016 05:15:55 AM (9 years ago)
Author:
boonebgorges
Message:

Move bp-members classes to their own files.

See #6870.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/classes/class-bp-members-theme-compat.php

    r10515 r10521  
    11<?php
    22/**
    3  * BuddyPress Member Screens.
    4  *
    5  * Handlers for member screens that aren't handled elsewhere.
     3 * BuddyPress Member Theme Compat.
    64 *
    75 * @package BuddyPress
    86 * @subpackage MembersScreens
    9  * @since 1.5.0
     7 * @since 1.7.0
    108 */
    119
    1210// Exit if accessed directly.
    1311defined( 'ABSPATH' ) || exit;
    14 
    15 /**
    16  * Handle the display of the profile page by loading the correct template file.
    17  */
    18 function bp_members_screen_display_profile() {
    19 
    20     /**
    21      * Fires right before the loading of the Member profile screen template file.
    22      *
    23      * @since 1.5.0
    24      */
    25     do_action( 'bp_members_screen_display_profile' );
    26 
    27     /**
    28      * Filters the template to load for the Member profile page screen.
    29      *
    30      * @since 1.5.0
    31      *
    32      * @param string $template Path to the Member template to load.
    33      */
    34     bp_core_load_template( apply_filters( 'bp_members_screen_display_profile', 'members/single/home' ) );
    35 }
    36 
    37 /**
    38  * Handle the display of the members directory index.
    39  */
    40 function bp_members_screen_index() {
    41     if ( bp_is_members_directory() ) {
    42         bp_update_is_directory( true, 'members' );
    43 
    44         /**
    45          * Fires right before the loading of the Member directory index screen template file.
    46          *
    47          * @since 1.5.0
    48          */
    49         do_action( 'bp_members_screen_index' );
    50 
    51         /**
    52          * Filters the template to load for the Member directory page screen.
    53          *
    54          * @since 1.5.0
    55          *
    56          * @param string $value Path to the member directory template to load.
    57          */
    58         bp_core_load_template( apply_filters( 'bp_members_screen_index', 'members/index' ) );
    59     }
    60 }
    61 add_action( 'bp_screens', 'bp_members_screen_index' );
    62 
    63 /**
    64  * Handle the loading of the signup screen.
    65  */
    66 function bp_core_screen_signup() {
    67     $bp = buddypress();
    68 
    69     if ( ! bp_is_current_component( 'register' ) || bp_current_action() )
    70         return;
    71 
    72     // Not a directory.
    73     bp_update_is_directory( false, 'register' );
    74 
    75     // If the user is logged in, redirect away from here.
    76     if ( is_user_logged_in() ) {
    77 
    78         $redirect_to = bp_is_component_front_page( 'register' )
    79             ? bp_get_members_directory_permalink()
    80             : bp_get_root_domain();
    81 
    82         /**
    83          * Filters the URL to redirect logged in users to when visiting registration page.
    84          *
    85          * @since 1.5.1
    86          *
    87          * @param string $redirect_to URL to redirect user to.
    88          */
    89         bp_core_redirect( apply_filters( 'bp_loggedin_register_page_redirect_to', $redirect_to ) );
    90 
    91         return;
    92     }
    93 
    94     $bp->signup->step = 'request-details';
    95 
    96     if ( !bp_get_signup_allowed() ) {
    97         $bp->signup->step = 'registration-disabled';
    98 
    99     // If the signup page is submitted, validate and save.
    100     } elseif ( isset( $_POST['signup_submit'] ) && bp_verify_nonce_request( 'bp_new_signup' ) ) {
    101 
    102         /**
    103          * Fires before the validation of a new signup.
    104          *
    105          * @since 2.0.0
    106          */
    107         do_action( 'bp_signup_pre_validate' );
    108 
    109         // Check the base account details for problems.
    110         $account_details = bp_core_validate_user_signup( $_POST['signup_username'], $_POST['signup_email'] );
    111 
    112         // If there are errors with account details, set them for display.
    113         if ( !empty( $account_details['errors']->errors['user_name'] ) )
    114             $bp->signup->errors['signup_username'] = $account_details['errors']->errors['user_name'][0];
    115 
    116         if ( !empty( $account_details['errors']->errors['user_email'] ) )
    117             $bp->signup->errors['signup_email'] = $account_details['errors']->errors['user_email'][0];
    118 
    119         // Check that both password fields are filled in.
    120         if ( empty( $_POST['signup_password'] ) || empty( $_POST['signup_password_confirm'] ) )
    121             $bp->signup->errors['signup_password'] = __( 'Please make sure you enter your password twice', 'buddypress' );
    122 
    123         // Check that the passwords match.
    124         if ( ( !empty( $_POST['signup_password'] ) && !empty( $_POST['signup_password_confirm'] ) ) && $_POST['signup_password'] != $_POST['signup_password_confirm'] )
    125             $bp->signup->errors['signup_password'] = __( 'The passwords you entered do not match.', 'buddypress' );
    126 
    127         $bp->signup->username = $_POST['signup_username'];
    128         $bp->signup->email = $_POST['signup_email'];
    129 
    130         // Now we've checked account details, we can check profile information.
    131         if ( bp_is_active( 'xprofile' ) ) {
    132 
    133             // Make sure hidden field is passed and populated.
    134             if ( isset( $_POST['signup_profile_field_ids'] ) && !empty( $_POST['signup_profile_field_ids'] ) ) {
    135 
    136                 // Let's compact any profile field info into an array.
    137                 $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
    138 
    139                 // Loop through the posted fields formatting any datebox values then validate the field.
    140                 foreach ( (array) $profile_field_ids as $field_id ) {
    141                     if ( !isset( $_POST['field_' . $field_id] ) ) {
    142                         if ( !empty( $_POST['field_' . $field_id . '_day'] ) && !empty( $_POST['field_' . $field_id . '_month'] ) && !empty( $_POST['field_' . $field_id . '_year'] ) )
    143                             $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $_POST['field_' . $field_id . '_day'] . $_POST['field_' . $field_id . '_month'] . $_POST['field_' . $field_id . '_year'] ) );
    144                     }
    145 
    146                     // Create errors for required fields without values.
    147                     if ( xprofile_check_is_required_field( $field_id ) && empty( $_POST[ 'field_' . $field_id ] ) && ! bp_current_user_can( 'bp_moderate' ) )
    148                         $bp->signup->errors['field_' . $field_id] = __( 'This is a required field', 'buddypress' );
    149                 }
    150 
    151             // This situation doesn't naturally occur so bounce to website root.
    152             } else {
    153                 bp_core_redirect( bp_get_root_domain() );
    154             }
    155         }
    156 
    157         // Finally, let's check the blog details, if the user wants a blog and blog creation is enabled.
    158         if ( isset( $_POST['signup_with_blog'] ) ) {
    159             $active_signup = bp_core_get_root_option( 'registration' );
    160 
    161             if ( 'blog' == $active_signup || 'all' == $active_signup ) {
    162                 $blog_details = bp_core_validate_blog_signup( $_POST['signup_blog_url'], $_POST['signup_blog_title'] );
    163 
    164                 // If there are errors with blog details, set them for display.
    165                 if ( !empty( $blog_details['errors']->errors['blogname'] ) )
    166                     $bp->signup->errors['signup_blog_url'] = $blog_details['errors']->errors['blogname'][0];
    167 
    168                 if ( !empty( $blog_details['errors']->errors['blog_title'] ) )
    169                     $bp->signup->errors['signup_blog_title'] = $blog_details['errors']->errors['blog_title'][0];
    170             }
    171         }
    172 
    173         /**
    174          * Fires after the validation of a new signup.
    175          *
    176          * @since 1.1.0
    177          */
    178         do_action( 'bp_signup_validate' );
    179 
    180         // Add any errors to the action for the field in the template for display.
    181         if ( !empty( $bp->signup->errors ) ) {
    182             foreach ( (array) $bp->signup->errors as $fieldname => $error_message ) {
    183                 /*
    184                  * The addslashes() and stripslashes() used to avoid create_function()
    185                  * syntax errors when the $error_message contains quotes.
    186                  */
    187 
    188                 /**
    189                  * Filters the error message in the loop.
    190                  *
    191                  * @since 1.5.0
    192                  *
    193                  * @param string $value Error message wrapped in html.
    194                  */
    195                 add_action( 'bp_' . $fieldname . '_errors', create_function( '', 'echo apply_filters(\'bp_members_signup_error_message\', "<div class=\"error\">" . stripslashes( \'' . addslashes( $error_message ) . '\' ) . "</div>" );' ) );
    196             }
    197         } else {
    198             $bp->signup->step = 'save-details';
    199 
    200             // No errors! Let's register those deets.
    201             $active_signup = bp_core_get_root_option( 'registration' );
    202 
    203             if ( 'none' != $active_signup ) {
    204 
    205                 // Make sure the extended profiles module is enabled.
    206                 if ( bp_is_active( 'xprofile' ) ) {
    207                     // Let's compact any profile field info into usermeta.
    208                     $profile_field_ids = explode( ',', $_POST['signup_profile_field_ids'] );
    209 
    210                     // Loop through the posted fields formatting any datebox values then add to usermeta - @todo This logic should be shared with the same in xprofile_screen_edit_profile().
    211                     foreach ( (array) $profile_field_ids as $field_id ) {
    212                         if ( ! isset( $_POST['field_' . $field_id] ) ) {
    213 
    214                             if ( ! empty( $_POST['field_' . $field_id . '_day'] ) && ! empty( $_POST['field_' . $field_id . '_month'] ) && ! empty( $_POST['field_' . $field_id . '_year'] ) ) {
    215                                 // Concatenate the values.
    216                                 $date_value = $_POST['field_' . $field_id . '_day'] . ' ' . $_POST['field_' . $field_id . '_month'] . ' ' . $_POST['field_' . $field_id . '_year'];
    217 
    218                                 // Turn the concatenated value into a timestamp.
    219                                 $_POST['field_' . $field_id] = date( 'Y-m-d H:i:s', strtotime( $date_value ) );
    220                             }
    221                         }
    222 
    223                         if ( !empty( $_POST['field_' . $field_id] ) )
    224                             $usermeta['field_' . $field_id] = $_POST['field_' . $field_id];
    225 
    226                         if ( !empty( $_POST['field_' . $field_id . '_visibility'] ) )
    227                             $usermeta['field_' . $field_id . '_visibility'] = $_POST['field_' . $field_id . '_visibility'];
    228                     }
    229 
    230                     // Store the profile field ID's in usermeta.
    231                     $usermeta['profile_field_ids'] = $_POST['signup_profile_field_ids'];
    232                 }
    233 
    234                 // Hash and store the password.
    235                 $usermeta['password'] = wp_hash_password( $_POST['signup_password'] );
    236 
    237                 // If the user decided to create a blog, save those details to usermeta.
    238                 if ( 'blog' == $active_signup || 'all' == $active_signup )
    239                     $usermeta['public'] = ( isset( $_POST['signup_blog_privacy'] ) && 'public' == $_POST['signup_blog_privacy'] ) ? true : false;
    240 
    241                 /**
    242                  * Filters the user meta used for signup.
    243                  *
    244                  * @since 1.1.0
    245                  *
    246                  * @param array $usermeta Array of user meta to add to signup.
    247                  */
    248                 $usermeta = apply_filters( 'bp_signup_usermeta', $usermeta );
    249 
    250                 // Finally, sign up the user and/or blog.
    251                 if ( isset( $_POST['signup_with_blog'] ) && is_multisite() )
    252                     $wp_user_id = bp_core_signup_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $_POST['signup_username'], $_POST['signup_email'], $usermeta );
    253                 else
    254                     $wp_user_id = bp_core_signup_user( $_POST['signup_username'], $_POST['signup_password'], $_POST['signup_email'], $usermeta );
    255 
    256                 if ( is_wp_error( $wp_user_id ) ) {
    257                     $bp->signup->step = 'request-details';
    258                     bp_core_add_message( $wp_user_id->get_error_message(), 'error' );
    259                 } else {
    260                     $bp->signup->step = 'completed-confirmation';
    261                 }
    262             }
    263 
    264             /**
    265              * Fires after the completion of a new signup.
    266              *
    267              * @since 1.1.0
    268              */
    269             do_action( 'bp_complete_signup' );
    270         }
    271 
    272     }
    273 
    274     /**
    275      * Fires right before the loading of the Member registration screen template file.
    276      *
    277      * @since 1.5.0
    278      */
    279     do_action( 'bp_core_screen_signup' );
    280 
    281     /**
    282      * Filters the template to load for the Member registration page screen.
    283      *
    284      * @since 1.5.0
    285      *
    286      * @param string $value Path to the Member registration template to load.
    287      */
    288     bp_core_load_template( apply_filters( 'bp_core_template_register', array( 'register', 'registration/register' ) ) );
    289 }
    290 add_action( 'bp_screens', 'bp_core_screen_signup' );
    291 
    292 /**
    293  * Handle the loading of the Activate screen.
    294  *
    295  * @todo Move the actual activation process into an action in bp-members-actions.php
    296  */
    297 function bp_core_screen_activation() {
    298 
    299     // Bail if not viewing the activation page.
    300     if ( ! bp_is_current_component( 'activate' ) ) {
    301         return false;
    302     }
    303 
    304     // If the user is already logged in, redirect away from here.
    305     if ( is_user_logged_in() ) {
    306 
    307         // If activation page is also front page, set to members directory to
    308         // avoid an infinite loop. Otherwise, set to root domain.
    309         $redirect_to = bp_is_component_front_page( 'activate' )
    310             ? bp_get_members_directory_permalink()
    311             : bp_get_root_domain();
    312 
    313         // Trailing slash it, as we expect these URL's to be.
    314         $redirect_to = trailingslashit( $redirect_to );
    315 
    316         /**
    317          * Filters the URL to redirect logged in users to when visiting activation page.
    318          *
    319          * @since 1.9.0
    320          *
    321          * @param string $redirect_to URL to redirect user to.
    322          */
    323         $redirect_to = apply_filters( 'bp_loggedin_activate_page_redirect_to', $redirect_to );
    324 
    325         // Redirect away from the activation page.
    326         bp_core_redirect( $redirect_to );
    327     }
    328 
    329     // Grab the key (the old way).
    330     $key = isset( $_GET['key'] ) ? $_GET['key'] : '';
    331 
    332     // Grab the key (the new way).
    333     if ( empty( $key ) ) {
    334         $key = bp_current_action();
    335     }
    336 
    337     // Get BuddyPress.
    338     $bp = buddypress();
    339 
    340     // We've got a key; let's attempt to activate the signup.
    341     if ( ! empty( $key ) ) {
    342 
    343         /**
    344          * Filters the activation signup.
    345          *
    346          * @since 1.1.0
    347          *
    348          * @param bool|int $value Value returned by activation.
    349          *                        Integer on success, boolean on failure.
    350          */
    351         $user = apply_filters( 'bp_core_activate_account', bp_core_activate_signup( $key ) );
    352 
    353         // If there were errors, add a message and redirect.
    354         if ( ! empty( $user->errors ) ) {
    355             bp_core_add_message( $user->get_error_message(), 'error' );
    356             bp_core_redirect( trailingslashit( bp_get_root_domain() . '/' . $bp->pages->activate->slug ) );
    357         }
    358 
    359         $hashed_key = wp_hash( $key );
    360 
    361         // Check if the signup avatar folder exists. If it does, move the folder to
    362         // the BP user avatars directory.
    363         if ( file_exists( bp_core_avatar_upload_path() . '/avatars/signups/' . $hashed_key ) ) {
    364             @rename( bp_core_avatar_upload_path() . '/avatars/signups/' . $hashed_key, bp_core_avatar_upload_path() . '/avatars/' . $user );
    365         }
    366 
    367         bp_core_add_message( __( 'Your account is now active!', 'buddypress' ) );
    368         $bp->activation_complete = true;
    369     }
    370 
    371     /**
    372      * Filters the template to load for the Member activation page screen.
    373      *
    374      * @since 1.1.1
    375      *
    376      * @param string $value Path to the Member activation template to load.
    377      */
    378     bp_core_load_template( apply_filters( 'bp_core_template_activate', array( 'activate', 'registration/activate' ) ) );
    379 }
    380 add_action( 'bp_screens', 'bp_core_screen_activation' );
    381 
    382 /** Theme Compatibility *******************************************************/
    38312
    38413/**
     
    585214    }
    586215}
    587 new BP_Members_Theme_Compat();
    588 
    589 /**
    590  * The main theme compat class for BuddyPress Registration.
    591  *
    592  * This class sets up the necessary theme compatibility actions to safely output
    593  * registration template parts to the_title and the_content areas of a theme.
    594  *
    595  * @since 1.7.0
    596  */
    597 class BP_Registration_Theme_Compat {
    598 
    599     /**
    600      * Setup the groups component theme compatibility.
    601      *
    602      * @since 1.7.0
    603      */
    604     public function __construct() {
    605         add_action( 'bp_setup_theme_compat', array( $this, 'is_registration' ) );
    606     }
    607 
    608     /**
    609      * Are we looking at either the registration or activation pages?
    610      *
    611      * @since 1.7.0
    612      */
    613     public function is_registration() {
    614 
    615         // Bail if not looking at the registration or activation page.
    616         if ( ! bp_is_register_page() && ! bp_is_activation_page() ) {
    617             return;
    618         }
    619 
    620         // Not a directory.
    621         bp_update_is_directory( false, 'register' );
    622 
    623         // Setup actions.
    624         add_filter( 'bp_get_buddypress_template',                array( $this, 'template_hierarchy' ) );
    625         add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'dummy_post'    ) );
    626         add_filter( 'bp_replace_the_content',                    array( $this, 'dummy_content' ) );
    627     }
    628 
    629     /** Template ***********************************************************/
    630 
    631     /**
    632      * Add template hierarchy to theme compat for registration/activation pages.
    633      *
    634      * This is to mirror how WordPress has
    635      * {@link https://codex.wordpress.org/Template_Hierarchy template hierarchy}.
    636      *
    637      * @since 1.8.0
    638      *
    639      * @param string $templates The templates from bp_get_theme_compat_templates().
    640      * @return array $templates Array of custom templates to look for.
    641      */
    642     public function template_hierarchy( $templates ) {
    643         $component = sanitize_file_name( bp_current_component() );
    644 
    645         /**
    646          * Filters the template hierarchy for theme compat and registration/activation pages.
    647          *
    648          * This filter is a variable filter that depends on the current component
    649          * being used.
    650          *
    651          * @since 1.8.0
    652          *
    653          * @param array $value Array of template paths to add to hierarchy.
    654          */
    655         $new_templates = apply_filters( "bp_template_hierarchy_{$component}", array(
    656             "members/index-{$component}.php"
    657         ) );
    658 
    659         // Merge new templates with existing stack
    660         // @see bp_get_theme_compat_templates().
    661         $templates = array_merge( (array) $new_templates, $templates );
    662 
    663         return $templates;
    664     }
    665 
    666     /**
    667      * Update the global $post with dummy data.
    668      *
    669      * @since 1.7.0
    670      */
    671     public function dummy_post() {
    672         // Registration page.
    673         if ( bp_is_register_page() ) {
    674             $title = __( 'Create an Account', 'buddypress' );
    675 
    676             if ( 'completed-confirmation' == bp_get_current_signup_step() ) {
    677                 $title = __( 'Check Your Email To Activate Your Account!', 'buddypress' );
    678             }
    679 
    680         // Activation page.
    681         } else {
    682             $title = __( 'Activate Your Account', 'buddypress' );
    683 
    684             if ( bp_account_was_activated() ) {
    685                 $title = __( 'Account Activated', 'buddypress' );
    686             }
    687         }
    688 
    689         bp_theme_compat_reset_post( array(
    690             'ID'             => 0,
    691             'post_title'     => $title,
    692             'post_author'    => 0,
    693             'post_date'      => 0,
    694             'post_content'   => '',
    695             'post_type'      => 'page',
    696             'post_status'    => 'publish',
    697             'is_page'        => true,
    698             'comment_status' => 'closed'
    699         ) );
    700     }
    701 
    702     /**
    703      * Filter the_content with either the register or activate templates.
    704      *
    705      * @since 1.7.0
    706      */
    707     public function dummy_content() {
    708         if ( bp_is_register_page() ) {
    709             return bp_buffer_template_part( 'members/register', null, false );
    710         } else {
    711             return bp_buffer_template_part( 'members/activate', null, false );
    712         }
    713     }
    714 }
    715 new BP_Registration_Theme_Compat();
Note: See TracChangeset for help on using the changeset viewer.