]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
permission change 644 to 755 for addons
[friendica-addons.git] / wppost / wppost.php
1 <?php
2
3 /**
4  * Name: WordPress Post Connector
5  * Version: 1.0
6  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
7  */
8
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');
15
16 }
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');
23
24         // obsolete - remove
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');
28
29 }
30
31
32 function wppost_jot_nets(&$a,&$b) {
33     if(! local_user())
34         return;
35
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>';
42     }
43 }
44
45
46 function wppost_settings(&$a,&$s) {
47
48     if(! local_user())
49         return;
50
51     /* Add our stylesheet to the page so we can make our settings look nice */
52
53     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
54
55     /* Get the current state of our config variables */
56
57     $enabled = get_pconfig(local_user(),'wppost','post');
58
59     $checked = (($enabled) ? ' checked="checked" ' : '');
60
61     $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
62
63     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
64
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');
68
69
70     /* Add some HTML to the existing form */
71
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>';
78
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>';
83
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>';
88
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>';
93
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>';
98
99     /* provide a submit button */
100
101     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
102
103 }
104
105
106 function wppost_settings_post(&$a,&$b) {
107
108         if(x($_POST,'wppost-submit')) {
109
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']));
115
116         }
117
118 }
119
120 function wppost_post_local(&$a,&$b) {
121
122         // This can probably be changed to allow editing by pointing to a different API endpoint
123
124         if($b['edit'])
125                 return;
126
127         if((! local_user()) || (local_user() != $b['uid']))
128                 return;
129
130         if($b['private'] || $b['parent'])
131                 return;
132
133     $wp_post   = intval(get_pconfig(local_user(),'wppost','post'));
134
135         $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
136
137         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default')))
138                 $wp_enable = 1;
139
140     if(! $wp_enable)
141        return;
142
143     if(strlen($b['postopts']))
144        $b['postopts'] .= ',';
145      $b['postopts'] .= 'wppost';
146 }
147
148
149
150
151 function wppost_send(&$a,&$b) {
152
153     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
154         return;
155
156     if(! strstr($b['postopts'],'wppost'))
157         return;
158
159     if($b['parent'] != $b['id'])
160         return;
161
162
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');
166
167         if($wp_username && $wp_password && $wp_blog) {
168
169                 require_once('include/bbcode.php');
170
171                 $title = '<title>' . (($b['title']) ? $b['title'] : t('Post from Friendica')) . '</title>';
172                 $post = $title . bbcode($b['body']);
173                 $post = xmlify($post);
174
175                 $xml = <<< EOT
176
177 <?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
178 <methodCall>
179   <methodName>blogger.newPost</methodName>
180   <params>
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>
187   </params>
188 </methodCall>
189
190 EOT;
191
192                 logger('wppost: data: ' . $xml, LOGGER_DATA);
193
194                 if($wp_blog !== 'test')
195                         $x = post_url($wp_blog,$xml);
196                 logger('posted to wordpress: ' . ($x) ? $x : '');
197
198         }
199 }
200