mainwp-child/class/class-mainwp-child-comments.php

228 lines
6.4 KiB
PHP
Raw Normal View History

2020-05-22 01:09:56 +07:00
<?php
2020-05-21 19:41:44 -04:00
/**
* MainWP Child Comments
*
* This file handles all Child Site comment actions.
2020-06-04 20:04:29 +02:00
*
* @package MainWP\Child
2020-05-21 19:41:44 -04:00
*/
2020-05-22 01:09:56 +07:00
namespace MainWP\Child;
2020-05-21 19:41:44 -04:00
/**
* Class MainWP_Child_Comments
*
2020-06-04 20:04:29 +02:00
* Handles all Child Site comment actions.
2020-05-21 19:41:44 -04:00
*/
2020-05-22 01:09:56 +07:00
class MainWP_Child_Comments {
/**
2020-06-04 20:04:29 +02:00
* Public static variable to hold the single instance of the class.
*
* @var mixed Default null
*/
protected static $instance = null;
/**
2020-06-04 20:04:29 +02:00
* Comments and clauses.
*
* @var string Comments and clauses.
*
* @deprecated Unused element.
*/
private $comments_and_clauses;
/**
* Get Class Name.
*
* @return string
*/
2020-05-22 01:09:56 +07:00
public static function get_class_name() {
return __CLASS__;
}
/**
* MainWP_Child_Comments constructor.
*/
public function __construct() {
2020-05-22 01:09:56 +07:00
$this->comments_and_clauses = '';
}
/**
* Create a public static instance of ainWP_Child_Comments.
*
* @return MainWP_Child_Comments|null
*/
public static function get_instance() {
2020-05-22 01:09:56 +07:00
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* MainWP Child Comment actions: approve, unapprove, spam, unspam, trash, restore, delete.
*/
public function comment_action() {
2020-05-22 01:09:56 +07:00
$action = $_POST['action'];
$commentId = $_POST['id'];
if ( 'approve' === $action ) {
wp_set_comment_status( $commentId, 'approve' );
} elseif ( 'unapprove' === $action ) {
wp_set_comment_status( $commentId, 'hold' );
} elseif ( 'spam' === $action ) {
wp_spam_comment( $commentId );
} elseif ( 'unspam' === $action ) {
wp_unspam_comment( $commentId );
} elseif ( 'trash' === $action ) {
add_action( 'trashed_comment', array( '\MainWP_Child_Links_Checker', 'hook_trashed_comment' ), 10, 1 );
wp_trash_comment( $commentId );
} elseif ( 'restore' === $action ) {
wp_untrash_comment( $commentId );
} elseif ( 'delete' === $action ) {
wp_delete_comment( $commentId, true );
} else {
$information['status'] = 'FAIL';
}
if ( ! isset( $information['status'] ) ) {
$information['status'] = 'SUCCESS';
}
MainWP_Helper::write( $information );
}
/**
* MainWP Child Bulk Comment actions: approve, unapprove, spam, unspam, trash, restore, delete.
*/
public function comment_bulk_action() {
2020-05-22 01:09:56 +07:00
$action = $_POST['action'];
$commentIds = explode( ',', $_POST['ids'] );
$information['success'] = 0;
foreach ( $commentIds as $commentId ) {
if ( $commentId ) {
$information['success'] ++;
if ( 'approve' === $action ) {
wp_set_comment_status( $commentId, 'approve' );
} elseif ( 'unapprove' === $action ) {
wp_set_comment_status( $commentId, 'hold' );
} elseif ( 'spam' === $action ) {
wp_spam_comment( $commentId );
} elseif ( 'unspam' === $action ) {
wp_unspam_comment( $commentId );
} elseif ( 'trash' === $action ) {
wp_trash_comment( $commentId );
} elseif ( 'restore' === $action ) {
wp_untrash_comment( $commentId );
} elseif ( 'delete' === $action ) {
wp_delete_comment( $commentId, true );
} else {
$information['success']--;
}
}
}
MainWP_Helper::write( $information );
}
/**
* Comment WHERE Clauses.
*
2020-06-04 20:04:29 +02:00
* @param array $clauses MySQL WHERE Clause.
*
* @return array $clauses, Array of MySQL WHERE Clauses.
*
* @deprecated Unused Element.
*/
public function comments_clauses( $clauses ) {
2020-05-22 01:09:56 +07:00
if ( $this->comments_and_clauses ) {
$clauses['where'] .= ' ' . $this->comments_and_clauses;
}
return $clauses;
}
/**
* Get all comments.
*/
public function get_all_comments() {
2020-05-21 19:41:44 -04:00
2020-05-22 01:09:56 +07:00
global $wpdb;
add_filter( 'comments_clauses', array( &$this, 'comments_clauses' ) );
if ( isset( $_POST['postId'] ) ) {
$this->comments_and_clauses .= " AND $wpdb->comments.comment_post_ID = " . $_POST['postId'];
} else {
if ( isset( $_POST['keyword'] ) ) {
$this->comments_and_clauses .= " AND $wpdb->comments.comment_content LIKE '%" . $_POST['keyword'] . "%'";
}
if ( isset( $_POST['dtsstart'] ) && '' !== $_POST['dtsstart'] ) {
$this->comments_and_clauses .= " AND $wpdb->comments.comment_date > '" . $_POST['dtsstart'] . "'";
}
if ( isset( $_POST['dtsstop'] ) && '' !== $_POST['dtsstop'] ) {
$this->comments_and_clauses .= " AND $wpdb->comments.comment_date < '" . $_POST['dtsstop'] . "'";
}
}
$maxComments = 50;
if ( defined( 'MAINWP_CHILD_NR_OF_COMMENTS' ) ) {
$maxComments = MAINWP_CHILD_NR_OF_COMMENTS; // to compatible.
}
if ( isset( $_POST['maxRecords'] ) ) {
$maxComments = $_POST['maxRecords'];
}
if ( 0 === $maxComments ) {
$maxComments = 99999;
}
$rslt = $this->get_recent_comments( explode( ',', $_POST['status'] ), $maxComments );
$this->comments_and_clauses = '';
MainWP_Helper::write( $rslt );
}
/**
* Get recent comments.
*
2020-06-04 20:04:29 +02:00
* @param array $pAllowedStatuses An array containing allowed comment statuses.
* @param int $pCount Number of comments to return.
*
* @return array $allComments Array of all comments found.
*/
public function get_recent_comments( $pAllowedStatuses, $pCount ) {
2020-05-22 18:53:57 +07:00
if ( ! function_exists( '\get_comment_author_url' ) ) {
2020-05-22 01:09:56 +07:00
include_once WPINC . '/comment-template.php';
}
$allComments = array();
foreach ( $pAllowedStatuses as $status ) {
$params = array( 'status' => $status );
if ( 0 !== $pCount ) {
$params['number'] = $pCount;
}
$comments = get_comments( $params );
if ( is_array( $comments ) ) {
foreach ( $comments as $comment ) {
$post = get_post( $comment->comment_post_ID );
2020-05-22 01:09:56 +07:00
$outComment = array();
$outComment['id'] = $comment->comment_ID;
$outComment['status'] = wp_get_comment_status( $comment->comment_ID );
$outComment['author'] = $comment->comment_author;
$outComment['author_url'] = get_comment_author_url( $comment->comment_ID );
$outComment['author_ip'] = get_comment_author_IP( $comment->comment_ID );
$outComment['author_email'] = apply_filters( 'comment_email', $comment->comment_author_email );
$outComment['postId'] = $comment->comment_post_ID;
$outComment['postName'] = $post->post_title;
$outComment['comment_count'] = $post->comment_count;
$outComment['content'] = $comment->comment_content;
$outComment['dts'] = strtotime( $comment->comment_date_gmt );
$allComments[] = $outComment;
}
}
}
return $allComments;
}
}