-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.php
120 lines (98 loc) · 3.03 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
<?php
use NewfoldLabs\WP\Module\Tasks\Tasks;
use NewfoldLabs\WP\ModuleLoader\Container;
use function NewfoldLabs\WP\ModuleLoader\register;
/**
* Add the task table
*
* @param string $table_name The table name to be used for tasks
*/
function setup_task_table( $table_name ) {
global $wpdb;
// Maybe create the table
if ( ! function_exists( 'maybe_create_table' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE `{$wpdb->prefix}{$table_name}` (
task_id bigint(20) NOT NULL AUTO_INCREMENT,
task_name varchar(63) NOT NULL,
task_execute varchar(125),
args longtext,
num_retries int(2) UNSIGNED,
task_interval int(2) UNSIGNED,
task_priority int(2) UNSIGNED,
task_status varchar(12),
updated TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
enabled tinyint(1),
PRIMARY KEY (task_id)
) $charset_collate;";
maybe_create_table( $wpdb->prefix . $table_name, $sql );
}
/**
* Add the task results table
*
* @param string $table_name The table name to be used for task results
*/
function setup_task_results_table( $table_name ) {
global $wpdb;
// Maybe create the table
if ( ! function_exists( 'maybe_create_table' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE `{$wpdb->prefix}{$table_name}` (
task_result_id bigint(20) NOT NULL AUTO_INCREMENT,
task_name varchar(63) NOT NULL,
stacktrace longtext,
success tinyint(1),
updated TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (task_result_id)
) $charset_collate;";
maybe_create_table( $wpdb->prefix . $table_name, $sql );
}
/**
* Drop the tables on plugin deactivation
*/
function purge_tables() {
global $wpdb;
// phpcs:ignore
$wpdb->query( "DROP TABLE IF EXISTS `$wpdb->prefix}nfd_tasks`" );
// phpcs:ignore
$wpdb->query( "DROP TABLE IF EXISTS `$wpdb->prefix}nfd_task_results`" );
}
if ( function_exists( 'add_action' ) ) {
add_action(
'plugins_loaded',
function () {
// Set Global Constants
if ( ! defined( 'MODULE_TASKS_VERSION' ) ) {
define( 'MODULE_TASKS_VERSION', '0.0.1' );
}
if ( ! defined( 'MODULE_TASKS_DIR' ) ) {
define( 'MODULE_TASKS_DIR', __DIR__ );
}
if ( ! defined( 'MODULE_TASKS_TASK_TABLE_NAME' ) ) {
define( 'MODULE_TASKS_TASK_TABLE_NAME', 'nfd_tasks' );
}
if ( ! defined( 'MODULE_TASKS_TASK_RESULTS_TABLE_NAME' ) ) {
define( 'MODULE_TASKS_TASK_RESULTS_TABLE_NAME', 'nfd_task_results' );
}
// Make the table setup calls
setup_task_table( MODULE_TASKS_TASK_TABLE_NAME );
setup_task_results_table( MODULE_TASKS_TASK_RESULTS_TABLE_NAME );
register_deactivation_hook( __FILE__, 'purge_tables' );
register(
[
'name' => 'tasks',
'label' => __( 'Tasks', 'newfold-tasks-module' ),
'callback' => function ( Container $container ) {
new Tasks( $container );
},
'isActive' => true,
'isHidden' => true,
]
);
}
);
}