29 Nov 2008

Wordbook Status Widget

Posted by havvg

Wordbook Status Widget: Code

Hier der gesamte Quellcode für das Widget.

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
<?php
/**
 *
 * @author Toni Uebernickel <tuebernickel@whitestarprogramming.de>
 * @example http://toni.uebernickel.info/
 * @copyright http://sam.zoy.org/wtfpl/
 * @version  0.3b
 * @name Wordbook Status Widget
 * @todo make defaultUserId a dropdown of registered users with a given sessionkey
 *
 */
 
// actually there is no need to overwrite these
define( 'WORDBOOK_WIDGET_NAME', 'Wordbook Status' );
define( 'WORDBOOK_WIDGET_NAMESLUG', 'wordbook-status');
define( 'WORDBOOK_WIDGET_DEFAULT_TITLE', 'Wordbook Status');
 
function widget_wordbook_init()
{
	global $user_ID;
 
	// check to see required Widget API functions are defined...
	if ( !function_exists( 'register_sidebar_widget' ) || !function_exists( 'register_widget_control' ) )
	{
		// ...and if not, exit gracefully from the script
		return;
	}
 
	/**
	 * actual widget, that's shown within sidebar
	 *
	 * @param array $args
	 */
	function widget_wordbook( $args )
	{
		// exract theme strings
		extract( $args );
 
		// collect our widget's options.
		$options = get_option( 'widget_' . WORDBOOK_WIDGET_NAMESLUG );
 
		// if no title is set in options, use default
		if ( empty( $options[ 'title' ] ) )
		{
			$title = WORDBOOK_WIDGET_DEFAULT_TITLE;
		}
		// a title was set, so use this one
		else
		{
			$title = $options[ 'title' ];
		}
 
		// no userid given?
		if ( is_null( $user_ID ) )
		{
			// use set userid in options
			if ( isset( $options[ 'defaultUserId' ] ) )
			{
				$user_ID = intval($options[ 'defaultUserId' ]);
			}
 
			// no userid set in options either?
			if ( is_null( $user_ID ) or !$user_ID )
			{
				// defaults to '2',
				// as after installing wordpress, the "owner" is the user with id 2
				$user_ID = 2;
			}
		}
 
		// gather information from facebook
		// disable error output, so no sidebar gets "killed" :)
		@$fbclient = wordbook_fbclient( wordbook_get_userdata( $user_ID ) );
		// extract variables from facebook RESTful webservice
		list($fbuid, $users, $error_code, $error_msg) =
			@wordbook_fbclient_getinfo($fbclient, array(
				'has_added_app',
				'first_name',
				'name',
				'status',
				'pic',
				));
		$profile_url = "http://www.facebook.com/profile.php?id=$fbuid";
 
		// only output anything, if nothing went wrong
		if ($fbuid)
		{
			// and any user is given
			if ( is_array( $users ) )
			{
				// retrieve user information
				$user = &$users[ 0 ];
 
				// for special integration, its possible to disable the theme widget frame
				// I'm sorry for this doubled negation
				if ( isset( $options[ 'noThemeFrame' ] ) and !$options[ 'noThemeFrame' ] )
				{
					// theme frame
					echo $before_widget;
					// title with theme frame
					echo $before_title . $title . $after_title;
				}
				// if noThemeFrame option is clearly set
				elseif ( isset( $options[ 'noThemeFrame' ] ) and $options[ 'noThemeFrame' ] === true )
				{
					// make the complete content available within one object
					// for better usage within CSS
					echo '<div id="wordbook-notheme-statuslayer">';
				}
 
				// retrieve a more personal name, if given
				if ( !isset( $user[ 'first_name' ] ) or !( $name = $user[ 'first_name' ] ) )
				{
					$name = $user[ 'name' ];
				}
 
				// widget options set a special name, to overwrite this display
				if ( isset( $options[ 'overwriteName' ] ) )
				{
					$name = $options[ 'overwriteName' ];
				}
 
				// only show pic, if options set and pic available
				if ( isset( $options[ 'showPic' ] ) and $options[ 'showPic' ] and $user[ 'pic' ] )
				{
					echo '<a class="wordbook_picture wordbook_profilelink" href="', $profile_url ,'" target="facebook"><img src="', $user[ 'pic' ], '" /></a>';
				}
 
				// write status message
				if ( isset( $user[ 'status' ][ 'message' ] ) and $user[ 'status' ][ 'message' ] )
				{
					//echo '<p><a href="', $profile_url, '>', $name, '</a><i>', $user['status']['message'], '</i>(', date( 'D M j, g:i a', $user[ 'status' ][ 'time' ] ),').</p>';
					echo '<strong id="wordbook-post"><a class="wordbook_profilelink" href="', $profile_url ,'" target="facebook">', $name, '</a> ', $user[ 'status' ][ 'message' ], '</strong>';
				}
 
				// for special integration, its possible to disable the theme widget frame
				if ( isset( $options[ 'noThemeFrame' ] ) and !$options[ 'noThemeFrame' ] )
				{
					// theme
					echo $after_widget;
				}
				// if noThemeFrame option is clearly set
				elseif ( isset( $options[ 'noThemeFrame' ] ) and $options[ 'noThemeFrame' ] === true )
				{
					echo '</div>';
				}
			}
		}
	}
 
	/**
	 * control function for Wordbook Status widget
	 */
	function widget_wordbook_control()
	{
		// register all available options for this widget
		$availableOptions = array(
			// the title within the sidebar
			'title',
			// a name, that will overwrite the given facebook name
			'overwriteName',
			// an option to disable theme widget frame (before_widget, after_widget ..)
			'noThemeFrame',
			// a default user on which the facebook request is made
			'defaultUserId',
		);
 
		// get saved widget options
		$options = get_option( 'widget_' . WORDBOOK_WIDGET_NAMESLUG );
 
		// on control submit
		if ( $_POST[ WORDBOOK_WIDGET_NAMESLUG.'-submit' ] )
		{
			// go through all options and set them
			foreach ( $availableOptions as $eachOption )
			{
				$eachPostValue = &$_POST[ WORDBOOK_WIDGET_NAMESLUG . '-' . $eachOption ];
 
				// checkboxes
				if ( $eachPostValue === 'on' )
				{
					$newOptions[ $eachOption ] = true;
				}
				else
				{
					$newOptions[ $eachOption ] = strip_tags( stripslashes( $eachPostValue ) );
				}
			}
 
			// update on change only
			if ( $options != $newOptions )
			{
				// overwrite old options, so the form (widget control) is filled with new values, too
				$options = $newOptions;
				update_option( 'widget_' . WORDBOOK_WIDGET_NAMESLUG, $options );
			}
		}
 
		// escape options
		foreach ( $availableOptions as $eachOption )
		{
			// simulate php extract() function with callback on htmlspecialchars()
			if ( isset( $options[ $eachOption ]))
			{
				$$eachOption = htmlspecialchars( $options[ $eachOption ], ENT_QUOTES );
			}
			else
			{
				$$eachOption = null;
			}
		}
 
		// the html for the widget control panel (settings)
		$controlHTML = '
			<p><label for="' . WORDBOOK_WIDGET_NAMESLUG . '-title">Title: <br /><input type="text" id="' . WORDBOOK_WIDGET_NAMESLUG . '-title" name="' . WORDBOOK_WIDGET_NAMESLUG . '-title" value="' . $title . '" /></label></p>
			<p><label for="' . WORDBOOK_WIDGET_NAMESLUG . '-overwriteName">Name: <br /><input type="text" id="' . WORDBOOK_WIDGET_NAMESLUG . '-overwriteName" name="' . WORDBOOK_WIDGET_NAMESLUG . '-overwriteName" value="' . $overwriteName . '" /></label></p>
			<fieldset>
				<legend>Advanced (leave untouched, if unsure)</legend>
				<p><label for="' . WORDBOOK_WIDGET_NAMESLUG . '-defaultUserId">Default UserID: <br /><input type="text" id="' . WORDBOOK_WIDGET_NAMESLUG . '-defaultUserId" name="' . WORDBOOK_WIDGET_NAMESLUG . '-defaultUserId" value="' . $defaultUserId . '" /></label></p>
				<p><label for="' . WORDBOOK_WIDGET_NAMESLUG . '-noThemeFrame"><input type="checkbox" id="' . WORDBOOK_WIDGET_NAMESLUG . '-noThemeFrame" name="' . WORDBOOK_WIDGET_NAMESLUG . '-noThemeFrame"' . ($noThemeFrame ? ' checked="checked"' : '') . ' /> disable ThemeFrame</label></p>
			</fieldset>
			<input type="hidden" name="' . WORDBOOK_WIDGET_NAMESLUG . '-submit" id="' . WORDBOOK_WIDGET_NAMESLUG . '-submit" value="1" />
		';
 
		echo $controlHTML;
	}
 
	// register widget to sidebar
	register_sidebar_widget( WORDBOOK_WIDGET_NAME, 'widget_wordbook' );
 
	// register light control
	register_widget_control( WORDBOOK_WIDGET_NAME, 'widget_wordbook_control' );
}
 
add_action( 'plugins_loaded', 'widget_wordbook_init' );

Pages: 1 2 3

Leave a Reply

Message: