Completed
Push — master ( 88dfbf...b0f2bc )
by
unknown
12:27
created

eo_module_management   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 171
Duplicated Lines 4.68 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 8
loc 171
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 0
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A admin_assets() 0 7 2
A declare_options() 0 6 1
C validate_options() 0 40 7
A module_listing() 0 18 2
B core_util() 4 17 8
C extra_modules() 4 41 15

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 if ( !defined( 'ABSPATH' ) ) exit;
2
/**
3
 * Fichier du controleur principal pour la gestion des modules internes dans les extensions wordpress / Main controller file for internal modules management into wordpress plugins
4
 *
5
 * @author Eoxia development team <[email protected]>
6
 * @version 2.0
7
 */
8
9
/*	Check if file is include. No direct access possible with file url	*/
10
if ( !defined( 'WPSHOP_VERSION' ) ) {
11
	die( __('Access is not allowed by this way', 'wpshop') );
12
}
13
14
/**
15
 * Classe du controleur principal pour la gestion des modules internes dans les extensions wordpress / Main controller class for internal modules management into wordpress plugins
16
 *
17
 * @author Eoxia development team <[email protected]>
18
 * @version 2.0
19
 */
20
class eo_module_management {
21
22
	/**
23
	 * Instanciation du gestionnaire de modules /  Instanciate modules manager
24
	 */
25
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
		/**	Ajoute une interface aux options pour gérer les modules / Add an interface to plugin options screen in order to manage modules	*/
27
		add_action( 'admin_init', array( $this, 'declare_options' ), 11 );
28
29
		/**	Appel des styles pour l'administration / Call style for administration	*/
30
		add_action( 'admin_enqueue_scripts', array( &$this, 'admin_assets' ) );
31
	}
32
33
	/**
34
	 * Inclusion des feuilles de styles pour l'administration / Admin css enqueue
35
	 */
36
	function admin_assets( $hook ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
		if ( $hook != 'settings_page_wpshop_option' )
38
			return;
39
40
		wp_register_style( 'eomodmanager-admin-css', EOMODMAN_URL . '/assets/css/backend.css', '', EOMODMAN_VERSION );
41
		wp_enqueue_style( 'eomodmanager-admin-css' );
42
	}
43
44
	/**
45
	 * OPTIONS - Déclare les options permettant de gérer les statuts des modules / Declare add-on configuration panel for managing modules status
46
	 */
47
	function declare_options() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
		add_settings_section( 'wps_internal_modules', '<i class="dashicons dashicons-admin-plugins"></i>' . __( 'Internal modules management', 'eo-modmanager-i18n' ), '', 'wpshop_addons_options' );
49
		register_setting( 'wpshop_options', 'wpshop_modules', array( &$this, 'validate_options' ) );
50
51
		add_settings_field( 'wpshop_opinions_field', __( 'Internal modules management', 'eo-modmanager-i18n' ), array( &$this, 'module_listing' ), 'wpshop_addons_options', 'wps_internal_modules' );
52
	}
53
54
	/**
55
	 * OPTIONS -
56
	 *
57
	 * @param array $input
0 ignored issues
show
Bug introduced by
There is no parameter named $input. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
58
	 *
59
	 * @return array
60
	 */
61
	function validate_options( $settings ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
62
		if ( is_array( $settings ) ) {
63
			$module_option = get_option( 'wpshop_modules' );
64
			$log_error = array();
65
			foreach ( $settings as $module => $module_state ) {
66
				if ( !array_key_exists( 'activated', $module_state ) && ( 'on' == $module_state[ 'old_activated' ] ) ) {
67
					$module_option[ $module ][ 'activated' ] = 'off';
68
					$module_option[ $module ][ 'date_off' ] = gmdate( "Y-m-d H:i:s", time() );
69
					$module_option[ $module ][ 'author_off' ] = get_current_user_id();
70
					$settings[ $module ] = $module_option[ $module ];
71
72
					/**	Log module activation	*/
73
					$user = get_userdata( $module_option[ $folder ][ 'author_on' ] );
0 ignored issues
show
Bug introduced by
The variable $folder does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
74
					$author = $user->display_name;
75
					$log_error[ 'message' ] = sprintf( __( 'Activation made on %1$s by %2$s', 'eo-modmanager-i18n' ), mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $settings[ $module ][ 'date_on' ], true ), $author);
76
				}
77
				else if ( array_key_exists( 'activated', $module_state ) && ( 'off' == $module_state[ 'old_activated' ] ) ) {
78
					$module_option[ $module ][ 'activated' ] = 'on';
79
					$module_option[ $module ][ 'date_on' ] = gmdate( "Y-m-d H:i:s", time() );
80
					$module_option[ $module ][ 'author_on' ] = get_current_user_id();
81
					$settings[ $module ] = $module_option[ $module ];
82
83
					/**	Log module activation	*/
84
					$user = get_userdata( $module_option[ $folder ][ 'author_off' ] );
85
					$author = $user->display_name;
86
					$log_error[ 'message' ] = sprintf( __( 'Deactivation made on %1$s by %2$s', 'eo-modmanager-i18n' ), mysql2date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $settings[ $module ][ 'date_off' ], true ), $author);
87
				}
88
				else {
89
					$settings[ $module ] = $module_option[ $module ];
90
				}
91
				unset( $settings[ $module ][ 'old_activated' ] );
92
				$log_error[ 'object_id' ] = $module;
93
			}
94
95
			wpeologs_ctr::log_datas_in_files( 'wps_addon', $log_error, 0 );
96
97
		}
98
99
		return $settings;
100
	}
101
102
	/**
103
	 * OPTIONS - Affiche les modules présents et leur état actuel / Display all modules and they current state
104
	 */
105
	function module_listing() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
106
		/**	Define the directory containing all extra modules for current plugin	*/
107
		$module_folder = WPSHOP_MODULES_DIR;
108
109
		/**	Get	current modules options to know if they are activated or not */
110
		$module_option = get_option( 'wpshop_modules' );
0 ignored issues
show
Unused Code introduced by
$module_option 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...
111
112
		/**	Check if the defined directory exists for reading and displaying an input to activate/deactivate the module	*/
113
		if( is_dir( $module_folder ) ) {
114
			$parent_folder_content = scandir( $module_folder );
0 ignored issues
show
Unused Code introduced by
$parent_folder_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...
115
116
			require_once( wpshop_tools::get_template_part( EOMODMAN_DIR, EOMODMAN_TEMPLATES_MAIN_DIR, 'backend', 'settings' ) );
117
		}
118
		else {
119
			_e( 'There is no modules to include into current plugin', 'eo-modmanager-i18n' );
120
		}
121
122
	}
123
124
	/**
125
	 * CORE - Activation des modules "coeur" ne devant pas être désactivés / Activation of "core" modules that does not have to be deactivated
126
	 */
127
	public static function core_util() {
128
		/**	Define the directory containing all "core" modules for current plugin	*/
129
		$module_folder = WPSHOP_DIR . '/core/';
130
131
		/**	Check if the defined directory exists for reading and including the different modules	*/
132
		if( is_dir( $module_folder ) ) {
133
			$parent_folder_content = scandir( $module_folder );
134
			foreach ( $parent_folder_content as $folder ) {
135
				if ( $folder && substr( $folder, 0, 1) != '.' && ( EOMODMAN_DIR != $folder ) ) {
136 View Code Duplication
					if ( is_dir( $module_folder . $folder ) && file_exists( $module_folder . $folder . '/' . $folder . '.php') ) {
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...
137
						$f =  $module_folder . $folder . '/' . $folder . '.php';
138
						require( $f );
139
					}
140
				}
141
			}
142
		}
143
	}
144
145
	/**
146
	 * CORE - Activations des modules complémentaires pour l'extension / Activation of complementary modules for plugin
147
	 */
148
	public static function extra_modules() {
149
		/**	Define the directory containing all extra modules for current plugin	*/
150
		$module_folder = WPSHOP_MODULES_DIR;
151
152
		/**	Get	current modules options to know if they are activated or not */
153
		$module_option = get_option( 'wpshop_modules' );
154
155
		/**	Check if the defined directory exists for reading and including the different modules	*/
156
		if( is_dir( $module_folder ) ) {
157
			$parent_folder_content = scandir( $module_folder );
158
			$update_option = false;
159
			foreach ( $parent_folder_content as $folder ) {
160
				if ( $folder && substr( $folder, 0, 1) != '.' ) {
161
					$is_activated = false;
162
					/**	Check current module state to know if we have to include it or not	*/
163
					if ( !empty( $module_option ) && array_key_exists( $folder, $module_option ) && ( 'on' == $module_option[ $folder ][ 'activated' ] ) ) {
164
						$is_activated = true;
165
					}
166
					else if ( empty( $module_option ) || ( !empty( $module_option ) && !array_key_exists( $folder, $module_option ) ) ) {
167
						$module_option[ $folder ] = array(
168
							'activated' => 'on',
169
							'date_on' => gmdate( "Y-m-d H:i:s", time() ),
170
							'author_on' => 'auto',
171
						);
172
						$is_activated = true;
173
						$update_option = true;
174
					}
175
176
					/**	Finaly include module if the state allow it	*/
177 View Code Duplication
					if ( $is_activated && is_dir( $module_folder . $folder ) && file_exists( $module_folder . $folder . '/' . $folder . '.php') ) {
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...
178
						$f =  $module_folder . $folder . '/' . $folder . '.php';
179
						require( $f );
180
					}
181
				}
182
			}
183
			/**	Update option only if it is necessary	*/
184
			if ( $update_option ) {
185
				update_option( 'wpshop_modules', $module_option );
186
			}
187
		}
188
	}
189
190
}
191