]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
479ad6f1699a1773fec04f68af59ce1d6ba5877a
[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_end',   'addon/wppost/wppost.php', 'wppost_send');
11     register_hook('jot_networks',     'addon/wppost/wppost.php', 'wppost_jot_nets');
12     register_hook('plugin_settings',  'addon/wppost/wppost.php', 'wppost_settings');
13     register_hook('plugin_settings_post',  'addon/wppost/wppost.php', 'wppost_settings_post');
14
15 }
16 function wppost_uninstall() {
17     unregister_hook('post_local_end',   'addon/wppost/wppost.php', 'wppost_send');
18     unregister_hook('jot_networks',     'addon/wppost/wppost.php', 'wppost_jot_nets');
19     unregister_hook('plugin_settings',  'addon/wppost/wppost.php', 'wppost_settings');
20     unregister_hook('plugin_settings_post',  'addon/wppost/wppost.php', 'wppost_settings_post');
21 }
22
23
24 function wppost_jot_nets(&$a,&$b) {
25     if(! local_user())
26         return;
27
28     $wp_post = get_pconfig(local_user(),'wppost','post');
29     if(intval($wp_post) == 1) {
30         $wp_defpost = get_pconfig(local_user(),'wppost','post_by_default');
31         $selected = ((intval($wp_defpost) == 1) ? ' checked="checked" ' : '');
32         $b .= '<div class="profile-jot-net"><input type="checkbox" name="wppost_enable"' . $selected . 'value="1" /> '
33             . t('Post to Wordpress') . '</div>';
34     }
35 }
36
37
38 function wppost_settings(&$a,&$s) {
39
40     if(! local_user())
41         return;
42
43     /* Add our stylesheet to the page so we can make our settings look nice */
44
45     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/wppost/wppost.css' . '" media="all" />' . "\r\n";
46
47     /* Get the current state of our config variables */
48
49     $enabled = get_pconfig(local_user(),'wppost','post');
50
51     $checked = (($enabled) ? ' checked="checked" ' : '');
52
53     $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
54
55     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
56
57         $wp_username = get_pconfig(local_user(), 'wppost', 'wp_username');
58         $wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
59         $wp_blog = get_pconfig(local_user(), 'wppost', 'wp_blog');
60
61
62     /* Add some HTML to the existing form */
63
64     $s .= '<div class="settings-block">';
65     $s .= '<h3>' . t('WordPress Post Settings') . '</h3>';
66     $s .= '<div id="wppost-enable-wrapper">';
67     $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
68     $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
69     $s .= '</div><div class="clear"></div>';
70
71     $s .= '<div id="wppost-username-wrapper">';
72     $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
73     $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
74     $s .= '</div><div class="clear"></div>';
75
76     $s .= '<div id="wppost-password-wrapper">';
77     $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
78     $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
79     $s .= '</div><div class="clear"></div>';
80
81     $s .= '<div id="wppost-blog-wrapper">';
82     $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
83     $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
84     $s .= '</div><div class="clear"></div>';
85
86     $s .= '<div id="wppost-bydefault-wrapper">';
87     $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
88     $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
89     $s .= '</div><div class="clear"></div>';
90
91     /* provide a submit button */
92
93     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
94
95 }
96
97
98 function wppost_settings_post(&$a,&$b) {
99
100         if(x($_POST,'wppost-submit')) {
101
102                 set_pconfig(local_user(),'wppost','post',intval($_POST['wppost']));
103                 set_pconfig(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
104                 set_pconfig(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
105                 set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
106                 set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
107
108         }
109
110 }
111
112
113
114
115 function wppost_send(&$a,&$b) {
116
117         if((! local_user()) || (local_user() != $b['uid']))
118                 return;
119
120         if($b['prvnets'] && $b['private'])
121                 return;
122
123         if($b['parent'])
124                 return;
125
126         $wp_username = get_pconfig(local_user(),'wppost','wp_username');
127         $wp_password = get_pconfig(local_user(),'wppost','wp_password');
128         $wp_blog = get_pconfig(local_user(),'wppost','wp_blog');
129     $wp_post   = intval(get_pconfig(local_user(),'wppost','post'));
130
131         $wp_enable = (($wp_post && x($_POST,'wppost_enable')) ? intval($_POST['wppost_enable']) : 0);
132
133         if($_POST['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default')))
134                 $wp_enable = 1;
135
136         if($wp_username && $wp_password && $wp_blog && $wp_post && $wp_enable) {
137
138                 require_once('include/bbcode.php');
139                 $post = xmlify(bbcode($b['body']));
140
141                 $xml = <<< EOT
142
143 <?xml version=\"1.0\" encoding=\"iso-8859-1\"?>
144 <methodCall>
145   <methodName>blogger.newPost</methodName>
146   <params>
147     <param><value><string/></value></param>
148     <param><value><string/></value></param>
149     <param><value><string>$wp_username</string></value></param>
150     <param><value><string>$wp_password</string></value></param>
151     <param><value><string>$post</string></value></param>
152     <param><value><int>1</int></value></param>
153   </params>
154 </methodCall>
155
156 EOT;
157
158                 logger('wppost: data: ' . $xml, LOGGER_DATA);
159
160                 if($wp_blog !== 'test')
161                         $x = post_url($wp_blog,$xml);
162                 logger('posted to wordpress');
163
164         }
165 }
166