Complex classes like WC_Comments often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Comments, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class WC_Comments { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Hook in methods. |
||
| 22 | */ |
||
| 23 | public static function init() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Exclude order comments from queries and RSS. |
||
| 58 | * |
||
| 59 | * This code should exclude shop_order comments from queries. Some queries (like the recent comments widget on the dashboard) are hardcoded. |
||
| 60 | * and are not filtered, however, the code current_user_can( 'read_post', $comment->comment_post_ID ) should keep them safe since only admin and. |
||
| 61 | * shop managers can view orders anyway. |
||
| 62 | * |
||
| 63 | * The frontend view order pages get around this filter by using remove_filter('comments_clauses', array( 'WC_Comments' ,'exclude_order_comments'), 10, 1 ); |
||
| 64 | * @param array $clauses |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public static function exclude_order_comments( $clauses ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Exclude order comments from queries and RSS. |
||
| 93 | * @param string $join |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public static function exclude_order_comments_from_feed_join( $join ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Exclude order comments from queries and RSS. |
||
| 108 | * @param string $where |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public static function exclude_order_comments_from_feed_where( $where ) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Exclude webhook comments from queries and RSS. |
||
| 125 | * @since 2.2 |
||
| 126 | * @param array $clauses |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public static function exclude_webhook_comments( $clauses ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Exclude webhook comments from queries and RSS. |
||
| 151 | * @since 2.2 |
||
| 152 | * @param string $join |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public static function exclude_webhook_comments_from_feed_join( $join ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Exclude webhook comments from queries and RSS. |
||
| 167 | * @since 2.1 |
||
| 168 | * @param string $where |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public static function exclude_webhook_comments_from_feed_where( $where ) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Validate the comment ratings. |
||
| 185 | * @param array $comment_data |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public static function check_comment_rating( $comment_data ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Rating field for comments. |
||
| 207 | * @param int $comment_id |
||
| 208 | */ |
||
| 209 | public static function add_comment_rating( $comment_id ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Modify recipient of review email. |
||
| 220 | * @param array $emails |
||
| 221 | * @param int $comment_id |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | public static function comment_moderation_recipients( $emails, $comment_id ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Ensure product average rating and review count is kept up to date. |
||
| 236 | * @param int $post_id |
||
| 237 | */ |
||
| 238 | public static function clear_transients( $post_id ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Delete comments count cache whenever there is |
||
| 246 | * new comment or the status of a comment changes. Cache |
||
| 247 | * will be regenerated next time WC_Comments::wp_count_comments() |
||
| 248 | * is called. |
||
| 249 | * |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | public static function delete_comments_count_cache() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Remove order notes from wp_count_comments(). |
||
| 258 | * @since 2.2 |
||
| 259 | * @param object $stats |
||
| 260 | * @param int $post_id |
||
| 261 | * @return object |
||
| 262 | */ |
||
| 263 | public static function wp_count_comments( $stats, $post_id ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Make sure WP displays avatars for comments with the `review` type. |
||
| 305 | * @since 2.3 |
||
| 306 | * @param array $comment_types |
||
| 307 | * @return array |
||
| 308 | */ |
||
| 309 | public static function add_avatar_for_review_comment_type( $comment_types ) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Determine if a review is from a verified owner at submission. |
||
| 315 | * @param int $comment_id |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public static function add_comment_purchase_verification( $comment_id ) { |
||
| 327 | } |
||
| 328 | |||
| 330 |