Test Failed
Push — master ( 76928d...4654fc )
by Devin
09:23
created

Give_Settings_License   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 17.7 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 113
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 2
A get_settings() 0 21 1
A remove_license_tab() 0 11 2
A is_show_setting_page() 6 14 3
A render_license_key() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Give Settings Page/Tab
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Settings_License
7
 * @copyright   Copyright (c) 2016, GiveWP
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.8
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit; // Exit if accessed directly.
14
}
15
16
if ( ! class_exists( 'Give_Settings_License' ) ) :
17
18
	/**
19
	 * Give_Settings_License.
20
	 *
21
	 * @sine 1.8
22
	 */
23
	class Give_Settings_License extends Give_Settings_Page {
24
25
		/**
26
		 * Constructor.
27
		 */
28 View Code Duplication
		public function __construct() {
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...
29
			$this->id    = 'licenses';
30
			$this->label = esc_html__( 'Licenses', 'give' );
31
32
			parent::__construct();
33
34
			// Filter to remove the license tab.
35
			add_filter( 'give-settings_tabs_array', array( $this, 'remove_license_tab' ), 9999999, 1 );
36
37
			// Do not use main form for this tab.
38
			if ( give_get_current_setting_tab() === $this->id ) {
39
				add_action( 'give_admin_field_license_key', array( $this, 'render_license_key' ), 10, 2 );
40
			}
41
		}
42
43
		/**
44
		 * Get settings array.
45
		 *
46
		 * @since  1.8
47
		 * @return array
48
		 */
49
		public function get_settings() {
50
			$settings = array();
51
52
			/**
53
			 * Filter the licenses settings.
54
			 * Backward compatibility: Please do not use this filter. This filter is deprecated in 1.8
55
			 */
56
			$settings = apply_filters( 'give_settings_licenses', $settings );
57
58
			/**
59
			 * Filter the settings.
60
			 *
61
			 * @since  1.8
62
			 *
63
			 * @param  array $settings
64
			 */
65
			$settings = apply_filters( 'give_get_settings_' . $this->id, $settings );
66
67
			// Output.
68
			return $settings;
69
		}
70
71
		/**
72
		 * Remove the license tab if no Give addon
73
		 * is activated.
74
		 *
75
		 * @param array $tabs Give Settings Tabs.
76
		 *
77
		 * @since 2.1.4
78
		 *
79
		 * @return array
80
		 */
81
		public function remove_license_tab( $tabs ) {
82
			/**
83
			 * Remove the license tab if no Give licensed addon
84
			 * is activated.
85
			 */
86
			if ( ! $this->is_show_setting_page() ) {
87
				unset( $tabs['licenses'] );
88
			}
89
90
			return $tabs;
91
		}
92
93
		/**
94
		 * Returns if at least one Give addon is activated.
95
		 * Note: note only for internal logic
96
		 *
97
		 * @since 2.1.4
98
		 * @access private
99
		 *
100
		 * @return bool
101
		 */
102
		private function is_show_setting_page() {
103
			$licensed_addons   = Give_License::get_licensed_addons();
104
			$activated_plugins = get_option( 'active_plugins', array() );
105
106
			// Get list of network enabled plugin.
107 View Code Duplication
			if ( is_multisite() ) {
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...
108
				$sitewide_activated_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
109
				$activated_plugins = ! empty( $activated_plugins )
110
					? array_merge( $sitewide_activated_plugins, $activated_plugins )
111
					: $sitewide_activated_plugins;
112
			}
113
114
			return (bool) count( array_intersect( $activated_plugins, $licensed_addons ) );
115
		}
116
117
118
		/**
119
		 * Render  license key field
120
		 *
121
		 * @since 2.5.0
122
		 *
123
		 * @param $field
124
		 * @param $value
125
		 */
126
		public function render_license_key( $field, $value ) {
127
			ob_start();
128
			give_license_key_callback( $field, $value );
129
130
			printf(
131
				'<div class="give-settings-wrap give-settings-wrap-licenses">%s</div>',
132
				ob_get_clean()
133
			);
134
		}
135
	}
136
137
endif;
138
139
return new Give_Settings_License();
140