Removed commented stuff
[friendica-addons.git/.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\Database\DBA;
14 use Friendica\DI;
15 use Friendica\Util\Strings;
16 use Friendica\Util\XML;
17
18 function wppost_install()
19 {
20         Hook::register('hook_fork',            'addon/wppost/wppost.php', 'wppost_hook_fork');
21         Hook::register('post_local',           'addon/wppost/wppost.php', 'wppost_post_local');
22         Hook::register('notifier_normal',      'addon/wppost/wppost.php', 'wppost_send');
23         Hook::register('jot_networks',         'addon/wppost/wppost.php', 'wppost_jot_nets');
24         Hook::register('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
25         Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
26 }
27
28 function wppost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
29 {
30         if (!local_user()) {
31                 return;
32         }
33
34         if (DI::pConfig()->get(local_user(),'wppost','post')) {
35                 $jotnets_fields[] = [
36                         'type' => 'checkbox',
37                         'field' => [
38                                 'wppost_enable',
39                                 DI::l10n()->t('Post to Wordpress'),
40                                 DI::pConfig()->get(local_user(),'wppost','post_by_default')
41                         ]
42                 ];
43         }
44 }
45
46
47 function wppost_settings(&$a, &$s) {
48
49         if(! local_user())
50                 return;
51
52         /* Add our stylesheet to the page so we can make our settings look nice */
53
54         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
55
56         /* Get the current state of our config variables */
57
58         $enabled = DI::pConfig()->get(local_user(),'wppost','post');
59         $checked = (($enabled) ? ' checked="checked" ' : '');
60
61         $css = (($enabled) ? '' : '-disabled');
62
63         $def_enabled = DI::pConfig()->get(local_user(),'wppost','post_by_default');
64         $back_enabled = DI::pConfig()->get(local_user(),'wppost','backlink');
65         $shortcheck_enabled = DI::pConfig()->get(local_user(),'wppost','shortcheck');
66
67         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
68         $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
69         $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
70
71         $wp_username = DI::pConfig()->get(local_user(), 'wppost', 'wp_username');
72         $wp_password = DI::pConfig()->get(local_user(), 'wppost', 'wp_password');
73         $wp_blog = DI::pConfig()->get(local_user(), 'wppost', 'wp_blog');
74         $wp_backlink_text = DI::pConfig()->get(local_user(), 'wppost', 'wp_backlink_text');
75
76
77     /* Add some HTML to the existing form */
78
79     $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
80     $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. DI::l10n()->t('Wordpress Export').'</h3>';
81     $s .= '</span>';
82     $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
83     $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
84     $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. DI::l10n()->t('Wordpress Export').'</h3>';
85     $s .= '</span>';
86     $s .= '<div id="wppost-enable-wrapper">';
87     $s .= '<label id="wppost-enable-label" for="wppost-enable">' . DI::l10n()->t('Enable WordPress Post Addon') . '</label>';
88         $s .= '<input type="hidden" name="wppost" value="0"/>';
89     $s .= '<input id="wppost-enable" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
90     $s .= '</div><div class="clear"></div>';
91
92     $s .= '<div id="wppost-username-wrapper">';
93     $s .= '<label id="wppost-username-label" for="wppost-username">' . DI::l10n()->t('WordPress username') . '</label>';
94     $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
95     $s .= '</div><div class="clear"></div>';
96
97     $s .= '<div id="wppost-password-wrapper">';
98     $s .= '<label id="wppost-password-label" for="wppost-password">' . DI::l10n()->t('WordPress password') . '</label>';
99     $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
100     $s .= '</div><div class="clear"></div>';
101
102     $s .= '<div id="wppost-blog-wrapper">';
103     $s .= '<label id="wppost-blog-label" for="wppost-blog">' . DI::l10n()->t('WordPress API URL') . '</label>';
104     $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
105     $s .= '</div><div class="clear"></div>';
106
107     $s .= '<div id="wppost-bydefault-wrapper">';
108     $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . DI::l10n()->t('Post to WordPress by default') . '</label>';
109         $s .= '<input type="hidden" name="wp_bydefault" value="0"/>';
110     $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
111     $s .= '</div><div class="clear"></div>';
112
113     $s .= '<div id="wppost-backlink-wrapper">';
114     $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . DI::l10n()->t('Provide a backlink to the Friendica post') . '</label>';
115         $s .= '<input type="hidden" name="wp_backlink" value="0"/>';
116     $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
117     $s .= '</div><div class="clear"></div>';
118     $s .= '<div id="wppost-backlinktext-wrapper">';
119     $s .= '<label id="wppost-backlinktext-label" for="wp_backlink_text">' . DI::l10n()->t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.') . '</label>';
120     $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
121     $s .= '</div><div class="clear"></div>';
122
123     $s .= '<div id="wppost-shortcheck-wrapper">';
124     $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . DI::l10n()->t("Don't post messages that are too short") . '</label>';
125     $s .= '<input type="hidden" name="wp_shortcheck" value="0"/>';
126     $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
127     $s .= '</div><div class="clear"></div>';
128
129     /* provide a submit button */
130
131     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
132
133 }
134
135
136 function wppost_settings_post(&$a, &$b)
137 {
138         if(!empty($_POST['wppost-submit'])) {
139                 DI::pConfig()->set(local_user(), 'wppost', 'post'           , intval($_POST['wppost']));
140                 DI::pConfig()->set(local_user(), 'wppost', 'post_by_default', intval($_POST['wp_bydefault']));
141                 DI::pConfig()->set(local_user(), 'wppost', 'wp_username'    ,   trim($_POST['wp_username']));
142                 DI::pConfig()->set(local_user(), 'wppost', 'wp_password'    ,   trim($_POST['wp_password']));
143                 DI::pConfig()->set(local_user(), 'wppost', 'wp_blog'        ,   trim($_POST['wp_blog']));
144                 DI::pConfig()->set(local_user(), 'wppost', 'backlink'       , intval($_POST['wp_backlink']));
145                 DI::pConfig()->set(local_user(), 'wppost', 'shortcheck'     , intval($_POST['wp_shortcheck']));
146                 $wp_backlink_text = BBCode::convert(trim($_POST['wp_backlink_text']), false, BBCode::BACKLINK);
147                 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
148                 DI::pConfig()->set(local_user(), 'wppost', 'wp_backlink_text', $wp_backlink_text);
149         }
150 }
151
152 function wppost_hook_fork(&$a, &$b)
153 {
154         if ($b['name'] != 'notifier_normal') {
155                 return;
156         }
157
158         $post = $b['data'];
159
160         if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
161                 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
162                 $b['execute'] = false;
163                 return;
164         }
165 }
166
167 function wppost_post_local(&$a, &$b) {
168
169         // This can probably be changed to allow editing by pointing to a different API endpoint
170
171         if ($b['edit']) {
172                 return;
173         }
174
175         if (!local_user() || (local_user() != $b['uid'])) {
176                 return;
177         }
178
179         if ($b['private'] || $b['parent']) {
180                 return;
181         }
182
183         $wp_post   = intval(DI::pConfig()->get(local_user(), 'wppost', 'post'));
184
185         $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
186
187         if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'wppost', 'post_by_default'))) {
188                 $wp_enable = 1;
189         }
190
191         if (!$wp_enable) {
192                 return;
193         }
194
195         if (strlen($b['postopts'])) {
196                 $b['postopts'] .= ',';
197         }
198
199         $b['postopts'] .= 'wppost';
200 }
201
202
203
204
205 function wppost_send(&$a, &$b)
206 {
207         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
208                 return;
209         }
210
211         if(! strstr($b['postopts'],'wppost')) {
212                 return;
213         }
214
215         if($b['parent'] != $b['id']) {
216                 return;
217         }
218
219         // Dont't post if the post doesn't belong to us.
220         // This is a check for forum postings
221         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
222         if ($b['contact-id'] != $self['id']) {
223                 return;
224         }
225
226         $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
227         $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
228         $wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog');
229         $wp_backlink_text = DI::pConfig()->get($b['uid'],'wppost','wp_backlink_text');
230         if ($wp_backlink_text == '') {
231                 $wp_backlink_text = DI::l10n()->t('Read the orig­i­nal post and com­ment stream on Friendica');
232         }
233
234         if ($wp_username && $wp_password && $wp_blog) {
235                 $wptitle = trim($b['title']);
236
237                 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
238                         // Checking, if its a post that is worth a blog post
239                         $postentry = false;
240                         $siteinfo = BBCode::getAttachedData($b["body"]);
241
242                         // Is it a link to an aricle, a video or a photo?
243                         if (isset($siteinfo["type"])) {
244                                 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
245                                         $postentry = true;
246                                 }
247                         }
248
249                         // Does it have a title?
250                         if ($wptitle != "") {
251                                 $postentry = true;
252                         }
253
254                         // Is it larger than 500 characters?
255                         if (strlen($b['body']) > 500) {
256                                 $postentry = true;
257                         }
258
259                         if (!$postentry) {
260                                 return;
261                         }
262                 }
263
264                 // If the title is empty then try to guess
265                 if ($wptitle == '') {
266                         // Fetch information about the post
267                         $siteinfo = BBCode::getAttachedData($b["body"]);
268                         if (isset($siteinfo["title"])) {
269                                 $wptitle = $siteinfo["title"];
270                         }
271
272                         // If no bookmark is found then take the first line
273                         if ($wptitle == '') {
274                                 // Remove the share element before fetching the first line
275                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism", "\n$1\n", $b['body']));
276
277                                 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
278                                 $pos = strpos($title, "\n");
279                                 $trailer = "";
280                                 if (($pos == 0) || ($pos > 100)) {
281                                         $pos = 100;
282                                         $trailer = "...";
283                                 }
284
285                                 $wptitle = substr($title, 0, $pos).$trailer;
286                         }
287                 }
288
289                 $title = '<title>' . (($wptitle) ? $wptitle : DI::l10n()->t('Post from Friendica')) . '</title>';
290                 $post = BBCode::convert($b['body'], false, BBCode::CONNECTORS);
291
292                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
293                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
294                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
295
296                 $post = $title.$post;
297
298                 $wp_backlink = intval(DI::pConfig()->get($b['uid'],'wppost','backlink'));
299                 if($wp_backlink && $b['plink']) {
300                         $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
301                                 . $wp_backlink_text . '</a>' . EOL . EOL;
302                 }
303
304                 $post = XML::escape($post);
305
306
307                 $xml = <<< EOT
308 <?xml version=\"1.0\" encoding=\"utf-8\"?>
309 <methodCall>
310   <methodName>blogger.newPost</methodName>
311   <params>
312     <param><value><string/></value></param>
313     <param><value><string/></value></param>
314     <param><value><string>$wp_username</string></value></param>
315     <param><value><string>$wp_password</string></value></param>
316     <param><value><string>$post</string></value></param>
317     <param><value><int>1</int></value></param>
318   </params>
319 </methodCall>
320
321 EOT;
322
323                 Logger::log('wppost: data: ' . $xml, Logger::DATA);
324
325                 if ($wp_blog !== 'test') {
326                         $x = DI::httpRequest()->post($wp_blog, $xml)->getBody();
327                 }
328                 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);
329         }
330 }