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