Completed
Push — try/scrutinizer-wp-coding-stan... ( dcaef3...dfddad )
by
unknown
02:30
created

Comment_Meta_Container::drop_unique_field_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Meta_Datastore;
6
use Carbon_Fields\Datastore\Comment_Meta_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 comment meta container
17
	 *
18
	 * @param string $title Unique title of the container
19
	 **/
20
	public function __construct( $title ) {
21
		parent::__construct( $title );
22
23
		if ( ! $this->get_datastore() ) {
24
			$this->set_datastore( new Comment_Meta_Datastore() );
25
		}
26
	}
27
28
	/**
29
	 * Perform instance initialization after calling setup()
30
	 **/
31
	public function init() {
32
		if ( isset( $_GET['c'] ) && $comment_id = absint( $_GET['c'] ) ) { // Input var okay.
33
			$this->set_comment_id( $comment_id );
34
		}
35
36
		add_action( 'admin_init', array( $this, '_attach' ) );
37
		add_action( 'edit_comment', array( $this, '_save' ) );
38
	}
39
40
	/**
41
	 * Checks whether the current request is valid
42
	 *
43
	 * @return bool
44
	 **/
45 View Code Duplication
	public function is_valid_save() {
1 ignored issue
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...
46
		if ( ! isset( $_REQUEST[ $this->get_nonce_name() ] ) || ! wp_verify_nonce( $_REQUEST[ $this->get_nonce_name() ], $this->get_nonce_name() ) ) { // Input var okay.
0 ignored issues
show
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
47
			return false;
48
		}
49
50
		return true;
51
	}
52
53
	/**
54
	 * Add meta box to the comment
55
	 **/
56
	public function attach() {
57
		add_meta_box(
58
			$this->id,
59
			$this->title,
60
			array( $this, 'render' ),
61
			'comment',
62
			'normal',
63
			'high'
64
		);
65
	}
66
67
	/**
68
	 * Revert the result of attach()
69
	 **/
70 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...
71
		parent::detach();
72
73
		remove_action( 'admin_init', array( $this, '_attach' ) );
74
		remove_action( 'edit_comment', array( $this, '_save' ) );
75
76
		// unregister field names
77
		foreach ( $this->fields as $field ) {
78
			$this->drop_unique_field_name( $field->get_name() );
79
		}
80
	}
81
82
	/**
83
	 * Output the container markup
84
	 **/
85
	public function render() {
86
		include \Carbon_Fields\DIR . '/templates/Container/comment_meta.php';
87
	}
88
89
	/**
90
	 * Set the comment ID the container will operate with.
91
	 *
92
	 * @param int $comment_id
93
	 **/
94
	public function set_comment_id( $comment_id ) {
95
		$this->comment_id = $comment_id;
96
		$this->store->set_id( $comment_id );
97
	}
98
99
	/**
100
	 * Perform save operation after successful is_valid_save() check.
101
	 * The call is propagated to all fields in the container.
102
	 *
103
	 * @param int $comment_id ID of the comment against which save() is ran
104
	 **/
105
	public function save( $comment_id ) {
106
107
		// Unhook action to guarantee single save
108
		remove_action( 'edit_comment', array( $this, '_save' ) );
109
110
		$this->set_comment_id( $comment_id );
111
112
		foreach ( $this->fields as $field ) {
113
			$field->set_value_from_input();
114
			$field->save();
115
		}
116
	}
117
}
118