install.php ➔ give_on_create_blog()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 6
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Install Function
4
 *
5
 * @package     Give
6
 * @subpackage  Functions/Install
7
 * @copyright   Copyright (c) 2016, GiveWP
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
21
 * 'donations' slug and also creates the plugin and populates the settings fields for those plugin pages. After
22
 * successful install, the user is redirected to the Give Welcome screen.
23
 *
24
 * @since 1.0
25
 *
26
 * @param bool $network_wide
27
 *
28
 * @global     $wpdb
29
 * @return void
30
 */
31
function give_install( $network_wide = false ) {
32
33
	global $wpdb;
34
35
	if ( is_multisite() && $network_wide ) {
36
37
		foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) {
38
39
			switch_to_blog( $blog_id );
40
			give_run_install();
41
			restore_current_blog();
42
43
		}
44
45
	} else {
46
47
		give_run_install();
48
49
	}
50
51
}
52
53
/**
54
 * Run the Give Install process.
55
 *
56
 * @since  1.5
57
 * @return void
58
 */
59
function give_run_install() {
60
	$give_options = give_get_settings();
61
62
	// Setup the Give Custom Post Types.
63
	give_setup_post_types();
64
65
	// Add Upgraded From Option.
66
	$current_version = get_option( 'give_version' );
67
	if ( $current_version ) {
68
		update_option( 'give_version_upgraded_from', $current_version, false );
69
	}
70
71
	// Setup some default options.
72
	$options = array();
73
74
	//Fresh Install? Setup Test Mode, Base Country (US), Test Gateway, Currency.
75
	if ( empty( $current_version ) ) {
76
		$options = array_merge( $options, give_get_default_settings() );
77
	}
78
79
	// Populate the default values.
80
	update_option( 'give_settings', array_merge( $give_options, $options ), false );
81
82
	/**
83
	 * Run plugin upgrades.
84
	 *
85
	 * @since 1.8
86
	 */
87
	do_action( 'give_upgrades' );
88
89
	if ( GIVE_VERSION !== get_option( 'give_version' ) ) {
90
		update_option( 'give_version', GIVE_VERSION, false );
91
	}
92
93
	// Create Give roles.
94
	$roles = new Give_Roles();
95
	$roles->add_roles();
96
	$roles->add_caps();
97
98
	// Set api version, end point and refresh permalink.
99
	$api = new Give_API();
100
	$api->add_endpoint();
101
	update_option( 'give_default_api_version', 'v' . $api->get_version(), false );
102
103
	flush_rewrite_rules();
104
105
	// Create databases.
106
	__give_register_tables();
107
108
	// Add a temporary option to note that Give pages have been created.
109
	Give_Cache::set( '_give_installed', $options, 30, true );
110
111
	if ( ! $current_version ) {
112
113
		require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
114
115
		// When new upgrade routines are added, mark them as complete on fresh install.
116
		$upgrade_routines = array(
117
			'upgrade_give_user_caps_cleanup',
118
			'upgrade_give_payment_customer_id',
119
			'upgrade_give_offline_status',
120
			'v18_upgrades_core_setting',
121
			'v18_upgrades_form_metadata',
122
			'v189_upgrades_levels_post_meta',
123
			'v1812_update_amount_values',
124
			'v1812_update_donor_purchase_values',
125
			'v1813_update_user_roles',
126
			'v1813_update_donor_user_roles',
127
			'v1817_update_donation_iranian_currency_code',
128
			'v1817_cleanup_user_roles',
129
			'v1818_assign_custom_amount_set_donation',
130
			'v1818_give_worker_role_cleanup',
131
			'v20_upgrades_form_metadata',
132
			'v20_logs_upgrades',
133
			'v20_move_metadata_into_new_table',
134
			'v20_rename_donor_tables',
135
			'v20_upgrades_donor_name',
136
			'v20_upgrades_user_address',
137
			'v20_upgrades_payment_metadata',
138
			'v201_upgrades_payment_metadata',
139
			'v201_add_missing_donors',
140
			'v201_move_metadata_into_new_table',
141
			'v201_logs_upgrades',
142
			'v210_verify_form_status_upgrades',
143
			'v213_delete_donation_meta',
144
			'v215_update_donor_user_roles',
145
			'v220_rename_donation_meta_type',
146
			'v224_update_donor_meta',
147
			'v224_update_donor_meta_forms_id',
148
			'v230_move_donor_note',
149
			'v230_move_donation_note',
150
			'v230_delete_donor_wall_related_donor_data',
151
			'v230_delete_donor_wall_related_comment_data',
152
			'v240_update_form_goal_progress',
153
			'v241_remove_sale_logs'
154
		);
155
156
		foreach ( $upgrade_routines as $upgrade ) {
157
			give_set_upgrade_complete( $upgrade );
158
		}
159
	}
160
161
	// Bail if activating from network, or bulk.
162
	if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
163
		return;
164
	}
165
166
	// Add the transient to redirect.
167
	Give_Cache::set( '_give_activation_redirect', true, 30, true );
168
}
169
170
/**
171
 * Network Activated New Site Setup.
172
 *
173
 * When a new site is created when Give is network activated this function runs the appropriate install function to set
174
 * up the site for Give.
175
 *
176
 * @since      1.3.5
177
 *
178
 * @param  int    $blog_id The Blog ID created.
179
 * @param  int    $user_id The User ID set as the admin.
180
 * @param  string $domain  The URL.
181
 * @param  string $path    Site Path.
182
 * @param  int    $site_id The Site ID.
183
 * @param  array  $meta    Blog Meta.
184
 */
185
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...
186
187
	if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
188
189
		switch_to_blog( $blog_id );
190
		give_install();
191
		restore_current_blog();
192
193
	}
194
195
}
196
197
add_action( 'wpmu_new_blog', 'give_on_create_blog', 10, 6 );
198
199
200
/**
201
 * Drop Give's custom tables when a mu site is deleted.
202
 *
203
 * @since  1.4.3
204
 *
205
 * @param  array $tables  The tables to drop.
206
 * @param  int   $blog_id The Blog ID being deleted.
207
 *
208
 * @return array          The tables to drop.
209
 */
210
function give_wpmu_drop_tables( $tables, $blog_id ) {
211
212
	switch_to_blog( $blog_id );
213
	$custom_tables = __give_get_tables();
214
215
	/* @var Give_DB $table */
216
	foreach ( $custom_tables as $table ) {
217
		if ( $table->installed() ) {
218
			$tables[] = $table->table_name;
219
		}
220
	}
221
222
	restore_current_blog();
223
224
	return $tables;
225
226
}
227
228
add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );
229
230
/**
231
 * Post-installation
232
 *
233
 * Runs just after plugin installation and exposes the give_after_install hook.
234
 *
235
 * @since 1.0
236
 * @return void
237
 */
238
function give_after_install() {
239
240
	if ( ! is_admin() ) {
241
		return;
242
	}
243
244
	$give_options     = Give_Cache::get( '_give_installed', true );
245
	$give_table_check = get_option( '_give_table_check', false );
246
247
	if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {
248
249
		if ( ! @Give()->donor_meta->installed() ) {
250
251
			// Create the donor meta database.
252
			// (this ensures it creates it on multisite instances where it is network activated).
253
			@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...
254
255
		}
256
257
		if ( ! @Give()->donors->installed() ) {
258
			// Create the donor database.
259
			// (this ensures it creates it on multisite instances where it is network activated).
260
			@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...
261
262
			/**
263
			 * Fires after plugin installation.
264
			 *
265
			 * @since 1.0
266
			 *
267
			 * @param array $give_options Give plugin options.
268
			 */
269
			do_action( 'give_after_install', $give_options );
270
		}
271
272
		update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ), false );
273
274
	}
275
276
	// Delete the transient
277
	if ( false !== $give_options ) {
278
		Give_Cache::delete( Give_Cache::get_key( '_give_installed' ) );
279
	}
280
281
282
}
283
284
add_action( 'admin_init', 'give_after_install' );
285
286
287
/**
288
 * Install user roles on sub-sites of a network
289
 *
290
 * Roles do not get created when Give is network activation so we need to create them during admin_init
291
 *
292
 * @since 1.0
293
 * @return void
294
 */
295
function give_install_roles_on_network() {
296
297
	global $wp_roles;
298
299
	if ( ! is_object( $wp_roles ) ) {
300
		return;
301
	}
302
303
	if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {
304
305
		// Create Give plugin roles
306
		$roles = new Give_Roles();
307
		$roles->add_roles();
308
		$roles->add_caps();
309
310
	}
311
312
}
313
314
add_action( 'admin_init', 'give_install_roles_on_network' );
315
316
/**
317
 * Default core setting values.
318
 *
319
 * @since 1.8
320
 * @return array
321
 */
322
function give_get_default_settings() {
323
324
	$options = array(
325
		// General.
326
		'base_country'                                => 'US',
327
		'test_mode'                                   => 'enabled',
328
		'currency'                                    => 'USD',
329
		'currency_position'                           => 'before',
330
		'session_lifetime'                            => '604800',
331
		'email_access'                                => 'enabled',
332
		'thousands_separator'                         => ',',
333
		'decimal_separator'                           => '.',
334
		'number_decimals'                             => 2,
335
		'sequential-ordering_status'                  => 'enabled',
336
337
		// Display options.
338
		'css'                                         => 'enabled',
339
		'floatlabels'                                 => 'disabled',
340
		'welcome'                                     => 'enabled',
341
		'company_field'                               => 'disabled',
342
		'name_title_prefix'                           => 'disabled',
343
		'forms_singular'                              => 'enabled',
344
		'forms_archives'                              => 'enabled',
345
		'forms_excerpt'                               => 'enabled',
346
		'form_featured_img'                           => 'enabled',
347
		'form_sidebar'                                => 'enabled',
348
		'categories'                                  => 'disabled',
349
		'tags'                                        => 'disabled',
350
		'terms'                                       => 'disabled',
351
		'admin_notices'                               => 'enabled',
352
		'cache'                                       => 'enabled',
353
		'uninstall_on_delete'                         => 'disabled',
354
		'the_content_filter'                          => 'enabled',
355
		'scripts_footer'                              => 'disabled',
356
		'agree_to_terms_label'                        => __( 'Agree to Terms?', 'give' ),
357
		'agreement_text'                              => give_get_default_agreement_text(),
358
		'babel_polyfill_script'                       => 'enabled',
359
360
		// Paypal IPN verification.
361
		'paypal_verification'                         => 'enabled',
362
363
		// Default is manual gateway.
364
		'gateways'                                    => array( 'manual' => 1, 'offline' => 1, 'stripe' => 1 ),
365
		'default_gateway'                             => 'manual',
366
367
		// Offline gateway setup.
368
		'global_offline_donation_content'             => give_get_default_offline_donation_content(),
369
		'global_offline_donation_email'               => give_get_default_offline_donation_content(),
370
371
		// Billing address.
372
		'give_offline_donation_enable_billing_fields' => 'disabled',
373
374
		// Default donation notification email.
375
		'donation_notification'                       => give_get_default_donation_notification_email(),
376
377
		// Default email receipt message.
378
		'donation_receipt'                            => give_get_default_donation_receipt_email(),
379
380
		'donor_default_user_role'                     => 'give_donor',
381
382
	);
383
384
	return $options;
385
}
386
387
/**
388
 * Default terms and conditions.
389
 */
390
function give_get_default_agreement_text() {
391
392
	$org_name = get_bloginfo( 'name' );
393
394
	$agreement = sprintf(
395
		'<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>
396
				<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>
397
				<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>
398
				<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>
399
				<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>
400
				<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>
401
				<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>
402
				<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>',
403
		$org_name
404
	);
405
406
	return apply_filters( 'give_get_default_agreement_text', $agreement, $org_name );
407
}
408
409
410
/**
411
 * This function will install give related page which is not created already.
412
 *
413
 * @since 1.8.11
414
 *
415
 * @return void
416
 */
417
function give_create_pages() {
418
419
	// Bailout if pages already created.
420
	if ( Give_Cache_Setting::get_option( 'give_install_pages_created' ) ) {
421
		return;
422
	}
423
424
	$options = array();
425
426
	// Checks if the Success Page option exists AND that the page exists.
427 View Code Duplication
	if ( ! get_post( give_get_option( 'success_page' ) ) ) {
0 ignored issues
show
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...
428
429
		// Donation Confirmation (Success) Page
430
		$success = wp_insert_post(
431
			array(
432
				'post_title'     => esc_html__( 'Donation Confirmation', 'give' ),
433
				'post_content'   => '[give_receipt]',
434
				'post_status'    => 'publish',
435
				'post_author'    => 1,
436
				'post_type'      => 'page',
437
				'comment_status' => 'closed'
438
			)
439
		);
440
441
		// Store our page IDs
442
		$options['success_page'] = $success;
443
	}
444
445
	// Checks if the Failure Page option exists AND that the page exists.
446 View Code Duplication
	if ( ! get_post( give_get_option( 'failure_page' ) ) ) {
0 ignored issues
show
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...
447
448
		// Failed Donation Page
449
		$failed = wp_insert_post(
450
			array(
451
				'post_title'     => esc_html__( 'Donation Failed', 'give' ),
452
				'post_content'   => esc_html__( 'We\'re sorry, your donation failed to process. Please try again or contact site support.', 'give' ),
453
				'post_status'    => 'publish',
454
				'post_author'    => 1,
455
				'post_type'      => 'page',
456
				'comment_status' => 'closed'
457
			)
458
		);
459
460
		$options['failure_page'] = $failed;
461
	}
462
463
	// Checks if the History Page option exists AND that the page exists.
464 View Code Duplication
	if ( ! get_post( give_get_option( 'history_page' ) ) ) {
0 ignored issues
show
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...
465
		// Donation History Page
466
		$history = wp_insert_post(
467
			array(
468
				'post_title'     => esc_html__( 'Donation History', 'give' ),
469
				'post_content'   => '[donation_history]',
470
				'post_status'    => 'publish',
471
				'post_author'    => 1,
472
				'post_type'      => 'page',
473
				'comment_status' => 'closed'
474
			)
475
		);
476
477
		$options['history_page'] = $history;
478
	}
479
480
	if ( ! empty( $options ) ) {
481
		update_option( 'give_settings', array_merge( give_get_settings(), $options ), false );
482
	}
483
484
	add_option( 'give_install_pages_created', 1, '', false );
485
}
486
487
// @TODO we can add this hook only when plugin activate instead of every admin page load.
488
// @see known issue https://github.com/impress-org/give/issues/1848
489
add_action( 'admin_init', 'give_create_pages', - 1 );
490
491
492
/**
493
 * Install tables on plugin update if missing
494
 * Note: only for internal use
495
 *
496
 * @since 2.4.1
497
 *
498
 * @param string $old_version
499
 */
500
function give_install_tables_on_plugin_update( $old_version ) {
501
	update_option( 'give_version_upgraded_from', $old_version, false );
502
	__give_register_tables();
503
}
504
505
add_action( 'update_option_give_version', 'give_install_tables_on_plugin_update', 0, 2 );
506
507
508
/**
509
 * Get array of table class objects
510
 *
511
 * Note: only for internal purpose use
512
 *
513
 * @sice 2.3.1
514
 *
515
 */
516
function __give_get_tables() {
517
	$tables = array(
518
		'donors_db'       => new Give_DB_Donors(),
519
		'donor_meta_db'   => new Give_DB_Donor_Meta(),
520
		'comment_db'      => new Give_DB_Comments(),
521
		'comment_db_meta' => new Give_DB_Comment_Meta(),
522
		'give_session'    => new Give_DB_Sessions(),
523
		'log_db'          => new Give_DB_Logs(),
524
		'logmeta_db'      => new Give_DB_Log_Meta(),
525
		'formmeta_db'     => new Give_DB_Form_Meta(),
526
		'sequential_db'   => new Give_DB_Sequential_Ordering(),
527
		'donation_meta'   => new Give_DB_Payment_Meta(),
528
	);
529
530
	return $tables;
531
}
532
533
/**
534
 * Register classes
535
 * Note: only for internal purpose use
536
 *
537
 * @sice 2.3.1
538
 *
539
 */
540
function __give_register_tables() {
541
	$tables = __give_get_tables();
542
543
	/* @var Give_DB $table */
544
	foreach ( $tables  as $table ) {
545
		if( ! $table->installed() ) {
546
			$table->register_table();
547
		}
548
	}
549
}
550