Completed
Push — issues/1132 ( b11cf8...b0ddd9 )
by Ravinder
19:03
created

install.php ➔ give_on_create_blog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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