Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate unique slug for the group when provided group slug exists #289

Open
wants to merge 1 commit into
base: 1.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion js/group-url.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function($){
var $errorFormat, $errorTaken, $nameField, $urlField, $urlFieldParent;
var isValid, isTaken;
var isValid, uniqueUrlTimer;

$(document).ready(function(){
isValid = isAvailable = false;
Expand Down Expand Up @@ -49,6 +49,11 @@
toggleError();
});

// Cancel uniqueURL event if user started typing.
$urlField.on( 'keydown', function() {
clearTimeout(uniqueUrlTimer);
} );

// Only run the AJAX request on blur.
$urlField.on( 'blur', function() {
// No value? Nothing to check.
Expand Down Expand Up @@ -79,6 +84,8 @@
} else {
isAvailable = false;
$errorTaken.removeAttr( 'aria-hidden' );

uniqueUrl( response.data );
}
toggleError();
}
Expand All @@ -94,5 +101,17 @@
$ajaxStatus.removeClass('fa-check').addClass('fa-exclamation-circle');
}
}

var uniqueUrl = function( slug ) {
uniqueUrlTimer = setTimeout( function() {
$urlField.val( slug );

// Remove Error
isAvailable = true;
isValid = true;
$errorTaken.attr( 'aria-hidden', 'true' );
toggleError();
}, 5000 );
}
});
}(jQuery));
46 changes: 43 additions & 3 deletions lib/group-funcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,8 @@ function openlab_ajax_group_url_validate() {
$found = groups_get_id( $url );

if ( $found && $found !== $group_id ) {
// Try to get a unique slug for the group.
wp_send_json_error();
$unique_slug = openlab_unique_group_slug( $url );
wp_send_json_error( $unique_slug );
} else {
wp_send_json_success();
}
Expand Down Expand Up @@ -1385,7 +1385,6 @@ function openlab_previous_step_type( $url ) {
add_filter( 'bp_get_group_creation_previous_link', 'openlab_previous_step_type' );

/**
>>>>>>> 1.3.x
* Remove the 'hidden' class from hidden group leave buttons
*
* A crummy conflict with wp-ajax-edit-comments causes these items to be
Expand Down Expand Up @@ -2638,3 +2637,44 @@ function openlab_group_avatar_script_data( $script_data ) {
return $script_data;
}
add_filter( 'bp_attachment_avatar_script_data', 'openlab_group_avatar_script_data' );

/**
* Generate unique slug for group.
*
* @param string $slug Desired slug.
* @return string Unique slug for the group.
*/
function openlab_unique_group_slug( $slug ) {
global $wpdb, $bp;

$suffix = 2;
$slug = sanitize_title( $slug );

$group_slugs = $wpdb->get_col( $wpdb->prepare( "SELECT slug FROM {$bp->groups->table_name} WHERE slug LIKE %s", $slug . '%' ) );

if ( empty( $group_slugs ) ) {
return "$slug-$suffix";
}

// Get all suffixes from the group slugs.
$suffixes = array_reduce( $group_slugs, function( $carry, $slug ) {
$parts = explode( '-', $slug );

// String will be covnerted to 0.
$carry[] = (int) end( $parts );

return $carry;
} );
$suffixes = array_filter( array_unique( $suffixes ) );

// Slugs have no suffixes. Return default suffix (-2).
if ( empty( $suffixes ) ) {
return "$slug-$suffix";
}

// Assume the highest suffix is the last one.
$last = max( $suffixes );
$suffix = ++$last;

return "$slug-$suffix";
}