Skip to content

Commit c78e6fa

Browse files
Merge pull request #1029 from Codeinwp/fix/issue-627
feat: add regex support to Search/Replace action
2 parents aa6ca12 + 8b92b1f commit c78e6fa

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

includes/admin/feedzy-rss-feeds-actions.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,24 @@ private function trim_content() {
398398
*/
399399
private function search_replace() {
400400
$content = call_user_func( array( $this, $this->current_job->tag ) );
401-
return str_replace( $this->current_job->data->search, $this->current_job->data->searchWith, $content );
401+
$search = $this->current_job->data->search;
402+
$replace = $this->current_job->data->searchWith;
403+
$mode = isset( $this->current_job->data->mode ) ? $this->current_job->data->mode : 'text';
404+
405+
switch ( $mode ) {
406+
case 'wildcard':
407+
$pattern = preg_quote( $search, '/' );
408+
$pattern = str_replace( '\\*', '.*', $pattern );
409+
return preg_replace( '/' . $pattern . '/i', $replace, $content );
410+
case 'regex':
411+
if ( ! preg_match( '/^\/.\/[imsxuADU]$/', $search ) ) {
412+
$pattern = '/' . $search . '/i';
413+
}
414+
415+
return preg_replace( $pattern, $replace, $content );
416+
default:
417+
return str_replace( $search, $replace, $content );
418+
}
402419
}
403420

404421
/**

js/ActionPopup/SortableItem.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,32 @@ const SortableItem = ({ propRef, loopIndex, item }) => {
113113
<div className="fz-action-event">
114114
<PanelBody title={ __( 'Search and Replace', 'feedzy-rss-feeds' ) } icon={ DragHandle } initialOpen={ false }>
115115
<PanelRow>
116+
<BaseControl>
117+
<SelectControl
118+
label={ __( 'Type', 'feedzy-rss-feeds' ) }
119+
value={ item.data.mode || 'text' }
120+
options={[
121+
{
122+
label: __( 'Text', 'feedzy-rss-feeds' ),
123+
value: 'text',
124+
},
125+
{
126+
label: __( 'Regex', 'feedzy-rss-feeds' ),
127+
value: 'regex',
128+
},
129+
{
130+
label: __( 'Wildcard', 'feedzy-rss-feeds' ),
131+
value: 'wildcard',
132+
}
133+
]}
134+
onChange={ ( currentValue ) => propRef.onChangeHandler( { 'index': loopIndex, 'mode': currentValue ?? 'text' } ) }
135+
/>
136+
</BaseControl>
116137
<BaseControl>
117138
<TextControl
118139
type="text"
119140
label={__( 'Search', 'feedzy-rss-feeds' )}
120-
placeholder={__( 'Enter term', 'feedzy-rss-feeds' )}
141+
placeholder={__( 'Enter term or regex', 'feedzy-rss-feeds' )}
121142
value={ item.data.search ? unescape(item.data.search.replaceAll('&#039;', '\'')) : '' }
122143
onChange={ ( currentValue ) => propRef.onChangeHandler( { 'index': loopIndex, 'search': currentValue ?? '' } ) }
123144
/>

0 commit comments

Comments
 (0)