| | 661 | |
| | 662 | /** |
| | 663 | * Parses a search string for status, type, author, and tag keywords. Returns |
| | 664 | * an associative array which can be passed to Posts::get(). If multiple |
| | 665 | * authors, statuses, tags, or types are specified, we assume an implicit OR |
| | 666 | * such that (e.g.) any author that matches would be returned. |
| | 667 | * |
| | 668 | * @param string $search_string The search string |
| | 669 | * @return array An associative array which can be passed to Posts::get() |
| | 670 | */ |
| | 671 | public static function search_to_get( $search_string ) |
| | 672 | { |
| | 673 | $keywords= array( 'author' => 1, 'status' => 1, 'type' => 1, 'tag' => 1 ); |
| | 674 | $statuses= Post::list_post_statuses(); |
| | 675 | $types= Post::list_active_post_types(); |
| | 676 | $arguments= array( |
| | 677 | 'user_id' => array(), |
| | 678 | 'status' => array(), |
| | 679 | 'content_type' => array(), |
| | 680 | 'tag' => array() |
| | 681 | ); |
| | 682 | $criteria= ''; |
| | 683 | |
| | 684 | $tokens= explode( ' ', $search_string ); |
| | 685 | |
| | 686 | foreach( $tokens as $token ) { |
| | 687 | // check for a keyword:value pair |
| | 688 | if ( preg_match( '/^\w+:\S+$/', $token ) ) { |
| | 689 | list( $keyword, $value )= explode( ':', $token ); |
| | 690 | |
| | 691 | $keyword= strtolower( $keyword ); |
| | 692 | switch ( $keyword ) { |
| | 693 | case 'author': |
| | 694 | if ( $u= User::get( $value ) ) { |
| | 695 | $arguments['user_id'][]= (int) $u->id; |
| | 696 | } |
| | 697 | break; |
| | 698 | case 'tag': |
| | 699 | $arguments['tag'][]= $value; |
| | 700 | break; |
| | 701 | case 'status': |
| | 702 | if ( isset( $statuses[$value] ) ) { |
| | 703 | $arguments['status'][]= (int) $statuses[$value]; |
| | 704 | } |
| | 705 | break; |
| | 706 | case 'type': |
| | 707 | if ( isset( $types[$value] ) ) { |
| | 708 | $arguments['content_type'][]= (int) $types[$value]; |
| | 709 | } |
| | 710 | break; |
| | 711 | } |
| | 712 | } |
| | 713 | else { |
| | 714 | $criteria .= $token . ' '; |
| | 715 | } |
| | 716 | } |
| | 717 | |
| | 718 | // flatten keys that have single-element or no-element arrays |
| | 719 | foreach ( $arguments as $key => $arg ) { |
| | 720 | switch ( count( $arg ) ) { |
| | 721 | case 0: |
| | 722 | unset( $arguments[$key] ); |
| | 723 | break; |
| | 724 | case 1: |
| | 725 | $arguments[$key]= $arg[0]; |
| | 726 | break; |
| | 727 | } |
| | 728 | } |
| | 729 | |
| | 730 | if ( $criteria != '' ) { |
| | 731 | $arguments['criteria']= $criteria; |
| | 732 | } |
| | 733 | |
| | 734 | return $arguments; |
| | 735 | } |