]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
Merge pull request 'Rename group and forum' (#1394) from MrPetovan/friendica-addons...
[friendica-addons.git] / wppost / wppost.php
1 <?php
2 /**
3  * Name: WordPress Post Connector
4  * Description: Post to WordPress (or anything else which uses blogger XMLRPC API)
5  * Version: 1.1
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Item;
17 use Friendica\Model\Post;
18 use Friendica\Util\XML;
19
20 function wppost_install()
21 {
22         Hook::register('hook_fork',            'addon/wppost/wppost.php', 'wppost_hook_fork');
23         Hook::register('post_local',           'addon/wppost/wppost.php', 'wppost_post_local');
24         Hook::register('notifier_normal',      'addon/wppost/wppost.php', 'wppost_send');
25         Hook::register('jot_networks',         'addon/wppost/wppost.php', 'wppost_jot_nets');
26         Hook::register('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
27         Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
28 }
29
30 function wppost_jot_nets(array &$jotnets_fields)
31 {
32         if (!DI::userSession()->getLocalUserId()) {
33                 return;
34         }
35
36         if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post')) {
37                 $jotnets_fields[] = [
38                         'type' => 'checkbox',
39                         'field' => [
40                                 'wppost_enable',
41                                 DI::l10n()->t('Post to Wordpress'),
42                                 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default')
43                         ]
44                 ];
45         }
46 }
47
48
49 function wppost_settings(array &$data)
50 {
51         if (!DI::userSession()->getLocalUserId()) {
52                 return;
53         }
54
55         $enabled            = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post', false);
56         $wp_username        = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'wp_username');
57         $wp_blog            = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'wp_blog');
58         $def_enabled        = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default', false);
59         $back_enabled       = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'backlink', false);
60         $wp_backlink_text   = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'wp_backlink_text');
61         $shortcheck_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'shortcheck', false);
62
63         $t    = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/wppost/');
64         $html = Renderer::replaceMacros($t, [
65                 '$enabled'       => ['wppost', DI::l10n()->t('Enable Wordpress Post Addon'), $enabled],
66                 '$username'      => ['wp_username', DI::l10n()->t('Wordpress username'), $wp_username],
67                 '$password'      => ['wp_password', DI::l10n()->t('Wordpress password')],
68                 '$blog'          => ['wp_blog', DI::l10n()->t('WordPress API URL'), $wp_blog],
69                 '$bydefault'     => ['wp_bydefault', DI::l10n()->t('Post to Wordpress by default'), $def_enabled],
70                 '$backlink'      => ['wp_backlink', DI::l10n()->t('Provide a backlink to the Friendica post'), $back_enabled],
71                 '$backlink_text' => ['wp_backlink_text', DI::l10n()->t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.'), $wp_backlink_text],
72                 '$shortcheck'    => ['wp_shortcheck', DI::l10n()->t('Don\'t post messages that are too short'), $shortcheck_enabled],
73         ]);
74
75         $data = [
76                 'connector' => 'wppost',
77                 'title'     => DI::l10n()->t('Wordpress Export'),
78                 'image'     => 'images/wordpress.png',
79                 'enabled'   => $enabled,
80                 'html'      => $html,
81         ];
82 }
83
84
85 function wppost_settings_post(array &$b)
86 {
87         if (!empty($_POST['wppost-submit'])) {
88                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'post', intval($_POST['wppost']));
89                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default', intval($_POST['wp_bydefault']));
90                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_username',   trim($_POST['wp_username']));
91                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_password',   trim($_POST['wp_password']));
92                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_blog',   trim($_POST['wp_blog']));
93                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'backlink', intval($_POST['wp_backlink']));
94                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'shortcheck', intval($_POST['wp_shortcheck']));
95                 $wp_backlink_text = BBCode::convert(trim($_POST['wp_backlink_text']), false, BBCode::BACKLINK);
96                 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
97                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_backlink_text', $wp_backlink_text);
98         }
99 }
100
101 function wppost_hook_fork(array &$b)
102 {
103         if ($b['name'] != 'notifier_normal') {
104                 return;
105         }
106
107         $post = $b['data'];
108
109         if (
110                 $post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
111                 !strstr($post['postopts'] ?? '', 'wppost') || ($post['parent'] != $post['id'])
112         ) {
113                 $b['execute'] = false;
114                 return;
115         }
116 }
117
118 function wppost_post_local(array &$b)
119 {
120
121         // This can probably be changed to allow editing by pointing to a different API endpoint
122
123         if ($b['edit']) {
124                 return;
125         }
126
127         if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
128                 return;
129         }
130
131         if ($b['private'] || $b['parent']) {
132                 return;
133         }
134
135         $wp_post   = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post'));
136
137         $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
138
139         if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default'))) {
140                 $wp_enable = 1;
141         }
142
143         if (!$wp_enable) {
144                 return;
145         }
146
147         if (strlen($b['postopts'])) {
148                 $b['postopts'] .= ',';
149         }
150
151         $b['postopts'] .= 'wppost';
152 }
153
154
155
156
157 function wppost_send(array &$b)
158 {
159         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
160                 return;
161         }
162
163         if (!strstr($b['postopts'], 'wppost')) {
164                 return;
165         }
166
167         if ($b['gravity'] != Item::GRAVITY_PARENT) {
168                 return;
169         }
170
171         // Dont't post if the post doesn't belong to us.
172         // This is a check for group postings
173         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
174         if ($b['contact-id'] != $self['id']) {
175                 return;
176         }
177
178         $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
179
180         $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
181         $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
182         $wp_blog = DI::pConfig()->get($b['uid'], 'wppost', 'wp_blog');
183         $wp_backlink_text = DI::pConfig()->get($b['uid'], 'wppost', 'wp_backlink_text');
184         if ($wp_backlink_text == '') {
185                 $wp_backlink_text = DI::l10n()->t('Read the orig­i­nal post and com­ment stream on Friendica');
186         }
187
188         if ($wp_username && $wp_password && $wp_blog) {
189                 $wptitle = trim($b['title']);
190
191                 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
192                         // Checking, if its a post that is worth a blog post
193                         $postentry = (bool)Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML, Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]);
194
195                         // Does it have a title?
196                         if ($wptitle != "") {
197                                 $postentry = true;
198                         }
199
200                         // Is it larger than 500 characters?
201                         if (strlen($b['body']) > 500) {
202                                 $postentry = true;
203                         }
204
205                         if (!$postentry) {
206                                 return;
207                         }
208                 }
209
210                 // If the title is empty then try to guess
211                 if ($wptitle == '') {
212                         // Fetch information about the post
213                         $media = Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML]);
214                         if (!empty($media) && !empty($media[0]['name']) && ($media[0]['name'] != $media[0]['url'])) {
215                                 $wptitle = $media[0]['name'];
216                         }
217
218                         // If no bookmark is found then take the first line
219                         if ($wptitle == '') {
220                                 // Remove the share element before fetching the first line
221                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism", "\n$1\n", $b['body']));
222
223                                 $title = BBCode::toPlaintext($title) . "\n";
224                                 $pos = strpos($title, "\n");
225                                 $trailer = "";
226                                 if (($pos == 0) || ($pos > 100)) {
227                                         $pos = 100;
228                                         $trailer = "...";
229                                 }
230
231                                 $wptitle = substr($title, 0, $pos) . $trailer;
232                         }
233                 }
234
235                 $title = '<title>' . (($wptitle) ? $wptitle : DI::l10n()->t('Post from Friendica')) . '</title>';
236                 $post = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
237
238                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
239                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
240                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
241
242                 $post = $title . $post;
243
244                 $wp_backlink = intval(DI::pConfig()->get($b['uid'], 'wppost', 'backlink'));
245                 if ($wp_backlink && $b['plink']) {
246                         $post .= '<p><a href="' . $b['plink'] . '">' . $wp_backlink_text . '</a></p>';
247                 }
248
249                 $post = XML::escape($post);
250
251
252                 $xml = <<< EOT
253 <?xml version=\"1.0\" encoding=\"utf-8\"?>
254 <methodCall>
255   <methodName>blogger.newPost</methodName>
256   <params>
257     <param><value><string/></value></param>
258     <param><value><string/></value></param>
259     <param><value><string>$wp_username</string></value></param>
260     <param><value><string>$wp_password</string></value></param>
261     <param><value><string>$post</string></value></param>
262     <param><value><int>1</int></value></param>
263   </params>
264 </methodCall>
265
266 EOT;
267
268                 Logger::debug('wppost: data: ' . $xml);
269
270                 if ($wp_blog !== 'test') {
271                         $x = DI::httpClient()->post($wp_blog, $xml)->getBody();
272                 }
273                 Logger::info('posted to wordpress: ' . (($x) ? $x : ''));
274         }
275 }