supportcandy
/
includes
/
admin
/
settings
/
dashboard-settings
/
class-wpsc-dashboard-cards-setting.php
class-wpsc-dashboard-cards-setting.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/admin/settings/dashboard-settings/class-wpsc-dashboard-cards-setting.php
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly! |
| 4 | } |
| 5 | |
| 6 | if ( ! class_exists( 'WPSC_Dashboard_Cards_Setting' ) ) : |
| 7 | |
| 8 | final class WPSC_Dashboard_Cards_Setting { |
| 9 | |
| 10 | /** |
| 11 | * Initialize this class |
| 12 | */ |
| 13 | public static function init() { |
| 14 | |
| 15 | // Dashboard cards. |
| 16 | add_action( 'wp_ajax_wpsc_get_dashboard_cards_settings', array( __CLASS__, 'get_dashboard_cards' ) ); |
| 17 | |
| 18 | // Set load order. |
| 19 | add_action( 'wp_ajax_wpsc_set_dashboard_card_load_order', array( __CLASS__, 'set_dashboard_card_load_order' ) ); |
| 20 | |
| 21 | // Toggle enable/disable of a single card. |
| 22 | add_action( 'wp_ajax_wpsc_toggle_dashboard_card_status', array( __CLASS__, 'toggle_dashboard_card_status' ) ); |
| 23 | |
| 24 | // Enable/disable all cards at once. |
| 25 | add_action( 'wp_ajax_wpsc_toggle_all_dashboard_cards_status', array( __CLASS__, 'toggle_all_dashboard_cards_status' ) ); |
| 26 | |
| 27 | // allow access to new agent role. |
| 28 | add_action( 'wpsc_after_add_agent_role', array( __CLASS__, 'after_add_agent_role' ) ); |
| 29 | |
| 30 | // allow access to cloned agent role wherever the source role was allowed. |
| 31 | add_action( 'wpsc_after_clone_agent_role', array( __CLASS__, 'after_clone_agent_role' ), 10, 2 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Load dashboard cards |
| 36 | */ |
| 37 | public static function get_dashboard_cards() { |
| 38 | |
| 39 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 40 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 41 | } |
| 42 | |
| 43 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 44 | ob_start(); ?> |
| 45 | |
| 46 | <div class="wpsc-dock-container"> |
| 47 | <?php |
| 48 | printf( |
| 49 | /* translators: Click here to see the documentation */ |
| 50 | esc_attr__( '%s to see the documentation!', 'supportcandy' ), |
| 51 | '<a href="https://supportcandy.net/docs/dashboard-cards/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 52 | ); |
| 53 | ?> |
| 54 | </div> |
| 55 | <?php |
| 56 | $enabled_count = 0; |
| 57 | foreach ( $dbc as $card ) { |
| 58 | if ( ! empty( $card['is_enable'] ) ) { |
| 59 | ++$enabled_count; |
| 60 | } |
| 61 | } |
| 62 | $all_enabled = $dbc && $enabled_count === count( $dbc ); |
| 63 | ?> |
| 64 | <div class="wpsc-setting-cards-toggle-all"> |
| 65 | <label class="wpsc-dbc-toggle-switch"> |
| 66 | <input |
| 67 | type="checkbox" |
| 68 | class="wpsc-toggle-all-dashboard-cards" |
| 69 | <?php checked( $all_enabled ); ?> |
| 70 | onchange="wpsc_toggle_all_dashboard_cards( this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_toggle_all_dashboard_cards_status' ) ); ?>' );" |
| 71 | /> |
| 72 | <span class="wpsc-dbc-slider"></span> |
| 73 | </label> |
| 74 | <span class="wpsc-toggle-all-label"><?php esc_attr_e( 'Enable All / Disable All', 'supportcandy' ); ?></span> |
| 75 | </div> |
| 76 | <div class="wpsc-setting-cards-container ui-sortable"> |
| 77 | <?php |
| 78 | foreach ( $dbc as $key => $card ) { |
| 79 | if ( ! class_exists( $card['class'] ) ) { |
| 80 | continue; |
| 81 | } |
| 82 | $style = ! $card['is_enable'] ? 'background-color:#eec7ca;color:#dc2222' : ''; |
| 83 | ?> |
| 84 | <div class="wpsc-setting-card" data-id="<?php echo esc_attr( $key ); ?>" style="<?php echo esc_attr( $style ); ?>"> |
| 85 | <span class="wpsc-sort-handle action-btn"><?php WPSC_Icons::get( 'sort' ); ?></span> |
| 86 | <span class="title"> |
| 87 | <?php |
| 88 | $card_title = $card['title'] ? WPSC_Translations::get( 'wpsc-dashboard-card-' . $key, stripslashes( htmlspecialchars( $card['title'] ) ) ) : stripslashes( htmlspecialchars( $card['title'] ) ); |
| 89 | echo esc_attr( $card_title ); |
| 90 | ?> |
| 91 | </span> |
| 92 | <div class="actions"> |
| 93 | <label class="wpsc-dbc-toggle-switch" onclick="event.stopPropagation();"> |
| 94 | <input |
| 95 | type="checkbox" |
| 96 | class="wpsc-toggle-dashboard-card" |
| 97 | <?php checked( ! empty( $card['is_enable'] ) ); ?> |
| 98 | onchange="wpsc_toggle_dashboard_card_status( this, '<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_toggle_dashboard_card_status' ) ); ?>' );" |
| 99 | /> |
| 100 | <span class="wpsc-dbc-slider"></span> |
| 101 | </label> |
| 102 | <span class="action-btn" onclick="wpsc_get_edit_dashboard_card_widget( 'wpsc-dashboard-cards', '<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_dashboard_card_widget' ) ); ?>' );"><?php WPSC_Icons::get( 'edit' ); ?></span> |
| 103 | </div> |
| 104 | </div> |
| 105 | <?php |
| 106 | } |
| 107 | ?> |
| 108 | </div> |
| 109 | <div class="setting-footer-actions"> |
| 110 | <button class="wpsc-button normal secondary wpsc-save-sort-order"><?php esc_attr_e( 'Save Order', 'supportcandy' ); ?></button> |
| 111 | </div> |
| 112 | <script> |
| 113 | var items = jQuery( ".wpsc-setting-cards-container" ).sortable({ handle: '.wpsc-sort-handle' }); |
| 114 | jQuery(".wpsc-save-sort-order").click(function(){ |
| 115 | var slugs = items.sortable( "toArray", {attribute: 'data-id'} ); |
| 116 | wpsc_set_dashboard_card_load_order(slugs, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_dashboard_card_load_order' ) ); ?>'); |
| 117 | }); |
| 118 | </script> |
| 119 | <?php |
| 120 | wp_die(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set dashboard cards order |
| 125 | * |
| 126 | * @return void |
| 127 | */ |
| 128 | public static function set_dashboard_card_load_order() { |
| 129 | |
| 130 | if ( check_ajax_referer( 'wpsc_set_dashboard_card_load_order', '_ajax_nonce', false ) != 1 ) { |
| 131 | wp_send_json_error( 'Unauthorized request!', 401 ); |
| 132 | } |
| 133 | |
| 134 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 135 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 136 | } |
| 137 | |
| 138 | $slugs = isset( $_POST['slugs'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['slugs'] ) ) ) : array(); |
| 139 | if ( ! $slugs ) { |
| 140 | wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 141 | } |
| 142 | $sorted_cards = array(); |
| 143 | |
| 144 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 145 | $dbc_keys = array_keys( $dbc ); |
| 146 | // Verifying if slug is present in list item. |
| 147 | foreach ( $slugs as $slug ) { |
| 148 | if ( ! in_array( $slug, $dbc_keys ) ) { |
| 149 | wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | foreach ( $slugs as $slug ) : |
| 154 | $sorted_cards[ $slug ] = $dbc[ $slug ]; |
| 155 | endforeach; |
| 156 | update_option( 'wpsc-dashboard-cards', $sorted_cards ); |
| 157 | wp_die(); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Toggle enable/disable status of a single dashboard card |
| 162 | * |
| 163 | * @return void |
| 164 | */ |
| 165 | public static function toggle_dashboard_card_status() { |
| 166 | |
| 167 | if ( check_ajax_referer( 'wpsc_toggle_dashboard_card_status', '_ajax_nonce', false ) != 1 ) { |
| 168 | wp_send_json_error( 'Unauthorized request!', 401 ); |
| 169 | } |
| 170 | |
| 171 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 172 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 173 | } |
| 174 | |
| 175 | $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; |
| 176 | $is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0; |
| 177 | |
| 178 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 179 | if ( ! $slug || ! isset( $dbc[ $slug ] ) ) { |
| 180 | wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 181 | } |
| 182 | |
| 183 | $dbc[ $slug ]['is_enable'] = $is_enable; |
| 184 | update_option( 'wpsc-dashboard-cards', $dbc ); |
| 185 | wp_die(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Enable or disable all dashboard cards at once |
| 190 | * |
| 191 | * @return void |
| 192 | */ |
| 193 | public static function toggle_all_dashboard_cards_status() { |
| 194 | |
| 195 | if ( check_ajax_referer( 'wpsc_toggle_all_dashboard_cards_status', '_ajax_nonce', false ) != 1 ) { |
| 196 | wp_send_json_error( 'Unauthorized request!', 401 ); |
| 197 | } |
| 198 | |
| 199 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 200 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 201 | } |
| 202 | |
| 203 | $is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0; |
| 204 | |
| 205 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 206 | foreach ( $dbc as $key => $card ) { |
| 207 | $dbc[ $key ]['is_enable'] = $is_enable; |
| 208 | } |
| 209 | update_option( 'wpsc-dashboard-cards', $dbc ); |
| 210 | wp_die(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * After new agent role added add that role in ticket cards |
| 215 | * |
| 216 | * @param integer $role_id - agent role id. |
| 217 | * @return void |
| 218 | */ |
| 219 | public static function after_add_agent_role( $role_id ) { |
| 220 | |
| 221 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 222 | foreach ( $dbc as $key => $card ) { |
| 223 | |
| 224 | $card['allowed-agent-roles'][] = $role_id; |
| 225 | $dbc[ $key ] = $card; |
| 226 | } |
| 227 | update_option( 'wpsc-dashboard-cards', $dbc ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * After agent role cloned, add the new role to every card the source role was allowed on. |
| 232 | * |
| 233 | * @param integer $new_role_id - newly cloned agent role id. |
| 234 | * @param integer $source_role_id - source agent role id that was cloned. |
| 235 | * @return void |
| 236 | */ |
| 237 | public static function after_clone_agent_role( $new_role_id, $source_role_id ) { |
| 238 | |
| 239 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 240 | foreach ( $dbc as $key => $card ) { |
| 241 | |
| 242 | if ( in_array( $source_role_id, $card['allowed-agent-roles'] ) ) { |
| 243 | $card['allowed-agent-roles'][] = $new_role_id; |
| 244 | $dbc[ $key ] = $card; |
| 245 | } |
| 246 | } |
| 247 | update_option( 'wpsc-dashboard-cards', $dbc ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | endif; |
| 252 | |
| 253 | WPSC_Dashboard_Cards_Setting::init(); |
| 254 |