Completed
Push — hotfix/r18 ( c27e1c...daafc2 )
by Ravinder
37:16 queued 17:07
created

install.php ➔ give_run_install()   F

Complexity

Conditions 11
Paths 256

Size

Total Lines 182
Code Lines 93

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 93
nc 256
nop 0
dl 0
loc 182
rs 3.8181
c 0
b 0
f 0

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 29 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     https://opensource.org/licenses/gpl-license 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
 *
24
 * @param bool $network_wide
25
 *
26
 * @global     $wpdb
27
 * @return void
28
 */
29
function give_install( $network_wide = false ) {
30
31
	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...
32
33
	if ( is_multisite() && $network_wide ) {
34
35
		foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) {
36
37
			switch_to_blog( $blog_id );
38
			give_run_install();
39
			restore_current_blog();
40
41
		}
42
43
	} else {
44
45
		give_run_install();
46
47
	}
48
49
}
50
51
register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );
52
53
/**
54
 * Run the Give Install process.
55
 *
56
 * @since  1.5
57
 * @return void
58
 */
59
function give_run_install() {
60
61
	$give_options = give_get_settings();
62
63
	// Setup the Give Custom Post Types.
64
	give_setup_post_types();
65
66
	// Clear the permalinks.
67
	flush_rewrite_rules( false );
68
69
	// Add Upgraded From Option.
70
	$current_version = get_option( 'give_version' );
71
	if ( $current_version ) {
72
		update_option( 'give_version_upgraded_from', $current_version );
73
	}
74
75
	// Setup some default options.
76
	$options = array();
77
78
	// Checks if the Success Page option exists AND that the page exists.
79
	if ( ! get_post( give_get_option( 'success_page' ) ) ) {
80
81
		// Donation Confirmation (Success) Page
82
		$success = wp_insert_post(
83
			array(
84
				'post_title'     => esc_html__( 'Donation Confirmation', 'give' ),
85
				'post_content'   => '[give_receipt]',
86
				'post_status'    => 'publish',
87
				'post_author'    => 1,
88
				'post_type'      => 'page',
89
				'comment_status' => 'closed'
90
			)
91
		);
92
93
		// Store our page IDs
94
		$options['success_page'] = $success;
95
	}
96
97
	// Checks if the Failure Page option exists AND that the page exists.
98
	if ( ! get_post( give_get_option( 'failure_page' ) ) ) {
99
100
		// Failed Donation Page
101
		$failed = wp_insert_post(
102
			array(
103
				'post_title'     => esc_html__( 'Donation Failed', 'give' ),
104
				'post_content'   => esc_html__( 'We\'re sorry, your donation failed to process. Please try again or contact site support.', 'give' ),
105
				'post_status'    => 'publish',
106
				'post_author'    => 1,
107
				'post_type'      => 'page',
108
				'comment_status' => 'closed'
109
			)
110
		);
111
112
		$options['failure_page'] = $failed;
113
	}
114
115
	// Checks if the History Page option exists AND that the page exists.
116
	if ( ! get_post( give_get_option( 'history_page' ) ) ) {
117
		// Donation History Page
118
		$history = wp_insert_post(
119
			array(
120
				'post_title'     => esc_html__( 'Donation History', 'give' ),
121
				'post_content'   => '[donation_history]',
122
				'post_status'    => 'publish',
123
				'post_author'    => 1,
124
				'post_type'      => 'page',
125
				'comment_status' => 'closed'
126
			)
127
		);
128
129
		$options['history_page'] = $history;
130
	}
131
132
	//Fresh Install? Setup Test Mode, Base Country (US), Test Gateway, Currency.
133
	if ( empty( $current_version ) ) {
134
135
		// General.
136
		$options['base_country']       = 'US';
137
		$options['test_mode']          = 'enabled';
138
		$options['currency']           = 'USD';
139
		$options['currency_position']   = 'before';
140
		$options['session_lifetime']   = '604800';
141
		$options['email_access']       = 'disabled';
142
		$options['number_decimals']    = 2;
143
144
		// Display options.
145
		$options['css']                  = 'enabled';
146
		$options['floatlabels']          = 'disabled';
147
		$options['welcome']              = 'enabled';
148
		$options['forms_singular']       = 'enabled';
149
		$options['forms_archives']       = 'enabled';
150
		$options['forms_excerpt']        = 'enabled';
151
		$options['form_featured_img']    = 'enabled';
152
		$options['form_sidebar']         = 'enabled';
153
		$options['categories']           = 'disabled';
154
		$options['tags']                 = 'disabled';
155
		$options['terms']                = 'disabled';
156
		$options['admin_notices']        = 'enabled';
157
		$options['uninstall_on_delete']  = 'disabled';
158
		$options['the_content_filter']   = 'enabled';
159
		$options['scripts_footer']       = 'disabled';
160
161
		// Paypal IPN verification.
162
		$options['paypal_verification']       = 'enabled';
163
164
		// Default is manual gateway.
165
		$options['gateways']['manual'] = 1;
166
		$options['default_gateway']    = 'manual';
167
168
		// Offline gateway setup.
169
		$options['gateways']['offline']             = 1;
170
		$options['global_offline_donation_content'] = give_get_default_offline_donation_content();
171
172
		// Billing address.
173
		$options['give_offline_donation_enable_billing_fields'] = 'disabled';
174
175
		// Default donation notification email.
176
		$options['donation_notification'] = give_get_default_donation_notification_email();
177
178
		// Default email receipt message.
179
		$options['donation_receipt'] = give_get_default_donation_receipt_email();
180
181
	}
182
183
	// Populate the default values.
184
	update_option( 'give_settings', array_merge( $give_options, $options ) );
185
186
	/**
187
	 * Fire action
188
	 *
189
	 * @since 1.8
190
	 */
191
	do_action( 'give_core_upgrades' );
192
193
	// Update plugin version.
194
	if( GIVE_VERSION != get_option( 'give_version' ) ) {
195
		update_option( 'give_version', GIVE_VERSION );
196
	}
197
198
	// Create Give roles.
199
	$roles = new Give_Roles();
200
	$roles->add_roles();
201
	$roles->add_caps();
202
203
	$api = new Give_API();
204
	update_option( 'give_default_api_version', 'v' . $api->get_version() );
205
206
	// Create the customers databases.
207
	@Give()->customers->create_table();
208
	@Give()->customer_meta->create_table();
209
210
	// Check for PHP Session support, and enable if available.
211
	Give()->session->use_php_sessions();
212
213
	// Add a temporary option to note that Give pages have been created.
214
	set_transient( '_give_installed', $options, 30 );
215
216
	if ( ! $current_version ) {
217
218
		require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
219
220
		// When new upgrade routines are added, mark them as complete on fresh install.
221
		$upgrade_routines = array(
222
			'upgrade_give_user_caps_cleanup',
223
			'upgrade_give_payment_customer_id',
224
			'upgrade_give_offline_status'
225
		);
226
227
		foreach ( $upgrade_routines as $upgrade ) {
228
			give_set_upgrade_complete( $upgrade );
229
		}
230
	}
231
232
	// Bail if activating from network, or bulk.
233
	if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
234
		return;
235
	}
236
237
	// Add the transient to redirect.
238
	set_transient( '_give_activation_redirect', true, 30 );
239
240
}
241
242
register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );
243
244
/**
245
 * Network Activated New Site Setup.
246
 *
247
 * 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.
248
 *
249
 * @since      1.3.5
250
 *
251
 * @param  int    $blog_id The Blog ID created.
252
 * @param  int    $user_id The User ID set as the admin.
253
 * @param  string $domain  The URL.
254
 * @param  string $path    Site Path.
255
 * @param  int    $site_id The Site ID.
256
 * @param  array  $meta    Blog Meta.
257
 */
258
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...
259
260
	if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
261
262
		switch_to_blog( $blog_id );
263
		give_install();
264
		restore_current_blog();
265
266
	}
267
268
}
269
270
add_action( 'wpmu_new_blog', 'on_create_blog', 10, 6 );
271
272
273
/**
274
 * Drop Give's custom tables when a mu site is deleted.
275
 *
276
 * @since  1.4.3
277
 *
278
 * @param  array $tables  The tables to drop.
279
 * @param  int   $blog_id The Blog ID being deleted.
280
 *
281
 * @return array          The tables to drop.
282
 */
283
function give_wpmu_drop_tables( $tables, $blog_id ) {
284
285
	switch_to_blog( $blog_id );
286
	$customers_db     = new Give_DB_Customers();
287
	$customer_meta_db = new Give_DB_Customer_Meta();
288
289
	if ( $customers_db->installed() ) {
290
		$tables[] = $customers_db->table_name;
291
		$tables[] = $customer_meta_db->table_name;
292
	}
293
	restore_current_blog();
294
295
	return $tables;
296
297
}
298
299
add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );
300
301
/**
302
 * Post-installation
303
 *
304
 * Runs just after plugin installation and exposes the give_after_install hook.
305
 *
306
 * @since 1.0
307
 * @return void
308
 */
309
function give_after_install() {
310
311
	if ( ! is_admin() ) {
312
		return;
313
	}
314
315
	$give_options     = get_transient( '_give_installed' );
316
	$give_table_check = get_option( '_give_table_check', false );
317
318
	if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {
319
320
		if ( ! @Give()->customer_meta->installed() ) {
321
322
			// Create the customer meta database
323
			// (this ensures it creates it on multisite instances where it is network activated).
324
			@Give()->customer_meta->create_table();
325
326
		}
327
328
		if ( ! @Give()->customers->installed() ) {
329
			// Create the customers database
330
			// (this ensures it creates it on multisite instances where it is network activated).
331
			@Give()->customers->create_table();
332
333
			/**
334
			 * Fires after plugin installation.
335
			 *
336
			 * @since 1.0
337
			 *
338
			 * @param array $give_options Give plugin options.
339
			 */
340
			do_action( 'give_after_install', $give_options );
341
		}
342
343
		update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ) );
344
345
	}
346
347
	// Delete the transient
348
	if ( false !== $give_options ) {
349
		delete_transient( '_give_installed' );
350
	}
351
352
353
}
354
355
add_action( 'admin_init', 'give_after_install' );
356
357
358
/**
359
 * Install user roles on sub-sites of a network
360
 *
361
 * Roles do not get created when Give is network activation so we need to create them during admin_init
362
 *
363
 * @since 1.0
364
 * @return void
365
 */
366
function give_install_roles_on_network() {
367
368
	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...
369
370
	if ( ! is_object( $wp_roles ) ) {
371
		return;
372
	}
373
374
	if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {
375
376
		// Create Give plugin roles
377
		$roles = new Give_Roles();
378
		$roles->add_roles();
379
		$roles->add_caps();
380
381
	}
382
383
}
384
385
add_action( 'admin_init', 'give_install_roles_on_network' );
386