Completed
Pull Request — master (#1854)
by Devin
10:39
created

install.php ➔ give_run_install()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 83
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 32
nop 0
dl 0
loc 83
ccs 42
cts 48
cp 0.875
crap 8.125
rs 5.8854
c 0
b 0
f 0

How to fix   Long Method   

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 2
function give_install( $network_wide = false ) {
30
31 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...
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 2
	} else {
44
45
		give_run_install();
46
47 2
	}
48
49
}
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
	//Fresh Install? Setup Test Mode, Base Country (US), Test Gateway, Currency.
77 2
	if ( empty( $current_version ) ) {
78
		$options = array_merge( $options, give_get_default_settings() );
79
	}
80 2
81
	// Populate the default values.
82 2
	update_option( 'give_settings', array_merge( $give_options, $options ) );
83 2
84 2
	/**
85 2
	 * Run plugin upgrades.
86 2
	 *
87
	 * @since 1.8
88 2
	 */
89 2
	do_action( 'give_upgrades' );
90
91
	if ( GIVE_VERSION !== get_option( 'give_version' ) ) {
92 2
		update_option( 'give_version', GIVE_VERSION );
93 2
	}
94
95
	// Create Give roles.
96 2
	$roles = new Give_Roles();
97
	$roles->add_roles();
98
	$roles->add_caps();
99 2
100
	$api = new Give_API();
101 2
	update_option( 'give_default_api_version', 'v' . $api->get_version() );
102 2
103 2
	// Check for PHP Session support, and enable if available.
104 2
	$give_sessions = new Give_Session();
105 2
	$give_sessions->use_php_sessions();
106
107 2
	// Add a temporary option to note that Give pages have been created.
108 2
	Give_Cache::set( '_give_installed', $options, 30, true );
109
110 2
	if ( ! $current_version ) {
111 2
112
		require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
113
114 2
		// When new upgrade routines are added, mark them as complete on fresh install.
115
		$upgrade_routines = array(
116 2
			'upgrade_give_user_caps_cleanup',
117
			'upgrade_give_payment_customer_id',
118 2
			'upgrade_give_offline_status',
119 2
			'v18_upgrades_core_setting',
120 2
			'v18_upgrades_form_metadata',
121 2
			'v189_upgrades_levels_post_meta'
122 2
		);
123
124 2
		foreach ( $upgrade_routines as $upgrade ) {
125 2
			give_set_upgrade_complete( $upgrade );
126
		}
127 2
	}
128 2
129
	// Bail if activating from network, or bulk.
130
	if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
131 2
		return;
132
	}
133
134
	// Add the transient to redirect.
135
	Give_Cache::set( '_give_activation_redirect', true, 30, true );
136
137
	// Set 'Donation Form' meta box enabled by default.
138
	give_nav_donation_metabox_enabled();
139
}
140
141
/**
142
 * Network Activated New Site Setup.
143
 *
144
 * 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.
145
 *
146
 * @since      1.3.5
147
 *
148 2
 * @param  int $blog_id The Blog ID created.
149 2
 * @param  int $user_id The User ID set as the admin.
150
 * @param  string $domain The URL.
151
 * @param  string $path Site Path.
152 2
 * @param  int $site_id The Site ID.
153 2
 * @param  array $meta Blog Meta.
154 2
 */
155
function give_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...
156
157 2
	if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
158 2
159 2
		switch_to_blog( $blog_id );
160
		give_install();
161 2
		restore_current_blog();
162 2
163
	}
164
165 2
}
166
167
add_action( 'wpmu_new_blog', 'give_on_create_blog', 10, 6 );
168 2
169
170
/**
171 2
 * Drop Give's custom tables when a mu site is deleted.
172
 *
173 2
 * @since  1.4.3
174
 *
175
 * @param  array $tables The tables to drop.
176
 * @param  int $blog_id The Blog ID being deleted.
177
 *
178
 * @return array          The tables to drop.
179
 */
180
function give_wpmu_drop_tables( $tables, $blog_id ) {
181
182
	switch_to_blog( $blog_id );
183
	$donors_db     = new Give_DB_Donors();
184
	$donor_meta_db = new Give_DB_Donor_Meta();
185
186
	if ( $donors_db->installed() ) {
187
		$tables[] = $donors_db->table_name;
188
		$tables[] = $donor_meta_db->table_name;
189
	}
190 2
	restore_current_blog();
191 1
192
	return $tables;
193
194
}
195 1
196
add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );
197 1
198
/**
199
 * Post-installation
200
 *
201
 * Runs just after plugin installation and exposes the give_after_install hook.
202
 *
203
 * @since 1.0
204
 * @return void
205
 */
206
function give_after_install() {
207
208
	if ( ! is_admin() ) {
209
		return;
210
	}
211
212
	$give_options     = Give_Cache::get( '_give_installed', true );
213
	$give_table_check = get_option( '_give_table_check', false );
214
215
	if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {
216
217
		if ( ! @Give()->donor_meta->installed() ) {
218
219
			// Create the donor meta database.
220
			// (this ensures it creates it on multisite instances where it is network activated).
221
			@Give()->donor_meta->create_table();
222
223
		}
224
225
		if ( ! @Give()->donors->installed() ) {
226
			// Create the donor database.
227
			// (this ensures it creates it on multisite instances where it is network activated).
228
			@Give()->donors->create_table();
229
230
			/**
231
			 * Fires after plugin installation.
232
			 *
233
			 * @since 1.0
234
			 *
235
			 * @param array $give_options Give plugin options.
236
			 */
237
			do_action( 'give_after_install', $give_options );
238
		}
239
240
		update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ) );
241
242
	}
243
244
	// Delete the transient
245
	if ( false !== $give_options ) {
246
		Give_Cache::delete( Give_Cache::get_key( '_give_installed' ) );
247
	}
248
249
250
}
251
252
add_action( 'admin_init', 'give_after_install' );
253
254
255
/**
256
 * Install user roles on sub-sites of a network
257
 *
258
 * Roles do not get created when Give is network activation so we need to create them during admin_init
259
 *
260
 * @since 1.0
261
 * @return void
262
 */
263
function give_install_roles_on_network() {
264
265 3
	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...
266 1
267
	if ( ! is_object( $wp_roles ) ) {
268
		return;
269 2
	}
270 2
271
	if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {
272 2
273
		// Create Give plugin roles
274 2
		$roles = new Give_Roles();
275
		$roles->add_roles();
276
		$roles->add_caps();
277
278
	}
279
280
}
281 2
282
add_action( 'admin_init', 'give_install_roles_on_network' );
283 2
284
/**
285
 * Default core setting values.
286 2
 *
287 1
 * @since 1.8
288 1
 * @return array
289
 */
290
function give_get_default_settings() {
291 2
292
	$options = array(
293
		// General.
294
		'base_country'                                => 'US',
295
		'test_mode'                                   => 'enabled',
296
		'currency'                                    => 'USD',
297
		'currency_position'                           => 'before',
298
		'session_lifetime'                            => '604800',
299
		'email_access'                                => 'disabled',
300
		'number_decimals'                             => 2,
301
302
		// Display options.
303
		'css'                                         => 'enabled',
304
		'floatlabels'                                 => 'disabled',
305
		'welcome'                                     => 'enabled',
306 2
		'forms_singular'                              => 'enabled',
307
		'forms_archives'                              => 'enabled',
308 2
		'forms_excerpt'                               => 'enabled',
309 1
		'form_featured_img'                           => 'enabled',
310
		'form_sidebar'                                => 'enabled',
311
		'categories'                                  => 'disabled',
312 1
		'tags'                                        => 'disabled',
313
		'terms'                                       => 'disabled',
314
		'admin_notices'                               => 'enabled',
315 1
		'uninstall_on_delete'                         => 'disabled',
316 1
		'the_content_filter'                          => 'enabled',
317 1
		'scripts_footer'                              => 'disabled',
318
		'agree_to_terms_label'                        => __( 'Agree to Terms?', 'give' ),
319 1
		'agreement_text'                              => give_get_default_agreement_text(),
320
321 1
		// Paypal IPN verification.
322
		'paypal_verification'                         => 'enabled',
323
324
		// Default is manual gateway.
325
		'gateways'                                    => array( 'manual' => 1, 'offline' => 1 ),
326
		'default_gateway'                             => 'manual',
327
328
		// Offline gateway setup.
329
		'global_offline_donation_content'             => give_get_default_offline_donation_content(),
330
		'global_offline_donation_email'               => give_get_default_offline_donation_content(),
331
332
		// Billing address.
333
		'give_offline_donation_enable_billing_fields' => 'disabled',
334
335
		// Default donation notification email.
336
		'donation_notification'                       => give_get_default_donation_notification_email(),
337
338
		// Default email receipt message.
339
		'donation_receipt'                            => give_get_default_donation_receipt_email(),
340
	);
341
342
	return $options;
343
}
344
345
/**
346
 * Default terms and conditions.
347
 */
348
function give_get_default_agreement_text() {
349
350
	$org_name = get_bloginfo( 'name' );
351
352
	$agreement = sprintf(
353
		'<p>Acceptance of any contribution, gift or grant is at the discretion of the %1$s. The  %1$s will not accept any gift unless it can be used or expended consistently with the purpose and mission of the  %1$s.</p>
354
				<p>No irrevocable gift, whether outright or life-income in character, will be accepted if under any reasonable set of circumstances the gift would jeopardize the donor’s financial security.</p>
355
				<p>The %1$s will refrain from providing advice about the tax or other treatment of gifts and will encourage donors to seek guidance from their own professional advisers to assist them in the process of making their donation.</p>
356
				<p>The %1$s will accept donations of cash or publicly traded securities. Gifts of in-kind services will be accepted at the discretion of the %1$s.</p>
357
				<p>Certain other gifts, real property, personal property, in-kind gifts, non-liquid securities, and contributions whose sources are not transparent or whose use is restricted in some manner, must be reviewed prior to acceptance due to the special obligations raised or liabilities they may pose for %1$s.</p>
358
				<p>The %1$s will provide acknowledgments to donors meeting tax requirements for property received by the charity as a gift. However, except for gifts of cash and publicly traded securities, no value shall be ascribed to any receipt or other form of substantiation of a gift received by %1$s.</p>
359
				<p>The %1$s will respect the intent of the donor relating to gifts for restricted purposes and those relating to the desire to remain anonymous. With respect to anonymous gifts, the %1$s will restrict information about the donor to only those staff members with a need to know.</p>
360
				<p>The %1$s will not compensate, whether through commissions, finders\' fees, or other means, any third party for directing a gift or a donor to the %1$s.</p>',
361
		$org_name
362
	);
363
364
	return apply_filters( 'give_get_default_agreement_text', $agreement, $org_name );
365
}
366
367
368
/**
369
 * This function will install give related page which is not created already.
370
 *
371
 * @since 1.8.11
372
 */
373
function give_create_pages(){
374
375
	// Bailout if pages already created.
376
	if( get_option( 'give_install_pages_created') ) {
377
		return false;
378
	}
379
380
	$options = array();
381
382
	// Checks if the Success Page option exists AND that the page exists.
383
	if ( ! get_post( give_get_option( 'success_page' ) ) ) {
384
385
		// Donation Confirmation (Success) Page
386
		$success = wp_insert_post(
387
			array(
388
				'post_title'     => esc_html__( 'Donation Confirmation', 'give' ),
389
				'post_content'   => '[give_receipt]',
390
				'post_status'    => 'publish',
391
				'post_author'    => 1,
392
				'post_type'      => 'page',
393
				'comment_status' => 'closed'
394
			)
395
		);
396
397
		// Store our page IDs
398
		$options['success_page'] = $success;
399
	}
400
401
	// Checks if the Failure Page option exists AND that the page exists.
402
	if ( ! get_post( give_get_option( 'failure_page' ) ) ) {
403
404
		// Failed Donation Page
405
		$failed = wp_insert_post(
406
			array(
407
				'post_title'     => esc_html__( 'Donation Failed', 'give' ),
408
				'post_content'   => esc_html__( 'We\'re sorry, your donation failed to process. Please try again or contact site support.', 'give' ),
409
				'post_status'    => 'publish',
410
				'post_author'    => 1,
411
				'post_type'      => 'page',
412
				'comment_status' => 'closed'
413
			)
414
		);
415
416
		$options['failure_page'] = $failed;
417
	}
418
419
	// Checks if the History Page option exists AND that the page exists.
420
	if ( ! get_post( give_get_option( 'history_page' ) ) ) {
421
		// Donation History Page
422
		$history = wp_insert_post(
423
			array(
424
				'post_title'     => esc_html__( 'Donation History', 'give' ),
425
				'post_content'   => '[donation_history]',
426
				'post_status'    => 'publish',
427
				'post_author'    => 1,
428
				'post_type'      => 'page',
429
				'comment_status' => 'closed'
430
			)
431
		);
432
433
		$options['history_page'] = $history;
434
	}
435
436
	if( ! empty( $options ) ) {
437
		update_option( 'give_settings', array_merge( give_get_settings(), $options ) );
438
	}
439
440
	add_option( 'give_install_pages_created', 1, '', 'no' );
441
}
442
add_action( 'admin_init', 'give_create_pages', -1 );
443