4 * Name: WordPress Post Connector
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
9 function wppost_install() {
10 register_hook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
11 register_hook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
12 register_hook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
13 register_hook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
14 register_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
17 function wppost_uninstall() {
18 unregister_hook('post_local', 'addon/wppost/wppost.php', 'wppost_post_local');
19 unregister_hook('notifier_normal', 'addon/wppost/wppost.php', 'wppost_send');
20 unregister_hook('jot_networks', 'addon/wppost/wppost.php', 'wppost_jot_nets');
21 unregister_hook('connector_settings', 'addon/wppost/wppost.php', 'wppost_settings');
22 unregister_hook('connector_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
25 unregister_hook('post_local_end', 'addon/wppost/wppost.php', 'wppost_send');
26 unregister_hook('plugin_settings', 'addon/wppost/wppost.php', 'wppost_settings');
27 unregister_hook('plugin_settings_post', 'addon/wppost/wppost.php', 'wppost_settings_post');
32 function wppost_jot_nets(&$a,&$b) {
36 $wp_post = get_pconfig(local_user(),'wppost','post');
37 if(intval($wp_post) == 1) {
38 $wp_defpost = get_pconfig(local_user(),'wppost','post_by_default');
39 $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
40 $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable"' . $selected . 'value="1" /> '
41 . t('Post to Wordpress') . '</div>';
46 function wppost_settings(&$a,&$s) {
51 /* Add our stylesheet to the page so we can make our settings look nice */
53 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
55 /* Get the current state of our config variables */
57 $enabled = get_pconfig(local_user(),'wppost','post');
59 $checked = (($enabled) ? ' checked="checked" ' : '');
61 $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
63 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
65 $wp_username = get_pconfig(local_user(), 'wppost', 'wp_username');
66 $wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
67 $wp_blog = get_pconfig(local_user(), 'wppost', 'wp_blog');
70 /* Add some HTML to the existing form */
72 $s .= '<div class="settings-block">';
73 $s .= '<h3>' . t('WordPress Post Settings') . '</h3>';
74 $s .= '<div id="wppost-enable-wrapper">';
75 $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
76 $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
77 $s .= '</div><div class="clear"></div>';
79 $s .= '<div id="wppost-username-wrapper">';
80 $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
81 $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
82 $s .= '</div><div class="clear"></div>';
84 $s .= '<div id="wppost-password-wrapper">';
85 $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
86 $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
87 $s .= '</div><div class="clear"></div>';
89 $s .= '<div id="wppost-blog-wrapper">';
90 $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
91 $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
92 $s .= '</div><div class="clear"></div>';
94 $s .= '<div id="wppost-bydefault-wrapper">';
95 $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
96 $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
97 $s .= '</div><div class="clear"></div>';
99 /* provide a submit button */
101 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
106 function wppost_settings_post(&$a,&$b) {
108 if(x($_POST,'wppost-submit')) {
110 set_pconfig(local_user(),'wppost','post',intval($_POST['wppost']));
111 set_pconfig(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
112 set_pconfig(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
113 set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
114 set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
120 function wppost_post_local(&$a,&$b) {
122 // This can probably be changed to allow editing by pointing to a different API endpoint
127 if((! local_user()) || (local_user() != $b['uid']))
130 if($b['private'] || $b['parent'])
133 $wp_post = intval(get_pconfig(local_user(),'wppost','post'));
135 $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
137 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default')))
143 if(strlen($b['postopts']))
144 $b['postopts'] .= ',';
145 $b['postopts'] .= 'wppost';
151 function wppost_send(&$a,&$b) {
153 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
156 if(! strstr($b['postopts'],'wppost'))
159 if($b['parent'] != $b['id'])
163 $wp_username = get_pconfig($b['uid'],'wppost','wp_username');
164 $wp_password = get_pconfig($b['uid'],'wppost','wp_password');
165 $wp_blog = get_pconfig($b['uid'],'wppost','wp_blog');
167 if($wp_username && $wp_password && $wp_blog) {
169 require_once('include/bbcode.php');
171 $title = '<title>' . (($b['title']) ? $b['title'] : t('Post from Friendica')) . '</title>';
172 $post = $title . bbcode($b['body']);
173 $post = xmlify($post);
177 <?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
179 <methodName>blogger.newPost</methodName>
181 <param><value><string/></value></param>
182 <param><value><string/></value></param>
183 <param><value><string>$wp_username</string></value></param>
184 <param><value><string>$wp_password</string></value></param>
185 <param><value><string>$post</string></value></param>
186 <param><value><int>1</int></value></param>
192 logger('wppost: data: ' . $xml, LOGGER_DATA);
194 if($wp_blog !== 'test')
195 $x = post_url($wp_blog,$xml);
196 logger('posted to wordpress');