Completed
Push — master ( 2104f9...0aa65f )
by Justin
07:33
created

WC_Admin_Addons::output_small_dark_block()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
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 featured for the addons screen
22
	 *
23
	 * @return array of objects
24
	 */
25
	public static function get_featured() {
26 View Code Duplication
		if ( false === ( $featured = get_transient( 'wc_addons_featured' ) ) ) {
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...
27
			$raw_featured = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/add-ons/featured.json', array( 'user-agent' => 'WooCommerce Addons Page' ) );
28
			if ( ! is_wp_error( $raw_featured ) ) {
29
				$featured = json_decode( wp_remote_retrieve_body( $raw_featured ) );
30
				if ( $featured ) {
31
					set_transient( 'wc_addons_featured', $featured, WEEK_IN_SECONDS );
32
				}
33
			}
34
		}
35
36
		if ( is_object( $featured ) ) {
37
			self::output_featured_sections( $featured->sections );
38
			return $featured;
39
		}
40
	}
41
42
	/**
43
	 * Get sections for the addons screen
44
	 *
45
	 * @return array of objects
46
	 */
47
	public static function get_sections() {
48 View Code Duplication
		if ( false === ( $sections = get_transient( 'wc_addons_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...
49
			$raw_sections = wp_safe_remote_get( 'https://d3t0oesq8995hv.cloudfront.net/addon-sections.json', array( 'user-agent' => 'WooCommerce Addons Page' ) );
50
			if ( ! is_wp_error( $raw_sections ) ) {
51
				$sections = json_decode( wp_remote_retrieve_body( $raw_sections ) );
52
53
				if ( $sections ) {
54
					set_transient( 'wc_addons_sections', $sections, WEEK_IN_SECONDS );
55
				}
56
			}
57
		}
58
59
		$addon_sections = array();
60
61
		if ( $sections ) {
62
			foreach ( $sections as $sections_id => $section ) {
63
				if ( empty( $sections_id ) ) {
64
					continue;
65
				}
66
				$addon_sections[ $sections_id ]           = new stdClass;
67
				$addon_sections[ $sections_id ]->title    = wc_clean( $section->title );
68
				$addon_sections[ $sections_id ]->endpoint = wc_clean( $section->endpoint );
69
			}
70
		}
71
72
		return apply_filters( 'woocommerce_addons_sections', $addon_sections );
73
	}
74
75
	/**
76
	 * Get section for the addons screen.
77
	 *
78
	 * @param  string $section_id
79
	 *
80
	 * @return object|bool
81
	 */
82
	public static function get_section( $section_id ) {
83
		$sections = self::get_sections();
84
		if ( isset( $sections[ $section_id ] ) ) {
85
			return $sections[ $section_id ];
86
		}
87
		return false;
88
	}
89
90
	/**
91
	 * Get section content for the addons screen.
92
	 *
93
	 * @param  string $section_id
94
	 *
95
	 * @return array
96
	 */
97
	public static function get_section_data( $section_id ) {
98
		$section      = self::get_section( $section_id );
99
		$section_data = '';
100
101
		if ( ! empty( $section->endpoint ) ) {
102
			if ( false === ( $section_data = get_transient( 'wc_addons_section_' . $section_id ) ) ) {
103
				$raw_section = wp_safe_remote_get( esc_url_raw( $section->endpoint ), array( 'user-agent' => 'WooCommerce Addons Page' ) );
104
105
				if ( ! is_wp_error( $raw_section ) ) {
106
					$section_data = json_decode( wp_remote_retrieve_body( $raw_section ) );
107
108
					if ( ! empty( $section_data->products ) ) {
109
						set_transient( 'wc_addons_section_' . $section_id, $section_data, WEEK_IN_SECONDS );
110
					}
111
				}
112
			}
113
		}
114
115
		return apply_filters( 'woocommerce_addons_section_data', $section_data->products, $section_id );
116
	}
117
118
	/**
119
	 * Handles the outputting of a contextually aware Storefront link (points to child themes if Storefront is already active).
120
	 */
121
	public static function output_storefront_button() {
122
		$template   = get_option( 'template' );
123
		$stylesheet = get_option( 'stylesheet' );
124
125
		if ( 'storefront' === $template ) {
126
			if ( 'storefront' === $stylesheet ) {
127
				$url         = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/';
128
				$text        = __( 'Need a fresh look? Try Storefront child themes', 'woocommerce' );
129
				$utm_content = 'nostorefrontchildtheme';
130
			} else {
131
				$url         = 'https://woocommerce.com/product-category/themes/storefront-child-theme-themes/';
132
				$text        = __( 'View more Storefront child themes', 'woocommerce' );
133
				$utm_content = 'hasstorefrontchildtheme';
134
			}
135
		} else {
136
			$url         = 'https://woocommerce.com/storefront/';
137
			$text        = __( 'Need a theme? Try Storefront', 'woocommerce' );
138
			$utm_content = 'nostorefront';
139
		}
140
141
		$url = add_query_arg( array(
142
			'utm_source'   => 'addons',
143
			'utm_medium'   => 'product',
144
			'utm_campaign' => 'woocommerceplugin',
145
			'utm_content'  => $utm_content,
146
		), $url );
147
148
		echo '<a href="' . esc_url( $url ) . '" class="add-new-h2">' . esc_html( $text ) . '</a>' . "\n";
149
	}
150
151
	/**
152
	 * Handles the outputting of a banner block.
153
	 *
154
	 * @param object $block
155
	 */
156 View Code Duplication
	public static function output_banner_block( $block ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
157
		?>
158
		<div class="addons-banner-block">
159
			<h1><?php echo esc_html( $block->title ); ?></h1>
160
			<p><?php echo esc_html( $block->description ); ?></p>
161
			<div class="addons-banner-block-items">
162
				<?php foreach ( $block->items as $item ) : ?>
163
					<div class="addons-banner-block-item">
164
						<div class="addons-banner-block-item-icon">
165
							<img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" />
166
						</div>
167
						<div class="addons-banner-block-item-content">
168
							<h3><?php echo esc_html( $item->title ); ?></h3>
169
							<p><?php echo esc_html( $item->description ); ?></p>
170
							<?php
171
								self::output_button(
172
									$item->href,
173
									$item->button,
174
									'addons-button-solid',
175
									$item->plugin
176
								);
177
							?>
178
						</div>
179
					</div>
180
				<?php endforeach; ?>
181
			</div>
182
		</div>
183
		<?php
184
	}
185
186
	/**
187
	 * Handles the outputting of a column.
188
	 *
189
	 * @param object $block
190
	 */
191
	public static function output_column( $block ) {
192
		if ( isset( $block->container ) && 'column_container_start' === $block->container ) {
193
			?>
194
			<div class="addons-column-section">
195
			<?php
196
		}
197
		if ( 'column_start' === $block->module ) {
198
			?>
199
			<div class="addons-column">
200
			<?php
201
		} else {
202
			?>
203
			</div>
204
			<?php
205
		}
206
		if ( isset( $block->container ) && 'column_container_end' === $block->container ) {
207
			?>
208
			</div>
209
			<?php
210
		}
211
	}
212
213
	/**
214
	 * Handles the outputting of a column block.
215
	 *
216
	 * @param object $block
217
	 */
218 View Code Duplication
	public static function output_column_block( $block ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
219
		?>
220
		<div class="addons-column-block">
221
			<h1><?php echo esc_html( $block->title ); ?></h1>
222
			<p><?php echo esc_html( $block->description ); ?></p>
223
			<?php foreach ( $block->items as $item ) : ?>
224
				<div class="addons-column-block-item">
225
					<div class="addons-column-block-item-icon">
226
						<img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" />
227
					</div>
228
229
					<div class="addons-column-block-item-content">
230
						<h2><?php echo esc_html( $item->title ); ?></h2>
231
						<?php
232
							self::output_button(
233
								$item->href,
234
								$item->button,
235
								'addons-button-solid',
236
								$item->plugin
237
							);
238
						?>
239
						<p><?php echo esc_html( $item->description ); ?></p>
240
241
					</div>
242
				</div>
243
			<?php endforeach; ?>
244
		</div>
245
246
		<?php
247
	}
248
249
	/**
250
	 * Handles the outputting of a small light block.
251
	 *
252
	 * @param object $block
253
	 */
254 View Code Duplication
	public static function output_small_light_block( $block ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
255
		?>
256
		<div class="addons-small-light-block">
257
			<img class="addons-img" src="<?php echo esc_url( $block->image ) ?>" />
258
			<div class="addons-small-light-block-content">
259
				<h1><?php echo esc_html( $block->title ); ?></h1>
260
				<p><?php echo esc_html( $block->description ); ?></p>
261
				<div class="addons-small-light-block-buttons">
262
					<?php foreach ( $block->buttons as $button ) : ?>
263
						<?php
264
							self::output_button(
265
								$button->href,
266
								$button->text,
267
								'addons-button-solid'
268
							);
269
						?>
270
					<?php endforeach; ?>
271
				</div>
272
			</div>
273
		</div>
274
		<?php
275
	}
276
277
	/**
278
	 * Handles the outputting of a small dark block.
279
	 *
280
	 * @param object $block
281
	 */
282 View Code Duplication
	public static function output_small_dark_block( $block ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
283
		?>
284
		<div class="addons-small-dark-block">
285
			<h1><?php echo esc_html( $block->title ); ?></h1>
286
			<p><?php echo esc_html( $block->description ); ?></p>
287
			<div class="addons-small-dark-items">
288
				<?php foreach ( $block->items as $item ) : ?>
289
					<div class="addons-small-dark-item">
290
						<div class="addons-small-dark-item-icon">
291
							<img class="addons-img" src="<?php echo esc_url( $item->image ); ?>" />
292
						</div>
293
						<?php
294
							self::output_button(
295
								$item->href,
296
								$item->button,
297
								'addons-button-outline-white'
298
							);
299
						?>
300
					</div>
301
				<?php endforeach; ?>
302
			</div>
303
		</div>
304
		<?php
305
	}
306
307
	/**
308
	 * Handles the outputting of featured sections
309
	 *
310
	 * @param array $sections
311
	 */
312
	public static function output_featured_sections( $sections ) {
313
		foreach ( $sections as $section ) {
314
			switch ( $section->module ) {
315
				case 'banner_block':
316
					self::output_banner_block( $section );
317
					break;
318
				case 'column_start':
319
					self::output_column( $section );
0 ignored issues
show
Unused Code introduced by
The call to the method WC_Admin_Addons::output_column() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
320
					break;
321
				case 'column_end':
322
					self::output_column( $section );
0 ignored issues
show
Unused Code introduced by
The call to the method WC_Admin_Addons::output_column() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
323
					break;
324
				case 'column_block':
325
					self::output_column_block( $section );
326
					break;
327
				case 'small_light_block':
328
					self::output_small_light_block( $section );
329
					break;
330
				case 'small_dark_block':
331
					self::output_small_dark_block( $section );
332
					break;
333
			}
334
		}
335
	}
336
337
	/**
338
	 * Outputs a button.
339
	 *
340
	 * @param string $url
341
	 * @param string $text
342
	 * @param string $theme
343
	 * @param string $plugin
344
	 */
345
	public static function output_button( $url, $text, $theme, $plugin = '' ) {
346
		$theme = __( 'Free', 'woocommerce' ) === $text ? 'addons-button-outline-green' : $theme;
347
		$theme = is_plugin_active( $plugin ) ? 'addons-button-installed' : $theme;
348
		$text = is_plugin_active( $plugin ) ? __( 'Installed', 'woocommerce' ) : $text;
349
		?>
350
		<a
351
			class="addons-button <?php echo esc_attr( $theme ); ?>"
352
			href="<?php echo esc_url( $url ); ?>">
353
			<?php echo esc_html( $text ); ?>
354
		</a>
355
		<?php
356
	}
357
358
359
	/**
360
	 * Handles output of the addons page in admin.
361
	 */
362
	public static function output() {
363
		$sections        = self::get_sections();
364
		$theme           = wp_get_theme();
365
		$section_keys    = array_keys( $sections );
366
		$current_section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : current( $section_keys );
367
		include_once( dirname( __FILE__ ) . '/views/html-admin-page-addons.php' );
368
	}
369
}
370