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\Strings;
16 use Friendica\Util\XML;
18 function wppost_install()
20 Hook::register('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
21 Hook::register('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
22 Hook::register('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
23 Hook::register('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
24 Hook::register('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
25 Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
28 function wppost_jot_nets(\Friendica\App &$a, array &$jotnets_fields)
34 if (DI::pConfig()->get(local_user(),'wppost','post')) {
39 DI::l10n()->t('Post to Wordpress'),
40 DI::pConfig()->get(local_user(),'wppost','post_by_default')
47 function wppost_settings(&$a,&$s) {
52 /* Add our stylesheet to the page so we can make our settings look nice */
54 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
56 /* Get the current state of our config variables */
58 $enabled = DI::pConfig()->get(local_user(),'wppost','post');
59 $checked = (($enabled) ? ' checked="checked" ' : '');
61 $css = (($enabled) ? '' : '-disabled');
63 $def_enabled = DI::pConfig()->get(local_user(),'wppost','post_by_default');
64 $back_enabled = DI::pConfig()->get(local_user(),'wppost','backlink');
65 $shortcheck_enabled = DI::pConfig()->get(local_user(),'wppost','shortcheck');
67 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
68 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
69 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
71 $wp_username = DI::pConfig()->get(local_user(), 'wppost', 'wp_username');
72 $wp_password = DI::pConfig()->get(local_user(), 'wppost', 'wp_password');
73 $wp_blog = DI::pConfig()->get(local_user(), 'wppost', 'wp_blog');
74 $wp_backlink_text = DI::pConfig()->get(local_user(), 'wppost', 'wp_backlink_text');
77 /* Add some HTML to the existing form */
79 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
80 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. DI::l10n()->t('Wordpress Export').'</h3>';
82 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
83 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
84 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. DI::l10n()->t('Wordpress Export').'</h3>';
86 $s .= '<div id="wppost-enable-wrapper">';
87 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . DI::l10n()->t('Enable WordPress Post Addon') . '</label>';
88 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
89 $s .= '</div><div class="clear"></div>';
91 $s .= '<div id="wppost-username-wrapper">';
92 $s .= '<label id="wppost-username-label" for="wppost-username">' . DI::l10n()->t('WordPress username') . '</label>';
93 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
94 $s .= '</div><div class="clear"></div>';
96 $s .= '<div id="wppost-password-wrapper">';
97 $s .= '<label id="wppost-password-label" for="wppost-password">' . DI::l10n()->t('WordPress password') . '</label>';
98 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
99 $s .= '</div><div class="clear"></div>';
101 $s .= '<div id="wppost-blog-wrapper">';
102 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . DI::l10n()->t('WordPress API URL') . '</label>';
103 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
104 $s .= '</div><div class="clear"></div>';
106 $s .= '<div id="wppost-bydefault-wrapper">';
107 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . DI::l10n()->t('Post to WordPress by default') . '</label>';
108 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
109 $s .= '</div><div class="clear"></div>';
111 $s .= '<div id="wppost-backlink-wrapper">';
112 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . DI::l10n()->t('Provide a backlink to the Friendica post') . '</label>';
113 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
114 $s .= '</div><div class="clear"></div>';
115 $s .= '<div id="wppost-backlinktext-wrapper">';
116 $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>';
117 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
118 $s .= '</div><div class="clear"></div>';
120 $s .= '<div id="wppost-shortcheck-wrapper">';
121 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . DI::l10n()->t("Don't post messages that are too short") . '</label>';
122 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
123 $s .= '</div><div class="clear"></div>';
125 /* provide a submit button */
127 $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>';
132 function wppost_settings_post(&$a,&$b) {
134 if(!empty($_POST['wppost-submit'])) {
135 DI::pConfig()->set(local_user(),'wppost','post',intval($_POST['wppost']));
136 DI::pConfig()->set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault'] ?? false));
137 DI::pConfig()->set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
138 DI::pConfig()->set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
139 DI::pConfig()->set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
140 DI::pConfig()->set(local_user(),'wppost','backlink',trim($_POST['wp_backlink'] ?? ''));
141 DI::pConfig()->set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
142 $wp_backlink_text = Strings::escapeTags(trim($_POST['wp_backlink_text']));
143 $wp_backlink_text = BBCode::convert($wp_backlink_text, false, BBCode::BACKLINK);
144 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
145 DI::pConfig()->set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
150 function wppost_hook_fork(&$a, &$b)
152 if ($b['name'] != 'notifier_normal') {
158 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
159 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
160 $b['execute'] = false;
165 function wppost_post_local(&$a, &$b) {
167 // This can probably be changed to allow editing by pointing to a different API endpoint
173 if (!local_user() || (local_user() != $b['uid'])) {
177 if ($b['private'] || $b['parent']) {
181 $wp_post = intval(DI::pConfig()->get(local_user(),'wppost','post'));
183 $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
185 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'wppost','post_by_default'))) {
193 if (strlen($b['postopts'])) {
194 $b['postopts'] .= ',';
197 $b['postopts'] .= 'wppost';
203 function wppost_send(&$a, &$b)
205 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
209 if(! strstr($b['postopts'],'wppost')) {
213 if($b['parent'] != $b['id']) {
217 // Dont't post if the post doesn't belong to us.
218 // This is a check for forum postings
219 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
220 if ($b['contact-id'] != $self['id']) {
224 $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
225 $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
226 $wp_blog = DI::pConfig()->get($b['uid'],'wppost','wp_blog');
227 $wp_backlink_text = DI::pConfig()->get($b['uid'],'wppost','wp_backlink_text');
228 if ($wp_backlink_text == '') {
229 $wp_backlink_text = DI::l10n()->t('Read the original post and comment stream on Friendica');
232 if ($wp_username && $wp_password && $wp_blog) {
233 $wptitle = trim($b['title']);
235 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
236 // Checking, if its a post that is worth a blog post
238 $siteinfo = BBCode::getAttachedData($b["body"]);
240 // Is it a link to an aricle, a video or a photo?
241 if (isset($siteinfo["type"])) {
242 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
247 // Does it have a title?
248 if ($wptitle != "") {
252 // Is it larger than 500 characters?
253 if (strlen($b['body']) > 500) {
262 // If the title is empty then try to guess
263 if ($wptitle == '') {
264 // Fetch information about the post
265 $siteinfo = BBCode::getAttachedData($b["body"]);
266 if (isset($siteinfo["title"])) {
267 $wptitle = $siteinfo["title"];
270 // If no bookmark is found then take the first line
271 if ($wptitle == '') {
272 // Remove the share element before fetching the first line
273 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
275 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
276 $pos = strpos($title, "\n");
278 if (($pos == 0) || ($pos > 100)) {
283 $wptitle = substr($title, 0, $pos).$trailer;
287 $title = '<title>' . (($wptitle) ? $wptitle : DI::l10n()->t('Post from Friendica')) . '</title>';
288 $post = BBCode::convert($b['body'], false, BBCode::CONNECTORS);
290 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
291 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
292 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
294 $post = $title.$post;
296 $wp_backlink = intval(DI::pConfig()->get($b['uid'],'wppost','backlink'));
297 if($wp_backlink && $b['plink']) {
298 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
299 . $wp_backlink_text . '</a>' . EOL . EOL;
302 $post = XML::escape($post);
306 <?xml version=\"1.0\" encoding=\"utf-8\"?>
308 <methodName>blogger.newPost</methodName>
310 <param><value><string/></value></param>
311 <param><value><string/></value></param>
312 <param><value><string>$wp_username</string></value></param>
313 <param><value><string>$wp_password</string></value></param>
314 <param><value><string>$post</string></value></param>
315 <param><value><int>1</int></value></param>
321 Logger::log('wppost: data: ' . $xml, Logger::DATA);
323 if ($wp_blog !== 'test') {
324 $x = DI::httpRequest()->post($wp_blog, $xml)->getBody();
326 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);