Example #1
0
 /**
  * @covers ::bbp_admin_repair_forum_reply_count
  */
 public function test_bbp_admin_repair_forum_reply_count()
 {
     $c = $this->factory->forum->create(array('forum_meta' => array('forum_type' => 'category', 'status' => 'open')));
     $f = $this->factory->forum->create(array('post_parent' => $c, 'forum_meta' => array('forum_id' => $c, 'forum_type' => 'forum', 'status' => 'open')));
     $t = $this->factory->topic->create(array('post_parent' => $f, 'topic_meta' => array('forum_id' => $f)));
     $r = $this->factory->reply->create(array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(1, $count);
     $r = $this->factory->reply->create_many(3, array('post_parent' => $t, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     bbp_update_forum_reply_count($c);
     bbp_update_forum_reply_count($f);
     // Category reply count.
     $count = bbp_get_forum_reply_count($c, false, true);
     $this->assertSame(0, $count);
     // Category total reply count.
     $count = bbp_get_forum_reply_count($c, true, true);
     $this->assertSame(4, $count);
     // Forum reply count.
     $count = bbp_get_forum_reply_count($f, false, true);
     $this->assertSame(4, $count);
     // Forum total reply count.
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(4, $count);
     bbp_spam_reply($r[0]);
     bbp_unapprove_reply($r[2]);
     // Category reply count.
     $count = bbp_get_forum_reply_count($c, false, true);
     $this->assertSame(0, $count);
     // Category total reply count.
     $count = bbp_get_forum_reply_count($c, true, true);
     $this->assertSame(2, $count);
     // Forum reply count.
     $count = bbp_get_forum_reply_count($f, false, true);
     $this->assertSame(2, $count);
     // Forum total reply count.
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(2, $count);
     // Delete the _bbp_total_reply_count meta key.
     $this->assertTrue(delete_post_meta_by_key('_bbp_total_reply_count'));
     // Delete the _bbp_reply_count meta key.
     $this->assertTrue(delete_post_meta_by_key('_bbp_reply_count'));
     // Category reply count.
     $count = bbp_get_forum_reply_count($c, false, true);
     $this->assertSame(0, $count);
     // Category total reply count.
     $count = bbp_get_forum_reply_count($c, true, true);
     $this->assertSame(0, $count);
     // Forum reply count.
     $count = bbp_get_forum_reply_count($f, false, true);
     $this->assertSame(0, $count);
     // Forum total reply count.
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(0, $count);
     // Repair the forum reply count meta.
     bbp_admin_repair_forum_reply_count();
     // Category reply count.
     $count = bbp_get_forum_reply_count($c, false, true);
     $this->assertSame(0, $count);
     // Category total reply count.
     $count = bbp_get_forum_reply_count($c, true, true);
     $this->assertSame(2, $count);
     // Forum reply count.
     $count = bbp_get_forum_reply_count($f, false, true);
     $this->assertSame(2, $count);
     // Forum total reply count.
     $count = bbp_get_forum_reply_count($f, true, true);
     $this->assertSame(2, $count);
 }
 /**
  * Perform the export
  *
  * @access public
  * @since 1.0
  * @uses bbp_Export::csv_rows_out()
  * @return void
  */
 public function import()
 {
     if (!$this->can_import()) {
         wp_die(__('You do not have permission to import data.', 'bbp-export-import'), __('Error', 'bbp-export-import'));
     }
     $csv_array = $this->csv_to_array($_FILES['bbp_csv_file']['tmp_name'], ';');
     foreach ($csv_array as $key => $line) {
         $post_args = $line;
         $meta_args = array();
         // Setup author date
         if ($post_args['anonymous'] == '1') {
             $meta_args['anonymous_email'] = $line['post_author'];
         } else {
             $user = get_user_by('email', $post_args['post_author']);
             if (!$user) {
                 // The user doesn't exist, so create them
                 $user = wp_insert_user(array('user_email' => $post_args['post_author'], 'user_login' => $post_args['user_login']));
             }
             $post_args['post_author'] = $user->ID;
         }
         // Decode content
         $post_args['post_content'] = html_entity_decode($post_args['post_content']);
         $topic_type = bbp_get_topic_post_type();
         $reply_type = bbp_get_reply_post_type();
         // Remove the post args we don't want sent to wp_insert_post
         unset($post_args['anonymous']);
         unset($post_args['user_login']);
         switch ($line['post_type']) {
             case $topic_type:
                 // Set the forum parent for topics
                 $post_args['post_parent'] = $this->forum_id;
                 $meta_args['voice_count'] = $line['voices'];
                 $meta_args['reply_count'] = $post_args['reply_count'];
                 $topic_id = bbp_insert_topic($post_args, $meta_args);
                 // Subscribe the original poster to the topic
                 bbp_add_user_subscription($post_args['post_author'], $topic_id);
                 // Add the topic to the user's favorites
                 if (bbp_is_user_favorite($post_args['post_author'], $topic_id)) {
                     bbp_add_user_favorite($post_args['post_author'], $topic_id);
                 }
                 // Set topic as resolved if GetShopped Support Forum is active
                 if ($post_args['resolved'] == '1') {
                     add_post_meta($topic_id, '_bbps_topic_status', '2');
                 }
                 break;
             case $reply_type:
                 // Set the forum parent for replies. The topic ID is created above when the replie's topic is first created
                 $post_args['post_parent'] = $topic_id;
                 $reply_id = bbp_insert_reply($post_args, $meta_args);
                 // Subscribe reply author, if not already
                 if (!bbp_is_user_subscribed($post_args['post_author'], $topic_id)) {
                     bbp_add_user_subscription($post_args['post_author'], $topic_id);
                 }
                 // Mark as favorite
                 if (bbp_is_user_favorite($post_args['post_author'], $topic_id)) {
                     bbp_add_user_favorite($post_args['post_author'], $topic_id);
                 }
                 // Check if the next row is a topic, meaning we have reached the last reply and need to update the last active time
                 if ($csv_array[$key + 1]['post_type'] == bbp_get_topic_post_type()) {
                     bbp_update_forum_last_active_time($this->forum_id, $post_args['post_date']);
                 }
                 break;
         }
     }
     // Recount forum topic / reply counts
     bbp_admin_repair_forum_topic_count();
     bbp_admin_repair_forum_reply_count();
     wp_redirect(admin_url('post.php?post=' . $this->forum_id . '&action=edit'));
     exit;
 }