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