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