Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

includes/install.php (26 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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;
32
33
	if ( is_multisite() && $network_wide ) {
34
35
		foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) {
0 ignored issues
show
Usage of a direct database call is discouraged.
Loading history...
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
36
37
			switch_to_blog( $blog_id );
0 ignored issues
show
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
38
			give_run_install();
39
			restore_current_blog();
40
41
		}
0 ignored issues
show
Blank line found after control structure
Loading history...
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
			'v1812_update_amount_values',
123
			'v1812_update_donor_purchase_values'
0 ignored issues
show
Comma required after last value in array declaration
Loading history...
124 2
		);
125 2
126
		foreach ( $upgrade_routines as $upgrade ) {
127 2
			give_set_upgrade_complete( $upgrade );
128 2
		}
129
	}
130
131 2
	// Bail if activating from network, or bulk.
132
	if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
133
		return;
134
	}
135
136
	// Add the transient to redirect.
137
	Give_Cache::set( '_give_activation_redirect', true, 30, true );
138
139
	// Set 'Donation Form' meta box enabled by default.
140
	give_nav_donation_metabox_enabled();
141
}
142
143
/**
144
 * Network Activated New Site Setup.
145
 *
146
 * 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.
147
 *
148 2
 * @since      1.3.5
149 2
 *
150
 * @param  int $blog_id The Blog ID created.
151
 * @param  int $user_id The User ID set as the admin.
152 2
 * @param  string $domain The URL.
153 2
 * @param  string $path Site Path.
154 2
 * @param  int $site_id The Site ID.
155
 * @param  array $meta Blog Meta.
156
 */
157 2
function give_on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
158 2
159 2
	if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
160
161 2
		switch_to_blog( $blog_id );
0 ignored issues
show
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
162 2
		give_install();
163
		restore_current_blog();
164
165 2
	}
166
167
}
168 2
169
add_action( 'wpmu_new_blog', 'give_on_create_blog', 10, 6 );
170
171 2
172
/**
173 2
 * Drop Give's custom tables when a mu site is deleted.
174
 *
175
 * @since  1.4.3
176
 *
177
 * @param  array $tables The tables to drop.
178
 * @param  int $blog_id The Blog ID being deleted.
179
 *
180
 * @return array          The tables to drop.
181
 */
182
function give_wpmu_drop_tables( $tables, $blog_id ) {
183
184
	switch_to_blog( $blog_id );
0 ignored issues
show
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
185
	$donors_db     = new Give_DB_Donors();
186
	$donor_meta_db = new Give_DB_Donor_Meta();
187
188
	if ( $donors_db->installed() ) {
189
		$tables[] = $donors_db->table_name;
190 2
		$tables[] = $donor_meta_db->table_name;
191 1
	}
192
	restore_current_blog();
193
194
	return $tables;
195 1
196
}
197 1
198
add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );
199
200
/**
201
 * Post-installation
202
 *
203
 * Runs just after plugin installation and exposes the give_after_install hook.
204
 *
205
 * @since 1.0
206
 * @return void
207
 */
208
function give_after_install() {
209
210
	if ( ! is_admin() ) {
211
		return;
212
	}
213
214
	$give_options     = Give_Cache::get( '_give_installed', true );
215
	$give_table_check = get_option( '_give_table_check', false );
216
217
	if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {
218
219
		if ( ! @Give()->donor_meta->installed() ) {
0 ignored issues
show
Silencing errors is discouraged
Loading history...
220
221
			// Create the donor meta database.
222
			// (this ensures it creates it on multisite instances where it is network activated).
223
			@Give()->donor_meta->create_table();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Silencing errors is discouraged
Loading history...
224
225
		}
226
227
		if ( ! @Give()->donors->installed() ) {
0 ignored issues
show
Silencing errors is discouraged
Loading history...
228
			// Create the donor database.
229
			// (this ensures it creates it on multisite instances where it is network activated).
230
			@Give()->donors->create_table();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Silencing errors is discouraged
Loading history...
231
232
			/**
233
			 * Fires after plugin installation.
234
			 *
235
			 * @since 1.0
236
			 *
237
			 * @param array $give_options Give plugin options.
238
			 */
239
			do_action( 'give_after_install', $give_options );
240
		}
241
242
		update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ) );
243
244
	}
245
246
	// Delete the transient
247
	if ( false !== $give_options ) {
248
		Give_Cache::delete( Give_Cache::get_key( '_give_installed' ) );
249
	}
250
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
251
252
}
253
254
add_action( 'admin_init', 'give_after_install' );
255
256
257
/**
258
 * Install user roles on sub-sites of a network
259
 *
260
 * Roles do not get created when Give is network activation so we need to create them during admin_init
261
 *
262
 * @since 1.0
263
 * @return void
264
 */
265 3
function give_install_roles_on_network() {
266 1
267
	global $wp_roles;
268
269 2
	if ( ! is_object( $wp_roles ) ) {
270 2
		return;
271
	}
272 2
273
	if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {
274 2
275
		// Create Give plugin roles
276
		$roles = new Give_Roles();
277
		$roles->add_roles();
278
		$roles->add_caps();
279
280
	}
281 2
282
}
283 2
284
add_action( 'admin_init', 'give_install_roles_on_network' );
285
286 2
/**
287 1
 * Default core setting values.
288 1
 *
289
 * @since 1.8
290
 * @return array
291 2
 */
292
function give_get_default_settings() {
293
294
	$options = array(
295
		// General.
296
		'base_country'                                => 'US',
297
		'test_mode'                                   => 'enabled',
298
		'currency'                                    => 'USD',
299
		'currency_position'                           => 'before',
300
		'session_lifetime'                            => '604800',
301
		'email_access'                                => 'disabled',
302
		'number_decimals'                             => 2,
303
304
		// Display options.
305
		'css'                                         => 'enabled',
306 2
		'floatlabels'                                 => 'disabled',
307
		'welcome'                                     => 'enabled',
308 2
		'forms_singular'                              => 'enabled',
309 1
		'forms_archives'                              => 'enabled',
310
		'forms_excerpt'                               => 'enabled',
311
		'form_featured_img'                           => 'enabled',
312 1
		'form_sidebar'                                => 'enabled',
313
		'categories'                                  => 'disabled',
314
		'tags'                                        => 'disabled',
315 1
		'terms'                                       => 'disabled',
316 1
		'admin_notices'                               => 'enabled',
317 1
		'uninstall_on_delete'                         => 'disabled',
318
		'the_content_filter'                          => 'enabled',
319 1
		'scripts_footer'                              => 'disabled',
320
		'agree_to_terms_label'                        => __( 'Agree to Terms?', 'give' ),
321 1
		'agreement_text'                              => give_get_default_agreement_text(),
322
323
		// Paypal IPN verification.
324
		'paypal_verification'                         => 'enabled',
325
326
		// Default is manual gateway.
327
		'gateways'                                    => array( 'manual' => 1, 'offline' => 1 ),
328
		'default_gateway'                             => 'manual',
329
330
		// Offline gateway setup.
331
		'global_offline_donation_content'             => give_get_default_offline_donation_content(),
332
		'global_offline_donation_email'               => give_get_default_offline_donation_content(),
333
334
		// Billing address.
335
		'give_offline_donation_enable_billing_fields' => 'disabled',
336
337
		// Default donation notification email.
338
		'donation_notification'                       => give_get_default_donation_notification_email(),
339
340
		// Default email receipt message.
341
		'donation_receipt'                            => give_get_default_donation_receipt_email(),
342
	);
343
344
	return $options;
345
}
346
347
/**
348
 * Default terms and conditions.
349
 */
350
function give_get_default_agreement_text() {
351
352
	$org_name = get_bloginfo( 'name' );
353
354
	$agreement = sprintf(
355
		'<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>
356
				<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>
357
				<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>
358
				<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>
359
				<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>
360
				<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>
361
				<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>
362
				<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>',
363
		$org_name
364
	);
365
366
	return apply_filters( 'give_get_default_agreement_text', $agreement, $org_name );
367
}
368
369
370
/**
371
 * This function will install give related page which is not created already.
372
 *
373
 * @since 1.8.11
374
 */
375
function give_create_pages(){
376
377
	// Bailout if pages already created.
378
	if( get_option( 'give_install_pages_created') ) {
0 ignored issues
show
Space after opening control structure is required
Loading history...
No space before opening parenthesis is prohibited
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
379
		return false;
380
	}
381
382
	$options = array();
383
384
	// Checks if the Success Page option exists AND that the page exists.
385 View Code Duplication
	if ( ! get_post( give_get_option( 'success_page' ) ) ) {
0 ignored issues
show
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...
386
387
		// Donation Confirmation (Success) Page
388
		$success = wp_insert_post(
389
			array(
390
				'post_title'     => esc_html__( 'Donation Confirmation', 'give' ),
391
				'post_content'   => '[give_receipt]',
392
				'post_status'    => 'publish',
393
				'post_author'    => 1,
394
				'post_type'      => 'page',
395
				'comment_status' => 'closed'
0 ignored issues
show
Each line in an array declaration must end in a comma
Loading history...
396
			)
397
		);
398
399
		// Store our page IDs
400
		$options['success_page'] = $success;
401
	}
402
403
	// Checks if the Failure Page option exists AND that the page exists.
404 View Code Duplication
	if ( ! get_post( give_get_option( 'failure_page' ) ) ) {
0 ignored issues
show
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...
405
406
		// Failed Donation Page
407
		$failed = wp_insert_post(
408
			array(
409
				'post_title'     => esc_html__( 'Donation Failed', 'give' ),
410
				'post_content'   => esc_html__( 'We\'re sorry, your donation failed to process. Please try again or contact site support.', 'give' ),
411
				'post_status'    => 'publish',
412
				'post_author'    => 1,
413
				'post_type'      => 'page',
414
				'comment_status' => 'closed'
0 ignored issues
show
Each line in an array declaration must end in a comma
Loading history...
415
			)
416
		);
417
418
		$options['failure_page'] = $failed;
419
	}
420
421
	// Checks if the History Page option exists AND that the page exists.
422 View Code Duplication
	if ( ! get_post( give_get_option( 'history_page' ) ) ) {
0 ignored issues
show
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...
423
		// Donation History Page
424
		$history = wp_insert_post(
425
			array(
426
				'post_title'     => esc_html__( 'Donation History', 'give' ),
427
				'post_content'   => '[donation_history]',
428
				'post_status'    => 'publish',
429
				'post_author'    => 1,
430
				'post_type'      => 'page',
431
				'comment_status' => 'closed'
0 ignored issues
show
Each line in an array declaration must end in a comma
Loading history...
432
			)
433
		);
434
435
		$options['history_page'] = $history;
436
	}
437
438
	if( ! empty( $options ) ) {
0 ignored issues
show
Space after opening control structure is required
Loading history...
No space before opening parenthesis is prohibited
Loading history...
439
		update_option( 'give_settings', array_merge( give_get_settings(), $options ) );
440
	}
441
442
	add_option( 'give_install_pages_created', 1, '', 'no' );
443
}
444
add_action( 'admin_init', 'give_create_pages', -1 );
445