Completed
Pull Request — master (#9750)
by Bryce
12:28
created

WC_Admin_Addons::get_section()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Addons Page
4
 *
5
 * @author   WooThemes
6
 * @category Admin
7
 * @package  WooCommerce/Admin
8
 * @version  2.5.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * WC_Admin_Addons Class.
17
 */
18
class WC_Admin_Addons {
19
20
	/**
21
	 * Get sections for the addons screen
22
	 * @return array of objects
23
	 */
24
	public static function get_sections() {
25
		if ( false === ( $sections = get_transient( 'wc_addons_sections' ) ) ) {
26
			$raw_sections = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/addon-sections.json', array( 'user-agent' => 'WooCommerce Addons Page' ) );
27
			if ( ! is_wp_error( $raw_sections ) ) {
28
				$sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
29
30
				if ( $sections ) {
31
					set_transient( 'wc_addons_sections', $sections, WEEK_IN_SECONDS );
32
				}
33
			}
34
		}
35
36
		$addon_sections = array();
37
38
		if ( $sections ) {
39
			foreach ( $sections as $sections_id => $section ) {
40
				if ( empty( $sections_id ) ) {
41
					continue;
42
				}
43
				$addon_sections[ $sections_id ]           = new stdClass;
44
				$addon_sections[ $sections_id ]->title    = wc_clean( $section->title );
45
				$addon_sections[ $sections_id ]->endpoint = wc_clean( $section->endpoint );
46
			}
47
		}
48
49
		return apply_filters( 'woocommerce_addons_sections', $addon_sections );
50
	}
51
52
	/**
53
	 * Get section for the addons screen
54
	 * @return object|bool
55
	 */
56
	public static function get_section( $section_id ) {
57
		$sections = self::get_sections();
58
		if ( isset( $sections[ $section_id ] ) ) {
59
			return $sections[ $section_id ];
60
		}
61
		return false;
62
	}
63
64
	/**
65
	 * Get section content for the addons screen
66
	 * @return string
67
	 */
68
	public static function get_section_data( $section_id ) {
69
		$section      = self::get_section( $section_id );
70
		$section_data = '';
71
72
		if ( ! empty( $section->endpoint ) ) {
73
			if ( false === ( $section_data = get_transient( 'wc_addons_section_' . $section_id ) ) ) {
74
				$raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) );
75
76
				if ( ! is_wp_error( $raw_section ) ) {
77
					$section_data = json_decode( wp_remote_retrieve_body( $raw_section ) );
78
79
					if ( ! empty( $section_data->products ) ) {
80
						set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS );
81
					}
82
				}
83
			}
84
		}
85
86
		return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id );
87
	}
88
89
	/**
90
	 * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active).
91
	 */
92
	public static function output_storefront_button() {
93
		$url         = 'http://www.woothemes.com/storefront/';
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
		$text        = __( 'View more about Storefront', 'woocommerce' );
0 ignored issues
show
Unused Code introduced by
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
		$template    = get_option( 'template' );
96
		$stylesheet  = get_option( 'stylesheet' );
97
		$utm_content = 'hasstorefront';
0 ignored issues
show
Unused Code introduced by
$utm_content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
99
		if ( 'storefront' === $template ) {
100
			if ( 'storefront' === $stylesheet ) {
101
				$url         = 'http:///www.woothemes.com/product-category/themes/storefront-child-theme-themes/';
102
				$text        = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' );
103
				$utm_content = 'nostorefrontchildtheme';
104
			} else {
105
				$url         = 'http:///www.woothemes.com/product-category/themes/storefront-child-theme-themes/';
106
				$text        = __( 'View more Storefront child themes', 'woocommerce' );
107
				$utm_content = 'hasstorefrontchildtheme';
108
			}
109
		} else {
110
			$url         = 'http://www.woothemes.com/storefront/';
111
			$text        = __( 'Need a theme? Try Storefront', 'woocommerce' );
112
			$utm_content = 'nostorefront';
113
		}
114
115
		$url = add_query_arg( array(
116
			'utm_source'   => 'product',
117
			'utm_medium'   => 'upsell',
118
			'utm_campaign' => 'wcaddons',
119
			'utm_content'  => $utm_content,
120
		), $url );
121
122
		echo '<a href="' . esc_url( $url ) . '" class="add-new-h2">' . esc_html( $text ) . '</a>' . "\n";
123
	}
124
125
	/**
126
	 * Handles output of the addons page in admin.
127
	 */
128
	public static function output() {
129
		$sections        = self::get_sections();
130
		$theme           = wp_get_theme();
1 ignored issue
show
Unused Code introduced by
$theme is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
		$section_keys    = array_keys( $sections );
132
		$current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : current( $section_keys );
1 ignored issue
show
Unused Code introduced by
$current_section is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
		include_once( 'views/html-admin-page-addons.php' );
134
	}
135
}
136