<?php
/*
Template Name: Comments page
 */

$limit = 25; /* Comments per Page */

function get_url_var($name) {
    $strURL = $_SERVER['REQUEST_URI'];
    $arrVals = split("/",$strURL);
    $found = 0;
    foreach ($arrVals as $index => $value) {
        if($value == $name)
            $found = $index;
    }
    $place = $found + 1;
    return $arrVals[$place];
}

/* return thumbnail url and image find url for first image of a given post */
function get_comment_thumb_full ( $post_id ) {
    $images = get_attached_media('image', $post_id);
    $thumbsrc = '';
    $fullsrc = '';
    foreach($images as $image) {
        #       $thumbsrc = wp_get_attachment_image_src($image->ID,'optics-mws-gallery-thumb')[0];
        $thumbsrc = wp_get_attachment_image_src($image->ID,'thumbnail')[0];
        $fullsrc =  wp_get_attachment_image_src($image->ID,'full')[0];
        break;
    }
    return [$thumbsrc, $fullsrc];
}

$page = get_query_var('comments');

$offset = (intval($page) * $limit) - $limit;
if ( $offset < 0 ) {
    $offset = 0;
}

$param = array( 'status' => 'approve',
                'offset' => $offset,
                'number' => $limit );
$total_comments = get_comments( array( 'status' => 'approve' ) ); 
$pages = ceil( count( $total_comments ) / $limit );
$comments = get_comments( $param );

get_header(); ?>

<div id="content" class="site-content">
  <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
    
      <article id="comments-page" class="comments-page page type-page status-publish hentry entry">
      
	<header class="entry-header alignwide">
	  <h2 class="entry-title">Comments</h1>			
	</header>
	
	<div class="entry-content">
          <?php
          foreach( $comments as $comment ) {
              $post_id = $comment->comment_post_ID;
              if ( $old_post_id && $old_post_id == $post_id) {
                  echo "	                      <div class='entry-summary'>\n";
                  if ($comment->comment_author == 'Markus Spring') {
                      echo " <strong><a href='{$comment->comment_author_url}' class='owner' "
                         . "target='_blank'>{$comment->comment_author}</a></strong>\n";
                  } else {
                      echo " <strong><a href='{$comment->comment_author_url}'"
                         . "target='_blank'>{$comment->comment_author}</a></strong>\n";
                  }
                  echo " said at {$comment->comment_date}:<br />{$comment->comment_content}\n";
                  echo "</div><!-- .entry-summary -->\n";
              } else {
                  if ($old_post_id) {
                      echo "<br style='clear: both;' /><hr class='comment-sep' />\n";
                  }
                  $old_post_id = $post_id;
                  list( $thumbsrc, $fullsrc ) = get_comment_thumb_full( $post_id );
                  if ( $thumbsrc ) {
                      echo "<div class='wp-block-image' style='margin-bottom: 5px !important;'>"
                         . "<a href='" . get_permalink($post_id)
                         . "'><figure class='alignleft size-thumbnail'>"
                         . "<img loading='lazy' width='268' height='268' "
                         . "src='" . $thumbsrc . "' alt='' class='wp-image-comments'/></figure></a></div>\n";
                      echo "<h4 style='display: inline;'>On&nbsp;<a href='" . get_permalink($post_id) . "'>"
                         . get_the_title($post_id) . "</a></h4>\n"; 
                  } else {
                      echo "                        On '" . get_the_title($post_id) . "',<br />"; 
                  }
                  echo "	                      <div class='entry-summary'>\n";
                  if ($comment->comment_author == 'Markus Spring') {
                      echo "                            <strong><a href='{$comment->comment_author_url}' class='owner' "
                         . "target='_blank'>{$comment->comment_author}</a></strong>\n";
                  } else {
                      echo "                            <strong><a href='{$comment->comment_author_url}'"
                         . "target='_blank'>{$comment->comment_author}</a></strong>\n";
                  }
                  /* echo "                            said at {$comment->comment_date}:<br />{$comment->comment_content}\n"; */
                  echo "                            said at {$comment->comment_date}:<br />"
                     . apply_filters('the_content', $comment->comment_content) . "\n";
                  echo "                        </div><!-- .entry-summary -->\n";
              }
          }
          ?>
          <footer class="entry-footer">
            <?php //optics_entry_footer(); ?>
          </footer><!-- .entry-footer -->
        </div><!-- end search-result -->
      </article><!-- #post-## -->
      
<?php
/* get_sidebar();
   get_footer(); */
?>
