| 1 |
<?php |
| 2 |
|
| 3 |
class LSD_Plugin_Hooks |
| 4 |
{ |
| 5 |
protected static ?LSD_Plugin_Hooks $instance = null; |
| 6 |
|
| 7 |
protected LSD_Main $main; |
| 8 |
protected LSD_db $db; |
| 9 |
|
| 10 |
/** |
| 11 |
* Listdom Plugin Hooks Instance. |
| 12 |
* |
| 13 |
* @return LSD_Plugin_Hooks |
| 14 |
* @since 1.0.0 |
| 15 |
* @static |
| 16 |
*/ |
| 17 |
public static function instance(): LSD_Plugin_Hooks |
| 18 |
{ |
| 19 |
// Get an instance of Class |
| 20 |
if (is_null(self::$instance)) self::$instance = new self(); |
| 21 |
|
| 22 |
// Return the instance |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
protected function __construct() |
| 27 |
{ |
| 28 |
register_activation_hook(LSD_BASENAME, [$this, 'activate']); |
| 29 |
register_deactivation_hook(LSD_BASENAME, [$this, 'deactivate']); |
| 30 |
register_uninstall_hook(LSD_BASENAME, ['LSD_Plugin_Hooks', 'uninstall']); |
| 31 |
|
| 32 |
// Main Class |
| 33 |
$this->main = new LSD_Main(); |
| 34 |
|
| 35 |
// DB Class |
| 36 |
$this->db = new LSD_db(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Runs on plugin activation |
| 41 |
* @param bool $network |
| 42 |
*/ |
| 43 |
public function activate(bool $network = false) |
| 44 |
{ |
| 45 |
// Redirect user to Listdom Dashboard |
| 46 |
add_option('lsd_activation_redirect', true); |
| 47 |
|
| 48 |
$current_blog_id = get_current_blog_id(); |
| 49 |
|
| 50 |
// Plugin activated only for one blog |
| 51 |
if (!function_exists('is_multisite') || !is_multisite()) $network = false; |
| 52 |
if (!$network) |
| 53 |
{ |
| 54 |
$this->install($current_blog_id); |
| 55 |
|
| 56 |
// Add WordPress flush rewrite rules in to do list |
| 57 |
LSD_RewriteRules::todo(); |
| 58 |
|
| 59 |
// Don't run rest of the function |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Plugin activated for all blogs |
| 64 |
$blogs = $this->db->select("SELECT `blog_id` FROM `#__blogs`", 'loadColumn'); |
| 65 |
foreach ($blogs as $blog_id) |
| 66 |
{ |
| 67 |
switch_to_blog($blog_id); |
| 68 |
|
| 69 |
try { $this->install($blog_id); } |
| 70 |
finally { restore_current_blog(); } |
| 71 |
} |
| 72 |
|
| 73 |
// Add WordPress flush rewrite rules in to do list |
| 74 |
LSD_RewriteRules::todo(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Install the plugin on s certain blog |
| 79 |
* @param int $blog_id |
| 80 |
*/ |
| 81 |
public function install(int $blog_id = 1) |
| 82 |
{ |
| 83 |
// Default Settings |
| 84 |
$settings = LSD_Options::defaults(); |
| 85 |
add_option('lsd_settings', $settings); |
| 86 |
|
| 87 |
// Installed Version |
| 88 |
add_option('lsd_version', LSD_VERSION); |
| 89 |
|
| 90 |
// Default Social Networks |
| 91 |
$socials = LSD_Options::defaults('socials'); |
| 92 |
add_option('lsd_socials', $socials); |
| 93 |
|
| 94 |
// Default Styles |
| 95 |
$styles = LSD_Options::defaults('styles'); |
| 96 |
add_option('lsd_styles', $styles); |
| 97 |
|
| 98 |
// Default Page Details Options |
| 99 |
$details_page = LSD_Options::defaults('details_page'); |
| 100 |
add_option('lsd_details_page', $details_page); |
| 101 |
|
| 102 |
// Default Page Details Pattern |
| 103 |
$details_page_pattern = LSD_Options::defaults('details_page_pattern'); |
| 104 |
add_option('lsd_details_page_pattern', $details_page_pattern); |
| 105 |
|
| 106 |
// DB Update |
| 107 |
if ($this->main->is_db_update_required()) |
| 108 |
{ |
| 109 |
LSD_Plugin_Hooks::db_update(); |
| 110 |
} |
| 111 |
|
| 112 |
// Default Customizer |
| 113 |
update_option('lsd_customizer', LSD_Options::customizer()); |
| 114 |
|
| 115 |
// Generate personalized CSS File |
| 116 |
LSD_Personalize::generate(); |
| 117 |
|
| 118 |
// Ensure Listdom user roles exist on every activation path. |
| 119 |
LSD_Roles::add(); |
| 120 |
|
| 121 |
// Save Installation Time |
| 122 |
add_option('lsd_installed_at', current_time('timestamp')); |
| 123 |
} |
| 124 |
|
| 125 |
public static function db_update() |
| 126 |
{ |
| 127 |
global $wpdb; |
| 128 |
|
| 129 |
$table_name = $wpdb->prefix . 'lsd_data'; |
| 130 |
$charset_collate = $wpdb->get_charset_collate(); |
| 131 |
|
| 132 |
$sql = "CREATE TABLE `$table_name` ( |
| 133 |
`id` mediumint(9) NOT NULL AUTO_INCREMENT, |
| 134 |
`latitude` decimal(10,8) NOT NULL DEFAULT '0.00000000', |
| 135 |
`longitude` decimal(11,8) NOT NULL DEFAULT '0.00000000', |
| 136 |
`point` point DEFAULT NULL, |
| 137 |
PRIMARY KEY (id), |
| 138 |
INDEX `latitude` (`latitude`,`longitude`) |
| 139 |
) $charset_collate;"; |
| 140 |
|
| 141 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 142 |
dbDelta($sql); |
| 143 |
|
| 144 |
$table_name = $wpdb->prefix . 'lsd_jobs'; |
| 145 |
$sql = "CREATE TABLE `$table_name` ( |
| 146 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 147 |
`type` varchar(20) NOT NULL, |
| 148 |
`sub_type` varchar(20) NULL, |
| 149 |
`data` text NULL, |
| 150 |
`priority` tinyint(1) NOT NULL DEFAULT '1', |
| 151 |
`runs` tinyint(3) NOT NULL DEFAULT '0', |
| 152 |
`run_at` datetime DEFAULT NULL, |
| 153 |
`created_at` datetime DEFAULT NULL, |
| 154 |
`updated_at` datetime DEFAULT NULL, |
| 155 |
PRIMARY KEY (id), |
| 156 |
KEY `run_at` (`run_at`) |
| 157 |
) $charset_collate;"; |
| 158 |
|
| 159 |
dbDelta($sql); |
| 160 |
|
| 161 |
$table_name = $wpdb->prefix . 'lsd_ai_embeddings'; |
| 162 |
$sql = "CREATE TABLE `$table_name` ( |
| 163 |
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 164 |
`object_type` varchar(20) NOT NULL DEFAULT 'listing', |
| 165 |
`object_id` bigint(20) unsigned NOT NULL, |
| 166 |
`usage` varchar(50) NOT NULL DEFAULT 'semantic-search', |
| 167 |
`profile_id` varchar(100) NOT NULL DEFAULT '', |
| 168 |
`model` varchar(100) NOT NULL DEFAULT '', |
| 169 |
`config_hash` char(32) NOT NULL DEFAULT '', |
| 170 |
`content_hash` char(32) NOT NULL DEFAULT '', |
| 171 |
`document` longtext NULL, |
| 172 |
`embedding` longtext NULL, |
| 173 |
`dimensions` smallint(5) unsigned NOT NULL DEFAULT '0', |
| 174 |
`indexed_at` datetime NULL, |
| 175 |
`updated_at` datetime NULL, |
| 176 |
PRIMARY KEY (`id`), |
| 177 |
UNIQUE KEY `object_usage_profile` (`object_type`, `object_id`, `usage`, `profile_id`), |
| 178 |
KEY `usage_config` (`usage`, `config_hash`), |
| 179 |
KEY `object_type_object_id` (`object_type`, `object_id`) |
| 180 |
) $charset_collate;"; |
| 181 |
|
| 182 |
dbDelta($sql); |
| 183 |
|
| 184 |
update_option('lsd_db_version', LSD_Base::DB_VERSION); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Runs on plugin deactivation |
| 189 |
* @param bool $network |
| 190 |
*/ |
| 191 |
public function deactivate(bool $network = false) |
| 192 |
{ |
| 193 |
// Clear Scheduled Hook |
| 194 |
if ($network && is_multisite()) |
| 195 |
{ |
| 196 |
$db = new LSD_db(); |
| 197 |
$blogs = $db->select("SELECT `blog_id` FROM `#__blogs`", 'loadColumn'); |
| 198 |
|
| 199 |
foreach ($blogs as $blog_id) |
| 200 |
{ |
| 201 |
switch_to_blog($blog_id); |
| 202 |
|
| 203 |
try { self::clear_scheduled_hooks(); } |
| 204 |
finally { restore_current_blog(); } |
| 205 |
} |
| 206 |
} |
| 207 |
else self::clear_scheduled_hooks(); |
| 208 |
|
| 209 |
/** |
| 210 |
* Refresh WordPress rewrite rules |
| 211 |
* We cannot use LSD_RewriteRules here because plugin is deactivated and it won't run |
| 212 |
*/ |
| 213 |
flush_rewrite_rules(); |
| 214 |
} |
| 215 |
|
| 216 |
protected static function clear_scheduled_hooks(): void |
| 217 |
{ |
| 218 |
wp_clear_scheduled_hook('lsd_jobs_run'); |
| 219 |
wp_clear_scheduled_hook(LSD_Announcements::CRON_HOOK); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Runs on plugin uninstallation |
| 224 |
*/ |
| 225 |
public static function uninstall() |
| 226 |
{ |
| 227 |
// DB Class |
| 228 |
$db = new LSD_db(); |
| 229 |
|
| 230 |
// Getting current blog |
| 231 |
$current_blog_id = get_current_blog_id(); |
| 232 |
|
| 233 |
// Single WordPress Installation |
| 234 |
if (!function_exists('is_multisite') || !is_multisite()) |
| 235 |
{ |
| 236 |
self::purge($current_blog_id); |
| 237 |
|
| 238 |
/** |
| 239 |
* Refresh WordPress rewrite rules |
| 240 |
* We cannot use LSD_RewriteRules here because plugin is removed and it won't run |
| 241 |
*/ |
| 242 |
flush_rewrite_rules(); |
| 243 |
|
| 244 |
// Don't run rest of the function |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
// WordPress is multisite, so we should purge the plugin from all blogs |
| 249 |
$blogs = $db->select("SELECT `blog_id` FROM `#__blogs`", 'loadColumn'); |
| 250 |
foreach ($blogs as $blog_id) |
| 251 |
{ |
| 252 |
switch_to_blog($blog_id); |
| 253 |
|
| 254 |
try { self::purge($blog_id); } |
| 255 |
finally { restore_current_blog(); } |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Refresh WordPress rewrite rules |
| 260 |
* We cannot use LSD_RewriteRules here because plugin is removed and it won't run |
| 261 |
*/ |
| 262 |
flush_rewrite_rules(); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Remove Listdom from a blog |
| 267 |
* @param int $blog_id |
| 268 |
*/ |
| 269 |
public static function purge(int $blog_id = 1) |
| 270 |
{ |
| 271 |
// Delete the data or not! |
| 272 |
$delete = apply_filters('lsd_purge_options', true); |
| 273 |
|
| 274 |
// Listdom Deleted |
| 275 |
if ($delete) |
| 276 |
{ |
| 277 |
delete_option('lsd_activation_redirect'); |
| 278 |
delete_option('lsd_settings'); |
| 279 |
delete_option('lsd_socials'); |
| 280 |
delete_option('lsd_styles'); |
| 281 |
delete_option('lsd_addons'); |
| 282 |
delete_option('lsd_todo_flush'); |
| 283 |
delete_option('lsd_installed_at'); |
| 284 |
delete_option('lsd_purchase_code'); |
| 285 |
delete_option('lsd_activation_id'); |
| 286 |
delete_option('lsd_version'); |
| 287 |
delete_option('lsd_default_notifications_seeded'); |
| 288 |
delete_option(LSD_Announcements::OPTION_KEY); |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|