Completed
Push — master ( 48986a...95c69f )
by David
06:07
created

Wordlift_Admin_Select2_Element::render()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 32
nc 4
nop 1
dl 0
loc 47
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elements: Language Select.
4
 *
5
 * An Select element with the list of languages.
6
 *
7
 * @since      3.11.0
8
 * @package    Wordlift
9
 * @subpackage Wordlift/admin
10
 */
11
12
/**
13
 * Define the {@link Wordlift_Admin_Select_Element} class.
14
 *
15
 * @since      3.11.0
16
 * @package    Wordlift
17
 * @subpackage Wordlift/admin
18
 */
19
class Wordlift_Admin_Select2_Element implements Wordlift_Admin_Element {
20
21
	/**
22
	 * @inheritdoc
23
	 */
24
	public function render( $args ) {
25
26
		// Enqueue select2 library js and css.
27
		// Underscore is needed for Select2's `templateResult` and `templateSelection` templates.
28
		wp_enqueue_script( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/js/select2.full' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.js', array(
29
			'jquery',
30
			'underscore',
31
		), '4.0.3' );
32
		wp_enqueue_style( 'wordlift-select2', plugin_dir_url( dirname( __FILE__ ) ) . 'admin/js/select2/css/select2' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '.min' : '' ) . '.css', array(), '4.0.3' );
33
34
35
		// Parse the arguments and merge with default values.
36
		$params = wp_parse_args( $args, array(
37
			'id'                 => uniqid( 'wl-input-' ),
38
			'name'               => uniqid( 'wl-input-' ),
39
			'value'              => null,
40
			'options'            => array(),
41
			'description'        => false,
42
			'data'               => array(),
43
			'template-result'    => '<%= text %>',
44
			'template-selection' => '<%= text %>',
45
		) );
46
47
		$description = $params['description'] ? '<p>' . wp_kses( $params['description'], array( 'a' => array( 'href' => array() ) ) ) . '</p>' : '';
48
49
		// For data attributes we define our own "namespace" `wl-select2-` to avoid
50
		// Select2 preloading configuration on its own. In fact we need to support
51
		// underscore templates.
52
		?>
53
		<select id="<?php echo esc_attr( $params['id'] ); ?>"
54
		        name="<?php echo esc_attr( $params['name'] ); ?>"
55
		        class="wl-select2-element"
56
		        data-wl-select2-data="<?php echo esc_attr( json_encode( $params['data'] ) ); ?>"
57
		        data-wl-select2-template-result="<?php echo esc_attr( $params['template-result'] ); ?>"
58
		        data-wl-select2-template-selection="<?php echo esc_attr( $params['template-selection'] ); ?>">
59
			<?php foreach ( $params['options'] as $value => $label ) { ?>
60
				<option value="<?php echo esc_attr( $value ); ?>"
61
					<?php selected( $params['value'], $value ) ?>><?php
62
					echo esc_html( $label ); ?></option>
63
			<?php } ?>
64
		</select>
65
		<?php echo $description; ?>
66
67
		<?php
68
69
		return $this;
70
	}
71
72
}
73