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