This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.php
73 lines (59 loc) · 1.82 KB
/
start.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
<?php
/**
* Pushes blog posts to community forums through web services
*/
register_elgg_event_handler('init', 'system', 'blog2groups_init');
function blog2groups_init() {
register_elgg_event_handler('create', 'object', 'blog2groups_push_post');
register_elgg_event_handler('update', 'object', 'blog2groups_check_publish_status');
}
/**
* Push the blog post to the configured site
*/
function blog2groups_push_post($event, $object_type, $object) {
// work around Elgg bug with subtype
$id = get_subtype_id('object', 'blog');
if ($object->subtype !== 'blog' && $object->subtype !== $id) {
return;
}
if ($object->access_id == ACCESS_PRIVATE) {
return;
}
$url = get_plugin_setting('url', 'blog2groups');
if (!$url) {
return;
}
// work around a Elgg bug with encoding parameters
$url = str_replace('&', '&', $url);
$body = $object->summary . "\n\n" . $object->description;
$params = array(
'username' => $object->getOwnerEntity()->username,
'title' => $object->title,
'body' => $body,
);
$post_data = http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
$result = json_decode($json);
if ($result->status != 0) {
error_log("Failed to send blog post: $result->message");
}
}
/**
* Check for change in access status and push if going from private to public
*/
function blog2groups_check_publish_status($event, $object_type, $object) {
if ($object->getSubtype() !== 'blog') {
return;
}
$new_access = get_input('access');
if ($new_access == ACCESS_PUBLIC && $object->access_id == ACCESS_PRIVATE) {
$object->access_id = ACCESS_PUBLIC;
blog2groups_push_post($event, $object_type, $object);
}
}