-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbootstrap.php
163 lines (138 loc) · 3.6 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
use NewfoldLabs\WP\Module\Data\Data;
use NewfoldLabs\WP\Module\Data\Helpers\Encryption;
use NewfoldLabs\WP\Module\Data\Helpers\Transient;
use NewfoldLabs\WP\Module\Data\SiteCapabilities;
use NewfoldLabs\WP\ModuleLoader\Container;
use WP_Forge\UpgradeHandler\UpgradeHandler;
use function NewfoldLabs\WP\ModuleLoader\container;
use function NewfoldLabs\WP\ModuleLoader\register as registerModule;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
return;
}
// Do not allow multiple copies of the module to be active
if ( defined( 'NFD_DATA_MODULE_VERSION' ) ) {
return;
}
define( 'NFD_DATA_MODULE_VERSION', '2.6.10' );
if ( function_exists( 'is_admin' ) && is_admin() ) {
$upgrade_handler = new UpgradeHandler(
__DIR__ . '/upgrades',
get_option( 'nfd_data_module_version' ),
NFD_DATA_MODULE_VERSION
);
if ( $upgrade_handler->maybe_upgrade() ) {
// If an upgrade occurred, update the new version in the database to prevent running the routine(s) again.
update_option( 'nfd_data_module_version', NFD_DATA_MODULE_VERSION, true );
}
}
/**
* Register the data module
*/
if ( function_exists( 'add_action' ) && function_exists( 'add_filter' ) ) {
add_action(
'plugins_loaded',
function () {
registerModule(
array(
'name' => 'data',
'label' => __( 'Data', 'newfold-data-module' ),
'callback' => function () {
$module = new Data();
$module->start();
},
'isActive' => true,
'isHidden' => true,
)
);
}
);
// Auto-encrypt token on save.
add_filter(
'pre_update_option_nfd_data_token',
function ( $value ) {
return ( new Encryption() )->encrypt( $value );
}
);
// Auto-decrypt token when fetched
add_filter(
'option_nfd_data_token',
function ( $value ) {
return ( new Encryption() )->decrypt( $value );
}
);
// Temporary filter, as the migrate capability isn't working as expected.
add_filter(
'pre_set_transient_nfd_site_capabilities',
function ( $transient ) {
if ( empty( $transient ) && 'bluehost' === container()->plugin()->brand ) {
return array(
'canMigrateSite' => true,
'hasAISiteGen' => true,
);
}
return $transient;
},
10,
2
);
// Register activation/deactivation hooks
add_action(
'newfold_container_set',
function ( Container $container ) {
register_activation_hook(
$container->plugin()->file,
function () use ( $container ) {
nfd_create_event_queue_table();
Transient::set( 'nfd_plugin_activated', $container->plugin()->basename );
}
);
register_deactivation_hook(
$container->plugin()->file,
function () use ( $container ) {
delete_option( 'nfd_data_module_version' );
nfd_drop_event_queue_table();
}
);
$container->set(
'capabilities',
$container->service(
function () {
return new SiteCapabilities();
}
)
);
}
);
}
/**
* Create the event queue table
*/
function nfd_create_event_queue_table() {
global $wpdb;
if ( ! function_exists( 'dbDelta' ) ) {
require ABSPATH . 'wp-admin/includes/upgrade.php';
}
$wpdb->hide_errors();
$charset_collate = $wpdb->get_charset_collate();
$sql = <<<SQL
CREATE TABLE {$wpdb->prefix}nfd_data_event_queue (
id bigint(20) NOT NULL AUTO_INCREMENT,
event longtext NOT NULL,
attempts tinyint(3) NOT NULL DEFAULT 0,
reserved_at datetime DEFAULT NULL,
available_at datetime NOT NULL,
created_at datetime NOT NULL,
PRIMARY KEY (id)
) $charset_collate;
SQL;
dbDelta( $sql );
}
/**
* Drop the event queue table
*/
function nfd_drop_event_queue_table() {
global $wpdb;
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}nfd_data_event_queue" );
}