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\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Database\DBA;
17 use Friendica\Util\Network;
18 use Friendica\Util\Strings;
19 use Friendica\Util\XML;
21 function wppost_install()
23 Hook::register('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
24 Hook::register('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
25 Hook::register('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
26 Hook::register('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
27 Hook::register('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
28 Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
31 function wppost_uninstall()
33 Hook::unregister('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
34 Hook::unregister('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
35 Hook::unregister('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
36 Hook::unregister('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
37 Hook::unregister('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
38 Hook::unregister('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
41 Hook::unregister('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
42 Hook::unregister('addon_settings', 'addon/wppost/wppost.php', 'wppost_settings');
43 Hook::unregister('addon_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
47 function wppost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
53 if (DI::pConfig()->get(local_user(),'wppost','post')) {
58 L10n::t('Post to Wordpress'),
59 DI::pConfig()->get(local_user(),'wppost','post_by_default')
66 function wppost_settings(&$a,&$s) {
71 /* Add our stylesheet to the page so we can make our settings look nice */
73 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
75 /* Get the current state of our config variables */
77 $enabled = DI::pConfig()->get(local_user(),'wppost','post');
78 $checked = (($enabled) ? ' checked="checked" ' : '');
80 $css = (($enabled) ? '' : '-disabled');
82 $def_enabled = DI::pConfig()->get(local_user(),'wppost','post_by_default');
83 $back_enabled = DI::pConfig()->get(local_user(),'wppost','backlink');
84 $shortcheck_enabled = DI::pConfig()->get(local_user(),'wppost','shortcheck');
86 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
87 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
88 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
90 $wp_username = DI::pConfig()->get(local_user(), 'wppost', 'wp_username');
91 $wp_password = DI::pConfig()->get(local_user(), 'wppost', 'wp_password');
92 $wp_blog = DI::pConfig()->get(local_user(), 'wppost', 'wp_blog');
93 $wp_backlink_text = DI::pConfig()->get(local_user(), 'wppost', 'wp_backlink_text');
96 /* Add some HTML to the existing form */
98 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
99 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
101 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
102 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
103 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
105 $s .= '<div id="wppost-enable-wrapper">';
106 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . L10n::t('Enable WordPress Post Addon') . '</label>';
107 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
108 $s .= '</div><div class="clear"></div>';
110 $s .= '<div id="wppost-username-wrapper">';
111 $s .= '<label id="wppost-username-label" for="wppost-username">' . L10n::t('WordPress username') . '</label>';
112 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
113 $s .= '</div><div class="clear"></div>';
115 $s .= '<div id="wppost-password-wrapper">';
116 $s .= '<label id="wppost-password-label" for="wppost-password">' . L10n::t('WordPress password') . '</label>';
117 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
118 $s .= '</div><div class="clear"></div>';
120 $s .= '<div id="wppost-blog-wrapper">';
121 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . L10n::t('WordPress API URL') . '</label>';
122 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
123 $s .= '</div><div class="clear"></div>';
125 $s .= '<div id="wppost-bydefault-wrapper">';
126 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . L10n::t('Post to WordPress by default') . '</label>';
127 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
128 $s .= '</div><div class="clear"></div>';
130 $s .= '<div id="wppost-backlink-wrapper">';
131 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . L10n::t('Provide a backlink to the Friendica post') . '</label>';
132 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
133 $s .= '</div><div class="clear"></div>';
134 $s .= '<div id="wppost-backlinktext-wrapper">';
135 $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>';
136 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
137 $s .= '</div><div class="clear"></div>';
139 $s .= '<div id="wppost-shortcheck-wrapper">';
140 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . L10n::t("Don't post messages that are too short") . '</label>';
141 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
142 $s .= '</div><div class="clear"></div>';
144 /* provide a submit button */
146 $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>';
151 function wppost_settings_post(&$a,&$b) {
153 if(!empty($_POST['wppost-submit'])) {
154 DI::pConfig()->set(local_user(),'wppost','post',intval($_POST['wppost']));
155 DI::pConfig()->set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault'] ?? false));
156 DI::pConfig()->set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
157 DI::pConfig()->set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
158 DI::pConfig()->set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
159 DI::pConfig()->set(local_user(),'wppost','backlink',trim($_POST['wp_backlink'] ?? ''));
160 DI::pConfig()->set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
161 $wp_backlink_text = Strings::escapeTags(trim($_POST['wp_backlink_text']));
162 $wp_backlink_text = BBCode::convert($wp_backlink_text, false, 8);
163 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
164 DI::pConfig()->set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
169 function wppost_hook_fork(&$a, &$b)
171 if ($b['name'] != 'notifier_normal') {
177 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
178 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
179 $b['execute'] = false;
184 function wppost_post_local(&$a, &$b) {
186 // This can probably be changed to allow editing by pointing to a different API endpoint
192 if (!local_user() || (local_user() != $b['uid'])) {
196 if ($b['private'] || $b['parent']) {
200 $wp_post = intval(DI::pConfig()->get(local_user(),'wppost','post'));
202 $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
204 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'wppost','post_by_default'))) {
212 if (strlen($b['postopts'])) {
213 $b['postopts'] .= ',';
216 $b['postopts'] .= 'wppost';
222 function wppost_send(&$a, &$b)
224 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
228 if(! strstr($b['postopts'],'wppost')) {
232 if($b['parent'] != $b['id']) {
236 // Dont't post if the post doesn't belong to us.
237 // This is a check for forum postings
238 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
239 if ($b['contact-id'] != $self['id']) {
243 $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
244 $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
245 $wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog');
246 $wp_backlink_text = DI::pConfig()->get($b['uid'],'wppost','wp_backlink_text');
247 if ($wp_backlink_text == '') {
248 $wp_backlink_text = L10n::t('Read the original post and comment stream on Friendica');
251 if ($wp_username && $wp_password && $wp_blog) {
252 $wptitle = trim($b['title']);
254 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
255 // Checking, if its a post that is worth a blog post
257 $siteinfo = BBCode::getAttachedData($b["body"]);
259 // Is it a link to an aricle, a video or a photo?
260 if (isset($siteinfo["type"])) {
261 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
266 // Does it have a title?
267 if ($wptitle != "") {
271 // Is it larger than 500 characters?
272 if (strlen($b['body']) > 500) {
281 // If the title is empty then try to guess
282 if ($wptitle == '') {
283 // Fetch information about the post
284 $siteinfo = BBCode::getAttachedData($b["body"]);
285 if (isset($siteinfo["title"])) {
286 $wptitle = $siteinfo["title"];
289 // If no bookmark is found then take the first line
290 if ($wptitle == '') {
291 // Remove the share element before fetching the first line
292 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
294 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
295 $pos = strpos($title, "\n");
297 if (($pos == 0) || ($pos > 100)) {
302 $wptitle = substr($title, 0, $pos).$trailer;
306 $title = '<title>' . (($wptitle) ? $wptitle : L10n::t('Post from Friendica')) . '</title>';
307 $post = BBCode::convert($b['body'], false, 4);
309 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
310 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
311 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
313 $post = $title.$post;
315 $wp_backlink = intval(DI::pConfig()->get($b['uid'],'wppost','backlink'));
316 if($wp_backlink && $b['plink']) {
317 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
318 . $wp_backlink_text . '</a>' . EOL . EOL;
321 $post = XML::escape($post);
325 <?xml version=\"1.0\" encoding=\"utf-8\"?>
327 <methodName>blogger.newPost</methodName>
329 <param><value><string/></value></param>
330 <param><value><string/></value></param>
331 <param><value><string>$wp_username</string></value></param>
332 <param><value><string>$wp_password</string></value></param>
333 <param><value><string>$post</string></value></param>
334 <param><value><int>1</int></value></param>
340 Logger::log('wppost: data: ' . $xml, Logger::DATA);
342 if ($wp_blog !== 'test') {
343 $x = Network::post($wp_blog, $xml)->getBody();
345 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);