| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Classes\Base; |
| 4 |
|
| 5 |
use Bookit\Classes\Admin\SettingsController; |
| 6 |
use Bookit\Classes\Vendor\DatabaseModel; |
| 7 |
use Bookit\Helpers\FreemiusHelper; |
| 8 |
use Bookit\Helpers\MailTemplateHelper; |
| 9 |
use BookitPayments\Classes\Admin\Base; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Plugin |
| 13 |
* base plugin functions here |
| 14 |
*/ |
| 15 |
class Plugin { |
| 16 |
|
| 17 |
public static $prefix = 'bookit-'; |
| 18 |
protected static $info = array(); |
| 19 |
protected static $wp_options = array( |
| 20 |
'bookit_version', |
| 21 |
'bookit_db_version', |
| 22 |
'bookit_import_file', |
| 23 |
'bookit_settings', |
| 24 |
); |
| 25 |
|
| 26 |
/** |
| 27 |
* Run plugin. |
| 28 |
*/ |
| 29 |
public static function run() { |
| 30 |
register_activation_hook( BOOKIT_FILE, array( __CLASS__, 'activate' ) ); |
| 31 |
register_deactivation_hook( BOOKIT_FILE, array( __CLASS__, 'deactivate' ) ); |
| 32 |
register_uninstall_hook( BOOKIT_FILE, array( __CLASS__, 'uninstall' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Plugin activation |
| 37 |
*/ |
| 38 |
public static function activate() { |
| 39 |
/** create tables */ |
| 40 |
\Bookit\Classes\Database\Appointments::create_table(); |
| 41 |
\Bookit\Classes\Database\Categories::create_table(); |
| 42 |
\Bookit\Classes\Database\Coupons::create_table(); |
| 43 |
\Bookit\Classes\Database\Customers::create_table(); |
| 44 |
\Bookit\Classes\Database\Discounts::create_table(); |
| 45 |
\Bookit\Classes\Database\Services::create_table(); |
| 46 |
\Bookit\Classes\Database\Staff::create_table(); |
| 47 |
\Bookit\Classes\Database\Staff_Services::create_table(); |
| 48 |
\Bookit\Classes\Database\Staff_Working_Hours::create_table(); |
| 49 |
\Bookit\Classes\Database\Payments::create_table(); |
| 50 |
|
| 51 |
/** settings */ |
| 52 |
SettingsController::save_default_settings(); |
| 53 |
|
| 54 |
/** add bookit roles and append bookit capabilites to wp admins */ |
| 55 |
User::addBookitUserRoles(); |
| 56 |
User::addBookitCapabilitiesToWpRoles(); |
| 57 |
|
| 58 |
/** Add email templates to WPML strings if WPML installed */ |
| 59 |
MailTemplateHelper::registerTemplateDataToWPMLStrings(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Plugin uninstaller |
| 64 |
*/ |
| 65 |
public static function uninstall() { |
| 66 |
$settings = SettingsController::get_settings(); |
| 67 |
|
| 68 |
/** delete all data if clean all on delete was enabled */ |
| 69 |
if ( filter_var( $settings['clean_all_on_delete'], FILTER_VALIDATE_BOOLEAN ) == true ) { |
| 70 |
/** remove all plugin options */ |
| 71 |
foreach ( self::$wp_options as $option ) { |
| 72 |
delete_option( $option ); |
| 73 |
} |
| 74 |
/** clean database */ |
| 75 |
DatabaseModel::drop_tables(); |
| 76 |
|
| 77 |
/** Clean roles and capabilities */ |
| 78 |
User::cleanRoles(); |
| 79 |
User::cleanCapabilities(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Plugin deactivation |
| 85 |
*/ |
| 86 |
public static function deactivate() { |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get prefix. |
| 91 |
*/ |
| 92 |
public static function getPrefix() { |
| 93 |
if ( null === static::$prefix ) { |
| 94 |
static::$prefix = str_replace( array( '-addon', '-' ), array( '', '_' ), static::getSlug() ) . '_'; |
| 95 |
} |
| 96 |
|
| 97 |
return static::$prefix; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get plugin info (title, version, text domain). |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public static function getPluginInfo() { |
| 106 |
$info = array(); |
| 107 |
if ( empty( static::$info ) ) { |
| 108 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 109 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 110 |
} |
| 111 |
$plugin_data = get_plugin_data( BOOKIT_FILE ); |
| 112 |
$info['version'] = $plugin_data['Version']; |
| 113 |
$info['title'] = $plugin_data['Name']; |
| 114 |
$info['text_domain'] = $plugin_data['TextDomain']; |
| 115 |
} |
| 116 |
|
| 117 |
return $info; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Check is addon installed |
| 122 |
* |
| 123 |
* @param $addon |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public static function isAddonInstalledAndEnabled( $addon ) { |
| 128 |
$addons = FreemiusHelper::get_installed_addons(); |
| 129 |
$addonKey = array_search( $addon, array_column( $addons, 'name' ) ); |
| 130 |
|
| 131 |
if ( false !== $addonKey |
| 132 |
&& filter_var( get_deep_array_value_by_path( 'data.settings.enabled', $addons[ $addonKey ] ), FILTER_VALIDATE_BOOLEAN ) ) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get plugin addon info by name. |
| 141 |
* |
| 142 |
* @param $addon |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public static function getAddonInfo( $addon ) { |
| 147 |
$classNamespacePart = str_replace( '-', '', ucwords( self::$prefix . $addon, '-' ) ); |
| 148 |
|
| 149 |
/** base addon class */ |
| 150 |
$addonClass = sprintf( '%s\Classes\Admin\Base', ucwords( $classNamespacePart ) ); |
| 151 |
if ( ! class_exists( $addonClass ) ) { |
| 152 |
return array(); |
| 153 |
} |
| 154 |
|
| 155 |
$info = array( |
| 156 |
'name' => $addon, |
| 157 |
'data' => $addonClass::getAddonData(), |
| 158 |
); |
| 159 |
|
| 160 |
/** |
| 161 |
* Filter an add-on plugin info. |
| 162 |
* |
| 163 |
* @since TBD |
| 164 |
* |
| 165 |
* @param array<string|mixed> $info The add-on info. |
| 166 |
*/ |
| 167 |
return apply_filters( 'bookit_addon_info', $info ); |
| 168 |
} |
| 169 |
} |
| 170 |
|