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 function wppost_install() {
11 register_hook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
12 register_hook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
13 register_hook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
14 register_hook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
15 register_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
18 function wppost_uninstall() {
19 unregister_hook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
20 unregister_hook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
21 unregister_hook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
22 unregister_hook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
23 unregister_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
26 unregister_hook('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
27 unregister_hook('plugin_settings', 'addon/wppost/wppost.php', 'wppost_settings');
28 unregister_hook('plugin_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
33 function wppost_jot_nets(&$a,&$b) {
37 $wp_post = get_pconfig(local_user(),'wppost','post');
38 if(intval($wp_post) == 1) {
39 $wp_defpost = get_pconfig(local_user(),'wppost','post_by_default');
40 $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
41 $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable" ' . $selected . ' value="1" /> '
42 . t('Post to Wordpress') . '</div>';
47 function wppost_settings(&$a,&$s) {
52 /* Add our stylesheet to the page so we can make our settings look nice */
54 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
56 /* Get the current state of our config variables */
58 $enabled = get_pconfig(local_user(),'wppost','post');
59 $checked = (($enabled) ? ' checked="checked" ' : '');
61 $css = (($enabled) ? '' : '-disabled');
63 $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
64 $back_enabled = get_pconfig(local_user(),'wppost','backlink');
65 $shortcheck_enabled = get_pconfig(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 = get_pconfig(local_user(), 'wppost', 'wp_username');
72 $wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
73 $wp_blog = get_pconfig(local_user(), 'wppost', 'wp_blog');
76 /* Add some HTML to the existing form */
78 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
79 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. t('Wordpress Export').'</h3>';
81 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
82 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
83 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. t('Wordpress Export').'</h3>';
85 $s .= '<div id="wppost-enable-wrapper">';
86 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
87 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
88 $s .= '</div><div class="clear"></div>';
90 $s .= '<div id="wppost-username-wrapper">';
91 $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
92 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
93 $s .= '</div><div class="clear"></div>';
95 $s .= '<div id="wppost-password-wrapper">';
96 $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
97 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
98 $s .= '</div><div class="clear"></div>';
100 $s .= '<div id="wppost-blog-wrapper">';
101 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
102 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
103 $s .= '</div><div class="clear"></div>';
105 $s .= '<div id="wppost-bydefault-wrapper">';
106 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
107 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
108 $s .= '</div><div class="clear"></div>';
110 $s .= '<div id="wppost-backlink-wrapper">';
111 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . t('Provide a backlink to the Friendica post') . '</label>';
112 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
113 $s .= '</div><div class="clear"></div>';
115 $s .= '<div id="wppost-shortcheck-wrapper">';
116 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . t("Don't post messages that are too short") . '</label>';
117 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
118 $s .= '</div><div class="clear"></div>';
120 /* provide a submit button */
122 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
127 function wppost_settings_post(&$a,&$b) {
129 if(x($_POST,'wppost-submit')) {
131 set_pconfig(local_user(),'wppost','post',intval($_POST['wppost']));
132 set_pconfig(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
133 set_pconfig(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
134 set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
135 set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
136 set_pconfig(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
137 set_pconfig(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
143 function wppost_post_local(&$a,&$b) {
145 // This can probably be changed to allow editing by pointing to a different API endpoint
150 if((! local_user()) || (local_user() != $b['uid']))
153 if($b['private'] || $b['parent'])
156 $wp_post = intval(get_pconfig(local_user(),'wppost','post'));
158 $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
160 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default')))
166 if(strlen($b['postopts']))
167 $b['postopts'] .= ',';
168 $b['postopts'] .= 'wppost';
174 function wppost_send(&$a,&$b) {
176 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
179 if(! strstr($b['postopts'],'wppost'))
182 if($b['parent'] != $b['id'])
186 $wp_username = xmlify(get_pconfig($b['uid'],'wppost','wp_username'));
187 $wp_password = xmlify(get_pconfig($b['uid'],'wppost','wp_password'));
188 $wp_blog = get_pconfig($b['uid'],'wppost','wp_blog');
190 if($wp_username && $wp_password && $wp_blog) {
192 require_once('include/bbcode.php');
193 require_once('include/html2plain.php');
194 require_once('include/plaintext.php');
196 $wptitle = trim($b['title']);
198 if (intval(get_pconfig($b['uid'],'wppost','shortcheck'))) {
199 // Checking, if its a post that is worth a blog post
201 $siteinfo = get_attached_data($b["body"]);
203 // Is it a link to an aricle, a video or a photo?
204 if (isset($siteinfo["type"]))
205 if (in_array($siteinfo["type"], array("link", "audio", "video", "photo")))
208 // Does it have a title?
212 // Is it larger than 500 characters?
213 if (strlen($b['body']) > 500)
220 // If the title is empty then try to guess
221 if ($wptitle == '') {
222 // Take the description from the bookmark
223 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
224 $wptitle = $matches[2];
226 // If no bookmark is found then take the first line
227 if ($wptitle == '') {
228 // Remove the share element before fetching the first line
229 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
231 $title = html2plain(bbcode($title, false, false), 0, true)."\n";
232 $pos = strpos($title, "\n");
234 if (($pos == 0) or ($pos > 100)) {
239 $wptitle = substr($title, 0, $pos).$trailer;
243 $title = '<title>' . (($wptitle) ? $wptitle : t('Post from Friendica')) . '</title>';
244 $post = bbcode($b['body'], false, false, 4);
246 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
247 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
248 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
250 $post = $title.$post;
252 $wp_backlink = intval(get_pconfig($b['uid'],'wppost','backlink'));
253 if($wp_backlink && $b['plink'])
254 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
255 . t('Read the original post and comment stream on Friendica') . '</a>' . EOL . EOL;
257 $post = xmlify($post);
261 <?xml version=\"1.0\" encoding=\"utf-8\"?>
263 <methodName>blogger.newPost</methodName>
265 <param><value><string/></value></param>
266 <param><value><string/></value></param>
267 <param><value><string>$wp_username</string></value></param>
268 <param><value><string>$wp_password</string></value></param>
269 <param><value><string>$post</string></value></param>
270 <param><value><int>1</int></value></param>
276 logger('wppost: data: ' . $xml, LOGGER_DATA);
278 if($wp_blog !== 'test')
279 $x = post_url($wp_blog,$xml);
280 logger('posted to wordpress: ' . (($x) ? $x : ''), LOGGER_DEBUG);