Completed
Push — develop ( 5c7568...6a9ba1 )
by David
14:52
created

Wordlift_Navigator_Shortcode::render()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 57

Duplication

Lines 12
Ratio 21.05 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 12
loc 57
rs 8.9381
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
 * Shortcodes: Navigator Shorcode.
4
 *
5
 * @since      3.5.4
6
 * @package    Wordlift
7
 * @subpackage Wordlift/public
8
 */
9
10
/**
11
 * Define the {@link Wordlift_Navigator_Shortcode} class which provides the
12
 * `wl_navigator` implementation.
13
 *
14
 * @since      3.5.4
15
 * @package    Wordlift
16
 * @subpackage Wordlift/public
17
 */
18
class Wordlift_Navigator_Shortcode extends Wordlift_Shortcode {
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	const SHORTCODE = 'wl_navigator';
24
25
	/**
26
	 * {@inheritdoc}
27
	 */
28
	public function render( $atts ) {
29
30
		// Extract attributes and set default values.
31
		$shortcode_atts = shortcode_atts( array(
32
			'title'          => __( 'Related articles', 'wordlift' ),
33
			'with_carousel'  => true,
34
			'squared_thumbs' => false,
35
		), $atts );
36
37 View Code Duplication
		foreach (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
			array(
39
				'with_carousel',
40
				'squared_thumbs',
41
			) as $att
42
		) {
43
44
			// See http://wordpress.stackexchange.com/questions/119294/pass-boolean-value-in-shortcode
45
			$shortcode_atts[ $att ] = filter_var(
46
				$shortcode_atts[ $att ], FILTER_VALIDATE_BOOLEAN
47
			);
48
		}
49
50
		/*
51
		 * Display the navigator only on the single post/page.
52
		 *
53
		 * @see https://github.com/insideout10/wordlift-plugin/issues/831
54
		 *
55
		 * @since 3.19.3 we're using `is_singular` instead of `is_single` to allow the navigator also on pages.
56
		 */
57
		// avoid building the widget when there is a list of posts.
58
		if ( ! is_singular() ) {
59
			return '';
60
		}
61
62
		$current_post = get_post();
63
64
		// Enqueue common shortcode scripts.
65
		$this->enqueue_scripts();
66
67
		// Use the registered style which define an optional dependency to font-awesome.
68
		//
69
		// @see https://github.com/insideout10/wordlift-plugin/issues/699
70
		//		wp_enqueue_style( 'wordlift-ui', dirname( plugin_dir_url( __FILE__ ) ) . '/css/wordlift-ui.min.css' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
		wp_enqueue_style( 'wordlift-ui' );
72
73
		$navigator_id = uniqid( 'wl-navigator-widget-' );
74
75
		wp_localize_script( 'wordlift-ui', 'wl_navigator_params', array(
76
				'ajax_url' => admin_url( 'admin-ajax.php' ),
77
				'action'   => 'wl_navigator',
78
				'post_id'  => $current_post->ID,
79
				'attrs'    => $shortcode_atts,
80
			)
81
		);
82
83
		return "<div id='$navigator_id' class='wl-navigator-widget'></div>";
84
	}
85
86
}
87