Completed
Push — master ( 37bdb3...5c343e )
by David
03:17 queued 10s
created

Wordlift_Admin_Publisher_Element   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 151
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B render() 0 32 2
A select() 0 13 1
B create() 0 39 1
1
<?php
2
/**
3
 * Elements: Publisher element.
4
 *
5
 * A complex element that displays the current publisher with a select to select
6
 * another one from existing Organizations/Persons or a form to create a new one.
7
 *
8
 * @since      3.11.0
9
 * @package    Wordlift
10
 * @subpackage Wordlift/admin
11
 */
12
13
/**
14
 * Define the {@link Wordlift_Admin_Publisher_Element} class.
15
 *
16
 * @since      3.11.0
17
 * @package    Wordlift
18
 * @subpackage Wordlift/admin
19
 */
20
class Wordlift_Admin_Publisher_Element extends Wordlift_Admin_Author_Element {
21
22
	/**
23
	 * The {@link Wordlift_Configuration_Service} instance.
24
	 *
25
	 * @since  3.11.0
26
	 * @access private
27
	 * @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
28
	 */
29
	private $configuration_service;
30
31
	/**
32
	 * The {@link Wordlift_Publisher_Service} instance.
33
	 *
34
	 * @since  3.11.0
35
	 * @access private
36
	 * @var \Wordlift_Publisher_Service $publisher_service The {@link Wordlift_Publisher_Service} instance.
37
	 */
38
	private $publisher_service;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
39
40
	/**
41
	 * @var Wordlift_Admin_Tabs_Element
42
	 */
43
	private $tabs_element;
44
45
	/**
46
	 * Create a {@link Wordlift_Admin_Publisher_Element} instance.
47
	 *
48
	 * @since 3.11.0
49
	 *
50
	 * @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance.
51
	 * @param \Wordlift_Publisher_Service     $publisher_service     The {@link Wordlift_Publisher_Service} instance.
52
	 * @param \Wordlift_Admin_Tabs_Element    $tabs_element          The {@link Wordlift_Admin_Tabs_Element} instance.
53
	 * @param \Wordlift_Admin_Select2_Element $select_element        The {@link Wordlift_Admin_Select_Element} instance.
54
	 */
55
	function __construct( $configuration_service, $publisher_service, $tabs_element, $select_element ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
		parent::__construct($publisher_service, $select_element);
57
58
		$this->configuration_service = $configuration_service;
59
		$this->publisher_service     = $publisher_service;
60
61
		// Child elements.
62
		$this->tabs_element   = $tabs_element;
63
64
	}
65
66
	/**
67
	 * @inheritdoc
68
	 */
69
	public function render( $args ) {
70
71
		// Parse the arguments and merge with default values.
72
		$params = wp_parse_args( $args, array(
73
			'id'   => uniqid( 'wl-input-' ),
74
			'name' => uniqid( 'wl-input-' ),
75
		) );
76
77
		// Get the number of potential candidates as publishers.
78
		$count = $this->publisher_service->count();
79
80
		$this->tabs_element->render( array(
81
			'tabs'   => array(
82
				array(
83
					'label'    => __( 'Select an Existing Publisher', 'wordlift' ),
84
					'callback' => array( $this, 'select' ),
85
					'args'     => $params,
86
				),
87
				array(
88
					'label'    => __( 'Create a New Publisher', 'wordlift' ),
89
					'callback' => array( $this, 'create' ),
90
					'args'     => $params,
91
				),
92
			),
93
			// Set the default tab according to the number of potential publishers
94
			// configured in WP: 0 = select, 1 = create.
95
			'active' => 0 === $count ? 1 : 0,
96
		) );
97
98
		// Finally return the element instance.
99
		return $this;
100
	}
101
102
	/**
103
	 * Render the publisher's select.
104
	 *
105
	 * @since 3.11.0
106
	 *
107
	 * @param array $params An array of parameters.
108
	 */
109
	public function select( $params ) {
110
111
		// Get the configured publisher id. In case a publisher id is already configured
112
		// this must be pre-loaded in the options.
113
		$publisher_id = $this->configuration_service->get_publisher_id();
114
115
		// Get the publisher data.
116
		$data = $this->publisher_service->query();
117
118
		// Call the select internal render.
119
		$this->do_render( $params, $publisher_id, $data );
120
121
	}
122
123
	/**
124
	 * Render the 'create publisher' form.
125
	 *
126
	 * @since 3.11.0
127
	 *
128
	 * @param array $params An array of parameters.
129
	 */
130
	public function create( $params ) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
		?>
132
		<p>
133
			<strong><?php esc_html_e( 'Are you publishing as an individual or as a company?', 'wordlift' ) ?></strong>
134
		</p>
135
		<p id="wl-publisher-type">
136
			<span>
137
				<input id="wl-publisher-person" type="radio"
138
				       name="wl_publisher[type]" value="person"
139
				       checked="checked">
140
				<label for="wl-publisher-person"><?php
141
					esc_html_e( 'Person', 'wordlift' ) ?></label>
142
			</span>
143
			<span>
144
				<input id="wl-publisher-company" type="radio"
145
				       name="wl_publisher[type]" value="organization"">
146
				<label for="wl-publisher-company"><?php
147
					esc_html_e( 'Company', 'wordlift' ) ?></label>
148
			</span>
149
		</p>
150
		<p id="wl-publisher-name">
151
			<input type="text" name="wl_publisher[name]"
152
			       placeholder="<?php echo esc_attr__( "What's your name?", 'wordlift' ) ?>">
153
		</p>
154
		<div id="wl-publisher-logo">
155
			<input type="hidden" id="wl-publisher-media-uploader-id"
156
			       name="wl_publisher[thumbnail_id]" />
157
			<p>
158
				<b><?php esc_html_e( "Choose the publisher's Logo", 'wordlift' ) ?></b>
159
			</p>
160
			<p>
161
				<img id="wl-publisher-media-uploader-preview" />
162
				<button type="button" class="button"
163
				        id="wl-publisher-media-uploader"><?php
164
					esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?></button>
165
			</p>
166
		</div>
167
		<?php
168
	}
169
170
}
171