| 1 |
<?php |
| 2 |
/** |
| 3 |
* Comments handling |
| 4 |
* |
| 5 |
* Handles all comments operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.4.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the comments functionality. |
| 16 |
* |
| 17 |
* Handles all comments operations and detection. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.4.0 |
| 22 |
*/ |
| 23 |
class Comment { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a fully qualified comment name. |
| 35 |
* |
| 36 |
* @param mixed $id Optional. The comment id or WP_comment instance. |
| 37 |
* @return string The comment pseudo name if detected. |
| 38 |
* @since 1.4.0 |
| 39 |
*/ |
| 40 |
public static function get_full_comment_name( $id ) { |
| 41 |
$comment = null; |
| 42 |
if ( is_numeric( $id ) ) { |
| 43 |
$comment = get_comment( $id ); |
| 44 |
} |
| 45 |
if ( $id instanceof \WP_Comment ) { |
| 46 |
$comment = $id; |
| 47 |
} |
| 48 |
if ( $comment instanceof \WP_Comment ) { |
| 49 |
return sprintf( '"%s" (comment ID %s)', wp_trim_words( wp_kses( $comment->comment_content, [] ), 8 ), $comment->comment_ID ); |
| 50 |
} else { |
| 51 |
return 'unknow comment'; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get comment types (like get_post_types() do for posts). |
| 57 |
* |
| 58 |
* @return array An array of comment type names. |
| 59 |
* @since 3.0.0 |
| 60 |
*/ |
| 61 |
public static function get_types() { |
| 62 |
$cache_id = 'data/comment_types'; |
| 63 |
$types = Cache::get( $cache_id, true ); |
| 64 |
if ( ! $types ) { |
| 65 |
$types = [ |
| 66 |
'comment' => 'comment', |
| 67 |
'trackback' => 'trackback', |
| 68 |
'pingback' => 'pingback', |
| 69 |
'pings' => 'pings', |
| 70 |
]; |
| 71 |
global $wpdb; |
| 72 |
$sql = 'SELECT DISTINCT `comment_type` FROM `' . $wpdb->comments . '`;'; |
| 73 |
// phpcs:ignore |
| 74 |
$comt = $wpdb->get_results( $sql, ARRAY_A ); |
| 75 |
foreach ( $comt as $t ) { |
| 76 |
if ( ! array_key_exists( $t['comment_type'], $types ) ) { |
| 77 |
$types[ strtolower( $t['comment_type'] ) ] = strtolower( str_replace( '_', ' ', $t['comment_type'] ) ); |
| 78 |
} |
| 79 |
} |
| 80 |
Cache::set( $cache_id, $types, 'longquery', true ); |
| 81 |
} |
| 82 |
return $types; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get comment status (like get_post_status() do for posts). |
| 87 |
* |
| 88 |
* @return array An array of comment status names. |
| 89 |
* @since 3.0.0 |
| 90 |
*/ |
| 91 |
public static function get_status() { |
| 92 |
$cache_id = 'data/comment_status'; |
| 93 |
$status = Cache::get( $cache_id, true ); |
| 94 |
if ( ! $status ) { |
| 95 |
$status = [ |
| 96 |
'hold' => 'Unapproved', |
| 97 |
'approve' => 'Approved', |
| 98 |
'spam' => 'Spam', |
| 99 |
'trash' => 'Trash', |
| 100 |
]; |
| 101 |
global $wpdb; |
| 102 |
$sql = 'SELECT DISTINCT `comment_approved` FROM `' . $wpdb->comments . '`;'; |
| 103 |
// phpcs:ignore |
| 104 |
$comt = $wpdb->get_results( $sql, ARRAY_A ); |
| 105 |
foreach ( $comt as $t ) { |
| 106 |
if ( ! array_key_exists( $t['comment_approved'], $status ) && ! is_numeric( $t['comment_approved'] ) ) { |
| 107 |
$status[ strtolower( $t['comment_approved'] ) ] = strtolower( str_replace( '_', ' ', $t['comment_approved'] ) ); |
| 108 |
} |
| 109 |
} |
| 110 |
Cache::set( $cache_id, $status, 'longquery', true ); |
| 111 |
} |
| 112 |
return $status; |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|