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