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\PConfig;
14 use Friendica\Database\DBA;
15 use Friendica\Util\Network;
17 function wppost_install() {
18 Addon::registerHook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
19 Addon::registerHook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
20 Addon::registerHook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
21 Addon::registerHook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
22 Addon::registerHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
25 function wppost_uninstall() {
26 Addon::unregisterHook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
27 Addon::unregisterHook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
28 Addon::unregisterHook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
29 Addon::unregisterHook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
30 Addon::unregisterHook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
33 Addon::unregisterHook('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
34 Addon::unregisterHook('addon_settings', 'addon/wppost/wppost.php', 'wppost_settings');
35 Addon::unregisterHook('addon_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
40 function wppost_jot_nets(&$a,&$b) {
44 $wp_post = PConfig::get(local_user(),'wppost','post');
45 if(intval($wp_post) == 1) {
46 $wp_defpost = PConfig::get(local_user(),'wppost','post_by_default');
47 $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
48 $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable" ' . $selected . ' value="1" /> '
49 . L10n::t('Post to Wordpress') . '</div>';
54 function wppost_settings(&$a,&$s) {
59 /* Add our stylesheet to the page so we can make our settings look nice */
61 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
63 /* Get the current state of our config variables */
65 $enabled = PConfig::get(local_user(),'wppost','post');
66 $checked = (($enabled) ? ' checked="checked" ' : '');
68 $css = (($enabled) ? '' : '-disabled');
70 $def_enabled = PConfig::get(local_user(),'wppost','post_by_default');
71 $back_enabled = PConfig::get(local_user(),'wppost','backlink');
72 $shortcheck_enabled = PConfig::get(local_user(),'wppost','shortcheck');
74 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
75 $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
76 $shortcheck_checked = (($shortcheck_enabled) ? ' checked="checked" ' : '');
78 $wp_username = PConfig::get(local_user(), 'wppost', 'wp_username');
79 $wp_password = PConfig::get(local_user(), 'wppost', 'wp_password');
80 $wp_blog = PConfig::get(local_user(), 'wppost', 'wp_blog');
81 $wp_backlink_text = PConfig::get(local_user(), 'wppost', 'wp_backlink_text');
84 /* Add some HTML to the existing form */
86 $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
87 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
89 $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
90 $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
91 $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. L10n::t('Wordpress Export').'</h3>';
93 $s .= '<div id="wppost-enable-wrapper">';
94 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . L10n::t('Enable WordPress Post Addon') . '</label>';
95 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
96 $s .= '</div><div class="clear"></div>';
98 $s .= '<div id="wppost-username-wrapper">';
99 $s .= '<label id="wppost-username-label" for="wppost-username">' . L10n::t('WordPress username') . '</label>';
100 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
101 $s .= '</div><div class="clear"></div>';
103 $s .= '<div id="wppost-password-wrapper">';
104 $s .= '<label id="wppost-password-label" for="wppost-password">' . L10n::t('WordPress password') . '</label>';
105 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
106 $s .= '</div><div class="clear"></div>';
108 $s .= '<div id="wppost-blog-wrapper">';
109 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . L10n::t('WordPress API URL') . '</label>';
110 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
111 $s .= '</div><div class="clear"></div>';
113 $s .= '<div id="wppost-bydefault-wrapper">';
114 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . L10n::t('Post to WordPress by default') . '</label>';
115 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
116 $s .= '</div><div class="clear"></div>';
118 $s .= '<div id="wppost-backlink-wrapper">';
119 $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . L10n::t('Provide a backlink to the Friendica post') . '</label>';
120 $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
121 $s .= '</div><div class="clear"></div>';
122 $s .= '<div id="wppost-backlinktext-wrapper">';
123 $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>';
124 $s .= '<input id="wppost-backlinktext" type="text" name="wp_backlink_text" value="'. $wp_backlink_text.'" ' . $wp_backlink_text . '/>';
125 $s .= '</div><div class="clear"></div>';
127 $s .= '<div id="wppost-shortcheck-wrapper">';
128 $s .= '<label id="wppost-shortcheck-label" for="wppost-shortcheck">' . L10n::t("Don't post messages that are too short") . '</label>';
129 $s .= '<input id="wppost-shortcheck" type="checkbox" name="wp_shortcheck" value="1" '.$shortcheck_checked.'/>';
130 $s .= '</div><div class="clear"></div>';
132 /* provide a submit button */
134 $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>';
139 function wppost_settings_post(&$a,&$b) {
141 if(x($_POST,'wppost-submit')) {
143 PConfig::set(local_user(),'wppost','post',intval($_POST['wppost']));
144 PConfig::set(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
145 PConfig::set(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
146 PConfig::set(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
147 PConfig::set(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
148 PConfig::set(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
149 PConfig::set(local_user(),'wppost','shortcheck',trim($_POST['wp_shortcheck']));
150 $wp_backlink_text = notags(trim($_POST['wp_backlink_text']));
151 $wp_backlink_text = BBCode::convert($wp_backlink_text, false, 8);
152 $wp_backlink_text = HTML::toPlaintext($wp_backlink_text, 0, true);
153 PConfig::set(local_user(),'wppost','wp_backlink_text', $wp_backlink_text);
159 function wppost_post_local(&$a, &$b) {
161 // This can probably be changed to allow editing by pointing to a different API endpoint
167 if (!local_user() || (local_user() != $b['uid'])) {
171 if ($b['private'] || $b['parent']) {
175 $wp_post = intval(PConfig::get(local_user(),'wppost','post'));
177 $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
179 if ($b['api_source'] && intval(PConfig::get(local_user(),'wppost','post_by_default'))) {
187 if (strlen($b['postopts'])) {
188 $b['postopts'] .= ',';
191 $b['postopts'] .= 'wppost';
197 function wppost_send(&$a,&$b) {
199 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
203 if(! strstr($b['postopts'],'wppost')) {
207 if($b['parent'] != $b['id']) {
211 // Dont't post if the post doesn't belong to us.
212 // This is a check for forum postings
213 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
214 if ($b['contact-id'] != $self['id']) {
218 $wp_username = xmlify(PConfig::get($b['uid'],'wppost','wp_username'));
219 $wp_password = xmlify(PConfig::get($b['uid'],'wppost','wp_password'));
220 $wp_blog = PConfig::get($b['uid'],'wppost','wp_blog');
221 $wp_backlink_text = PConfig::get($b['uid'],'wppost','wp_backlink_text');
222 if ($wp_backlink_text == '') {
223 $wp_backlink_text = L10n::t('Read the original post and comment stream on Friendica');
226 if ($wp_username && $wp_password && $wp_blog) {
227 $wptitle = trim($b['title']);
229 if (intval(PConfig::get($b['uid'], 'wppost', 'shortcheck'))) {
230 // Checking, if its a post that is worth a blog post
232 $siteinfo = BBCode::getAttachedData($b["body"]);
234 // Is it a link to an aricle, a video or a photo?
235 if (isset($siteinfo["type"])) {
236 if (in_array($siteinfo["type"], ["link", "audio", "video", "photo"])) {
241 // Does it have a title?
242 if ($wptitle != "") {
246 // Is it larger than 500 characters?
247 if (strlen($b['body']) > 500) {
256 // If the title is empty then try to guess
257 if ($wptitle == '') {
258 // Fetch information about the post
259 $siteinfo = BBCode::getAttachedData($b["body"]);
260 if (isset($siteinfo["title"])) {
261 $wptitle = $siteinfo["title"];
264 // If no bookmark is found then take the first line
265 if ($wptitle == '') {
266 // Remove the share element before fetching the first line
267 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
269 $title = HTML::toPlaintext(BBCode::convert($title, false), 0, true)."\n";
270 $pos = strpos($title, "\n");
272 if (($pos == 0) || ($pos > 100)) {
277 $wptitle = substr($title, 0, $pos).$trailer;
281 $title = '<title>' . (($wptitle) ? $wptitle : L10n::t('Post from Friendica')) . '</title>';
282 $post = BBCode::convert($b['body'], false, 4);
284 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
285 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
286 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
288 $post = $title.$post;
290 $wp_backlink = intval(PConfig::get($b['uid'],'wppost','backlink'));
291 if($wp_backlink && $b['plink']) {
292 $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
293 . $wp_backlink_text . '</a>' . EOL . EOL;
296 $post = xmlify($post);
300 <?xml version=\"1.0\" encoding=\"utf-8\"?>
302 <methodName>blogger.newPost</methodName>
304 <param><value><string/></value></param>
305 <param><value><string/></value></param>
306 <param><value><string>$wp_username</string></value></param>
307 <param><value><string>$wp_password</string></value></param>
308 <param><value><string>$post</string></value></param>
309 <param><value><int>1</int></value></param>
315 logger('wppost: data: ' . $xml, LOGGER_DATA);
317 if ($wp_blog !== 'test') {
318 $x = Network::post($wp_blog, $xml);
320 logger('posted to wordpress: ' . (($x) ? $x : ''), LOGGER_DEBUG);