-
Notifications
You must be signed in to change notification settings - Fork 9
/
blimply.php
412 lines (358 loc) · 17 KB
/
blimply.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/*
Plugin Name: Blimply
Plugin URI: http://doejo.com
Description: Blimply allows you to send push notifications to your mobile users utilizing Urban Airship API. It sports a post meta box and a dashboard widgets. You have the ability to broadcast pushes, and to push to specific Urban Airship tags as well.
Author: Rinat Khaziev, doejo
Version: 0.6
Author URI: http://doejo.com
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
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; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
define( 'BLIMPLY_VERSION', '0.6' );
define( 'BLIMPLY_ROOT' , dirname( __FILE__ ) );
define( 'BLIMPLY_FILE_PATH' , BLIMPLY_ROOT . '/' . basename( __FILE__ ) );
define( 'BLIMPLY_URL' , plugins_url( '/', __FILE__ ) );
define( 'BLIMPLY_PREFIX' , 'blimply' );
// Composer autoload
require_once BLIMPLY_ROOT . '/vendor/autoload.php';
require_once BLIMPLY_ROOT . '/vendor/rinatkhaziev/wordpress-settings-api-class/class.settings-api.php';
require_once BLIMPLY_ROOT . '/lib/blimply-settings.php';
require_once BLIMPLY_ROOT . '/lib/class.wpairship.php';
use UrbanAirship\WpAirship as Airship;
use UrbanAirship\UALog;
use UrbanAirship\Push as P;
class Blimply {
public $options, $airships, $airship, $tags;
/**
* Instantiate
*/
function __construct() {
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
add_action( 'save_post', array( $this, 'action_save_post' ), 50 );
add_action( 'add_meta_boxes', array( $this, 'post_meta_boxes' ) );
add_action( 'update_option_blimply_options', array( $this, 'sync_airship_tags' ), 5, 2 );
add_action( 'register_taxonomy', array( $this, 'after_register_taxonomy' ), 5, 3 );
add_action( 'create_term', array( $this, 'action_create_term' ), 5, 3 );
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'wp_dashboard_setup', array( $this, 'dashboard_setup' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts_and_styles' ) );
add_action( 'wp_ajax_blimply-send-push', array( $this, 'handle_ajax_post' ) );
}
function dashboard_setup() {
if ( is_blog_admin() && current_user_can( apply_filters( 'blimply_push_cap', 'edit_posts' ) ) )
wp_add_dashboard_widget( 'dashboard_blimply', __( 'Send a Push Notification' ), array( $this, 'dashboard_widget' ) );
}
/**
* Init hook
*
*/
function action_init() {
register_taxonomy( 'blimply_tags', array( 'post' ), array(
'public' => false,
'labels' => array(
'name' => __( 'Urban Airship Tags', 'blimply' ),
'singular_name' => __( 'Urban Airship Tags', 'blimply' ),
),
'show_in_nav_menus' => false,
'show_ui' => true
) );
load_plugin_textdomain( 'blimply', false, dirname( plugin_basename( __FILE__ ) ) . '/lib/languages/' );
}
/**
* Set basic app properties
*
*/
function action_admin_init() {
global $pagenow;
// Init the plugin only on proper pages and if doing ajax request
if ( ! in_array( $pagenow, array( 'post-new.php', 'post.php', 'index.php', 'options.php' ) ) && ! defined( 'DOING_AJAX' ) )
return;
$defaults = array(
BLIMPLY_PREFIX . '_app_key' => '',
BLIMPLY_PREFIX . '_app_secret' => '',
BLIMPLY_PREFIX . '_character_limit' => 140,
BLIMPLY_PREFIX . '_quiet_time_from' => '',
BLIMPLY_PREFIX . '_quiet_time_to' => '',
BLIMPLY_PREFIX . '_enable_quiet_time' => ''
);
// Try to set default options if option doesn't exist
$this->options = get_option( 'urban_airship', $defaults );
// Make sure that default options are set properly even if suboption key doesn't exist
// e.g. new option was added in a new verion;
$this->options = array_merge( $defaults, (array) $this->options );
$this->sounds = get_option( 'blimply_sounds' );
$this->airship = new Airship( $this->options['blimply_app_key'], $this->options['blimply_app_secret'] );
// We don't use built-in WP UI, instead we choose tag in custom Blimply meta box
$this->tags = get_terms( 'blimply_tags', array( 'hide_empty' => 0 ) );
}
/**
* Helper function to determine if current time should be quiet time (no push sounds)
*
* @return boolean [description]
*/
function _is_quiet_time() {
$current_time = date( "G:i", current_time( 'timestamp' ) );
$quiet_from = $this->options[BLIMPLY_PREFIX . '_quiet_time_from'];
$quiet_to = $this->options[BLIMPLY_PREFIX . '_quiet_time_to'];
$quiet_to_array = explode( ":", $quiet_to );
$is_quiet_from = $quiet_from < $current_time;
$is_quiet_to = ( $quiet_to > $current_time && $quiet_to_array[0] > 12 ) || ( $quiet_to < $current_time && $quiet_to_array[0] < 12 );
return $is_quiet_from && $is_quiet_to && 'on' === $this->options[BLIMPLY_PREFIX . '_enable_quiet_time'];
}
/**
* Register scripts and styles
*
*/
function register_scripts_and_styles() {
global $pagenow;
// Only load this on the proper page
if ( ! in_array( $pagenow, array( 'post-new.php', 'post.php', 'index.php', 'options-general.php' ) ) )
return;
wp_enqueue_style( 'blimply-style', BLIMPLY_URL . '/lib/css/blimply.css' );
wp_enqueue_script( 'timepicker', BLIMPLY_URL . '/lib/js/jquery.timePicker.min.js', array( 'jquery' ) );
wp_enqueue_script( 'blimply-js', BLIMPLY_URL . '/lib/js/blimply.js', array( 'jquery', 'timepicker' ) );
wp_localize_script( 'blimply-js', 'Blimply', array(
'push_sent' => __( 'Push notification successfully sent', 'blimply' ),
'push_error' => __( 'Sorry, there was some error while we were trying to send your push notification. Try again later!', 'blimply' ),
'character_limit' => (int) $this->options[ BLIMPLY_PREFIX .'_character_limit' ]
)
);
}
/**
* Sync our newly created tag with Urban Airship
*
* @param int $term_id term_id
* @param int $tt_id term_taxonomy_id
* @param string $taxonomy
*/
function action_create_term( $term_id, $tt_id, $taxonomy ) {
//
if ( 'blimply_tags' != $taxonomy )
return;
$tag = get_term( $term_id, $taxonomy );
// Let's sync
try {
$response = $this->airship->request( 'PUT', null, $this->airship->buildUrl( "/api/tags/{$tag->slug}" ) );
} catch ( \Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
return $response;
}
/**
* Send a push notification if checkbox is checked
*
* @param int $post_id
*/
function action_save_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || wp_is_post_revision( $post_id ) )
return;
if ( isset( $_POST['blimply_nonce'] ) && !wp_verify_nonce( $_POST['blimply_nonce'], BLIMPLY_FILE_PATH ) )
return;
if ( !current_user_can( apply_filters( 'blimply_push_cap', 'edit_posts' ) ) )
return;
if ( 1 == get_post_meta( $post_id, 'blimply_push_sent', true ) )
return;
if ( isset( $_POST['blimply_push'] ) && 1 == $_POST['blimply_push'] ) {
$alert = !empty( $_POST['blimply_push_alert'] ) ? sanitize_text_field( $_POST['blimply_push_alert'] ) : sanitize_text_field( $_POST['post_title'] );
// Do something specific for save_post hook before we actually make a request
do_action( 'blimply_save_post_before_send_push' );
$response = $this->_send_broadcast_or_push( $alert, $_POST['blimply_push_tag'], get_permalink( $post_id ), (bool) isset( $_POST['blimply_no_sound'] ) && $_POST['blimply_no_sound'] );
if ( ! is_wp_error( $response ) )
update_post_meta( $post_id, 'blimply_push_sent', true );
}
}
/**
* Method to handle AJAX request for Dashboard Widget
*/
function handle_ajax_post() {
if ( !wp_verify_nonce( $_POST['_wpnonce'], 'blimply-send-push' ) )
return;
if ( !current_user_can( apply_filters( 'blimply_push_cap', 'edit_posts' ) ) )
return;
$response = false;
// Truncate to whatever the limit is set (if not 0)
$alert = sanitize_text_field( $_POST['blimply_push_alert'] );
$limit = (int) $this->options[ BLIMPLY_PREFIX . '_character_limit' ];
if ( $limit )
$alert = substr( $alert, 0, $limit );
// Determine if sounds are disabled for the push
$no_sound = isset( $_POST['blimply_no_sound'] ) && $_POST['blimply_no_sound'];
$response = $this->_send_broadcast_or_push( $alert, $_POST['blimply_push_tag'], false, (bool) $no_sound );
wp_send_json( array( 'success' => !is_wp_error( $response ) ) );
}
/**
* Private method to send push or broadcast.
*
* @param string $alert
* @param string $tag
*
*/
function _send_broadcast_or_push( $alert, $tag, $url = false, $disable_sound = false ) {
// Strip escape slashes, otherwise double escaping would happen
$alert = html_entity_decode( stripcslashes( strip_tags( $alert ) ) );
// Grab an instance of PushRequest
$response = $this->airship->push();
// If tag is broadcast use Push\all const, otherwise set audience to tag
$audience = $tag == 'broadcast' ? P\all : P\tag( $tag );
$response->setAudience( $audience );
// Sound part of ios push
if ( !$disable_sound && isset( $this->sounds["blimply_sound_{$tag}"] ) && !empty( $this->sounds["blimply_sound_{$tag}"] ) )
$sound = $this->sounds["blimply_sound_{$tag}"];
// Or use the default sound
elseif ( !$disable_sound )
$sound = 'default';
else
$sound = null;
// Set extra payload arguments, for now it's just the url
$extra = $url ? array( 'url' => $url ) : null;
// iOS and Android specific parts of payload
$ios = P\ios( $alert, '+1', $sound, false, $extra );
$android = P\android( $alert, null, null, null, $extra );
// TODO: Windows and Blackberry payloads (HA!)
// Payload filter (allows to workaround quirks of UA API if any and/or fine tuning of payload data)
$payload = apply_filters( 'blimply_payload_override', P\notification( $alert, array( 'ios' => $ios, 'android' => $android ) ) );
$response->setNotification( $payload )
->setDeviceTypes( P\all )
;
do_action( 'blimply_before_send_push', $payload );
try {
$response->send();
} catch ( \Exception $e ) {
return new WP_Error( $e->getCode(), $e->getMessage() );
}
return $response;
}
/**
* Register metabox for selected post types
*
* @todo implement ability to actually pick specific post types
*/
function post_meta_boxes() {
// Enable meta box for all public post types by default but allow to override with filters
$post_types = apply_filters( 'blimply_enabled_post_types', get_post_types( array( 'public' => true ), 'objects' ) );
foreach ( $post_types as $post_type => $props )
add_meta_box( BLIMPLY_PREFIX, __( 'Push Notification', 'blimply' ), array( $this, 'post_meta_box' ), $post_type, 'side' );
}
/**
* Render HTML
*
* @todo make HTML prettier with instead of peppering everythin with line breaks
*/
function post_meta_box( $post ) {
$is_push_sent = get_post_meta( $post->ID, 'blimply_push_sent', true );
if ( 1 != $is_push_sent ) {
echo '<div class="blimply-wrapper">';
wp_nonce_field( BLIMPLY_FILE_PATH, 'blimply_nonce' );
echo '<label for="blimply_push_alert">';
esc_html_e( 'Push message', 'blimply' );
$nice_warning = __( 'Keep in mind that all HTML will be stripped out, and refrain from putting any links in the message.', 'blimply' );
// 0 means no limit
$limit = (int) apply_filters( BLIMPLY_PREFIX . '_character_limit', $this->options[BLIMPLY_PREFIX . '_character_limit'] );
$limit_html = $limit ? sprintf( ' maxlength="%d" ', $limit ) : '';
echo '</label><br/><small>' . esc_html( $nice_warning ) . ' Character limit is: ' . (int) $limit . '</small><br/>';
echo '<textarea id="blimply_push_alert" name="blimply_push_alert" class="bl_textarea"' . $limit_html . '>' . esc_textarea( $post->post_title ) . '</textarea><br/>';
echo '<strong>' . esc_html__( 'Send Push to following Urban Airship tags', 'blimply' ) . '</strong>';
foreach ( (array) $this->tags as $tag ) {
echo '<input type="radio" name="blimply_push_tag" id="blimply_tag_' . esc_attr( $tag->term_id ) . '" value="' . esc_attr( $tag->slug ) . '"/>';
echo '<label class="selectit" for="blimply_tag_' . esc_attr( $tag->term_id ) . '" style="margin-left: 4px">';
echo esc_html( $tag->name );
echo '</label><br/>';
}
if ( isset( $this->options['blimply_allow_broadcast'] ) && $this->options['blimply_allow_broadcast'] == 'on' ) {
echo '<input type="radio" name="blimply_push_tag" id="blimply_tag_broadcast" value="broadcast"/>';
echo '<label class="selectit" for="blimply_tag_broadcast" style="margin-left: 4px">';
esc_html_e( 'Broadcast (send to all tags)', 'blimply' );
echo '</label><br/>';
}
echo '<br/><label class="selectit" for="blimply_no_sound" style="margin-left: 4px">';
echo '<input type="checkbox" style="float:left" name="blimply_no_sound" id="blimply_no_sound" value="1" '. checked( $this->_is_quiet_time(), true, false ) . ' />';
esc_html_e( 'Turn the sound off', 'blimply' );
echo '</label><br/>';
echo '<br/><input type="hidden" id="" name="blimply_push" value="0" />';
echo '<input type="checkbox" id="blimply_push" name="blimply_push" value="1" disabled="disabled" />';
echo '<label for="blimply_push">';
esc_html_e( 'Check to confirm sending', 'blimply' );
echo '</label> ';
echo '</div>';
} else {
esc_html_e( 'Push notification is already sent', 'blimply' );
}
}
/**
* Dashboard widget
*
*/
function dashboard_widget() {
$limit = (int) apply_filters( BLIMPLY_PREFIX . '_character_limit', $this->options[BLIMPLY_PREFIX . '_character_limit'] );
$limit_html = $limit ? sprintf( ' maxlength="%d" ', $limit ) : '';
?>
<form name="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post" id="blimply-dashboard-widget">
<fieldset>
<h4 id="content-label"><label for="content"><?php esc_html_e( 'Send Push Notification' ) ?></label></h4>
<small><?php esc_html_e( 'Keep in mind that all HTML will be stripped out, and refrain from putting any links in the message.', 'blimply' ); ?></small><br/>
<?php if ( $limit ): ?>
<small><?php esc_html_e( 'Character limit is: ', 'blimply' ); ?><strong><?php echo (int) $limit ?></strong>.</small> <br/>
<small><strong><?php esc_html_e( 'Characters left: ', 'blimply' ); ?><span class="limit"><?php echo (int) $limit ?></strong></span>.
<?php esc_html_e( "You won't be able to type more than that.", 'blimply' ); ?>
</small>
<?php endif; ?>
<div class="textarea-wrap">
<textarea name="blimply_push_alert" id="content" rows="3" cols="15" tabindex="2" <?php echo $limit_html ?> placeholder="Your push message"></textarea>
</div>
<h4><label for="tags-input"><?php esc_html_e( 'Choose a tag' ) ?></label></h4>
<?php
foreach ( (array) $this->tags as $tag ) {
echo '<label class="float-left f-left selectit" for="blimply_tag_' . esc_attr( $tag->term_id ) . '" style="margin-left: 4px">';
echo '<input type="radio" class="float-left f-left" name="blimply_push_tag" id="blimply_tag_' . esc_attr( $tag->term_id ) . '" value="' . esc_attr( $tag->slug ) . '"/>';
echo $tag->name;
echo '</label><br/>';
}
if ( isset( $this->options['blimply_allow_broadcast'] ) && $this->options['blimply_allow_broadcast'] == 'on' ) {
echo '<label class="selectit" for="blimply_tag_broadcast" style="margin-left: 4px">';
echo '<input type="radio" name="blimply_push_tag" id="blimply_tag_broadcast" value="broadcast"/>';
esc_html_e( 'Broadcast (send to all tags)', 'blimply' );
echo '</label><br/>';
}
?>
<br/>
<h4><label for="blimply_no_sound"><?php esc_html_e( 'Turn the sound off' ) ?></label></h4> <?php
echo '<label class="selectit" for="blimply_no_sound" style="margin-left: 4px">';
echo '<input type="checkbox" name="blimply_no_sound" id="blimply_no_sound" value="1" '. checked( $this->_is_quiet_time(), true, false ) . ' />';
esc_html_e( 'Turn the sound off', 'blimply' );
echo '</label><br/>';
?>
<p class="submit">
<input type="hidden" name="action" id="blimply-push-action" value="blimply-send-push" />
<?php wp_nonce_field( 'blimply-send-push' ); ?>
<input type="reset" value="<?php esc_attr_e( 'Reset' ); ?>" class="button" />
<span id="publishing-action">
<?php
if ( current_user_can( apply_filters( 'blimply_push_cap', 'publish_posts' ) ) ):
?>
<input type="submit" name="publish" disabled="disabled" id="blimply_push_send" accesskey="p" tabindex="5" class="button-primary" value="<?php esc_attr_e( 'Send push notification', 'blimply' ) ?>" />
<?php endif; ?>
<img class="waiting" style="display:none;" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
</span>
<br class="clear" />
</p>
</fieldset>
</form>
<?php
}
}
// define BLIMPLY_NOINIT constant somewhere in your theme to easily subclass Blimply
if ( ! defined( 'BLIMPLY_NOINIT' ) || defined( 'BLIMPLY_NOINIT' ) && BLIMPLY_NOINIT ) {
global $blimply;
$blimply = new Blimply;
}