[] ]; function __construct() { register_activation_hook(__FILE__, array(&$this, 'plugin_init')); $this->settings = get_option($this->key); if (!is_array($this->settings)) { update_option($this->key, $this->default_settings); } $this->settings = get_option($this->key); add_filter("plugin_action_links_" . plugin_basename($this->plugin_path), array(&$this, 'plugins_page_links')); add_filter('plugin_row_meta', array(&$this, 'plugins_page_links_meta'), 10, 2); add_action('admin_menu', array(&$this, 'insert_menu')); add_action('admin_init', array(&$this, 'init')); add_action('wp_footer', array(&$this, 'load_scripts')); function load_custom_wp_admin_style($hook) { if (strpos($hook, 'announcekit') !== false) { wp_enqueue_script('my_custom_script', plugins_url('admin.js', __FILE__), array('jquery'), 'v1'); wp_enqueue_style('bootstrap', plugins_url('bootstrap.min.css', __FILE__), null, 'v1'); } } add_action('admin_enqueue_scripts', 'load_custom_wp_admin_style'); register_setting($this->key, $this->key, function ($input) { if (sanitize_text_field($_POST['action']) !== 'update') { return $this->settings; } $widget_id = sanitize_text_field($_GET['widget_id']); $widget_url = []; preg_match('/https:\/\/announcekit.app\/widget\/([a-zA-Z0-9]+)/', $input['js_code'], $widget_url); if ($widget_url && $widget_url[0]) { $input['widget_url'] = $widget_url[0]; } $input['type'] = "Float"; preg_match('/"selector"/', $input['js_code'], $type); if (count($type)) { $input['type'] = "Badge / Line"; } preg_match('/"embed"/', $input['js_code'], $embed); if (count($embed)) { $input['type'] = "Embed"; } if ($input['type'] == "Float") { $input['selector'] = null; } if ($widget_id != "") { $this->settings['widget_list'][$widget_id] = $input; } else { array_push($this->settings['widget_list'], $input); } return $this->settings; }); if (isset($_GET['delete_widget_id'])) { array_splice($this->settings['widget_list'], sanitize_text_field($_GET['delete_widget_id']), 1); update_option($this->key, $this->settings); header("Location: options-general.php?page=" . plugin_basename($this->plugin_path)); die(); } } function get_menu_list() { $ml = []; $menuLocations = get_nav_menu_locations(); foreach (array_values($menuLocations) as $key => $menuID) { $menus = wp_get_nav_menu_items($menuID); if (is_array($menus)) { foreach ($menus as $menu) { array_push($ml, $menu); } } } return $ml; } function plugin_init() { update_option($this->key, $this->default_settings); } function insert_menu() { add_options_page($this->plugin_name, $this->plugin_name, 'administrator', $this->plugin_path, array( &$this, 'render_settings_page' )); } function plugins_page_links($links) { $settings_link = '' . __('Settings') . ''; array_push($links, $settings_link); return $links; } function plugins_page_links_meta($links, $file) { if (strpos($file, 'announcekit') !== false) { $settings_link = [ 'demo' => 'Demo', 'documentation' => 'Documentation' ]; $links = array_merge($links, $settings_link); } return $links; } function init() { $first_section = $this->key . "first"; add_settings_section($first_section, null, null, __FILE__); add_settings_field("name", 'Widget name', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "widget_name" )); add_settings_field("js_code", 'JS Code', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "js_code", "textarea" => true )); add_settings_field("widget_url", null, array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "widget_url", "hidden" => "true" )); add_settings_field("selector", 'Attach to menu', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "selector" )); add_settings_field("position_right", 'Right', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "position_right" )); add_settings_field("position_left", 'Left', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "position_left" )); add_settings_field("position_top", 'Top', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "position_top" )); add_settings_field("position_bottom", 'Bottom', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "position_bottom" )); add_settings_field("selector", 'Attach to menu', array(&$this, 'render_settings_field'), __FILE__, $first_section, array( "key" => "selector" )); } function render_settings_field($arg) { $widget_id = sanitize_text_field($_GET['widget_id']); $options = get_option($this->key); $html = ""; $default_attrs = "id='" . $arg["key"] . "' name='" . $this->key . "[" . $arg["key"] . "]'"; if (isset($widget_id)) { $value = $options['widget_list'][$widget_id][$arg["key"]]; } else { $value = $options['widget_list'][$arg["key"]]; } switch ($arg["key"]) { case 'position_top': case 'position_bottom': case 'position_left': case 'position_right': $html = "
px
"; break; case 'selector': $opts = array_values( array_map(function ($item) { return array( 'label' => $item->menu_item_parent ? ' + ' . $item->title : $item->title, 'value' => $item->ID ); }, $this->get_menu_list()) ); $html = ""; break; default: if ($arg['hidden']) { $html = ""; break; } if ($arg['textarea']) { $html = ""; } else { $html = ""; } break; } if ($arg['placeholder']) { $html = $html . "Example: " . htmlspecialchars($arg['placeholder']) . " "; } echo $html; } function render_option_tag($opts, $value) { $optsHtml = ""; foreach ($opts as $item) { $optsHtml .= "\n"; } return $optsHtml; } function render_settings_page() { $menuArr = []; foreach ($this->get_menu_list() as $key => $menu) { $menuArr[$menu->ID] = $menu; } ?>

Boost user engagement, retention and customer happiness 🎉
Beautifully designed newsfeed powered with fancy widgets and email notifications.

Add widget settings['widget_list'] as $key => $item) { ?>
# Widget Type Attached to Action
title; ?> Edit / Delete
Before adding a widget, make sure you have already created it in AnnounceKit.

key); ?> plugin_path); ?>

settings; $option[$key] = $value; update_option($this->key, $option); } function load_scripts() { $widgets = ""; foreach ($this->settings['widget_list'] as $key => $item) { $conf = ["version" => 2]; $stlye = []; $type = null; switch ($item['type']) { case 'Badge / Line': $stlye['position'] = "absolute"; $type = "badge"; break; case "Float": $type = "float"; break; case 'Embed': $type = "embed"; break; default: break; } if ($item['position_right'] != "") { $stlye['right'] = $item['position_right'] . 'px'; } if ($item['position_left'] != "") { $stlye['left'] = $item['position_left'] . 'px'; } if ($item['position_bottom'] != "") { $stlye['bottom'] = $item['position_bottom'] . 'px'; } if ($item['position_top'] != "") { $stlye['top'] = $item['position_top'] . 'px'; } $conf['selector'] = $item['selector'] ? "XXXSELECTORXXX" : null; $conf['widget'] = $item['widget_url']; $conf[$type] = ['style' => $stlye]; $js = json_encode(array_filter((array) $conf), JSON_UNESCAPED_SLASHES); if ($conf['selector']) { $js = str_replace( '"XXXSELECTORXXX"', "'.' + (document.querySelector('.menu-item-" . $item['selector'] . "') || document.querySelector('.ak-man')).getAttribute('class').replace(/ /g, '.')", $js ); } $widgets .= 'window.announcekit.push(' . $js . ');'; } echo ' '; } } new Announcekit();