3 * Name: WordPress Post Connector
4 * Description: Post to WordPress (or anything else which uses blogger XMLRPC API)
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9 use Friendica\Content\Text\BBCode;
10 use Friendica\Content\Text\HTML;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Database\DBA;
15 use Friendica\Util\Network;
16 use Friendica\Util\Strings;
17 use Friendica\Util\XML;
19 function wppost_install()
21 Hook::register('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
22 Hook::register('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
23 Hook::register('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
24 Hook::register('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
25 Hook::register('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
26 Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
29 function wppost_uninstall()
31 Hook::unregister('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
32 Hook::unregister('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
33 Hook::unregister('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
34 Hook::unregister('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
35 Hook::unregister('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
36 Hook::unregister('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
39 Hook::unregister('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
40 Hook::unregister('addon_settings', 'addon/wppost/wppost.php', 'wppost_settings');
41 Hook::unregister('addon_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
45 function wppost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
51 if (DI::pConfig()->get(local_user(),'wppost','post')) {
56 DI::l10n()->t('Post to Wordpress'),
57 DI::pConfig()->get(local_user(),'wppost','post_by_default')
64 function wppost_settings(&$a,&$s) {
69 /* Add our stylesheet to the page so we can make our settings look nice */
71 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
73 /* Get the current state of our config variables */
75 $enabled = DI::pConfig()->get(local_user(),'wppost','post');
76 $checked = (($enabled) ? ' checked="checked" ' : '');
78 $css = (($enabled) ? '' : '-disabled');
80 $def_enabled = DI::pConfig()->get(local_user(),'wppost','post_by_default');
81 $back_enabled = DI::pConfig()->get(local_user(),'wppost','backlink');
82 $shortcheck_enabled = DI::pConfig()->get(local_user(),'wppost','shortcheck');
84 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
85 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
86 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
88 $wp_username = DI::pConfig()->get(local_user(), 'wppost', 'wp_username');
89 $wp_password = DI::pConfig()->get(local_user(), 'wppost', 'wp_password');
90 $wp_blog = DI::pConfig()->get(local_user(), 'wppost', 'wp_blog');
91 $wp_backlink_text = DI::pConfig()->get(local_user(), 'wppost', 'wp_backlink_text');
94 /* Add some HTML to the existing form */
96 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
97 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. DI::l10n()->t('Wordpress Export').'</h3>';
99 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
100 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
101 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. DI::l10n()->t('Wordpress Export').'</h3>';
103 $s .= '<div id="wppost-enable-wrapper">';
104 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . DI::l10n()->t('Enable WordPress Post Addon') . '</label>';
105 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
106 $s .= '</div><div class="clear"></div>';
108 $s .= '<div id="wppost-username-wrapper">';
109 $s .= '<label id="wppost-username-label" for="wppost-username">' . DI::l10n()->t('WordPress username') . '</label>';
110 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
111 $s .= '</div><div class="clear"></div>';
113 $s .= '<div id="wppost-password-wrapper">';
114 $s .= '<label id="wppost-password-label" for="wppost-password">' . DI::l10n()->t('WordPress password') . '</label>';
115 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
116 $s .= '</div><div class="clear"></div>';
118 $s .= '<div id="wppost-blog-wrapper">';
119 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . DI::l10n()->t('WordPress API URL') . '</label>';
120 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
121 $s .= '</div><div class="clear"></div>';
123 $s .= '<div id="wppost-bydefault-wrapper">';
124 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . DI::l10n()->t('Post to WordPress by default') . '</label>';
125 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
126 $s .= '</div><div class="clear"></div>';
128 $s .= '<div id="wppost-backlink-wrapper">';
129 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . DI::l10n()->t('Provide a backlink to the Friendica post') . '</label>';
130 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
131 $s .= '</div><div class="clear"></div>';
132 $s .= '<div id="wppost-backlinktext-wrapper">';
133 $s .= '<label id="wppost-backlinktext-label" for="wp_backlink_text">' . DI::l10n()->t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.') . '</label>';
134 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
135 $s .= '</div><div class="clear"></div>';
137 $s .= '<div id="wppost-shortcheck-wrapper">';
138 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . DI::l10n()->t("Don't post messages that are too short") . '</label>';
139 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
140 $s .= '</div><div class="clear"></div>';
142 /* provide a submit button */
144 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
149 function wppost_settings_post(&$a,&$b) {
151 if(!empty($_POST['wppost-submit'])) {
152 DI::pConfig()->set(local_user(),'wppost','post',intval($_POST['wppost']));
153 DI::pConfig()->set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault'] ?? false));
154 DI::pConfig()->set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
155 DI::pConfig()->set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
156 DI::pConfig()->set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
157 DI::pConfig()->set(local_user(),'wppost','backlink',trim($_POST['wp_backlink'] ?? ''));
158 DI::pConfig()->set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
159 $wp_backlink_text = Strings::escapeTags(trim($_POST['wp_backlink_text']));
160 $wp_backlink_text = BBCode::convert($wp_backlink_text, false, 8);
161 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
162 DI::pConfig()->set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
167 function wppost_hook_fork(&$a, &$b)
169 if ($b['name'] != 'notifier_normal') {
175 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
176 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
177 $b['execute'] = false;
182 function wppost_post_local(&$a, &$b) {
184 // This can probably be changed to allow editing by pointing to a different API endpoint
190 if (!local_user() || (local_user() != $b['uid'])) {
194 if ($b['private'] || $b['parent']) {
198 $wp_post = intval(DI::pConfig()->get(local_user(),'wppost','post'));
200 $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
202 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'wppost','post_by_default'))) {
210 if (strlen($b['postopts'])) {
211 $b['postopts'] .= ',';
214 $b['postopts'] .= 'wppost';
220 function wppost_send(&$a, &$b)
222 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
226 if(! strstr($b['postopts'],'wppost')) {
230 if($b['parent'] != $b['id']) {
234 // Dont't post if the post doesn't belong to us.
235 // This is a check for forum postings
236 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
237 if ($b['contact-id'] != $self['id']) {
241 $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
242 $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
243 $wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog');
244 $wp_backlink_text = DI::pConfig()->get($b['uid'],'wppost','wp_backlink_text');
245 if ($wp_backlink_text == '') {
246 $wp_backlink_text = DI::l10n()->t('Read the original post and comment stream on Friendica');
249 if ($wp_username && $wp_password && $wp_blog) {
250 $wptitle = trim($b['title']);
252 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
253 // Checking, if its a post that is worth a blog post
255 $siteinfo = BBCode::getAttachedData($b["body"]);
257 // Is it a link to an aricle, a video or a photo?
258 if (isset($siteinfo["type"])) {
259 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
264 // Does it have a title?
265 if ($wptitle != "") {
269 // Is it larger than 500 characters?
270 if (strlen($b['body']) > 500) {
279 // If the title is empty then try to guess
280 if ($wptitle == '') {
281 // Fetch information about the post
282 $siteinfo = BBCode::getAttachedData($b["body"]);
283 if (isset($siteinfo["title"])) {
284 $wptitle = $siteinfo["title"];
287 // If no bookmark is found then take the first line
288 if ($wptitle == '') {
289 // Remove the share element before fetching the first line
290 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
292 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
293 $pos = strpos($title, "\n");
295 if (($pos == 0) || ($pos > 100)) {
300 $wptitle = substr($title, 0, $pos).$trailer;
304 $title = '<title>' . (($wptitle) ? $wptitle : DI::l10n()->t('Post from Friendica')) . '</title>';
305 $post = BBCode::convert($b['body'], false, 4);
307 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
308 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
309 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
311 $post = $title.$post;
313 $wp_backlink = intval(DI::pConfig()->get($b['uid'],'wppost','backlink'));
314 if($wp_backlink && $b['plink']) {
315 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
316 . $wp_backlink_text . '</a>' . EOL . EOL;
319 $post = XML::escape($post);
323 <?xml version=\"1.0\" encoding=\"utf-8\"?>
325 <methodName>blogger.newPost</methodName>
327 <param><value><string/></value></param>
328 <param><value><string/></value></param>
329 <param><value><string>$wp_username</string></value></param>
330 <param><value><string>$wp_password</string></value></param>
331 <param><value><string>$post</string></value></param>
332 <param><value><int>1</int></value></param>
338 Logger::log('wppost: data: ' . $xml, Logger::DATA);
340 if ($wp_blog !== 'test') {
341 $x = Network::post($wp_blog, $xml)->getBody();
343 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);