| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Install class. |
| 4 |
* |
| 5 |
* The responsibility of this class is to manage the events that need to happen |
| 6 |
* when the plugin is activated. |
| 7 |
* |
| 8 |
* @package Charitable/Class/Charitable Install |
| 9 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 1.0.0 |
| 12 |
* @version 1.8.1 added activity tables. |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! class_exists( 'Charitable_Install' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* Charitable_Install |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
class Charitable_Install { |
| 27 |
|
| 28 |
/** |
| 29 |
* Includes directory path. |
| 30 |
* |
| 31 |
* @since 1.6.42 |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $includes_path; |
| 36 |
|
| 37 |
/** |
| 38 |
* Install the plugin. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* |
| 42 |
* @param string $includes_path Path to the includes directory. |
| 43 |
*/ |
| 44 |
public function __construct( $includes_path ) { |
| 45 |
$this->includes_path = $includes_path; |
| 46 |
|
| 47 |
$this->setup_roles(); |
| 48 |
$this->create_tables(); |
| 49 |
$this->setup_upgrade_log(); |
| 50 |
$this->setup_initial_settings(); |
| 51 |
|
| 52 |
set_transient( 'charitable_install', 1, 0 ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Finish the plugin installation. |
| 57 |
* |
| 58 |
* @since 1.3.4 |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public static function finish_installing() { |
| 63 |
Charitable_Cron::schedule_events(); |
| 64 |
|
| 65 |
add_action( 'init', 'flush_rewrite_rules' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Create wp roles and assign capabilities |
| 70 |
* |
| 71 |
* @since 1.0.0 |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
protected function setup_roles() { |
| 76 |
require_once( $this->includes_path . '/users/class-charitable-roles.php' ); //phpcs:ignore |
| 77 |
$roles = new Charitable_Roles(); |
| 78 |
$roles->add_roles(); |
| 79 |
$roles->add_caps(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Create database tables. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* @version 1.8.1 |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
protected function create_tables() { |
| 91 |
require_once( $this->includes_path . 'abstracts/abstract-class-charitable-db.php' ); //phpcs:ignore |
| 92 |
|
| 93 |
$tables = array( |
| 94 |
$this->includes_path . 'data/class-charitable-donors-db.php' => 'Charitable_Donors_DB', |
| 95 |
$this->includes_path . 'data/class-charitable-donormeta-db.php' => 'Charitable_Donormeta_DB', |
| 96 |
$this->includes_path . 'data/class-charitable-campaign-donations-db.php' => 'Charitable_Campaign_Donations_DB', |
| 97 |
$this->includes_path . 'data/class-charitable-donation-activities-db.php' => 'Charitable_Donation_Activities_DB', |
| 98 |
$this->includes_path . 'data/class-charitable-campaign-activities-db.php' => 'Charitable_Campaign_Activities_DB', |
| 99 |
); |
| 100 |
|
| 101 |
foreach ( $tables as $file => $class ) { |
| 102 |
require_once( $file ); //phpcs:ignore |
| 103 |
$table = new $class(); |
| 104 |
$table->create_table(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Set up the upgrade log. |
| 110 |
* |
| 111 |
* @since 1.3.0 |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
protected function setup_upgrade_log() { |
| 116 |
require_once( $this->includes_path . '/admin/upgrades/class-charitable-upgrade.php' ); //phpcs:ignore |
| 117 |
Charitable_Upgrade::get_instance()->populate_upgrade_log_on_install(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Set up initial settings for install. |
| 122 |
* |
| 123 |
* @since 1.8.3.1 |
| 124 |
* @version 1.8.3.7 - Added general default settings. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
protected function setup_initial_settings() { |
| 129 |
|
| 130 |
$activated = (array) get_option( 'charitable_activated', array() ); |
| 131 |
|
| 132 |
// If no activation date is set, assume it's a new install. |
| 133 |
if ( false === $activated || empty( $activated ) ) { |
| 134 |
|
| 135 |
// Default general settings. |
| 136 |
$this->set_initial_charitable_option( 'donation_form_display', 'modal' ); |
| 137 |
$this->set_initial_charitable_option( 'country', 'US' ); |
| 138 |
$this->set_initial_charitable_option( 'currency', 'USD' ); |
| 139 |
$this->set_initial_charitable_option( 'currency_format', 'left' ); |
| 140 |
$this->set_initial_charitable_option( 'decimal_separator', '.' ); |
| 141 |
$this->set_initial_charitable_option( 'thousands_separator', ',' ); |
| 142 |
$this->set_initial_charitable_option( 'decimal_count', '2' ); |
| 143 |
|
| 144 |
// Default gateway settings. |
| 145 |
$this->set_initial_charitable_option( 'test_mode', 1 ); |
| 146 |
$this->set_initial_charitable_option( |
| 147 |
'active_gateways', |
| 148 |
array( |
| 149 |
0 => 'stripe', |
| 150 |
'stripe' => 'Charitable_Gateway_Stripe_AM', |
| 151 |
) |
| 152 |
); |
| 153 |
$this->set_initial_charitable_option( 'default_gateway', 'stripe' ); |
| 154 |
|
| 155 |
// Default advanced settings. |
| 156 |
$this->set_initial_charitable_option( 'disable_campaign_legacy_mode', 1 ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Set an initial Charitable option. |
| 162 |
* |
| 163 |
* @since 1.8.3.1 |
| 164 |
* |
| 165 |
* @param string $setting The setting key. |
| 166 |
* @param mixed $value The setting value. |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
private function set_initial_charitable_option( $setting = '', $value = false ) { |
| 170 |
$settings = get_option( 'charitable_settings', [] ); |
| 171 |
|
| 172 |
// if the setting somehow already exists, don't overwrite it. |
| 173 |
if ( '' === $setting || isset( $settings[ $setting ] ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$settings[ $setting ] = $value; |
| 178 |
|
| 179 |
update_option( 'charitable_settings', $settings ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
endif; |
| 184 |
|