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
40 changes: 31 additions & 9 deletions models/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
* flag_trailing: bool,
* flag_regex: bool,
* database_stage?: array{stage?: string|false, stages?: array<mixed>, status?: string|false},
* location?: string
* location?: string,
* ignore_posttypes?: array<string>,
* }
*/
class Red_Options {
Expand Down Expand Up @@ -192,6 +193,7 @@ public static function get_default_options() {
'cache_key' => 0,
'plugin_update' => 'prompt',
'update_notice' => 0,
'ignore_posttypes' => [],
];

$defaults = array_merge( $defaults, $flags->get_json() );
Expand Down Expand Up @@ -271,16 +273,36 @@ function ( $header ) use ( $available ) {
$options['rest_api'] = intval( $settings['rest_api'], 10 );
}

if ( isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] ) ) {
$allowed = red_get_post_types( false );

foreach ( $settings['monitor_types'] as $type ) {
if ( in_array( $type, $allowed, true ) ) {
$monitor_types[] = $type;
}
}
$ignore_posttypes_settings_exist = isset( $settings['ignore_posttypes'] ) && is_array( $settings['ignore_posttypes'] );
$monitor_types_settings_exist = isset( $settings['monitor_types'] ) && is_array( $settings['monitor_types'] );

if ( $monitor_types_settings_exist || $ignore_posttypes_settings_exist ) {
$allowed = red_get_post_types( false );

$options['monitor_types'] = $monitor_types;
if( $monitor_types_settings_exist ){

foreach ( $settings['monitor_types'] as $type ) {
if ( in_array( $type, $allowed, true ) ) {
$monitor_types[] = $type;
}
}

$options['monitor_types'] = $monitor_types;

}

if( $ignore_posttypes_settings_exist ){

$ignore_posttypes = [];
foreach ( $settings['ignore_posttypes'] as $post_type ) {
if ( in_array( $post_type, $allowed, true ) ) {
$ignore_posttypes[] = $post_type;
}
}

$options['ignore_posttypes'] = $ignore_posttypes;
}
}

if ( isset( $settings['associated_redirect'] ) && is_string( $settings['associated_redirect'] ) ) {
Expand Down
32 changes: 32 additions & 0 deletions models/url/url-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,36 @@ public function is_protected_url() {

return false;
}

/**
* Check if the current URL's post type is in the ignored post types list.
*
* This function resolves the original URL to a post ID, determines its post type,
* and compares it against the `ignore_posttypes` setting.
*
* @return bool True if the post type is in the ignore list, false otherwise.
*/
public function is_ignore_posttypes(){


$options = $options = Red_Options::get();;

if( empty( $options['ignore_posttypes'][0] ) ){
return false;
}

// Resolve the original URL to a post ID.
$post_id = url_to_postid( $this->original_url );
if( empty( $post_id ) ){
return false;
}

$post_type = get_post_type( $post_id );
if( empty( $post_type ) ){
return false;
}

// Check if the post type is listed in ignored post types.
return in_array( $post_type, $options['ignore_posttypes'], true );
}
}
2 changes: 1 addition & 1 deletion modules/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function init() {
$request = new Red_Url_Request( Redirection_Request::get_request_url() );

// Make sure we don't try and redirect something essential
if ( $request->is_valid() && ! $request->is_protected_url() ) {
if ( $request->is_valid() && ! $request->is_protected_url() && ! $request->is_ignore_posttypes() ) {
do_action( 'redirection_first', $request->get_decoded_url(), $this );

// Get all redirects that match the URL
Expand Down
81 changes: 81 additions & 0 deletions src/page/options/options-form/ignore-posttypes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* External dependencies
*/

import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { TableRow } from 'component/form-table';
import { createInterpolateElement, MultiOptionDropdown, Notice } from 'wp-plugin-components';

interface PostTypes {
[ key: string ]: string;
}

interface Settings {
ignore_posttypes: string[];
}

interface IgnorePostTypesOptionsProps {
settings: Settings;
onChange: ( ev: React.ChangeEvent< HTMLInputElement | HTMLTextAreaElement > | { [ key: string ]: any } ) => void;
getLink: ( rel: string, anchor?: string ) => string;
postTypes: PostTypes;
}

function IgnorePostTypesOptions( props: IgnorePostTypesOptionsProps ) {
const { settings, onChange, getLink, postTypes } = props;
const { ignore_posttypes } = settings;
const ignorePostTypesLength = ignore_posttypes.length;
return (
<>
<tr className="redirect-option__row">
<td colSpan={ 2 }>
<h2 className="title">{ __( 'Post types to ignore from redirection', 'redirection' ) }</h2>
</td>
</tr>
<TableRow
title={ __( 'Post types', 'redirection' ) + ':' }
url={ getLink( 'options', 'ignore_posttypes' ) }
>
<MultiOptionDropdown
options={ Object.keys( postTypes ).map( ( item ) => {
return { value: item, label: postTypes[ item ] };
} ) }
selected={ ignore_posttypes }
multiple
badges={ ignorePostTypesLength > 0 }
hideTitle={ ignorePostTypesLength > 0 }
onApply={ ( options: any ) => onChange( { ignore_posttypes: options } ) }
title={ ignorePostTypesLength === 0 ? __( 'Posttypes', 'redirection' ) : '' }
/>
{ ignorePostTypesLength > 0 && (
<>
<p>
{ ' ' }
{ __(
'Redirection rules will not be applied to posts of the selected post types.',
'redirection'
) }{ ' ' }
</p>
<p>
<Notice status="warning">
{ createInterpolateElement(
__(
'Warning: This feature will not work if your site’s permalink settings are set to <code>Plain</code>.',
'redirection'
),
{ code: <code /> }
) }
</Notice>
</p>
</>
) }
</TableRow>
</>
);
}

export default IgnorePostTypesOptions;
7 changes: 7 additions & 0 deletions src/page/options/options-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { __ } from '@wordpress/i18n';
import { FormTable } from 'component/form-table';
import { Button } from '@wp-plugin-components';
import LogOptions from './log-options';
import IgnorePostTypesOptions from './ignore-posttypes';
import OtherOptions from './other-options';
import UrlOptions from './url-options';
import './style.scss';
Expand Down Expand Up @@ -73,6 +74,12 @@ function OptionsForm() {
<form onSubmit={ onSubmit }>
<FormTable>
<LogOptions settings={ settings as any } onChange={ onChange } getLink={ supportLink } />
<IgnorePostTypesOptions
settings={ settings as any }
onChange={ onChange }
getLink={ supportLink }
postTypes={ postTypes }
/>
<UrlOptions
settings={ settings as any }
onChange={ onChange }
Expand Down