Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/25/2020 03:13:24 AM (5 years ago)
Author:
imath
Message:

Blogs: renovate the create a blog form

This blog form let logged in users create sites. It aged badly since version 1.0 and we were unnecessarily resetting the blog domain for subdomain installs though wpmu_validate_blog_signup() is already generating this domain.

Fixes #8365

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-template.php

    r12636 r12734  
    10031003
    10041004/**
    1005  * Checks whether blog creation is enabled.
    1006  *
    1007  * Returns true when blog creation is enabled for logged-in users only, or
    1008  * when it's enabled for new registrations.
    1009  *
    1010  * @return bool True if blog registration is enabled.
    1011  */
    1012 function bp_blog_signup_enabled() {
    1013     $bp = buddypress();
    1014 
    1015     $active_signup = isset( $bp->site_options['registration'] )
    1016         ? $bp->site_options['registration']
    1017         : 'all';
    1018 
    1019     /**
    1020      * Filters whether or not blog creation is enabled.
    1021      *
    1022      * Return "all", "none", "blog" or "user".
    1023      *
    1024      * @since 1.0.0
    1025      *
    1026      * @param string $active_signup Value of the registration site option creation status.
    1027      */
    1028     $active_signup = apply_filters( 'wpmu_active_signup', $active_signup );
    1029 
    1030     if ( 'none' == $active_signup || 'user' == $active_signup )
    1031         return false;
    1032 
    1033     return true;
    1034 }
    1035 
    1036 /**
    10371005 * Output the wrapper markup for the blog signup form.
     1006 *
     1007 * @since 1.0.0
    10381008 *
    10391009 * @param string          $blogname   Optional. The default blog name (path or domain).
     
    10421012 *                                    submission attempt.
    10431013 */
    1044 function bp_show_blog_signup_form($blogname = '', $blog_title = '', $errors = '') {
    1045     global $current_user;
    1046 
    1047     if ( isset($_POST['submit']) ) {
    1048         bp_blogs_validate_blog_signup();
    1049     } else {
    1050         if ( ! is_wp_error($errors) ) {
     1014function bp_show_blog_signup_form( $blogname = '', $blog_title = '', $errors = '' ) {
     1015    $blog_id = bp_blogs_validate_blog_signup();
     1016
     1017    // Display the signup form.
     1018    if ( false === $blog_id || is_wp_error( $blog_id ) ) {
     1019        if ( is_wp_error( $blog_id ) ) {
     1020            $errors = $blog_id;
     1021        } else {
    10511022            $errors = new WP_Error();
    10521023        }
     
    10641035         */
    10651036        $filtered_results = apply_filters('signup_another_blog_init', array('blogname' => $blogname, 'blog_title' => $blog_title, 'errors' => $errors ));
    1066         $blogname = $filtered_results['blogname'];
    1067         $blog_title = $filtered_results['blog_title'];
    1068         $errors = $filtered_results['errors'];
     1037        $blogname         = $filtered_results['blogname'];
     1038        $blog_title       = $filtered_results['blog_title'];
     1039        $errors           = $filtered_results['errors'];
    10691040
    10701041        if ( $errors->get_error_code() ) {
    1071             echo "<p>" . __('There was a problem; please correct the form below and try again.', 'buddypress') . "</p>";
    1072         }
     1042            if ( in_array( $errors->get_error_code(), array( 'blogname', 'blog_title' ), true ) ) {
     1043                printf(
     1044                    '<p class="error">%s</p>',
     1045                    esc_html__( 'There was a problem; please correct the form below and try again.', 'buddypress' )
     1046                );
     1047            } else {
     1048                printf(
     1049                    '<p class="error">%s</p>',
     1050                    $errors->get_error_message()
     1051                );
     1052            }
     1053        }
     1054
     1055        printf(
     1056            '<p>%1$s <strong>%2$s</strong>. %3$s</p>',
     1057            esc_html__( 'By filling out the form below, you can', 'buddypress' ),
     1058            esc_html__( 'add a site to your account', 'buddypress' ),
     1059            esc_html__( 'There is no limit to the number of sites that you can have, so create to your heart’s content, but blog responsibly!', 'buddypress' )
     1060        );
    10731061        ?>
    1074         <p><?php printf(__("By filling out the form below, you can <strong>add a site to your account</strong>. There is no limit to the number of sites that you can have, so create to your heart's content, but blog responsibly!", 'buddypress'), $current_user->display_name) ?></p>
    1075 
    1076         <p><?php _e("If you&#8217;re not going to use a great domain, leave it for a new user. Now have at it!", 'buddypress') ?></p>
     1062
     1063        <p>
     1064            <?php esc_html_e( 'If you’re not going to use a great domain, leave it for a new user. Now have at it!', 'buddypress' ); ?>
     1065        </p>
    10771066
    10781067        <form class="standard-form" id="setupform" method="post" action="">
     
    10881077            do_action( 'signup_hidden_fields' ); ?>
    10891078
    1090             <?php bp_blogs_signup_blog($blogname, $blog_title, $errors); ?>
     1079            <?php bp_blogs_signup_blog( $blogname, $blog_title, $errors ); ?>
    10911080            <p>
    1092                 <input id="submit" type="submit" name="submit" class="submit" value="<?php esc_attr_e('Create Site', 'buddypress') ?>" />
     1081                <input id="submit" type="submit" name="submit" class="submit" value="<?php esc_attr_e( 'Create Site', 'buddypress' ); ?>" />
    10931082            </p>
    10941083
     
    10961085        </form>
    10971086        <?php
     1087
     1088        // Display the confirmation form.
     1089    } elseif ( is_numeric( $blog_id ) ) {
     1090        // Validate the site.
     1091        $site = get_site( $blog_id );
     1092
     1093        if ( isset( $site->id ) && $site->id ) {
     1094            $current_user = wp_get_current_user();
     1095
     1096            bp_blogs_confirm_blog_signup(
     1097                $site->domain,
     1098                $site->path,
     1099                $site->blogname,
     1100                $current_user->user_login,
     1101                $current_user->user_email,
     1102                '',
     1103                $site->id
     1104            );
     1105        }
    10981106    }
    10991107}
     
    11011109/**
    11021110 * Output the input fields for the blog creation form.
     1111 *
     1112 * @since 1.0.0
    11031113 *
    11041114 * @param string          $blogname   Optional. The default blog name (path or domain).
     
    11081118 */
    11091119function bp_blogs_signup_blog( $blogname = '', $blog_title = '', $errors = '' ) {
    1110     global $current_site;
    1111 
    1112     // Blog name.
    1113     if( !is_subdomain_install() )
    1114         echo '<label for="blogname">' . __('Site Name:', 'buddypress') . '</label>';
    1115     else
    1116         echo '<label for="blogname">' . __('Site Domain:', 'buddypress') . '</label>';
    1117 
    1118     if ( $errmsg = $errors->get_error_message('blogname') ) { ?>
    1119 
    1120         <p class="error"><?php echo $errmsg ?></p>
    1121 
    1122     <?php }
    1123 
    1124     if ( !is_subdomain_install() )
    1125         echo '<span class="prefix_address">' . $current_site->domain . $current_site->path . '</span> <input name="blogname" type="text" id="blogname" value="'.$blogname.'" maxlength="63" /><br />';
    1126     else
    1127         echo '<input name="blogname" type="text" id="blogname" value="'.$blogname.'" maxlength="63" ' . bp_get_form_field_attributes( 'blogname' ) . '/> <span class="suffix_address">.' . bp_signup_get_subdomain_base() . '</span><br />';
    1128 
    1129     if ( !is_user_logged_in() ) {
    1130         print '(<strong>' . __( 'Your address will be ' , 'buddypress');
    1131 
    1132         if ( !is_subdomain_install() ) {
    1133             print $current_site->domain . $current_site->path . __( 'blogname' , 'buddypress');
     1120    $current_site = get_current_site();
     1121
     1122    if ( ! $blogname && ! $blog_title ) {
     1123        $submitted_vars = bp_blogs_get_signup_form_submitted_vars();
     1124
     1125        if ( array_filter( $submitted_vars ) ) {
     1126            $blogname   = $submitted_vars['blogname'];
     1127            $blog_title = $submitted_vars['blog_title'];
     1128        }
     1129    }
     1130    ?>
     1131
     1132    <p>
     1133        <?php
     1134        // Blog name.
     1135        if ( ! is_subdomain_install() ) {
     1136            printf( '<label for="blogname">%s</label>', esc_html__( 'Site Name:', 'buddypress' ) );
    11341137        } else {
    1135             print __( 'domain.' , 'buddypress') . $current_site->domain . $current_site->path;
    1136         }
    1137 
    1138         echo '.</strong> ' . __( 'Must be at least 4 characters, letters and numbers only. It cannot be changed so choose carefully!)' , 'buddypress') . '</p>';
     1138            printf( '<label for="blogname">%s</label>', esc_html__( 'Site Domain:', 'buddypress' ) );
     1139        }
     1140
     1141        if ( ! is_subdomain_install() ) {
     1142            printf(
     1143                '<span class="prefix_address">%1$s</span> <input name="blogname" type="text" id="blogname" value="%2$s" maxlength="63" style="width: auto!important" /><br />',
     1144                esc_html( $current_site->domain . $current_site->path ),
     1145                esc_attr( $blogname )
     1146            );
     1147        } else {
     1148            printf(
     1149                '<input name="blogname" type="text" id="blogname" value="%1$s" maxlength="63" style="width: auto!important" %2$s/> <span class="suffix_address">.%3$s</span><br />',
     1150                esc_attr( $blogname ),
     1151                bp_get_form_field_attributes( 'blogname' ),
     1152                bp_signup_get_subdomain_base()
     1153            );
     1154        }
     1155        if ( is_wp_error( $errors ) && $errors->get_error_message( 'blogname' ) ) {
     1156            printf( '<div class="error">%s</div>', $errors->get_error_message( 'blogname' ) );
     1157        }
     1158        ?>
     1159    </p>
     1160
     1161    <?php
     1162    if ( ! is_user_logged_in() ) {
     1163        $url = sprintf(
     1164            /* translators: %s is the site domain and path. */
     1165            __( 'domain.%s' , 'buddypress' ),
     1166            $current_site->domain . $current_site->path
     1167        );
     1168
     1169        if ( ! is_subdomain_install() ) {
     1170            $url = sprintf(
     1171                /* translators: %s is the site domain and path. */
     1172                __( '%sblogname' , 'buddypress'),
     1173                $current_site->domain . $current_site->path
     1174            );
     1175        }
     1176
     1177        printf(
     1178            '<p>(<strong>%1$s.</strong> %2$s)</p>',
     1179            sprintf( esc_html__( 'Your address will be %s' , 'buddypress' ), $url ),
     1180            esc_html__( 'Must be at least 4 characters, letters and numbers only. It cannot be changed so choose carefully!' , 'buddypress' )
     1181        );
    11391182    }
    11401183
    11411184    // Blog Title.
    11421185    ?>
    1143 
    1144     <label for="blog_title"><?php _e('Site Title:', 'buddypress') ?></label>
    1145 
    1146     <?php if ( $errmsg = $errors->get_error_message('blog_title') ) { ?>
    1147 
    1148         <p class="error"><?php echo $errmsg ?></p>
    1149 
    1150     <?php }
    1151     echo '<input name="blog_title" type="text" id="blog_title" value="'.esc_html($blog_title, 1).'" /></p>';
    1152     ?>
     1186    <p>
     1187        <label for="blog_title"><?php esc_html_e('Site Title:', 'buddypress') ?></label>
     1188        <input name="blog_title" type="text" id="blog_title" value="<?php echo esc_html( $blog_title ); ?>" />
     1189
     1190        <?php
     1191        if ( is_wp_error( $errors ) && $errors->get_error_message( 'blog_title' ) ) {
     1192            printf( '<div class="error">%s</div>', $errors->get_error_message( 'blog_title' ) );
     1193        }
     1194        ?>
     1195    </p>
    11531196
    11541197    <fieldset class="create-site">
    1155         <legend class="label"><?php _e('Privacy: I would like my site to appear in search engines, and in public listings around this network', 'buddypress') ?></legend>
    1156 
    1157         <label class="checkbox" for="blog_public_on">
    1158             <input type="radio" id="blog_public_on" name="blog_public" value="1" <?php if( !isset( $_POST['blog_public'] ) || '1' == $_POST['blog_public'] ) { ?>checked="checked"<?php } ?> />
    1159             <strong><?php _e( 'Yes' , 'buddypress'); ?></strong>
    1160         </label>
    1161         <label class="checkbox" for="blog_public_off">
    1162             <input type="radio" id="blog_public_off" name="blog_public" value="0" <?php if( isset( $_POST['blog_public'] ) && '0' == $_POST['blog_public'] ) { ?>checked="checked"<?php } ?> />
    1163             <strong><?php _e( 'No' , 'buddypress'); ?></strong>
    1164         </label>
     1198
     1199        <legend class="label"><?php esc_html_e( 'Privacy: I would like my site to appear in search engines, and in public listings around this network', 'buddypress' ) ?></legend>
     1200
     1201        <p>
     1202            <label class="checkbox" for="blog_public_on">
     1203                <input type="radio" id="blog_public_on" name="blog_public" value="1" <?php checked( ! isset( $_POST['blog_public'] ) || 1 === (int) $_POST['blog_public'] ); ?> />
     1204                <strong><?php esc_html_e( 'Yes' , 'buddypress'); ?></strong>
     1205            </label>
     1206        </p>
     1207
     1208        <p>
     1209            <label class="checkbox" for="blog_public_off">
     1210                <input type="radio" id="blog_public_off" name="blog_public" value="0" <?php checked( isset( $_POST['blog_public'] ) && 0 === (int) $_POST['blog_public'] ); ?> />
     1211                <strong><?php esc_html_e( 'No' , 'buddypress'); ?></strong>
     1212            </label>
     1213        </p>
     1214
    11651215    </fieldset>
    11661216
     
    11741224     * @param WP_Error $errors WP_Error object if any present.
    11751225     */
    1176     do_action('signup_blogform', $errors);
     1226    do_action( 'signup_blogform', $errors );
    11771227}
    11781228
     
    11821232 * Passes submitted values to {@link wpmu_create_blog()}.
    11831233 *
    1184  * @return bool True on success, false on failure.
     1234 * @since 1.0.0
     1235 *
     1236 * @return bool|int|WP_Error False if not a form submission, the Blog ID on success, a WP_Error object on failure.
    11851237 */
    11861238function bp_blogs_validate_blog_signup() {
    1187     global $wpdb, $current_user, $blogname, $blog_title, $errors, $domain, $path, $current_site;
    1188 
    1189     if ( !check_admin_referer( 'bp_blog_signup_form' ) )
     1239    if ( ! isset( $_POST['submit'] ) ) {
    11901240        return false;
    1191 
     1241    }
     1242
     1243    $current_site = get_current_site();
    11921244    $current_user = wp_get_current_user();
    1193 
    1194     if( !is_user_logged_in() )
    1195         die();
    1196 
    1197     $result = bp_blogs_validate_blog_form();
    1198     extract($result);
    1199 
    1200     if ( $errors->get_error_code() ) {
    1201         unset($_POST['submit']);
    1202         bp_show_blog_signup_form( $blogname, $blog_title, $errors );
    1203         return false;
    1204     }
    1205 
    1206     $public = (int) $_POST['blog_public'];
    1207 
    1208     // Depreciated.
    1209     $meta = apply_filters( 'signup_create_blog_meta', array( 'lang_id' => 1, 'public' => $public ) );
     1245    $blog_name    = '';
     1246    $blog_title   = '';
     1247    $public       = 1;
     1248
     1249    if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['_wpnonce'] ), 'bp_blog_signup_form' ) || ! $current_user->ID ) {
     1250        return new WP_Error( 'bp_blogs_doing_it_wrong', __( 'Sorry, we cannot create the site. Please try again later.', 'buddypress' ) );
     1251    }
     1252
     1253    $submitted_vars = bp_blogs_get_signup_form_submitted_vars();
     1254
     1255    if ( array_filter( $submitted_vars ) ) {
     1256        $blog_name  = $submitted_vars['blogname'];
     1257        $blog_title = $submitted_vars['blog_title'];
     1258        $public     = (int) $submitted_vars['blog_public'];
     1259    }
     1260
     1261    $blog = bp_blogs_validate_blog_form( $blog_name, $blog_title );
     1262
     1263    if ( is_wp_error( $blog['errors'] ) && $blog['errors']->get_error_code() ) {
     1264        return $blog['errors'];
     1265    }
    12101266
    12111267    /**
     
    12191275     * }
    12201276     */
    1221     $meta = apply_filters( 'add_signup_meta', $meta );
    1222 
    1223     // If this is a subdomain install, set up the site inside the root domain.
    1224     if ( is_subdomain_install() )
    1225         $domain = $blogname . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
    1226 
    1227     $blog_id = wpmu_create_blog( $domain, $path, $blog_title, $current_user->ID, $meta, $wpdb->siteid );
    1228     bp_blogs_confirm_blog_signup( $domain, $path, $blog_title, $current_user->user_login, $current_user->user_email, $meta, $blog_id );
    1229     return true;
    1230 }
    1231 
    1232 /**
    1233  * Validate a blog creation submission.
    1234  *
    1235  * Essentially, a wrapper for {@link wpmu_validate_blog_signup()}.
    1236  *
    1237  * @return array Contains the new site data and error messages.
    1238  */
    1239 function bp_blogs_validate_blog_form() {
    1240     $user = '';
    1241     if ( is_user_logged_in() )
    1242         $user = wp_get_current_user();
    1243 
    1244     return wpmu_validate_blog_signup($_POST['blogname'], $_POST['blog_title'], $user);
     1277    $meta = apply_filters( 'add_signup_meta', array( 'lang_id' => 1, 'public' => $public ) );
     1278
     1279    return wpmu_create_blog(
     1280        $blog['domain'],
     1281        $blog['path'],
     1282        $blog['blog_title'],
     1283        $current_user->ID, $meta,
     1284        $current_site->id
     1285    );
    12451286}
    12461287
     
    12481289 * Display a message after successful blog registration.
    12491290 *
     1291 * @since 1.0.0
    12501292 * @since 2.6.0 Introduced `$blog_id` parameter.
    12511293 *
     
    12651307
    12661308    ?>
    1267     <p><?php _e( 'Congratulations! You have successfully registered a new site.', 'buddypress' ) ?></p>
     1309    <p class="success"><?php esc_html_e( 'Congratulations! You have successfully registered a new site.', 'buddypress' ) ?></p>
    12681310    <p>
    12691311        <?php printf(
     
    12901332     * @since 1.0.0
    12911333     */
    1292     do_action('signup_finished');
     1334    do_action( 'signup_finished' );
    12931335}
    12941336
Note: See TracChangeset for help on using the changeset viewer.