options = ['api_key', 'push_enabled', 'gcm_sender_id', 'safari_web_push_id', 'request_push_permission_selector', 'request_push_permission_event', 'custom_event_names', 'custom_event_selectors', 'custom_event_events', 'logging_enabled', 'show_feed'];
$this->id = 'appboywebsdk';
$this->sdk_version = '2.0';
}
/**
* Adds handlers to activation and deactivation hooks
*/
private function add_hooks_appboy() {
register_activation_hook(__FILE__, array($this, 'on_activate_appboy'));
register_deactivation_hook(__FILE__, array($this, 'on_deactivate_appboy'));
}
/**
* Adds handlers for admin actions
*/
private function add_admin_actions_appboy() {
if (is_admin()) {
add_action('admin_init', array($this, 'admin_init_appboy'));
add_action('admin_menu', array($this, 'add_link_to_settings_menu_appboy'));
} else {
add_action('wp_head', array($this, 'generate_sdk_implementation_appboy'));
}
}
/**
* Since the API key is required, notify the user if they don't have it set
*/
private function print_warnings_appboy() {
if (empty(get_option('api_key'))) {
_e('
WARNING: you must specify a valid API key for the SDK to be initialized properly.
');
}
}
/**
* Registers all the plugin options as settings
*/
public function admin_init_appboy() {
foreach ($this->options as $opt) {
register_setting($this->id, $opt);
}
}
/**
* When the plugin is deactivated, delete all the options that were registered as settings
*/
public function on_deactivate_appboy() {
foreach ($this->options as $opt) {
delete_option($opt);
}
}
/**
* Adds the link to the options form under the settings section on the admin panel
*/
public function add_link_to_settings_menu_appboy() {
add_options_page('Appboy Web SDK Settings', 'Appboy Web SDK', 'manage_options', 'appboywebsdk', array($this, 'render_options_form_appboy'));
}
/**
* Generates a friendly hash to render the custom events
*/
public function save_custom_events_hash_appboy($event_names, $event_selectors, $event_events) {
$hash = [];
if (intval(count($event_names)) > 0) {
foreach($event_names as $i => $item) {
$new_custom_event = [
'name' => $event_names[$i],
'selector' => $event_selectors[$i],
'event' => $event_events[$i]
];
array_push($hash, $new_custom_event);
}
}
update_option(custom_events, $hash);
return $hash;
}
/**
* Generates the javascript for logging the saved custom events
*/
public function generate_custom_events_js_appboy($custom_events) {
$js = '';
foreach($custom_events as $custom_event) {
$js .= 'document.querySelector(' . json_encode($custom_event['selector']) . ').addEventListener(' . json_encode($custom_event['event']) . ', function() { appboy.logCustomEvent(' . json_encode($custom_event['name']) . '); }, false);';
$js .= "\r\n";
}
return $js;
}
/**
* Generates the javascript for enabling push
*/
public function generate_push_permission_appboy($dom_selector, $dom_event, $safari_web_push_id) {
$js = '';
$safariJs = '';
if ($safari_web_push_id) {
$safariJs .= 'null, null, ' . json_encode($safari_web_push_id) . '';
}
if ($dom_selector && $dom_event) {
$js .= 'document.querySelector(' . json_encode($dom_selector) . ').addEventListener(' . json_encode($dom_event) . ', function() { appboy.registerAppboyPushMessages(' . $safariJs . '); }, false);';
} else {
$js .= 'appboy.registerAppboyPushMessages(' . $safariJs . ');';
}
$js .= "\r\n";
return $js;
}
/**
* Renders the options form
*/
public function render_options_form_appboy() {
?>
Appboy Web SDK Settings
'); }
?>
add_hooks_appboy();
$this->add_admin_actions_appboy();
}
}
/**
* Only initalize if within a wordpress plugin
*/
if (defined('WP_CONTENT_URL')) {
$ab = new \Appboy\Plugin\Appboy();
$ab->init();
}