1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Meta_Datastore; |
6
|
|
|
use Carbon_Fields\Datastore\Datastore; |
7
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Comment meta container class. |
11
|
|
|
*/ |
12
|
|
|
class Comment_Meta_Container extends Container { |
13
|
|
|
protected $comment_id; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create a new container |
17
|
|
|
* |
18
|
|
|
* @param string $unique_id Unique id of the container |
19
|
|
|
* @param string $title title of the container |
20
|
|
|
* @param string $type Type of the container |
21
|
|
|
**/ |
22
|
|
View Code Duplication |
public function __construct( $unique_id, $title, $type ) { |
23
|
|
|
parent::__construct( $unique_id, $title, $type ); |
24
|
|
|
|
25
|
|
|
if ( ! $this->get_datastore() ) { |
26
|
|
|
$this->set_datastore( Datastore::make( 'comment_meta' ), $this->has_default_datastore() ); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Perform instance initialization |
32
|
|
|
**/ |
33
|
|
|
public function init() { |
34
|
|
|
if ( isset( $_GET['c'] ) && $comment_id = absint( $_GET['c'] ) ) { // Input var okay. |
35
|
|
|
$this->set_comment_id( $comment_id ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
39
|
|
|
add_action( 'edit_comment', array( $this, '_save' ) ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Checks whether the current request is valid |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
**/ |
47
|
|
|
public function is_valid_save() { |
48
|
|
|
return $this->verified_nonce_in_request(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Perform save operation after successful is_valid_save() check. |
53
|
|
|
* The call is propagated to all fields in the container. |
54
|
|
|
* |
55
|
|
|
* @param int $comment_id ID of the comment against which save() is ran |
56
|
|
|
**/ |
57
|
|
|
public function save( $comment_id ) { |
58
|
|
|
|
59
|
|
|
// Unhook action to guarantee single save |
60
|
|
|
remove_action( 'edit_comment', array( $this, '_save' ) ); |
61
|
|
|
|
62
|
|
|
$this->set_comment_id( $comment_id ); |
63
|
|
|
|
64
|
|
|
foreach ( $this->fields as $field ) { |
65
|
|
|
$field->set_value_from_input(); |
66
|
|
|
$field->save(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Check container attachment rules against current page request (in admin) |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
**/ |
75
|
|
|
public function is_valid_attach_for_request() { |
76
|
|
|
global $pagenow; |
77
|
|
|
|
78
|
|
|
return ( $pagenow === 'comment.php' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check container attachment rules against object id |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
**/ |
86
|
|
|
public function is_valid_attach_for_object( $object_id = null ) { |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add meta box to the comment |
92
|
|
|
**/ |
93
|
|
|
public function attach() { |
94
|
|
|
add_meta_box( |
95
|
|
|
$this->id, |
96
|
|
|
$this->title, |
97
|
|
|
array( $this, 'render' ), |
98
|
|
|
'comment', |
99
|
|
|
'normal', |
100
|
|
|
'high' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Output the container markup |
106
|
|
|
**/ |
107
|
|
|
public function render() { |
108
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/comment_meta.php'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the comment ID the container will operate with. |
113
|
|
|
* |
114
|
|
|
* @param int $comment_id |
115
|
|
|
**/ |
116
|
|
|
public function set_comment_id( $comment_id ) { |
117
|
|
|
$this->comment_id = $comment_id; |
118
|
|
|
$this->get_datastore()->set_id( $comment_id ); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: