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