Ejemplo n.º 1
0
 /**
  * @group canonical
  * @covers ::bbp_insert_topic
  */
 public function test_bbp_insert_topic()
 {
     $f = $this->factory->forum->create();
     $now = time();
     $post_date = date('Y-m-d H:i:s', $now - 60 * 60 * 100);
     $t = $this->factory->topic->create(array('post_title' => 'Topic 1', 'post_content' => 'Content for Topic 1', 'post_parent' => $f, 'post_date' => $post_date, 'topic_meta' => array('forum_id' => $f)));
     $r = $this->factory->reply->create(array('post_parent' => $t, 'post_date' => $post_date, 'reply_meta' => array('forum_id' => $f, 'topic_id' => $t)));
     // Get the topic.
     $topic = bbp_get_topic($t);
     remove_all_filters('bbp_get_topic_content');
     // Topic post.
     $this->assertSame('Topic 1', bbp_get_topic_title($t));
     $this->assertSame('Content for Topic 1', bbp_get_topic_content($t));
     $this->assertSame('publish', bbp_get_topic_status($t));
     $this->assertSame($f, wp_get_post_parent_id($t));
     $this->assertEquals('http://' . WP_TESTS_DOMAIN . '/?topic=' . $topic->post_name, $topic->guid);
     // Topic meta.
     $this->assertSame($f, bbp_get_topic_forum_id($t));
     $this->assertSame(1, bbp_get_topic_reply_count($t, true));
     $this->assertSame(0, bbp_get_topic_reply_count_hidden($t, true));
     $this->assertSame(1, bbp_get_topic_voice_count($t, true));
     $this->assertSame($r, bbp_get_topic_last_reply_id($t));
     $this->assertSame($r, bbp_get_topic_last_active_id($t));
     $this->assertSame('4 days, 4 hours ago', bbp_get_topic_last_active_time($t));
 }
        function widget($args, $instance)
        {
            if (RWLogger::IsOn()) {
                $params = func_get_args();
                RWLogger::LogEnterence("RatingWidgetPlugin_TopRatedWidget.widget", $params, true);
            }
            if (!defined("WP_RW__SITE_PUBLIC_KEY") || false === WP_RW__SITE_PUBLIC_KEY) {
                return;
            }
            if (RatingWidgetPlugin::$WP_RW__HIDE_RATINGS) {
                return;
            }
            extract($args, EXTR_SKIP);
            $bpInstalled = ratingwidget()->IsBuddyPressInstalled();
            $bbInstalled = ratingwidget()->IsBBPressInstalled();
            $types = $this->GetTypesInfo();
            $show_any = false;
            foreach ($types as $type => $data) {
                if (false !== $instance["show_{$type}"]) {
                    $show_any = true;
                    break;
                }
            }
            if (RWLogger::IsOn()) {
                RWLogger::Log('RatingWidgetPlugin_TopRatedWidget', 'show_any = ' . ($show_any ? 'TRUE' : 'FALSE'));
            }
            if (false === $show_any) {
                // Nothing to show.
                return;
            }
            $details = array("uid" => WP_RW__SITE_PUBLIC_KEY);
            $queries = array();
            foreach ($types as $type => $type_data) {
                if (isset($instance["show_{$type}"]) && $instance["show_{$type}"] && $instance["{$type}_count"] > 0) {
                    $options = ratingwidget()->GetOption($type_data["options"]);
                    $queries[$type] = array("rclasses" => $type_data["classes"], "votes" => max(1, (int) $instance["{$type}_min_votes"]), "orderby" => $instance["{$type}_orderby"], "order" => $instance["{$type}_order"], "limit" => (int) $instance["{$type}_count"], "types" => isset($options->type) ? $options->type : "star");
                    $since_created = isset($instance["{$type}_since_created"]) ? (int) $instance["{$type}_since_created"] : WP_RW__TIME_ALL_TIME;
                    // since_created should be at least 24 hours (86400 seconds), skip otherwise.
                    if ($since_created >= WP_RW__TIME_24_HOURS_IN_SEC) {
                        $time = current_time('timestamp', true) - $since_created;
                        // c: ISO 8601 full date/time, e.g.: 2004-02-12T15:19:21+00:00
                        $queries[$type]['since_created'] = date('c', $time);
                    }
                }
            }
            $details["queries"] = urlencode(json_encode($queries));
            $rw_ret_obj = ratingwidget()->RemoteCall("action/query/ratings.php", $details, WP_RW__CACHE_TIMEOUT_TOP_RATED);
            if (false === $rw_ret_obj) {
                return;
            }
            $rw_ret_obj = json_decode($rw_ret_obj);
            if (null === $rw_ret_obj || true !== $rw_ret_obj->success) {
                return;
            }
            $title = empty($instance['title']) ? __('Top Rated', WP_RW__ID) : apply_filters('widget_title', $instance['title']);
            $titleMaxLength = isset($instance['title_max_length']) && is_numeric($instance['title_max_length']) ? (int) $instance['title_max_length'] : 30;
            $empty = true;
            $toprated_data = new stdClass();
            $toprated_data->id = rand(1, 100);
            $toprated_data->title = array('label' => $title, 'show' => true, 'before' => $this->EncodeHtml($before_title), 'after' => $this->EncodeHtml($after_title));
            $toprated_data->options = array('align' => 'vertical', 'direction' => 'ltr', 'html' => array('before' => $this->EncodeHtml($before_widget), 'after' => $this->EncodeHtml($after_widget)));
            $toprated_data->site = array('id' => WP_RW__SITE_ID, 'domain' => $_SERVER['HTTP_HOST'], 'type' => 'WordPress');
            $toprated_data->itemGroups = array();
            if (count($rw_ret_obj->data) > 0) {
                foreach ($rw_ret_obj->data as $type => $ratings) {
                    if (is_array($ratings) && count($ratings) > 0) {
                        $item_group = new stdClass();
                        $item_group->type = $type;
                        $item_group->title = $instance["{$type}_title"];
                        $item_group->showTitle = 1 === $instance["show_{$type}_title"] && '' !== trim($item_group->title);
                        if (is_numeric($instance["{$type}_style"])) {
                            switch ($instance["{$type}_style"]) {
                                case 0:
                                    $instance["{$type}_style"] = 'legacy';
                                    break;
                                case 1:
                                default:
                                    $instance["{$type}_style"] = 'thumbs';
                                    break;
                            }
                        }
                        $item_group->style = $instance["{$type}_style"];
                        $item_group->options = array('title' => array('maxLen' => $titleMaxLength));
                        $item_group->items = array();
                        $has_thumb = strtolower($instance["{$type}_style"]) !== 'legacy';
                        $thumb_width = 160;
                        $thumb_height = 100;
                        if ($has_thumb) {
                            switch ($instance["{$type}_style"]) {
                                case '2':
                                case 'compact_thumbs':
                                    $thumb_width = 50;
                                    $thumb_height = 40;
                                    break;
                                case '1':
                                case 'thumbs':
                                default:
                                    $thumb_width = 160;
                                    $thumb_height = 100;
                                    break;
                            }
                            $item_group->options['thumb'] = array('width' => $thumb_width, 'height' => $thumb_height);
                        }
                        $cell = 0;
                        foreach ($ratings as $rating) {
                            $urid = $rating->urid;
                            $rclass = $types[$type]["rclass"];
                            $rclasses[$rclass] = true;
                            $extension_type = false;
                            if (RWLogger::IsOn()) {
                                RWLogger::Log('HANDLED_ITEM', 'Urid = ' . $urid . '; Class = ' . $rclass . ';');
                            }
                            if ('posts' === $type || 'pages' === $type) {
                                $post = null;
                                $id = RatingWidgetPlugin::Urid2PostId($urid);
                                $status = @get_post_status($id);
                                if (false === $status) {
                                    if (RWLogger::IsOn()) {
                                        RWLogger::Log('POST_NOT_EXIST', $id);
                                    }
                                    // Post not exist.
                                    continue;
                                } else {
                                    if ('publish' !== $status && 'private' !== $status) {
                                        if (RWLogger::IsOn()) {
                                            RWLogger::Log('POST_NOT_VISIBLE', 'status = ' . $status);
                                        }
                                        // Post not yet published.
                                        continue;
                                    } else {
                                        if ('private' === $status && !is_user_logged_in()) {
                                            if (RWLogger::IsOn()) {
                                                RWLogger::Log('RatingWidgetPlugin_TopRatedWidget::widget', 'POST_PRIVATE && USER_LOGGED_OUT');
                                            }
                                            // Private post but user is not logged in.
                                            continue;
                                        }
                                    }
                                }
                                $post = @get_post($id);
                                $title = trim(strip_tags($post->post_title));
                                $permalink = get_permalink($post->ID);
                            } else {
                                if ('comments' === $type) {
                                    $comment = null;
                                    $id = RatingWidgetPlugin::Urid2CommentId($urid);
                                    $status = @wp_get_comment_status($id);
                                    if (false === $status) {
                                        if (RWLogger::IsOn()) {
                                            RWLogger::Log('COMMENT_NOT_EXIST', $id);
                                        }
                                        // Comment not exist.
                                        continue;
                                    } else {
                                        if ('approved' !== $status) {
                                            if (RWLogger::IsOn()) {
                                                RWLogger::Log('COMMENT_NOT_VISIBLE', 'status = ' . $status);
                                            }
                                            // Comment not approved.
                                            continue;
                                        }
                                    }
                                    $comment = @get_comment($id);
                                    $title = trim(strip_tags($comment->comment_content));
                                    $permalink = get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID;
                                } else {
                                    if ('activity_updates' === $type || 'activity_comments' === $type) {
                                        $id = RatingWidgetPlugin::Urid2ActivityId($urid);
                                        $activity = new bp_activity_activity($id);
                                        if (!is_object($activity)) {
                                            if (RWLogger::IsOn()) {
                                                RWLogger::Log('BP_ACTIVITY_NOT_EXIST', $id);
                                            }
                                            // Activity not exist.
                                            continue;
                                        } else {
                                            if (!empty($activity->is_spam)) {
                                                if (RWLogger::IsOn()) {
                                                    RWLogger::Log('BP_ACTIVITY_NOT_VISIBLE (SPAM or TRASH)');
                                                }
                                                // Activity marked as SPAM or TRASH.
                                                continue;
                                            } else {
                                                if (!empty($activity->hide_sitewide)) {
                                                    if (RWLogger::IsOn()) {
                                                        RWLogger::Log('BP_ACTIVITY_HIDE_SITEWIDE');
                                                    }
                                                    // Activity marked as hidden in site.
                                                    continue;
                                                }
                                            }
                                        }
                                        $title = trim(strip_tags($activity->content));
                                        $permalink = bp_activity_get_permalink($id);
                                    } else {
                                        if ('users' === $type) {
                                            $id = RatingWidgetPlugin::Urid2UserId($urid);
                                            if ($bpInstalled) {
                                                $title = trim(strip_tags(bp_core_get_user_displayname($id)));
                                                $permalink = bp_core_get_user_domain($id);
                                            } else {
                                                if ($bbInstalled) {
                                                    $title = trim(strip_tags(bbp_get_user_display_name($id)));
                                                    $permalink = bbp_get_user_profile_url($id);
                                                } else {
                                                    continue;
                                                }
                                            }
                                        } else {
                                            if ('forum_posts' === $type || 'forum_replies' === $type) {
                                                $id = RatingWidgetPlugin::Urid2ForumPostId($urid);
                                                if (function_exists('bp_forums_get_post')) {
                                                    $forum_post = @bp_forums_get_post($id);
                                                    if (!is_object($forum_post)) {
                                                        continue;
                                                    }
                                                    $title = trim(strip_tags($forum_post->post_text));
                                                    $page = bb_get_page_number($forum_post->post_position);
                                                    $permalink = get_topic_link($id, $page) . "#post-{$id}";
                                                } else {
                                                    if (function_exists('bbp_get_reply_id')) {
                                                        $forum_item = bbp_get_topic();
                                                        if (is_object($forum_item)) {
                                                            $is_topic = true;
                                                        } else {
                                                            $is_topic = false;
                                                            $forum_item = bbp_get_reply($id);
                                                            if (!is_object($forum_item)) {
                                                                if (RWLogger::IsOn()) {
                                                                    RWLogger::Log('BBP_FORUM_ITEM_NOT_EXIST', $id);
                                                                }
                                                                // Invalid id (no topic nor reply).
                                                                continue;
                                                            }
                                                            if (RWLogger::IsOn()) {
                                                                RWLogger::Log('BBP_IS_TOPIC_REPLY', $is_topic ? 'FALSE' : 'TRUE');
                                                            }
                                                        }
                                                        // Visible statueses: Public or Closed.
                                                        $visible_statuses = array(bbp_get_public_status_id(), bbp_get_closed_status_id());
                                                        if (!in_array($forum_item->post_status, $visible_statuses)) {
                                                            if (RWLogger::IsOn()) {
                                                                RWLogger::Log('BBP_FORUM_ITEM_HIDDEN', $forum_item->post_status);
                                                            }
                                                            // Item is not public nor closed.
                                                            continue;
                                                        }
                                                        $is_reply = !$is_topic;
                                                        if ($is_reply) {
                                                            // Get parent topic.
                                                            $forum_topic = bbp_get_topic($forum_post->post_parent);
                                                            if (!in_array($forum_topic->post_status, $visible_statuses)) {
                                                                if (RWLogger::IsOn()) {
                                                                    RWLogger::Log('BBP_PARENT_FORUM_TOPIC_IS_HIDDEN', 'TRUE');
                                                                }
                                                                // Parent topic is not public nor closed.
                                                                continue;
                                                            }
                                                        }
                                                        $title = trim(strip_tags($forum_post->post_title));
                                                        $permalink = get_permalink($forum_post->ID);
                                                    } else {
                                                        continue;
                                                    }
                                                }
                                                $types[$type]['handler']->GetElementInfoByRating();
                                            } else {
                                                $found_handler = false;
                                                $extensions = ratingwidget()->GetExtensions();
                                                foreach ($extensions as $ext) {
                                                    $result = $ext->GetElementInfoByRating($type, $rating);
                                                    if (false !== $result) {
                                                        $found_handler = true;
                                                        break;
                                                    }
                                                }
                                                if ($found_handler) {
                                                    $id = $result['id'];
                                                    $title = $result['title'];
                                                    $permalink = $result['permalink'];
                                                    $img = rw_get_thumb_url($result['img'], $thumb_width, $thumb_height, $result['permalink']);
                                                    $extension_type = true;
                                                } else {
                                                    continue;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            $queued = ratingwidget()->QueueRatingData($urid, "", "", $rclass);
                            // Override rating class in case the same rating has already been queued with a different rclass.
                            $rclass = $queued['rclass'];
                            $short = mb_strlen($title) > $titleMaxLength ? trim(mb_substr($title, 0, $titleMaxLength)) . "..." : $title;
                            $item = array('site' => array('id' => WP_RW__SITE_ID, 'domain' => $_SERVER['HTTP_HOST']), 'page' => array('externalID' => $id, 'url' => $permalink, 'title' => $short), 'rating' => array('localID' => $urid, 'options' => array('rclass' => $rclass)));
                            // Add thumb url.
                            if ($extension_type && is_string($img)) {
                                $item['page']['img'] = $img;
                            } else {
                                if ($has_thumb && in_array($type, array('posts', 'pages'))) {
                                    $item['page']['img'] = rw_get_post_thumb_url($post, $thumb_width, $thumb_height);
                                }
                            }
                            $item_group->items[] = $item;
                            $cell++;
                            $empty = false;
                        }
                        $toprated_data->itemGroups[] = $item_group;
                    }
                }
            }
            if (true === $empty) {
                //            echo '<p style="margin: 0;">There are no rated items for this period.</p>';
                //        echo $before_widget;
                //        echo $after_widget;
            } else {
                // Set a flag that the widget is loaded.
                ratingwidget()->TopRatedWidgetLoaded();
                ?>
					<b class="rw-ui-recommendations" data-id="<?php 
                echo $toprated_data->id;
                ?>
"></b>
					<script type="text/javascript">
						var _rwq = _rwq || [];
						_rwq.push(['_setRecommendations', <?php 
                echo json_encode($toprated_data);
                ?>
]);
					</script>
				<?php 
            }
        }
    /**
     * Prints an admin notice when a topic has been (un)reported
     *
     * @since 1.0.0
     *
     * @return null
     */
    public function toggle_topic_notice_admin()
    {
        // Bail if we're not editing topics
        if (bbp_get_topic_post_type() != get_current_screen()->post_type) {
            return;
        }
        // Only proceed if GET is a topic toggle action
        if (bbp_is_get_request() && !empty($_GET['bbp_topic_toggle_notice']) && in_array($_GET['bbp_topic_toggle_notice'], array('unreported')) && !empty($_GET['topic_id'])) {
            $notice = $_GET['bbp_topic_toggle_notice'];
            // Which notice?
            $topic_id = (int) $_GET['topic_id'];
            // What's the topic id?
            $is_failure = !empty($_GET['failed']) ? true : false;
            // Was that a failure?
            // Bails if no topic_id or notice
            if (empty($notice) || empty($topic_id)) {
                return;
            }
            // Bail if topic is missing
            $topic = bbp_get_topic($topic_id);
            if (empty($topic)) {
                return;
            }
            $topic_title = bbp_get_topic_title($topic->ID);
            switch ($notice) {
                case 'unreported':
                    $message = $is_failure === true ? sprintf(__('There was a problem unreporting the topic "%1$s".', 'bbpress-report-content'), $topic_title) : sprintf(__('Topic "%1$s" successfully unreported.', 'bbpress-report-content'), $topic_title);
                    break;
            }
            ?>

			<div id="message" class="<?php 
            echo $is_failure === true ? 'error' : 'updated';
            ?>
 fade">
				<p style="line-height: 150%"><?php 
            echo esc_html($message);
            ?>
</p>
			</div>

			<?php 
        }
    }
Ejemplo n.º 4
0
 /**
  * Add bbPress top left & right ratings.
  * Invoked on bbp_theme_before_reply_admin_links.
  */
 public function AddBBPressTopLeftOrRightRating()
 {
     if (RWLogger::IsOn()) {
         $params = func_get_args();
         RWLogger::LogEnterence('AddBBPressTopLeftOrRightRating', $params);
     }
     $forum_item = bbp_get_reply(bbp_get_reply_id());
     $is_reply = is_object($forum_item);
     if (!$is_reply) {
         $forum_item = bbp_get_topic(bbp_get_topic_id());
     }
     $class = $is_reply ? 'forum-reply' : 'forum-post';
     if (RWLogger::IsOn()) {
         RWLogger::Log('AddBBPressTopLeftOrRightRating', $class . ': ' . var_export($forum_item, true));
     }
     $ratingHtml = $this->EmbedRatingIfVisibleByPost($forum_item, $class, false, 'f' . $this->forum_post_align->hor, 'display: inline; margin-' . ('left' === $this->forum_post_align->hor ? 'right' : 'left') . ': 10px;');
     echo $ratingHtml;
 }
Ejemplo n.º 5
0
    /**
     * Process the user's bbPress topic selections when the post is saved
     */
    function process_topic_option($post_ID, $post)
    {
        /** Don't process on AJAX-based auto-drafts */
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
        /** Don't process on initial page load auto drafts */
        if ($post->post_status == 'auto-draft') {
            return;
        }
        /** Only process for post types we specify */
        if (!in_array($post->post_type, apply_filters('bbppt_eligible_post_types', array('post', 'page')))) {
            return;
        }
        // TODO: combine existing topic and draft settings branches
        if (isset($_POST['bbpress_topic'])) {
            /**
             * Post was created through the full post editor form
             * Check the POST for settings
             */
            if (!empty($_POST['bbpress_topic']['enabled'])) {
                $bbppt_options = $_POST['bbpress_topic'];
                $create_topic = true;
                $use_defaults = isset($bbppt_options['use_defaults']);
                bbppt_debug('Processing a topic for regular post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
                bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
            } else {
                $create_topic = false;
                $bbppt_options = array();
                bbppt_debug('NOT creating a topic for regular post ' . $post_ID);
            }
        } else {
            if (get_post_meta($post_ID, 'bbpress_discussion_topic_id', true)) {
                /**
                 * If post has an existing topic, use its settings
                 */
                $bbppt_options = $this->get_topic_options_for_post($post_ID);
                $create_topic = !empty($bbppt_options['enabled']);
                $use_defaults = !empty($bbppt_options['use_defaults']);
                bbppt_debug('Processing topic for existing post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
                bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
            } else {
                if ($this->has_draft_settings($post)) {
                    /**
                     * If the post has draft settings saved, use draft settings
                     */
                    $bbppt_options = $this->get_draft_settings($post);
                    $create_topic = !empty($bbppt_options['enabled']);
                    $use_defaults = !empty($bbppt_options['use_defaults']);
                    bbppt_debug('Processing a topic for draft post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
                    bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
                } else {
                    /**
                     * Post was created through some other means (including XML-RPC) - use defaults
                     */
                    $bbppt_options = get_option('bbpress_discussion_defaults');
                    $create_topic = !empty($bbppt_options['enabled']);
                    $use_defaults = true;
                    $bbppt_options['use_defaults'] = $use_defaults;
                    bbppt_debug('Processing a topic for unattended post ' . $post_ID . ' with the following settings: ' . print_r($bbppt_options, true));
                    bbppt_debug('Creating topic?: ' . $create_topic . '; using defaults?: ' . $use_defaults);
                }
            }
        }
        /** If post is saved as a draft, store selected settings for publication later */
        if (in_array($post->post_status, apply_filters('bbppt_draft_post_status', array('draft', 'future')))) {
            $this->set_draft_settings($bbppt_options, $post);
            return;
        }
        /** Only process further when the post is published */
        if (!in_array($post->post_status, apply_filters('bbppt_eligible_post_status', array('publish', 'private')))) {
            return;
        }
        /**
         * The user requested to use a bbPress topic for discussion
         */
        if ($create_topic) {
            if (!function_exists('bbp_has_forums')) {
                ?>
<br /><p><?php 
                _e('bbPress Topics for Posts cannot process this request because it cannot detect your bbPress setup.', 'bbpress-post-topics');
                ?>
</p><?php 
                return;
            }
            $topic_slug = isset($bbppt_options['slug']) ? $bbppt_options['slug'] : '';
            $topic_forum = isset($bbppt_options['forum_id']) ? (int) $bbppt_options['forum_id'] : 0;
            if (!$use_defaults) {
                $topic_display = isset($bbppt_options['display']) ? $bbppt_options['display'] : 'topic';
                /** Store extra data for xreplies, as well as any custom display formats */
                $topic_display_extras = apply_filters('bbppt_store_display_extras', $bbppt_options['display-extras'], $post);
            }
            if (!empty($topic_slug)) {
                /** if user has selected an existing topic */
                if (is_numeric($topic_slug)) {
                    $topic = bbp_get_topic((int) $topic_slug);
                } else {
                    $topic = bbppt_get_topic_by_slug($topic_slug);
                }
                if ($topic == null) {
                    // return an error of some kind
                    wp_die(__('There was an error with your selected topic.', 'bbpress-post-topics'), __('Error Locating bbPress Topic', 'bbpress-post-topics'));
                } else {
                    $topic_ID = $topic->ID;
                    update_post_meta($post_ID, 'use_bbpress_discussion_topic', true);
                    update_post_meta($post_ID, 'bbpress_discussion_topic_id', $topic_ID);
                    /** Update topic with tags from the post */
                    if (!empty($bbppt_options['copy_tags'])) {
                        $post_tags = wp_get_post_tags($post_ID);
                        $post_tags = array_map(create_function('$term', 'return $term->name;'), $post_tags);
                        wp_set_post_terms($topic_ID, join(',', $post_tags), bbp_get_topic_tag_tax_id(), true);
                        update_post_meta($post_ID, 'bbpress_discussion_tags_copied', time());
                    }
                    /** Export comments from the post to the new bbPress topic */
                    if (!empty($bbppt_options['copy_comments'])) {
                        bbppt_import_comments($post_ID, $topic_ID);
                        update_post_meta($post_ID, 'bbpress_discussion_comments_copied', time());
                    }
                    if ($use_defaults) {
                        update_post_meta($post_ID, 'bbpress_discussion_use_defaults', true);
                    } else {
                        delete_post_meta($post_ID, 'bbpress_discussion_use_defaults');
                        update_post_meta($post_ID, 'bbpress_discussion_display_format', $topic_display);
                        update_post_meta($post_ID, 'bbpress_discussion_display_extras', $topic_display_extras);
                    }
                }
                do_action('bbppt_topic_assigned', $post_ID, $topic_ID);
            } else {
                if ($topic_forum != 0) {
                    /** if user has opted to create a new topic */
                    $topic_ID = $this->build_new_topic($post, $topic_forum);
                    if (!$topic_ID) {
                        // return an error of some kind
                        wp_die(__('There was an error creating a new topic.', 'bbpress-post-topics'), __('Error Creating bbPress Topic', 'bbpress-post-topics'));
                    } else {
                        update_post_meta($post_ID, 'use_bbpress_discussion_topic', true);
                        update_post_meta($post_ID, 'bbpress_discussion_topic_id', $topic_ID);
                        /** Update topic with tags from the post */
                        if ($bbppt_options['copy_tags']) {
                            $post_tags = wp_get_post_tags($post_ID);
                            $post_tags = array_map(create_function('$term', 'return $term->name;'), $post_tags);
                            wp_set_post_terms($topic_ID, join(',', $post_tags), bbp_get_topic_tag_tax_id(), false);
                            update_post_meta($post_ID, 'bbpress_discussion_tags_copied', time());
                        }
                        /** Export comments from the post to the new bbPress topic */
                        if ($bbppt_options['copy_comments']) {
                            bbppt_import_comments($post_ID, $topic_ID);
                            update_post_meta($post_ID, 'bbpress_discussion_comments_copied', time());
                        }
                        if ($use_defaults) {
                            update_post_meta($post_ID, 'bbpress_discussion_use_defaults', true);
                        } else {
                            update_post_meta($post_ID, 'bbpress_discussion_display_format', $topic_display);
                            update_post_meta($post_ID, 'bbpress_discussion_display_extras', $topic_display_extras);
                        }
                    }
                    do_action('bbppt_topic_created', $post_ID, $topic_ID);
                }
            }
        } else {
            delete_post_meta($post_ID, 'use_bbpress_discussion_topic');
            delete_post_meta($post_ID, 'bbpress_discussion_topic_id');
            delete_post_meta($post_ID, 'bbpress_discussion_use_defaults');
            delete_post_meta($post_ID, 'bbpress_discussion_display_format');
            delete_post_meta($post_ID, 'bbpress_discussion_display_extras');
            delete_post_meta($post_ID, 'bbpress_discussion_tags_copied');
            delete_post_meta($post_ID, 'bbpress_discussion_comments_copied');
        }
        $this->delete_draft_settings($post);
        do_action('bbppt_topic_associated', $post_ID, $topic_ID);
    }
Ejemplo n.º 6
0
/**
 * Unspams a topic
 *
 * @since bbPress (r2740)
 *
 * @param int $topic_id Topic id
 * @uses bbp_get_topic() To get the topic
 * @uses do_action() Calls 'bbp_unspam_topic' with the topic id
 * @uses get_post_meta() To get the previous status
 * @uses delete_post_meta() To delete the previous status meta
 * @uses wp_update_post() To update the topic with the new status
 * @uses do_action() Calls 'bbp_unspammed_topic' with the topic id
 * @return mixed False or {@link WP_Error} on failure, topic id on success
 */
function bbp_unspam_topic($topic_id = 0)
{
    // Get the topic
    $topic = bbp_get_topic($topic_id);
    if (empty($topic)) {
        return $topic;
    }
    // Bail if already not spam
    if (bbp_get_spam_status_id() !== $topic->post_status) {
        return false;
    }
    // Execute pre unspam code
    do_action('bbp_unspam_topic', $topic_id);
    /** Untrash Replies *******************************************************/
    // Get the replies that were not previously trashed
    $pre_spammed_replies = get_post_meta($topic_id, '_bbp_pre_spammed_replies', true);
    // There are replies to untrash
    if (!empty($pre_spammed_replies)) {
        // Maybe reverse the trashed replies array
        if (is_array($pre_spammed_replies)) {
            $pre_spammed_replies = array_reverse($pre_spammed_replies);
        }
        // Loop through replies
        foreach ((array) $pre_spammed_replies as $reply) {
            wp_untrash_post($reply);
        }
    }
    /** Topic Tags ************************************************************/
    // Get pre-spam topic tags
    $terms = get_post_meta($topic_id, '_bbp_spam_topic_tags', true);
    // Topic had tags before it was spammed
    if (!empty($terms)) {
        // Set the tax_input of the topic
        $topic->tax_input = array(bbp_get_topic_tag_tax_id() => $terms);
        // Delete pre-spam topic tag meta
        delete_post_meta($topic_id, '_bbp_spam_topic_tags');
    }
    /** Topic Status **********************************************************/
    // Get pre spam status
    $topic_status = get_post_meta($topic_id, '_bbp_spam_meta_status', true);
    // If no previous status, default to publish
    if (empty($topic_status)) {
        $topic_status = bbp_get_public_status_id();
    }
    // Set post status to pre spam
    $topic->post_status = $topic_status;
    // Delete pre spam meta
    delete_post_meta($topic_id, '_bbp_spam_meta_status');
    // No revisions
    remove_action('pre_post_update', 'wp_save_post_revision');
    // Update the topic
    $topic_id = wp_update_post($topic);
    // Execute post unspam code
    do_action('bbp_unspammed_topic', $topic_id);
    // Return topic_id
    return $topic_id;
}
Ejemplo n.º 7
0
/**
 * Move reply handler
 *
 * Handles the front end move reply submission
 *
 * @since bbPress (r4521)
 *
 * @param string $action The requested action to compare this function to
 * @uses bbp_add_error() To add an error message
 * @uses bbp_get_reply() To get the reply
 * @uses bbp_get_topic() To get the topics
 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
 * @uses current_user_can() To check if the current user can edit the reply and topics
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
 * @uses do_action() Calls 'bbp_pre_move_reply' with the from reply id, source
 *                    and destination topic ids
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses wpdb::prepare() To prepare our sql query
 * @uses wpdb::get_results() To execute the sql query and get results
 * @uses wp_update_post() To update the replies
 * @uses bbp_update_reply_topic_id() To update the reply topic id
 * @uses bbp_get_topic_forum_id() To get the topic forum id
 * @uses bbp_update_reply_forum_id() To update the reply forum id
 * @uses do_action() Calls 'bbp_split_topic_reply' with the reply id and
 *                    destination topic id
 * @uses bbp_update_topic_last_reply_id() To update the topic last reply id
 * @uses bbp_update_topic_last_active_time() To update the topic last active meta
 * @uses do_action() Calls 'bbp_post_split_topic' with the destination and
 *                    source topic ids and source topic's forum id
 * @uses bbp_get_topic_permalink() To get the topic permalink
 * @uses wp_safe_redirect() To redirect to the topic link
 */
function bbp_move_reply_handler($action = '')
{
    // Bail if action is not 'bbp-move-reply'
    if ('bbp-move-reply' !== $action) {
        return;
    }
    // Prevent debug notices
    $move_reply_id = $destination_topic_id = 0;
    $destination_topic_title = '';
    $destination_topic = $move_reply = $source_topic = '';
    /** Move Reply ***********************************************************/
    if (empty($_POST['bbp_reply_id'])) {
        bbp_add_error('bbp_move_reply_reply_id', __('<strong>ERROR</strong>: Reply ID to move not found!', 'bbpress'));
    } else {
        $move_reply_id = (int) $_POST['bbp_reply_id'];
    }
    $move_reply = bbp_get_reply($move_reply_id);
    // Reply exists
    if (empty($move_reply)) {
        bbp_add_error('bbp_mover_reply_r_not_found', __('<strong>ERROR</strong>: The reply you want to move was not found.', 'bbpress'));
    }
    /** Topic to Move From ***************************************************/
    // Get the reply's current topic
    $source_topic = bbp_get_topic($move_reply->post_parent);
    // No topic
    if (empty($source_topic)) {
        bbp_add_error('bbp_move_reply_source_not_found', __('<strong>ERROR</strong>: The topic you want to move from was not found.', 'bbpress'));
    }
    // Nonce check failed
    if (!bbp_verify_nonce_request('bbp-move-reply_' . $move_reply->ID)) {
        bbp_add_error('bbp_move_reply_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress'));
        return;
    }
    // Use cannot edit topic
    if (!current_user_can('edit_topic', $source_topic->ID)) {
        bbp_add_error('bbp_move_reply_source_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress'));
    }
    // How to move
    if (!empty($_POST['bbp_reply_move_option'])) {
        $move_option = (string) trim($_POST['bbp_reply_move_option']);
    }
    // Invalid move option
    if (empty($move_option) || !in_array($move_option, array('existing', 'topic'))) {
        bbp_add_error('bbp_move_reply_option', __('<strong>ERROR</strong>: You need to choose a valid move option.', 'bbpress'));
        // Valid move option
    } else {
        // What kind of move
        switch ($move_option) {
            // Into an existing topic
            case 'existing':
                // Get destination topic id
                if (empty($_POST['bbp_destination_topic'])) {
                    bbp_add_error('bbp_move_reply_destination_id', __('<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress'));
                } else {
                    $destination_topic_id = (int) $_POST['bbp_destination_topic'];
                }
                // Get the destination topic
                $destination_topic = bbp_get_topic($destination_topic_id);
                // No destination topic
                if (empty($destination_topic)) {
                    bbp_add_error('bbp_move_reply_destination_not_found', __('<strong>ERROR</strong>: The topic you want to move to was not found!', 'bbpress'));
                }
                // User cannot edit the destination topic
                if (!current_user_can('edit_topic', $destination_topic->ID)) {
                    bbp_add_error('bbp_move_reply_destination_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress'));
                }
                // Bump the reply position
                $reply_position = bbp_get_topic_reply_count($destination_topic->ID) + 1;
                // Update the reply
                wp_update_post(array('ID' => $move_reply->ID, 'post_title' => sprintf(__('Reply To: %s', 'bbpress'), $destination_topic->post_title), 'post_name' => false, 'post_parent' => $destination_topic->ID, 'menu_order' => $reply_position, 'guid' => ''));
                // Adjust reply meta values
                bbp_update_reply_topic_id($move_reply->ID, $destination_topic->ID);
                bbp_update_reply_forum_id($move_reply->ID, bbp_get_topic_forum_id($destination_topic->ID));
                break;
                // Move reply to a new topic
            // Move reply to a new topic
            case 'topic':
            default:
                // User needs to be able to publish topics
                if (current_user_can('publish_topics')) {
                    // Use the new title that was passed
                    if (!empty($_POST['bbp_reply_move_destination_title'])) {
                        $destination_topic_title = esc_attr(strip_tags($_POST['bbp_reply_move_destination_title']));
                        // Use the source topic title
                    } else {
                        $destination_topic_title = $source_topic->post_title;
                    }
                    // Update the topic
                    $destination_topic_id = wp_update_post(array('ID' => $move_reply->ID, 'post_title' => $destination_topic_title, 'post_name' => false, 'post_type' => bbp_get_topic_post_type(), 'post_parent' => $source_topic->post_parent, 'guid' => ''));
                    $destination_topic = bbp_get_topic($destination_topic_id);
                    // Make sure the new topic knows its a topic
                    bbp_update_topic_topic_id($move_reply->ID);
                    // Shouldn't happen
                    if (false === $destination_topic_id || is_wp_error($destination_topic_id) || empty($destination_topic)) {
                        bbp_add_error('bbp_move_reply_destination_reply', __('<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress'));
                    }
                    // User cannot publish posts
                } else {
                    bbp_add_error('bbp_move_reply_destination_permission', __('<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', 'bbpress'));
                }
                break;
        }
    }
    // Bail if there are errors
    if (bbp_has_errors()) {
        return;
    }
    /** No Errors - Clean Up **************************************************/
    // Update counts, etc...
    do_action('bbp_pre_move_reply', $move_reply->ID, $source_topic->ID, $destination_topic->ID);
    /** Date Check ************************************************************/
    // Check if the destination topic is older than the move reply
    if (strtotime($move_reply->post_date) < strtotime($destination_topic->post_date)) {
        // Set destination topic post_date to 1 second before from reply
        $destination_post_date = date('Y-m-d H:i:s', strtotime($move_reply->post_date) - 1);
        // Update destination topic
        wp_update_post(array('ID' => $destination_topic_id, 'post_date' => $destination_post_date, 'post_date_gmt' => get_gmt_from_date($destination_post_date)));
    }
    // Set the last reply ID and freshness to the move_reply
    $last_reply_id = $move_reply->ID;
    $freshness = $move_reply->post_date;
    // Get the reply to
    $parent = bbp_get_reply_to($move_reply->ID);
    // Fix orphaned children
    $children = get_posts(array('post_type' => bbp_get_reply_post_type(), 'meta_key' => '_bbp_reply_to', 'meta_value' => $move_reply->ID));
    foreach ($children as $child) {
        bbp_update_reply_to($child->ID, $parent);
    }
    // Remove reply_to from moved reply
    delete_post_meta($move_reply->ID, '_bbp_reply_to');
    // It is a new topic and we need to set some default metas to make
    // the topic display in bbp_has_topics() list
    if ('topic' === $move_option) {
        bbp_update_topic_last_reply_id($destination_topic->ID, $last_reply_id);
        bbp_update_topic_last_active_id($destination_topic->ID, $last_reply_id);
        bbp_update_topic_last_active_time($destination_topic->ID, $freshness);
        // Otherwise update the existing destination topic
    } else {
        bbp_update_topic_last_reply_id($destination_topic->ID);
        bbp_update_topic_last_active_id($destination_topic->ID);
        bbp_update_topic_last_active_time($destination_topic->ID);
    }
    // Update source topic ID last active
    bbp_update_topic_last_reply_id($source_topic->ID);
    bbp_update_topic_last_active_id($source_topic->ID);
    bbp_update_topic_last_active_time($source_topic->ID);
    /** Successful Move ******************************************************/
    // Update counts, etc...
    do_action('bbp_post_move_reply', $move_reply->ID, $source_topic->ID, $destination_topic->ID);
    // Redirect back to the topic
    wp_safe_redirect(bbp_get_topic_permalink($destination_topic->ID));
    // For good measure
    exit;
}
Ejemplo n.º 8
0
/**
 * Return the spam link of the topic
 *
 * @since bbPress (r2727)
 *
 * @param mixed $args This function supports these args:
 *  - id: Optional. Topic id
 *  - link_before: Before the link
 *  - link_after: After the link
 *  - spam_text: Spam text
 *  - unspam_text: Unspam text
 * @uses bbp_get_topic_id() To get the topic id
 * @uses bbp_get_topic() To get the topic
 * @uses current_user_can() To check if the current user can edit the topic
 * @uses bbp_is_topic_spam() To check if the topic is marked as spam
 * @uses add_query_arg() To add custom args to the url
 * @uses wp_nonce_url() To nonce the url
 * @uses esc_url() To escape the url
 * @uses apply_filters() Calls 'bbp_get_topic_spam_link' with the link
 *                        and args
 * @return string Topic spam link
 */
function bbp_get_topic_spam_link($args = '')
{
    $defaults = array('id' => 0, 'link_before' => '', 'link_after' => '', 'sep' => ' | ', 'spam_text' => __('Spam', 'bbpress'), 'unspam_text' => __('Unspam', 'bbpress'));
    $r = bbp_parse_args($args, $defaults, 'get_topic_spam_link');
    extract($r);
    $topic = bbp_get_topic(bbp_get_topic_id((int) $id));
    if (empty($topic) || !current_user_can('moderate', $topic->ID)) {
        return;
    }
    $display = bbp_is_topic_spam($topic->ID) ? $unspam_text : $spam_text;
    $uri = add_query_arg(array('action' => 'bbp_toggle_topic_spam', 'topic_id' => $topic->ID));
    $uri = esc_url(wp_nonce_url($uri, 'spam-topic_' . $topic->ID));
    $retval = $link_before . '<a href="' . $uri . '">' . $display . '</a>' . $link_after;
    return apply_filters('bbp_get_topic_spam_link', $retval, $args);
}
Ejemplo n.º 9
0
/**
 * Unapproves a topic
 *
 * @since 2.6.0 bbPress (r5503)
 *
 * @param int $topic_id Topic id
 * @uses bbp_get_topic() To get the topic
 * @uses bbp_get_pending_status_id() To get the pending status id
 * @uses do_action() Calls 'bbp_unapprove_topic' with the topic id
 * @uses remove_action() To remove the auto save post revision action
 * @uses wp_update_post() To update the topic with the new status
 * @uses do_action() Calls 'bbp_unapproved_topic' with the topic id
 * @return mixed False or {@link WP_Error} on failure, topic id on success
 */
function bbp_unapprove_topic($topic_id = 0)
{
    // Get topic
    $topic = bbp_get_topic($topic_id);
    if (empty($topic)) {
        return $topic;
    }
    // Bail if already pending
    if (bbp_get_pending_status_id() === $topic->post_status) {
        return false;
    }
    // Execute pre open code
    do_action('bbp_unapprove_topic', $topic_id);
    // Set pending status
    $topic->post_status = bbp_get_pending_status_id();
    // No revisions
    remove_action('pre_post_update', 'wp_save_post_revision');
    // Update topic
    $topic_id = wp_update_post($topic);
    // Execute post open code
    do_action('bbp_unapproved_topic', $topic_id);
    // Return topic_id
    return $topic_id;
}
Ejemplo n.º 10
0
function likebtn_bbp_reply($wrap = true, $position = LIKEBTN_POSITION_BOTH, $alignment = '', $content = '')
{
    // Reply to topic
    $entity = bbp_get_reply(bbp_get_reply_id());
    // Topic
    if (!is_object($entity)) {
        $entity = bbp_get_topic(bbp_get_topic_id());
    }
    if (!$entity) {
        return false;
    }
    return _likebtn_get_content_universal(LIKEBTN_ENTITY_BBP_POST, $entity->ID, $content, $wrap, $position, $alignment);
}
Ejemplo n.º 11
0
/**
 * Add a topic to user's subscriptions
 *
 * @since 2.0.0 bbPress (r2668)
 *
 * @param int $user_id Optional. User id
 * @param int $topic_id Optional. Topic id
 * @uses bbp_get_topic() To get the topic
 * @uses bbp_add_user_subscription() To add the subscription
 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
 * @return bool Always true
 */
function bbp_add_user_topic_subscription($user_id = 0, $topic_id = 0)
{
    // Bail if not enough info
    if (empty($user_id) || empty($topic_id)) {
        return false;
    }
    // Bail if no topic
    $topic = bbp_get_topic($topic_id);
    if (empty($topic)) {
        return false;
    }
    // Bail if already subscribed
    if (bbp_is_user_subscribed_to_topic($user_id, $topic_id)) {
        return false;
    }
    // Bail if add fails
    if (!bbp_add_user_subscription($user_id, $topic_id)) {
        return false;
    }
    do_action('bbp_add_user_topic_subscription', $user_id, $topic_id);
    return true;
}
/**
 * Send a Pushover Notification when a moderator is assigned to a topic
 */
function wi_bbp_send_pushover_notification_on_assignment()
{
    if (isset($_POST['bbps_support_topic_assign'])) {
        if (!function_exists('ckpn_send_notification')) {
            return;
        }
        $user_id = absint($_POST['bbps_assign_list']);
        $topic = bbp_get_topic($_POST['bbps_topic_id']);
        if ($user_id > 0 && $user_id != get_current_user_id()) {
            $title = __('Easy Digital Downloads: A forum topic has been assigned to you', 'wi_bbp');
            $message = sprintf(__('You have been assigned to %1$s by another moderator', 'wi_bbp'), $topic->post_title);
            $user_push_key = get_user_meta($user_id, 'ckpn_user_key', true);
            if ($user_push_key) {
                $url = $topic->guid;
                $url_title = __('View Topic', 'wi_bbp');
                $args = array('title' => $title, 'message' => $message, 'user' => $user_push_key, 'url' => $url, 'url_title' => $url_title);
                ckpn_send_notification($args);
            }
        }
    }
}
Ejemplo n.º 13
0
 static function Add_fb_link_reply($reply)
 {
     // Get data
     $topic_id = bbp_get_reply_topic_id($reply->ID);
     $topic = bbp_get_topic($topic_id);
     $link_id = get_post_meta($topic->ID, c_al2fb_meta_link_id, true);
     if (empty($link_id)) {
         return;
     }
     if (get_post_meta($topic->ID, c_al2fb_meta_nointegrate, true)) {
         return;
     }
     $user_ID = WPAL2Facebook::Get_user_ID($topic);
     if (!get_user_meta($user_ID, c_al2fb_meta_fb_comments_postback, true)) {
         return;
     }
     // Build message
     $message = bbp_get_reply_author($reply->ID) . ' ' . __('commented on', c_al2fb_text_domain) . ' ';
     $message .= html_entity_decode(get_bloginfo('title'), ENT_QUOTES, get_bloginfo('charset')) . ":\n\n";
     $message .= $reply->post_content;
     $message = apply_filters('al2fb_reply', $message, $reply_id);
     // Do not disturb WordPress
     try {
         $url = 'https://graph.facebook.com/v2.2/' . $link_id . '/comments';
         $url = apply_filters('al2fb_url', $url);
         $query_array = array('access_token' => WPAL2Int::Get_access_token_by_post($topic), 'message' => $message);
         // http://developers.facebook.com/docs/reference/api/Comment/
         $query = http_build_query($query_array, '', '&');
         // Execute request
         $response = WPAL2Int::Request($url, $query, 'POST');
         // Process response
         $fb_comment = json_decode($response);
         add_post_meta($topic->ID, c_al2fb_meta_fb_comment_id, $fb_comment->id);
         // Remove previous errors
         $error = get_post_meta($topic->ID, c_al2fb_meta_error, true);
         if (strpos($error, 'Add reply: ') !== false) {
             delete_post_meta($topic->ID, c_al2fb_meta_error, $error);
             delete_post_meta($topic->ID, c_al2fb_meta_error_time);
         }
     } catch (Exception $e) {
         update_post_meta($topic->ID, c_al2fb_meta_error, 'Add reply: ' . $e->getMessage());
         update_post_meta($topic->ID, c_al2fb_meta_error_time, date('c'));
     }
 }
Ejemplo n.º 14
0
 public function get_object_by_id($topic_id)
 {
     return bbp_get_topic($topic_id);
 }
Ejemplo n.º 15
0
 /**
  * AJAX handler to Subscribe/Unsubscribe a user from a topic
  *
  * @since bbPress (r3732)
  *
  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  * @uses bbp_is_user_logged_in() To check if user is logged in
  * @uses bbp_get_current_user_id() To get the current user id
  * @uses current_user_can() To check if the current user can edit the user
  * @uses bbp_get_topic() To get the topic
  * @uses wp_verify_nonce() To verify the nonce
  * @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions
  * @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions
  * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
  * @uses bbp_ajax_response() To return JSON
  */
 public function ajax_subscription()
 {
     // Bail if subscriptions are not active
     if (!bbp_is_subscriptions_active()) {
         bbp_ajax_response(false, __('Subscriptions are no longer active.', 'bbpress'), 300);
     }
     // Bail if user is not logged in
     if (!is_user_logged_in()) {
         bbp_ajax_response(false, __('Please login to subscribe to this topic.', 'bbpress'), 301);
     }
     // Get user and topic data
     $user_id = bbp_get_current_user_id();
     $id = intval($_POST['id']);
     // Bail if user cannot add favorites for this user
     if (!current_user_can('edit_user', $user_id)) {
         bbp_ajax_response(false, __('You do not have permission to do this.', 'bbpress'), 302);
     }
     // Get the topic
     $topic = bbp_get_topic($id);
     // Bail if topic cannot be found
     if (empty($topic)) {
         bbp_ajax_response(false, __('The topic could not be found.', 'bbpress'), 303);
     }
     // Bail if user did not take this action
     if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'toggle-subscription_' . $topic->ID)) {
         bbp_ajax_response(false, __('Are you sure you meant to do that?', 'bbpress'), 304);
     }
     // Take action
     $status = bbp_is_user_subscribed($user_id, $topic->ID) ? bbp_remove_user_subscription($user_id, $topic->ID) : bbp_add_user_subscription($user_id, $topic->ID);
     // Bail if action failed
     if (empty($status)) {
         bbp_ajax_response(false, __('The request was unsuccessful. Please try again.', 'bbpress'), 305);
     }
     // Put subscription attributes in convenient array
     $attrs = array('topic_id' => $topic->ID, 'user_id' => $user_id);
     // Action succeeded
     bbp_ajax_response(true, bbp_get_user_subscribe_link($attrs, $user_id, false), 200);
 }
Ejemplo n.º 16
0
 /**
  * Redirect to the group forum screen
  *
  * @since bbPress (r3653)
  */
 public function new_reply_redirect_to($redirect_url = '', $redirect_to = '', $reply_id = 0)
 {
     global $wp_rewrite;
     if (bp_is_group()) {
         $topic_id = bbp_get_reply_topic_id($reply_id);
         $topic = bbp_get_topic($topic_id);
         $reply_position = bbp_get_reply_position($reply_id, $topic_id);
         $reply_page = ceil((int) $reply_position / (int) bbp_get_replies_per_page());
         $reply_hash = '#post-' . $reply_id;
         $topic_url = trailingslashit(bp_get_group_permalink(groups_get_current_group())) . trailingslashit($this->slug) . trailingslashit($this->topic_slug) . trailingslashit($topic->post_name);
         // Don't include pagination if on first page
         if (1 >= $reply_page) {
             $redirect_url = trailingslashit($topic_url) . $reply_hash;
             // Include pagination
         } else {
             $redirect_url = trailingslashit($topic_url) . trailingslashit($wp_rewrite->pagination_base) . trailingslashit($reply_page) . $reply_hash;
         }
         // Add topic view query arg back to end if it is set
         if (bbp_get_view_all()) {
             $redirect_url = bbp_add_view_all($redirect_url);
         }
     }
     return $redirect_url;
 }
Ejemplo n.º 17
0
/**
 * Handles the front end opening/closing, spamming/unspamming,
 * sticking/unsticking and trashing/untrashing/deleting of topics
 *
 * @since bbPress (r2727)
 *
 * @uses bbp_get_topic() To get the topic
 * @uses current_user_can() To check if the user is capable of editing or
 *                           deleting the topic
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses check_ajax_referer() To verify the nonce and check the referer
 * @uses bbp_is_topic_open() To check if the topic is open
 * @uses bbp_close_topic() To close the topic
 * @uses bbp_open_topic() To open the topic
 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
 * @uses bbp_unstick_topic() To unstick the topic
 * @uses bbp_stick_topic() To stick the topic
 * @uses bbp_is_topic_spam() To check if the topic is marked as spam
 * @uses bbp_spam_topic() To make the topic as spam
 * @uses bbp_unspam_topic() To unmark the topic as spam
 * @uses wp_trash_post() To trash the topic
 * @uses wp_untrash_post() To untrash the topic
 * @uses wp_delete_post() To delete the topic
 * @uses do_action() Calls 'bbp_toggle_topic_handler' with success, post data
 *                    and action
 * @uses bbp_get_forum_permalink() To get the forum link
 * @uses bbp_get_topic_permalink() To get the topic link
 * @uses add_query_arg() To add args to the url
 * @uses wp_safe_redirect() To redirect to the topic
 * @uses bbPress::errors:add() To log the error messages
 */
function bbp_toggle_topic_handler()
{
    // Bail if not a GET action
    if ('GET' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    // Bail if required GET actions aren't passed
    if (empty($_GET['topic_id']) || empty($_GET['action'])) {
        return;
    }
    // Setup possible get actions
    $possible_actions = array('bbp_toggle_topic_close', 'bbp_toggle_topic_stick', 'bbp_toggle_topic_spam', 'bbp_toggle_topic_trash');
    // Bail if actions aren't meant for this function
    if (!in_array($_GET['action'], $possible_actions)) {
        return;
    }
    $failure = '';
    // Empty failure string
    $view_all = false;
    // Assume not viewing all
    $action = $_GET['action'];
    // What action is taking place?
    $topic_id = (int) $_GET['topic_id'];
    // What's the topic id?
    $success = false;
    // Flag
    $post_data = array('ID' => $topic_id);
    // Prelim array
    $redirect = '';
    // Empty redirect URL
    // Make sure topic exists
    $topic = bbp_get_topic($topic_id);
    if (empty($topic)) {
        return;
    }
    // What is the user doing here?
    if (!current_user_can('edit_topic', $topic->ID) || 'bbp_toggle_topic_trash' == $action && !current_user_can('delete_topic', $topic->ID)) {
        bbp_add_error('bbp_toggle_topic_permission', __('<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress'));
        return;
    }
    // What action are we trying to perform?
    switch ($action) {
        // Toggle open/close
        case 'bbp_toggle_topic_close':
            check_ajax_referer('close-topic_' . $topic_id);
            $is_open = bbp_is_topic_open($topic_id);
            $success = $is_open ? bbp_close_topic($topic_id) : bbp_open_topic($topic_id);
            $failure = $is_open ? __('<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress');
            break;
            // Toggle sticky/super-sticky/unstick
        // Toggle sticky/super-sticky/unstick
        case 'bbp_toggle_topic_stick':
            check_ajax_referer('stick-topic_' . $topic_id);
            $is_sticky = bbp_is_topic_sticky($topic_id);
            $is_super = empty($is_sticky) && !empty($_GET['super']) && 1 == (int) $_GET['super'] ? true : false;
            $success = $is_sticky ? bbp_unstick_topic($topic_id) : bbp_stick_topic($topic_id, $is_super);
            $failure = $is_sticky ? __('<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress');
            break;
            // Toggle spam
        // Toggle spam
        case 'bbp_toggle_topic_spam':
            check_ajax_referer('spam-topic_' . $topic_id);
            $is_spam = bbp_is_topic_spam($topic_id);
            $success = $is_spam ? bbp_unspam_topic($topic_id) : bbp_spam_topic($topic_id);
            $failure = $is_spam ? __('<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress') : __('<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress');
            $view_all = !$is_spam;
            break;
            // Toggle trash
        // Toggle trash
        case 'bbp_toggle_topic_trash':
            $sub_action = in_array($_GET['sub_action'], array('trash', 'untrash', 'delete')) ? $_GET['sub_action'] : false;
            if (empty($sub_action)) {
                break;
            }
            switch ($sub_action) {
                case 'trash':
                    check_ajax_referer('trash-' . bbp_get_topic_post_type() . '_' . $topic_id);
                    $view_all = true;
                    $success = wp_trash_post($topic_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress');
                    break;
                case 'untrash':
                    check_ajax_referer('untrash-' . bbp_get_topic_post_type() . '_' . $topic_id);
                    $success = wp_untrash_post($topic_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress');
                    break;
                case 'delete':
                    check_ajax_referer('delete-' . bbp_get_topic_post_type() . '_' . $topic_id);
                    $success = wp_delete_post($topic_id);
                    $failure = __('<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress');
                    break;
            }
            break;
    }
    // Do additional topic toggle actions
    do_action('bbp_toggle_topic_handler', $success, $post_data, $action);
    // No errors
    if (false != $success && !is_wp_error($success)) {
        // Redirect back to the topic's forum
        if (isset($sub_action) && 'delete' == $sub_action) {
            $redirect = bbp_get_forum_permalink($success->post_parent);
            // Redirect back to the topic
        } else {
            // Get the redirect detination
            $permalink = bbp_get_topic_permalink($topic_id);
            $redirect = bbp_add_view_all($permalink, $view_all);
        }
        wp_safe_redirect($redirect);
        // For good measure
        exit;
        // Handle errors
    } else {
        bbp_add_error('bbp_toggle_topic', $failure);
    }
}
Ejemplo n.º 18
0
        ?>



            <?php 
        do_action('bbp_template_before_topics_loop');
        ?>

            <div class="clearfix"></div>
                <div class="td-topics-wrap">
                    <?php 
        while (bbp_topics()) {
            bbp_the_topic();
            ?>
                        <?php 
            $topic = bbp_get_topic('');
            ?>
                    <ul class="td-topic-list-table">
                        <li class="td-forum-topics-avatar">
                            <span class="td-topic-started-by">
                                <?php 
            bbp_author_link(array('post_id' => $topic->ID, 'size' => 60, 'type' => 'avatar'));
            //started by
            ?>
                            </span>

                            <span class="td-topic-last-reply">
                                <?php 
            $last_active_topic_id = get_post_meta($topic->ID, '_bbp_last_active_id', true);
            bbp_author_link(array('post_id' => $last_active_topic_id, 'size' => 25, 'type' => 'avatar'));
            ?>
Ejemplo n.º 19
0
 /**
  * Subscribe/Unsubscribe a user from a topic
  *
  * @since bbPress (r2668)
  *
  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  * @uses bbp_get_current_user_id() To get the current user id
  * @uses current_user_can() To check if the current user can edit the user
  * @uses bbp_get_topic() To get the topic
  * @uses check_ajax_referer() To verify the nonce & check the referer
  * @uses bbp_is_user_subscribed() To check if the topic is in user's
  *                                 subscriptions
  * @uses bbp_remove_user_subscriptions() To remove the topic from user's
  *                                        subscriptions
  * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
  */
 function bbp_skeleton_dim_subscription()
 {
     if (!bbp_is_subscriptions_active()) {
         return;
     }
     $user_id = bbp_get_current_user_id();
     $id = intval($_POST['id']);
     if (!current_user_can('edit_user', $user_id)) {
         die('-1');
     }
     if (!($topic = bbp_get_topic($id))) {
         die('0');
     }
     check_ajax_referer("toggle-subscription_{$topic->ID}");
     if (bbp_is_user_subscribed($user_id, $topic->ID)) {
         if (bbp_remove_user_subscription($user_id, $topic->ID)) {
             die('1');
         }
     } else {
         if (bbp_add_user_subscription($user_id, $topic->ID)) {
             die('1');
         }
     }
     die('0');
 }
Ejemplo n.º 20
0
        /**
         * Toggle topic notices
         *
         * Display the success/error notices from
         * {@link BBP_Admin::toggle_topic()}
         *
         * @since 2.0.0 bbPress (r2727)
         *
         * @uses bbp_get_topic() To get the topic
         * @uses bbp_get_topic_title() To get the topic title of the topic
         * @uses esc_html() To sanitize the topic title
         * @uses apply_filters() Calls 'bbp_toggle_topic_notice_admin' with
         *                        message, topic id, notice and is it a failure
         */
        public function toggle_topic_notice()
        {
            if ($this->bail()) {
                return;
            }
            // Only proceed if GET is a topic toggle action
            if (bbp_is_get_request() && !empty($_GET['bbp_topic_toggle_notice']) && in_array($_GET['bbp_topic_toggle_notice'], array('opened', 'closed', 'super_sticky', 'stuck', 'unstuck', 'spammed', 'unspammed', 'approved', 'unapproved')) && !empty($_GET['topic_id'])) {
                $notice = $_GET['bbp_topic_toggle_notice'];
                // Which notice?
                $topic_id = (int) $_GET['topic_id'];
                // What's the topic id?
                $is_failure = !empty($_GET['failed']) ? true : false;
                // Was that a failure?
                // Bais if no topic_id or notice
                if (empty($notice) || empty($topic_id)) {
                    return;
                }
                // Bail if topic is missing
                $topic = bbp_get_topic($topic_id);
                if (empty($topic)) {
                    return;
                }
                $topic_title = bbp_get_topic_title($topic->ID);
                switch ($notice) {
                    case 'opened':
                        $message = $is_failure === true ? sprintf(__('There was a problem opening the topic "%1$s".', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully opened.', 'bbpress'), $topic_title);
                        break;
                    case 'closed':
                        $message = $is_failure === true ? sprintf(__('There was a problem closing the topic "%1$s".', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully closed.', 'bbpress'), $topic_title);
                        break;
                    case 'super_sticky':
                        $message = $is_failure === true ? sprintf(__('There was a problem sticking the topic "%1$s" to front.', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully stuck to front.', 'bbpress'), $topic_title);
                        break;
                    case 'stuck':
                        $message = $is_failure === true ? sprintf(__('There was a problem sticking the topic "%1$s".', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully stuck.', 'bbpress'), $topic_title);
                        break;
                    case 'unstuck':
                        $message = $is_failure === true ? sprintf(__('There was a problem unsticking the topic "%1$s".', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully unstuck.', 'bbpress'), $topic_title);
                        break;
                    case 'spammed':
                        $message = $is_failure === true ? sprintf(__('There was a problem marking the topic "%1$s" as spam.', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully marked as spam.', 'bbpress'), $topic_title);
                        break;
                    case 'unspammed':
                        $message = $is_failure === true ? sprintf(__('There was a problem unmarking the topic "%1$s" as spam.', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully unmarked as spam.', 'bbpress'), $topic_title);
                        break;
                    case 'approved':
                        $message = $is_failure === true ? sprintf(__('There was a problem approving the topic "%1$s".', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully approved.', 'bbpress'), $topic_title);
                        break;
                    case 'unapproved':
                        $message = $is_failure === true ? sprintf(__('There was a problem unapproving the topic "%1$s".', 'bbpress'), $topic_title) : sprintf(__('Topic "%1$s" successfully unapproved.', 'bbpress'), $topic_title);
                        break;
                }
                // Do additional topic toggle notice filters (admin side)
                $message = apply_filters('bbp_toggle_topic_notice_admin', $message, $topic->ID, $notice, $is_failure);
                ?>

			<div id="message" class="<?php 
                echo $is_failure === true ? 'error' : 'updated';
                ?>
 fade">
				<p style="line-height: 150%"><?php 
                echo esc_html($message);
                ?>
</p>
			</div>

			<?php 
            }
        }
Ejemplo n.º 21
0
/**
 * Add a topic to user's subscriptions
 *
 * @since bbPress (r2668)
 *
 * @param int $user_id Optional. User id
 * @param int $topic_id Optional. Topic id
 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions
 * @uses bbp_get_topic() To get the topic
 * @uses update_user_option() To update the user's subscriptions
 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
 * @return bool Always true
 */
function bbp_add_user_subscription($user_id = 0, $topic_id = 0)
{
    if (empty($user_id) || empty($topic_id)) {
        return false;
    }
    $subscriptions = (array) bbp_get_user_subscribed_topic_ids($user_id);
    $topic = bbp_get_topic($topic_id);
    if (empty($topic)) {
        return false;
    }
    if (!in_array($topic_id, $subscriptions)) {
        $subscriptions[] = $topic_id;
        $subscriptions = array_filter($subscriptions);
        $subscriptions = (string) implode(',', $subscriptions);
        update_user_option($user_id, '_bbp_subscriptions', $subscriptions);
        wp_cache_delete('bbp_get_topic_subscribers_' . $topic_id, 'bbpress');
    }
    do_action('bbp_add_user_subscription', $user_id, $topic_id);
    return true;
}
 /**
  * Subscribe/Unsubscribe a user from a topic
  *
  * @since bbPress (r3732)
  *
  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  * @uses bbp_get_current_user_id() To get the current user id
  * @uses current_user_can() To check if the current user can edit the user
  * @uses bbp_get_topic() To get the topic
  * @uses check_ajax_referer() To verify the nonce & check the referer
  * @uses bbp_is_user_subscribed() To check if the topic is in user's
  *                                 subscriptions
  * @uses bbp_remove_user_subscriptions() To remove the topic from user's
  *                                        subscriptions
  * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
  */
 public function ajax_subscription()
 {
     if (!bbp_is_subscriptions_active()) {
         return;
     }
     $user_id = bbp_get_current_user_id();
     $id = intval($_POST['id']);
     if (!current_user_can('edit_user', $user_id)) {
         die('-1');
     }
     $topic = bbp_get_topic($id);
     if (empty($topic)) {
         die('0');
     }
     check_ajax_referer('toggle-subscription_' . $topic->ID);
     if (bbp_is_user_subscribed($user_id, $topic->ID)) {
         if (bbp_remove_user_subscription($user_id, $topic->ID)) {
             die('1');
         }
     } else {
         if (bbp_add_user_subscription($user_id, $topic->ID)) {
             die('1');
         }
     }
     die('0');
 }
Ejemplo n.º 23
0
/**
 * Return the link to go directly to the reply form
 *
 * @since 2.4.0 bbPress (r4966)
 *
 * @param array $args Arguments
 * @uses bbp_current_user_can_access_create_reply_form() To check permissions
 * @uses bbp_get_topic_id() To validate the topic id
 * @uses bbp_get_topic() To get the topic
 * @uses apply_filters() Calls 'bbp_get_topic_reply_link' with the formatted link,
 *                        the arguments array, and the topic
 * @return string Link for a reply to a topic
 */
function bbp_get_topic_reply_link($args = array())
{
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('id' => 0, 'link_before' => '', 'link_after' => '', 'reply_text' => esc_html__('Reply', 'bbpress')), 'get_topic_reply_link');
    // Get the reply to use it's ID and post_parent
    $topic = bbp_get_topic($r['id']);
    // Bail if no reply or user cannot reply
    if (empty($topic) || !bbp_current_user_can_access_create_reply_form()) {
        return;
    }
    $uri = '#new-post';
    // Add $uri to the array, to be passed through the filter
    $r['uri'] = $uri;
    $retval = $r['link_before'] . '<a href="' . esc_url($r['uri']) . '" class="bbp-topic-reply-link">' . $r['reply_text'] . '</a>' . $r['link_after'];
    return apply_filters('bbp_get_topic_reply_link', $retval, $r, $args);
}
Ejemplo n.º 24
0
function likebtn_bbp_reply($wrap = true, $position = LIKEBTN_POSITION_BOTH, $alignment = '', $content = '')
{
    // Reply to topic
    $entity = bbp_get_reply(bbp_get_reply_id());
    // Topic
    if (!is_object($entity)) {
        $entity = bbp_get_topic(bbp_get_topic_id());
    }
    if (!$entity) {
        return false;
    }
    // Allow in selected forums
    if (!empty($entity->post_parent)) {
        $allow_forums = get_option('likebtn_allow_forums_' . LIKEBTN_ENTITY_BBP_POST);
        if (!is_array($allow_forums)) {
            $allow_forums = array();
        }
        if ($allow_forums && !in_array($entity->post_parent, $allow_forums)) {
            return false;
        }
    }
    return _likebtn_get_content_universal(LIKEBTN_ENTITY_BBP_POST, $entity->ID, $content, $wrap, $position, $alignment);
}