Skip to content

Commit 986abd7

Browse files
committed
- Allow authors to have the same name publishpress#682
1 parent 51e68ad commit 986abd7

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/assets/js/multiple-authors.js

+35
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,41 @@ jQuery(document).ready(function ($) {
618618

619619
});
620620

621+
//change submit button to enable slug generation on custom button click
622+
if ($('body.taxonomy-author form#addtag #submit').length > 0) {
623+
$('body.taxonomy-author form#addtag #submit').hide();
624+
$('body.taxonomy-author form#addtag #submit').after('<input type="button" id="author-submit" class="button button-primary" value="' + $('body.taxonomy-author form#addtag #submit').val() + '">');
625+
}
626+
627+
//generate author slug when adding author.
628+
$(document).on('click', 'body.taxonomy-author form#addtag #author-submit', function (event) {
629+
630+
var $authorName = $('input[name="tag-name"]').val();
631+
var $form = $(this).closest('form#addtag');
632+
633+
event.preventDefault();
634+
635+
//prepare ajax data
636+
var data = {
637+
action: "handle_author_slug_generation",
638+
author_name: $authorName,
639+
nonce: MultipleAuthorsStrings.generate_author_slug_nonce,
640+
};
641+
642+
$form.find('.spinner').addClass('is-active');
643+
644+
$.post(ajaxurl, data, function (response) {
645+
$form.find('.spinner').removeClass('is-active');
646+
if (response.author_slug) {
647+
$('input[name="slug"]').val(response.author_slug);
648+
$('body.taxonomy-author form#addtag #submit').trigger('click');
649+
} else {
650+
$('body.taxonomy-author form#addtag #submit').trigger('click');
651+
}
652+
});
653+
654+
});
655+
621656
/**
622657
* Settings shortcode copy to clipboard
623658
*/

src/core/Classes/Admin_Ajax.php

+40
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,44 @@ public static function handle_mapped_author_validation()
316316
wp_send_json($response);
317317
exit;
318318
}
319+
320+
/**
321+
* Handle a request to generate author slug.
322+
*/
323+
public static function handle_author_slug_generation()
324+
{
325+
326+
$response['status'] = 'success';
327+
$response['content'] = esc_html__('Request status.', 'publishpress-authors');
328+
329+
//do not process request if nonce validation failed
330+
if (empty($_POST['nonce'])
331+
|| !wp_verify_nonce(sanitize_key($_POST['nonce']), 'generate_author_slug_nonce')
332+
) {
333+
$response['status'] = 'error';
334+
$response['content'] = esc_html__(
335+
'Security error. Kindly reload this page and try again',
336+
'publishpress-authors'
337+
);
338+
} elseif (empty($_POST['author_name'])) {
339+
$response['status'] = 'error';
340+
$response['content'] = esc_html__('Author name is required', 'publishpress-authors');
341+
} else {
342+
$author_slug = !empty($_POST['author_name']) ? sanitize_title($_POST['author_name']) : '';
343+
$generated_slug = $author_slug;
344+
$generated_slug_n = '';
345+
while (get_term_by('slug', $generated_slug .'-' . $generated_slug_n, 'author')) {
346+
if ($generated_slug_n == '') {
347+
$generated_slug_n = 1;
348+
} else {
349+
$generated_slug_n++;
350+
}
351+
}
352+
$new_slug = $generated_slug .'-' . $generated_slug_n;
353+
$response['author_slug'] = $new_slug;
354+
}
355+
356+
wp_send_json($response);
357+
exit;
358+
}
319359
}

src/core/Plugin.php

+5
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ public function __construct()
270270
'wp_ajax_mapped_author_validation',
271271
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_mapped_author_validation']
272272
);
273+
add_action(
274+
'wp_ajax_handle_author_slug_generation',
275+
['MultipleAuthors\\Classes\\Admin_Ajax', 'handle_author_slug_generation']
276+
);
273277

274278
add_filter('admin_footer_text', [$this, 'update_footer_admin']);
275279
}
@@ -1456,6 +1460,7 @@ public function enqueue_scripts($hook_suffix)
14561460
'publishpress-authors'
14571461
),
14581462
'mapped_author_nonce' => wp_create_nonce("mapped_author_nonce"),
1463+
'generate_author_slug_nonce' => wp_create_nonce("generate_author_slug_nonce"),
14591464
'term_author_link' => esc_url_raw($term_author_link),
14601465
'view_text' => esc_html__('View', 'publishpress-authors'),
14611466
];

0 commit comments

Comments
 (0)