← All changes
|
includes/admin/settings/class-wpsc-ticket-widgets.php
+117
-3
3.3.6
→
3.5.4
View file →
| @@ -15,10 +15,19 @@ | ||
| 15 | 15 | // Ticket widget. |
| 16 | 16 | add_action( 'wp_ajax_wpsc_get_ticket_widget', array( __CLASS__, 'get_ticket_widget' ) ); |
| 17 | 17 | add_action( 'wp_ajax_wpsc_set_tw_load_order', array( __CLASS__, 'set_tw_load_order' ) ); |
| 18 | 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 | + | |
| 19 | 25 | // allow access to new agent role. |
| 20 | 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 ); | |
| 21 | 30 | } |
| 22 | 31 | |
| 23 | 32 | /** |
| 24 | 33 | * Load ticket widgets |
| @@ -44,8 +53,29 @@ | ||
| 44 | 53 | '<a href="https://supportcandy.net/docs/ticket-widget-settings/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 45 | 54 | ); |
| 46 | 55 | ?> |
| 47 | 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> | |
| 48 | 78 | <div class="wpsc-setting-cards-container ui-sortable"> |
| 49 | 79 | <?php |
| 50 | 80 | foreach ( $ticket_widgets as $key => $ticket_widget ) { |
| 51 | 81 | if ( ! class_exists( $ticket_widget['class'] ) ) { |
| @@ -56,13 +86,22 @@ | ||
| 56 | 86 | <div class="wpsc-setting-card" data-id="<?php echo esc_attr( $key ); ?>" style="<?php echo esc_attr( $style ); ?>"> |
| 57 | 87 | <span class="wpsc-sort-handle action-btn"><?php WPSC_Icons::get( 'sort' ); ?></span> |
| 58 | 88 | <span class="title"> |
| 59 | 89 | <?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 ); | |
| 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 ); | |
| 62 | 92 | ?> |
| 63 | 93 | </span> |
| 64 | 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> | |
| 65 | 104 | <span class="action-btn" onclick="<?php echo esc_attr( $ticket_widget['callback'] ); ?>"><?php WPSC_Icons::get( 'edit' ); ?></span> |
| 66 | 105 | </div> |
| 67 | 106 | </div> |
| 68 | 107 | <?php |
| @@ -91,9 +130,9 @@ | ||
| 91 | 130 | */ |
| 92 | 131 | public static function set_tw_load_order() { |
| 93 | 132 | |
| 94 | 133 | if ( check_ajax_referer( 'wpsc_set_tw_load_order', '_ajax_nonce', false ) != 1 ) { |
| 95 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 134 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 96 | 135 | } |
| 97 | 136 | |
| 98 | 137 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 99 | 138 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| @@ -121,8 +160,61 @@ | ||
| 121 | 160 | wp_die(); |
| 122 | 161 | } |
| 123 | 162 | |
| 124 | 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 | + /** | |
| 125 | 217 | * After new agent role added add that role in ticket widgets |
| 126 | 218 | * |
| 127 | 219 | * @param integer $role_id - agent role id. |
| 128 | 220 | * @return void |
| @@ -133,8 +225,30 @@ | ||
| 133 | 225 | foreach ( $ticket_widgets as $key => $widget ) { |
| 134 | 226 | |
| 135 | 227 | $widget['allowed-agent-roles'][] = $role_id; |
| 136 | 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 | + } | |
| 137 | 251 | } |
| 138 | 252 | update_option( 'wpsc-ticket-widget', $ticket_widgets ); |
| 139 | 253 | } |
| 140 | 254 | } |