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>
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Content\Text\HTML;
12 use Friendica\Core\Addon;
13 use Friendica\Core\L10n;
14 use Friendica\Core\Logger;
15 use Friendica\Core\PConfig;
16 use Friendica\Database\DBA;
17 use Friendica\Util\Network;
18 use Friendica\Util\Strings;
19 use Friendica\Util\XML;
21 function wppost_install()
23 Addon::registerHook('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
24 Addon::registerHook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
25 Addon::registerHook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
26 Addon::registerHook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
27 Addon::registerHook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
28 Addon::registerHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
31 function wppost_uninstall()
33 Addon::unregisterHook('hook_fork', 'addon/wppost/wppost.php', 'wppost_hook_fork');
34 Addon::unregisterHook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
35 Addon::unregisterHook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
36 Addon::unregisterHook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
37 Addon::unregisterHook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
38 Addon::unregisterHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
41 Addon::unregisterHook('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
42 Addon::unregisterHook('addon_settings', 'addon/wppost/wppost.php', 'wppost_settings');
43 Addon::unregisterHook('addon_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
47 function wppost_jot_nets(&$a, &$b)
53 $wp_post = PConfig::get(local_user(), 'wppost', 'post');
54 if (intval($wp_post) == 1) {
55 $wp_defpost = PConfig::get(local_user(),'wppost','post_by_default');
56 $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
57 $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable" ' . $selected . ' value="1" /> '
58 . L10n::t('Post to Wordpress') . '</div>';
63 function wppost_settings(&$a,&$s) {
68 /* Add our stylesheet to the page so we can make our settings look nice */
70 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
72 /* Get the current state of our config variables */
74 $enabled = PConfig::get(local_user(),'wppost','post');
75 $checked = (($enabled) ? ' checked="checked" ' : '');
77 $css = (($enabled) ? '' : '-disabled');
79 $def_enabled = PConfig::get(local_user(),'wppost','post_by_default');
80 $back_enabled = PConfig::get(local_user(),'wppost','backlink');
81 $shortcheck_enabled = PConfig::get(local_user(),'wppost','shortcheck');
83 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
84 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
85 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
87 $wp_username = PConfig::get(local_user(), 'wppost', 'wp_username');
88 $wp_password = PConfig::get(local_user(), 'wppost', 'wp_password');
89 $wp_blog = PConfig::get(local_user(), 'wppost', 'wp_blog');
90 $wp_backlink_text = PConfig::get(local_user(), 'wppost', 'wp_backlink_text');
93 /* Add some HTML to the existing form */
95 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
96 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
98 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
99 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
100 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
102 $s .= '<div id="wppost-enable-wrapper">';
103 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . L10n::t('Enable WordPress Post Addon') . '</label>';
104 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
105 $s .= '</div><div class="clear"></div>';
107 $s .= '<div id="wppost-username-wrapper">';
108 $s .= '<label id="wppost-username-label" for="wppost-username">' . L10n::t('WordPress username') . '</label>';
109 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
110 $s .= '</div><div class="clear"></div>';
112 $s .= '<div id="wppost-password-wrapper">';
113 $s .= '<label id="wppost-password-label" for="wppost-password">' . L10n::t('WordPress password') . '</label>';
114 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
115 $s .= '</div><div class="clear"></div>';
117 $s .= '<div id="wppost-blog-wrapper">';
118 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . L10n::t('WordPress API URL') . '</label>';
119 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
120 $s .= '</div><div class="clear"></div>';
122 $s .= '<div id="wppost-bydefault-wrapper">';
123 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . L10n::t('Post to WordPress by default') . '</label>';
124 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
125 $s .= '</div><div class="clear"></div>';
127 $s .= '<div id="wppost-backlink-wrapper">';
128 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . L10n::t('Provide a backlink to the Friendica post') . '</label>';
129 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
130 $s .= '</div><div class="clear"></div>';
131 $s .= '<div id="wppost-backlinktext-wrapper">';
132 $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>';
133 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
134 $s .= '</div><div class="clear"></div>';
136 $s .= '<div id="wppost-shortcheck-wrapper">';
137 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . L10n::t("Don't post messages that are too short") . '</label>';
138 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
139 $s .= '</div><div class="clear"></div>';
141 /* provide a submit button */
143 $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>';
148 function wppost_settings_post(&$a,&$b) {
150 if(x($_POST,'wppost-submit')) {
152 PConfig::set(local_user(),'wppost','post',intval($_POST['wppost']));
153 PConfig::set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
154 PConfig::set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
155 PConfig::set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
156 PConfig::set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
157 PConfig::set(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
158 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 PConfig::set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
168 function wppost_hook_fork(&$a, &$b)
170 if ($b['name'] != 'notifier_normal') {
176 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
177 !strstr($post['postopts'], 'wppost') || ($post['parent'] != $post['id'])) {
178 $b['execute'] = false;
183 function wppost_post_local(&$a, &$b) {
185 // This can probably be changed to allow editing by pointing to a different API endpoint
191 if (!local_user() || (local_user() != $b['uid'])) {
195 if ($b['private'] || $b['parent']) {
199 $wp_post = intval(PConfig::get(local_user(),'wppost','post'));
201 $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
203 if ($b['api_source'] && intval(PConfig::get(local_user(),'wppost','post_by_default'))) {
211 if (strlen($b['postopts'])) {
212 $b['postopts'] .= ',';
215 $b['postopts'] .= 'wppost';
221 function wppost_send(&$a, &$b)
223 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
227 if(! strstr($b['postopts'],'wppost')) {
231 if($b['parent'] != $b['id']) {
235 // Dont't post if the post doesn't belong to us.
236 // This is a check for forum postings
237 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
238 if ($b['contact-id'] != $self['id']) {
242 $wp_username = XML::escape(PConfig::get($b['uid'], 'wppost', 'wp_username'));
243 $wp_password = XML::escape(PConfig::get($b['uid'], 'wppost', 'wp_password'));
244 $wp_blog = PConfig::get($b['uid'],'wppost','wp_blog');
245 $wp_backlink_text = PConfig::get($b['uid'],'wppost','wp_backlink_text');
246 if ($wp_backlink_text == '') {
247 $wp_backlink_text = L10n::t('Read the original post and comment stream on Friendica');
250 if ($wp_username && $wp_password && $wp_blog) {
251 $wptitle = trim($b['title']);
253 if (intval(PConfig::get($b['uid'], 'wppost', 'shortcheck'))) {
254 // Checking, if its a post that is worth a blog post
256 $siteinfo = BBCode::getAttachedData($b["body"]);
258 // Is it a link to an aricle, a video or a photo?
259 if (isset($siteinfo["type"])) {
260 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
265 // Does it have a title?
266 if ($wptitle != "") {
270 // Is it larger than 500 characters?
271 if (strlen($b['body']) > 500) {
280 // If the title is empty then try to guess
281 if ($wptitle == '') {
282 // Fetch information about the post
283 $siteinfo = BBCode::getAttachedData($b["body"]);
284 if (isset($siteinfo["title"])) {
285 $wptitle = $siteinfo["title"];
288 // If no bookmark is found then take the first line
289 if ($wptitle == '') {
290 // Remove the share element before fetching the first line
291 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
293 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
294 $pos = strpos($title, "\n");
296 if (($pos == 0) || ($pos > 100)) {
301 $wptitle = substr($title, 0, $pos).$trailer;
305 $title = '<title>' . (($wptitle) ? $wptitle : L10n::t('Post from Friendica')) . '</title>';
306 $post = BBCode::convert($b['body'], false, 4);
308 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
309 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
310 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
312 $post = $title.$post;
314 $wp_backlink = intval(PConfig::get($b['uid'],'wppost','backlink'));
315 if($wp_backlink && $b['plink']) {
316 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
317 . $wp_backlink_text . '</a>' . EOL . EOL;
320 $post = XML::escape($post);
324 <?xml version=\"1.0\" encoding=\"utf-8\"?>
326 <methodName>blogger.newPost</methodName>
328 <param><value><string/></value></param>
329 <param><value><string/></value></param>
330 <param><value><string>$wp_username</string></value></param>
331 <param><value><string>$wp_password</string></value></param>
332 <param><value><string>$post</string></value></param>
333 <param><value><int>1</int></value></param>
339 Logger::log('wppost: data: ' . $xml, Logger::DATA);
341 if ($wp_blog !== 'test') {
342 $x = Network::post($wp_blog, $xml)->getBody();
344 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);