Skip to content

Commit

Permalink
Merge branch 'master' into issue/504
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin Walker committed May 6, 2016
2 parents e393262 + 40575c8 commit 6db146c
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 49 deletions.
9 changes: 5 additions & 4 deletions give.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class Give {
/**
* Give Customers DB Object
*
* @var Give_Customer object
* @var object|Give_DB_Customers object
* @since 1.0
*/
public $customers;
Expand Down Expand Up @@ -400,16 +400,17 @@ public function load_textdomain() {


/**
* The main function responsible for returning the one true Give
* Instance to functions everywhere.
* Start Give
*
* The main function responsible for returning the one true Give instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $give = Give(); ?>
*
* @since 1.0
* @return object - The one true Give Instance
* @return object|Give
*/
function Give() {
return Give::instance();
Expand Down
14 changes: 11 additions & 3 deletions includes/class-give-db-customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,7 @@ public function count( $args = array() ) {
* @since 1.0
*/
public function create_table() {

global $wpdb;


require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

$sql = "CREATE TABLE " . $this->table_name . " (
Expand All @@ -594,4 +592,14 @@ public function create_table() {

update_option( $this->table_name . '_db_version', $this->version );
}

/**
* Check if the Customers table was ever installed
*
* @since 1.4.3
* @return bool Returns if the customers table was installed and upgrade routine run
*/
public function installed() {
return $this->table_exists( $this->table_name );
}
}
1 change: 1 addition & 0 deletions includes/class-give-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
Expand Down
147 changes: 108 additions & 39 deletions includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,37 @@
* @global $wp_version
* @return void
*/
function give_install() {
function give_install( $network_wide = false ) {

global $wpdb;

if ( is_multisite() && $network_wide ) {

foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs LIMIT 100" ) as $blog_id ) {

switch_to_blog( $blog_id );
give_run_install();
restore_current_blog();

}

} else {

give_run_install();

}

}

register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );

/**
* Run the Give Install process
*
* @since 1.5
* @return void
*/
function give_run_install() {

global $give_options;

Expand Down Expand Up @@ -139,9 +169,9 @@ function give_install() {

// Add a temporary option to note that Give pages have been created
set_transient( '_give_installed', $options, 30 );



if ( ! $current_version ) {

require_once GIVE_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';

// When new upgrade routines are added, mark them as complete on fresh install
Expand All @@ -155,17 +185,73 @@ function give_install() {
give_set_upgrade_complete( $upgrade );
}
}

// Bail if activating from network, or bulk
if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
return;
}

// Add the transient to redirect
set_transient( '_give_activation_redirect', true, 30 );

}

register_activation_hook( GIVE_PLUGIN_FILE, 'give_install' );

/**
* Network Activated New Site Setup
*
* @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.
*
* @since 1.3.5
*
* @param int $blog_id The Blog ID created
* @param int $user_id The User ID set as the admin
* @param string $domain The URL
* @param string $path Site Path
* @param int $site_id The Site ID
* @param array $meta Blog Meta
*/
function on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {

if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {

switch_to_blog( $blog_id );
give_install();
restore_current_blog();

}

}

add_action( 'wpmu_new_blog', 'on_create_blog', 10, 6 );


/**
* Drop Give's custom tables when a mu site is deleted
*
* @since 1.4.3
*
* @param array $tables The tables to drop
* @param int $blog_id The Blog ID being deleted
*
* @return array The tables to drop
*/
function give_wpmu_drop_tables( $tables, $blog_id ) {

switch_to_blog( $blog_id );
$customers_db = new Give_DB_Customers();
if ( $customers_db->installed() ) {
$tables[] = $customers_db->table_name;
}
restore_current_blog();

return $tables;

}

add_filter( 'wpmu_drop_tables', 'give_wpmu_drop_tables', 10, 2 );

/**
* Post-installation
*
Expand All @@ -180,20 +266,27 @@ function give_after_install() {
return;
}

$give_options = get_transient( '_give_installed' );
$give_options = get_transient( '_give_installed' );
$give_table_check = get_option( '_give_table_check', false );

// Exit if not in admin or the transient doesn't exist
if ( false === $give_options ) {
return;
}
if ( false === $give_table_check || current_time( 'timestamp' ) > $give_table_check ) {

// Create the donors database (this ensures it creates it on multisite instances where it is network activated)
@Give()->customers->create_table();
if ( ! @Give()->customers->installed() ) {
// Create the customers database (this ensures it creates it on multisite instances where it is network activated)
@Give()->customers->create_table();

do_action( 'give_after_install', $give_options );
}

update_option( '_give_table_check', ( current_time( 'timestamp' ) + WEEK_IN_SECONDS ) );

}

// Delete the transient
delete_transient( '_give_installed' );
if ( false !== $give_options ) {
delete_transient( '_give_installed' );
}

do_action( 'give_after_install', $give_options );

}

Expand All @@ -218,37 +311,13 @@ function give_install_roles_on_network() {

if ( ! array_key_exists( 'give_manager', $wp_roles->roles ) ) {

// Create Give shop roles
$roles = new Give_Roles;
// Create Give plugin roles
$roles = new Give_Roles();
$roles->add_roles();
$roles->add_caps();

}

}

add_action( 'admin_init', 'give_install_roles_on_network' );

/**
* Network Activated New Site Setup
*
* @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.
*
* @since 1.3.5
*
* @param $blog_id
* @param $user_id
* @param $domain
* @param $path
* @param $site_id
* @param $meta
*/
function on_create_blog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
if ( is_plugin_active_for_network( GIVE_PLUGIN_BASENAME ) ) {
switch_to_blog( $blog_id );
give_install();
restore_current_blog();
}
}

add_action( 'wpmu_new_blog', 'on_create_blog', 10, 6 );
add_action( 'admin_init', 'give_install_roles_on_network' );
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ We also really like WooCommerce. It's hands-down the most robust eCommerce platf
* Fix: Bug if the donation form's custom amount minimum is set to 0.00 the warning message would still display - https://github.com/WordImpress/Give/issues/604
* Fix: Bug with email access and the Donation History shortcode pagination not counting properly - https://github.com/WordImpress/Give/issues/600
* Fix: Fixed incorrect meta_key usage within is_single_price_mode() method which was returning false positives - https://github.com/WordImpress/Give/issues/602
* Update: Updated install process to account for WP multisite creation and deletion - https://github.com/WordImpress/Give/issues/609

= 1.4.2: April 26, 2016 =
* Fix: Bug with Custom Amount minimum and currencies with "," for decimal separator miscalculating the amounts - https://github.com/WordImpress/Give/issues/591
Expand Down
8 changes: 5 additions & 3 deletions tests/unit-tests/tests-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ public function setUp() {
*/
public function test_settings() {
global $give_options;
$this->assertArrayHasKey( 'history_page', $give_options );
$this->assertArrayHasKey( 'success_page', $give_options );
$this->assertArrayHasKey( 'failure_page', $give_options );
}

/**
* Test the install function, installing pages and setting option values.
*
* @since 2.2.4
* @since 1.3.3
*/
public function test_install() {

Expand All @@ -40,15 +41,16 @@ public function test_install() {
$origin_give_version = get_option( 'give_version' );

// Prepare values for testing
update_option( 'give_version', '2.1' );
delete_option( 'give_settings' );
update_option( 'give_version', '2.0' );
$give_options = array();


give_install();


// Test the give_version_upgraded_from value
$this->assertEquals( get_option( 'give_version_upgraded_from' ), '2.1' );
$this->assertEquals( get_option( 'give_version_upgraded_from' ), '2.0' );

// Test that new pages are created, and not the same as the already created ones.
// This is to make sure the test is giving the most accurate results.
Expand Down

0 comments on commit 6db146c

Please sign in to comment.