Skip to content
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
28 changes: 24 additions & 4 deletions js/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ jQuery( document ).ready( function () {
);
}

/**
* Generate the DataTables row ID for an option name in a table.
*
* The same option can be listed in more than one table, so the table ID is
* included to keep the <tr> ids unique on the page.
*
* @param {string} tableId - The table ID, without '#'.
* @param {string} optionName - The option name.
* @return {string} The row ID.
*/
function getTableRowId( tableId, optionName ) {
return tableId + '-' + generateRowId( optionName );
}

/**
* Store sources (plugin names) for each table from AJAX responses.
*
Expand Down Expand Up @@ -106,7 +120,7 @@ jQuery( document ).ready( function () {
responsive: true,
columns: getColumns( selector ),
rowId( data ) {
return generateRowId( data.name );
return getTableRowId( selector.substring( 1 ), data.name );
},
initComplete() {
this.api().columns( 'source:name' ).every( filterSetup );
Expand Down Expand Up @@ -508,7 +522,8 @@ jQuery( document ).ready( function () {
e.preventDefault();
const button = jQuery( this );
const table = button.closest( 'table' ).DataTable();
const optionName = button.data( 'option' );
// Read the raw attribute, .data() parses JSON-like names such as '["foo"]' or 'null'.
const optionName = button.attr( 'data-option' );

const requestData = { option_name: optionName };
let action = '';
Expand Down Expand Up @@ -553,7 +568,9 @@ jQuery( document ).ready( function () {
*/
function updateRowOnSuccess( response, table, optionName, action ) {
// Look the row up by DataTables row ID, not a CSS selector, so option names with special characters still match.
const row = table.row( '#' + generateRowId( optionName ) );
const row = table.row(
'#' + getTableRowId( table.table().node().id, optionName )
);
if ( action === 'delete-option' || action === 'create-option-false' ) {
row.remove().draw( 'full-hold' );
} else if (
Expand Down Expand Up @@ -690,9 +707,12 @@ jQuery( document ).ready( function () {
data: requestData,
success: () => {
const dt = table.DataTable();
const tableId = dt.table().node().id;

requestData.option_names.forEach( ( optionName ) => {
dt.row( '#' + generateRowId( optionName ) ).remove();
dt.row(
'#' + getTableRowId( tableId, optionName )
).remove();
} );

dt.draw( 'full-hold' );
Expand Down
32 changes: 20 additions & 12 deletions src/class-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ public function register_hooks() {
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
}

/**
* The REST argument definition for a single option name.
*
* The name is used as is, sanitize_text_field() would change names like 'a b' or 'a<b>c'
* and make the request act on a different option. Requiring a string rejects arrays and objects.
*
* @return array<string, mixed>
*/
protected function get_option_name_arg() {
return [
'required' => true,
'type' => 'string',
'minLength' => 1,
'maxLength' => 191,
];
}

/**
* Register the REST API routes.
*
Expand All @@ -50,10 +67,7 @@ public function register_rest_routes() {
return current_user_can( 'manage_options' );
},
'args' => [
'option_name' => [
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
],
'option_name' => $this->get_option_name_arg(),
'autoload' => [
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
Expand All @@ -72,10 +86,7 @@ public function register_rest_routes() {
return current_user_can( 'manage_options' );
},
'args' => [
'option_name' => [
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
],
'option_name' => $this->get_option_name_arg(),
],
]
);
Expand All @@ -90,10 +101,7 @@ public function register_rest_routes() {
return current_user_can( 'manage_options' );
},
'args' => [
'option_name' => [
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
],
'option_name' => $this->get_option_name_arg(),
],
]
);
Expand Down
Loading