| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* handle field output */ |
| 5 |
function wppb_blog_details_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){ |
| 6 |
|
| 7 |
// Display "Yes, I'd like to create a new site" checkbox if we're on the PB Register form and we are on Multisite with Network setting "Both sites and user accounts can be registered". |
| 8 |
if ( ( $form_location != 'register' ) || ( ! wppb_can_users_signup_blog() ) ){ |
| 9 |
return $output; |
| 10 |
} |
| 11 |
|
| 12 |
//Check if Blog Details field is added in Manage Fields |
| 13 |
$in_manage_fields = false; |
| 14 |
$wppb_manage_fields = get_option( 'wppb_manage_fields', 'not_set' ); |
| 15 |
if ( $wppb_manage_fields != 'not_set' ){ |
| 16 |
foreach ( $wppb_manage_fields as $field ) { |
| 17 |
if ( $field['field'] == 'Default - Blog Details' ) { |
| 18 |
$in_manage_fields = true; |
| 19 |
break; |
| 20 |
} |
| 21 |
} |
| 22 |
} |
| 23 |
if ( ! $in_manage_fields ) { |
| 24 |
return $output; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
$output = '<ul>'; |
| 29 |
|
| 30 |
$item_description = wppb_icl_t( 'plugin profile-builder-pro', 'default_field_'.$field['id'].'_description_translation', $field['description'], true ); |
| 31 |
$heading = '<li class="wppb-form-field wppb-blog-details-heading"><h4>'.wppb_icl_t('plugin profile-builder-pro', 'default_field_'.$field['id'].'_title_translation', $field['field-title'], true).'</h4><span class="wppb-description-delimiter">'.$item_description.'</span></li>'; |
| 32 |
$output .= apply_filters( 'wppb_blog_details_heading', $heading ); |
| 33 |
|
| 34 |
|
| 35 |
?><script type="text/javascript"> |
| 36 |
jQuery(document).ready(function(){ |
| 37 |
|
| 38 |
wppb_toggle_required_attrbute_for_blog_details(); |
| 39 |
jQuery("#wppb_create_new_site_checkbox").on("click",function(){ |
| 40 |
|
| 41 |
wppb_toggle_required_attrbute_for_blog_details(); |
| 42 |
jQuery(".wppb-blog-details-fields").toggle(); |
| 43 |
}); |
| 44 |
function wppb_toggle_required_attrbute_for_blog_details(){ |
| 45 |
|
| 46 |
// Trigger a custom event that will remove the HTML attribute -required- for hidden fields. This is necessary for browsers to allow form submission. |
| 47 |
if(document.getElementById('wppb_create_new_site_checkbox').checked) { |
| 48 |
jQuery(".wppb-blog-details-fields input" ).trigger( "wppbAddRequiredAttributeEvent" ); |
| 49 |
} else { |
| 50 |
jQuery(".wppb-blog-details-fields input" ).trigger( "wppbRemoveRequiredAttributeEvent" ); |
| 51 |
} |
| 52 |
} |
| 53 |
}); |
| 54 |
</script> <?php |
| 55 |
$checked = ''; |
| 56 |
if ( isset( $request_data['wppb_create_new_site_checkbox']) && ( $request_data['wppb_create_new_site_checkbox'] == 'yes') ) { |
| 57 |
$checked = 'checked'; |
| 58 |
}else{ |
| 59 |
echo '<style> .wppb-blog-details-fields {display:none;} </style>'; |
| 60 |
} |
| 61 |
$create_new_site_checkbox = ' |
| 62 |
<li class=" wppb-form-field wppb-create-new-site " id="wppb-create-new-site"> |
| 63 |
<label for="wppb_create_new_site_checkbox"> |
| 64 |
<input id="wppb_create_new_site_checkbox" type="checkbox" name="wppb_create_new_site_checkbox" value="yes" '.$checked.' autocomplete="off"> |
| 65 |
<strong>'. __('Yes, I\'d like to create a new site','profile-builder').'</strong> </label> |
| 66 |
</li>'; |
| 67 |
$output .= apply_filters( 'wppb_blog_details_checkbox', $create_new_site_checkbox ); |
| 68 |
|
| 69 |
$output .= '<ul class="wppb-blog-details-fields">'; |
| 70 |
|
| 71 |
// Site URL |
| 72 |
$item_description = __( 'Your site url will look like this:<br>', 'profile-builder' ); |
| 73 |
if ( is_subdomain_install() ) { |
| 74 |
global $current_site; |
| 75 |
$subdomain_base = apply_filters( 'wppb_blogs_subdomain_base', preg_replace( '|^www\.|', '', $current_site->domain ) . $current_site->path ); |
| 76 |
$domain = '"http://'. esc_attr( '<your-slug>.' ) . $subdomain_base; |
| 77 |
} else { |
| 78 |
$domain = '"' . esc_url( home_url( '/' ) ) . esc_attr( '<your-slug>' ) . '"'; |
| 79 |
} |
| 80 |
$blog_url_input_value = ''; |
| 81 |
$blog_url_input_value = ( isset( $request_data['wppb_blog_url'] ) ? trim( $request_data['wppb_blog_url'] ) : $blog_url_input_value ); |
| 82 |
$error_mark = '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>'; |
| 83 |
|
| 84 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 85 |
|
| 86 |
$error_class = ''; |
| 87 |
$is_error = wppb_check_individual_blog_fields( 'wppb_blog_url', $request_data, $form_location ); |
| 88 |
if ($is_error != '') { |
| 89 |
$error_mark = '<img src="' . WPPB_PLUGIN_URL . 'assets/images/pencil_delete.png" title="' . wppb_required_field_error('') . '"/>'; |
| 90 |
$error_class = ' wppb-field-error'; |
| 91 |
} |
| 92 |
|
| 93 |
$output .= ' |
| 94 |
<li class=" wppb-form-field wppb-blog-url ' . $error_class . '"> |
| 95 |
<label for="blog-url">' . __( 'Site URL slug', 'profile-builder' ) . $error_mark.'</label> |
| 96 |
<input class="text-input default_field_blog_url" name="wppb_blog_url" maxlength="'. apply_filters( 'wppb_maximum_character_length', 70, $field ) .'" type="text" id="wppb_blog_url" value="'. esc_attr( wp_unslash( $blog_url_input_value ) ) .'" '. $extra_attr .' />'; |
| 97 |
$output .= '<span class="wppb-description-delimiter">'. $item_description . $domain . '</span>'; |
| 98 |
$output .= $is_error .'</li>'; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
// Site title |
| 103 |
$blog_title_input_value = ''; |
| 104 |
$blog_title_input_value = ( isset( $request_data['wppb_blog_title'] ) ? trim( $request_data['wppb_blog_title'] ) : $blog_title_input_value ); |
| 105 |
$error_mark = '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>'; |
| 106 |
|
| 107 |
$extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location ); |
| 108 |
|
| 109 |
$error_class = ''; |
| 110 |
$is_error = wppb_check_individual_blog_fields( 'wppb_blog_title', $request_data, $form_location ); |
| 111 |
if ($is_error != '') { |
| 112 |
$error_mark = '<img src="' . WPPB_PLUGIN_URL . 'assets/images/pencil_delete.png" title="' . wppb_required_field_error('') . '"/>'; |
| 113 |
$error_class = ' wppb-field-error'; |
| 114 |
} |
| 115 |
|
| 116 |
$output .= ' |
| 117 |
<li class=" wppb-form-field wppb-blog-title ' . $error_class . '"> |
| 118 |
<label for="blog-title">' . __( 'Site Title', 'profile-builder' ) . $error_mark.'</label> |
| 119 |
<input class="text-input default_field_blog_title" name="wppb_blog_title" maxlength="'. apply_filters( 'wppb_maximum_character_length', 70, $field ) .'" type="text" id="wppb_blog_title" value="'. esc_attr( wp_unslash( $blog_title_input_value ) ) .'" '. $extra_attr .' />' . |
| 120 |
$is_error . '</li>'; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
// Privacy |
| 125 |
$blog_privacy_input_value = 'Yes'; |
| 126 |
$blog_privacy_input_value = ( isset( $request_data['wppb_blog_privacy'] ) ? trim( $request_data['wppb_blog_privacy'] ) : $blog_privacy_input_value ); |
| 127 |
$error_mark = '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>'; |
| 128 |
|
| 129 |
$radio_values = array( 'Yes', 'No' ); |
| 130 |
|
| 131 |
$error_class = ''; |
| 132 |
$is_error = wppb_check_individual_blog_fields( 'wppb_blog_privacy', $request_data, $form_location ); |
| 133 |
if ($is_error != '') { |
| 134 |
$error_mark = '<img src="' . WPPB_PLUGIN_URL . 'assets/images/pencil_delete.png" title="' . wppb_required_field_error('') . '"/>'; |
| 135 |
$error_class = ' wppb-field-error'; |
| 136 |
} |
| 137 |
|
| 138 |
$output .= ' |
| 139 |
<li class=" wppb-form-field wppb-blog-privacy ' . $error_class . ' "> |
| 140 |
<label for="blog-privacy">'. __( 'Privacy: I would like my site to appear in search engines, and in public listings around this network.', 'profile-builder' ) . $error_mark.'</label>'; |
| 141 |
$output .= '<ul class="wppb-radios">'; |
| 142 |
foreach( $radio_values as $key => $value){ |
| 143 |
$output .= '<li><input value="'.esc_attr( trim( $value ) ).'" class="blog_privacy_radio '. apply_filters( 'wppb_fields_extra_css_class', '', $field ) .'" id="'.Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $value ) ).'_'.$field['id'].'" name="wppb_blog_privacy" type="radio" '. $extra_attr .' '; |
| 144 |
|
| 145 |
if ( $blog_privacy_input_value === trim( $value ) ) |
| 146 |
$output .= ' checked'; |
| 147 |
|
| 148 |
$output .= ' /><label for="'.Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $value ) ).'_'.$field['id'].'" class="wppb-rc-value">'. trim( $radio_values[$key] ) .'</label></li>'; |
| 149 |
} |
| 150 |
$output .= '</ul>' . $is_error . '</li>'; |
| 151 |
|
| 152 |
// end wppb-blog-details-fields |
| 153 |
$output .= '</ul>'; |
| 154 |
|
| 155 |
$output .= '</ul>'; |
| 156 |
|
| 157 |
return apply_filters( 'wppb_blog_details_output', $output, $form_location, $field, $user_id, $field_check_errors, $request_data ); |
| 158 |
|
| 159 |
} |
| 160 |
add_filter( 'wppb_output_form_field_default-blog-details', 'wppb_blog_details_handler', 10, 6 ); |
| 161 |
|
| 162 |
/* handle field save */ |
| 163 |
function wppb_create_blog_on_registration( $field, $user_id, $request_data, $form_location ){ |
| 164 |
if( $form_location == 'register' && $field['field'] == 'Default - Blog Details' && isset( $request_data['wppb_create_new_site_checkbox'] ) && $request_data['wppb_create_new_site_checkbox'] == 'yes' ) { |
| 165 |
$blog_url = $request_data['wppb_blog_url']; |
| 166 |
$blog_title = $request_data['wppb_blog_title']; |
| 167 |
|
| 168 |
$usermeta['public'] = ( isset( $request_data['wppb_blog_privacy'] ) && 'Yes' == $request_data['wppb_blog_privacy'] ) ? true : false; |
| 169 |
$blog_details = wpmu_validate_blog_signup( $blog_url, $blog_title ); |
| 170 |
if ( empty($blog_details['errors']->errors['blogname']) && empty($blog_details['errors']->errors['blog_title'])) { |
| 171 |
wpmu_create_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $user_id, $usermeta ); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
add_action( 'wppb_save_form_field', 'wppb_create_blog_on_registration', 10, 4 ); |
| 176 |
|
| 177 |
/* handle field validation */ |
| 178 |
function wppb_check_blog_details_values( $message, $field, $request_data, $form_location ){ |
| 179 |
if ( isset( $request_data['wppb_create_new_site_checkbox'] ) && $request_data['wppb_create_new_site_checkbox'] == 'yes' ){ |
| 180 |
$blog_fields_array = wppb_blog_details_fields_array(); |
| 181 |
foreach ( $blog_fields_array as $blog_field ){ |
| 182 |
if( ( isset( $request_data[$blog_field] ) && ( trim( $request_data[$blog_field] ) == '' ) ) || !isset( $request_data[$blog_field] ) ){ |
| 183 |
return wppb_required_field_error($blog_field); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
return $message; |
| 188 |
} |
| 189 |
add_filter( 'wppb_check_form_field_default-blog-details', 'wppb_check_blog_details_values', 10, 4 ); |
| 190 |
|
| 191 |
/* Add blog details information to wp_signups table (when Email Confirmation is active) */ |
| 192 |
function wppb_add_blog_details_to_signup_table( $meta, $global_request, $role ){ |
| 193 |
if ( isset( $global_request['wppb_create_new_site_checkbox'] ) && $global_request['wppb_create_new_site_checkbox'] == 'yes' ) { |
| 194 |
$blog_details_fields_array = wppb_blog_details_fields_array(); |
| 195 |
|
| 196 |
foreach ($blog_details_fields_array as $blog_field) { |
| 197 |
$meta[$blog_field] = $global_request[$blog_field]; |
| 198 |
} |
| 199 |
} |
| 200 |
return $meta; |
| 201 |
} |
| 202 |
add_filter( 'wppb_add_to_user_signup_form_meta', 'wppb_add_blog_details_to_signup_table',10, 3 ); |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
function wppb_blog_details_fields_array(){ |
| 207 |
return array( |
| 208 |
'wppb_blog_title', |
| 209 |
'wppb_blog_url', |
| 210 |
'wppb_blog_privacy', |
| 211 |
'wppb_create_new_site_checkbox' |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
function wppb_check_individual_blog_fields( $field_key, $request_data, $form_location ){ |
| 216 |
if ( isset( $request_data['wppb_create_new_site_checkbox'] ) && $request_data['wppb_create_new_site_checkbox'] == 'yes' ) { |
| 217 |
if ( $field_key == 'wppb_blog_privacy' && ( ! isset( $request_data[$field_key] ) || ( isset( $request_data[$field_key] ) && ( trim( $request_data[$field_key] ) == '' ) ) ) ) { |
| 218 |
return '<span class="wppb-form-error">' . wppb_required_field_error($field_key) . '</span>'; |
| 219 |
} |
| 220 |
|
| 221 |
$wp_error = wpmu_validate_blog_signup($request_data['wppb_blog_url'], $request_data['wppb_blog_title']); |
| 222 |
|
| 223 |
if ( $field_key == 'wppb_blog_url' && !empty($wp_error['errors']->errors['blogname'])){ |
| 224 |
return '<span class="wppb-form-error">' . $wp_error['errors']->errors['blogname'][0] . '</span>'; |
| 225 |
} |
| 226 |
if ( $field_key == 'wppb_blog_title' && !empty($wp_error['errors']->errors['blog_title'])){ |
| 227 |
return '<span class="wppb-form-error">' . $wp_error['errors']->errors['blog_title'][0] . '</span>'; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
return ''; |
| 232 |
} |
| 233 |
|
| 234 |
|