1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Comment_Meta_Datastore; |
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Comment meta container class. |
10
|
|
|
*/ |
11
|
|
|
class Comment_Meta_Container extends Container { |
12
|
|
|
protected $comment_id; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new comment meta container |
16
|
|
|
* |
17
|
|
|
* @param string $title Unique title of the container |
18
|
|
|
**/ |
19
|
|
|
public function __construct( $title ) { |
20
|
|
|
parent::__construct( $title ); |
21
|
|
|
|
22
|
|
|
if ( ! $this->get_datastore() ) { |
23
|
|
|
$this->set_datastore( new Comment_Meta_Datastore() ); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Perform instance initialization after calling setup() |
29
|
|
|
**/ |
30
|
|
|
public function init() { |
31
|
|
|
if ( isset( $_GET['c'] ) && $comment_id = absint( $_GET['c'] ) ) { // Input var okay. |
32
|
|
|
$this->set_comment_id( $comment_id ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
add_action( 'admin_init', array( $this, '_attach' ) ); |
36
|
|
|
add_action( 'edit_comment', array( $this, '_save' ) ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Checks whether the current request is valid |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
**/ |
44
|
|
|
public function is_valid_save() { |
45
|
|
|
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
46
|
|
|
return false; |
47
|
|
|
} else if ( ! isset( $_REQUEST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_REQUEST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { |
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Add meta box to the comment |
56
|
|
|
**/ |
57
|
|
|
public function attach() { |
58
|
|
|
add_meta_box( |
59
|
|
|
$this->id, |
60
|
|
|
$this->title, |
61
|
|
|
array( $this, 'render' ), |
62
|
|
|
'comment', |
63
|
|
|
'normal', |
64
|
|
|
'high' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Revert the result of attach() |
70
|
|
|
**/ |
71
|
|
View Code Duplication |
public function detach() { |
|
|
|
|
72
|
|
|
parent::detach(); |
73
|
|
|
|
74
|
|
|
remove_action( 'admin_init', array( $this, '_attach' ) ); |
75
|
|
|
remove_action( 'edit_comment', array( $this, '_save' ) ); |
76
|
|
|
|
77
|
|
|
// unregister field names |
78
|
|
|
foreach ( $this->fields as $field ) { |
79
|
|
|
$this->drop_unique_field_name( $field->get_name() ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Output the container markup |
85
|
|
|
**/ |
86
|
|
|
public function render() { |
87
|
|
|
include \Carbon_Fields\DIR . '/templates/Container/comment_meta.php'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the comment ID the container will operate with. |
92
|
|
|
* |
93
|
|
|
* @param int $comment_id |
94
|
|
|
**/ |
95
|
|
|
public function set_comment_id( $comment_id ) { |
96
|
|
|
$this->comment_id = $comment_id; |
97
|
|
|
$this->store->set_id( $comment_id ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Perform save operation after successful is_valid_save() check. |
102
|
|
|
* The call is propagated to all fields in the container. |
103
|
|
|
* |
104
|
|
|
* @param int $comment_id ID of the comment against which save() is ran |
105
|
|
|
**/ |
106
|
|
|
public function save( $comment_id ) { |
107
|
|
|
|
108
|
|
|
// Unhook action to guarantee single save |
109
|
|
|
remove_action( 'edit_comment', array( $this, '_save' ) ); |
110
|
|
|
|
111
|
|
|
$this->set_comment_id( $comment_id ); |
112
|
|
|
|
113
|
|
|
foreach ( $this->fields as $field ) { |
114
|
|
|
$field->set_value_from_input(); |
115
|
|
|
$field->save(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Perform checks whether there is a field registered with the name $name. |
121
|
|
|
* If not, the field name is recorded. |
122
|
|
|
* |
123
|
|
|
* @param string $name |
124
|
|
|
**/ |
125
|
|
|
public function verify_unique_field_name( $name ) { |
126
|
|
|
if ( ! isset( self::$registered_field_names['comment'] ) ) { |
127
|
|
|
self::$registered_field_names['comment'] = array(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ( in_array( $name, self::$registered_field_names['comment'] ) ) { |
131
|
|
|
throw new Incorrect_Syntax_Exception( 'Field name "' . $name . '" already registered' ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
self::$registered_field_names['comment'][] = $name; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove field name $name from the list of unique field names |
139
|
|
|
* |
140
|
|
|
* @param string $name |
141
|
|
|
**/ |
142
|
|
|
public function drop_unique_field_name( $name ) { |
143
|
|
|
$index = array_search( $name, self::$registered_field_names['comment'] ); |
144
|
|
|
if ( $index !== false ) { |
|
|
|
|
145
|
|
|
unset( self::$registered_field_names['comment'][ $index ] ); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |