forked from morganestes/threads-workday-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads-workday-widget.php
More file actions
181 lines (159 loc) · 6.5 KB
/
threads-workday-widget.php
File metadata and controls
181 lines (159 loc) · 6.5 KB
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
<?php
/*
Plugin Name: Threads OKC Workdays
Plugin URI: http://threadsokc.github.io/workday-widget
GitHub Plugin URI: threadsokc/threads-workday-widget
Description: Adds a widget with the upcoming work day.
Version: 0.2.3
Author: morganestes
Author URI: http://www.morganestes.me
License: GPLv2 or later
Text Domain: threadsokc
*/
namespace ThreadsOKC\Workday;
use WP_Widget;
include_once __DIR__ . '/create-ical.php';
/**
* Adds Threads_Widget widget.
*/
class Threads_Widget extends \WP_Widget {
public $plugin_dir;
public $plugin_url;
public $plugin_name;
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'threads_widget', // Base ID
'Threads Workdays', // Name
array( 'description' => __( 'Display the next scheduled workday', 'threadsokc' ), ) // Args
);
$this->plugin_dir = plugin_dir_path( __FILE__ );
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_name = plugin_basename( __DIR__ );
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
$date = $instance['date'];
$extra_info = apply_filters( 'widget_text', $instance['extra_info'] );
$display_date = date( 'F d, Y', strtotime( $date ) );
$ics_date = date( 'Ymd', strtotime( $date ) );
$event_info = array(
'datestart' => strtotime( $ics_date . 'T190000Z' ),
'dateend' => strtotime( $ics_date . 'T220000Z' ),
'address' => esc_html( '2221 E. Memorial Rd., Edmond, OK 73013', 'threadsokc' ),
'uri' => esc_url( 'http://www.threadsokc.org/events.html' ),
'filename' => "threadsokc-workday-{$date}.ics",
'summary' => __( 'Threads OKC Workday', 'threadsokc' ),
'description' => __( '', 'threadsokc' ),
);
$google_calendar_atts = array(
'sprop' => rawurlencode( __( 'Threads of Compassion OKC', 'threadsokc' ) ),
'location' => rawurlencode( $event_info['address'] ),
'text' => rawurlencode( $event_info['summary'] ),
);
$ics_file = plugin_dir_url( __FILE__ ) . 'create-ical.php';
$ics_url = add_query_arg( $event_info, $ics_file );
$cal_img = $this->plugin_url . 'calendar_add.png';
$nonce = wp_create_nonce( $this->plugin_name );
$nonce_field = wp_nonce_field( 'build_calendar', "{$this->plugin_name}_nonce", false, false );
$ics_form = <<<HTML
<form style="display: inline" method="post" name="build-ics" action="#">
<input type="hidden" name="datestart" value="$event_info[datestart]" />
<input type="hidden" name="dateend" value="$event_info[dateend]" />
<input type="hidden" name="address" value="$event_info[address]" />
<input type="hidden" name="uri" value="$event_info[uri]" />
<input type="hidden" name="filename" value="$event_info[filename]" />
<input type="hidden" name="summary" value="$event_info[summary]" />
<input type="hidden" name="description" value="$event_info[description]" />
{$nonce_field}
<input title="Add to calendar" style="border: none; vertical-align: bottom;" type="image" value="build_calendar" src="$cal_img" />
</form>
HTML;
$google_calendar_link = esc_url( "http://www.google.com/calendar/event?action=TEMPLATE&text={$google_calendar_atts['text']}&dates={$event_info['datestart']}/{$event_info['dateend']}&details=&location={$google_calendar_atts['location']}&trp=false&sprop={$google_calendar_atts['sprop']}&sprop=name:{$event_info['uri']}" );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo "<strong class='date'>$display_date</strong>";
if ( ! empty( $extra_info ) ) {
echo "<p>$extra_info</p>";
}
echo $ics_form;
// echo "<a href='{$google_calendar_link}' target='" . esc_attr( '_blank' ) . "'><img src='//www.google.com/calendar/images/ext/gc_button1.gif' border=0></a>";
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*
* @return string|void
*/
public function form( $instance ) {
if ( $instance ) {
$title = esc_attr( $instance['title'] );
$date = esc_attr( $instance['date'] );
$extra_info = esc_textarea( $instance['extra_info'] );
} else {
$title = '';
$date = '2010-01-01';
$extra_info = '';
}
?>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:' ); ?></label><br>
<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; ?>" />
</p>
<p>
<label for "<?php echo $this->get_field_name( 'date' ); ?>"><?php _e( 'Date:' ); ?></label><br>
<input class="widefat" type="date" id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" value="<?php echo $date; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_name( 'extra_info' ); ?>"><?php _e( 'Extra info:' ); ?></label><br>
<textarea class="widefat" id="<?php echo $this->get_field_id( 'extra_info' ); ?>" name="<?php echo $this->get_field_name( 'extra_info' ); ?>"><?php echo $extra_info; ?></textarea>
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['date'] = ( ! empty( $new_instance['date'] ) ) ? strip_tags( $new_instance['date'] ) : '';
$instance['extra_info'] = ( ! empty( $new_instance['extra_info'] ) ) ? strip_tags( $new_instance['extra_info'] ) : '';
return $instance;
}
} // class Threads_Widget
/** Initialize the widget. */
add_action( 'widgets_init', function () {
register_widget( 'ThreadsOKC\Workday\Threads_Widget' );
} );
/** Handle form submission to generate ICS file. */
add_action( 'init', function () {
if ( ( 'POST' == $_SERVER['REQUEST_METHOD'] )
&& isset( $_POST['threads-next-workday_nonce'] )
) {
$calendar = new Calendar();
$calendar->create_ics();
}
} );