meta_box_context(), /*context=*/ $page->meta_box_context() ); } function html_tags_metabox ($page, $box = NULL) { if ($page->for_feed_settings()) : $ada_key_filters = maybe_unserialize($page->link->settings['ada key filters']); $include_sitewide_ada_key_filters = $page->link->settings['include sitewide ada key filters']; $syndicatedPosts = 'this feed\'s posts'; else : $ada_key_filters = get_option('feedwordpress_ada_key_filters'); $syndicatedPosts = 'syndicated posts'; endif; ?>

To remove a keyword filter, just blank out the text box - leave it empty - and save.

Keywords are case in-sensitive. Enter comma seperated list of words along with your selection of logics: OR, AND, OR NOT, AND NOT. Do not leave extra space as it will be used to match exactly.

OR
If any of the word in a comma seperated word list is found, include the syndicated content.
AND
Only when all of the words in a comma seperated word list is found, include the syndicated content.
OR NOT
If any of the word in a comma seperated word list is found, do NOT include the syndicated content.
AND NOT
Only when all of the words in a comma seperated word list is found, do NOT include the syndicated content.

When you only use *NOT logics [OR NOT, AND NOT], the syndicated content will be included if those words are not found

When there are *NOT logics [OR NOT, AND NOT] along with other logics [OR, AND], then *NOT logics take precedence. If *NOT logic's keyword is found within the syndicated content, all the other rules are ignored and the content is not included.

"No, must match keywords in %s", 'yes' => "Yes, skip keyword filters from %s", ); foreach ($filterSelector as $index => $value) : $filterSelector[$index] = sprintf(__($value), $syndicatedPosts); endforeach; $params = array( 'input_name' => 'disable_ada_key_filters', 'setting-default' => 'default', 'global-setting-default' => 'no', 'labels' => $filterSelector, 'default-input-value' => 'default', ); ?>
setting_radio_control( 'disable ada key filters', 'disable_ada_key_filters', $filterSelector, $params ); ?>
setting('disable ada key filters', 'feedwordpress_disable_ada_key_filters', 'yes'); if($disable_ada_key_filters != 'yes') $meta = $feed->setting('ada key filters', 'feedwordpress_ada_key_filters', false); } else { $disable_ada_key_filters = 'yes'; } $post['post_title'] = "I do"; $post['post_content'] = "I support free Tamileelam"; echo '$post[title]=' . $post['post_title'] . '
'; echo '$post[post_content]=' . $post['post_content'] . '
'; echo $disable_ada_key_filters . '=disable_ada_key_filters
'; echo (is_array($meta)) ? 'keywords are in good form [ARRAY]
' : 'keywords are not in good form [NOT ARRAY]
'; if($disable_ada_key_filters != 'yes' && is_array($meta)){ if(!$this->ada_include($post,$meta)) echo '***NOT INCLUDE***
'; else echo '***INCLUDE***
'; } CAPitalZ}*/ print "
$line) : if( count( array_filter(array_map('trim',explode(',',$line['keywords']))) ) == 0 ) { //remove any empty space elements, empty commas submitted by user & check unset($params['ada_key_filters'][$index]); } else { $line['keywords'] = serialize(array_filter(explode(',',$line['keywords']))); //trying to save performance by exploding and array_filtering on saving than on syndication. See ALREADY_ARRAY_KEYWORDS $params['ada_key_filters'][$index] = $line; } endforeach; // Convert indexes to 0..(N-1) to avoid possible collisions $params['ada_key_filters'] = array_values($params['ada_key_filters']); if(count($params['ada_key_filters']) > 0) { if($this->ada_is_found_in($params['ada_key_filters'], "NOT")) //If NOT logic found sort the logics so that all the NOT logics are at the top //could check for ada_is_not_logic_all(), but then I don't know which is costly $params['ada_key_filters'] = $this->ada_sort_logic_precedence($params['ada_key_filters']); } else //if no filters found unset($params['ada_key_filters']); } else //if disable_ada_key_filters == 'yes' unset($params['ada_key_filters']); if ($page->for_feed_settings()) : if ('default'==$params['disable_ada_key_filters']) { //for a particular feed, the setting is 'default', then no need to save anything as everything is pulled from Global setting unset($page->link->settings['disable ada key filters']); unset($page->link->settings['ada key filters']); } else { //for both 'no' or 'yes' $page->link->settings['disable ada key filters'] = $params['disable_ada_key_filters']; if(!empty($params['ada_key_filters'])) { //it will be empty if 'yes'. it was made previously $page->link->settings['ada key filters'] = serialize($params['ada_key_filters']); //$line['keywords'] is double serialized $page->link->settings['include sitewide ada key filters'] = $params['include_sitewide_ada_key_filters']; //If there is no other filters, user should use the global 'default' option } else { unset($page->link->settings['ada key filters']); unset($page->link->settings['include sitewide ada key filters']); } } $page->link->save_settings(/*reload=*/ true); else : update_option('feedwordpress_disable_ada_key_filters', $params['disable_ada_key_filters']); if(!empty($params['ada_key_filters'])) update_option('feedwordpress_ada_key_filters', $params['ada_key_filters']); else delete_option('feedwordpress_ada_key_filters'); endif; } } /* AdaFWPKeyFilters::posts_save() */ function ada_include($post,$meta){ $not_logic_found = $this->ada_is_found_in($meta, "NOT"); //is at least one NOT logic found? if($this->debug) { //echo "Checking *TITLE*:'" . post['post_title'] . "'
"; fprintf($this->handle, "Checking *TITLE*: '%s'\n", $post['post_title']); } $include = $this->ada_include_content($post['post_title'],$meta); if($this->debug) { //echo "Checking *TITLE*
"; fprintf($this->handle, "Should include by *TITLE*? %s\n", is_null($include) ? '***NOT DECIDED***' : ($include ? '***YES***' : '***NO***') ); } if(!empty($post['post_content']) && (is_null($include) || ($include && $not_logic_found))) { //$include == null means 'could not able to come to a decision' //even if $include is TRUE, we should check the post_content, if there is a NOT logic, //as there could be blacklisted keywords found in post_content //if $include is false, then no need to check the post_content. if($this->debug) { //echo "Checking *POST CONTENT*
"; fprintf($this->handle, "Checking *POST CONTENT*\n"); } $include = $this->ada_include_content($post['post_content'],$meta, $include); } if(is_null($include)) { if($this->ada_is_found_all($meta, "NOT")) { //is only the NOT logic found? $include = true; //since NOT keywords are not found, $include is still null, and user only specified NOT keywords and nothing else, we can safely include this post if($this->debug) { //echo "Could not find any keywords && all NOT logic, hence INCLUDE.
"; fprintf($this->handle, "Could not find any keywords && all NOT logic, hence INCLUDE.\n"); } } else{ //The user defined other logics also - not just NOT logics if($this->debug) { //echo "Could not find any keywords, hence NOT INCLUDE.
"; fprintf($this->handle, "Could not find any keywords, hence NOT INCLUDE.\n"); } $include = false; //NOT keywords are not found, as $include is still null. Yet, the user defined other logics are not found as well since this filter has not just NOT logics but others as well } } return $include; } /* AdaFWPKeyFilters::ada_include() */ //is 'needle' in every element of 2D 'haystack' array? function ada_is_found_all($haystack, $needle){ $correct = true; if(is_array($haystack)) foreach ($haystack as $rule) { if(!$this->ada_is_found($rule['logic'], $needle)){ $correct = false; break; } } else $correct = false; return $correct; } //is 'needle' found in at least one element of 2D 'haystack' array? function ada_is_found_in($haystack, $needle){ $found = false; if(is_array($haystack)) foreach ($haystack as $rule) { $found = $this->ada_is_found($rule['logic'], $needle); if($found) break; } return $found; } //is 'needle' found in at least one element of 'haystack' string list? function ada_is_found($haystack, $needle){ return stripos($haystack,$needle) !== false; } //Give precedence to NOT logic. Reorder logics so that NOT logics comes at the top function ada_sort_logic_precedence($meta){ $sorted_meta = array(); foreach($meta as $rule){ if($this->ada_is_found($rule['logic'], "NOT")) $sorted_meta[] = $rule; else $sorted_meta_others[] = $rule; } return is_array($sorted_meta_others) ? array_merge($sorted_meta, $sorted_meta_others) : $sorted_meta; } function ada_include_content($text,$meta,$include = null){ //$include = the result of step 1; checked against post title //$include = to say the logic already said to include the Post - after looking at post's Title, //but there is NOT logic [user entered] that needs to be checked for post_content //then only we can for sure say, include the Post if(is_array($meta)) foreach ($meta as $rule) { $keywords = unserialize($rule['keywords']); //unserializing the double serialized $rule['keywords'] //ALREADY_ARRAY_KEYWORDS: no need to explode & array_filter as the data from DB is optimized if($this->debug) { //echo "Checking " . $rule['logic'] . ": '" . implode(',', $keywords) . "'
"; fprintf($this->handle, "Checking %s: '%s'\n", $rule['logic'], implode(',', $keywords)); } if($include && !$this->ada_is_found($rule['logic'], "NOT")) //After post_title's iteration, the $include = true. // now this function is called first time and there was a NOT logic found and the whole funciton is executed. // but the 2nd time if the logic is not NOT, then no need to check further. // as we made all NOT logics to be at the top and we already established the post is $include=true based on other logics in the post_title's iteration break; //see comment SORTED_NOT_LOGIC else //if NOT logic found $is_found = $this->ada_is_keyword_found($text, $keywords, $rule['logic']); if($is_found) { if($this->debug) { //echo $rule['logic'] . ": '" . implode(',', $keywords) . "' found.
"; fprintf($this->handle, "%s: '%s' found\n", $rule['logic'], implode(',', $keywords)); } if($this->ada_is_found($rule['logic'], "NOT")) { //ANDNOT or ORNOT $include = false; break; //since black list keyword is found already } else { //AND or OR $include = true; break; //SORTED_NOT_LOGIC //originally we should not break yet, as there could be more NOTs //but assuming we would have the logics sorted that all the NOTs are at the top of the array - higher precedence //we can avoid some extra loops } } } return $include; } /* AdaFWPKeyFilters::ada_include_content() */ function ada_is_keyword_found($haystack, $array_of_needles, $logic) { $found = false; $count_of_keywords_to_match = -1; if($this->ada_is_found($logic, "AND")) //AND or ANDNOT $count_of_keywords_to_match = count($array_of_needles); //must match as many as keywords found foreach ($array_of_needles as $needle) { if($this->ada_is_found($haystack, $needle)) { if($count_of_keywords_to_match < 0) { //ORNOT and OR $found = true; break; } else //ANDNOT and AND $count_of_keywords_to_match--; //all keywords should be found } elseif($count_of_keywords_to_match > -1) { //ANDNOT and AND $found = false; //one keyword is not found in the ANDNOT or AND logic, hence we can break the loop break; } } if($count_of_keywords_to_match===0) { //if AND or ANDNOT logic found all keywords $found = true; } return $found; } /* AdaFWPKeyFilters::ada_is_keyword_found() */ function the_content ($posts, $syndiPost) { global $wpdb; if ( $this->debug ) { $wpdb->show_errors(); $this->handle = fopen("ada_key_filters.log", "a+"); fprintf($this->handle, "=============================================="); fprintf($this->handle, "Started on %s\n", date('Y-m-d h:i:s a', time())); } $link = $syndiPost; $disable_ada_key_filters = $link->setting('disable ada key filters', 'feedwordpress_disable_ada_key_filters', 'yes'); if ( $this->debug ) { fprintf($this->handle, "%s=disable_ada_key_filters\n", $disable_ada_key_filters); } if($disable_ada_key_filters == 'yes'){ if ( $this->debug ) { fclose($this->handle); } return $posts; } $meta = maybe_unserialize($link->setting('ada key filters', 'feedwordpress_ada_key_filters', false)); if(is_array($meta)){ $include_sitewide_ada_key_filters = $link->setting('include sitewide ada key filters', '', 0); if($include_sitewide_ada_key_filters) { if ( $this->debug ) { fprintf($this->handle, "meta is ARRAY\n"); fprintf($this->handle, "[include sitewide ada key filters] is set\n"); } //[include sitewide ada key filters] is not set in feed settings if there was no ada keyword filters. Even if you try to manually set it without any keywords and save, the program will un-set. Its not a bug // Then only we won't duplicate filters in here $sitewide_meta = get_option('feedwordpress_ada_key_filters'); if(is_array($sitewide_meta)) { if(!$this->ada_is_found_in($sitewide_meta, "NOT")) { $meta = array_merge($meta, $sitewide_meta); } else { //If NOT logic found, sort the logics so that all the NOT logics are at the top $meta = array_merge($meta, $sitewide_meta); $meta = $this->ada_sort_logic_precedence($meta); } } //if(is_array($sitewide_meta)) } //if($include_sitewide_ada_key_filters) if ( is_array( $posts )) { foreach ( $posts as $key => $item ) { $post = new SyndicatedPost( $item , $link ); if(!$this->ada_include($post->post,$meta)) { if($this->debug) { //echo '***NOT INCLUDE***
'; fprintf($this->handle, "***NOT INCLUDE***\n"); } unset( $posts[$key] ); } else if ( $this->debug ) { //echo '***INCLUDE***
'; fprintf($this->handle, "***INCLUDE***\n"); } } //foreach ( $posts as $key => $item ) } //if ( is_array( $posts )) } //if(is_array($meta)) if ( $this->debug ) { fclose($this->handle); } return $posts; //if using syndicated_post filter hook } /* AdaFWPKeyFilters::the_content() */ } /* class AdaFWPKeyFilters */ // Hook us in $ada_key_filters = new AdaFWPKeyFilters;