Completed
Push — master ( 435e74...e56e1f )
by Leon
03:03
created

woocommerce-template.php ➔ dp_alt_products()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 15
rs 9.4285
1
<?php
2
/**
3
 * WooCommerce Discontinued Products Template Functions
4
 *
5
 * @package woocommerce
6
 * @since 1.0.0
7
 */
8
9
if ( ! function_exists( 'dp_is_discontinued' ) ) {
10
11
	/**
12
	 * Is discontiued.
13
	 * Check if product is discontinued.
14
	 *
15
	 * @since 1.0.0
16
	 * @param int|null $product_id Optional. ID of the product to check.
17
	 */
18
	function dp_is_discontinued( $product_id = null ) {
0 ignored issues
show
Coding Style introduced by
function dp_is_discontinued() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
19
20
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
21
		if ( $post || $product_id !== null ) {
22
			$product_id      = $product_id !== null ? $product_id : $post->ID;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $product_id. This often makes code more readable.
Loading history...
23
			$is_discontinued = get_post_meta( $product_id, '_is_discontinued', true );
24
			return $is_discontinued === 'yes';
25
		}
26
		return false;
27
	}
28
}
29
30
if ( ! function_exists( 'dp_alt_products' ) ) {
31
32
	/**
33
	 * Alternative Products.
34
	 * Output buttons to alternative products.
35
	 *
36
	 * @since 1.0.0
37
	 */
38
	function dp_alt_products() {
39
40
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
41
		$alt_products = get_post_meta( $post->ID, '_alt_products', true );
42
		$alt_products = is_array( $alt_products ) ? $alt_products : array();
43
		$notice       = dp_alt_products_notice( $post->ID, empty( $alt_products ) );
44
		?>
45
		<h4><?php echo esc_html( $notice ); ?></h4>
46
		<?php
47
		foreach ( $alt_products as $alt_product ) {
48
			?>
49
			<a href="<?php echo esc_url( get_permalink( $alt_product ) ); ?>" class="button"><?php echo get_the_title( $alt_product ); ?></a>
50
			<?php
51
		}
52
	}
53
}
54
55
if ( ! function_exists( 'dp_alt_products_notice' ) ) {
56
57
	/**
58
	 * Alternative Products Notice.
59
	 * Determin notice output for discontinued products based on settings.
60
	 *
61
	 * @since 1.1.0
62
	 * @param int     $product_id ID of the product to check.
63
	 * @param boolean $no_alt true or false if there are no alternative products.
64
	 */
65
	function dp_alt_products_notice( $product_id, $no_alt ) {
66
67
		$prod_text_option = get_post_meta( $product_id, '_discontinued_product_text', true );
68
		$prod_alt_option = get_post_meta( $product_id, '_alt_product_text', true );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
		$text_option  = get_option( 'dc_discontinued_text' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 2 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
		$alt_option   = get_option( 'dc_alt_text' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 3 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
		$text         = dp_alt_products_text( $prod_text_option, $text_option, _( 'This product has been discontinued.' ) );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 9 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
		$alt          = dp_alt_products_text( $prod_alt_option, $alt_option, _( 'You may be interested in:' ) );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 10 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
		$notice       = $no_alt ? $text : $text . ' ' . $alt;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 7 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
		return $notice;
75
	}
76
}
77
78
if ( ! function_exists( 'dp_alt_products_text' ) ) {
79
80
	/**
81
	 * Alternative Products Text.
82
	 * Determin text for discontinued products based on settings.
83
	 *
84
	 * @since 1.1.0
85
	 * @param string $product_text product meta text.
86
	 * @param string $option_text options settings text.
87
	 * @param string $default_text default text.
88
	 */
89
	function dp_alt_products_text( $product_text, $option_text, $default_text ) {
90
91
		$text = $product_text ? $product_text : ( $option_text ? $option_text : $default_text );
92
		return $text;
93
	}
94
}
95