]> git.mxchange.org Git - friendica-addons.git/blob - wppost/wppost.php
Reduce the opacity of the network settings icon when disabled.
[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     $checked = (($enabled) ? ' checked="checked" ' : '');
60
61     $css = (($enabled) ? '' : '-disabled');
62
63     $def_enabled = get_pconfig(local_user(),'wppost','post_by_default');
64     $back_enabled = get_pconfig(local_user(),'wppost','backlink');
65
66     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
67     $back_checked = (($back_enabled) ? ' checked="checked" ' : '');
68
69         $wp_username = get_pconfig(local_user(), 'wppost', 'wp_username');
70         $wp_password = get_pconfig(local_user(), 'wppost', 'wp_password');
71         $wp_blog = get_pconfig(local_user(), 'wppost', 'wp_blog');
72
73
74     /* Add some HTML to the existing form */
75
76     $s .= '<span id="settings_wppost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
77     $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. t('Wordpress Export').'</h3>';
78     $s .= '</span>';
79     $s .= '<div id="settings_wppost_expanded" class="settings-block" style="display: none;">';
80     $s .= '<span class="fakelink" onclick="openClose(\'settings_wppost_expanded\'); openClose(\'settings_wppost_inflated\');">';
81     $s .= '<img class="connector'.$css.'" src="images/wordpress.png" /><h3 class="connector">'. t('Wordpress Export').'</h3>';
82     $s .= '</span>';
83     $s .= '<div id="wppost-enable-wrapper">';
84     $s .= '<label id="wppost-enable-label" for="wppost-checkbox">' . t('Enable WordPress Post Plugin') . '</label>';
85     $s .= '<input id="wppost-checkbox" type="checkbox" name="wppost" value="1" ' . $checked . '/>';
86     $s .= '</div><div class="clear"></div>';
87
88     $s .= '<div id="wppost-username-wrapper">';
89     $s .= '<label id="wppost-username-label" for="wppost-username">' . t('WordPress username') . '</label>';
90     $s .= '<input id="wppost-username" type="text" name="wp_username" value="' . $wp_username . '" />';
91     $s .= '</div><div class="clear"></div>';
92
93     $s .= '<div id="wppost-password-wrapper">';
94     $s .= '<label id="wppost-password-label" for="wppost-password">' . t('WordPress password') . '</label>';
95     $s .= '<input id="wppost-password" type="password" name="wp_password" value="' . $wp_password . '" />';
96     $s .= '</div><div class="clear"></div>';
97
98     $s .= '<div id="wppost-blog-wrapper">';
99     $s .= '<label id="wppost-blog-label" for="wppost-blog">' . t('WordPress API URL') . '</label>';
100     $s .= '<input id="wppost-blog" type="text" name="wp_blog" value="' . $wp_blog . '" />';
101     $s .= '</div><div class="clear"></div>';
102
103     $s .= '<div id="wppost-bydefault-wrapper">';
104     $s .= '<label id="wppost-bydefault-label" for="wppost-bydefault">' . t('Post to WordPress by default') . '</label>';
105     $s .= '<input id="wppost-bydefault" type="checkbox" name="wp_bydefault" value="1" ' . $def_checked . '/>';
106     $s .= '</div><div class="clear"></div>';
107
108     $s .= '<div id="wppost-backlink-wrapper">';
109     $s .= '<label id="wppost-backlink-label" for="wppost-backlink">' . t('Provide a backlink to the Friendica post') . '</label>';
110     $s .= '<input id="wppost-backlink" type="checkbox" name="wp_backlink" value="1" ' . $back_checked . '/>';
111
112     $s .= '</div><div class="clear"></div>';
113
114     /* provide a submit button */
115
116     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="wppost-submit" name="wppost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
117
118 }
119
120
121 function wppost_settings_post(&$a,&$b) {
122
123         if(x($_POST,'wppost-submit')) {
124
125                 set_pconfig(local_user(),'wppost','post',intval($_POST['wppost']));
126                 set_pconfig(local_user(),'wppost','post_by_default',intval($_POST['wp_bydefault']));
127                 set_pconfig(local_user(),'wppost','wp_username',trim($_POST['wp_username']));
128                 set_pconfig(local_user(),'wppost','wp_password',trim($_POST['wp_password']));
129                 set_pconfig(local_user(),'wppost','wp_blog',trim($_POST['wp_blog']));
130                 set_pconfig(local_user(),'wppost','backlink',trim($_POST['wp_backlink']));
131
132         }
133
134 }
135
136 function wppost_post_local(&$a,&$b) {
137
138         // This can probably be changed to allow editing by pointing to a different API endpoint
139
140         if($b['edit'])
141                 return;
142
143         if((! local_user()) || (local_user() != $b['uid']))
144                 return;
145
146         if($b['private'] || $b['parent'])
147                 return;
148
149     $wp_post   = intval(get_pconfig(local_user(),'wppost','post'));
150
151         $wp_enable = (($wp_post && x($_REQUEST,'wppost_enable')) ? intval($_REQUEST['wppost_enable']) : 0);
152
153         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'wppost','post_by_default')))
154                 $wp_enable = 1;
155
156     if(! $wp_enable)
157        return;
158
159     if(strlen($b['postopts']))
160        $b['postopts'] .= ',';
161      $b['postopts'] .= 'wppost';
162 }
163
164
165
166
167 function wppost_send(&$a,&$b) {
168
169     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
170         return;
171
172     if(! strstr($b['postopts'],'wppost'))
173         return;
174
175     if($b['parent'] != $b['id'])
176         return;
177
178
179         $wp_username = xmlify(get_pconfig($b['uid'],'wppost','wp_username'));
180         $wp_password = xmlify(get_pconfig($b['uid'],'wppost','wp_password'));
181         $wp_blog = get_pconfig($b['uid'],'wppost','wp_blog');
182
183         if($wp_username && $wp_password && $wp_blog) {
184
185                 require_once('include/bbcode.php');
186                 require_once('include/html2plain.php');
187
188                 $wptitle = trim($b['title']);
189
190                 // If the title is empty then try to guess
191                 if ($wptitle == '') {
192                         // Take the description from the bookmark
193                         if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
194                                 $wptitle = $matches[2];
195
196                         // If no bookmark is found then take the first line
197                         if ($wptitle == '') {
198                                 // Remove the share element before fetching the first line
199                                 $title = trim(preg_replace("/\[share.*?\](.*?)\[\/share\]/ism","\n$1\n",$b['body']));
200
201                                 $title = html2plain(bbcode($title, false, false), 0, true)."\n";
202                                 $pos = strpos($title, "\n");
203                                 $trailer = "";
204                                 if (($pos == 0) or ($pos > 100)) {
205                                         $pos = 100;
206                                         $trailer = "...";
207                                 }
208
209                                 $wptitle = substr($title, 0, $pos).$trailer;
210                         }
211                 }
212
213                 $title = '<title>' . (($wptitle) ? $wptitle : t('Post from Friendica')) . '</title>';
214                 $post = bbcode($b['body'], false, false, 4);
215
216                 // If a link goes to youtube then remove the stuff around it. Wordpress detects youtube links and embeds it
217                 $post = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
218                 $post = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$post);
219
220                 $post = $title.$post;
221
222                 $wp_backlink = intval(get_pconfig($b['uid'],'wppost','backlink'));
223                 if($wp_backlink && $b['plink'])
224                         $post .= EOL . EOL . '<a href="' . $b['plink'] . '">'
225                                 . t('Read the original post and comment stream on Friendica') . '</a>' . EOL . EOL;
226
227                 $post = xmlify($post);
228
229
230                 $xml = <<< EOT
231 <?xml version=\"1.0\" encoding=\"utf-8\"?>
232 <methodCall>
233   <methodName>blogger.newPost</methodName>
234   <params>
235     <param><value><string/></value></param>
236     <param><value><string/></value></param>
237     <param><value><string>$wp_username</string></value></param>
238     <param><value><string>$wp_password</string></value></param>
239     <param><value><string>$post</string></value></param>
240     <param><value><int>1</int></value></param>
241   </params>
242 </methodCall>
243
244 EOT;
245
246                 logger('wppost: data: ' . $xml, LOGGER_DATA);
247
248                 if($wp_blog !== 'test')
249                         $x = post_url($wp_blog,$xml);
250                 logger('posted to wordpress: ' . (($x) ? $x : ''), LOGGER_DEBUG);
251
252         }
253 }
254