"remote self" should work now with the addons
[friendica-addons.git] / wppost / wppost.php
1 <?php
2
3 /**
4  * Name: WordPress Post Connector
5  * Description: Post to WordPress (or anything else which uses blogger XMLRPC API)
6  * Version: 1.1
7  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8  */
9
10 function wppost_install() {
11     register_hook('post_local',           'addon/wppost/wppost.php', 'wppost_post_local');
12     register_hook('notifier_normal',      'addon/wppost/wppost.php', 'wppost_send');
13     register_hook('jot_networks',         'addon/wppost/wppost.php', 'wppost_jot_nets');
14     register_hook('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
15     register_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
16
17 }
18 function wppost_uninstall() {
19     unregister_hook('post_local',       'addon/wppost/wppost.php', 'wppost_post_local');
20     unregister_hook('notifier_normal',  'addon/wppost/wppost.php', 'wppost_send');
21     unregister_hook('jot_networks',     'addon/wppost/wppost.php', 'wppost_jot_nets');
22     unregister_hook('connector_settings',      'addon/wppost/wppost.php', 'wppost_settings');
23     unregister_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
24
25         // obsolete - remove
26     unregister_hook('post_local_end',   'addon/wppost/wppost.php', 'wppost_send');
27     unregister_hook('plugin_settings',  'addon/wppost/wppost.php', 'wppost_settings');
28     unregister_hook('plugin_settings_post',  'addon/wppost/wppost.php', 'wppost_settings_post');
29
30 }
31
32
33 function wppost_jot_nets(&$a,&$b) {
34     if(! local_user())
35         return;
36
37     $wp_post = get_pconfig(local_user(),'wppost','post');
38     if(intval($wp_post) == 1) {
39         $wp_defpost = get_pconfig(local_user(),'wppost','post_by_default');
40         $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
41         $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable" ' . $selected . ' value="1" /> '
42             . t('Post to Wordpress') . '</div>';
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         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
55
56         /* Get the current state of our config variables */
57
58         $enabled = get_pconfig(local_user(),'wppost','post');
59         $checked = (($enabled) ? ' checked="checked" ' : '');
60
61         $css = (($enabled) ? '' : '-disabled');
62
63         $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
64         $back_enabled = get_pconfig(local_user(),'wppost','backlink');
65         $shortcheck_enabled = get_pconfig(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 = get_pconfig(local_user(), 'wppost', 'wp_username');
72         $wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
73         $wp_blog = get_pconfig(local_user(), 'wppost', 'wp_blog');
74         $wp_backlink_text = get_pconfig(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">'. 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">'. t('Wordpress Export').'</h3>';
85     $s .= '</span>';
86     $s .= '<div id="wppost-enable-wrapper">';
87     $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
88     $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
89     $s .= '</div><div class="clear"></div>';
90
91     $s .= '<div id="wppost-username-wrapper">';
92     $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
93     $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
94     $s .= '</div><div class="clear"></div>';
95
96     $s .= '<div id="wppost-password-wrapper">';
97     $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
98     $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
99     $s .= '</div><div class="clear"></div>';
100
101     $s .= '<div id="wppost-blog-wrapper">';
102     $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
103     $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
104     $s .= '</div><div class="clear"></div>';
105
106     $s .= '<div id="wppost-bydefault-wrapper">';
107     $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
108     $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
109     $s .= '</div><div class="clear"></div>';
110
111     $s .= '<div id="wppost-backlink-wrapper">';
112     $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . t('Provide a backlink to the Friendica post') . '</label>';
113     $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
114     $s .= '</div><div class="clear"></div>';
115     $s .= '<div id="wppost-backlinktext-wrapper">';
116     $s .= '<label id="wppost-backlinktext-label" for="wp_backlink_text">' . t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.') . '</label>';
117     $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
118     $s .= '</div><div class="clear"></div>';
119
120     $s .= '<div id="wppost-shortcheck-wrapper">';
121     $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . t("Don't post messages that are too short") . '</label>';
122     $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
123     $s .= '</div><div class="clear"></div>';
124
125     /* provide a submit button */
126
127     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
128
129 }
130
131
132 function wppost_settings_post(&$a,&$b) {
133
134         if(x($_POST,'wppost-submit')) {
135
136                 set_pconfig(local_user(),'wppost','post',intval($_POST['wppost']));
137                 set_pconfig(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
138                 set_pconfig(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
139                 set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
140                 set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
141                 set_pconfig(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
142                 set_pconfig(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
143                 $wp_backlink_text = notags(trim($_POST['wp_backlink_text']));
144                 $wp_backlink_text = bbcode($wp_backlink_text, false, false, 8);
145                 $wp_backlink_text = html2plain($wp_backlink_text, 0, true);
146                 set_pconfig(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
147
148         }
149
150 }
151
152 function wppost_post_local(&$a, &$b) {
153
154         // This can probably be changed to allow editing by pointing to a different API endpoint
155
156         if ($b['edit']) {
157                 return;
158         }
159
160         if (!local_user() || (local_user() != $b['uid'])) {
161                 return;
162         }
163
164         if ($b['private'] || $b['parent']) {
165                 return;
166         }
167
168         $wp_post   = intval(get_pconfig(local_user(),'wppost','post'));
169
170         $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
171
172         if ($b['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default'))) {
173                 $wp_enable = 1;
174         }
175
176         if (!$wp_enable) {
177                 return;
178         }
179
180         if (strlen($b['postopts'])) {
181                 $b['postopts'] .= ',';
182         }
183
184         $b['postopts'] .= 'wppost';
185 }
186
187
188
189
190 function wppost_send(&$a,&$b) {
191
192         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
193                 return;
194
195         if(! strstr($b['postopts'],'wppost'))
196                 return;
197
198         if($b['parent'] != $b['id'])
199                 return;
200
201
202         $wp_username = xmlify(get_pconfig($b['uid'],'wppost','wp_username'));
203         $wp_password = xmlify(get_pconfig($b['uid'],'wppost','wp_password'));
204         $wp_blog = get_pconfig($b['uid'],'wppost','wp_blog');
205         $wp_backlink_text = get_pconfig($b['uid'],'wppost','wp_backlink_text');
206         if ($wp_backlink_text == '') {
207                 $wp_backlink_text = t('Read the orig­i­nal post and com­ment stream on Friendica');
208         }
209
210         if($wp_username && $wp_password && $wp_blog) {
211
212                 require_once('include/bbcode.php');
213                 require_once('include/html2plain.php');
214                 require_once('include/plaintext.php');
215
216                 $wptitle = trim($b['title']);
217
218                 if (intval(get_pconfig($b['uid'],'wppost','shortcheck'))) {
219                         // Checking, if its a post that is worth a blog post
220                         $postentry = false;
221                         $siteinfo = get_attached_data($b["body"]);
222
223                         // Is it a link to an aricle, a video or a photo?
224                         if (isset($siteinfo["type"])) {
225                                 if (in_array($siteinfo["type"], array("link", "audio", "video", "photo"))) {
226                                         $postentry = true;
227                                 }
228                         }
229
230                         // Does it have a title?
231                         if ($wptitle != "") {
232                                 $postentry = true;
233                         }
234
235                         // Is it larger than 500 characters?
236                         if (strlen($b['body']) > 500) {
237                                 $postentry = true;
238                         }
239
240                         if (!$postentry) {
241                                 return;
242                         }
243                 }
244
245                 // If the title is empty then try to guess
246                 if ($wptitle == '') {
247                         // Fetch information about the post
248                         $siteinfo = get_attached_data($b["body"]);
249                         if (isset($siteinfo["title"])) {
250                                 $wptitle = $siteinfo["title"];
251                         }
252
253                         // If no bookmark is found then take the first line
254                         if ($wptitle == '') {
255                                 // Remove the share element before fetching the first line
256                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
257
258                                 $title = html2plain(bbcode($title, false, false), 0, true)."\n";
259                                 $pos = strpos($title, "\n");
260                                 $trailer = "";
261                                 if (($pos == 0) || ($pos > 100)) {
262                                         $pos = 100;
263                                         $trailer = "...";
264                                 }
265
266                                 $wptitle = substr($title, 0, $pos).$trailer;
267                         }
268                 }
269
270                 $title = '<title>' . (($wptitle) ? $wptitle : t('Post from Friendica')) . '</title>';
271                 $post = bbcode($b['body'], false, false, 4);
272
273                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
274                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
275                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
276
277                 $post = $title.$post;
278
279                 $wp_backlink = intval(get_pconfig($b['uid'],'wppost','backlink'));
280                 if($wp_backlink && $b['plink']) {
281                         $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
282                                 . $wp_backlink_text . '</a>' . EOL . EOL;
283                 }
284
285                 $post = xmlify($post);
286
287
288                 $xml = <<< EOT
289 <?xml version=\"1.0\" encoding=\"utf-8\"?>
290 <methodCall>
291   <methodName>blogger.newPost</methodName>
292   <params>
293     <param><value><string/></value></param>
294     <param><value><string/></value></param>
295     <param><value><string>$wp_username</string></value></param>
296     <param><value><string>$wp_password</string></value></param>
297     <param><value><string>$post</string></value></param>
298     <param><value><int>1</int></value></param>
299   </params>
300 </methodCall>
301
302 EOT;
303
304                 logger('wppost: data: ' . $xml, LOGGER_DATA);
305
306                 if($wp_blog !== 'test') {
307                         $x = post_url($wp_blog,$xml);
308                 }
309                 logger('posted to wordpress: ' . (($x) ? $x : ''), LOGGER_DEBUG);
310
311         }
312 }