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