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