]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
Merge branch 'master', remote-tracking branch 'remotes/upstream/master'
[friendica-addons.git] / wppost / wppost.php
1 <?php
2
3 /**
4  * Name: WordPress Post Connector
5  * Description: Post to WordPress (or anything else which uses blogger XMLRPC API)
6  * Version: 1.0
7  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8  */
9
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');
16
17 }
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');
24
25         // obsolete - remove
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');
29
30 }
31
32
33 function wppost_jot_nets(&$a,&$b) {
34     if(! local_user())
35         return;
36
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>';
43     }
44 }
45
46
47 function wppost_settings(&$a,&$s) {
48
49     if(! local_user())
50         return;
51
52     /* Add our stylesheet to the page so we can make our settings look nice */
53
54     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
55
56     /* Get the current state of our config variables */
57
58     $enabled = get_pconfig(local_user(),'wppost','post');
59
60     $checked = (($enabled) ? ' checked="checked" ' : '');
61
62     $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
63
64     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
65
66         $wp_username = get_pconfig(local_user(), 'wppost', 'wp_username');
67         $wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
68         $wp_blog = get_pconfig(local_user(), 'wppost', 'wp_blog');
69
70
71     /* Add some HTML to the existing form */
72
73     $s .= '<div class="settings-block">';
74     $s .= '<h3>' . t('WordPress Post Settings') . '</h3>';
75     $s .= '<div id="wppost-enable-wrapper">';
76     $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
77     $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
78     $s .= '</div><div class="clear"></div>';
79
80     $s .= '<div id="wppost-username-wrapper">';
81     $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
82     $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
83     $s .= '</div><div class="clear"></div>';
84
85     $s .= '<div id="wppost-password-wrapper">';
86     $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
87     $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
88     $s .= '</div><div class="clear"></div>';
89
90     $s .= '<div id="wppost-blog-wrapper">';
91     $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
92     $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
93     $s .= '</div><div class="clear"></div>';
94
95     $s .= '<div id="wppost-bydefault-wrapper">';
96     $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
97     $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
98     $s .= '</div><div class="clear"></div>';
99
100     /* provide a submit button */
101
102     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
103
104 }
105
106
107 function wppost_settings_post(&$a,&$b) {
108
109         if(x($_POST,'wppost-submit')) {
110
111                 set_pconfig(local_user(),'wppost','post',intval($_POST['wppost']));
112                 set_pconfig(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
113                 set_pconfig(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
114                 set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
115                 set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
116
117         }
118
119 }
120
121 function wppost_post_local(&$a,&$b) {
122
123         // This can probably be changed to allow editing by pointing to a different API endpoint
124
125         if($b['edit'])
126                 return;
127
128         if((! local_user()) || (local_user() != $b['uid']))
129                 return;
130
131         if($b['private'] || $b['parent'])
132                 return;
133
134     $wp_post   = intval(get_pconfig(local_user(),'wppost','post'));
135
136         $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
137
138         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default')))
139                 $wp_enable = 1;
140
141     if(! $wp_enable)
142        return;
143
144     if(strlen($b['postopts']))
145        $b['postopts'] .= ',';
146      $b['postopts'] .= 'wppost';
147 }
148
149
150
151
152 function wppost_send(&$a,&$b) {
153
154     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
155         return;
156
157     if(! strstr($b['postopts'],'wppost'))
158         return;
159
160     if($b['parent'] != $b['id'])
161         return;
162
163
164         $wp_username = xmlify(get_pconfig($b['uid'],'wppost','wp_username'));
165         $wp_password = xmlify(get_pconfig($b['uid'],'wppost','wp_password'));
166         $wp_blog = get_pconfig($b['uid'],'wppost','wp_blog');
167
168         if($wp_username && $wp_password && $wp_blog) {
169
170                 require_once('include/bbcode.php');
171                 require_once('include/html2plain.php');
172
173                 // If the title is empty then try to guess
174                 if ($b['title'] == '') {
175                         // Take the description from the bookmark
176                         if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
177                                 $b['title'] = $matches[2];
178
179                         // If no bookmark is found then take the first line
180                         if ($b['title'] == '') {
181                                 $title = html2plain(bbcode($b['body']), 0, true);
182                                 $pos = strpos($title, "\n");
183                                 if (($pos == 0) or ($pos > 60))
184                                         $pos = 60;
185
186                                 $b['title'] = substr($title, 0, $pos);
187                         }
188                 }
189
190                 $title = '<title>' . (($b['title']) ? $b['title'] : t('Post from Friendica')) . '</title>';
191                 $post = $title . bbcode($b['body']);
192                 $post = xmlify($post);
193
194                 $xml = <<< EOT
195 <?xml version=\"1.0\" encoding=\"utf-8\"?>
196 <methodCall>
197   <methodName>blogger.newPost</methodName>
198   <params>
199     <param><value><string/></value></param>
200     <param><value><string/></value></param>
201     <param><value><string>$wp_username</string></value></param>
202     <param><value><string>$wp_password</string></value></param>
203     <param><value><string>$post</string></value></param>
204     <param><value><int>1</int></value></param>
205   </params>
206 </methodCall>
207
208 EOT;
209
210                 logger('wppost: data: ' . $xml, LOGGER_DATA);
211
212                 if($wp_blog !== 'test')
213                         $x = post_url($wp_blog,$xml);
214                 logger('posted to wordpress: ' . (($x) ? $x : ''), LOGGER_DEBUG);
215
216         }
217 }
218