/**
 * Checks if a given post uses Muut commenting.
 *
 * @param int $post_id The ID of the post we are checking.
 * @return bool Whether the post uses Muut commenting.
 * @author Paul Hughes
 * @since 3.0
 */
function muut_uses_muut_commenting($post_id = null)
{
    if (is_null($post_id)) {
        $post_id = get_the_ID();
    }
    return Muut_Post_Utility::isMuutCommentingPost($post_id);
}
 /**
  * Render the widget frontend output.
  *
  * @param array $args The sidebar arguments.
  * @param array $instance The widget instance parameters.
  * @return void
  * @author Paul Hughes
  * @since 3.0.2
  */
 public function widget($args, $instance)
 {
     // Make sure webhooks are active, or don't bother.
     if (!muut_is_webhooks_active() || self::$has_loaded || apply_filters('muut_hide_latest_comments_widget_display', false)) {
         return;
     }
     // Make sure the Muut resources get loaded (only stuff in the footer will work, as this happens
     // partway through page load).
     add_filter('muut_requires_muut_resources', '__return_true');
     muut()->enqueueFrontendScripts();
     $title = isset($instance['title']) ? $instance['title'] : '';
     $latest_comments_data = array_slice($this->getLatestCommentsData(), 0, $instance['number_of_comments']);
     // Render widget.
     // Used to allow the adding of other "allowed" comments base domains if it has changed or whatnot.
     // SAME filter as in webhooks.class.php
     $comments_base_domains = join('","', apply_filters('muut_webhooks_allowed_comments_base_domains', array(muut()->getOption('comments_base_domain'))));
     echo $args['before_widget'];
     echo '<script type="text/javascript">';
     echo 'var muut_latest_comments_num_posts = "' . $instance['number_of_comments'] . '";';
     echo 'var muut_latest_comments_path = ["' . $comments_base_domains . '"];';
     if (get_the_ID() && Muut_Post_Utility::isMuutCommentingPost(get_the_ID())) {
         echo 'var muut_wp_post_id = ' . get_the_ID() . ';';
         echo 'var muut_wp_post_permalink = "' . get_permalink() . '";';
         echo 'var muut_wp_post_title = "' . get_the_title() . '";';
     }
     echo '</script>';
     echo $args['before_title'] . $title . $args['after_title'];
     include muut()->getPluginPath() . 'views/widgets/widget-latest-comments.php';
     echo $args['after_widget'];
     self::$has_loaded = true;
 }
 /**
  * Checks whether we are/should be using escaped fragments on this page load.
  *
  * @return bool Whether we are using escaped fragments support on this page load.
  * @author Paul Hughes
  * @since 3.0.1
  */
 public function isUsingEscapedFragments()
 {
     if (!isset($this->maybeDoEscapedFragments)) {
         global $post;
         if (isset($_GET['_escaped_fragment_']) && isset($post) && is_a($post, 'WP_Post') && (Muut_Post_Utility::isMuutPost($post->ID) || Muut_Post_Utility::isMuutCommentingPost($post->ID))) {
             $this->maybeDoEscapedFragments = true;
         } else {
             $this->maybeDoEscapedFragments = false;
         }
     }
     return apply_filters('muut_is_using_escaped_fragments', $this->maybeDoEscapedFragments);
 }
示例#4
0
 /**
  * Checks if the a page needs to include the frontend Muut resources.
  *
  * @param int $page_id The page we are checking.
  * @return bool Whether we need the frontend Muut resources or not.
  * @author Paul Hughes
  * @since 3.0
  */
 public function needsMuutResources($page_id = null)
 {
     if (is_null($page_id)) {
         $page_id = get_the_ID();
     }
     $return = false;
     if (is_numeric($page_id) && (Muut_Post_Utility::isMuutPost($page_id) || Muut_Post_Utility::isMuutCommentingPost($page_id))) {
         $return = true;
     }
     return apply_filters('muut_requires_muut_resources', $return, $page_id);
 }
 /**
  * Gets (and caches) the comment counts for posts in the main query.
  *
  * @param array $posts The array of WP_Post objects that were fetched in the main query.
  * @return array The same array.
  * @author Paul Hughes
  * @since 3.0
  */
 public function fetchCommentCountForMuutPosts()
 {
     global $wp_query;
     // Only execute this functionality if "do not fetch" is not set.
     // That filter can be used (set to true) to prevent any of this from executing.
     if (!apply_filters('muut_do_not_fetch_post_counts', false) && $wp_query->is_main_query()) {
         $post_count_queue = array();
         $posts = $wp_query->posts;
         foreach ($posts as $post) {
             if (Muut_Post_Utility::isMuutCommentingPost($post->ID) && wp_cache_get("muut-comments-{$post->ID}", 'counts') === false && get_post_meta($post->ID, 'muut_comments_count', true) === '') {
                 $path = '/' . $this->getCommentsPath($post->ID, true);
                 $post_count_queue[$post->ID] = $path;
             }
         }
         // As long as there is at least one post that uses Muut commenting and doesn't have a cached value...
         if (count($post_count_queue) > 0) {
             global $wp_version;
             $api_endpoint = 'https://' . Muut::MUUTAPISERVER . '/postcounts';
             $api_args = '?path=' . join('&path=', $post_count_queue);
             $api_call = $api_endpoint . $api_args;
             $fetch_args = array('user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url') . ' MuutForum/' . muut()->getForumName(), 'timeout' => apply_filters('muut_api_post_counts_timeout', '2'));
             $response = wp_remote_get($api_call, $fetch_args);
             if (is_wp_error($response) && (muut()->isInDevelopMode() || !apply_filters('muut_suppress_api_errors', true))) {
                 error_log('Something went wrong fetching Muut API: ' . $response->get_error_message());
             } else {
                 if (wp_remote_retrieve_response_code($response) == 200) {
                     $body = wp_remote_retrieve_body($response);
                     $return_array = json_decode($body);
                     // Cache values for each returned post comment count.
                     if (!is_null($return_array)) {
                         $post_array = array_flip($post_count_queue);
                         foreach ($post_array as $url => $id) {
                             update_post_meta($id, 'muut_comments_count', $return_array->{$url}->size);
                             wp_cache_set("muut-comments-{$id}", $return_array->{$url}->size, 'counts');
                         }
                     }
                 }
             }
         }
     }
     return $posts;
 }
$commenting_defaults = muut()->getOption('commenting_defaults');
$type = isset($comments_settings['type']) ? $comments_settings['type'] : $commenting_defaults['type'];
$disable_uploads = isset($comments_settings['disable_uploads']) ? $comments_settings['disable_uploads'] : $commenting_defaults['disable_uploads'];
?>
<p>
	<span class="checkbox_row"><input type="checkbox" name="<?php 
echo $tab['meta_name'];
?>
[enabled-tab]" class="muut_enable_<?php 
echo $tab['name'];
?>
" id="muut_enable_tab-<?php 
echo $tab['name'];
?>
" <?php 
checked(true, Muut_Post_Utility::isMuutCommentingPost($post->ID));
?>
 value="1" /><label for="muut_enable_tab-<?php 
echo $tab['name'];
?>
"><?php 
echo $tab['enable_text'];
?>
</label></span>
</p>
<div class="enabled_tab_wrapper">
	<p>
		<span class="muut_metabox_radio"><input type="radio" name="<?php 
echo $meta_name;
?>
[type]" id="muut_comments_type_flat" value="flat" <?php 
 /**
  * Static function for checking if (and what is) the post ID that a given muut path is commenting on.
  *
  * @param string $path The path we are comparing/checking for.
  * @return int|false The post ID, if it is a comment on a given post or false, if not found.
  */
 public static function getPostIdRepliedTo($path)
 {
     // Used to allow the adding of other "allowed" comments base domains if it has changed or whatnot.
     $comments_base_domains = apply_filters('muut_webhooks_allowed_comments_base_domains', array(muut()->getOption('comments_base_domain')));
     $matches = array();
     // Check if the comment path is in the Muut post comment path format.
     foreach ($comments_base_domains as $base_domain) {
         preg_match_all('/^\\/' . addslashes(muut()->getForumName()) . '\\/' . addslashes($base_domain) . '\\/([0-9]+)(?:\\/|\\#)?.*$/', $path, $matches);
         // If there is a match, return it and exit this loop and function.
         if (!empty($matches) && isset($matches[1][0]) && is_numeric($matches[1][0]) && Muut_Post_Utility::isMuutCommentingPost($matches[1][0])) {
             return $matches[1][0];
         }
     }
 }
 /**
  * Enabled callback for the commenting tab.
  *
  * @return bool True if it should be enabled, False if not.
  * @author Paul Hughes
  * @since 3.0
  */
 public function isCommentingTabEnabled()
 {
     if (!isset($this->metaboxTabs['commenting']['enabled'])) {
         global $post;
         $last_active_tab = get_post_meta($post->ID, 'muut_last_active_tab', true);
         if ($last_active_tab == $this->metaboxTabs['commenting']['name'] || Muut_Post_Utility::isMuutCommentingPost($post->ID)) {
             $this->metaboxTabs['commenting']['enabled'] = true;
         } else {
             $this->metaboxTabs['commenting']['enabled'] = false;
         }
     }
     return $this->metaboxTabs['commenting']['enabled'];
 }