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\Addon;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Database\DBA;
16 use Friendica\Util\Network;
17 use Friendica\Util\Strings;
19 function wppost_install() {
20 Addon::registerHook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
21 Addon::registerHook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
22 Addon::registerHook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
23 Addon::registerHook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
24 Addon::registerHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
27 function wppost_uninstall() {
28 Addon::unregisterHook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
29 Addon::unregisterHook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
30 Addon::unregisterHook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
31 Addon::unregisterHook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
32 Addon::unregisterHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
35 Addon::unregisterHook('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
36 Addon::unregisterHook('addon_settings', 'addon/wppost/wppost.php', 'wppost_settings');
37 Addon::unregisterHook('addon_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
42 function wppost_jot_nets(&$a,&$b) {
46 $wp_post = PConfig::get(local_user(),'wppost','post');
47 if(intval($wp_post) == 1) {
48 $wp_defpost = PConfig::get(local_user(),'wppost','post_by_default');
49 $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
50 $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable" ' . $selected . ' value="1" /> '
51 . L10n::t('Post to Wordpress') . '</div>';
56 function wppost_settings(&$a,&$s) {
61 /* Add our stylesheet to the page so we can make our settings look nice */
63 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
65 /* Get the current state of our config variables */
67 $enabled = PConfig::get(local_user(),'wppost','post');
68 $checked = (($enabled) ? ' checked="checked" ' : '');
70 $css = (($enabled) ? '' : '-disabled');
72 $def_enabled = PConfig::get(local_user(),'wppost','post_by_default');
73 $back_enabled = PConfig::get(local_user(),'wppost','backlink');
74 $shortcheck_enabled = PConfig::get(local_user(),'wppost','shortcheck');
76 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
77 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
78 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
80 $wp_username = PConfig::get(local_user(), 'wppost', 'wp_username');
81 $wp_password = PConfig::get(local_user(), 'wppost', 'wp_password');
82 $wp_blog = PConfig::get(local_user(), 'wppost', 'wp_blog');
83 $wp_backlink_text = PConfig::get(local_user(), 'wppost', 'wp_backlink_text');
86 /* Add some HTML to the existing form */
88 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
89 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
91 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
92 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
93 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
95 $s .= '<div id="wppost-enable-wrapper">';
96 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . L10n::t('Enable WordPress Post Addon') . '</label>';
97 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
98 $s .= '</div><div class="clear"></div>';
100 $s .= '<div id="wppost-username-wrapper">';
101 $s .= '<label id="wppost-username-label" for="wppost-username">' . L10n::t('WordPress username') . '</label>';
102 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
103 $s .= '</div><div class="clear"></div>';
105 $s .= '<div id="wppost-password-wrapper">';
106 $s .= '<label id="wppost-password-label" for="wppost-password">' . L10n::t('WordPress password') . '</label>';
107 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
108 $s .= '</div><div class="clear"></div>';
110 $s .= '<div id="wppost-blog-wrapper">';
111 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . L10n::t('WordPress API URL') . '</label>';
112 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
113 $s .= '</div><div class="clear"></div>';
115 $s .= '<div id="wppost-bydefault-wrapper">';
116 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . L10n::t('Post to WordPress by default') . '</label>';
117 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
118 $s .= '</div><div class="clear"></div>';
120 $s .= '<div id="wppost-backlink-wrapper">';
121 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . L10n::t('Provide a backlink to the Friendica post') . '</label>';
122 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
123 $s .= '</div><div class="clear"></div>';
124 $s .= '<div id="wppost-backlinktext-wrapper">';
125 $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>';
126 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
127 $s .= '</div><div class="clear"></div>';
129 $s .= '<div id="wppost-shortcheck-wrapper">';
130 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . L10n::t("Don't post messages that are too short") . '</label>';
131 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
132 $s .= '</div><div class="clear"></div>';
134 /* provide a submit button */
136 $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>';
141 function wppost_settings_post(&$a,&$b) {
143 if(x($_POST,'wppost-submit')) {
145 PConfig::set(local_user(),'wppost','post',intval($_POST['wppost']));
146 PConfig::set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
147 PConfig::set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
148 PConfig::set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
149 PConfig::set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
150 PConfig::set(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
151 PConfig::set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
152 $wp_backlink_text = notags(trim($_POST['wp_backlink_text']));
153 $wp_backlink_text = BBCode::convert($wp_backlink_text, false, 8);
154 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
155 PConfig::set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
161 function wppost_post_local(&$a, &$b) {
163 // This can probably be changed to allow editing by pointing to a different API endpoint
169 if (!local_user() || (local_user() != $b['uid'])) {
173 if ($b['private'] || $b['parent']) {
177 $wp_post = intval(PConfig::get(local_user(),'wppost','post'));
179 $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
181 if ($b['api_source'] && intval(PConfig::get(local_user(),'wppost','post_by_default'))) {
189 if (strlen($b['postopts'])) {
190 $b['postopts'] .= ',';
193 $b['postopts'] .= 'wppost';
199 function wppost_send(&$a,&$b) {
201 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
205 if(! strstr($b['postopts'],'wppost')) {
209 if($b['parent'] != $b['id']) {
213 // Dont't post if the post doesn't belong to us.
214 // This is a check for forum postings
215 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
216 if ($b['contact-id'] != $self['id']) {
220 $wp_username = Strings::escape(PConfig::get($b['uid'], 'wppost', 'wp_username'));
221 $wp_password = Strings::escape(PConfig::get($b['uid'], 'wppost',' wp_password'));
222 $wp_blog = PConfig::get($b['uid'],'wppost','wp_blog');
223 $wp_backlink_text = PConfig::get($b['uid'],'wppost','wp_backlink_text');
224 if ($wp_backlink_text == '') {
225 $wp_backlink_text = L10n::t('Read the original post and comment stream on Friendica');
228 if ($wp_username && $wp_password && $wp_blog) {
229 $wptitle = trim($b['title']);
231 if (intval(PConfig::get($b['uid'], 'wppost', 'shortcheck'))) {
232 // Checking, if its a post that is worth a blog post
234 $siteinfo = BBCode::getAttachedData($b["body"]);
236 // Is it a link to an aricle, a video or a photo?
237 if (isset($siteinfo["type"])) {
238 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
243 // Does it have a title?
244 if ($wptitle != "") {
248 // Is it larger than 500 characters?
249 if (strlen($b['body']) > 500) {
258 // If the title is empty then try to guess
259 if ($wptitle == '') {
260 // Fetch information about the post
261 $siteinfo = BBCode::getAttachedData($b["body"]);
262 if (isset($siteinfo["title"])) {
263 $wptitle = $siteinfo["title"];
266 // If no bookmark is found then take the first line
267 if ($wptitle == '') {
268 // Remove the share element before fetching the first line
269 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
271 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
272 $pos = strpos($title, "\n");
274 if (($pos == 0) || ($pos > 100)) {
279 $wptitle = substr($title, 0, $pos).$trailer;
283 $title = '<title>' . (($wptitle) ? $wptitle : L10n::t('Post from Friendica')) . '</title>';
284 $post = BBCode::convert($b['body'], false, 4);
286 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
287 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
288 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
290 $post = $title.$post;
292 $wp_backlink = intval(PConfig::get($b['uid'],'wppost','backlink'));
293 if($wp_backlink && $b['plink']) {
294 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
295 . $wp_backlink_text . '</a>' . EOL . EOL;
298 $post = Strings::escape($post);
302 <?xml version=\"1.0\" encoding=\"utf-8\"?>
304 <methodName>blogger.newPost</methodName>
306 <param><value><string/></value></param>
307 <param><value><string/></value></param>
308 <param><value><string>$wp_username</string></value></param>
309 <param><value><string>$wp_password</string></value></param>
310 <param><value><string>$post</string></value></param>
311 <param><value><int>1</int></value></param>
317 Logger::log('wppost: data: ' . $xml, Logger::DATA);
319 if ($wp_blog !== 'test') {
320 $x = Network::post($wp_blog, $xml)->getBody();
322 Logger::log('posted to wordpress: ' . (($x) ? $x : ''), Logger::DEBUG);