Completed
Pull Request — master (#637)
by
unknown
12:04
created
includes/admin/shortcodes/abstract-shortcode-generator.php 1 patch
Spacing   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@  discard block
 block discarded – undo
11 11
  * @since       1.3.0
12 12
  */
13 13
 
14
-defined( 'ABSPATH' ) or exit;
14
+defined('ABSPATH') or exit;
15 15
 
16 16
 abstract class Give_Shortcode_Generator {
17 17
 
@@ -57,12 +57,12 @@  discard block
 block discarded – undo
57 57
 	 *
58 58
 	 * @since 1.0
59 59
 	 */
60
-	public function __construct( $shortcode ) {
60
+	public function __construct($shortcode) {
61 61
 
62 62
 
63 63
 		$this->shortcode_tag = $shortcode;
64 64
 
65
-		add_action( 'admin_init', array( $this, 'init' ) );
65
+		add_action('admin_init', array($this, 'init'));
66 66
 
67 67
 	}
68 68
 
@@ -72,9 +72,9 @@  discard block
 block discarded – undo
72 72
 	 */
73 73
 	public function init() {
74 74
 
75
-		if ( $this->shortcode_tag ) {
75
+		if ($this->shortcode_tag) {
76 76
 
77
-			$this->self = get_class( $this );
77
+			$this->self = get_class($this);
78 78
 
79 79
 			$this->errors   = array();
80 80
 			$this->required = array();
@@ -83,18 +83,18 @@  discard block
 block discarded – undo
83 83
 			$fields = $this->get_fields();
84 84
 
85 85
 			$defaults = array(
86
-				'btn_close' => __( 'Close', 'give' ),
87
-				'btn_okay'  => __( 'Insert Shortcode', 'give' ),
86
+				'btn_close' => __('Close', 'give'),
87
+				'btn_okay'  => __('Insert Shortcode', 'give'),
88 88
 				'errors'    => $this->errors,
89 89
 				'fields'    => $fields,
90
-				'label'     => '[' . $this->shortcode_tag . ']',
90
+				'label'     => '['.$this->shortcode_tag.']',
91 91
 				'required'  => $this->required,
92
-				'title'     => __( 'Insert Shortcode', 'give' ),
92
+				'title'     => __('Insert Shortcode', 'give'),
93 93
 			);
94 94
 
95
-			if( user_can_richedit() ) {
95
+			if (user_can_richedit()) {
96 96
 				
97
-				Give_Shortcode_Button::$shortcodes[ $this->shortcode_tag ] = wp_parse_args( $this->shortcode, $defaults );
97
+				Give_Shortcode_Button::$shortcodes[$this->shortcode_tag] = wp_parse_args($this->shortcode, $defaults);
98 98
 			
99 99
 			}	
100 100
 			//
@@ -125,13 +125,13 @@  discard block
 block discarded – undo
125 125
 	 *
126 126
 	 * @since 1.0
127 127
 	 */
128
-	protected function generate_fields( $defined_fields ) {
128
+	protected function generate_fields($defined_fields) {
129 129
 
130 130
 		$fields = array();
131 131
 
132
-		if ( is_array( $defined_fields ) ) {
132
+		if (is_array($defined_fields)) {
133 133
 
134
-			foreach ( $defined_fields as $field ) {
134
+			foreach ($defined_fields as $field) {
135 135
 
136 136
 				$defaults = array(
137 137
 					'label'       => false,
@@ -142,14 +142,14 @@  discard block
 block discarded – undo
142 142
 					'type'        => '',
143 143
 				);
144 144
 
145
-				$field  = wp_parse_args( (array) $field, $defaults );
146
-				$method = 'generate_' . strtolower( $field['type'] );
145
+				$field  = wp_parse_args((array) $field, $defaults);
146
+				$method = 'generate_'.strtolower($field['type']);
147 147
 
148
-				if ( method_exists( $this, $method ) ) {
148
+				if (method_exists($this, $method)) {
149 149
 
150
-					$field = call_user_func( array( $this, $method ), $field );
150
+					$field = call_user_func(array($this, $method), $field);
151 151
 
152
-					if ( $field ) {
152
+					if ($field) {
153 153
 						$fields[] = $field;
154 154
 					}
155 155
 				}
@@ -169,22 +169,22 @@  discard block
 block discarded – undo
169 169
 	protected function get_fields() {
170 170
 
171 171
 		$defined_fields   = $this->define_fields();
172
-		$generated_fields = $this->generate_fields( $defined_fields );
172
+		$generated_fields = $this->generate_fields($defined_fields);
173 173
 
174 174
 		$errors = array();
175 175
 
176
-		if ( ! empty( $this->errors ) ) {
177
-			foreach ( $this->required as $name => $alert ) {
178
-				if ( false === array_search( $name, array_column( $generated_fields, 'name' ) ) ) {
176
+		if ( ! empty($this->errors)) {
177
+			foreach ($this->required as $name => $alert) {
178
+				if (false === array_search($name, array_column($generated_fields, 'name'))) {
179 179
 
180
-					$errors[] = $this->errors[ $name ];
180
+					$errors[] = $this->errors[$name];
181 181
 				}
182 182
 			}
183 183
 
184 184
 			$this->errors = $errors;
185 185
 		}
186 186
 
187
-		if ( ! empty( $errors ) ) {
187
+		if ( ! empty($errors)) {
188 188
 
189 189
 			return $errors;
190 190
 		}
@@ -201,9 +201,9 @@  discard block
 block discarded – undo
201 201
 	 *
202 202
 	 * @since 1.0
203 203
 	 */
204
-	protected function generate_container( $field ) {
204
+	protected function generate_container($field) {
205 205
 
206
-		if ( array_key_exists( 'html', $field ) ) {
206
+		if (array_key_exists('html', $field)) {
207 207
 
208 208
 			return array(
209 209
 				'type' => $field['type'],
@@ -223,36 +223,36 @@  discard block
 block discarded – undo
223 223
 	 *
224 224
 	 * @since 1.0
225 225
 	 */
226
-	protected function generate_listbox( $field ) {
226
+	protected function generate_listbox($field) {
227 227
 
228
-		$listbox = shortcode_atts( array(
228
+		$listbox = shortcode_atts(array(
229 229
 			'label'    => '',
230 230
 			'minWidth' => '',
231 231
 			'name'     => false,
232 232
 			'tooltip'  => '',
233 233
 			'type'     => '',
234 234
 			'value'    => '',
235
-		), $field );
235
+		), $field);
236 236
 
237
-		if ( $this->validate( $field ) ) {
237
+		if ($this->validate($field)) {
238 238
 
239 239
 			$new_listbox = array();
240 240
 
241
-			foreach ( $listbox as $key => $value ) {
241
+			foreach ($listbox as $key => $value) {
242 242
 
243
-				if ( $key == 'value' && empty( $value ) ) {
244
-					$new_listbox[ $key ] = $listbox['name'];
245
-				} else if ( $value ) {
246
-					$new_listbox[ $key ] = $value;
243
+				if ($key == 'value' && empty($value)) {
244
+					$new_listbox[$key] = $listbox['name'];
245
+				} else if ($value) {
246
+					$new_listbox[$key] = $value;
247 247
 				}
248 248
 			}
249 249
 
250 250
 			// do not reindex array!
251 251
 			$field['options'] = array(
252
-				                    '' => ( $field['placeholder'] ? $field['placeholder'] : sprintf( '– %s –', __( 'Select', 'give' ) ) ),
252
+				                    '' => ($field['placeholder'] ? $field['placeholder'] : sprintf('– %s –', __('Select', 'give'))),
253 253
 			                    ) + $field['options'];
254 254
 
255
-			foreach ( $field['options'] as $value => $text ) {
255
+			foreach ($field['options'] as $value => $text) {
256 256
 				$new_listbox['values'][] = array(
257 257
 					'text'  => $text,
258 258
 					'value' => $value,
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
 	 *
275 275
 	 * @since 1.0
276 276
 	 */
277
-	protected function generate_post( $field ) {
277
+	protected function generate_post($field) {
278 278
 
279 279
 		$args = array(
280 280
 			'post_type'      => 'post',
@@ -283,23 +283,23 @@  discard block
 block discarded – undo
283 283
 			'posts_per_page' => 30,
284 284
 		);
285 285
 
286
-		$args    = wp_parse_args( (array) $field['query_args'], $args );
287
-		$posts   = get_posts( $args );
286
+		$args    = wp_parse_args((array) $field['query_args'], $args);
287
+		$posts   = get_posts($args);
288 288
 		$options = array();
289 289
 
290
-		if ( $posts ) {
291
-			foreach ( $posts as $post ) {
292
-				$options[ absint( $post->ID ) ] = esc_html( $post->post_title );
290
+		if ($posts) {
291
+			foreach ($posts as $post) {
292
+				$options[absint($post->ID)] = esc_html($post->post_title);
293 293
 			}
294 294
 
295 295
 			$field['type']    = 'listbox';
296 296
 			$field['options'] = $options;
297 297
 
298
-			return $this->generate_listbox( $field );
298
+			return $this->generate_listbox($field);
299 299
 		}
300 300
 
301 301
 		// perform validation here before returning false
302
-		$this->validate( $field );
302
+		$this->validate($field);
303 303
 
304 304
 		return false;
305 305
 	}
@@ -313,9 +313,9 @@  discard block
 block discarded – undo
313 313
 	 *
314 314
 	 * @since 1.0
315 315
 	 */
316
-	protected function generate_textbox( $field ) {
316
+	protected function generate_textbox($field) {
317 317
 
318
-		$textbox = shortcode_atts( array(
318
+		$textbox = shortcode_atts(array(
319 319
 			'label'     => '',
320 320
 			'maxLength' => '',
321 321
 			'minHeight' => '',
@@ -325,10 +325,10 @@  discard block
 block discarded – undo
325 325
 			'tooltip'   => '',
326 326
 			'type'      => '',
327 327
 			'value'     => '',
328
-		), $field );
328
+		), $field);
329 329
 
330
-		if ( $this->validate( $field ) ) {
331
-			return array_filter( $textbox, array( $this, 'return_textbox_value' ) );
330
+		if ($this->validate($field)) {
331
+			return array_filter($textbox, array($this, 'return_textbox_value'));
332 332
 		}
333 333
 
334 334
 		return false;
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
 	 *
342 342
 	 * @return bool
343 343
 	 */
344
-	function return_textbox_value( $value ) {
344
+	function return_textbox_value($value) {
345 345
 		return $value !== '';
346 346
 	}
347 347
 
@@ -357,9 +357,9 @@  discard block
 block discarded – undo
357 357
 	 *
358 358
 	 * @since 1.0
359 359
 	 */
360
-	protected function validate( $field ) {
360
+	protected function validate($field) {
361 361
 
362
-		extract( shortcode_atts(
362
+		extract(shortcode_atts(
363 363
 				array(
364 364
 					'name'     => false,
365 365
 					'required' => false,
@@ -367,34 +367,34 @@  discard block
 block discarded – undo
367 367
 				), $field )
368 368
 		);
369 369
 
370
-		if ( $name ) {
370
+		if ($name) {
371 371
 
372
-			if ( isset( $required['error'] ) ) {
372
+			if (isset($required['error'])) {
373 373
 
374 374
 				$error = array(
375 375
 					'type' => 'container',
376 376
 					'html' => $required['error'],
377 377
 				);
378 378
 
379
-				$this->errors[ $name ] = $this->generate_container( $error );
379
+				$this->errors[$name] = $this->generate_container($error);
380 380
 			}
381 381
 
382
-			if ( ! ! $required || is_array( $required ) ) {
382
+			if ( ! ! $required || is_array($required)) {
383 383
 
384
-				$alert = __( 'Some of the Shortcode options are required.', 'give' );
384
+				$alert = __('Some of the Shortcode options are required.', 'give');
385 385
 
386
-				if ( isset( $required['alert'] ) ) {
386
+				if (isset($required['alert'])) {
387 387
 
388 388
 					$alert = $required['alert'];
389 389
 
390
-				} else if ( ! empty( $label ) ) {
390
+				} else if ( ! empty($label)) {
391 391
 
392
-					$alert = sprintf( __( 'The "%s" option is required.', 'give' ),
393
-						str_replace( ':', '', $label )
392
+					$alert = sprintf(__('The "%s" option is required.', 'give'),
393
+						str_replace(':', '', $label)
394 394
 					);
395 395
 				}
396 396
 
397
-				$this->required[ $name ] = $alert;
397
+				$this->required[$name] = $alert;
398 398
 			}
399 399
 
400 400
 			return true;
Please login to merge, or discard this patch.