Completed
Push — develop ( ecf42a...6659d7 )
by David
14:29
created

Wordlift_Admin_Publisher_Element::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 67
rs 8.72
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		array_unshift( $data, array(
118
			'id'            => '',
119
			'text'          => _x( '(none)', 'Publisher Select in Settings Screen.', 'wordlift' ),
120
			'type'          => '',
121
			'thumbnail_url' => false,
122
		) );
123
124
		// Call the select internal render.
125
		$this->do_render( $params, $publisher_id, $data );
126
127
	}
128
129
	/**
130
	 * Render the 'create publisher' form.
131
	 *
132
	 * @since 3.11.0
133
	 *
134
	 * @param array $params An array of parameters.
135
	 */
136
	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...
137
		?>
138
        <p>
139
            <strong><?php esc_html_e( 'Are you publishing as an individual or as a company?', 'wordlift' ); ?></strong>
140
        </p>
141
142
        <p id="wl-publisher-type">
143
			<span>
144
				<input
145
                        id="wl-publisher-person"
146
                        type="radio"
147
                        name="wl_publisher[type]"
148
                        value="person"
149
                        checked="checked"
150
                >
151
152
				<label for="wl-publisher-person">
153
					<?php esc_html_e( 'Person', 'wordlift' ); ?>
154
				</label>
155
			</span>
156
157
            <span>
158
				<input
159
                        id="wl-publisher-company"
160
                        type="radio"
161
                        name="wl_publisher[type]"
162
                        value="organization"
163
                >
164
165
				<label for="wl-publisher-company">
166
					<?php esc_html_e( 'Company', 'wordlift' ); ?>
167
				</label>
168
			</span>
169
        </p>
170
171
        <p id="wl-publisher-name">
172
            <input
173
                    type="text"
174
                    name="wl_publisher[name]"
175
                    placeholder="<?php echo esc_attr__( "What's your name?", 'wordlift' ); ?>"
176
            >
177
        </p>
178
179
        <div id="wl-publisher-logo">
180
            <input
181
                    type="hidden"
182
                    id="wl-publisher-media-uploader-id"
183
                    name="wl_publisher[thumbnail_id]"
184
            />
185
186
            <p>
187
                <b><?php esc_html_e( "Choose the publisher's Logo", 'wordlift' ); ?></b>
188
            </p>
189
190
            <p>
191
                <img id="wl-publisher-media-uploader-preview"/>
192
193
                <button
194
                        type="button"
195
                        class="button"
196
                        id="wl-publisher-media-uploader">
197
					<?php esc_html_e( 'Select an existing image or upload a new one', 'wordlift' ); ?>
198
                </button>
199
            </p>
200
        </div>
201
		<?php
202
	}
203
204
}
205