function test_get_by_type()
 {
     $test_type_0 = 'test_type_0';
     $test_type_1 = 'test_type_1';
     $test_type_2 = 'test_type_2';
     add_action('init', 'create_post_types');
     function create_post_type()
     {
         register_post_type($test_type_0);
         register_post_type($test_type_1);
         register_post_type($test_type_2);
     }
     $num_type_1 = 11;
     $this->factory->post->create_many($num_type_1, array('post_type' => $test_type_1));
     $num_type_2 = 12;
     $this->factory->post->create_many($num_type_2, array('post_type' => $test_type_2));
     // seeding additional records
     $this->factory->post->create_many(5);
     $qq = new QQuery();
     $all_posts = $qq->all()->go();
     $type_0_posts = $qq->type($test_type_0)->all()->go();
     $type_1_posts = $qq->type($test_type_1)->all()->go();
     $type_2_posts = $qq->type($test_type_2)->all()->go();
     $this->assertEquals($num_type_1, count($type_1_posts));
     $this->assertEquals($num_type_2, count($type_2_posts));
     $this->assertEmpty($type_0_posts);
 }
Example #2
0
 function test_exclude_ids()
 {
     $howmany = 20;
     $post_ids = $this->factory->post->create_many($howmany);
     shuffle($post_ids);
     $working_ids = array_slice($post_ids, 5);
     $qq = new QQuery();
     $posts1 = $qq->all()->go();
     $posts2 = $qq->all()->exclude($working_ids)->go();
     $this->assertNotEquals($posts1, $posts2);
 }
 function test_offset()
 {
     $post_ids = $this->factory->post->create_many(25);
     // make sure we have more posts than we need
     $posts_per_page = 10;
     $page = 2;
     $offset = 1;
     $query = new WP_Query('posts_per_page=' . $posts_per_page . '&paged=' . $page . '&offset=' . $offset);
     $wp_offset_posts = $query->posts;
     // now get a QQ going
     $qq = new QQuery();
     $offset_qq_posts = $qq->ppp($posts_per_page)->page($page)->offset($offset)->sort('ID')->go();
     $this->assertEquals(array_map("get_ID", $offset_qq_posts), array_map("get_ID", $wp_offset_posts));
 }
Example #4
0
 function test_multiple_terms()
 {
     $post_1 = $this->factory->post->create();
     $post_2 = $this->factory->post->create();
     register_taxonomy('test', 'post', array());
     wp_set_object_terms($post_1, 'term-1', 'test');
     wp_set_object_terms($post_1, 'term-2', 'test', true);
     wp_set_object_terms($post_2, 'term-1', 'test');
     wp_set_object_terms($post_2, 'term-3', 'test', true);
     $qq = new QQuery();
     $posts_both = $qq->tax('term-1')->go();
     // should return both posts
     $this->assertEquals(2, count($posts_both));
     $post_first = $qq->tax('term-2')->go();
     // should only return the first
     $this->assertEquals(1, count($post_first));
     $post_second = $qq->tax('term-3')->go();
     // should only return the second
     $this->assertEquals(1, count($post_second));
     $posts_none = $qq->tax(array('term-2', 'term-3'))->go();
     // should return none because default to AND
     $this->assertEquals(0, count($posts_none));
     $posts_both_again = $qq->tax(array('term-2', 'term-3'), array('relation' => 'OR'))->go();
     // should return both because we set OR
     $this->assertEquals(2, count($posts_both_again));
 }
 function test_status()
 {
     $test_post_status_0 = 'draft';
     $test_post_status_1 = 'pending';
     $test_post_status_2 = 'private';
     $basic_post_count = 5;
     $draft_post_count = 10;
     $pending_post_count = 15;
     $private_post_count = 20;
     $total_post_count = $basic_post_count + $draft_post_count + $pending_post_count + $private_post_count;
     $post_ids = $this->factory->post->create_many($basic_post_count);
     $post_draft_ids = $this->factory->post->create_many($draft_post_count, array('post_status' => $test_post_status_0));
     $post_pending_ids = $this->factory->post->create_many($pending_post_count, array('post_status' => $test_post_status_1));
     $post_private_ids = $this->factory->post->create_many($private_post_count, array('post_status' => $test_post_status_2));
     $qq = new QQuery();
     $all_posts = $qq->all()->status('any')->go();
     $draft_posts = $qq->status($test_post_status_0)->all()->go();
     $pending_posts = $qq->status($test_post_status_1)->all()->go();
     $private_and_pending_posts = $qq->status(array($test_post_status_2, $test_post_status_1))->all()->go();
     $this->assertEquals(count($all_posts), $total_post_count);
     $this->assertEquals(count($draft_posts), $draft_post_count);
     $this->assertEquals(count($pending_posts), $pending_post_count);
     $this->assertEquals(count($private_and_pending_posts), $pending_post_count + $private_post_count);
 }
Example #6
0
 /**
  * Get one post object using a unique identifier
  * @param  string/int 	$post_id_or_slug an int post ID or string post_name
  * @return WP_Post
  */
 public static function get($post_id_or_slug)
 {
     if (is_string($post_id_or_slug)) {
         global $wpdb;
         $post_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $post_id_or_slug));
     } else {
         $post_id = $post_id_or_slug;
     }
     // get_post will only return a WP_Post object OR null
     $post = get_post($post_id);
     if (!$post instanceof WP_Post) {
         return false;
     }
     if (class_exists('acf')) {
         $posts = QQuery::acf_filter(array($post));
         $post = $posts[0];
     }
     // $posts = $this->meta_filter( [$post] );
     // $posts = apply_filters('wp_ups_query_go_posts', $posts);
     // $this->comments_params['post_id'] = $post->ID;
     // $post->comments = get_comments( $this->comments_params );
     return $post;
 }
 /**
  * Internally called method to assist with calling Qcubed Query for this class
  * on load methods.
  * @param QQueryBuilder &$objQueryBuilder the QueryBuilder object that will be created
  * @param QQCondition $objConditions any conditions on the query, itself
  * @param QQClause[] $objOptionalClausees additional optional QQClause object or array of QQClause objects for this query
  * @param mixed[] $mixParameterArray a array of name-value pairs to perform PrepareStatement with (sending in null will skip the PrepareStatement step)
  * @param boolean $blnCountOnly only select a rowcount
  * @return string the query statement
  */
 protected static function BuildQueryStatement(&$objQueryBuilder, QQCondition $objConditions, $objOptionalClauses, $mixParameterArray, $blnCountOnly)
 {
     // Get the Database Object for this Class
     $objDatabase = DleMailLog::GetDatabase();
     // Create/Build out the QueryBuilder object with DleMailLog-specific SELET and FROM fields
     $objQueryBuilder = new QQueryBuilder($objDatabase, 'dle_mail_log');
     $blnAddAllFieldsToSelect = true;
     if ($objDatabase->OnlyFullGroupBy) {
         // see if we have any group by or aggregation clauses, if yes, don't add the fields to select clause
         if ($objOptionalClauses instanceof QQClause) {
             if ($objOptionalClauses instanceof QQAggregationClause || $objOptionalClauses instanceof QQGroupBy) {
                 $blnAddAllFieldsToSelect = false;
             }
         } else {
             if (is_array($objOptionalClauses)) {
                 foreach ($objOptionalClauses as $objClause) {
                     if ($objClause instanceof QQAggregationClause || $objClause instanceof QQGroupBy) {
                         $blnAddAllFieldsToSelect = false;
                         break;
                     }
                 }
             }
         }
     }
     if ($blnAddAllFieldsToSelect) {
         DleMailLog::GetSelectFields($objQueryBuilder, null, QQuery::extractSelectClause($objOptionalClauses));
     }
     $objQueryBuilder->AddFromItem('dle_mail_log');
     // Set "CountOnly" option (if applicable)
     if ($blnCountOnly) {
         $objQueryBuilder->SetCountOnlyFlag();
     }
     // Apply Any Conditions
     if ($objConditions) {
         try {
             $objConditions->UpdateQueryBuilder($objQueryBuilder);
         } catch (QCallerException $objExc) {
             $objExc->IncrementOffset();
             throw $objExc;
         }
     }
     // Iterate through all the Optional Clauses (if any) and perform accordingly
     if ($objOptionalClauses) {
         if ($objOptionalClauses instanceof QQClause) {
             $objOptionalClauses->UpdateQueryBuilder($objQueryBuilder);
         } else {
             if (is_array($objOptionalClauses)) {
                 foreach ($objOptionalClauses as $objClause) {
                     $objClause->UpdateQueryBuilder($objQueryBuilder);
                 }
             } else {
                 throw new QCallerException('Optional Clauses must be a QQClause object or an array of QQClause objects');
             }
         }
     }
     // Get the SQL Statement
     $strQuery = $objQueryBuilder->GetStatement();
     // Prepare the Statement with the Query Parameters (if applicable)
     if ($mixParameterArray) {
         if (is_array($mixParameterArray)) {
             if (count($mixParameterArray)) {
                 $strQuery = $objDatabase->PrepareStatement($strQuery, $mixParameterArray);
             }
             // Ensure that there are no other Unresolved Named Parameters
             if (strpos($strQuery, chr(QQNamedValue::DelimiterCode) . '{') !== false) {
                 throw new QCallerException('Unresolved named parameters in the query');
             }
         } else {
             throw new QCallerException('Parameter Array must be an array of name-value parameter pairs');
         }
     }
     // Return the Objects
     return $strQuery;
 }
 /**
  * Takes a query builder object and outputs the sql query that corresponds to its structure and the given parameters.
  *
  * @param QQueryBuilder &$objQueryBuilder the QueryBuilder object that will be created
  * @param QQCondition $objConditions any conditions on the query, itself
  * @param QQClause[] $objOptionalClauses additional optional QQClause object or array of QQClause objects for this query
  * @param mixed[] $mixParameterArray a array of name-value pairs to perform PrepareStatement with (sending in null will skip the PrepareStatement step)
  * @param boolean $blnCountOnly only select a rowcount
  * @return string the query statement
  * @throws QCallerException
  * @throws Exception
  */
 protected static function BuildQueryStatement(&$objQueryBuilder, QQCondition $objConditions, $objOptionalClauses, $mixParameterArray, $blnCountOnly)
 {
     // Get the Database Object for this Class
     $objDatabase = static::GetDatabase();
     $strTableName = static::GetTableName();
     // Create/Build out the QueryBuilder object with class-specific SELECT and FROM fields
     $objQueryBuilder = new QQueryBuilder($objDatabase, $strTableName);
     $blnAddAllFieldsToSelect = true;
     if ($objDatabase->OnlyFullGroupBy) {
         // see if we have any group by or aggregation clauses, if yes, don't add all the fields to select clause by default
         // because these databases post an error instead of just choosing a value to return when a select item could
         // have multiple values
         if ($objOptionalClauses instanceof QQClause) {
             if ($objOptionalClauses instanceof QQAggregationClause || $objOptionalClauses instanceof QQGroupBy) {
                 $blnAddAllFieldsToSelect = false;
             }
         } else {
             if (is_array($objOptionalClauses)) {
                 foreach ($objOptionalClauses as $objClause) {
                     if ($objClause instanceof QQAggregationClause || $objClause instanceof QQGroupBy) {
                         $blnAddAllFieldsToSelect = false;
                         break;
                     }
                 }
             }
         }
     }
     $objSelectClauses = QQuery::ExtractSelectClause($objOptionalClauses);
     if ($objSelectClauses || $blnAddAllFieldsToSelect) {
         static::BaseNode()->PutSelectFields($objQueryBuilder, null, $objSelectClauses);
     }
     $objQueryBuilder->AddFromItem($strTableName);
     // Set "CountOnly" option (if applicable)
     if ($blnCountOnly) {
         $objQueryBuilder->SetCountOnlyFlag();
     }
     // Apply Any Conditions
     if ($objConditions) {
         try {
             $objConditions->UpdateQueryBuilder($objQueryBuilder);
         } catch (QCallerException $objExc) {
             $objExc->IncrementOffset();
             $objExc->IncrementOffset();
             throw $objExc;
         }
     }
     // Iterate through all the Optional Clauses (if any) and perform accordingly
     if ($objOptionalClauses) {
         if ($objOptionalClauses instanceof QQClause) {
             try {
                 $objOptionalClauses->UpdateQueryBuilder($objQueryBuilder);
             } catch (QCallerException $objExc) {
                 $objExc->IncrementOffset();
                 $objExc->IncrementOffset();
                 throw $objExc;
             }
         } else {
             if (is_array($objOptionalClauses)) {
                 foreach ($objOptionalClauses as $objClause) {
                     try {
                         $objClause->UpdateQueryBuilder($objQueryBuilder);
                     } catch (QCallerException $objExc) {
                         $objExc->IncrementOffset();
                         $objExc->IncrementOffset();
                         throw $objExc;
                     }
                 }
             } else {
                 throw new QCallerException('Optional Clauses must be a QQClause object or an array of QQClause objects');
             }
         }
     }
     // Get the SQL Statement
     $strQuery = $objQueryBuilder->GetStatement();
     // Substitute the correct sql variable names for the placeholders specified in the query, if any.
     if ($mixParameterArray) {
         if (is_array($mixParameterArray)) {
             if (count($mixParameterArray)) {
                 $strQuery = $objDatabase->PrepareStatement($strQuery, $mixParameterArray);
             }
             // Ensure that there are no other Unresolved Named Parameters
             if (strpos($strQuery, chr(QQNamedValue::DelimiterCode) . '{') !== false) {
                 throw new QCallerException('Unresolved named parameters in the query');
             }
         } else {
             throw new QCallerException('Parameter Array must be an array of name-value parameter pairs');
         }
     }
     // Return the Objects
     return $strQuery;
 }
Example #9
0
 /**
  * @param QQClause[]|QQClause|null $objClauses QQClause object or array of QQClause objects
  * @return QQSelect QQSelect clause containing all the nodes from all the QQSelect clauses from $objClauses,
  * or null if $objClauses contains no QQSelect clauses
  */
 public static function extractSelectClause($objClauses)
 {
     if ($objClauses instanceof QQSelect) {
         return $objClauses;
     }
     if (is_array($objClauses)) {
         $hasSelects = false;
         $objSelect = QQuery::Select();
         foreach ($objClauses as $objClause) {
             if ($objClause instanceof QQSelect) {
                 $hasSelects = true;
                 $objSelect->Merge($objClause);
             }
         }
         if (!$hasSelects) {
             return null;
         }
         return $objSelect;
     }
     return null;
 }