get_option( $option ); } /** * Validates an array * * @param $array the array to validate * * @return bool true on validate or false on invalid value * @ since 2.0.0 **/ function als_is_array( $array ) { return ( is_array($array) && count($array) > 0 ); } /** * Validates a string * * @param $string the string to validate * * @return bool true on validate or false on invalid value * @ since 2.0.0 **/ function als_is_string( $string ) { return ( !is_null($string) && strlen($string) > 0 ); } /** * Get's a given search modifier from a search term * * @param $modifier the modifier eg intittle: * @param $q the query * * @return bool true on validate or false on invalid value * @ since 2.0.0 **/ function als_query_modifier( $modifier, $q ) { // the value to be modified starts with a quote, we use it as our delimeter // Otherwise, we use spaces as delimiters $is_quoted = false; $start = stripos($q, $modifier); $regex = $modifier . '([\'"]{1})'; $q = stripslashes($q); //Took me 4 hours to realise that the query has been escaped with slashes if($start === false){ return false; // end early to save resources } else if(preg_match("/$regex/i", $q, $matches)){ $is_quoted = true; $quote=$matches[1]; } $regex = '/\b' . $modifier . '(.+?)\s/i'; //captures the value assigned to a modifier if($is_quoted){ $regex = '/\b' . $modifier . $quote . '(.+?)' . $quote .'/i'; //captures a quoted value assigned to a modifier } $q .= ' ';//To accomodate the space (\s) in the first regex if(preg_match($regex , $q, $matches)) return $matches[1]; return false; } /** * Searchable post types * * @since 2.1.1 * @return array an array of selected post types */ function als_post_types( ) { $all = als_get_option( 'searchable-post-types' ); if( !is_array( $all ) ) { return array(); } return $all; } /** * Prints the ajax/live search section on result pages * * @since 2.1.1 * @return void */ function als_ajax_init() { echo ''; } /** * Fetches details of searches that have/lack results * * @since 1.0.0 * * @return array */ function als_results_vs_no_results() { global $wpdb; $table = $wpdb->prefix . 'als_log'; $sql_empty = "SELECT COUNT(id) as no_results FROM $table WHERE hits=0"; $sql_empty = $wpdb->get_results($sql_empty); $sql_empty = $sql_empty[0]->no_results; $sql_non_empty = "SELECT COUNT(id) as results FROM $table WHERE hits<>0"; $sql_non_empty = $wpdb->get_results($sql_non_empty); $sql_non_empty = $sql_non_empty[0]->results; return array($sql_empty, $sql_non_empty); }