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

wps_installer_ctr::admin_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
/**
3
 * File for installer control class definition
4
 *
5
 * @author Development team <[email protected]>
6
 * @version 1.0
7
 *
8
 */
9
10
/**
11
 * Class for installer control
12
 *
13
 * @author Development team <[email protected]>
14
 * @version 1.0
15
 *
16
 */
17
class wps_installer_ctr {
18
19
	/**	Get the current step of installation	*/
20
	private $current_installation_step;
21
22
	/**
23
	 * Instanciate the module controller
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
27
		/**	Call administration style definition & scripts	*/
28
		add_action( 'admin_init', array( &$this, 'admin_scripts' ) );
29
30
		$current_step = ( !empty( $_GET['wps-installation-step'] ) ) ? sanitize_title( $_GET['wps-installation-step'] ) : $this->current_installation_step;
31
32
		/**	Instanciate datas saver components */
33
		$wps_installer_model = new wps_installer_model();
34
35
		$action = !empty( $_POST[ 'action' ] ) ? sanitize_text_field( $_POST[ 'action' ] ) : '';
36
		/**	Call datas saver	*/
37
		if ( !empty( $current_step ) && !empty( $action ) && ( "wps-installation" == $action ) ) {
38
			$step_to_save = $current_step - 1;
39
			$wps_installer_model->save_step( $step_to_save );
40
			if ( WPSINSTALLER_STEPS_COUNT == $current_step ) {
41
				add_action( 'init', array( &$this, 'go_to_wpshop_about' ) );
42
			}
43
		}
44
45
		/**	Set the current installatino step	*/
46
		$this->current_installation_step = get_option( 'wps-installation-current-step', 1 );
47
48
		/**	Get current version for wpshop plugin	*/
49
		$wps_current_db_version = get_option( 'wpshop_db_options', 0 );
50
51
		/**	Check the configuration state	*/
52
		$installation_state = !empty( $_GET[ 'installation_state' ] ) ? sanitize_text_field( $_GET[ 'installation_state' ] ) : '';
53
		if ( isset( $installation_state ) && !empty( $installation_state ) && !empty( $wps_current_db_version )
54
		&& (empty( $wps_current_db_version[ 'installation_state' ] ) || ( $wps_current_db_version[ 'installation_state' ] != 'completed' ) ) ) {
55
			$wps_current_db_version = $wps_installer_model->installer_state_saver( $installation_state, $wps_current_db_version );
0 ignored issues
show
Documentation introduced by
$wps_current_db_version is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
		}
57
58
		/**	Do verification for shop who are configured for being sale shop	*/
59
		$current_page = strstr( $_SERVER[ "REQUEST_URI" ], 'wps-about');
60
		if ( isset( $wps_current_db_version['installation_state'] ) && ( $wps_current_db_version[ 'installation_state' ] == 'completed' ) && ( WPSHOP_DEFINED_SHOP_TYPE == 'sale' ) && empty( $current_page ) ) {
61
			add_action( 'admin_notices', array( 'wpshop_notices' , 'sale_shop_notice' ) );
62
		}
63
64
		/**	Create an administration menu	*/
65
		add_action( 'admin_menu', array( $this, 'admin_menu' ), 10 );
66
67
		/**	In case that configuration have not been done and that instalation is not asked to be ignored	*/
68
		if ( empty( $wps_current_db_version ) || empty( $wps_current_db_version[ 'installation_state' ] ) || ( !empty( $wps_current_db_version[ 'installation_state' ] ) && !in_array( $wps_current_db_version[ 'installation_state' ], array( 'completed', 'ignored' ) ) ) ) {
69
			/*	Check the db installation state for admin message output	*/
70
			$current_page = strstr( $_SERVER[ "REQUEST_URI" ], 'wps-installer');
71
			if( ( WPSINSTALLER_STEPS_COUNT > $this->current_installation_step ) && ( empty( $current_page ) ) ) {
72
				add_action( 'admin_notices', array( &$this, 'install_admin_notice' ) );
73
			}
74
		}
75
76
		/**	Hook wpshop dashboard in order to display the notice banner with quick links - wordpress like	*/
77
		add_filter( 'wps-dashboard-notice', array( $this, 'wps_dashboard_notice' ), 10, 1 );
78
79
		/**	Hook ajax action when clicking on hide welcome banner on wpshop dashboard	*/
80
		add_action( 'wp_ajax_wps-hide-welcome-panel', array( $this, 'wps_hide_welcome_panel' ) );
81
	}
82
83
84
	/**
85
	 * Enqueue style definition
86
	 * Enqueue scripts definition
87
	 */
88 View Code Duplication
	function admin_scripts($hook_suffix) {
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...
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...
89
		if ( 'wps-installer.php' !== $hook_suffix )
90
        	return;
91
92
		wp_register_style( 'wps_installer_style', WPS_INSTALLER_URL . WPS_INSTALLER_DIR . '/assets/css/backend-styles.css', '', WPS_INSTALLER_VERSION );
93
		wp_enqueue_style( 'wps_installer_style' );
94
		wp_enqueue_script( 'wps-installer-admin-scripts', WPS_INSTALLER_URL . WPS_INSTALLER_DIR . '/assets/js/backend-scripts.js', array( 'jquery' ), WPS_INSTALLER_VERSION );
95
	}
96
97
	/**
98
	 * Call the menu for displaying installer interface
99
	 */
100
	function admin_menu() {
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...
101
		/**	Get current version for wpshop plugin	*/
102
		$wps_current_db_version = get_option( 'wpshop_db_options', 0 );
0 ignored issues
show
Unused Code introduced by
$wps_current_db_version 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...
103
104
		add_menu_page( __( 'Install WPShop', 'wpshop' ), __( 'WPShop - install', 'wpshop' ), 'manage_options', 'wps-installer', array( &$this, 'installer_main_page' ) );
105
		remove_menu_page( 'wps-installer' );
106
	}
107
108
	/**
109
	 * Display the installer interface
110
	 */
111
	function installer_main_page() {
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...
112
		$current_step = ( !empty( $_GET['wps-installation-step'] ) ) ? wpshop_tools::varSanitizer( $_GET['wps-installation-step'] ) : $this->current_installation_step;
113
		$steps = unserialize( WPSINSTALLER_STEPS );
0 ignored issues
show
Unused Code introduced by
$steps 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...
114
115
		/**	Get the defined shop type in order to display the different element to	*/
116
		$wps_shop_type = get_option( 'wpshop_shop_type', WPSHOP_DEFAULT_SHOP_TYPE );
0 ignored issues
show
Unused Code introduced by
$wps_shop_type 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...
117
118
		/** Check the current step to display */
119
		$current_step_output = '';
0 ignored issues
show
Unused Code introduced by
$current_step_output 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...
120
		$the_step_file = '';
0 ignored issues
show
Unused Code introduced by
$the_step_file 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...
121
		switch( $current_step ) {
122
			case 2:
123
				$the_step_file = 'step_two';
124
				break;
125
126
			default:
127
				$the_step_file = 'step_one';
128
				break;
129
		}
130
131
		/**	Create display for current step	*/
132
		ob_start();
133
		require_once( wpshop_tools::get_template_part( WPS_INSTALLER_DIR, WPSINSTALLER_TPL_DIR, "backend", $the_step_file ) );
134
		$current_step_output = ob_get_contents();
0 ignored issues
show
Unused Code introduced by
$current_step_output 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...
135
		ob_end_clean();
136
137
		require_once( wpshop_tools::get_template_part( WPS_INSTALLER_DIR, WPSINSTALLER_TPL_DIR, "backend", "installer" ) );
138
	}
139
140
	/**
141
	 * Create a notice for admin user when plugin is activated and not yet configured
142
	 */
143
	function install_admin_notice() {
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...
144
		require_once( wpshop_tools::get_template_part( WPS_INSTALLER_DIR, WPSINSTALLER_TPL_DIR, "backend", "notice" ) );
145
	}
146
147
	/**
148
	 * Redirect the user automatically to the wpshop about page
149
	 */
150
	function go_to_wpshop_about() {
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...
151
		wp_redirect( admin_url( 'admin.php?page=wpshop_about' ) );
152
		exit();
153
	}
154
155
	/**
156
	 * DISPLAY - Output the about page for wpshop
157
	 */
158
	function wps_about_page() {
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...
159
		require_once( wpshop_tools::get_template_part( WPS_INSTALLER_DIR, WPSINSTALLER_TPL_DIR, "backend", "about" ) );
160
	}
161
162
	/**
163
	 * DISPLAY - Display a banner on wpshop dashboard
164
	 */
165
	function wps_dashboard_notice() {
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...
166
		$user_pref = get_user_meta( get_current_user_id(), '_wps_hide_notice_messages_indicator', true );
167
168
		if ( empty( $user_pref ) || empty( $user_pref[ 'welcome-banner' ] ) ) {
169
			/**	Get current shop type	*/
170
			$shop_type = get_option( 'wpshop_shop_type' );
0 ignored issues
show
Unused Code introduced by
$shop_type 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...
171
172
			/**	Get the current number of product created	*/
173
			$nb_products = 0;
174
			$created_products = wp_count_posts( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT );
175
			if ( !empty( $created_products ) ) {
176
				foreach ( $created_products as $created_product_type => $created_product_nb) {
177
					/**	Don't count product that are automatically created and not accessible through amin interface	*/
178
					if ( !in_array( $created_product_type, array( 'auto-draft', 'inherit' ) ) ) {
179
						$nb_products += $created_product_nb;
180
					}
181
				}
182
			}
183
184
			/**	Get configuration about payment method 	*/
185
			$no_payment_mode_configurated = true;
0 ignored issues
show
Unused Code introduced by
$no_payment_mode_configurated 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...
186 View Code Duplication
			if ( !empty($paymentMethod ) && !empty($paymentMethod['mode']) ) {
0 ignored issues
show
Bug introduced by
The variable $paymentMethod seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
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...
187
				foreach( $paymentMethod['mode'] as $k => $pm ) {
188
					if ( !empty($pm['active'] ) ) {
189
						$no_payment_mode_configurated = false;
0 ignored issues
show
Unused Code introduced by
$no_payment_mode_configurated 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...
190
					}
191
				}
192
			}
193
194
			/**	Get configuration about emails 	*/
195
			$emails = get_option('wpshop_emails', array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$emails 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...
196
197
			require_once( wpshop_tools::get_template_part( WPS_INSTALLER_DIR, WPSINSTALLER_TPL_DIR, "backend", "welcome" ) );
198
		}
199
	}
200
201
	/**
202
	 * AJAX - Launch ajax action allowing to hide welcome panel for current user
203
	 */
204
	function wps_hide_welcome_panel() {
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...
205
		$wpshop_ajax_nonce = !empty( $_REQUEST['wpshop_ajax_nonce'] ) ? sanitize_text_field( $_REQUEST['wpshop_ajax_nonce'] ) : '';
0 ignored issues
show
Unused Code introduced by
$wpshop_ajax_nonce 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...
206
207
		if ( !wp_verify_nonce( $_wpnonce, 'wps-installer-welcome-panel-close' ) )
0 ignored issues
show
Bug introduced by
The variable $_wpnonce 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...
208
			wp_die();
209
210
		$user_pref = get_user_meta( get_current_user_id(), '_wps_hide_notice_messages_indicator', true );
211
		$user_pref[ 'welcome-banner' ] = true;
212
213
		$response = array(
214
			'status' => false,
215
		);
216
217
		$user_pref = update_user_meta( get_current_user_id(), '_wps_hide_notice_messages_indicator', $user_pref );
218
		if ( false !== $user_pref ) {
219
			$response[ 'status' ] = true;
220
		}
221
222
		wp_die( json_encode( $response ) );
223
	}
224
}
225
226
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
227