Completed
Pull Request — master (#613)
by Devin
47:40 queued 29:58
created

install.php ➔ give_wpmu_drop_tables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
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 27 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     http://opensource.org/licenses/gpl-2.0.php 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
 * @description: 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
 * @global $wpdb
24
 * @global $wp_version
25
 * @return void
26
 */
27
function give_install( $network_wide = false ) {
28
29 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...
30
31
	if ( is_multisite() && $network_wide ) {
32 2
33
		foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) {
34
35 2
			switch_to_blog( $blog_id );
36
			give_run_install();
37
			restore_current_blog();
38 2
39 2
		}
40 2
41 2
	} else {
42
43
		give_run_install();
44 2
45
	}
46
47 2
}
48
49
register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );
50 2
51
/**
52 2
 * Run the Give Install process
53 2
 *
54 2
 * @since  1.5
55 2
 * @return void
56 2
 */
57
function give_run_install() {
58 2
59 2
	global $give_options;
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...
60
61
	// Setup the Give Custom Post Types
62 2
	give_setup_post_types();
63 2
64
	// Clear the permalinks
65
	flush_rewrite_rules( false );
66 2
67
	// Add Upgraded From Option
68
	$current_version = get_option( 'give_version' );
69 2
	if ( $current_version ) {
70
		update_option( 'give_version_upgraded_from', $current_version );
71 2
	}
72 2
73 2
	// Setup some default options
74 2
	$options = array();
75 2
76
	// Checks if the Success Page option exists AND that the page exists
77 2
	if ( ! get_post( give_get_option( 'success_page' ) ) ) {
78 2
79
		// Purchase Confirmation (Success) Page
80 2
		$success = wp_insert_post(
81 2
			array(
82
				'post_title'     => __( 'Donation Confirmation', 'give' ),
83
				'post_content'   => __( '[give_receipt]', 'give' ),
84 2
				'post_status'    => 'publish',
85
				'post_author'    => 1,
86 2
				'post_type'      => 'page',
87
				'comment_status' => 'closed'
88 2
			)
89 2
		);
90 2
91 2
		// Store our page IDs
92 2
		$options['success_page'] = $success;
93
	}
94 2
95 2
	// Checks if the Failure Page option exists AND that the page exists
96
	if ( ! get_post( give_get_option( 'failure_page' ) ) ) {
97 2
98 2
		// Failed Purchase Page
99
		$failed = wp_insert_post(
100
			array(
101 2
				'post_title'     => __( 'Transaction Failed', 'give' ),
102
				'post_content'   => __( 'We\'re sorry, your transaction 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
		// Purchase History (History) Page
116
		$history = wp_insert_post(
117
			array(
118 2
				'post_title'     => __( 'Donation History', 'give' ),
119 2
				'post_content'   => '[donation_history]',
120
				'post_status'    => 'publish',
121
				'post_author'    => 1,
122 2
				'post_type'      => 'page',
123 2
				'comment_status' => 'closed'
124 2
			)
125
		);
126
127 2
		$options['history_page'] = $history;
128 2
	}
129 2
130
	//Fresh Install? Setup Test Mode, Base Country (US), Test Gateway, Currency
131 2
	if ( empty( $current_version ) ) {
132 2
		$options['base_country']       = 'US';
133
		$options['test_mode']          = 1;
134
		$options['currency']           = 'USD';
135 2
		$options['session_lifetime']   = '604800';
136
		$options['gateways']['manual'] = 1;
137
		$options['default_gateway']    = 'manual'; //default is manual
138 2
139
		//Offline Gateway Setup
140
		$options['gateways']['offline']             = 1;
141 2
		$options['global_offline_donation_content'] = give_get_default_offline_donation_content();
142
143
		//Emails
144 2
		$options['donation_notification'] = give_get_default_donation_notification_email();
145
	}
146
147
	// Populate some default values
148
	update_option( 'give_settings', array_merge( $give_options, $options ) );
149
	update_option( 'give_version', GIVE_VERSION );
150
151
	//Update Version Number
152
	if ( $current_version ) {
153
		update_option( 'give_version_upgraded_from', $current_version );
154
	}
155
156
	// Create Give roles
157
	$roles = new Give_Roles();
158 2
	$roles->add_roles();
159 1
	$roles->add_caps();
160
161
	$api = new Give_API();
162 1
	update_option( 'give_default_api_version', 'v' . $api->get_version() );
163
164 1
	// Create the customers database
165
	@Give()->customers->create_table();
166
167
	// Check for PHP Session support, and enable if available
168
	Give()->session->use_php_sessions();
169
170
	// Add a temporary option to note that Give pages have been created
171
	set_transient( '_give_installed', $options, 30 );
172
	
173
	if ( ! $current_version ) {
174
175
		require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
176
177
		// When new upgrade routines are added, mark them as complete on fresh install
178 3
		$upgrade_routines = array(
179 1
			'upgrade_give_payment_customer_id',
180
			'upgrade_give_offline_status'
181
		);
182 2
183
		foreach ( $upgrade_routines as $upgrade ) {
184
			give_set_upgrade_complete( $upgrade );
185 2
		}
186 1
	}
187
188
	// Bail if activating from network, or bulk
189
	if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
190 1
		return;
191
	}
192
193 1
	// Add the transient to redirect
194
	set_transient( '_give_activation_redirect', true, 30 );
195 1
196
}
197 1
198
register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );
199
200
/**
201
 * Network Activated New Site Setup
202
 *
203
 * @description: 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.
204
 *
205
 * @since      1.3.5
206
 *
207
 * @param  int    $blog_id The Blog ID created
208
 * @param  int    $user_id The User ID set as the admin
209
 * @param  string $domain  The URL
210
 * @param  string $path    Site Path
211
 * @param  int    $site_id The Site ID
212 2
 * @param  array  $meta    Blog Meta
213
 */
214 2
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...
215 1
216
	if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
217
218 1
		switch_to_blog( $blog_id );
219
		give_install();
220
		restore_current_blog();
221 1
222 1
	}
223 1
224
}
225 1
226
add_action( 'wpmu_new_blog', 'on_create_blog', 10, 6 );
227 1
228
229
/**
230
 * Drop Give's custom tables when a mu site is deleted
231
 *
232
 * @since  2.5
233
 *
234
 * @param  array $tables The tables to drop
235
 * @param  int $blog_id The Blog ID being deleted
236
 *
237
 * @return array          The tables to drop
238
 */
239
function give_wpmu_drop_tables( $tables, $blog_id ) {
240
241
	switch_to_blog( $blog_id );
242
	$customers_db = new Give_DB_Customers();
243
	if ( $customers_db->installed() ) {
244
		$tables[] = $customers_db->table_name;
245
	}
246
	restore_current_blog();
247
248
	return $tables;
249
250
}
251
252
add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );
253
254
/**
255
 * Post-installation
256
 *
257
 * Runs just after plugin installation and exposes the give_after_install hook.
258
 *
259
 * @since 1.0
260
 * @return void
261
 */
262
function give_after_install() {
263
264
	if ( ! is_admin() ) {
265
		return;
266
	}
267
268
	$give_options     = get_transient( '_give_installed' );
269
	$give_table_check = get_option( '_give_table_check', false );
270
271
	if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {
272
273
		if ( ! @Give()->customers->installed() ) {
274
			// Create the customers database (this ensures it creates it on multisite instances where it is network activated)
275
			@Give()->customers->create_table();
276
277
			do_action( 'give_after_install', $give_options );
278
		}
279
280
		update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ) );
281
282
	}
283
284
	// Delete the transient
285
	if ( false !== $give_options ) {
286
		delete_transient( '_give_installed' );
287
	}
288
289
290
}
291
292
add_action( 'admin_init', 'give_after_install' );
293
294
295
/**
296
 * Install user roles on sub-sites of a network
297
 *
298
 * Roles do not get created when Give is network activation so we need to create them during admin_init
299
 *
300
 * @since 1.0
301
 * @return void
302
 */
303
function give_install_roles_on_network() {
304
305
	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...
306
307
	if ( ! is_object( $wp_roles ) ) {
308
		return;
309
	}
310
311
	if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {
312
313
		// Create Give plugin roles
314
		$roles = new Give_Roles;
315
		$roles->add_roles();
316
		$roles->add_caps();
317
318
	}
319
320
}
321
322
add_action( 'admin_init', 'give_install_roles_on_network' );