-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingiverse-stream-widget.php
executable file
·130 lines (117 loc) · 5.55 KB
/
thingiverse-stream-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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
require_once("lib/thingiverse_stream.php");
/**
* ThingiverseStreamWidget Class
*/
class ThingiverseStreamWidget extends WP_Widget {
protected $widget_slug = 'thingiverse-press';
/** constructor */
public function __construct() {
parent::__construct(
$this->widget_slug, 'Thingiverse Stream', $this->widget_slug, array(
'classname' => $this->widget_slug . '-class',
'description' => 'Display Thingiverse.com streams',
$this->widget_slug
)
);
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title',
empty( $instance['title'] ) ?
__( 'Thingiverse' ) :
$instance['title']);
$type = empty( $instance['type'] ) ? __( 'newest' ) : $instance['type'];
$user = $instance['user'];
$max_items = empty( $instance['max_items'] ) ? __( '3' ) : $instance['max_items'];
$stream = new ThingiverseStream($type, $user);
$logo_url = WP_PLUGIN_URL . "/thingiverse-press/makerbot-thingiverse-logo.png";
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<div class="thingiverse-stream">
<?php
$i = 0;
$numItems = count($stream->things);
foreach ( $stream->things as $thing ) {
$last_class="";
$see_more="";
$title="title=\"".$thing->title."\"";
$thing_url=$thing->url;
if( ($i+1 >= $max_items or $i+1 === $numItems) and $this->is_checked( $instance, 'show_see_more_at_last' )){
$last_class="tv-stream-thing-last";
$title="";
$see_more="<span>See more on <img src=\"".$logo_url."\" /></span>";
$thing_url=$stream->user_url;
}
?>
<div class="tv-stream-thing <?php echo $last_class ?>">
<a href="<?php echo $thing_url ?>"><?php echo $see_more;?><img src="<?php echo $thing->main_image ?>" <?php echo $title;?>/></a>
</div>
<?php $i++; if( $i >= $max_items ) { break; }
}
?>
</div>
<?php echo $after_widget; ?>
<?php
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['type'] = strip_tags($new_instance['type']);
$instance['user'] = strip_tags($new_instance['user']);
$instance['max_items'] = strip_tags($new_instance['max_items']);
$instance['show_see_more_at_last'] = strip_tags($new_instance['show_see_more_at_last']);
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$title = esc_attr($instance['title']);
$type = esc_attr($instance['type']);
$user = esc_attr($instance['user']);
$max_items = esc_attr($instance['max_items']);
$show_see_more_at_last = esc_attr($instance['show_see_more_at_last']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Stream Type:'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>" type="text" value="<?php echo $type; ?>" />
</label>
<br />
Global Streams: <em>newest, featured, popular, derivatives, instances</em><br />
User Streams: <em>designed, likes, made, favorites, collections</em><br />
</p>
<p>
<label for="<?php echo $this->get_field_id('user'); ?>"><?php _e('User Name:'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('user'); ?>" name="<?php echo $this->get_field_name('user'); ?>" type="text" value="<?php echo $user; ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('max_items'); ?>"><?php _e('Number of Things to show (<em>Default: 3</em>):'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('max_items'); ?>" name="<?php echo $this->get_field_name('max_items'); ?>" type="text" value="<?php echo $max_items; ?>" />
</label>
</p>
<p>
<h4>Show</h4>
<input class="checkbox" type="checkbox" <?php checked( ${'show_see_more_at_last'}, 'on' ); ?>
id="<?php echo $this->get_field_id( 'show_see_more_at_last' ) ?>"
name="<?php echo $this->get_field_name( 'show_see_more_at_last' ) ?>"/>
<label for="<?php echo $this->get_field_id( 'show_see_more_at_last' ) ?>">
<?php echo ucfirst( str_replace( '_', ' ', ucfirst( 'show_see_more_at_last' ) ) ) ?>
</label>
<br/>
</p>
<?php
}
public function is_checked( $conf, $name ) {
return isset( $conf[ $name ] ) && $conf[ $name ] == 'on';
}
} // class ThingiverseStreamWidget
?>