]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
2f09a258c856a7efbe1d80641fbe273f6f80e0d4
[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\Addon;
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         Addon::registerHook('hook_fork',            'addon/wppost/wppost.php', 'wppost_hook_fork');
23         Addon::registerHook('post_local',           'addon/wppost/wppost.php', 'wppost_post_local');
24         Addon::registerHook('notifier_normal',      'addon/wppost/wppost.php', 'wppost_send');
25         Addon::registerHook('jot_networks',         'addon/wppost/wppost.php', 'wppost_jot_nets');
26         Addon::registerHook('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
27         Addon::registerHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
28 }
29
30 function wppost_uninstall()
31 {
32         Addon::unregisterHook('hook_fork',        'addon/wppost/wppost.php', 'wppost_hook_fork');
33         Addon::unregisterHook('post_local',       'addon/wppost/wppost.php', 'wppost_post_local');
34         Addon::unregisterHook('notifier_normal',  'addon/wppost/wppost.php', 'wppost_send');
35         Addon::unregisterHook('jot_networks',     'addon/wppost/wppost.php', 'wppost_jot_nets');
36         Addon::unregisterHook('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
37         Addon::unregisterHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
38
39         // obsolete - remove
40         Addon::unregisterHook('post_local_end',   'addon/wppost/wppost.php', 'wppost_send');
41         Addon::unregisterHook('addon_settings',  'addon/wppost/wppost.php', 'wppost_settings');
42         Addon::unregisterHook('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(x($_POST,'wppost-submit')) {
150
151                 PConfig::set(local_user(),'wppost','post',intval($_POST['wppost']));
152                 PConfig::set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
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($_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
167 function wppost_hook_fork(&$a, &$b)
168 {
169         if ($b['name'] != 'notifier_normal') {
170                 return;
171         }
172
173         $post = $b['data'];
174
175         if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
176                 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
177                 $b['execute'] = false;
178                 return;
179         }
180 }
181
182 function wppost_post_local(&$a, &$b) {
183
184         // This can probably be changed to allow editing by pointing to a different API endpoint
185
186         if ($b['edit']) {
187                 return;
188         }
189
190         if (!local_user() || (local_user() != $b['uid'])) {
191                 return;
192         }
193
194         if ($b['private'] || $b['parent']) {
195                 return;
196         }
197
198         $wp_post   = intval(PConfig::get(local_user(),'wppost','post'));
199
200         $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
201
202         if ($b['api_source'] && intval(PConfig::get(local_user(),'wppost','post_by_default'))) {
203                 $wp_enable = 1;
204         }
205
206         if (!$wp_enable) {
207                 return;
208         }
209
210         if (strlen($b['postopts'])) {
211                 $b['postopts'] .= ',';
212         }
213
214         $b['postopts'] .= 'wppost';
215 }
216
217
218
219
220 function wppost_send(&$a,&$b) {
221
222         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
223                 return;
224         }
225
226         if(! strstr($b['postopts'],'wppost')) {
227                 return;
228         }
229
230         if($b['parent'] != $b['id']) {
231                 return;
232         }
233
234         // Dont't post if the post doesn't belong to us.
235         // This is a check for forum postings
236         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
237         if ($b['contact-id'] != $self['id']) {
238                 return;
239         }
240
241         $wp_username = XML::escape(PConfig::get($b['uid'], 'wppost', 'wp_username'));
242         $wp_password = XML::escape(PConfig::get($b['uid'], 'wppost',' wp_password'));
243         $wp_blog = PConfig::get($b['uid'],'wppost','wp_blog');
244         $wp_backlink_text = PConfig::get($b['uid'],'wppost','wp_backlink_text');
245         if ($wp_backlink_text == '') {
246                 $wp_backlink_text = L10n::t('Read the orig­i­nal post and com­ment stream on Friendica');
247         }
248
249         if ($wp_username && $wp_password && $wp_blog) {
250                 $wptitle = trim($b['title']);
251
252                 if (intval(PConfig::get($b['uid'], 'wppost', 'shortcheck'))) {
253                         // Checking, if its a post that is worth a blog post
254                         $postentry = false;
255                         $siteinfo = BBCode::getAttachedData($b["body"]);
256
257                         // Is it a link to an aricle, a video or a photo?
258                         if (isset($siteinfo["type"])) {
259                                 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
260                                         $postentry = true;
261                                 }
262                         }
263
264                         // Does it have a title?
265                         if ($wptitle != "") {
266                                 $postentry = true;
267                         }
268
269                         // Is it larger than 500 characters?
270                         if (strlen($b['body']) > 500) {
271                                 $postentry = true;
272                         }
273
274                         if (!$postentry) {
275                                 return;
276                         }
277                 }
278
279                 // If the title is empty then try to guess
280                 if ($wptitle == '') {
281                         // Fetch information about the post
282                         $siteinfo = BBCode::getAttachedData($b["body"]);
283                         if (isset($siteinfo["title"])) {
284                                 $wptitle = $siteinfo["title"];
285                         }
286
287                         // If no bookmark is found then take the first line
288                         if ($wptitle == '') {
289                                 // Remove the share element before fetching the first line
290                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
291
292                                 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
293                                 $pos = strpos($title, "\n");
294                                 $trailer = "";
295                                 if (($pos == 0) || ($pos > 100)) {
296                                         $pos = 100;
297                                         $trailer = "...";
298                                 }
299
300                                 $wptitle = substr($title, 0, $pos).$trailer;
301                         }
302                 }
303
304                 $title = '<title>' . (($wptitle) ? $wptitle : L10n::t('Post from Friendica')) . '</title>';
305                 $post = BBCode::convert($b['body'], false, 4);
306
307                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
308                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
309                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
310
311                 $post = $title.$post;
312
313                 $wp_backlink = intval(PConfig::get($b['uid'],'wppost','backlink'));
314                 if($wp_backlink && $b['plink']) {
315                         $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
316                                 . $wp_backlink_text . '</a>' . EOL . EOL;
317                 }
318
319                 $post = XML::escape($post);
320
321
322                 $xml = <<< EOT
323 <?xml version=\"1.0\" encoding=\"utf-8\"?>
324 <methodCall>
325   <methodName>blogger.newPost</methodName>
326   <params>
327     <param><value><string/></value></param>
328     <param><value><string/></value></param>
329     <param><value><string>$wp_username</string></value></param>
330     <param><value><string>$wp_password</string></value></param>
331     <param><value><string>$post</string></value></param>
332     <param><value><int>1</int></value></param>
333   </params>
334 </methodCall>
335
336 EOT;
337
338                 Logger::log('wppost: data: ' . $xml, Logger::DATA);
339
340                 if ($wp_blog !== 'test') {
341                         $x = Network::post($wp_blog, $xml)->getBody();
342                 }
343                 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);
344         }
345 }