-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmomentousaddon.php
133 lines (111 loc) · 4.53 KB
/
momentousaddon.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
<?php
/*
Plugin Name: Gravity Forms Momentous Feed Add-On
Plugin URI: https://www.shakewell.agency/
Description: An add-on for Momentous
Version: 1.8
Author: Shakewell
Author URI: https://www.shakewell.agency/
*/
define('GF_MOMENTOUS_FEED_ADDON_VERSION', '1.8');
add_action('gform_loaded', array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'load' ), 5);
add_action('gform_after_submission', array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'processSubmission' ), 5);
add_action('momentous_async_send_cron', array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'do_async_send' ));
add_action('momentous_process_failed_requests_cron', array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'process_async_failed_requests' ));
add_filter('cron_schedules', array('GF_Momentous_Feed_AddOn_Bootstrap','cron_schedule_filter'));
register_activation_hook(__FILE__, array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'activate_cron' ));
register_activation_hook(__FILE__, array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'create_table' ));
register_deactivation_hook(__FILE__, array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'delete_table' ));
register_deactivation_hook(__FILE__, array( 'GF_Momentous_Feed_AddOn_Bootstrap', 'deactivate_cron' ));
class GF_Momentous_Feed_AddOn_Bootstrap
{
const REQUEST_TABLE = 'momentous_requests';
public static function load()
{
if (! method_exists('GFForms', 'include_feed_addon_framework')) {
return;
}
require_once('class-gfmomentousfeedaddon.php');
GFAddOn::register('GFMomentousFeedAddOn');
}
public static function create_table()
{
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . self::REQUEST_TABLE;
$sql = "CREATE TABLE " . $table_name . " (
id int auto_increment,
body LONGTEXT not null,
accounts_call_status varchar(10) null,
opportunities_call_status varchar(10) null,
status varchar(10) not null,
accounts_response LONGTEXT null,
opportunities_response LONGTEXT null,
created_at TIMESTAMP not null,
executed_at TIMESTAMP null,
last_attempt_at TIMESTAMP null,
completed_at TIMESTAMP null,
constraint PRIMARY_ID_KEY
primary key (id)
) $charset_collate;";
dbDelta($sql);
}
public static function delete_table()
{
global $wpdb;
$table_name = $wpdb->prefix . self::REQUEST_TABLE;
$wpdb->query("DROP TABLE IF EXISTS $table_name");
}
public static function activate_cron()
{
if (!wp_next_scheduled('momentous_async_send_cron')) {
wp_schedule_event(time(), 'every_one_minute', 'momentous_async_send_cron');
}
if (!wp_next_scheduled('momentous_process_failed_requests_cron')) {
wp_schedule_event(time(), 'every_two_minutes', 'momentous_process_failed_requests_cron');
}
}
public static function deactivate_cron()
{
$timestamp = wp_next_scheduled('momentous_async_send_cron');
if ($timestamp) {
wp_unschedule_event($timestamp, 'momentous_async_send_cron');
}
$timestamp = wp_next_scheduled('momentous_process_failed_requests_cron');
if ($timestamp) {
wp_unschedule_event($timestamp, 'momentous_process_failed_requests_cron');
}
}
public static function do_async_send()
{
$object = gf_momentous_feed_addon();
$object->process_async_requests();
}
public static function process_async_failed_requests() {
$object = gf_momentous_feed_addon();
$object->process_failed_async_requests();
}
public static function cron_schedule_filter() {
$schedules['every_two_minutes'] = [
'interval' => 120, // 180 seconds = 3 minutes
'display' => __('Every minute'),
];
$schedules['every_one_minute'] = [
'interval' => 60, // 60 seconds = 1 minute
'display' => __('Every minute'),
];
return $schedules;
}
public static function processSubmission($form)
{
$object = gf_momentous_feed_addon();
$mapping = $object->get_mapped_fields($form['form_id']);
$values = $object->process_mapped_fields($mapping, $form);
$object->process_request($values);
}
}
function gf_momentous_feed_addon()
{
return GFMomentousFeedAddOn::get_instance();
}