-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpushbots.php
executable file
·141 lines (120 loc) · 6.08 KB
/
pushbots.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
<?php
class WPPushBots {
public function __construct()
{
// Plugin Details
$this->plugin = new stdClass;
$this->plugin->name = 'pushbots'; // Plugin Folder
$this->plugin->sdk = 'sdk'; // Plugin Folder
$this->plugin->displayName = 'PushBots'; // Plugin Name
$this->plugin->version = '1.0.7';
$this->plugin->folder = plugin_dir_path( __FILE__ );
$this->plugin->url = plugin_dir_url( __FILE__ );
$this->plugin->db_welcome_dismissed_key = $this->plugin->name . '_welcome_dismissed_key';
// Get latest settings
$this->settings = array(
'pb_application_id' => esc_html( wp_unslash( get_option('pb_application_id') ) ),
'pb_gcm_sender_id' => esc_html( wp_unslash( get_option('pb_gcm_sender_id') ) ),
'pb_application_secret' => esc_html( wp_unslash( get_option('pb_application_secret') ) ),
'pb_website_url' => (get_option('pb_website_url')) ? esc_html( wp_unslash( get_option('pb_website_url') ) ) : get_site_url(),
'pb_safari_push_id' => esc_html( wp_unslash( get_option('pb_safari_push_id') ) ),
'pb_enable_welcome_message' => esc_html( wp_unslash( get_option('pb_enable_welcome_message') ) ),
'pb_welcome_title' => esc_html( wp_unslash( get_option('pb_welcome_title') ) ),
'pb_welcome_message' => esc_html( wp_unslash( get_option('pb_welcome_message') ) ),
);
// Hooks
add_action( 'admin_menu', array($this, 'pushbots_menu' ));
add_action( 'admin_enqueue_scripts', array($this, 'register_admin_scripts') );
add_image_size( 'pb_notification', 256, 256, true );
if($this->settings['pb_application_id'])
require_once __DIR__ . "/script.php";
}
function pushbots_menu() {
add_menu_page( $this->plugin->displayName, $this->plugin->displayName, 4, $this->plugin->name, array( $this, 'pb_admin_settings'));
add_action( 'save_post', array($this,'send_article_notification'));
// add_action( 'post_submitbox_misc_actions', array($this,'article_push_checkbox') );
add_action( 'add_meta_boxes', array($this,'cd_meta_box_add') );
}
function register_admin_scripts() {
wp_enqueue_script( 'pushbots-toggle', plugins_url( 'admin-settings.js', __FILE__ ) );
}
function pb_admin_settings() {
// only admin user can access this page
if ( !current_user_can( 'administrator' ) ) {
echo '<p>' . __( 'Sorry, you are not allowed to access this page.', $this->plugin->name ) . '</p>';
return;
}
// Save Settings
if ( isset( $_REQUEST['submit'] ) ) {
$app_id = sanitize_text_field($_REQUEST['pb_application_id']);
$app_secret = sanitize_text_field($_REQUEST['pb_application_secret']);
$gcm_sender_id = sanitize_text_field($_REQUEST['pb_gcm_sender_id']);
$website_url = esc_url_raw($_REQUEST['pb_website_url']);
$safari_push_id = sanitize_text_field($_REQUEST['pb_safari_push_id']);
$welcome_message_enabled = $_REQUEST['pb_enable_welcome_message'];
if($_REQUEST['pb_enable_welcome_message'] === 'on' || !$_REQUEST['pb_enable_welcome_message']) {
update_option( 'pb_enable_welcome_message', $welcome_message_enabled);
}
update_option( 'pb_application_id', $app_id);
update_option( 'pb_application_secret', $app_secret );
update_option( 'pb_gcm_sender_id', $gcm_sender_id );
update_option( 'pb_website_url', $website_url);
update_option( 'pb_safari_push_id', $safari_push_id );
if($welcome_message_enabled) {
update_option( 'pb_welcome_title', sanitize_text_field($_REQUEST['pb_welcome_title']));
update_option( 'pb_welcome_message', sanitize_text_field($_REQUEST['pb_welcome_message']));
}
$this->message = __( 'Settings Saved.', 'pushbots' );
}
// Load Settings Form
require_once __DIR__ . "/admin-settings.php";
}
function cd_meta_box_add() {
add_meta_box( 'pb-box', $this->plugin->displayName, array($this, 'article_push_checkbox'), 'post', 'side', 'high' );
}
function article_push_checkbox()
{
global $post;
/* check if this is a post, if not then we won't add the custom field */
/* change this post type to any type you want to add the custom field to */
if (get_post_type($post) != 'post') return false;
/* get the value corrent value of the custom field */
?>
<div class="misc-pub-section">
<label><input type="checkbox" name="pb_send_notification" id="pb_send_notification" /> Send push notification</label>
</div>
<?php
}
function send_article_notification($postid) {
/* check if this is an autosave */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
/* check if the user can edit this page */
if ( !current_user_can( 'edit_page', $postid ) ) return false;
/* check if there's a post id and check if this is a post */
/* make sure this is the same post type as above */
if(empty($postid) || $_POST['post_type'] != 'post' ) return false;
/* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
if(isset($_POST['pb_send_notification']) && get_post_status($postid) == 'publish'){
/* store the value in the database */
$appID = $this->settings['pb_application_id'];;
$appSecret = $this->settings['pb_application_secret'];
$args = array(
"nTitle"=>html_entity_decode(get_the_title($postid), ENT_QUOTES, "UTF-8"),
"openURL" => get_permalink($postid)
);
if(has_post_thumbnail( $postid )) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $postid ), 'pb_notification', true );
$args['icon'] = $image[0] ;
}
require_once( __DIR__ . "/sdk/PushBots.class.php");
$pb = new PushBots();
$pb->App($appID, $appSecret);
$pb->Platform(array(2,3,4,5));
$post = get_post($postid);
$pb->Alert(htmlspecialchars_decode(wp_trim_words($post->post_content, 55)));
$pb->Payload($args);
$pb->Push();
setup_postdata( $postid );
}
}
}