When adding URLs, new HTML rows should have unique HTML IDs. (#3431)

* When adding URLs, new HTML rows should have unique HTML IDs.
* Fix newly added assertions in Format_General::test_string2htmlid()

Co-authored-by: ྅༻ Ǭɀħ ༄༆ཉ <ozh@ozh.org>
This commit is contained in:
Barry Hughes 2022-10-04 11:06:48 -07:00 committed by GitHub
parent d86b53bc26
commit b3119b3157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 11 deletions

View File

@ -18,7 +18,7 @@ switch( $action ) {
case 'add':
yourls_verify_nonce( 'add_url', $_REQUEST['nonce'], false, 'omg error' );
$return = yourls_add_new_link( $_REQUEST['url'], $_REQUEST['keyword'] );
$return = yourls_add_new_link( $_REQUEST['url'], $_REQUEST['keyword'], '', $_REQUEST['rowid'] );
echo json_encode($return);
break;

View File

@ -52,12 +52,16 @@ function yourls_string2int($string, $chars = null) {
* Return a unique string to be used as a valid HTML id
*
* @since 1.8.3
* @param string $prefix Optional prefix
* @return string The unique string
* @param string $prefix Optional prefix
* @param int $initial_val The initial counter value (defaults to one)
* @return string The unique string
*/
function yourls_unique_element_id($prefix = 'yid') {
static $id_counter = 0;
return yourls_apply_filter( 'unique_element_id', $prefix . (string)++$id_counter );
function yourls_unique_element_id($prefix = 'yid', $initial_val = 1) {
static $id_counter = 1;
if ($initial_val > 1) {
$id_counter = (int) $initial_val;
}
return yourls_apply_filter( 'unique_element_id', $prefix . (string) $id_counter++ );
}
/**

View File

@ -545,11 +545,12 @@ RETURN;
* @param string $ip IP
* @param string|int $clicks Number of clicks
* @param string $timestamp Timestamp
* @param int $row_id Numeric value used to form row IDs, defaults to one
* @return string HTML of the row
*/
function yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp ) {
function yourls_table_add_row( $keyword, $url, $title, $ip, $clicks, $timestamp, $row_id = 1 ) {
$keyword = yourls_sanitize_keyword($keyword);
$id = yourls_unique_element_id();
$id = yourls_unique_element_id('yid', $row_id);
$shorturl = yourls_link( $keyword );
$statlink = yourls_statlink( $keyword );

View File

@ -26,9 +26,10 @@
* @param string $url URL to shorten
* @param string $keyword optional "keyword"
* @param string $title option title
* @param int $row_id used to form unique IDs in the generated HTML
* @return array array with error/success state and short URL information
*/
function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
function yourls_add_new_link( $url, $keyword = '', $title = '', $row_id = 1 ) {
// Allow plugins to short-circuit the whole function
$pre = yourls_apply_filter( 'shunt_add_new_link', false, $url, $keyword, $title );
if ( false !== $pre ) {
@ -140,7 +141,7 @@ function yourls_add_new_link( $url, $keyword = '', $title = '' ) {
$return['status'] = 'success';
$return['message'] = /* //translators: eg "http://someurl/ added to DB" */ yourls_s( '%s added to database', yourls_trim_long_string( $url ) );
$return['title'] = $title;
$return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time() );
$return['html'] = yourls_table_add_row( $keyword, $url, $title, $ip, 0, time(), $row_id );
$return['shorturl'] = yourls_link($keyword);
$return['statusCode'] = 200; // 200 OK
} else {

View File

@ -35,10 +35,11 @@ function add_link() {
return;
}
var keyword = $("#add-keyword").val();
var nextid = parseInt($('#main_table tbody tr[id^="id-"]').length) + 1;
add_loading("#add-button");
$.getJSON(
ajaxurl,
{action:'add', url: newurl, keyword: keyword, nonce: nonce},
{action:'add', url: newurl, keyword: keyword, nonce: nonce, rowid: nextid},
function(data){
if(data.status == 'success') {
$('#main_table tbody').prepend( data.html ).trigger("update");

View File

@ -105,9 +105,13 @@ class Format_General extends PHPUnit\Framework\TestCase {
public function test_string2htmlid() {
$id1 = yourls_unique_element_id();
$id2 = yourls_unique_element_id();
$id3 = yourls_unique_element_id('foo', 10);
$id4 = yourls_unique_element_id();
$this->assertIsString($id1);
$this->assertIsString($id2);
$this->assertNotSame($id1, $id2);
$this->assertEquals('foo10', $id3, 'ID is built using the specified prefix and counter value.');
$this->assertStringEndsWith('11', $id4, 'ID counter continues to increment from the last value.');
}
/**