forked from idno/WordPress
/
Main.php
85 lines (66 loc) · 2.98 KB
/
Main.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
<?php
namespace IdnoPlugins\WordPress {
class Main extends \Idno\Common\Plugin {
function registerPages(){
// Register settings page
\Idno\Core\site()->addPageHandler('account/wordpress/?', '\IdnoPlugins\WordPress\Pages\Account');
/** Template extensions */
// Add menu item to account screen
\Idno\Core\site()->template()->extendTemplate('account/menu/items', 'account/wordpress/menu');
}
function registerEventHooks()
{
\Idno\Core\site()->syndication()->registerService('wordpress', function() {
return $this->hasWordPress();
}, ['article']);
// Push "articles" to WordPress
\Idno\Core\site()->addEventHook('post/article/wordpress', function (\Idno\Core\Event $event) {
if ($this->hasWordPress()) {
$wpClient = $this->connect();
$object = $event->data()['object'];
$title = $object->getTitle();
$body = $object->getDescription();
$hashtags = $object->getTags();
foreach($hashtags as $tag){
$tags[] = str_replace('#', '', $tag);
}
$content['terms_names'] = array('post_tag' => $tags);
$post = $wpClient->newPost($title, $body, $content);
if ($post) {
$postinfo = $wpClient->getPost($post);
$object->setPosseLink('wordpress',$postinfo['link']);
$object->save();
}
}
});
}
/**
* Connect to WordPress
* @return bool|\wpClient
*/
function connect(){
require_once(dirname(__FILE__) . '/external/WordpressClient.php');
$username = \Idno\Core\site()->config()->wordpress['wp_username'];
$password = \Idno\Core\site()->config()->wordpress['wp_password'];
$url = \Idno\Core\site()->config()->wordpress['wp_url'];
$url .= (substr($url, -1) == '/' ? '' : '/');
$endpoint = $url . 'xmlrpc.php';
# Create client instance
$wpClient = new \HieuLe\WordpressXmlrpcClient\WordpressClient();
# Set the credentials for the next requests
$wpClient->setCredentials($endpoint, $username, $password);
return $wpClient;
}
/**
* Do we have WordPress information?
* @return bool
*/
function hasWordPress()
{
if (!empty(\Idno\Core\site()->config()->wordpress)) {
return true;
}
return false;
}
}
}