class-comment-meta.php
5 months ago
class-comment.php
5 months ago
class-meta.php
5 months ago
class-options.php
5 months ago
class-post-meta.php
5 months ago
class-post.php
5 months ago
class-terms.php
4 months ago
class-user-meta.php
5 months ago
class-user.php
5 months ago
class-comment.php
268 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Source\Core; |
| 4 | |
| 5 | use SearchRegex\Source; |
| 6 | use SearchRegex\Search; |
| 7 | use SearchRegex\Sql; |
| 8 | use SearchRegex\Plugin; |
| 9 | |
| 10 | /** |
| 11 | * Source for comments |
| 12 | */ |
| 13 | class Comment extends Source\Source { |
| 14 | use Source\Has_Meta; |
| 15 | |
| 16 | public function get_actions( Search\Result $result ) { |
| 17 | $id = $result->get_row_id(); |
| 18 | $link = get_edit_comment_link( $id ); |
| 19 | $comment = get_comment( $id ); |
| 20 | |
| 21 | if ( $link && is_object( $comment ) ) { |
| 22 | $view = get_comment_link( $comment ); |
| 23 | |
| 24 | $actions = [ |
| 25 | 'edit' => str_replace( '&', '&', $link ), |
| 26 | ]; |
| 27 | if ( $view !== false && $view !== '' ) { |
| 28 | $actions['view'] = $view; |
| 29 | } |
| 30 | return $actions; |
| 31 | } |
| 32 | |
| 33 | return []; |
| 34 | } |
| 35 | |
| 36 | public function get_table_id() { |
| 37 | return 'comment_ID'; |
| 38 | } |
| 39 | |
| 40 | public function get_table_name() { |
| 41 | global $wpdb; |
| 42 | |
| 43 | return $wpdb->comments; |
| 44 | } |
| 45 | |
| 46 | public function get_title_column() { |
| 47 | return 'comment_author'; |
| 48 | } |
| 49 | |
| 50 | public function get_row_columns( $row_id ) { |
| 51 | $meta = $this->get_meta( get_comment_meta( $row_id ) ); |
| 52 | $row_columns = parent::get_row_columns( $row_id ); |
| 53 | if ( $row_columns instanceof \WP_Error ) { |
| 54 | return $row_columns; |
| 55 | } |
| 56 | |
| 57 | return [ ...$row_columns, $meta ]; |
| 58 | } |
| 59 | |
| 60 | public function save( $row_id, array $changes ) { |
| 61 | $comment = $this->get_columns_to_change( $changes ); |
| 62 | $comment['comment_ID'] = $row_id; |
| 63 | |
| 64 | $this->process_meta( $row_id, 'comment', $changes ); |
| 65 | |
| 66 | if ( count( $comment ) > 1 ) { |
| 67 | // wp_update_comment expects slashes to be present, which are then removed |
| 68 | if ( isset( $comment['comment_content'] ) && is_string( $comment['comment_content'] ) ) { |
| 69 | $comment['comment_content'] = wp_slash( $comment['comment_content'] ); |
| 70 | } |
| 71 | |
| 72 | $this->log_save( 'comment', $comment ); |
| 73 | |
| 74 | $result = true; |
| 75 | |
| 76 | if ( Plugin\Settings::init()->can_save() ) { |
| 77 | $result = wp_update_comment( $comment ); |
| 78 | } |
| 79 | |
| 80 | if ( $result ) { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | return new \WP_Error( 'searchregex', 'Failed to update comment.' ); |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | public function delete_row( $row_id ) { |
| 91 | $this->log_save( 'delete comment', $row_id ); |
| 92 | |
| 93 | if ( Plugin\Settings::init()->can_save() ) { |
| 94 | if ( wp_delete_comment( $row_id, true ) ) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return new \WP_Error( 'searchregex_delete', 'Failed to delete comment', 401 ); |
| 99 | } |
| 100 | |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | public function autocomplete( array $column, $value ) { |
| 105 | global $wpdb; |
| 106 | |
| 107 | if ( ! isset( $column['column'] ) ) { |
| 108 | return []; |
| 109 | } |
| 110 | |
| 111 | if ( $column['column'] === 'comment_post_ID' ) { |
| 112 | return Source\Autocomplete::get_post( $value, Sql\Value::column( 'ID' ), Sql\Value::column( 'post_title' ) ); |
| 113 | } |
| 114 | |
| 115 | if ( $column['column'] === 'user_id' ) { |
| 116 | return Source\Autocomplete::get_user( $value ); |
| 117 | } |
| 118 | |
| 119 | if ( $column['column'] === 'meta' ) { |
| 120 | return Source\Autocomplete::get_meta( Sql\Value::table( 'commentmeta' ), $value ); |
| 121 | } |
| 122 | |
| 123 | if ( $column['column'] === 'comment_parent' ) { |
| 124 | return Source\Autocomplete::get_comment( $value ); |
| 125 | } |
| 126 | |
| 127 | // General text searches |
| 128 | if ( $column['column'] === 'comment_author' || $column['column'] === 'comment_author_email' ) { |
| 129 | // phpcs:ignore |
| 130 | return $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT " . $column['column'] . " as id," . $column['column'] . " as value FROM {$wpdb->comments} WHERE " . $column['column'] . " LIKE %s LIMIT %d", '%' . $wpdb->esc_like( $value ) . '%', self::AUTOCOMPLETE_LIMIT ) ); |
| 131 | } |
| 132 | |
| 133 | return []; |
| 134 | } |
| 135 | |
| 136 | public function get_schema() { |
| 137 | global $wpdb; |
| 138 | |
| 139 | return [ |
| 140 | 'name' => __( 'Comments', 'search-regex' ), |
| 141 | 'table' => $wpdb->comments, |
| 142 | 'columns' => [ |
| 143 | [ |
| 144 | 'column' => 'comment_ID', |
| 145 | 'type' => 'integer', |
| 146 | 'title' => __( 'ID', 'search-regex' ), |
| 147 | 'modify' => false, |
| 148 | ], |
| 149 | [ |
| 150 | 'column' => 'comment_post_ID', |
| 151 | 'type' => 'integer', |
| 152 | 'title' => __( 'Post ID', 'search-regex' ), |
| 153 | 'options' => 'api', |
| 154 | 'joined_by' => 'post', |
| 155 | ], |
| 156 | [ |
| 157 | 'column' => 'comment_author', |
| 158 | 'type' => 'string', |
| 159 | 'title' => __( 'Author', 'search-regex' ), |
| 160 | 'options' => 'api', |
| 161 | 'global' => true, |
| 162 | ], |
| 163 | [ |
| 164 | 'column' => 'comment_author_email', |
| 165 | 'type' => 'string', |
| 166 | 'title' => __( 'Email', 'search-regex' ), |
| 167 | 'options' => 'api', |
| 168 | 'global' => true, |
| 169 | ], |
| 170 | [ |
| 171 | 'column' => 'comment_author_url', |
| 172 | 'type' => 'string', |
| 173 | 'title' => __( 'URL', 'search-regex' ), |
| 174 | 'global' => true, |
| 175 | ], |
| 176 | [ |
| 177 | 'column' => 'comment_content', |
| 178 | 'type' => 'string', |
| 179 | 'title' => __( 'Content', 'search-regex' ), |
| 180 | 'multiline' => true, |
| 181 | 'global' => true, |
| 182 | ], |
| 183 | [ |
| 184 | 'column' => 'comment_author_IP', |
| 185 | 'type' => 'string', |
| 186 | 'title' => __( 'IP', 'search-regex' ), |
| 187 | ], |
| 188 | [ |
| 189 | 'column' => 'comment_date', |
| 190 | 'type' => 'date', |
| 191 | 'title' => __( 'Date', 'search-regex' ), |
| 192 | 'source' => 'date', |
| 193 | ], |
| 194 | [ |
| 195 | 'column' => 'comment_date_gmt', |
| 196 | 'type' => 'date', |
| 197 | 'title' => __( 'Date GMT', 'search-regex' ), |
| 198 | 'source' => 'date', |
| 199 | ], |
| 200 | [ |
| 201 | 'column' => 'comment_approved', |
| 202 | 'type' => 'member', |
| 203 | 'options' => [ |
| 204 | [ |
| 205 | 'value' => '0', |
| 206 | 'label' => __( 'Unapproved', 'search-regex' ), |
| 207 | ], |
| 208 | [ |
| 209 | 'value' => '1', |
| 210 | 'label' => __( 'Approved', 'search-regex' ), |
| 211 | ], |
| 212 | [ |
| 213 | 'value' => 'spam', |
| 214 | 'label' => __( 'Spam', 'search-regex' ), |
| 215 | ], |
| 216 | ], |
| 217 | 'title' => __( 'Approval Status', 'search-regex' ), |
| 218 | ], |
| 219 | [ |
| 220 | 'column' => 'comment_agent', |
| 221 | 'type' => 'string', |
| 222 | 'title' => __( 'User Agent', 'search-regex' ), |
| 223 | ], |
| 224 | [ |
| 225 | 'column' => 'comment_type', |
| 226 | 'type' => 'member', |
| 227 | 'title' => __( 'Type', 'search-regex' ), |
| 228 | 'options' => [ |
| 229 | [ |
| 230 | 'value' => 'pingback', |
| 231 | 'label' => __( 'Pingback', 'search-regex' ), |
| 232 | ], |
| 233 | [ |
| 234 | 'value' => 'trackback', |
| 235 | 'label' => __( 'Trackback', 'search-regex' ), |
| 236 | ], |
| 237 | [ |
| 238 | 'value' => 'comment', |
| 239 | 'label' => __( 'Comment', 'search-regex' ), |
| 240 | ], |
| 241 | ], |
| 242 | ], |
| 243 | [ |
| 244 | 'column' => 'comment_parent', |
| 245 | 'type' => 'integer', |
| 246 | 'title' => __( 'Parent Comment', 'search-regex' ), |
| 247 | 'options' => 'api', |
| 248 | ], |
| 249 | [ |
| 250 | 'column' => 'user_id', |
| 251 | 'type' => 'integer', |
| 252 | 'title' => __( 'User ID', 'search-regex' ), |
| 253 | 'options' => 'api', |
| 254 | 'source' => 'user', |
| 255 | 'joined_by' => 'user', |
| 256 | ], |
| 257 | [ |
| 258 | 'column' => 'meta', |
| 259 | 'type' => 'keyvalue', |
| 260 | 'title' => __( 'Comment meta', 'search-regex' ), |
| 261 | 'options' => 'api', |
| 262 | 'join' => 'commentmeta', |
| 263 | ], |
| 264 | ], |
| 265 | ]; |
| 266 | } |
| 267 | } |
| 268 |