WC_Admin_Addons::get_section_data()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 7
Ratio 35 %

Importance

Changes 0
Metric Value
cc 5
dl 7
loc 20
rs 8.8571
c 0
b 0
f 0
eloc 11
nc 5
nop 1
1
<?php
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 View Code Duplication
			if ( ! is_wp_error( $raw_sections ) ) {
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...
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
	 *
55
	 * @param  string $section_id
56
	 *
57
	 * @return object|bool
58
	 */
59
	public static function get_section( $section_id ) {
60
		$sections = self::get_sections();
61
		if ( isset( $sections[ $section_id ] ) ) {
62
			return $sections[ $section_id ];
63
		}
64
		return false;
65
	}
66
67
	/**
68
	 * Get section content for the addons screen.
69
	 *
70
	 * @param  string $section_id
71
	 *
72
	 * @return array
73
	 */
74
	public static function get_section_data( $section_id ) {
75
		$section      = self::get_section( $section_id );
76
		$section_data = '';
77
78
		if ( ! empty( $section->endpoint ) ) {
79
			if ( false === ( $section_data = get_transient( 'wc_addons_section_' . $section_id ) ) ) {
80
				$raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) );
81
82 View Code Duplication
				if ( ! is_wp_error( $raw_section ) ) {
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...
83
					$section_data = json_decode( wp_remote_retrieve_body( $raw_section ) );
84
85
					if ( ! empty( $section_data->products ) ) {
86
						set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS );
87
					}
88
				}
89
			}
90
		}
91
92
		return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id );
93
	}
94
95
	/**
96
	 * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active).
97
	 */
98
	public static function output_storefront_button() {
99
		$template   = get_option( 'template' );
100
		$stylesheet = get_option( 'stylesheet' );
101
102
		if ( 'storefront' === $template ) {
103
			if ( 'storefront' === $stylesheet ) {
104
				$url         = 'https://www.woothemes.com/product-category/themes/storefront-child-theme-themes/';
105
				$text        = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' );
106
				$utm_content = 'nostorefrontchildtheme';
107
			} else {
108
				$url         = 'https://www.woothemes.com/product-category/themes/storefront-child-theme-themes/';
109
				$text        = __( 'View more Storefront child themes', 'woocommerce' );
110
				$utm_content = 'hasstorefrontchildtheme';
111
			}
112
		} else {
113
			$url         = 'https://www.woothemes.com/storefront/';
114
			$text        = __( 'Need a theme? Try Storefront', 'woocommerce' );
115
			$utm_content = 'nostorefront';
116
		}
117
118
		$url = add_query_arg( array(
119
			'utm_source'   => 'product',
120
			'utm_medium'   => 'upsell',
121
			'utm_campaign' => 'wcaddons',
122
			'utm_content'  => $utm_content,
123
		), $url );
124
125
		echo '<a href="' . esc_url( $url ) . '" class="add-new-h2">' . esc_html( $text ) . '</a>' . "\n";
126
	}
127
128
	/**
129
	 * Handles output of the addons page in admin.
130
	 */
131
	public static function output() {
132
		$sections        = self::get_sections();
133
		$theme           = wp_get_theme();
134
		$section_keys    = array_keys( $sections );
135
		$current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : current( $section_keys );
136
		include_once( 'views/html-admin-page-addons.php' );
137
	}
138
}
139