Completed
Push — master ( 7fba5d...16c9f9 )
by Marin
14:40
created

Comment_Meta_Container::is_valid_save()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 9
rs 8.8571
cc 5
eloc 6
nc 3
nop 0
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() ) ) {
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
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() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 ) {
1 ignored issue
show
introduced by
Found "!== false". Use Yoda Condition checks, you must
Loading history...
145
			unset( self::$registered_field_names['comment'][ $index ] );
146
		}
147
	}
148
149
}