Skip to content

Commit c0c7cb7

Browse files
committed
fix: the mess with escape and unescaping
1 parent 64ddc9b commit c0c7cb7

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,10 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
565565
$data_meta['import_auto_translation'] = isset( $data_meta['import_auto_translation'] ) ? $data_meta['import_auto_translation'] : 'no';
566566
// Check feeds external image URL checkbox checked OR not.
567567
$data_meta['import_use_external_image'] = isset( $data_meta['import_use_external_image'] ) ? $data_meta['import_use_external_image'] : 'no';
568+
// If it is filter_conditions we want to escape it.
569+
if ( isset( $data_meta['filter_conditions'] ) ) {
570+
$data_meta['filter_conditions'] = wp_slash( $data_meta['filter_conditions'] );
571+
}
568572

569573
// $data_meta['feedzy_post_author'] should be the author username. We convert it to the author ID.
570574
if ( ! empty( $data_meta['import_post_author'] ) ) {

includes/util/feedzy-rss-feeds-conditions.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,10 @@ public function is_condition_met( $condition, $value ): bool {
219219
}
220220
break;
221221
case self::OPERATOR_REGEX:
222-
$regex_pattern = $condition_value;
223222
if ( ! preg_match( '/^\/.*\/[imsxuADU]*$/', $condition_value ) ) {
224-
$regex_pattern = '/' . $condition_value . '/i';
223+
$condition_value = '/' . $condition_value . '/i';
225224
}
226-
return preg_match( $regex_pattern, $value ) === 1;
225+
return preg_match( $condition_value, $value ) === 1;
227226
default:
228227
// Default is self::OPERATOR_HAS_VALUE
229228
return ! empty( $value );

js/Conditions/index.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,11 @@ const App = () => {
3636
const field = document.getElementById('feed-post-filters-conditions');
3737
if (field && field.value) {
3838
const parsedConditions = JSON.parse(field.value);
39-
if (parsedConditions && parsedConditions.conditions) {
40-
parsedConditions.conditions = parsedConditions.conditions.map(
41-
(condition) => {
42-
// We do all these schananigans to make sure we JS doesn't confuse regex for special characters.
43-
if (typeof condition.value === 'string') {
44-
condition.value = condition.value
45-
.replace(/\u0008/g, '\\b')
46-
.replace(/\u000C/g, '\\f')
47-
.replace(/\n/g, '\\n')
48-
.replace(/\r/g, '\\r')
49-
.replace(/\t/g, '\\t');
50-
}
51-
return condition;
52-
}
53-
);
54-
setConditions(parsedConditions);
55-
} else {
56-
setConditions({ conditions: [], match: 'all' });
57-
}
58-
} else {
59-
setConditions({ conditions: [], match: 'all' });
39+
setConditions(
40+
parsedConditions && parsedConditions.conditions
41+
? parsedConditions
42+
: { conditions: [], match: 'all' }
43+
);
6044
}
6145
}, []);
6246

tests/test-conditions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,12 @@ public function test_is_condition_met_regex() {
145145
'value' => '/test/',
146146
);
147147
$this->assertTrue( $this->conditions->is_condition_met( $condition, 'this is a test' ) );
148+
149+
150+
$condition = array(
151+
'operator' => 'regex',
152+
'value' => '\band\b',
153+
);
154+
$this->assertTrue( $this->conditions->is_condition_met( $condition, 'matt and tommy' ) );
148155
}
149156
}

0 commit comments

Comments
 (0)