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