Skip to content

Commit 5ca253c

Browse files
committed
feat: add filters support to shortcode
1 parent ad167d8 commit 5ca253c

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,12 @@ public function feedzy_rss( $atts, $content = '' ) {
442442
}
443443
}
444444

445-
$sc['filters'] = apply_filters( 'feedzy_filter_conditions_migration', $sc );
445+
// If the user is explicitly using filters attribute, then use it and ignore old filters.
446+
if ( isset( $sc['filters'] ) && ! empty( $sc['filters'] ) && feedzy_is_pro() ) {
447+
$sc['filters'] = apply_filters( 'feedzy_filter_conditions_attribute', $sc['filters'] );
448+
} else {
449+
$sc['filters'] = apply_filters( 'feedzy_filter_conditions_migration', $sc );
450+
}
446451

447452
$sc = array_diff_key(
448453
$sc,
@@ -505,7 +510,11 @@ public function feedzy_lazy_load( $data ) {
505510
$sc = $this->get_short_code_attributes( $atts );
506511
$feed_url = $this->normalize_urls( $sc['feeds'] );
507512

508-
$sc['filters'] = apply_filters( 'feedzy_filter_conditions_migration', $sc );
513+
if ( isset( $sc['filters'] ) && ! empty( $sc['filters'] ) && feedzy_is_pro() ) {
514+
$sc['filters'] = apply_filters( 'feedzy_filter_conditions_attribute', $sc['filters'] );
515+
} else {
516+
$sc['filters'] = apply_filters( 'feedzy_filter_conditions_migration', $sc );
517+
}
509518

510519
$sc = array_diff_key(
511520
$sc,
@@ -614,6 +623,7 @@ public function get_short_code_attributes( $atts ) {
614623
'to_datetime' => '',
615624
// Disable default style.
616625
'disable_default_style' => 'no',
626+
'filters' => '',
617627
),
618628
$atts,
619629
'feedzy_default'

includes/feedzy-rss-feeds.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ function () {
268268

269269
$plugin_conditions = new Feedzy_Rss_Feeds_Conditions();
270270
$this->loader->add_action( 'feedzy_filter_conditions_migration', $plugin_conditions, 'migrate_conditions' );
271+
$this->loader->add_action( 'feedzy_filter_conditions_attribute', $plugin_conditions, 'convert_filter_string_to_json' );
271272
$this->loader->add_action( 'feedzy_item_keyword', $plugin_conditions, 'evaluate_conditions', 10, 5 );
272273
}
273274

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,52 @@ private function check_contains( $value, $condition_value ): bool {
344344
}
345345
return false;
346346
}
347+
348+
/**
349+
* Convert a filter string to JSON.
350+
*
351+
* This function converts a filter string to a JSON object.
352+
*
353+
* @param string $filter_str The filter string to convert.
354+
*
355+
* @return string The JSON object.
356+
*/
357+
public function convert_filter_string_to_json( $filter_str ) {
358+
// Split into segments by semicolon.
359+
$segments = explode( ';', $filter_str );
360+
$filter_array = array( 'conditions' => array() );
361+
362+
foreach ( $segments as $segment ) {
363+
$segment = trim( $segment );
364+
365+
// Check if this segment defines 'match'.
366+
if ( strpos( $segment, 'match=' ) === 0 ) {
367+
// Extract match value.
368+
list( , $match_val ) = explode( '=', $segment, 2 );
369+
$filter_array['match'] = trim( $match_val );
370+
} elseif ( strpos( $segment, 'condition=' ) === 0 ) {
371+
// Check if this segment defines a 'condition'.
372+
// Remove "condition=" prefix.
373+
$condition_str = substr( $segment, strlen( 'condition=' ) );
374+
$pairs = explode( ',', $condition_str );
375+
$condition = array();
376+
377+
// Each pair is in the form key:value.
378+
foreach ( $pairs as $pair ) {
379+
$pair = trim( $pair );
380+
if ( strpos( $pair, ':' ) !== false ) {
381+
list( $key, $val ) = explode( ':', $pair, 2 );
382+
$condition[ trim( $key ) ] = trim( $val );
383+
}
384+
}
385+
386+
if ( ! empty( $condition ) ) {
387+
$filter_array['conditions'][] = $condition;
388+
}
389+
}
390+
}
391+
392+
// Encode the final array as JSON.
393+
return json_encode( $filter_array );
394+
}
347395
}

0 commit comments

Comments
 (0)