Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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() { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
148 | |||
149 | } |