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.4.2, 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 | // allow access to new agent role. |
| 22 | add_action( 'wpsc_after_add_agent_role', array( __CLASS__, 'after_add_agent_role' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Load dashboard cards |
| 27 | */ |
| 28 | public static function get_dashboard_cards() { |
| 29 | |
| 30 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 31 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 32 | } |
| 33 | |
| 34 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 35 | ob_start(); ?> |
| 36 | |
| 37 | <div class="wpsc-dock-container"> |
| 38 | <?php |
| 39 | printf( |
| 40 | /* translators: Click here to see the documentation */ |
| 41 | esc_attr__( '%s to see the documentation!', 'supportcandy' ), |
| 42 | '<a href="https://supportcandy.net/docs/dashboard-cards/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 43 | ); |
| 44 | ?> |
| 45 | </div> |
| 46 | <div class="wpsc-setting-cards-container ui-sortable"> |
| 47 | <?php |
| 48 | foreach ( $dbc as $key => $card ) { |
| 49 | if ( ! class_exists( $card['class'] ) ) { |
| 50 | continue; |
| 51 | } |
| 52 | $style = ! $card['is_enable'] ? 'background-color:#eec7ca;color:#dc2222' : ''; |
| 53 | ?> |
| 54 | <div class="wpsc-setting-card" data-id="<?php echo esc_attr( $key ); ?>" style="<?php echo esc_attr( $style ); ?>"> |
| 55 | <span class="wpsc-sort-handle action-btn"><?php WPSC_Icons::get( 'sort' ); ?></span> |
| 56 | <span class="title"> |
| 57 | <?php |
| 58 | $card_title = $card['title'] ? WPSC_Translations::get( 'wpsc-dashboard-card-' . $key, stripslashes( htmlspecialchars( $card['title'] ) ) ) : stripslashes( htmlspecialchars( $card['title'] ) ); |
| 59 | echo esc_attr( $card_title ); |
| 60 | ?> |
| 61 | </span> |
| 62 | <div class="actions"> |
| 63 | <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> |
| 64 | </div> |
| 65 | </div> |
| 66 | <?php |
| 67 | } |
| 68 | ?> |
| 69 | </div> |
| 70 | <div class="setting-footer-actions"> |
| 71 | <button class="wpsc-button normal secondary wpsc-save-sort-order"><?php esc_attr_e( 'Save Order', 'supportcandy' ); ?></button> |
| 72 | </div> |
| 73 | <script> |
| 74 | var items = jQuery( ".wpsc-setting-cards-container" ).sortable({ handle: '.wpsc-sort-handle' }); |
| 75 | jQuery(".wpsc-save-sort-order").click(function(){ |
| 76 | var slugs = items.sortable( "toArray", {attribute: 'data-id'} ); |
| 77 | wpsc_set_dashboard_card_load_order(slugs, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_dashboard_card_load_order' ) ); ?>'); |
| 78 | }); |
| 79 | </script> |
| 80 | <?php |
| 81 | wp_die(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set dashboard cards order |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public static function set_dashboard_card_load_order() { |
| 90 | |
| 91 | if ( check_ajax_referer( 'wpsc_set_dashboard_card_load_order', '_ajax_nonce', false ) != 1 ) { |
| 92 | wp_send_json_error( 'Unauthorised request!', 401 ); |
| 93 | } |
| 94 | |
| 95 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 96 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 97 | } |
| 98 | |
| 99 | $slugs = isset( $_POST['slugs'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['slugs'] ) ) ) : array(); |
| 100 | if ( ! $slugs ) { |
| 101 | wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 102 | } |
| 103 | $sorted_cards = array(); |
| 104 | |
| 105 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 106 | $dbc_keys = array_keys( $dbc ); |
| 107 | // Verifying if slug is present in list item. |
| 108 | foreach ( $slugs as $slug ) { |
| 109 | if ( ! in_array( $slug, $dbc_keys ) ) { |
| 110 | wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | foreach ( $slugs as $slug ) : |
| 115 | $sorted_cards[ $slug ] = $dbc[ $slug ]; |
| 116 | endforeach; |
| 117 | update_option( 'wpsc-dashboard-cards', $sorted_cards ); |
| 118 | wp_die(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * After new agent role added add that role in ticket cards |
| 123 | * |
| 124 | * @param integer $role_id - agent role id. |
| 125 | * @return void |
| 126 | */ |
| 127 | public static function after_add_agent_role( $role_id ) { |
| 128 | |
| 129 | $dbc = get_option( 'wpsc-dashboard-cards', array() ); |
| 130 | foreach ( $dbc as $key => $card ) { |
| 131 | |
| 132 | $card['allowed-agent-roles'][] = $role_id; |
| 133 | $dbc[ $key ] = $card; |
| 134 | } |
| 135 | update_option( 'wpsc-dashboard-cards', $dbc ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | endif; |
| 140 | |
| 141 | WPSC_Dashboard_Cards_Setting::init(); |
| 142 |