Skip to content

Commit

Permalink
fix: the mess with escape and unescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Dec 20, 2024
1 parent 64ddc9b commit c0c7cb7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
4 changes: 4 additions & 0 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ public function save_feedzy_import_feed_meta( $post_id, $post ) {
$data_meta['import_auto_translation'] = isset( $data_meta['import_auto_translation'] ) ? $data_meta['import_auto_translation'] : 'no';
// Check feeds external image URL checkbox checked OR not.
$data_meta['import_use_external_image'] = isset( $data_meta['import_use_external_image'] ) ? $data_meta['import_use_external_image'] : 'no';
// If it is filter_conditions we want to escape it.
if ( isset( $data_meta['filter_conditions'] ) ) {
$data_meta['filter_conditions'] = wp_slash( $data_meta['filter_conditions'] );
}

// $data_meta['feedzy_post_author'] should be the author username. We convert it to the author ID.
if ( ! empty( $data_meta['import_post_author'] ) ) {
Expand Down
5 changes: 2 additions & 3 deletions includes/util/feedzy-rss-feeds-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,10 @@ public function is_condition_met( $condition, $value ): bool {
}
break;
case self::OPERATOR_REGEX:
$regex_pattern = $condition_value;
if ( ! preg_match( '/^\/.*\/[imsxuADU]*$/', $condition_value ) ) {
$regex_pattern = '/' . $condition_value . '/i';
$condition_value = '/' . $condition_value . '/i';
}
return preg_match( $regex_pattern, $value ) === 1;
return preg_match( $condition_value, $value ) === 1;
default:
// Default is self::OPERATOR_HAS_VALUE
return ! empty( $value );
Expand Down
26 changes: 5 additions & 21 deletions js/Conditions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,11 @@ const App = () => {
const field = document.getElementById('feed-post-filters-conditions');
if (field && field.value) {
const parsedConditions = JSON.parse(field.value);
if (parsedConditions && parsedConditions.conditions) {
parsedConditions.conditions = parsedConditions.conditions.map(
(condition) => {
// We do all these schananigans to make sure we JS doesn't confuse regex for special characters.
if (typeof condition.value === 'string') {
condition.value = condition.value
.replace(/\u0008/g, '\\b')
.replace(/\u000C/g, '\\f')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
}
return condition;
}
);
setConditions(parsedConditions);
} else {
setConditions({ conditions: [], match: 'all' });
}
} else {
setConditions({ conditions: [], match: 'all' });
setConditions(
parsedConditions && parsedConditions.conditions
? parsedConditions
: { conditions: [], match: 'all' }
);
}
}, []);

Expand Down
7 changes: 7 additions & 0 deletions tests/test-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,12 @@ public function test_is_condition_met_regex() {
'value' => '/test/',
);
$this->assertTrue( $this->conditions->is_condition_met( $condition, 'this is a test' ) );


$condition = array(
'operator' => 'regex',
'value' => '\band\b',
);
$this->assertTrue( $this->conditions->is_condition_met( $condition, 'matt and tommy' ) );
}
}

0 comments on commit c0c7cb7

Please sign in to comment.