diff --git a/models/options.php b/models/options.php index 19f495ba..f6a041fb 100644 --- a/models/options.php +++ b/models/options.php @@ -39,7 +39,8 @@ * flag_trailing: bool, * flag_regex: bool, * database_stage?: array{stage?: string|false, stages?: array, status?: string|false}, - * location?: string + * location?: string, + * ignore_posttypes?: array, * } */ class Red_Options { @@ -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() ); @@ -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'] ) ) { diff --git a/models/url/url-request.php b/models/url/url-request.php index 78ab8e41..572450df 100644 --- a/models/url/url-request.php +++ b/models/url/url-request.php @@ -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 ); + } } diff --git a/modules/wordpress.php b/modules/wordpress.php index 68ff1126..e070fb38 100644 --- a/modules/wordpress.php +++ b/modules/wordpress.php @@ -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 diff --git a/src/page/options/options-form/ignore-posttypes.tsx b/src/page/options/options-form/ignore-posttypes.tsx new file mode 100644 index 00000000..cb70feb0 --- /dev/null +++ b/src/page/options/options-form/ignore-posttypes.tsx @@ -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 ( + <> + + +

{ __( 'Post types to ignore from redirection', 'redirection' ) }

+ + + + { + 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 && ( + <> +

+ { ' ' } + { __( + 'Redirection rules will not be applied to posts of the selected post types.', + 'redirection' + ) }{ ' ' } +

+

+ + { createInterpolateElement( + __( + 'Warning: This feature will not work if your site’s permalink settings are set to Plain.', + 'redirection' + ), + { code: } + ) } + +

+ + ) } +
+ + ); +} + +export default IgnorePostTypesOptions; diff --git a/src/page/options/options-form/index.tsx b/src/page/options/options-form/index.tsx index 5a7d3b88..fab38948 100644 --- a/src/page/options/options-form/index.tsx +++ b/src/page/options/options-form/index.tsx @@ -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'; @@ -73,6 +74,12 @@ function OptionsForm() {
+