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\Core\Renderer;
14 use Friendica\Database\DBA;
16 use Friendica\Model\Item;
17 use Friendica\Model\Post;
18 use Friendica\Util\XML;
20 function wppost_install()
22 Hook::register('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
23 Hook::register('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
24 Hook::register('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
25 Hook::register('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
26 Hook::register('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
27 Hook::register('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
30 function wppost_jot_nets(array &$jotnets_fields)
32 if (!DI::userSession()->getLocalUserId()) {
36 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post')) {
41 DI::l10n()->t('Post to Wordpress'),
42 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default')
49 function wppost_settings(array &$data)
51 if (!DI::userSession()->getLocalUserId()) {
55 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post', false);
56 $wp_username = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'wp_username');
57 $wp_blog = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'wp_blog');
58 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default', false);
59 $back_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'backlink', false);
60 $wp_backlink_text = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'wp_backlink_text');
61 $shortcheck_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'shortcheck', false);
63 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/wppost/');
64 $html = Renderer::replaceMacros($t, [
65 '$enabled' => ['wppost', DI::l10n()->t('Enable Wordpress Post Addon'), $enabled],
66 '$username' => ['wp_username', DI::l10n()->t('Wordpress username'), $wp_username],
67 '$password' => ['wp_password', DI::l10n()->t('Wordpress password')],
68 '$blog' => ['wp_blog', DI::l10n()->t('WordPress API URL'), $wp_blog],
69 '$bydefault' => ['wp_bydefault', DI::l10n()->t('Post to Wordpress by default'), $def_enabled],
70 '$backlink' => ['wp_backlink', DI::l10n()->t('Provide a backlink to the Friendica post'), $back_enabled],
71 '$backlink_text' => ['wp_backlink_text', DI::l10n()->t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.'), $wp_backlink_text],
72 '$shortcheck' => ['wp_shortcheck', DI::l10n()->t('Don\'t post messages that are too short'), $shortcheck_enabled],
76 'connector' => 'wppost',
77 'title' => DI::l10n()->t('Wordpress Export'),
78 'image' => 'images/wordpress.png',
79 'enabled' => $enabled,
85 function wppost_settings_post(array &$b)
87 if (!empty($_POST['wppost-submit'])) {
88 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'post', intval($_POST['wppost']));
89 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default', intval($_POST['wp_bydefault']));
90 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_username', trim($_POST['wp_username']));
91 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_password', trim($_POST['wp_password']));
92 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_blog', trim($_POST['wp_blog']));
93 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'backlink', intval($_POST['wp_backlink']));
94 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'shortcheck', intval($_POST['wp_shortcheck']));
95 $wp_backlink_text = BBCode::convert(trim($_POST['wp_backlink_text']), false, BBCode::BACKLINK);
96 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
97 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'wppost', 'wp_backlink_text', $wp_backlink_text);
101 function wppost_hook_fork(array &$b)
103 if ($b['name'] != 'notifier_normal') {
110 $post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
111 !strstr($post['postopts'] ?? '', 'wppost') || ($post['parent'] != $post['id'])
113 $b['execute'] = false;
118 function wppost_post_local(array &$b)
121 // This can probably be changed to allow editing by pointing to a different API endpoint
127 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
131 if ($b['private'] || $b['parent']) {
135 $wp_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post'));
137 $wp_enable = (($wp_post && !empty($_REQUEST['wppost_enable'])) ? intval($_REQUEST['wppost_enable']) : 0);
139 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'wppost', 'post_by_default'))) {
147 if (strlen($b['postopts'])) {
148 $b['postopts'] .= ',';
151 $b['postopts'] .= 'wppost';
157 function wppost_send(array &$b)
159 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
163 if (!strstr($b['postopts'], 'wppost')) {
167 if ($b['gravity'] != Item::GRAVITY_PARENT) {
171 // Dont't post if the post doesn't belong to us.
172 // This is a check for forum postings
173 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
174 if ($b['contact-id'] != $self['id']) {
178 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
180 $wp_username = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_username'));
181 $wp_password = XML::escape(DI::pConfig()->get($b['uid'], 'wppost', 'wp_password'));
182 $wp_blog = DI::pConfig()->get($b['uid'], 'wppost', 'wp_blog');
183 $wp_backlink_text = DI::pConfig()->get($b['uid'], 'wppost', 'wp_backlink_text');
184 if ($wp_backlink_text == '') {
185 $wp_backlink_text = DI::l10n()->t('Read the original post and comment stream on Friendica');
188 if ($wp_username && $wp_password && $wp_blog) {
189 $wptitle = trim($b['title']);
191 if (intval(DI::pConfig()->get($b['uid'], 'wppost', 'shortcheck'))) {
192 // Checking, if its a post that is worth a blog post
193 $postentry = (bool)Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML, Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]);
195 // Does it have a title?
196 if ($wptitle != "") {
200 // Is it larger than 500 characters?
201 if (strlen($b['body']) > 500) {
210 // If the title is empty then try to guess
211 if ($wptitle == '') {
212 // Fetch information about the post
213 $media = Post\Media::getByURIId($b['uri-id'], [Post\Media::HTML]);
214 if (!empty($media) && !empty($media[0]['name']) && ($media[0]['name'] != $media[0]['url'])) {
215 $wptitle = $media[0]['name'];
218 // If no bookmark is found then take the first line
219 if ($wptitle == '') {
220 // Remove the share element before fetching the first line
221 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism", "\n$1\n", $b['body']));
223 $title = BBCode::toPlaintext($title) . "\n";
224 $pos = strpos($title, "\n");
226 if (($pos == 0) || ($pos > 100)) {
231 $wptitle = substr($title, 0, $pos) . $trailer;
235 $title = '<title>' . (($wptitle) ? $wptitle : DI::l10n()->t('Post from Friendica')) . '</title>';
236 $post = BBCode::convertForUriId($b['uri-id'], $b['body'], BBCode::CONNECTORS);
238 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
239 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
240 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism', "\n$1\n", $post);
242 $post = $title . $post;
244 $wp_backlink = intval(DI::pConfig()->get($b['uid'], 'wppost', 'backlink'));
245 if ($wp_backlink && $b['plink']) {
246 $post .= '<p><a href="' . $b['plink'] . '">' . $wp_backlink_text . '</a></p>';
249 $post = XML::escape($post);
253 <?xml version=\"1.0\" encoding=\"utf-8\"?>
255 <methodName>blogger.newPost</methodName>
257 <param><value><string/></value></param>
258 <param><value><string/></value></param>
259 <param><value><string>$wp_username</string></value></param>
260 <param><value><string>$wp_password</string></value></param>
261 <param><value><string>$post</string></value></param>
262 <param><value><int>1</int></value></param>
268 Logger::debug('wppost: data: ' . $xml);
270 if ($wp_blog !== 'test') {
271 $x = DI::httpClient()->post($wp_blog, $xml)->getBody();
273 Logger::info('posted to wordpress: ' . (($x) ? $x : ''));