| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Dashboard_Settings' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Dashboard_Settings { |
| 9 |
|
| 10 |
/** |
| 11 |
* Tabs for this section |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $tabs; |
| 16 |
|
| 17 |
/** |
| 18 |
* Current tab |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public static $current_tab; |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize this class |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public static function init() { |
| 30 |
|
| 31 |
// Load tabs for this section. |
| 32 |
add_action( 'admin_init', array( __CLASS__, 'load_tabs' ) ); |
| 33 |
|
| 34 |
// Add current tab to admin localization data. |
| 35 |
add_filter( 'wpsc_admin_localizations', array( __CLASS__, 'localizations' ) ); |
| 36 |
|
| 37 |
// Load section tab layout. |
| 38 |
add_action( 'wp_ajax_wpsc_get_dashboard_settings', array( __CLASS__, 'get_dashboard_settings' ) ); |
| 39 |
|
| 40 |
// Dashboard edit/set edit card/widget. |
| 41 |
add_action( 'wp_ajax_wpsc_get_edit_dashboard_card_widget', array( __CLASS__, 'get_edit_dashboard_card_widget' ) ); |
| 42 |
add_action( 'wp_ajax_wpsc_set_edit_dashboard_card_widget', array( __CLASS__, 'set_edit_dashboard_card_widget' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Load tabs for this section |
| 47 |
*/ |
| 48 |
public static function load_tabs() { |
| 49 |
|
| 50 |
self::$tabs = apply_filters( |
| 51 |
'wpsc_dashboard_setting_tabs', |
| 52 |
array( |
| 53 |
'dashboard-general' => array( |
| 54 |
'slug' => 'dashboard_general', |
| 55 |
'label' => esc_attr__( 'General', 'supportcandy' ), |
| 56 |
'callback' => 'wpsc_get_dashboard_general_settings', |
| 57 |
), |
| 58 |
'dashboard-cards' => array( |
| 59 |
'slug' => 'dashboard_cards', |
| 60 |
'label' => esc_attr__( 'Cards', 'supportcandy' ), |
| 61 |
'callback' => 'wpsc_get_dashboard_cards_settings', |
| 62 |
), |
| 63 |
'dashboard-widgets' => array( |
| 64 |
'slug' => 'dashboard_widgets', |
| 65 |
'label' => esc_attr__( 'Widgets', 'supportcandy' ), |
| 66 |
'callback' => 'wpsc_get_dashboard_widgets_settings', |
| 67 |
), |
| 68 |
) |
| 69 |
); |
| 70 |
self::$current_tab = isset( $_REQUEST['tab'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ) : 'dashboard-cards'; // phpcs:ignore |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add localizations to local JS |
| 75 |
* |
| 76 |
* @param array $localizations - localization. |
| 77 |
* @return array |
| 78 |
*/ |
| 79 |
public static function localizations( $localizations ) { |
| 80 |
|
| 81 |
if ( ! ( WPSC_Settings::$is_current_page && WPSC_Settings::$current_section === 'dashboard-settings' ) ) { |
| 82 |
return $localizations; |
| 83 |
} |
| 84 |
|
| 85 |
// Current section. |
| 86 |
$localizations['current_tab'] = self::$current_tab; |
| 87 |
|
| 88 |
return $localizations; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* General setion body layout |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public static function get_dashboard_settings() { |
| 97 |
|
| 98 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 99 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 100 |
}?> |
| 101 |
|
| 102 |
<div class="wpsc-setting-tab-container"> |
| 103 |
<?php |
| 104 |
foreach ( self::$tabs as $key => $tab ) : |
| 105 |
$active = self::$current_tab === $key ? 'active' : '' |
| 106 |
?> |
| 107 |
<button |
| 108 |
class="<?php echo esc_attr( $key ) . ' ' . esc_attr( $active ); ?>" |
| 109 |
onclick="<?php echo esc_attr( $tab['callback'] ) . '();'; ?>"> |
| 110 |
<?php echo esc_attr( $tab['label'] ); ?> |
| 111 |
</button> |
| 112 |
<?php |
| 113 |
endforeach; |
| 114 |
?> |
| 115 |
</div> |
| 116 |
<div class="wpsc-setting-section-body"></div> |
| 117 |
<?php |
| 118 |
wp_die(); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get edit dashboard cards |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public static function get_edit_dashboard_card_widget() { |
| 127 |
|
| 128 |
if ( check_ajax_referer( 'wpsc_get_edit_dashboard_card_widget', '_ajax_nonce', false ) != 1 ) { |
| 129 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 133 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 134 |
} |
| 135 |
|
| 136 |
$type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; //phpcs:ignore |
| 137 |
if ( ! $type ) { |
| 138 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 139 |
} |
| 140 |
|
| 141 |
$slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; //phpcs:ignore |
| 142 |
if ( ! $slug ) { |
| 143 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 144 |
} |
| 145 |
|
| 146 |
$dashboard_cards = get_option( $type, array() ); |
| 147 |
$card = $dashboard_cards[ $slug ]; |
| 148 |
$title = $card['title']; |
| 149 |
$roles = get_option( 'wpsc-agent-roles', array() ); |
| 150 |
ob_start(); |
| 151 |
?> |
| 152 |
|
| 153 |
<form action="#" onsubmit="return false;" class="wpsc-frm-edit-dashboard-cards-widgets"> |
| 154 |
<div class="wpsc-input-group"> |
| 155 |
<div class="label-container"> |
| 156 |
<label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label> |
| 157 |
</div> |
| 158 |
<?php |
| 159 |
$translation_slug = $type == 'wpsc-dashboard-cards' ? 'wpsc-dashboard-card-' . $slug : 'wpsc-dashboard-widget-' . $slug; |
| 160 |
$title = $card['title'] ? WPSC_Translations::get( $translation_slug, stripslashes( htmlspecialchars( $card['title'] ) ) ) : stripslashes( htmlspecialchars( $card['title'] ) ); |
| 161 |
?> |
| 162 |
<input name="label" type="text" value="<?php echo esc_attr( $title ); ?>" autocomplete="off"> |
| 163 |
</div> |
| 164 |
<div class="wpsc-input-group"> |
| 165 |
<div class="label-container"> |
| 166 |
<label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label> |
| 167 |
</div> |
| 168 |
<select name="is_enable"> |
| 169 |
<option <?php selected( $card['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 170 |
<option <?php selected( $card['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 171 |
</select> |
| 172 |
</div> |
| 173 |
<div class="wpsc-input-group"> |
| 174 |
<div class="label-container"> |
| 175 |
<label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label> |
| 176 |
</div> |
| 177 |
<select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent..."> |
| 178 |
<?php |
| 179 |
foreach ( $roles as $key => $role ) : |
| 180 |
$selected = in_array( $key, $card['allowed-agent-roles'] ) ? 'selected="selected"' : '' |
| 181 |
?> |
| 182 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option> |
| 183 |
<?php |
| 184 |
endforeach; |
| 185 |
?> |
| 186 |
</select> |
| 187 |
</div> |
| 188 |
<script> |
| 189 |
jQuery('#wpsc-select-agents').selectWoo({ |
| 190 |
allowClear: false, |
| 191 |
placeholder: "" |
| 192 |
}); |
| 193 |
</script> |
| 194 |
|
| 195 |
<?php |
| 196 |
if ( method_exists( $card['class'], 'get_edit_dbw_properties' ) ) { |
| 197 |
$card['class']::get_edit_dbw_properties( $card ); |
| 198 |
} |
| 199 |
?> |
| 200 |
|
| 201 |
<input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>"> |
| 202 |
<input type="hidden" name="slug" value="<?php echo esc_attr( $slug ); ?>"> |
| 203 |
<input type="hidden" name="action" value="wpsc_set_edit_dashboard_card_widget"> |
| 204 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_dashboard_card_widget' ) ); ?>"> |
| 205 |
</form> |
| 206 |
<?php |
| 207 |
$body = ob_get_clean(); |
| 208 |
|
| 209 |
ob_start(); |
| 210 |
?> |
| 211 |
<button class="wpsc-button small primary" onclick="wpsc_set_edit_dashboard_card_widget(this);"> |
| 212 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 213 |
</button> |
| 214 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 215 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 216 |
</button> |
| 217 |
<?php |
| 218 |
do_action( 'wpsc_get_edit_dashboard_card_widget_footer' ); |
| 219 |
$footer = ob_get_clean(); |
| 220 |
|
| 221 |
$response = array( |
| 222 |
'title' => $title, |
| 223 |
'body' => $body, |
| 224 |
'footer' => $footer, |
| 225 |
); |
| 226 |
wp_send_json( $response ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Set edit dashboard cards |
| 231 |
*/ |
| 232 |
public static function set_edit_dashboard_card_widget() { |
| 233 |
|
| 234 |
if ( check_ajax_referer( 'wpsc_set_edit_dashboard_card_widget', '_ajax_nonce', false ) != 1 ) { |
| 235 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 239 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 240 |
} |
| 241 |
|
| 242 |
$type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; |
| 243 |
if ( ! $type ) { |
| 244 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 245 |
} |
| 246 |
|
| 247 |
$slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; |
| 248 |
if ( ! $slug ) { |
| 249 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 250 |
} |
| 251 |
|
| 252 |
$label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; |
| 253 |
if ( ! $label ) { |
| 254 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 255 |
} |
| 256 |
|
| 257 |
$is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0; |
| 258 |
$agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array(); |
| 259 |
|
| 260 |
$chart_type = isset( $_POST['chart-type'] ) ? sanitize_text_field( wp_unslash( $_POST['chart-type'] ) ) : ''; |
| 261 |
|
| 262 |
$list = get_option( $type, array() ); |
| 263 |
$list[ $slug ]['title'] = $label; |
| 264 |
$list[ $slug ]['is_enable'] = $is_enable; |
| 265 |
$list[ $slug ]['allowed-agent-roles'] = $agents; |
| 266 |
$list[ $slug ]['chart-type'] = $chart_type; |
| 267 |
update_option( $type, $list ); |
| 268 |
|
| 269 |
// remove string translations. |
| 270 |
$strings = $type == 'wpsc-dashboard-cards' ? 'wpsc-dashboard-card-' . $slug : 'wpsc-dashboard-widget-' . $slug; |
| 271 |
WPSC_Translations::remove( $strings ); |
| 272 |
WPSC_Translations::add( $strings, stripslashes( $label ) ); |
| 273 |
wp_die(); |
| 274 |
} |
| 275 |
} |
| 276 |
endif; |
| 277 |
|
| 278 |
WPSC_Dashboard_Settings::init(); |
| 279 |
|