Completed
Push — develop ( 1daf21...588ea4 )
by David
10:26 queued 06:52
created

Wordlift_Navigator_Shortcode::render()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 25
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 46
rs 8.9411
1
<?php
2
3
/**
4
 * The `wl_navigator` implementation.
5
 *
6
 * @since 3.5.4
7
 */
8
class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode {
9
10
	/**
11
	 * {@inheritdoc}
12
	 */
13
	const SHORTCODE = 'wl_navigator';
14
15
	/**
16
	 * {@inheritdoc}
17
	 */
18
	public function render( $atts ) {
19
20
		// Extract attributes and set default values.
21
		$shortcode_atts = shortcode_atts( array(
22
			'title'          => __( 'Related articles', 'wordlift' ),
23
			'with_carousel'  => TRUE,
24
			'squared_thumbs' => FALSE
25
		), $atts );
26
27
		foreach (
28
			array(
29
				'with_carousel',
30
				'squared_thumbs'
31
			) as $att
32
		) {
33
34
			// See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode
35
			$shortcode_atts[ $att ] = filter_var(
36
				$shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN
37
			);
38
		}
39
40
		// avoid building the widget when there is a list of posts.
41
		if ( ! is_single() ) {
42
			return '';
43
		}
44
45
		$current_post = get_post();
46
47
		// Enqueue common shortcode scripts.
48
		$this->enqueue_scripts();
49
50
		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
51
52
		$navigator_id = uniqid( 'wl-navigator-widget-' );
53
54
		wp_localize_script( 'wordlift-ui', 'wl_navigator_params', array(
55
				'ajax_url' => admin_url( 'admin-ajax.php' ),
56
				'action'   => 'wl_navigator',
57
				'post_id'  => $current_post->ID,
58
				'attrs'    => $shortcode_atts
59
			)
60
		);
61
62
		return "<div id='$navigator_id' class='wl-navigator-widget'></div>";
63
	}
64
65
}
66