Support
2 months ago
Cross.php
2 months ago
Fallback.php
2 months ago
Fields.php
2 months ago
Helper.php
2 months ago
I18n.php
2 months ago
Plugin.php
2 months ago
Popup.php
2 months ago
PostType.php
2 months ago
Review.php
2 months ago
Settings.php
2 months ago
Shortcode.php
2 months ago
Upgrade.php
2 months ago
Settings.php
492 lines
| 1 | <?php |
| 2 | namespace NTA_WhatsApp; |
| 3 | |
| 4 | use NTA_WhatsApp\Helper; |
| 5 | use NTA_WhatsApp\Fields; |
| 6 | use NTA_WhatsApp\PostType; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | /** |
| 10 | * Settings Page |
| 11 | */ |
| 12 | class Settings { |
| 13 | |
| 14 | protected $option; |
| 15 | protected $option_group = 'nta_whatsapp_group'; |
| 16 | protected $option_design = 'nta_whatsapp_design'; |
| 17 | protected $option_button_group = 'nta_whatsapp_button_group'; |
| 18 | protected $option_woo_button_group = 'nta_wa_woo_button_group'; |
| 19 | protected $option_ga_group = 'nta_wa_ga_group'; |
| 20 | |
| 21 | protected $settings; |
| 22 | |
| 23 | private $floatingWidgetSlug = ''; |
| 24 | private $settingSlug = ''; |
| 25 | |
| 26 | protected static $instance = null; |
| 27 | |
| 28 | public static function getInstance() { |
| 29 | if ( null === self::$instance ) { |
| 30 | self::$instance = new self(); |
| 31 | self::$instance->doHooks(); |
| 32 | } |
| 33 | return self::$instance; |
| 34 | } |
| 35 | |
| 36 | private function doHooks() { |
| 37 | add_action( 'admin_init', array( $this, 'register_setting' ) ); |
| 38 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 39 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 40 | add_action( 'admin_footer', array( $this, 'admin_footer' ) ); |
| 41 | |
| 42 | add_action( 'wp_ajax_njt_wa_set_account_position', array( $this, 'set_account_position' ) ); |
| 43 | add_action( 'wp_ajax_njt_wa_load_accounts_ajax', array( $this, 'load_accounts_ajax' ) ); |
| 44 | add_action( 'wp_ajax_njt_wa_set_account_status', array( $this, 'set_account_status' ) ); |
| 45 | |
| 46 | add_action( 'wp_ajax_njt_wa_save_display_setting', array( $this, 'save_display_setting' ) ); |
| 47 | add_action( 'wp_ajax_njt_wa_save_design_setting', array( $this, 'save_design_setting' ) ); |
| 48 | add_action( 'wp_ajax_njt_wa_save_woocommerce_setting', array( $this, 'save_woocommerce_setting' ) ); |
| 49 | add_action( 'wp_ajax_njt_wa_save_analytics_setting', array( $this, 'save_analytics_setting' ) ); |
| 50 | add_action( 'wp_ajax_njt_wa_save_url_setting', array( $this, 'save_url_setting' ) ); |
| 51 | add_action( 'wp_ajax_njt_wa_save_user_role_setting', array( $this, 'save_user_role_setting' ) ); |
| 52 | |
| 53 | add_filter( 'plugin_action_links_' . NTA_WHATSAPP_BASE_NAME, array( $this, 'addActionLinks' ) ); |
| 54 | add_filter( 'plugin_row_meta', array( $this, 'pluginRowMeta' ), 10, 2 ); |
| 55 | } |
| 56 | |
| 57 | public function __construct() { |
| 58 | } |
| 59 | |
| 60 | public function addActionLinks( $links ) { |
| 61 | $links = array_merge( |
| 62 | array( |
| 63 | '<a href="' . esc_url( admin_url( '/admin.php?page=nta_whatsapp_floating_widget' ) ) . '">' . __( 'Settings', 'wp-whatsapp' ) . '</a>', |
| 64 | ), |
| 65 | $links |
| 66 | ); |
| 67 | |
| 68 | $links[] = '<a target="_blank" href="https://1.envato.market/WhatsApp-Plugin" style="color: #43B854; font-weight: bold">' . __( 'Go Pro', 'wp-whatsapp' ) . '</a>'; |
| 69 | |
| 70 | return $links; |
| 71 | } |
| 72 | |
| 73 | public function pluginRowMeta( $links, $file ) { |
| 74 | if ( strpos( $file, 'whatsapp.php' ) !== false ) { |
| 75 | $new_links = array( |
| 76 | 'doc' => '<a href="https://ninjateam.org/wordpress-whatsapp-chat-tutorial/" target="_blank">' . __( 'Documentation', 'wp-whatsapp' ) . '</a>', |
| 77 | ); |
| 78 | |
| 79 | $links = array_merge( $links, $new_links ); |
| 80 | } |
| 81 | |
| 82 | return $links; |
| 83 | } |
| 84 | |
| 85 | public function admin_menu() { |
| 86 | $edit_account_link = 'post-new.php?post_type=whatsapp-accounts'; |
| 87 | |
| 88 | add_menu_page( 'NTA Whatsapp', 'WhatsApp', 'nta_wa_manage', 'nta_whatsapp', array( $this, 'create_page_setting_widget' ), NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp-menu.svg', 60 ); |
| 89 | add_submenu_page( 'nta_whatsapp', __( 'Add New account', 'wp-whatsapp' ), __( 'Add New account', 'wp-whatsapp' ), 'nta_wa_manage', $edit_account_link ); |
| 90 | $this->floatingWidgetSlug = add_submenu_page( 'nta_whatsapp', __( 'Floating Widget', 'wp-whatsapp' ), __( 'Floating Widget', 'wp-whatsapp' ), 'nta_wa_manage', 'nta_whatsapp_floating_widget', array( $this, 'floating_widget_view' ) ); |
| 91 | $this->settingSlug = add_submenu_page( 'nta_whatsapp', __( 'Settings', 'wp-whatsapp' ), __( 'Settings', 'wp-whatsapp' ), 'manage_options', 'nta_whatsapp_setting', array( $this, 'create_page_setting_widget' ) ); |
| 92 | add_submenu_page( |
| 93 | 'nta_whatsapp', |
| 94 | '', |
| 95 | '<span>' . __( 'Go Pro', 'wp-whatsapp' ) . '</span>', |
| 96 | 'manage_options', |
| 97 | 'go_whatsapp_pro', |
| 98 | array( $this, 'go_pro_redirects' ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | public function go_pro_redirects() { |
| 103 | if ( empty( $_GET['page'] ) ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | if ( 'go_whatsapp_pro' === $_GET['page'] ) { |
| 108 | ?> |
| 109 | <script>window.location.href = 'https://1.envato.market/whatsapp-pro'</script> |
| 110 | <?php |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function admin_footer() { |
| 115 | ?> |
| 116 | <style> |
| 117 | body.admin-color-fresh #adminmenu #toplevel_page_nta_whatsapp a[href="admin.php?page=go_whatsapp_pro"] { |
| 118 | color: #00BC28; |
| 119 | font-weight: bold; |
| 120 | position: relative; |
| 121 | } |
| 122 | |
| 123 | body.admin-color-fresh #adminmenu #toplevel_page_nta_whatsapp a[href="admin.php?page=go_whatsapp_pro"]::after { |
| 124 | content: ''; |
| 125 | position: absolute; |
| 126 | width: 4px; |
| 127 | top: 0; |
| 128 | bottom: 0; |
| 129 | left: 0; |
| 130 | background: green; |
| 131 | } |
| 132 | </style> |
| 133 | <script> |
| 134 | jQuery(document).ready(function() { |
| 135 | jQuery('#toplevel_page_nta_whatsapp a[href="admin.php?page=go_whatsapp_pro"]').click(function(event) { |
| 136 | event.preventDefault() |
| 137 | window.open('https://1.envato.market/whatsapp-pro', '_blank') |
| 138 | }) |
| 139 | }) |
| 140 | </script> |
| 141 | <?php |
| 142 | $screen = get_current_screen(); |
| 143 | if ( $screen->base !== $this->floatingWidgetSlug ) { |
| 144 | return; |
| 145 | } |
| 146 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/design-preview.php'; |
| 147 | } |
| 148 | |
| 149 | public function admin_enqueue_scripts( $hook_suffix ) { |
| 150 | if ( 'edit.php' === $hook_suffix || 'post-new.php' === $hook_suffix || 'post.php' === $hook_suffix ) { |
| 151 | if ( 'whatsapp-accounts' !== get_post_type() ) { |
| 152 | return; |
| 153 | } |
| 154 | } elseif ( ! in_array( $hook_suffix, array( $this->settingSlug, $this->floatingWidgetSlug ) ) ) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | wp_register_style( 'nta-css', NTA_WHATSAPP_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), NTA_WHATSAPP_VERSION ); |
| 159 | wp_enqueue_style( 'nta-css' ); |
| 160 | |
| 161 | wp_register_style( 'nta-tippy-css', NTA_WHATSAPP_PLUGIN_URL . 'assets/css/tooltip.css', array(), NTA_WHATSAPP_VERSION ); |
| 162 | wp_enqueue_style( 'nta-tippy-css' ); |
| 163 | |
| 164 | wp_dequeue_style( 'woosea_jquery_ui-css' ); |
| 165 | |
| 166 | wp_register_style( 'nta-wa-widget', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/css/style.css', array(), NTA_WHATSAPP_VERSION ); |
| 167 | wp_enqueue_style( 'nta-wa-widget' ); |
| 168 | wp_enqueue_style( 'ui-range', NTA_WHATSAPP_PLUGIN_URL . 'assets/libs/ui-range.css', array(), NTA_WHATSAPP_VERSION ); |
| 169 | |
| 170 | if ( function_exists( 'wp_timezone_string' ) ) { |
| 171 | $timezone = wp_timezone_string(); |
| 172 | } else { |
| 173 | $timezone = Helper::wp_timezone_string(); |
| 174 | } |
| 175 | |
| 176 | wp_register_script( |
| 177 | 'nta-wa-js', |
| 178 | NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/js/app.js', |
| 179 | array( |
| 180 | 'jquery', |
| 181 | 'wp-color-picker', |
| 182 | 'backbone', |
| 183 | 'underscore', |
| 184 | 'jquery-ui-tabs', |
| 185 | 'jquery-ui-sortable', |
| 186 | 'jquery-ui-autocomplete', |
| 187 | ), |
| 188 | NTA_WHATSAPP_VERSION, |
| 189 | true |
| 190 | ); |
| 191 | wp_localize_script( |
| 192 | 'nta-wa-js', |
| 193 | 'njt_wa', |
| 194 | array( |
| 195 | 'url' => admin_url( 'admin-ajax.php' ), |
| 196 | 'nonce' => wp_create_nonce( 'njt-wa-nonce' ), |
| 197 | 'settings' => array( |
| 198 | 'widget' => array( |
| 199 | 'styles' => Fields::getWidgetStyles(), |
| 200 | ), |
| 201 | ), |
| 202 | 'timezone' => $timezone, |
| 203 | 'i18n' => array( |
| 204 | 'select_post' => __( 'Select posts to display the widget', 'wp-whatsapp' ), |
| 205 | ), |
| 206 | ) |
| 207 | ); |
| 208 | wp_enqueue_script( 'nta-wa-js' ); |
| 209 | wp_enqueue_script( 'jquery-validate', NTA_WHATSAPP_PLUGIN_URL . 'assets/libs/jquery.validate.min.js', array( 'jquery' ), NTA_WHATSAPP_VERSION, true ); |
| 210 | } |
| 211 | |
| 212 | public function page_display_settings_section_callback() { |
| 213 | $option = Fields::getWidgetDisplay(); |
| 214 | $option['time_symbols'] = explode( ':', $option['time_symbols'] ); |
| 215 | |
| 216 | $args = array( |
| 217 | 'post_type' => 'page', |
| 218 | 'post_status' => 'publish', |
| 219 | 'posts_per_page' => -1, |
| 220 | ); |
| 221 | $pages = get_posts( $args ); |
| 222 | |
| 223 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/display-settings.php'; |
| 224 | } |
| 225 | |
| 226 | public function page_design_settings_section_callback() { |
| 227 | $option = Fields::getWidgetStyles(); |
| 228 | $editor_settings = array( |
| 229 | 'media_buttons' => false, |
| 230 | 'textarea_rows' => get_option( 'default_post_edit_rows', 5 ), |
| 231 | 'quicktags' => false, |
| 232 | 'teeny' => true, |
| 233 | ); |
| 234 | $editor_settings_quicktags = array( |
| 235 | 'media_buttons' => false, |
| 236 | 'textarea_rows' => get_option( 'default_post_edit_rows', 5 ), |
| 237 | 'quicktags' => true, |
| 238 | 'teeny' => true, |
| 239 | ); |
| 240 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/design-settings.php'; |
| 241 | } |
| 242 | |
| 243 | public function page_selected_accounts_section_callback() { |
| 244 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/selected-accounts.php'; |
| 245 | } |
| 246 | |
| 247 | public function woocommerce_button_callback() { |
| 248 | $option = Fields::getWoocommerceSetting(); |
| 249 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/woocommerce-button.php'; |
| 250 | } |
| 251 | |
| 252 | public function analytics_callback() { |
| 253 | $option = Fields::getAnalyticsSetting(); |
| 254 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/analytics.php'; |
| 255 | } |
| 256 | |
| 257 | public function url_callback() { |
| 258 | $option = Fields::getURLSettings(); |
| 259 | require NTA_WHATSAPP_PLUGIN_DIR . 'views/url-settings.php'; |
| 260 | } |
| 261 | |
| 262 | public function user_role_callback() { |
| 263 | $option = Fields::getUserRoleSettings(); |
| 264 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/user-role-settings.php'; |
| 265 | } |
| 266 | |
| 267 | public function create_page_setting_widget() { |
| 268 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/settings.php'; |
| 269 | } |
| 270 | |
| 271 | public function floating_widget_view() { |
| 272 | require_once NTA_WHATSAPP_PLUGIN_DIR . 'views/floating-widget-settings.php'; |
| 273 | } |
| 274 | |
| 275 | public function register_setting() { |
| 276 | register_setting( $this->option_group, 'nta_whatsapp_setting' ); |
| 277 | register_setting( $this->option_design, 'nta_whatsapp_setting' ); |
| 278 | register_setting( $this->option_woo_button_group, 'nta_wa_woobutton_setting', array( $this, 'save_woobutton_setting' ) ); |
| 279 | register_setting( $this->option_ga_group, 'nta_wa_ga_setting', array( $this, 'save_ga_setting' ) ); |
| 280 | |
| 281 | add_settings_section( 'page_selected_accounts_section', '', array( $this, 'page_selected_accounts_section_callback' ), 'floating-widget-whatsapp-1' ); |
| 282 | add_settings_section( 'page_design_settings_section', '', array( $this, 'page_design_settings_section_callback' ), 'floating-widget-whatsapp-2' ); |
| 283 | add_settings_section( 'page_display_settings_section', '', array( $this, 'page_display_settings_section_callback' ), 'floating-widget-whatsapp-3' ); |
| 284 | add_settings_section( 'nta_woocommerce_button', '', array( $this, 'woocommerce_button_callback' ), 'settings-whatsapp-1' ); |
| 285 | add_settings_section( 'nta_analytics', '', array( $this, 'analytics_callback' ), 'settings-whatsapp-2' ); |
| 286 | add_settings_section( 'nta_url', '', array( $this, 'url_callback' ), 'settings-whatsapp-3' ); |
| 287 | add_settings_section( 'nta_user_role', '', array( $this, 'user_role_callback' ), 'settings-whatsapp-4' ); |
| 288 | } |
| 289 | |
| 290 | public function save_woobutton_setting() { |
| 291 | $new_input = array(); |
| 292 | |
| 293 | $new_input['nta_woo_button_position'] = isset( $_POST['nta_woo_button_position'] ) ? sanitize_text_field( $_POST['nta_woo_button_position'] ) : 'after_atc'; |
| 294 | $new_input['nta_woo_button_status'] = isset( $_POST['nta_woo_button_status'] ) ? 'ON' : 'OFF'; |
| 295 | return $new_input; |
| 296 | } |
| 297 | |
| 298 | public function save_ga_setting() { |
| 299 | if ( isset( $_POST['nta_wa_ga_status'] ) ) { |
| 300 | return '1'; |
| 301 | } |
| 302 | return '0'; |
| 303 | } |
| 304 | |
| 305 | public function save_display_setting() { |
| 306 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 307 | $new_input = array(); |
| 308 | |
| 309 | $excludePages = Helper::sanitize_array( $_POST['excludePages'] ); |
| 310 | $includePages = Helper::sanitize_array( $_POST['includePages'] ); |
| 311 | $includePosts = Helper::sanitize_array( $_POST['includePosts'] ); |
| 312 | |
| 313 | $new_input = Fields::getWidgetDisplay(); |
| 314 | $new_input['displayCondition'] = sanitize_text_field( $_POST['displayCondition'] ); |
| 315 | $new_input['excludePages'] = empty( $excludePages ) ? array() : $excludePages; |
| 316 | $new_input['includePages'] = empty( $includePages ) ? array() : $includePages; |
| 317 | $new_input['includePosts'] = empty( $includePosts ) ? array() : $includePosts; |
| 318 | $new_input['showOnDesktop'] = isset( $_POST['showOnDesktop'] ) ? 'ON' : 'OFF'; |
| 319 | $new_input['showOnMobile'] = isset( $_POST['showOnMobile'] ) ? 'ON' : 'OFF'; |
| 320 | |
| 321 | $time_symbols = Helper::sanitize_array( $_POST['time_symbols'] ); |
| 322 | $new_input['time_symbols'] = wp_unslash( $time_symbols['hourSymbol'] ) . ':' . wp_unslash( $time_symbols['minSymbol'] ); |
| 323 | |
| 324 | update_option( 'nta_wa_widget_display', $new_input ); |
| 325 | wp_send_json_success(); |
| 326 | } |
| 327 | |
| 328 | public function save_design_setting() { |
| 329 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 330 | |
| 331 | $new_input = array(); |
| 332 | |
| 333 | $new_input = Fields::getWidgetStyles(); |
| 334 | $new_input['title'] = sanitize_text_field( wp_unslash( $_POST['title'] ) ); |
| 335 | $new_input['textColor'] = sanitize_hex_color( $_POST['textColor'] ); |
| 336 | $new_input['titleSize'] = sanitize_text_field( $_POST['titleSize'] ); |
| 337 | $new_input['accountNameSize'] = sanitize_text_field( $_POST['accountNameSize'] ); |
| 338 | $new_input['descriptionTextSize'] = sanitize_text_field( $_POST['descriptionTextSize'] ); |
| 339 | $new_input['regularTextSize'] = sanitize_text_field( $_POST['regularTextSize'] ); |
| 340 | $new_input['backgroundColor'] = sanitize_hex_color( $_POST['backgroundColor'] ); |
| 341 | $new_input['description'] = wp_kses_post( wp_unslash( $_POST['description'] ) ); |
| 342 | $new_input['responseText'] = wp_kses_post( wp_unslash( $_POST['responseText'] ) ); |
| 343 | $new_input['scrollHeight'] = sanitize_text_field( $_POST['scrollHeight'] ); |
| 344 | $new_input['isShowScroll'] = isset( $_POST['isShowScroll'] ) ? 'ON' : 'OFF'; |
| 345 | $new_input['isShowResponseText'] = isset( $_POST['isShowResponseText'] ) ? 'ON' : 'OFF'; |
| 346 | $new_input['btnLabel'] = wp_kses_post( wp_unslash( $_POST['btnLabel'] ) ); // It can be an html tag |
| 347 | $new_input['btnPosition'] = sanitize_text_field( $_POST['btnPosition'] ); |
| 348 | $new_input['btnLabelWidth'] = sanitize_text_field( $_POST['btnLabelWidth'] ); |
| 349 | $new_input['btnLeftDistance'] = sanitize_text_field( $_POST['btnLeftDistance'] ); |
| 350 | $new_input['btnRightDistance'] = sanitize_text_field( $_POST['btnRightDistance'] ); |
| 351 | $new_input['btnBottomDistance'] = sanitize_text_field( $_POST['btnBottomDistance'] ); |
| 352 | $new_input['isShowBtnLabel'] = isset( $_POST['isShowBtnLabel'] ) ? 'ON' : 'OFF'; |
| 353 | |
| 354 | $new_input['isShowGDPR'] = isset( $_POST['isShowGDPR'] ) ? 'ON' : 'OFF'; |
| 355 | $new_input['gdprContent'] = wp_kses_post( wp_unslash( $_POST['gdprContent'] ) ); |
| 356 | |
| 357 | update_option( 'nta_wa_widget_styles', $new_input ); |
| 358 | wp_send_json_success(); |
| 359 | } |
| 360 | |
| 361 | public function save_woocommerce_setting() { |
| 362 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 363 | |
| 364 | $new_input = array(); |
| 365 | |
| 366 | $new_input = Fields::getWoocommerceSetting(); |
| 367 | $new_input['position'] = sanitize_text_field( $_POST['position'] ); |
| 368 | $new_input['isShow'] = isset( $_POST['isShow'] ) ? 'ON' : 'OFF'; |
| 369 | |
| 370 | update_option( 'nta_wa_woocommerce', $new_input ); |
| 371 | wp_send_json_success(); |
| 372 | } |
| 373 | |
| 374 | public function save_analytics_setting() { |
| 375 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 376 | |
| 377 | $new_input = array(); |
| 378 | |
| 379 | $new_input = Fields::getAnalyticsSetting(); |
| 380 | $new_input['enabledGoogle'] = isset( $_POST['enabledGoogle'] ) ? 'ON' : 'OFF'; |
| 381 | $new_input['enabledFacebook'] = isset( $_POST['enabledFacebook'] ) ? 'ON' : 'OFF'; |
| 382 | $new_input['enabledGoogleGA4'] = isset( $_POST['enabledGoogleGA4'] ) ? 'ON' : 'OFF'; |
| 383 | |
| 384 | update_option( 'nta_wa_analytics', $new_input ); |
| 385 | wp_send_json_success(); |
| 386 | } |
| 387 | |
| 388 | public function save_url_setting() { |
| 389 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 390 | |
| 391 | $new_input = array(); |
| 392 | |
| 393 | $new_input = Fields::getURLSettings(); |
| 394 | $new_input['openInNewTab'] = isset( $_POST['openInNewTab'] ) ? 'ON' : 'OFF'; |
| 395 | $new_input['onDesktop'] = sanitize_text_field( $_POST['onDesktop'] ); |
| 396 | $new_input['onMobile'] = sanitize_text_field( $_POST['onMobile'] ); |
| 397 | |
| 398 | update_option( 'nta_wa_url', $new_input ); |
| 399 | wp_send_json_success(); |
| 400 | } |
| 401 | |
| 402 | public function save_user_role_setting() { |
| 403 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 404 | |
| 405 | $new_input = array( 'administrator' => true ); |
| 406 | |
| 407 | if ( isset( $_POST['userRole'] ) ) { |
| 408 | $userRole = Helper::sanitize_array( $_POST['userRole'] ); |
| 409 | foreach ( $userRole as $role ) { |
| 410 | $new_input[ $role ] = true; |
| 411 | // Add capability to user role |
| 412 | $role = get_role( $role ); |
| 413 | $role->add_cap( 'nta_wa_manage' ); |
| 414 | } |
| 415 | } |
| 416 | $old_settings = Fields::getUserRoleSettings(); |
| 417 | $roles_remove_cap = array_diff_key( $old_settings, $new_input ); |
| 418 | foreach ( $roles_remove_cap as $role => $value ) { |
| 419 | $role = get_role( $role ); |
| 420 | $role->remove_cap( 'nta_wa_manage' ); |
| 421 | } |
| 422 | update_option( 'nta_wa_user_role', $new_input ); |
| 423 | |
| 424 | wp_send_json_success(); |
| 425 | } |
| 426 | |
| 427 | public function set_account_position() { |
| 428 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 429 | |
| 430 | $positions = Helper::sanitize_array( $_POST['positions'] ); |
| 431 | $type = sanitize_text_field( $_POST['type'] ); |
| 432 | |
| 433 | foreach ( $positions as $index => $id ) { |
| 434 | update_post_meta( $id, "nta_wa_{$type}", $index ); |
| 435 | } |
| 436 | |
| 437 | wp_send_json_success(); |
| 438 | } |
| 439 | |
| 440 | public function load_accounts_ajax() { |
| 441 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 442 | $postType = PostType::getInstance(); |
| 443 | $accountsList = $postType->get_posts(); |
| 444 | $results = array_map( |
| 445 | function ( $account ) { |
| 446 | $meta = get_post_meta( $account->ID, 'nta_wa_account_info', true ); |
| 447 | $avatar = get_the_post_thumbnail_url( $account->ID ); |
| 448 | $wg_show = get_post_meta( $account->ID, 'nta_wa_widget_show', true ); |
| 449 | $wg_position = get_post_meta( $account->ID, 'nta_wa_widget_position', true ); |
| 450 | $wc_show = get_post_meta( $account->ID, 'nta_wa_wc_show', true ); |
| 451 | $wc_position = get_post_meta( $account->ID, 'nta_wa_wc_position', true ); |
| 452 | |
| 453 | return array_merge( |
| 454 | array( |
| 455 | 'accountId' => $account->ID, |
| 456 | 'accountName' => $account->post_title, |
| 457 | 'edit_link' => get_edit_post_link( $account->ID ), |
| 458 | 'avatar' => false !== $avatar ? $avatar : '', |
| 459 | 'widget_show' => empty( $wg_show ) ? 'OFF' : $wg_show, |
| 460 | 'widget_position' => $wg_position, |
| 461 | 'wc_show' => empty( $wc_show ) ? 'OFF' : $wc_show, |
| 462 | 'wc_position' => $wc_position, |
| 463 | ), |
| 464 | $meta |
| 465 | ); |
| 466 | }, |
| 467 | $accountsList |
| 468 | ); |
| 469 | wp_send_json_success( $results ); |
| 470 | } |
| 471 | |
| 472 | public function set_account_status() { |
| 473 | check_ajax_referer( 'njt-wa-nonce', 'nonce', true ); |
| 474 | $id = sanitize_text_field( $_POST['accountId'] ); |
| 475 | $type = sanitize_text_field( $_POST['type'] ); |
| 476 | $status = sanitize_text_field( $_POST['status'] ); |
| 477 | |
| 478 | $wg_position = get_post_meta( $id, 'nta_wa_widget_position', true ); |
| 479 | $wc_position = get_post_meta( $id, 'nta_wa_wc_position', true ); |
| 480 | |
| 481 | if ( '' === $wg_position ) { |
| 482 | update_post_meta( $id, 'nta_wa_widget_position', 0 ); |
| 483 | } |
| 484 | |
| 485 | if ( '' === $wc_position ) { |
| 486 | update_post_meta( $id, 'nta_wa_wc_position', 0 ); |
| 487 | } |
| 488 | |
| 489 | update_post_meta( $id, "nta_wa_{$type}", $status ); |
| 490 | wp_send_json_success(); |
| 491 | } |
| 492 | } |