Completed
Push — master ( cbfad0...1d76cf )
by Devin
67:21 queued 47:22
created

install.php ➔ give_run_install()   D

Complexity

Conditions 10
Paths 128

Size

Total Lines 144
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 60
CRAP Score 11.2294

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 72
c 1
b 0
f 1
nc 128
nop 0
dl 0
loc 144
ccs 60
cts 78
cp 0.7692
crap 11.2294
rs 4.606

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 14.

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.

Loading history...
2
/**
3
 * Install Function
4
 *
5
 * @package     Give
6
 * @subpackage  Functions/Install
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Install
19
 *
20
 * Runs on plugin install by setting up the post types, custom taxonomies, flushing rewrite rules to initiate the new 'donations' slug and also creates the plugin and populates the settings fields for those plugin pages. After successful install, the user is redirected to the Give Welcome screen.
21
 *
22
 * @since 1.0
23
 * @global $wpdb
24
 * @global $wp_version
25
 * @return void
26
 */
27
function give_install( $network_wide = false ) {
28
29 2
	global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
30
31 2
	if ( is_multisite() && $network_wide ) {
32
33
		foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) {
34
35
			switch_to_blog( $blog_id );
36
			give_run_install();
37
			restore_current_blog();
38
39
		}
40
41
	} else {
42
43 2
		give_run_install();
44
45
	}
46
47 2
}
48
49
register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );
50
51
/**
52
 * Run the Give Install process.
53
 *
54
 * @since  1.5
55
 * @return void
56
 */
57
function give_run_install() {
58
59 2
	$give_options = give_get_settings();
60
61
	// Setup the Give Custom Post Types.
62 2
	give_setup_post_types();
63
64
	// Clear the permalinks.
65 2
	flush_rewrite_rules( false );
66
67
	// Add Upgraded From Option.
68 2
	$current_version = get_option( 'give_version' );
69 2
	if ( $current_version ) {
70 2
		update_option( 'give_version_upgraded_from', $current_version );
71 2
	}
72
73
	// Setup some default options.
74 2
	$options = array();
75
76
	// Checks if the Success Page option exists AND that the page exists.
77 2
	if ( ! get_post( give_get_option( 'success_page' ) ) ) {
78
79
		// Donations Confirmation (Success) Page.
80 2
		$success = wp_insert_post(
81
			array(
82 2
				'post_title'     => esc_html__( 'Donation Confirmation', 'give' ),
83 2
				'post_content'   => '[give_receipt]',
84 2
				'post_status'    => 'publish',
85 2
				'post_author'    => 1,
86 2
				'post_type'      => 'page',
87
				'comment_status' => 'closed'
88 2
			)
89 2
		);
90
91
		// Store our page IDs
92 2
		$options['success_page'] = $success;
93 2
	}
94
95
	// Checks if the Failure Page option exists AND that the page exists.
96 2
	if ( ! get_post( give_get_option( 'failure_page' ) ) ) {
97
98
		// Failed Donation Page.
99 2
		$failed = wp_insert_post(
100
			array(
101 2
				'post_title'     => esc_html__( 'Transaction Failed', 'give' ),
102 2
				'post_content'   => esc_html__( 'We\'re sorry, your transaction failed to process. Please try again or contact site support.', 'give' ),
103 2
				'post_status'    => 'publish',
104 2
				'post_author'    => 1,
105 2
				'post_type'      => 'page',
106
				'comment_status' => 'closed'
107 2
			)
108 2
		);
109
110 2
		$options['failure_page'] = $failed;
111 2
	}
112
113
	// Checks if the History Page option exists AND that the page exists.
114 2
	if ( ! get_post( give_get_option( 'history_page' ) ) ) {
115
		// Purchase History (History) Page.
116 2
		$history = wp_insert_post(
117
			array(
118 2
				'post_title'     => esc_html__( 'Donation History', 'give' ),
119 2
				'post_content'   => '[donation_history]',
120 2
				'post_status'    => 'publish',
121 2
				'post_author'    => 1,
122 2
				'post_type'      => 'page',
123
				'comment_status' => 'closed'
124 2
			)
125 2
		);
126
127 2
		$options['history_page'] = $history;
128 2
	}
129
130
	//Fresh Install? Setup Test Mode, Base Country (US), Test Gateway, Currency.
131 2
	if ( empty( $current_version ) ) {
132
		$options['base_country']       = 'US';
133
		$options['test_mode']          = 1;
134
		$options['currency']           = 'USD';
135
		$options['session_lifetime']   = '604800';
136
		$options['gateways']['manual'] = 1;
137
		$options['default_gateway']    = 'manual'; //default is manual gateway.
138
139
		//Offline gateway setup.
140
		$options['gateways']['offline']             = 1;
141
		$options['global_offline_donation_content'] = give_get_default_offline_donation_content();
142
143
		//Default number of decimals.
144
		$options['number_decimals'] = 2;
145
146
		//Default donation notification email.
147
		$options['donation_notification'] = give_get_default_donation_notification_email();
148 2
149 2
		//Default email receipt message.
150
		$options['donation_receipt'] = give_get_default_donation_receipt_email();
151
152 2
	}
153 2
154 2
	// Populate the default values.
155
	update_option( 'give_settings', array_merge( $give_options, $options ) );
156
	update_option( 'give_version', GIVE_VERSION );
157 2
158 2
	// Create Give roles.
159 2
	$roles = new Give_Roles();
160
	$roles->add_roles();
161 2
	$roles->add_caps();
162 2
163
	$api = new Give_API();
164
	update_option( 'give_default_api_version', 'v' . $api->get_version() );
165 2
166
	// Create the customers databases.
167
	@Give()->customers->create_table();
168 2
	@Give()->customer_meta->create_table();
169
170
	// Check for PHP Session support, and enable if available.
171 2
	Give()->session->use_php_sessions();
172
173 2
	// Add a temporary option to note that Give pages have been created.
174
	set_transient( '_give_installed', $options, 30 );
175
176
	if ( ! $current_version ) {
177
178
		require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
179
180
		// When new upgrade routines are added, mark them as complete on fresh install.
181
		$upgrade_routines = array(
182
			'upgrade_give_user_caps_cleanup',
183
			'upgrade_give_payment_customer_id',
184
			'upgrade_give_offline_status'
185
		);
186
187
		foreach ( $upgrade_routines as $upgrade ) {
188
			give_set_upgrade_complete( $upgrade );
189
		}
190 2
	}
191 1
192
	// Bail if activating from network, or bulk.
193
	if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
194
		return;
195 1
	}
196
197 1
	// Add the transient to redirect.
198
	set_transient( '_give_activation_redirect', true, 30 );
199
200
}
201
202
register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );
203
204
/**
205
 * Network Activated New Site Setup.
206
 *
207
 * When a new site is created when Give is network activated this function runs the appropriate install function to set up the site for Give.
208
 *
209
 * @since      1.3.5
210
 *
211
 * @param  int    $blog_id The Blog ID created.
212
 * @param  int    $user_id The User ID set as the admin.
213
 * @param  string $domain The URL.
214
 * @param  string $path Site Path.
215
 * @param  int    $site_id The Site ID.
216
 * @param  array  $meta Blog Meta.
217
 */
218
function on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $domain is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $site_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
220
	if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
221
222
		switch_to_blog( $blog_id );
223
		give_install();
224
		restore_current_blog();
225
226
	}
227
228
}
229
230
add_action( 'wpmu_new_blog', 'on_create_blog', 10, 6 );
231
232
233
/**
234
 * Drop Give's custom tables when a mu site is deleted.
235
 *
236
 * @since  1.4.3
237
 *
238
 * @param  array $tables The tables to drop.
239
 * @param  int   $blog_id The Blog ID being deleted.
240
 *
241
 * @return array          The tables to drop.
242
 */
243
function give_wpmu_drop_tables( $tables, $blog_id ) {
244
245
	switch_to_blog( $blog_id );
246
	$customers_db     = new Give_DB_Customers();
247
	$customer_meta_db = new Give_DB_Customer_Meta();
248
249
	if ( $customers_db->installed() ) {
250
		$tables[] = $customers_db->table_name;
251
		$tables[] = $customer_meta_db->table_name;
252
	}
253
	restore_current_blog();
254
255
	return $tables;
256
257
}
258
259
add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );
260
261
/**
262
 * Post-installation
263
 *
264
 * Runs just after plugin installation and exposes the give_after_install hook.
265 3
 *
266 1
 * @since 1.0
267
 * @return void
268
 */
269 2
function give_after_install() {
270 2
271
	if ( ! is_admin() ) {
272 2
		return;
273
	}
274 2
275
	$give_options     = get_transient( '_give_installed' );
276
	$give_table_check = get_option( '_give_table_check', false );
277
278
	if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {
279
280
		if ( ! @Give()->customer_meta->installed() ) {
281 2
282
			// Create the customer meta database
283 2
			// (this ensures it creates it on multisite instances where it is network activated).
284
			@Give()->customer_meta->create_table();
285
286 2
		}
287 1
288 1
		if ( ! @Give()->customers->installed() ) {
289
			// Create the customers database
290
			// (this ensures it creates it on multisite instances where it is network activated).
291 2
			@Give()->customers->create_table();
292
293
			do_action( 'give_after_install', $give_options );
294
		}
295
296
		update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ) );
297
298
	}
299
300
	// Delete the transient
301
	if ( false !== $give_options ) {
302
		delete_transient( '_give_installed' );
303
	}
304
305
306 2
}
307
308 2
add_action( 'admin_init', 'give_after_install' );
309 1
310
311
/**
312 1
 * Install user roles on sub-sites of a network
313
 *
314
 * Roles do not get created when Give is network activation so we need to create them during admin_init
315 1
 *
316 1
 * @since 1.0
317 1
 * @return void
318
 */
319 1
function give_install_roles_on_network() {
320
321 1
	global $wp_roles;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
322
323
	if ( ! is_object( $wp_roles ) ) {
324
		return;
325
	}
326
327
	if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {
328
329
		// Create Give plugin roles
330
		$roles = new Give_Roles();
331
		$roles->add_roles();
332
		$roles->add_caps();
333
334
	}
335
336
}
337
338
add_action( 'admin_init', 'give_install_roles_on_network' );