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