/
extension.driver.php
134 lines (109 loc) · 4.37 KB
/
extension.driver.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
<?php
require_once(TOOLKIT . '/class.gateway.php');
class Extension_Google_Blog_Search_Ping extends Extension {
private static $config_handle = 'google-blog-search-ping-api';
public function about() {
return array(
'name' => 'Google Blog Search Pinging Service API',
'version' => '0.9',
'release-date' => '2011-11-07',
'author' => array(
array(
'name' => 'Brendan Abbott',
'email' => 'brendan@bloodbone.ws'
),
),
'description' => 'A simple extension that notifies Google Blog Search of updated content using their Pinging API.'
);
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'addCustomPreferenceFieldsets'
),
array(
'page' => '/publish/new/',
'delegate' => 'EntryPostCreate',
'callback' => 'entryPostEdit'
),
array(
'page' => '/publish/edit/',
'delegate' => 'EntryPostEdit',
'callback' => 'entryPostEdit'
)
);
}
/*-------------------------------------------------------------------------
Utilities
-------------------------------------------------------------------------*/
public function get($key) {
return Symphony::Configuration()->get($key, self::$config_handle);
}
/*-------------------------------------------------------------------------
Delegate Callbacks:
-------------------------------------------------------------------------*/
public function addCustomPreferenceFieldsets($context) {
$wrapper = $context['wrapper'];
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Google Blog Search Pinging API')));
$group = new XMLElement('div');
$group->setAttribute('class', 'group');
// Monitor this section
$options = array();
$sectionManager = new SectionManager($this->_Parent);
$sections = $sectionManager->fetch();
foreach($sections as $section) {
$options[] = array($section->get('id'), $section->get('id') == $this->get('monitor-sections'), $section->get('name'));
}
$label = Widget::Label(__('Monitor this section'));
$input = Widget::Select('settings[' . self::$config_handle . '][monitor-sections]', $options);
$label->appendChild($input);
$group->appendChild($label);
// Send this URL
$options = array();
$pages = Symphony::Database()->fetch('
SELECT `p`.id
FROM tbl_pages AS `p`
RIGHT JOIN tbl_pages_types `pt` ON(`p`.id = `pt`.page_id)
WHERE `pt`.type = "xml"
');
foreach($pages as $page) {
$page_url = URL . '/' . Administration::instance()->resolvePagePath($page['id']) . '/';
$options[] = array($page['id'], $page_url == $this->get('ping-url'), $page_url);
}
$label = Widget::Label(__('Ping URL'));
$input = Widget::Select('settings[' . self::$config_handle . '][ping-url]', $options);
$label->appendChild($input);
$group->appendChild($label);
$fieldset->appendChild($group);
$fieldset->appendChild(
new XMLElement('p', __('When new entries are created in the selected section, a request will be sent to Google with the Ping URL which should be an RSS, Atom or RDF feed.'), array('class' => 'help'))
);
$wrapper->appendChild($fieldset);
}
public function entryPostEdit(Array &$context) {
// Check the Entry is being edited in the Monitored section, otherwise return
if($context['section']->get('id') != $this->get('monitor-sections')) return;
// Make sure a Ping URL is set.
if(is_null($this->get('ping-url'))) return;
// Lets hit Google, with a ping, not a bat. We're matching Google's format
// specified @ http://www.google.com/help/blogsearch/pinging_API.html#rest.
// The Site Name and URL will come from the config file, convention over configuration baby.
$ping_request = array(
'name' => Symphony::Configuration()->get('sitename', 'general'),
'url' => URL,
'changesURL' => $this->get('ping-url')
);
// Gateway
$g = new Gateway;
$g->init('http://blogsearch.google.com/ping?' . http_build_query($ping_request));
// Catch the result, we don't really need it though.
$result = $g->exec();
if(isset(Symphony::$Log)) Symphony::$Log->pushToLog(__('Google Blog Search API: ') . $result, E_USER_NOTICE, true);
$info = $g->getInfoLast();
return $info['http_code'] == 200;
}
}