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
|
|
|
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/'; |
|
|
|
|
94
|
|
|
$text = __( 'View more about Storefront', 'woocommerce' ); |
|
|
|
|
95
|
|
|
$template = get_option( 'template' ); |
96
|
|
|
$stylesheet = get_option( 'stylesheet' ); |
97
|
|
|
$utm_content = 'hasstorefront'; |
|
|
|
|
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(); |
|
|
|
|
131
|
|
|
$section_keys = array_keys( $sections ); |
132
|
|
|
$current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : current( $section_keys ); |
|
|
|
|
133
|
|
|
include_once( 'views/html-admin-page-addons.php' ); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.