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