Beispiel #1
0
 function as_array($value) {
   
   if (is_woof_collection($value)) {
     $values = $value->items();
   } else if (is_object($value)) {
     $values = array( $value );
   } else if (is_array($value)) {
     $values = $value;
   } else {
     $values = explode(",", (string) $value);
   }
   
   return $values;
   
 }
Beispiel #2
0
  public function intersect($coll, $on = "id") {
    
    global $wf;
    
    if (is_woof_collection($coll)) {
    
      if (count($coll) && count($this)) {

        $int = array();
      
        foreach ($this as $item) {
          if ($coll->find_by($on, $item->get($on))) {
            $int[] = $item;
          }
        }
      
        return $wf->collection($int);

      } else if (count($this)) {
        
        return $this;
        
      } else {
        
        return $coll;
        
      }
      
    } else {

      return $this;

    }
    
    
  }
Beispiel #3
0
  function in_a($terms, $taxonomy = NULL, $args = array(), $operator = "IN", $relation = "OR") {
    
    global $wf;

    if (!is_array($terms) && is_string($terms)) {
      $terms = explode(",", $terms);
    }

    $r = wp_parse_args( 
      $args,
      array(
        'posts_per_page' => -1
      )
    );

    $r['post_type'] = $this->name;
    
    if (!isset($taxonomy)) {
      
      // if the taxonomy isn't set, $terms MUST be a single or collection of WOOF_Term objects for this to work
      
      if (WOOF::is_or_extends($terms, "WOOF_Term")) {
        $terms = $wf->collection( array($terms) );
      }
      
      if (is_woof_collection($terms, "WOOF_Term") && count($terms)) {
        
        // now build a tax query
        
        $r['tax_query'] = array("relation" => $relation);
          
        $grouped = $terms->group_by("taxonomy_name");
        
        foreach ($grouped as $taxonomy_name => $terms) {
          
          $r['tax_query'][] = array(
      	    'taxonomy' => $taxonomy_name,
      	    'terms' => $terms->extract("slug"),
      	    'field' => 'slug',
            'operator' => $operator
    	    );

        }
        
        
      } else {

        return new WOOF_Silent(__("To perform a query without specifying a taxonomy, you must provide a single WOOF_Term or a collection of WOOF_Term objects", WOOF_DOMAIN));
      
      }
      
      
    } else {
    
      $r['tax_query'] = array(
        array(
    	    'taxonomy' => $taxonomy,
    	    'terms' => $terms,
    	    'field' => 'slug',
          'operator' => $operator
    	  )
      );
    
    }
    
    return $wf->posts($r);
      
  }
Beispiel #4
0
  function in_a($terms, $taxonomy = NULL, $args = array(), $operator = "IN", $relation = "OR") {
    
    global $wf;

    $posts = parent::in_a($terms, $taxonomy, $args, $operator, $relation);
    
    
    if ($wf->regard_field_terms()) {
      
      if (!is_array($terms) && is_string($terms)) {
        $terms = explode(",", $terms);
      }

      if (!isset($taxonomy)) {

        $term_objects = $terms;
        
        if (WOOF::is_or_extends($terms, "WOOF_Term")) {
          $terms = $wf->collection( array($terms) );
        }
      
      } else {
        
        $term_objects = array();
        
        foreach ($terms as $term) {
          
          $obj = $wf->term($term, $taxonomy);
      
          if (!is_woof_silent($obj)) {
            $term_objects[] = $obj;
          }

        }
    
        $term_objects = $wf->collection( $term_objects );

      }
      
      
      if (is_woof_collection($term_objects, "WOOF_Term") && count($term_objects)) {
        
        $grouped = $term_objects->group_by( "taxonomy_name" );

        $matching_posts = $wf->collection();
        
        foreach ($grouped as $taxonomy_name => $terms) {
          
          $tax_posts = $wf->collection();
          
          foreach ($terms as $term) {

            if ($operator == "IN") {
              $tax_posts->merge( $term->incoming( array("post_type" => $this->name ) ), false );
            } else if ($operator == "AND") {
              $tax_posts = $tax_posts->intersect( $term->incoming( array("post_type" => $this->name ) ), "slug" );
            }
          
          }

          if ($relation == "OR") {
            $matching_posts = $matching_posts->merge( $tax_posts, false );
          } else {
            $matching_posts = $matching_posts->intersect( $tax_posts, "slug" );
          }

          
        }
        
        $posts->merge( $matching_posts, false );

        $posts->dedupe();
        
      }
      
    }
    
    return $posts;
    
  }