Completed
Push — milestone/2_0/react-ui ( db1aa5...e223fb )
by
unknown
04:25
created

Widget_Container::get_environment_for_request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 2
c 2
b 2
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\App;
6
7
/**
8
 * Widget container class
9
 */
10
class Widget_Container extends Container {
11
12
	/**
13
	 * Create a new widget
14
	 *
15
	 * @param string $unique_id Unique ID of the widget
16
	 * @param string $title Ignored
17
	 * @param string $type Container type
18
	 **/
19
	public function __construct( $unique_id, $title, $type ) {
20
		$this->id = $unique_id;
21
		$this->title = '';
22
		$this->type = $type;
23
24
		$this->condition_collection = App::resolve( 'container_condition_fulfillable_collection' );
25
		$this->condition_collection->set_condition_type_list(
26
			array_merge( $this->get_condition_types( true ), $this->get_condition_types( false ) ),
27
			true
28
		);
29
	}
30
31
	/**
32
	 * Perform instance initialization
33
	 **/
34
	public function init() {
35
		$this->_attach();
36
		$this->render();
37
38
		return $this;
39
	}
40
41
	/**
42
	 * Get environment array for page request (in admin)
43
	 *
44
	 * @return array
45
	 **/
46
	protected function get_environment_for_request() {
47
		return array();
48
	}
49
50
	/**
51
	 * Perform checks whether the container should be attached during the current request
52
	 *
53
	 * @return bool True if the container is allowed to be attached
54
	 **/
55
	public function is_valid_attach_for_request() {
56
		$screen = get_current_screen();
57
		$input = stripslashes_deep( $_REQUEST );
1 ignored issue
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
58
		$request_action = isset( $input['action'] ) ? $input['action'] : '';
59
		$is_widget_save = ( $request_action === 'save-widget' );
60
61
		if ( ( ! $screen || $screen->id !== 'widgets' ) && ! $is_widget_save ) {
62
			return false;
63
		}
64
65
		return $this->static_conditions_pass();
66
	}
67
68
	/**
69
	 * Get environment array for object id
70
	 *
71
	 * @return array
72
	 */
73
	protected function get_environment_for_object( $object_id ) {
74
		return array();
75
	}
76
77
	/**
78
	 * Check container attachment rules against object id
79
	 *
80
	 * @param int $object_id
81
	 * @return bool
82
	 **/
83
	public function is_valid_attach_for_object( $object_id = null ) {
84
		return $this->all_conditions_pass( intval( $object_id ) );
85
	}
86
87
	/* Checks whether the current save request is valid
88
	 *
89
	 * @return bool
90
	 **/
91
	public function is_valid_save( $object_id = 0 ) {
92
		if ( ! $this->is_valid_attach_for_request() ) {
93
			return false;
94
		}
95
		return $this->is_valid_attach_for_object( $object_id );
96
	}
97
98
	/**
99
	 * Output the container markup
100
	 **/
101
	public function render() {
102
		include \Carbon_Fields\DIR . '/templates/Container/widget.php';
103
	}
104
105
	/**
106
	 * Returns an array that holds the container data, suitable for JSON representation.
107
	 * This data will be available in the Underscore template and the Backbone Model.
108
	 *
109
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
110
	 * @return array
111
	 */
112
	public function to_json( $load ) {
113
		return parent::to_json( false );
114
	}
115
}
116
117