cgit logo

index : defsem-widgets.git/defsem-widgets.php

Defsem Widgets plugin for WordPress

Dean Scarff
summaryrefslogtreecommitdiff
blob: b24a900f27eb42dea86e485c8c11e88bc0abcffc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
 * Plugin Name: Defsem Widgets
 * Version: 1.4
 * Description: Widget collection for twitter, del.icio.us and last.fm
 * Author: Dean Scarff
 * Author URI: http://scarff.id.au/
 *
 * Copyright 2008  Dean Scarff  <dos@scarff.id.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.
 */
require_once(ABSPATH . WPINC . '/functions.php');
require_once(ABSPATH . WPINC . '/widgets.php');
require_once(ABSPATH . WPINC . '/script-loader.php');

// wordpress registration /////////////////////////////////////////////
function defsem_widgets_register() {
  $options = get_option('widget_defsem');
  if (false === $options) {
    $options = array(
      twitter => array(user => 'p00ya', count => 3),
      delicious => array(user => 'p00ya', count => 10),
      lastfm => array(user => 'p00ya', count => 2));
    add_option('widget_defsem', serialize($defsem_widgets_options), '', 'no');
  } elseif (is_string($options)) {
    $options = unserialize($options);
  }
  $GLOBALS['defsem_widgets_options'] = $options;

  register_sidebar_widget('twitter', 'widget_twitter');
  register_widget_control('twitter', 'widget_twitter_control');
  register_sidebar_widget('del.icio.us', 'widget_delicious');
  register_widget_control('del.icio.us', 'widget_delicious_control');
  register_sidebar_widget('delicious tag', 'widget_delicious_tag');
  register_widget_control('delicious tag', 'widget_delicious_control');
  register_sidebar_widget('last.fm', 'widget_lastfm');
  register_widget_control('last.fm', 'widget_lastfm_control');
  add_action('wp_footer', 'defsem_widgets_footer');

  wp_enqueue_script('defsem-widgets/common',
    '/wp-content/plugins/defsem-widgets/common.min.js',
    array('jquery'));
}
defsem_widgets_register();

function defsem_widgets_update_option() {
  update_option('widget_defsem', serialize($GLOBALS['defsem_widgets_options']));
}

function defsem_widgets_control($service) {
  $options =& $GLOBALS['defsem_widgets_options'][$service];
  if (is_null($options))
    $options = array();
  $changed = false;
  if (preg_match('/\s*([0-9]+)/', $_POST["widget_${service}_count"], $m)) {
    $options['count'] = (int)$m[1];
    $changed = true;
  }
  if (preg_match('/\s*([A-Za-z0-9]+)/', $_POST["widget_{$service}_user"], $m)) {
    $options['user'] = $m[1];
    $changed = true;
  }
  if ($_POST["widget_{$service}_proxy"]) {
    $options['proxy'] = true;
    $changed = true;
  }
  if ($changed)
    defsem_widgets_update_option();

  echo <<<END_HTML
    <p><label for="widget-${service}-user">Username
      <input id="widget-${service}-user" name="widget_${service}_user" class="widefat" type="text" value="{$options['user']}" />
      </label>
    </p>
    <p><label for="widget-${service}-count">Count
      <input id="widget-${service}-count" name="widget_${service}_count" class="widefat" type="text" value="{$options['count']}" />
      </label>
    </p>
END_HTML;
  if ('delicious' === $service) {
    echo <<<END_HTML
<p><small>Note that the settings are shared between the "delicious tag" widget
  and the "deli.icio.us" widget.</small></p>
END_HTML;
  }
  if ('twitter' !== $service) {
    $checked = $options['proxy'] ? 'checked="checked"' : '';
    echo <<<END_HTML
    <p><label for="widget-${service}-proxy" title="Serve webservice from this blog's server.  This saves the browser from making an additional connection, but may reduce parallelism.">
      <input id="widget-${service}-proxy" name="widget_${service}_proxy" class="checkbox" type="checkbox"$checked />
       Proxy request
      </label>
    </p>
END_HTML;
  }
}

function defsem_widgets_footer() {
  echo <<<END_HTML
    <script type="text/javascript" defer="defer">/* <![CDATA[ */
jQuery.ajaxSetup({cache: true, ifModified: true, timeout: 20000});

END_HTML;
  echo $GLOBALS['defsem_widgets_script'];
  echo <<<END_HTML
  /* ]]> */</script>
END_HTML;
}

// twitter ////////////////////////////////////////////////////////////

function widget_twitter_script() {
  extract($GLOBALS['defsem_widgets_options']['twitter']);
  if ($proxy) {
    $siteurl = get_option('siteurl');
    $url = "$siteurl/wp-content/plugins/defsem-widgets/proxy.php?service=twitter&";
  } else {
    $url = "http://twitter.com/statuses/user_timeline/$user.json?";
  }
  return <<<END_HTML
jQuery(function(\$){\$.ajax({url: '{$url}count=$count&callback=widgetTwitterCb', dataType: 'script', cache: false, ifModified: false})});

END_HTML;
}

function widget_twitter_control() {
  defsem_widgets_control('twitter');
}

function widget_twitter($args = array()) {
  extract($args);
  extract($GLOBALS['defsem_widgets_options']['twitter']);
  if (!is_string($user) && is_numeric($count))
    return;
  $title = '<a href="http://twitter.com/'. $user .'" title="twitter" rel="me">Activity</a>';
  echo $before_widget . $before_title . $title . $after_title;
  echo '<ul id="twitter_update_list"><li></li></ul>';
  echo $after_widget . "\n";
  $GLOBALS['defsem_widgets_script'] .= widget_twitter_script();
}

// del.icio.us ////////////////////////////////////////////////////////

function widget_delicious_script() {
  extract($GLOBALS['defsem_widgets_options']['delicious']);
  if ($proxy) {
    $siteurl = get_option('siteurl');
    $url = "$siteurl/wp-content/plugins/defsem-widgets/proxy.php?service=delicious&";
  } else {
    $url = "http://feeds.delicious.com/v2/json/$user?";
  }
  return <<<END_HTML
jQuery(function(\$){\$.getScript('{$url}count=$count&callback=widgetDeliciousCb')});

END_HTML;
}

function widget_delicious_control() {
  defsem_widgets_control('delicious');
}

function widget_delicious($args = array()) {
  extract($args);
  extract($GLOBALS['defsem_widgets_options']['delicious']);
  if (!is_string($user) && is_numeric($count))
    return;
  $title = '<a href="http://del.icio.us/'. $user .'/" title="del.icio.us" rel="me">Links</a>';
  echo $before_widget . $before_title . $title . $after_title;
  echo '<ul id="delicious_post_list"><li></li></ul>';
  echo $after_widget . "\n";
  $GLOBALS['defsem_widgets_script'] .= widget_delicious_script();
}

// del.icio.us tag ////////////////////////////////////////////////////

function widget_delicious_tag_script() {
  if (!is_tag())
    return; // only display for tag archives
  extract($GLOBALS['defsem_widgets_options']['delicious']);
  $tag = single_tag_title('', false);
  if ($proxy) {
    $siteurl = get_option('siteurl');
    $url = "$siteurl/wp-content/plugins/defsem-widgets/proxy.php?service=delicious_tag&tag=$tag&";
  } else {
    $url = "http://feeds.delicious.com/v2/json/network/$user/$tag?";
  }
  return <<<END_HTML
jQuery(function(\$){\$.getScript('{$url}count=$count&callback=widgetDeliciousTagCb')});

END_HTML;
}

function widget_delicious_tag($args = array()) {
  if (!is_tag())
    return; // only display for tag archives
  extract($args);
  extract($GLOBALS['defsem_widgets_options']['delicious']);
  $tag = single_tag_title('', false);
  if (!is_string($user) && is_numeric($count))
    return;
  $title = "<a href='http://del.icio.us/network/$user/$tag' title='del.icio.us' rel='me'>$tag Links</a>";
  echo $before_widget . $before_title . $title . $after_title;
  echo '<ul id="delicious_tag_post_list"><li>No links</li></ul>';
  echo $after_widget . "\n";
  $GLOBALS['defsem_widgets_script'] .= widget_delicious_tag_script();
}


// last.fm ////////////////////////////////////////////////////////////

function widget_lastfm_script() {
  extract($GLOBALS['defsem_widgets_options']['lastfm']);
  $siteurl = get_option('siteurl');
  return <<<END_HTML
jQuery(function(\$){\$.getScript('$siteurl/wp-content/plugins/defsem-widgets/proxy.php?service=lastfm&user=$user&count=$count&callback=widgetLastfmCb')});

END_HTML;
}

function widget_lastfm_control() {
  defsem_widgets_control('lastfm');
}

function widget_lastfm($args = array()) {
  if (is_array($args))
    extract($args);
  extract($GLOBALS['defsem_widgets_options']['lastfm']);
  if (!is_string($user) && is_numeric($count))
    return;
  $title = '<a href="http://www.last.fm/user/'. $user .'" title="last.fm" rel="me">Tunes</a>';
  echo $before_widget . $before_title . $title . $after_title;
  echo '<ul id="lastfm_recent_list"><li>Silence</li></ul>';
  echo $after_widget . "\n";
  $GLOBALS['defsem_widgets_script'] .= widget_lastfm_script();
}

?>
generated by cgit v0.8.3.3 at 2013-05-26 05:10:44 (GMT)