Skip to content

Commit e4d3b0b

Browse files
committed
Move more logic to parent class
1 parent db5cd78 commit e4d3b0b

6 files changed

+49
-81
lines changed

classes/Message/CommentModeration.php

+4-20
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@
1818
* @since 2.0.0
1919
*/
2020
class CommentModeration extends Section {
21-
/**
22-
* Constructor.
23-
*
24-
* @since 2.0.0
25-
* @access public
26-
*
27-
* @param array $entries The comment moderation entries.
28-
* @param WP_User $user The current user.
29-
*/
30-
public function __construct( $entries, WP_User $user = null ) {
31-
parent::__construct( $user );
32-
33-
foreach ( $entries as $comment => $time ) {
34-
$this->entries[] = $this->get_single_message( get_comment( $comment ), $time );
35-
}
36-
}
37-
3821
/**
3922
* Returns the comment moderation section message.
4023
*
@@ -85,12 +68,13 @@ public function get_message() {
8568
* @since 2.0.0
8669
* @access protected
8770
*
88-
* @param WP_Comment $comment The comment object.
89-
* @param int $time The timestamp when the comment was written.
71+
* @param int $comment The comment ID.
72+
* @param int $time The timestamp when the comment was written.
9073
*
9174
* @return string The comment moderation message.
9275
*/
93-
protected function get_single_message( WP_Comment $comment, $time ) {
76+
protected function get_single_message( $comment, $time ) {
77+
$comment = get_comment( $comment );
9478
if ( null === $comment || '0' !== $comment->comment_approved ) {
9579
return '';
9680
}

classes/Message/CommentNotification.php

+5-18
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@
1616
* Responsible for creating the comment notification section
1717
*/
1818
class CommentNotification extends CommentModeration {
19-
/**
20-
* Constructor.
21-
*
22-
* @param array $entries The comment moderation entries.
23-
* @param WP_User $user The current user.
24-
*/
25-
public function __construct( $entries, WP_User $user = null ) {
26-
parent::__construct( $entries, $user );
27-
28-
$this->entries = array();
29-
foreach ( $entries as $comment => $time ) {
30-
$this->entries[] = $this->get_single_message( get_comment( $comment ), $time );
31-
}
32-
}
33-
3419
/**
3520
* Get comment moderation section message.
3621
*
@@ -75,12 +60,14 @@ public function get_message() {
7560
/**
7661
* Get the comment moderation message.
7762
*
78-
* @param WP_Comment $comment The comment object.
79-
* @param int $time The timestamp when the comment was written.
63+
* @param int $comment The comment ID.
64+
* @param int $time The timestamp when the comment was written.
8065
*
8166
* @return string The comment moderation message.
8267
*/
83-
protected function get_single_message( WP_Comment $comment, $time ) {
68+
protected function get_single_message( $comment, $time ) {
69+
$comment = get_comment( $comment );
70+
8471
if ( null === $comment ) {
8572
return '';
8673
}

classes/Message/CoreUpdate.php

+20-7
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,37 @@ class CoreUpdate extends Section {
1818
/**
1919
* The comment moderation entries.
2020
*
21+
* @since 2.0.0
22+
* @access protected
23+
*
2124
* @var array
2225
*/
2326
protected $entries;
2427

28+
/**
29+
* The event type.
30+
*
31+
* @since 2.0.0
32+
* @access protected
33+
*
34+
* @var string
35+
*/
36+
protected $event;
37+
2538
/**
2639
* Constructor.
2740
*
41+
* @since 2.0.0
42+
* @access protected
43+
*
2844
* @param array $entries The core update entries.
2945
* @param WP_User $user The current user.
3046
* @param string $event The current event.
3147
*/
3248
public function __construct( $entries, WP_User $user = null, $event ) {
33-
parent::__construct( $user );
49+
parent::__construct( $entries, $user );
3450

35-
foreach ( $entries as $version => $time ) {
36-
$this->entries[] = $this->get_single_message( $version, $time, $event );
37-
}
51+
$this->event = $event;
3852
}
3953

4054
/**
@@ -51,12 +65,11 @@ public function get_message() {
5165
*
5266
* @param string $version The version WordPress was updated to.
5367
* @param int $time The timestamp when the update happened.
54-
* @param string $event The current event.
5568
*
5669
* @return string The core update message.
5770
*/
58-
protected function get_single_message( $version, $time, $event ) {
59-
if ( 'core_update_success' === $event ) {
71+
protected function get_single_message( $version, $time ) {
72+
if ( 'core_update_success' === $this->event ) {
6073
return $this->get_core_update_success_message( $version, $time );
6174
} else {
6275
return $this->get_core_update_fail_message( $version, $time );

classes/Message/PasswordChangeNotification.php

-17
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,6 @@
1717
* @since 2.0.0
1818
*/
1919
class PasswordChangeNotification extends Section {
20-
/**
21-
* Constructor.
22-
*
23-
* @since 2.0.0
24-
* @access public
25-
*
26-
* @param array $entries The user notification entries.
27-
* @param WP_User $user The current user.
28-
*/
29-
public function __construct( $entries, WP_User $user = null ) {
30-
parent::__construct( $user );
31-
32-
foreach ( $entries as $user_id => $time ) {
33-
$this->entries[] = $this->get_single_message( $user_id, $time );
34-
}
35-
}
36-
3720
/**
3821
* Get password change notification section message.
3922
*

classes/Message/Section.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ abstract class Section implements MessageInterface {
4747
*/
4848
abstract public function get_message();
4949

50+
/**
51+
* Returns a single message in the section.
52+
*
53+
* @since 2.0.0
54+
* @access protected
55+
*
56+
* @param string|int $entry The single entry item. For example a user ID or comment ID.
57+
* @param int $time The timestamp when the update happened.
58+
*
59+
* @return string The single message.
60+
*/
61+
abstract protected function get_single_message( $entry, $time );
62+
5063
/**
5164
* Constructor.
5265
*
@@ -55,9 +68,14 @@ abstract public function get_message();
5568
* @since 2.0.0
5669
* @access public
5770
*
58-
* @param WP_User $user The current user.
71+
* @param array $entries The message entries.
72+
* @param WP_User $user The current user.
5973
*/
60-
public function __construct( WP_User $user = null ) {
74+
public function __construct( $entries, WP_User $user = null ) {
75+
foreach ( $entries as $entry => $time ) {
76+
$this->entries[] = $this->get_single_message( $entry, $time );
77+
}
78+
6179
$this->user = $user;
6280
}
6381
}

classes/Message/UserNotification.php

-17
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,6 @@
1717
* @since 2.0.0
1818
*/
1919
class UserNotification extends Section {
20-
/**
21-
* Constructor.
22-
*
23-
* @since 2.0.0
24-
* @access public
25-
*
26-
* @param array $entries The user notification entries.
27-
* @param WP_User $user The current user.
28-
*/
29-
public function __construct( $entries, WP_User $user = null ) {
30-
parent::__construct( $user );
31-
32-
foreach ( $entries as $user_id => $time ) {
33-
$this->entries[] = $this->get_single_message( $user_id, $time );
34-
}
35-
}
36-
3720
/**
3821
* Returns the core update section message.
3922
*

0 commit comments

Comments
 (0)