Completed
Pull Request — master (#859)
by Devin
19:40
created

install.php ➔ give_after_install()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 38
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.6024

Importance

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