]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
[various] Remove App dependency from hook functions
[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\App;
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Content\Text\HTML;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Logger;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
16 use Friendica\DI;
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 ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
110                 !strstr($post['postopts'] ?? '', 'wppost') || ($post['parent'] != $post['id'])) {
111                 $b['execute'] = false;
112                 return;
113         }
114 }
115
116 function wppost_post_local(array &$b) {
117
118         // This can probably be changed to allow editing by pointing to a different API endpoint
119
120         if ($b['edit']) {
121                 return;
122         }
123
124         if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
125                 return;
126         }
127
128         if ($b['private'] || $b['parent']) {
129                 return;
130         }
131
132         $wp_post   = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post'));
133
134         $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
135
136         if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default'))) {
137                 $wp_enable = 1;
138         }
139
140         if (!$wp_enable) {
141                 return;
142         }
143
144         if (strlen($b['postopts'])) {
145                 $b['postopts'] .= ',';
146         }
147
148         $b['postopts'] .= 'wppost';
149 }
150
151
152
153
154 function wppost_send(array &$b)
155 {
156         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
157                 return;
158         }
159
160         if(! strstr($b['postopts'],'wppost')) {
161                 return;
162         }
163
164         if($b['parent'] != $b['id']) {
165                 return;
166         }
167
168         // Dont't post if the post doesn't belong to us.
169         // This is a check for forum postings
170         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
171         if ($b['contact-id'] != $self['id']) {
172                 return;
173         }
174
175         $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
176
177         $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
178         $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
179         $wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog');
180         $wp_backlink_text = DI::pConfig()->get($b['uid'],'wppost','wp_backlink_text');
181         if ($wp_backlink_text == '') {
182                 $wp_backlink_text = DI::l10n()->t('Read the orig­i­nal post and com­ment stream on Friendica');
183         }
184
185         if ($wp_username && $wp_password && $wp_blog) {
186                 $wptitle = trim($b['title']);
187
188                 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
189                         // Checking, if its a post that is worth a blog post
190                         $postentry = false;
191                         $siteinfo = BBCode::getAttachedData($b["body"]);
192
193                         // Is it a link to an aricle, a video or a photo?
194                         if (isset($siteinfo["type"])) {
195                                 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
196                                         $postentry = true;
197                                 }
198                         }
199
200                         // Does it have a title?
201                         if ($wptitle != "") {
202                                 $postentry = true;
203                         }
204
205                         // Is it larger than 500 characters?
206                         if (strlen($b['body']) > 500) {
207                                 $postentry = true;
208                         }
209
210                         if (!$postentry) {
211                                 return;
212                         }
213                 }
214
215                 // If the title is empty then try to guess
216                 if ($wptitle == '') {
217                         // Fetch information about the post
218                         $siteinfo = BBCode::getAttachedData($b["body"]);
219                         if (isset($siteinfo["title"])) {
220                                 $wptitle = $siteinfo["title"];
221                         }
222
223                         // If no bookmark is found then take the first line
224                         if ($wptitle == '') {
225                                 // Remove the share element before fetching the first line
226                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism", "\n$1\n", $b['body']));
227
228                                 $title = BBCode::toPlaintext($title)."\n";
229                                 $pos = strpos($title, "\n");
230                                 $trailer = "";
231                                 if (($pos == 0) || ($pos > 100)) {
232                                         $pos = 100;
233                                         $trailer = "...";
234                                 }
235
236                                 $wptitle = substr($title, 0, $pos).$trailer;
237                         }
238                 }
239
240                 $title = '<title>' . (($wptitle) ? $wptitle : DI::l10n()->t('Post from Friendica')) . '</title>';
241                 $post = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
242
243                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
244                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
245                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
246
247                 $post = $title.$post;
248
249                 $wp_backlink = intval(DI::pConfig()->get($b['uid'],'wppost','backlink'));
250                 if($wp_backlink && $b['plink']) {
251                         $post .= '<p><a href="' . $b['plink'] . '">' . $wp_backlink_text . '</a></p>';
252                 }
253
254                 $post = XML::escape($post);
255
256
257                 $xml = <<< EOT
258 <?xml version=\"1.0\" encoding=\"utf-8\"?>
259 <methodCall>
260   <methodName>blogger.newPost</methodName>
261   <params>
262     <param><value><string/></value></param>
263     <param><value><string/></value></param>
264     <param><value><string>$wp_username</string></value></param>
265     <param><value><string>$wp_password</string></value></param>
266     <param><value><string>$post</string></value></param>
267     <param><value><int>1</int></value></param>
268   </params>
269 </methodCall>
270
271 EOT;
272
273                 Logger::debug('wppost: data: ' . $xml);
274
275                 if ($wp_blog !== 'test') {
276                         $x = DI::httpClient()->post($wp_blog, $xml)->getBody();
277                 }
278                 Logger::info('posted to wordpress: ' . (($x) ? $x : ''));
279         }
280 }