4 * Name: WordPress Post Connector
5 * Description: Post to WordPress (or anything else which uses blogger XMLRPC API)
7 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
10 use Friendica\Core\PConfig;
12 function wppost_install() {
13 register_hook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
14 register_hook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
15 register_hook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
16 register_hook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
17 register_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
20 function wppost_uninstall() {
21 unregister_hook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
22 unregister_hook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
23 unregister_hook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
24 unregister_hook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
25 unregister_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
28 unregister_hook('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
29 unregister_hook('plugin_settings', 'addon/wppost/wppost.php', 'wppost_settings');
30 unregister_hook('plugin_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
35 function wppost_jot_nets(&$a,&$b) {
39 $wp_post = PConfig::get(local_user(),'wppost','post');
40 if(intval($wp_post) == 1) {
41 $wp_defpost = PConfig::get(local_user(),'wppost','post_by_default');
42 $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
43 $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable" ' . $selected . ' value="1" /> '
44 . t('Post to Wordpress') . '</div>';
49 function wppost_settings(&$a,&$s) {
54 /* Add our stylesheet to the page so we can make our settings look nice */
56 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
58 /* Get the current state of our config variables */
60 $enabled = PConfig::get(local_user(),'wppost','post');
61 $checked = (($enabled) ? ' checked="checked" ' : '');
63 $css = (($enabled) ? '' : '-disabled');
65 $def_enabled = PConfig::get(local_user(),'wppost','post_by_default');
66 $back_enabled = PConfig::get(local_user(),'wppost','backlink');
67 $shortcheck_enabled = PConfig::get(local_user(),'wppost','shortcheck');
69 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
70 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
71 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
73 $wp_username = PConfig::get(local_user(), 'wppost', 'wp_username');
74 $wp_password = PConfig::get(local_user(), 'wppost', 'wp_password');
75 $wp_blog = PConfig::get(local_user(), 'wppost', 'wp_blog');
76 $wp_backlink_text = PConfig::get(local_user(), 'wppost', 'wp_backlink_text');
79 /* Add some HTML to the existing form */
81 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
82 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. t('Wordpress Export').'</h3>';
84 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
85 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
86 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. t('Wordpress Export').'</h3>';
88 $s .= '<div id="wppost-enable-wrapper">';
89 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
90 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
91 $s .= '</div><div class="clear"></div>';
93 $s .= '<div id="wppost-username-wrapper">';
94 $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
95 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
96 $s .= '</div><div class="clear"></div>';
98 $s .= '<div id="wppost-password-wrapper">';
99 $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
100 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
101 $s .= '</div><div class="clear"></div>';
103 $s .= '<div id="wppost-blog-wrapper">';
104 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
105 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
106 $s .= '</div><div class="clear"></div>';
108 $s .= '<div id="wppost-bydefault-wrapper">';
109 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
110 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
111 $s .= '</div><div class="clear"></div>';
113 $s .= '<div id="wppost-backlink-wrapper">';
114 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . t('Provide a backlink to the Friendica post') . '</label>';
115 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
116 $s .= '</div><div class="clear"></div>';
117 $s .= '<div id="wppost-backlinktext-wrapper">';
118 $s .= '<label id="wppost-backlinktext-label" for="wp_backlink_text">' . t('Text for the backlink, e.g. Read the original post and comment stream on Friendica.') . '</label>';
119 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
120 $s .= '</div><div class="clear"></div>';
122 $s .= '<div id="wppost-shortcheck-wrapper">';
123 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . t("Don't post messages that are too short") . '</label>';
124 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
125 $s .= '</div><div class="clear"></div>';
127 /* provide a submit button */
129 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
134 function wppost_settings_post(&$a,&$b) {
136 if(x($_POST,'wppost-submit')) {
138 PConfig::set(local_user(),'wppost','post',intval($_POST['wppost']));
139 PConfig::set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
140 PConfig::set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
141 PConfig::set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
142 PConfig::set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
143 PConfig::set(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
144 PConfig::set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
145 $wp_backlink_text = notags(trim($_POST['wp_backlink_text']));
146 $wp_backlink_text = bbcode($wp_backlink_text, false, false, 8);
147 $wp_backlink_text = html2plain($wp_backlink_text, 0, true);
148 PConfig::set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
154 function wppost_post_local(&$a, &$b) {
156 // This can probably be changed to allow editing by pointing to a different API endpoint
162 if (!local_user() || (local_user() != $b['uid'])) {
166 if ($b['private'] || $b['parent']) {
170 $wp_post = intval(PConfig::get(local_user(),'wppost','post'));
172 $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
174 if ($b['api_source'] && intval(PConfig::get(local_user(),'wppost','post_by_default'))) {
182 if (strlen($b['postopts'])) {
183 $b['postopts'] .= ',';
186 $b['postopts'] .= 'wppost';
192 function wppost_send(&$a,&$b) {
194 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
197 if(! strstr($b['postopts'],'wppost'))
200 if($b['parent'] != $b['id'])
203 // Dont't post if the post doesn't belong to us.
204 // This is a check for forum postings
205 $self = dba::select('contact', array('id'), array('uid' => $b['uid'], 'self' => true), array('limit' => 1));
206 if ($b['contact-id'] != $self['id']) {
210 $wp_username = xmlify(PConfig::get($b['uid'],'wppost','wp_username'));
211 $wp_password = xmlify(PConfig::get($b['uid'],'wppost','wp_password'));
212 $wp_blog = PConfig::get($b['uid'],'wppost','wp_blog');
213 $wp_backlink_text = PConfig::get($b['uid'],'wppost','wp_backlink_text');
214 if ($wp_backlink_text == '') {
215 $wp_backlink_text = t('Read the original post and comment stream on Friendica');
218 if($wp_username && $wp_password && $wp_blog) {
220 require_once('include/bbcode.php');
221 require_once('include/html2plain.php');
222 require_once('include/plaintext.php');
224 $wptitle = trim($b['title']);
226 if (intval(PConfig::get($b['uid'],'wppost','shortcheck'))) {
227 // Checking, if its a post that is worth a blog post
229 $siteinfo = get_attached_data($b["body"]);
231 // Is it a link to an aricle, a video or a photo?
232 if (isset($siteinfo["type"])) {
233 if (in_array($siteinfo["type"], array("link", "audio", "video", "photo"))) {
238 // Does it have a title?
239 if ($wptitle != "") {
243 // Is it larger than 500 characters?
244 if (strlen($b['body']) > 500) {
253 // If the title is empty then try to guess
254 if ($wptitle == '') {
255 // Fetch information about the post
256 $siteinfo = get_attached_data($b["body"]);
257 if (isset($siteinfo["title"])) {
258 $wptitle = $siteinfo["title"];
261 // If no bookmark is found then take the first line
262 if ($wptitle == '') {
263 // Remove the share element before fetching the first line
264 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
266 $title = html2plain(bbcode($title, false, false), 0, true)."\n";
267 $pos = strpos($title, "\n");
269 if (($pos == 0) || ($pos > 100)) {
274 $wptitle = substr($title, 0, $pos).$trailer;
278 $title = '<title>' . (($wptitle) ? $wptitle : t('Post from Friendica')) . '</title>';
279 $post = bbcode($b['body'], false, false, 4);
281 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
282 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
283 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
285 $post = $title.$post;
287 $wp_backlink = intval(PConfig::get($b['uid'],'wppost','backlink'));
288 if($wp_backlink && $b['plink']) {
289 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
290 . $wp_backlink_text . '</a>' . EOL . EOL;
293 $post = xmlify($post);
297 <?xml version=\"1.0\" encoding=\"utf-8\"?>
299 <methodName>blogger.newPost</methodName>
301 <param><value><string/></value></param>
302 <param><value><string/></value></param>
303 <param><value><string>$wp_username</string></value></param>
304 <param><value><string>$wp_password</string></value></param>
305 <param><value><string>$post</string></value></param>
306 <param><value><int>1</int></value></param>
312 logger('wppost: data: ' . $xml, LOGGER_DATA);
314 if($wp_blog !== 'test') {
315 $x = post_url($wp_blog,$xml);
317 logger('posted to wordpress: ' . (($x) ? $x : ''), LOGGER_DEBUG);