/**
  * The singleton method.
  *
  * @return Muut_Comment_Overrides The instance.
  * @author Paul Hughes
  * @since  3.0
  */
 public static function instance()
 {
     if (!is_a(self::$instance, __CLASS__)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
/**
 * Gets embed anchor for a given post comments override.
 *
 * @param int $post_id The post we are getting the comments anchor for.
 * @return void|string The anchor markup or void, we $echo is set to true (and we echo the markup).
 * @param bool $echo Whether to echo the anchor or not.
 * @author Paul Hughes
 * @since 3.0
 */
function muut_comments_override_anchor($post_id = null, $echo = true)
{
    if (is_null($post_id)) {
        $post_id = get_the_ID();
    }
    if (class_exists('Muut_Comment_Overrides')) {
        return Muut_Comment_Overrides::instance()->commentsOverrideAnchor($post_id, $echo);
    } else {
        return false;
    }
}
 /**
  * Initializes the Comment Overrides class (when WP comments over overridden by Muut).
  *
  * @return void
  * @author Paul Hughes
  * @since 3.0
  */
 public function initCommentOverrides()
 {
     $class = 'Muut_Comment_Overrides';
     if (!in_array($class, $this->alreadyInit) && muut()->getOption('replace_comments', false)) {
         require_once muut()->getPluginPath() . 'lib/comment-overrides.class.php';
         if (class_exists($class)) {
             Muut_Comment_Overrides::instance();
         }
         $this->alreadyInit[] = $class;
     }
 }
 /**
  * Filters the commenting embed anchor to render the index content rather than the commenting anchor.
  *
  * @param string $content The current embed content (anchor).
  * @param int $post_id The post for which we are filtering the embed.
  * @return string The filtered content.
  * @author Paul Hughes
  * @since 3.0.1
  */
 public function filterCommentsOverrideIndexContent($content, $post_id, $type)
 {
     if ($this->isUsingEscapedFragments()) {
         $post_commenting_options = Muut_Post_Utility::getPostOption($post_id, 'commenting_settings');
         if (isset($post_commenting_options['type']) && $post_commenting_options['type'] == 'threaded') {
             $this->context = 'threaded-commenting';
         } else {
             $this->context = 'flat-commenting';
         }
         if ($_GET['_escaped_fragment_']) {
             $remote_path = $_GET['_escaped_fragment_'];
         } else {
             $remote_path = Muut_Comment_Overrides::instance()->getCommentsPath($post_id);
         }
         $content = $this->getIndexContentForPath($remote_path);
     }
     return $content;
 }
 /**
  * Updates/checks the latest reply-time on a post if one of its comments is removed (if it was the latest comment, obviously it's no longer the latest).
  *
  * @param $request array The array that was parsed from the request body.
  * @param $event string The event that was sent.
  * @return void
  * @author Paul Hughes
  * @since 3.0.2.1
  */
 public function updatePostLatestReplyTime($request, $event)
 {
     // Only execute for the applicable events.
     $events = array('remove', 'spam', 'unspam');
     if (in_array($event, $events)) {
         $path = $request['path'];
         $split_path = explode('#', $path);
         $split_final = explode('/', $split_path[1]);
         $comment_base = $split_path[0] . '#' . $split_final[0];
         // See if the path is a reply to a given post.
         $post_id = Muut_Webhooks::getPostIdRepliedTo($path);
         if (!$post_id) {
             return;
         }
         $latest_update_timestamp = get_post_time('U', true, $post_id);
         $muut_user = '';
         $has_replies = false;
         // If it is threaded commenting, make sure to check for the top-level times.
         if (Muut_Comment_Overrides::instance()->getCommentingPostCommentType($post_id) == 'threaded') {
             // Check if a WP post exists in the database that would match the path of the "post" request (for threaded commenting).
             $post_query_args = array('post_type' => Muut_Custom_Post_Types::MUUT_THREAD_CPT_NAME, 'post_status' => Muut_Custom_Post_Types::MUUT_PUBLIC_POST_STATUS, 'meta_query' => array(array('key' => 'muut_channel_path', 'value' => $split_path[0])), 'orderby' => 'post_date_gmt', 'order' => 'DESC', 'posts_per_page' => 1);
             $query = new WP_Query($post_query_args);
             $posts = $query->get_posts();
             if (!empty($posts) && is_array($posts)) {
                 $post_update_time = get_post_time('U', true, $posts[0]);
                 if ($post_update_time > $latest_update_timestamp) {
                     $latest_update_timestamp = $post_update_time;
                     $muut_user = get_post_meta($posts[0]->ID, 'muut_user', true);
                     $has_replies = true;
                 }
             }
         }
         // Also check the actual comments.
         $comment_query_args = array('meta_query' => array(array('key' => 'muut_path', 'value' => $comment_base)), 'orderby' => 'comment_date_gmt', 'order' => 'DESC', 'number' => 1);
         // Get the comment data.
         $comment_query = new WP_Comment_Query();
         $comments = $comment_query->query($comment_query_args);
         if (!empty($comments) && is_array($comments)) {
             $comment_update_time = strtotime($comments[0]->comment_date_gmt);
             if ($comment_update_time > $latest_update_timestamp) {
                 $latest_update_timestamp = $comment_update_time;
                 $muut_user = get_comment_meta($comments[0]->comment_ID, 'muut_user', true);
             }
             $has_replies = true;
         }
         if (!$has_replies) {
             delete_post_meta($post_id, self::REPLY_UPDATE_TIME_NAME);
             delete_post_meta($post_id, self::REPLY_LAST_USER_DATA_NAME);
         } else {
             // Add/update a meta for the post with the time of the last comment and the user data responsible.
             update_post_meta($post_id, self::REPLY_UPDATE_TIME_NAME, $latest_update_timestamp);
             update_post_meta($post_id, self::REPLY_LAST_USER_DATA_NAME, $muut_user);
         }
         self::refreshCache();
     }
 }