Habari\Posts::search_to_get PHP Метод

search_to_get() публичный статический Метод

Parses a search string for status, type, author, and tag keywords. Returns an associative array which can be passed to Posts::get(). If multiple authors, statuses, tags, or types are specified, we assume an implicit OR such that (e.g.) any author that matches would be returned.
public static search_to_get ( string $search_string ) : array
$search_string string The search string
Результат array An associative array which can be passed to Posts::get()
    public static function search_to_get($search_string)
    {
        $statuses = Post::list_post_statuses();
        $types = Post::list_active_post_types();
        $arguments = array('user_id' => array(), 'status' => array(), 'content_type' => array(), 'vocabulary' => array(), 'info' => array());
        // this says, find stuff that has the keyword at the start, and then some term straight after.
        // the terms should have no whitespace, or if it does, be ' delimited.
        // ie tag:foo or tag:'foo bar'
        $flag_regex = '/(?P<flag>\\w+):(?P<value>[^\'"][^\\s]*|(?P<quote>[\'"])[^\\3]+(?<!\\\\)\\3)/i';
        // now do some matching.
        preg_match_all($flag_regex, $search_string, $matches, PREG_SET_ORDER);
        // now we remove those terms from the search string, otherwise the keyword search below has issues. It will pick up things like
        // from tag:'pair of' -> matches of'
        $criteria = trim(preg_replace($flag_regex, '', $search_string));
        // Add special criteria based on the flag parameters.
        foreach ($matches as $match) {
            // trim out any quote marks that have been matched.
            $quote = isset($match['quote']) ? $match['quote'] : ' ';
            $value = trim(stripslashes($match['value']), $quote);
            $flag = $match['flag'];
            $arguments = Plugins::filter('posts_search_to_get', $arguments, $flag, $value, $match, $search_string);
            switch ($flag) {
                case 'author':
                    if ($u = User::get($value)) {
                        $arguments['user_id'][] = (int) $u->id;
                    }
                    break;
                case 'tag':
                    $arguments['vocabulary'][Tags::vocabulary()->name . ':term_display'][] = $value;
                    break;
                case 'status':
                    if (isset($statuses[$value])) {
                        $arguments['status'][] = (int) $statuses[$value];
                    }
                    break;
                case 'type':
                    if (isset($types[$value])) {
                        $arguments['content_type'][] = (int) $types[$value];
                    }
                    break;
                case 'info':
                    if (strpos($value, ':') !== false) {
                        list($infokey, $infovalue) = explode(':', $value, 2);
                        $arguments['info'][$infokey] = $infovalue;
                    }
                    break;
            }
        }
        // flatten keys that have single-element or no-element arrays
        foreach ($arguments as $key => $arg) {
            switch (count($arg)) {
                case 0:
                    unset($arguments[$key]);
                    break;
                case 1:
                    if (is_array($arg)) {
                        $arguments[$key] = $arg;
                    } else {
                        $arguments[$key] = $arg[0];
                    }
                    break;
            }
        }
        if ($criteria != '') {
            $arguments['criteria'] = $criteria;
        }
        return $arguments;
    }