/**
 * Review Rating comment ajax actions.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 *
 * @param $request
 * @return bool
 */
function geodir_reviewrating_comment_action($request)
{
    global $wpdb;
    $comment_ids = array();
    if (isset($request['comment_ids']) && $request['comment_ids'] != '') {
        $comment_ids = explode(',', $request['comment_ids']);
    }
    if (!empty($comment_ids) && $request['comment_ids'] != '') {
        if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'geodir_review_action_nonce')) {
            return false;
        }
        foreach ($comment_ids as $comment_id) {
            if ($comment_id != '') {
                switch ($request['comment_action']) {
                    case 'deletecomment':
                        wp_delete_comment($comment_id);
                        break;
                    case 'trashcomment':
                        wp_trash_comment($comment_id);
                        break;
                    case 'untrashcomment':
                        wp_untrash_comment($comment_id);
                        break;
                    case 'spamcomment':
                        wp_spam_comment($comment_id);
                        break;
                    case 'unspamcomment':
                        wp_unspam_comment($comment_id);
                        break;
                    case 'approvecomment':
                        wp_set_comment_status($comment_id, 'approve');
                        break;
                    case 'unapprovecomment':
                        wp_set_comment_status($comment_id, 'hold');
                        break;
                }
            }
        }
        if (isset($request['geodir_comment_search'])) {
            $geodir_commentsearch = $request['geodir_comment_search'];
        }
        if (isset($request['geodir_comment_posttype'])) {
            $post_type = $request['geodir_comment_posttype'];
        }
        $status = $request['subtab'];
        $orderby = 'comment_date_gmt';
        $order = 'DESC';
        if (isset($request['geodir_comment_sort'])) {
            if ($request['geodir_comment_sort'] == 'oldest') {
                $orderby = 'comment_date_gmt';
                $order = 'ASC';
            }
        }
        if (isset($request['paged']) && $request['paged'] != '') {
            $paged = $request['paged'];
        } else {
            $paged = 1;
        }
        $show_post = $request['show_post'];
        $defaults = array('paged' => $paged, 'show_post' => $show_post, 'orderby' => $orderby, 'order' => $order, 'post_type' => $post_type, 'comment_approved' => $status, 'user_id' => '', 'search' => $geodir_commentsearch);
        $comments = geodir_reviewrating_get_comments($defaults);
        geodir_reviewrating_show_comments($comments['comments']);
    }
    if (isset($request['gd_tab_head'])) {
        geodir_reviewrating_show_tab_head($request['gd_tab_head']);
    }
    exit;
}
/**
 * GeoDirectory settings page 'reviews' Tab comments.
 *
 * @since 1.0.0
 * @package GeoDirectory_Review_Rating_Manager
 */
function geodir_reviewrating_manage_comments()
{
    if (isset($_REQUEST['geodir_comment_search'])) {
        $geodir_commentsearch = $_REQUEST['geodir_comment_search'];
    }
    if (isset($_REQUEST['geodir_comment_posttype'])) {
        $post_type = $_REQUEST['geodir_comment_posttype'];
    }
    $status = $_REQUEST['subtab'];
    $orderby = 'comment_date_gmt';
    $order = 'DESC';
    if (isset($_REQUEST['geodir_comment_sort'])) {
        if ($_REQUEST['geodir_comment_sort'] == 'oldest') {
            $orderby = 'comment_date_gmt';
            $order = 'ASC';
        } elseif ($_REQUEST['geodir_comment_sort'] == 'lowest_rating') {
            $orderby = 'overall_rating';
            $order = 'ASC';
        } elseif ($_REQUEST['geodir_comment_sort'] == 'highest_rating') {
            $orderby = 'overall_rating';
            $order = 'DESC';
        }
    }
    if (isset($_REQUEST['paged']) && $_REQUEST['paged'] != '') {
        $paged = $_REQUEST['paged'];
    } else {
        $paged = 1;
    }
    if (get_option('comments_per_page')) {
        $_REQUEST['show_post'] = get_option('comments_per_page');
    } else {
        $_REQUEST['show_post'] = '20';
    }
    $show_post = isset($_REQUEST['show_post']) ? $_REQUEST['show_post'] : '';
    $defaults = array('paged' => $paged, 'show_post' => $show_post, 'orderby' => $orderby, 'order' => $order, 'post_type' => isset($post_type) ? $post_type : '', 'comment_approved' => $status, 'user_id' => '', 'search' => isset($geodir_commentsearch) ? $geodir_commentsearch : '');
    $comments = geodir_reviewrating_get_comments($defaults);
    ?>
		
		<div style="float:right; margin-top:0px;">
			<?php 
    echo $comment_pagings = geodir_reviewrating_pagination($comments['comment_count']);
    ?>
		</div>
		<div style="clear:both;"></div>
		
	 <div class="gd-content-heading" style="display:block">
		 <h3>
				<div class="clearfix">				
					 <input name="checkedall" type="checkbox" value="" style="float:left; margin-top:8px;" />
		         	<div class="three-tab">
			 <ul class="clearfix">
					<?php 
    if ($_REQUEST['subtab'] == 'pending') {
        ?>
						<li action="approvecomment"><a href="javascript:void(0);"><?php 
        _e('Approve', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="spamcomment"><a href="javascript:void(0);"><?php 
        _e('Spam', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="trashcomment"><a href="javascript:void(0);"><?php 
        _e('Trash', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<?php 
    } elseif ($_REQUEST['subtab'] == 'approved') {
        ?>
						<li action="unapprovecomment"><a href="javascript:void(0);"><?php 
        _e('Unapprove', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="spamcomment"><a href="javascript:void(0);"><?php 
        _e('Spam', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="trashcomment"><a href="javascript:void(0);"><?php 
        _e('Trash', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<?php 
    } elseif ($_REQUEST['subtab'] == 'spam') {
        ?>
						<li action="unspamcomment"><a href="javascript:void(0);"><?php 
        _e('Not Spam', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="deletecomment"><a href="javascript:void(0);"><?php 
        _e('Delete Permanently', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<?php 
    } elseif ($_REQUEST['subtab'] == 'trash') {
        ?>
						<li action="untrashcomment"><a href="javascript:void(0);"><?php 
        _e('Restore', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="deletecomment"><a href="javascript:void(0);"><?php 
        _e('Delete Permanently', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<?php 
    } else {
        ?>
						<li action="approvecomment"><a href="javascript:void(0);"><?php 
        _e('Approve', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="unapprovecomment"><a href="javascript:void(0);"><?php 
        _e('Unapprove', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="spamcomment"><a href="javascript:void(0);"><?php 
        _e('Spam', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<li action="trashcomment"><a href="javascript:void(0);"><?php 
        _e('Trash', GEODIRREVIEWRATING_TEXTDOMAIN);
        ?>
</a></li>
						<?php 
    }
    ?>
			 </ul>
		</div>
		<?php 
    $nonce = wp_create_nonce('geodir_review_action_nonce');
    ?>
				
		<form>
			<input type="hidden" name="geodir_review_action_nonce_field" value="<?php 
    echo $nonce;
    ?>
" />
			 <input type="hidden" name="review_url" value="<?php 
    echo admin_url('admin.php?page=geodirectory');
    ?>
" />
				<input type="hidden" name="geodir_review_paged" value="<?php 
    echo $paged;
    ?>
" />
				<input type="hidden" name="geodir_review_show_post" value="<?php 
    echo $show_post;
    ?>
" />
				<input type="hidden" name="tab" value="reviews_fields" />
				<input type="hidden" name="subtab" value="<?php 
    if (isset($_REQUEST['subtab'])) {
        echo $_REQUEST['subtab'];
    }
    ?>
" />
				<div class="gd-search">
				<input name="geodir_comment_search" value="<?php 
    if (isset($_REQUEST['geodir_comment_search'])) {
        echo $_REQUEST['geodir_comment_search'];
    }
    ?>
" type="text" />
				</div>
				
				<div class="gd-search">
				
				<?php 
    $geodir_post_types = get_option('geodir_post_types');
    $geodir_posttypes = geodir_get_posttypes();
    $selected = isset($_REQUEST['geodir_comment_posttype']) ? $_REQUEST['geodir_comment_posttype'] : '';
    ?>
				<select name="geodir_comment_posttype" id="commentposttype">
				<option value = "" >Show all post types</option>
				<?php 
    if (!empty($geodir_posttypes)) {
        foreach ($geodir_posttypes as $p_type) {
            $geodir_posttype_info = $geodir_post_types[$p_type];
            $listing_slug = $geodir_posttype_info['labels']['singular_name'];
            echo '<option value="', $p_type, '"', $selected == $p_type ? ' selected="selected"' : '', '>', $listing_slug, '</option>';
        }
    }
    ?>
				</select>
				</div>
				
				<div class="gd-sort">
				<label>Sort :</label>
				<select name="geodir_comment_sort">
					<option <?php 
    if (isset($_REQUEST['geodir_comment_sort']) && $_REQUEST['geodir_comment_sort'] == 'newest') {
        echo 'selected="selected"';
    }
    ?>
 value="newest"><?php 
    _e('Newest', GEODIRREVIEWRATING_TEXTDOMAIN);
    ?>
</option>
					<option <?php 
    if (isset($_REQUEST['geodir_comment_sort']) && $_REQUEST['geodir_comment_sort'] == 'oldest') {
        echo 'selected="selected"';
    }
    ?>
 value="oldest"><?php 
    _e('Oldest', GEODIRREVIEWRATING_TEXTDOMAIN);
    ?>
</option>
					<option <?php 
    if (isset($_REQUEST['geodir_comment_sort']) && $_REQUEST['geodir_comment_sort'] == 'lowest_rating') {
        echo 'selected="selected"';
    }
    ?>
 value="lowest_rating"><?php 
    _e('Lowest rating', GEODIRREVIEWRATING_TEXTDOMAIN);
    ?>
</option>
					<option <?php 
    if (isset($_REQUEST['geodir_comment_sort']) && $_REQUEST['geodir_comment_sort'] == 'highest_rating') {
        echo 'selected="selected"';
    }
    ?>
 value="highest_rating"><?php 
    _e('Highest rating', GEODIRREVIEWRATING_TEXTDOMAIN);
    ?>
</option>
				</select>
				</div>
				<div class="gd-search" style="padding-top:2px;">
				<input id="gdcomment-filter_button" class="button-primary" type="button" name="searchfilter" value="<?php 
    _e('Filter', GEODIRREVIEWRATING_TEXTDOMAIN);
    ?>
" />	
				</div>
		
		</form>
		</div>
		</h3>
				 
		<div class="comment-listing">			 
		<?php 
    geodir_reviewrating_show_comments($comments['comments']);
    ?>
		
		</div>
					 
	</div><?php 
}