This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
event-settings.php
191 lines (157 loc) · 7.36 KB
/
event-settings.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace KymaProject\WordPressConnector;
require_once( dirname( __FILE__ ) . '/class-event.php' );
class EventSettings {
private $name = "Wordpress";
private $description = "Wordpress Event Definition";
private $version = "1.0";
private $option_group;
private $page_name;
public $events = array();
function __construct($og, $page_name){
$this->option_group = $og;
$this->page_name = $page_name;
$events_setting_name = $this->option_group.'_events';
$events = get_option($events_setting_name);
foreach ($events as $id => $event) {
$this->events[$id] = Event::importArray($event, $events_setting_name, $id);
}
add_action("update_option_$events_setting_name", function ($old_value, $new_value) {
// set a flag when the events change, so that the application registration gets updated
if ($old_value !== $new_value) {
update_option('kymaconnector_events_updated', '1');
}
}, 10, 2);
}
public static function subscribe_events(){
$events = new EventSettings('kymaconnector', '');
foreach($events->events as $id => $event){
$event->register_hook();
}
}
public static function install($option_group){
$events = array();
array_push($events, array('event_type'=>'user.created', 'event_version'=>'v1', 'hook'=>'user_register', 'description'=>'User Register Event v1', 'payload'=>'{"userId":{"type":"string","description":"Id of a User","title":"User uid"}}'));
array_push($events, array('event_type'=>'comment.post', 'event_version'=>'v1', 'hook'=>'comment_post', 'description'=>'Comment Post Event v1', 'payload'=>'{"commentId":{"type":"string","description":"Unique id of a comment","title":"Comment id"},"commentStatus":{"type":"string","description":"Status of the comment","title":"Status of the comment"}}'));
array_push($events, array('event_type'=>'post.published', 'event_version'=>'v1', 'hook'=>'publish_post', 'description'=>'Comment Post Event v1', 'payload'=>'{"postId":{"type":"string","description":"Unique id of a post","title":"Post id"}}'));
add_option($option_group.'_events', $events);
}
public function settings_page() {
// TODO: Set default values
$events_setting_name = $this->option_group.'_events';
register_setting($this->option_group, $this->option_group.'_event_api_name');
register_setting($this->option_group, $this->option_group.'_event_api_description');
register_setting($this->option_group, $this->option_group.'_event_api_version');
register_setting($this->option_group, $events_setting_name);
add_option($this->option_group.'_event_api_name', 'Wordpress Events');
add_option($this->option_group.'_event_api_description', 'Wordpress Events');
add_option($this->option_group.'_event_api_version', 'v1');
add_settings_section(
$this->option_group.'_event_settings',
'Events Registration Settings',
array($this, 'settings_section_cb'),
$this->page_name
);
add_settings_field(
$this->option_group.'_event_api_name',
'Event API Name',
array($this, 'field_api_name_cb'),
$this->page_name,
$this->option_group.'_event_settings'
);
$this->name = get_option($this->option_group.'_event_api_name');
add_settings_field(
$this->option_group.'_event_api_version',
'Event API Version',
array($this, 'field_api_version_cb'),
$this->page_name,
$this->option_group.'_event_settings'
);
$this->version = get_option($this->option_group.'_event_api_version');
add_settings_field(
$this->option_group.'_event_api_description',
'Event API Description',
array($this, 'field_api_description_cb'),
$this->page_name,
$this->option_group.'_event_settings'
);
$this->description = get_option($this->option_group.'_event_api_description');
add_settings_field(
$this->option_group.'_events',
'Events',
array($this, 'field_events_cb'),
$this->page_name,
$this->option_group.'_event_settings'
);
}
public function settings_section_cb(){
echo '<p>API Registration details.</p>';
}
public function field_api_name_cb(){
$value = isset( $this->name ) ? $this->name : '';
printf(
'<input type="text" name="%s" value="%s">',
esc_attr($this->option_group . '_event_api_name'),
esc_attr($value)
);
}
public function field_api_version_cb(){
$value = isset( $this->version ) ? $this->version : '';
printf(
'<input type="text" name="%s" value="%s">',
esc_attr($this->option_group . '_event_api_version'),
esc_attr($value)
);
}
public function field_api_description_cb(){
$value = isset( $this->description ) ? $this->description : '';
printf(
'<textarea name="%s" rows="5" cols="50">%s</textarea>',
esc_attr($this->option_group . '_event_api_description'),
esc_textarea($value)
);
}
public function field_events_cb(){
?>
List of events forwarded to Kyma. The System will use <a href="https://codex.wordpress.org/Plugin_API/Action_Reference">Wordpress Action Hooks</a> to subscribe. <br/>The attributes deffined in the event payload section are mapped to the number of parameteres of the action hook.
<table id="event-settings">
<tr>
<th>Event Type</th>
<th>Event Version</th>
<th>Wordpress Action Hook</th>
<th>Event Description</th>
<th>Payload Definition</th>
<th></th>
</tr>
<?php
foreach ($this->events as $event) {
echo $event->render_settings();
}
?>
</table><?php
$events_setting_name = $this->option_group.'_events';
$e = new Event();
$jscode = "jQuery('#event-settings').append(".json_encode($e->render_settings()).");";
$jscode .= "var events = jQuery(this).data('events');";
$jscode .= "var prefix = '".$events_setting_name."[' + events + ']';";
$jscode .= "jQuery('.no-prefix').attr('name', function(i, val){return prefix + val;}).removeClass('no-prefix');";
$jscode .= "jQuery(this).data('events', events + 1);";
$jscode .= "return false;";
$next_id = end($this->events)->get_id() + 1;
echo '<a href="#" onclick="'.htmlspecialchars($jscode).'" data-events="'.$next_id.'">Add New Event</a>';
}
public function get_event_spec(){
$topics = "";
foreach ($this->events as $event) {
$topics .= $event->get_event_spec().",";
}
$topics = rtrim($topics, ",");
return sprintf(
'{"spec":{"asyncapi":"1.0.0","info":{"title":"%s","version":"%s","description":"%s"},"topics":{%s}}}',
$this->name,
$this->version,
$this->description,
$topics
);
}
}