Skip to content

Commit

Permalink
Settings for delete all redirects and numbers of redirects per page (#…
Browse files Browse the repository at this point in the history
…140)

* Settings for delete all redirects and numbers of redirects per page

* Add css class to delete all redirects button and change its text

* Improve CSS
  • Loading branch information
doanducanh authored Jun 28, 2024
1 parent 13a9928 commit 65dd471
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 44 deletions.
29 changes: 15 additions & 14 deletions css/scss/redirection.scss
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,22 @@
}
}

.ss-paginate {
.ss-redirects-footer {
display: flex;
justify-content: center;
gap: 20px;

.disabled {
display: none;
}

.selected a {
color: inherit;
}

a {
cursor: pointer;
justify-content: space-between;
align-items: center;
padding: 12px;

.ss-paginate {
ul {
margin: 0;
display: flex;
gap: 4px;

.disabled {
display: none!important;
}
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion css/scss/settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
.update-nag {
display: none;
}

.button-link-delete,
.button-link-delete:hover {
border-color: currentColor;
}
}

.ss-content {
Expand Down Expand Up @@ -132,4 +137,4 @@ p.submit {
&.ss-is-active {
display: block;
}
}
}
2 changes: 1 addition & 1 deletion css/settings.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions js/redirection/components/Limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { __, _n, sprintf } from '@wordpress/i18n';

const Limit = ( { limit, setLimit, total, setOffset, setIsCheckAll, setCheckedList } ) => {
const handleChange = e => {
setLimit( parseInt( e.target.value ) );

setOffset( 0 );
setIsCheckAll( false );
setCheckedList( [] );
};

return (
<div className='ss-limit'>
<select value={ limit } onChange={ handleChange }>
<option value={ 20 }>20</option>
<option value={ 50 }>50</option>
<option value={ 100 }>100</option>
<option value={ 200 }>200</option>
<option value={ total }>{ __( 'All', 'slim-seo' ) }</option>
</select>
&nbsp;
{ sprintf( _n( 'items per page. Total %d item.', 'items per page. Total %d items.', 'slim-seo' ), Number( total ) ) }
</div>
);
};

export default Limit;
31 changes: 20 additions & 11 deletions js/redirection/components/Paginate.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { __ } from '@wordpress/i18n';
import ReactPaginate from 'react-paginate';

const Paginate = ( { totalRows, limit, setOffset } ) => {
const Paginate = ( { totalRows, limit, offset, setOffset, setIsCheckAll, setCheckedList } ) => {
const pageCount = Math.ceil( totalRows / limit );

const handlePageClick = e => {
const newOffset = ( e.selected * limit ) % totalRows;
setOffset( newOffset );
setIsCheckAll( false );
setCheckedList( [] );
};

return pageCount > 1 && (
<ReactPaginate
className='ss-paginate'
breakLabel='...'
nextLabel={ __( 'Next »', 'slim-seo' ) }
onPageChange={ handlePageClick }
pageRangeDisplayed={ 5 }
pageCount={ pageCount }
previousLabel={ __( '« Previous', 'slim-seo' ) }
renderOnZeroPageCount={ null }
/>
<div className='ss-paginate'>
<ReactPaginate
breakLabel='...'
nextLabel='»'
onPageChange={ handlePageClick }
pageRangeDisplayed={ 3 }
pageCount={ pageCount }
previousLabel='«'
renderOnZeroPageCount={ null }
forcePage={ offset / limit }
pageLinkClassName="button"
previousLinkClassName="button"
nextLinkClassName="button"
breakLinkClassName="button"
activeLinkClassName="button-primary"
/>
</div>
);
};

Expand Down
34 changes: 19 additions & 15 deletions js/redirection/redirects/Items.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import Paginate from '../components/Paginate';
import { fetcher, useApi } from '../helper/misc';
import Header from './Header';
import Item from './Item';
import Limit from '../components/Limit';
import Paginate from '../components/Paginate';

const Items = ( { searchKeyword, redirectType, executeBulkAction, setExecuteBulkAction } ) => {
const LIMIT = 20;
const [ limit, setLimit ] = useState( 20 );
const [ offset, setOffset ] = useState( 0 );
const [ checkedList, setCheckedList ] = useState( [] );
const [ isCheckAll, setIsCheckAll ] = useState( false );
const [ remountPaginate, setRemountPaginate ] = useState( 0 );
const { result: redirects, mutate } = useApi( 'redirects', {}, { returnMutate: true } );

const checkAll = () => {
setIsCheckAll( !isCheckAll );

if ( isCheckAll ) {
setCheckedList( [] );
} else {
setCheckedList( redirects.map( redirect => redirect.id ) );
}
};

const deleteRedirects = ( ids = [] ) => {
fetcher( 'delete_redirects', { ids }, 'POST' ).then( result => {
mutate(
Expand Down Expand Up @@ -80,10 +71,20 @@ const Items = ( { searchKeyword, redirectType, executeBulkAction, setExecuteBulk
filteredRedirects = filteredRedirects.filter( redirect => redirect.type == redirectType );
}

const checkAll = () => {
setIsCheckAll( !isCheckAll );

if ( isCheckAll ) {
setCheckedList( [] );
} else {
setCheckedList( filteredRedirects.slice( offset, offset + limit ).map( redirect => redirect.id ) );
}
};

if ( !filteredRedirects.length ) {
return <span>{ __( 'No redirects found.', 'slim-seo' ) }</span>;
}

return (
<>
<table className='ss-table'>
Expand All @@ -92,15 +93,18 @@ const Items = ( { searchKeyword, redirectType, executeBulkAction, setExecuteBulk
</thead>

<tbody>
{ filteredRedirects.slice( offset, offset + LIMIT ).map( redirect => <Item key={ redirect.id } redirectItem={ redirect } checkedList={ checkedList } setCheckedList={ setCheckedList } deleteRedirects={ deleteRedirects } updateRedirects={ updateRedirects } /> ) }
{ filteredRedirects.slice( offset, offset + limit ).map( redirect => <Item key={ redirect.id } redirectItem={ redirect } checkedList={ checkedList } setCheckedList={ setCheckedList } deleteRedirects={ deleteRedirects } updateRedirects={ updateRedirects } /> ) }
</tbody>

<tfoot>
<Header isCheckAll={ isCheckAll } checkAll={ checkAll } />
</tfoot>
</table>

<Paginate key={ remountPaginate } totalRows={ filteredRedirects.length } limit={ LIMIT } setOffset={ setOffset } />
<div className='ss-redirects-footer'>
<Limit limit={ limit } setLimit={ setLimit } total={ filteredRedirects.length } setOffset={ setOffset } setIsCheckAll={ setIsCheckAll } setCheckedList={ setCheckedList } />
<Paginate key={ remountPaginate } totalRows={ filteredRedirects.length } limit={ limit } offset={ offset } setOffset={ setOffset } setIsCheckAll={ setIsCheckAll } setCheckedList={ setCheckedList } />
</div>
</>
);
};
Expand Down
25 changes: 24 additions & 1 deletion js/redirection/settings/Settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RawHTML, useReducer, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Tooltip } from '../helper/misc';
import { Tooltip, fetcher } from '../helper/misc';

const Settings = () => {
const { settings, settingsName } = SSRedirection;
Expand All @@ -13,6 +13,20 @@ const Settings = () => {
const [ redirect404To, setRedirect404To ] = useState( settings[ 'redirect_404_to' ] );
const [ redirect404ToURL, setRedirect404ToURL ] = useState( settings[ 'redirect_404_to_url' ] );

const deleteAllRedirects = e => {
e.preventDefault();

if ( ! confirm( __( 'Are you sure to delete all redirects?', 'slim-seo' ) ) ) {
return;
}

fetcher( 'delete_redirects', { ids: 'all' }, 'POST' ).then( result => {
if ( result ) {
location.reload();
}
} );
};

return (
<>
<table className='form-table'>
Expand Down Expand Up @@ -131,6 +145,15 @@ const Settings = () => {
}
</td>
</tr>

<tr>
<th scope="row">
<label htmlFor="ss-delete-all-redirects">{ __( 'Delete all redirects', 'slim-seo' ) }</label>
</th>
<td>
<button id="ss-delete-all-redirects" className='button button-link-delete' onClick={ deleteAllRedirects }>{ __( 'Delete', 'slim-seo' ) }</button>
</td>
</tr>
</tbody>
</table>

Expand Down
6 changes: 6 additions & 0 deletions src/Redirection/Api/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public function update_redirect( WP_REST_Request $request ): string {
public function delete_redirects( WP_REST_Request $request ): bool {
$ids = $request->get_param( 'ids' );

if ( 'all' === $ids ) {
$this->db_redirects->delete_all();

return true;
}

$this->db_redirects->delete( $ids );

return true;
Expand Down
4 changes: 4 additions & 0 deletions src/Redirection/Database/Redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public function delete( array $ids ) {
update_option( SLIM_SEO_REDIRECTS, $this->redirects );
}

public function delete_all() {
delete_option( SLIM_SEO_REDIRECTS, $this->redirects );
}

public function update_all( array $redirects ) {
$this->redirects = $redirects;

Expand Down
4 changes: 4 additions & 0 deletions src/Redirection/ExportImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public function import( WP_REST_Request $request ): bool {

$added = true;

$redirect['from'] = Helper::normalize_url( $redirect['from'], false );
$redirect['to'] = Helper::normalize_url( $redirect['to'], true, true, false );
$redirect['note'] = sanitize_text_field( $redirect['note'] );

$redirects[ uniqid() ] = $redirect;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redirection/Redirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function redirect() {
$current_url = rtrim( $url_parts[0], '/' );
}

$from = $redirect['from'];
$from = Helper::normalize_url( $redirect['from'], false );
$to = $redirect['to'];
$should_redirect = false;

Expand Down

0 comments on commit 65dd471

Please sign in to comment.