-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordnik-wotd-widget.php
109 lines (81 loc) · 3.04 KB
/
wordnik-wotd-widget.php
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
<?php
/*
Plugin Name: Wordnik Word of the Day Widget
Plugin URI: http://www.wordnik.com
Description: Simple widget to retrieve the Wordnik Word of the Day.
Version: 0.2
Author: Ben Lobaugh
Author URI: http://ben.lobaugh.net
*/
/*
* SETUP THE WIDGET
*/
add_action( 'widgets_init', create_function( '', 'register_widget( "Wordnik_WOTD_Widget" );' ) );
class Wordnik_WOTD_Widget extends WP_Widget{
/**
* Widget constructor that sets up the widget object with the correct
* options
*/
public function __construct() {
parent::__construct(
'wordnik_wotd', // Base ID
'Wordnik Word of the Day', // Name
array( 'description' => __( 'Display the Wordnik Word of the Day', 'text_domain' ), ) // Args
);
}
/**
* Builds the wp-admin widget form in Apperances -> Widgets
*
* @param Array $instance - Data from the current widget
*/
public function form( $instance ) {
// Setup default values if none currently exist
$defaults = array( 'wordnik_api_key' => '' );
// Check current instance for values. If none exist apply defaults
$instance = wp_parse_args( (array) $instance, $defaults );
$s .= "Wordnik API key: <input class='widefat' name='" . $this->get_field_name( 'wordnik_api_key' ) . "' type='text' value='" .
esc_attr( $instance['wordnik_api_key'] ) . "'>";
echo $s;
}
/**
* Determines how to display to site visitors
*
* @param Array $args - WordPress specific actions (before_widget, after_widget, etc)
* @param Array $instance - Widget form elements
*/
public function widget( $args, $instance ) {
extract( $args ); // Just because this is normal
$word = wordnik_wotd_get_word( $instance['wordnik_api_key']);
// Widget display HTML goes here
?>
<?php echo $before_widget;
echo $before_title . "Wordnik Word of the Day" . $after_title;
echo "<strong>{$word['word']}</strong>";
echo "<p>{$word['definition']}</p>";
echo "<p>Example usage: {$word['example']}</p>";
echo $after_widget; ?>
<?php
// End of widget display HTML
}
}
/**
* Retrieves the word of the day from the api
*
* @param String $day - Must be in format of YYYY-MM-DD
*/
function wordnik_wotd_get_word( $api_key, $day = '') {
if($day == '') {
// use today's date
$day = date('Y-m-d');
}
$url = "http://api.wordnik.com//v4/words.json/wordOfTheDay?date=$day&api_key=$api_key";
$response = wp_remote_get( $url );
$word = json_decode( $response['body'], true );
$arr = array(
'word' => ucfirst($word['word']),
'example' => $word['examples']['0']['text'],
'definition' => $word['definitions']['0']['text'],
'partOfSpeech' => $word['definitions']['0']['partOfSpeech']
);
return $arr;
}