| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_MS_TAC' ) ) : |
| 7 |
|
| 8 |
final class WPSC_MS_TAC { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// User interface. |
| 18 |
add_action( 'wp_ajax_wpsc_get_ms_term_and_conditions', array( __CLASS__, 'load_settings_ui' ) ); |
| 19 |
add_action( 'wp_ajax_wpsc_set_ms_term_and_conditions', array( __CLASS__, 'save_settings' ) ); |
| 20 |
add_action( 'wp_ajax_wpsc_reset_ms_term_and_conditions', array( __CLASS__, 'reset_settings' ) ); |
| 21 |
|
| 22 |
// Print in create ticket form. |
| 23 |
add_action( 'wpsc_print_tff', array( __CLASS__, 'print_tff' ) ); |
| 24 |
|
| 25 |
// TFF! validation. |
| 26 |
add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Reset settings |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function reset() { |
| 35 |
|
| 36 |
$wpsc_tandc_text = '<p>I agree to the terms and conditions</p>'; |
| 37 |
$condition = apply_filters( |
| 38 |
'wpsc_term_and_conditions', |
| 39 |
array( |
| 40 |
'allow-term-and-conditions' => 1, |
| 41 |
'tandc-text' => $wpsc_tandc_text, |
| 42 |
'editor' => 'html', |
| 43 |
'allow-term-and-conditions-reg-user' => 1, |
| 44 |
'tandc-text-reg-user' => $wpsc_tandc_text, |
| 45 |
'editor-reg-user' => 'html', |
| 46 |
) |
| 47 |
); |
| 48 |
update_option( 'wpsc-term-and-conditions', $condition ); |
| 49 |
WPSC_Translations::remove( 'wpsc-term-and-conditions', $condition['tandc-text'] ); |
| 50 |
WPSC_Translations::remove( 'wpsc-term-and-conditions-reg-user', $condition['tandc-text-reg-user'] ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Settings user interface |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public static function load_settings_ui() { |
| 59 |
|
| 60 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 61 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 62 |
} |
| 63 |
$settings = get_option( 'wpsc-term-and-conditions', array() );?> |
| 64 |
|
| 65 |
<form action="#" onsubmit="return false;" class="wpsc-frm-ms-tandc"> |
| 66 |
<div class="wpsc-dock-container"> |
| 67 |
<?php |
| 68 |
printf( |
| 69 |
/* translators: Click here to see the documentation */ |
| 70 |
esc_attr__( '%s to see the documentation!', 'supportcandy' ), |
| 71 |
'<a href="https://supportcandy.net/docs/terms-conditions/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 72 |
); |
| 73 |
?> |
| 74 |
</div> |
| 75 |
<h3><?php esc_attr_e( 'Create ticket form', 'supportcandy' ); ?></h3> |
| 76 |
|
| 77 |
<div class="wpsc-input-group"> |
| 78 |
<div class="label-container"> |
| 79 |
<label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label> |
| 80 |
</div> |
| 81 |
<select id="wpsc-allow-term-and-conditions" name="allow-term-and-conditions"> |
| 82 |
<option <?php selected( $settings['allow-term-and-conditions'], 1 ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 83 |
<option <?php selected( $settings['allow-term-and-conditions'], 0 ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 84 |
</select> |
| 85 |
</div> |
| 86 |
<div class="wpsc-input-group" id="wpsc-allow-tandc"> |
| 87 |
<div class="label-container"> |
| 88 |
<label for=""><?php esc_attr_e( 'Checkbox text', 'supportcandy' ); ?></label> |
| 89 |
</div> |
| 90 |
<div class="textarea-container "> |
| 91 |
<div class="wpsc_tinymce_editor_btns"> |
| 92 |
<div class="inner-container"> |
| 93 |
<button class="visual wpsc-switch-editor wpsc-switch-editor-ct <?php echo esc_attr( $settings['editor'] ) == 'html' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_tinymce(this, 'tandc-text','tandc_body');"><?php esc_attr_e( 'Visual', 'supportcandy' ); ?></button> |
| 94 |
<button class="text wpsc-switch-editor wpsc-switch-editor-ct <?php echo esc_attr( $settings['editor'] ) == 'text' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_textarea(this, 'tandc-text')"><?php esc_attr_e( 'Text', 'supportcandy' ); ?></button> |
| 95 |
</div> |
| 96 |
</div> |
| 97 |
<?php |
| 98 |
$tandc_text = $settings['tandc-text'] ? WPSC_Translations::get( 'wpsc-term-and-conditions', stripslashes( $settings['tandc-text'] ) ) : stripslashes( $settings['tandc-text'] ); |
| 99 |
?> |
| 100 |
<textarea name="tandc-text" id="tandc-text" class = "wpsc_textarea"><?php echo wp_kses_post( $tandc_text ); ?></textarea> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
|
| 104 |
<hr> |
| 105 |
<h3><?php esc_attr_e( 'Registration form', 'supportcandy' ); ?></h3> |
| 106 |
|
| 107 |
<div class="wpsc-input-group"> |
| 108 |
<div class="label-container"> |
| 109 |
<label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label> |
| 110 |
</div> |
| 111 |
<select id="wpsc-allow-tandc-reg-user" name="allow-term-and-conditions-reg-user"> |
| 112 |
<option <?php selected( $settings['allow-term-and-conditions-reg-user'], 1 ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 113 |
<option <?php selected( $settings['allow-term-and-conditions-reg-user'], 0 ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 114 |
</select> |
| 115 |
</div> |
| 116 |
<div class="wpsc-input-group"> |
| 117 |
<div class="label-container"> |
| 118 |
<label for=""><?php esc_attr_e( 'Checkbox text', 'supportcandy' ); ?></label> |
| 119 |
</div> |
| 120 |
<div class="textarea-container "> |
| 121 |
<div class="wpsc_tinymce_editor_btns"> |
| 122 |
<div class="inner-container"> |
| 123 |
<button class="visual wpsc-switch-editor wpsc-switch-editor-reg-user <?php echo esc_attr( $settings['editor-reg-user'] ) == 'html' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_tinymce(this, 'tandc-text-reg-user','tandc_body');"><?php esc_attr_e( 'Visual', 'supportcandy' ); ?></button> |
| 124 |
<button class="text wpsc-switch-editor wpsc-switch-editor-reg-user <?php echo esc_attr( $settings['editor-reg-user'] ) == 'text' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_textarea(this, 'tandc-text-reg-user')"><?php esc_attr_e( 'Text', 'supportcandy' ); ?></button> |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
<?php |
| 128 |
$tandc_text = $settings['tandc-text-reg-user'] ? WPSC_Translations::get( 'wpsc-term-and-conditions-reg-user', stripslashes( $settings['tandc-text-reg-user'] ) ) : stripslashes( $settings['tandc-text-reg-user'] ); |
| 129 |
?> |
| 130 |
<textarea name="tandc-text-reg-user" id="tandc-text-reg-user" class = "wpsc_textarea"><?php echo wp_kses_post( $tandc_text ); ?></textarea> |
| 131 |
</div> |
| 132 |
</div> |
| 133 |
<?php do_action( 'wpsc_ms_term_and_conditions' ); ?> |
| 134 |
<input type="hidden" name="action" value="wpsc_set_ms_term_and_conditions"> |
| 135 |
<input id="editor" type="hidden" name="editor" value="<?php echo esc_attr( $settings['editor'] ); ?>"> |
| 136 |
<input id="editor-reg-user" type="hidden" name="editor-reg-user" value="<?php echo esc_attr( $settings['editor-reg-user'] ); ?>"> |
| 137 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_ms_term_and_conditions' ) ); ?>"> |
| 138 |
</form> |
| 139 |
<script> |
| 140 |
<?php |
| 141 |
if ( $settings['editor'] == 'html' ) { |
| 142 |
?> |
| 143 |
jQuery('.wpsc-switch-editor-ct.visual').trigger('click'); |
| 144 |
<?php |
| 145 |
} |
| 146 |
if ( $settings['editor-reg-user'] == 'html' ) { |
| 147 |
?> |
| 148 |
jQuery('.wpsc-switch-editor-reg-user.visual').trigger('click'); |
| 149 |
<?php |
| 150 |
} |
| 151 |
?> |
| 152 |
|
| 153 |
function wpsc_get_tinymce(el, selector, body_id){ |
| 154 |
jQuery(el).parent().find('.text').removeClass('active'); |
| 155 |
jQuery(el).addClass('active'); |
| 156 |
tinymce.remove('#'+selector); |
| 157 |
tinymce.init({ |
| 158 |
selector:'#'+selector, |
| 159 |
body_id: body_id, |
| 160 |
menubar: false, |
| 161 |
statusbar: false, |
| 162 |
height : '200', |
| 163 |
plugins: [ |
| 164 |
'lists link image directionality' |
| 165 |
], |
| 166 |
image_advtab: true, |
| 167 |
toolbar: 'bold italic underline blockquote | alignleft aligncenter alignright | bullist numlist | rtl | link image', |
| 168 |
directionality: '<?php echo is_rtl() ? 'rtl' : 'ltr'; ?>', |
| 169 |
branding: false, |
| 170 |
autoresize_bottom_margin: 20, |
| 171 |
browser_spellcheck : true, |
| 172 |
relative_urls : false, |
| 173 |
remove_script_host : false, |
| 174 |
convert_urls : true, |
| 175 |
setup: function (editor) { |
| 176 |
} |
| 177 |
}); |
| 178 |
if( selector == 'tandc-text' ) { |
| 179 |
jQuery('#editor').val('html'); |
| 180 |
}else{ |
| 181 |
jQuery('#editor-reg-user').val('html'); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
function wpsc_get_textarea(el, selector){ |
| 186 |
jQuery(el).parent().find('.visual').removeClass('active'); |
| 187 |
jQuery(el).addClass('active'); |
| 188 |
tinymce.remove('#'+selector); |
| 189 |
if( selector == 'tandc-text' ) { |
| 190 |
jQuery('#editor').val('text'); |
| 191 |
}else{ |
| 192 |
jQuery('#editor-reg-user').val('text'); |
| 193 |
} |
| 194 |
} |
| 195 |
</script> |
| 196 |
<div class="setting-footer-actions"> |
| 197 |
<button |
| 198 |
class="wpsc-button normal primary margin-right" |
| 199 |
onclick="wpsc_set_ms_term_and_conditions(this);"> |
| 200 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button> |
| 201 |
<button |
| 202 |
class="wpsc-button normal secondary" |
| 203 |
onclick="wpsc_reset_ms_term_and_conditions(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_reset_ms_term_and_conditions' ) ); ?>');"> |
| 204 |
<?php esc_attr_e( 'Reset default', 'supportcandy' ); ?></button> |
| 205 |
</div> |
| 206 |
<?php |
| 207 |
wp_die(); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Save settings |
| 212 |
* |
| 213 |
* @return void |
| 214 |
*/ |
| 215 |
public static function save_settings() { |
| 216 |
|
| 217 |
if ( check_ajax_referer( 'wpsc_set_ms_term_and_conditions', '_ajax_nonce', false ) != 1 ) { |
| 218 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 219 |
} |
| 220 |
|
| 221 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 222 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 223 |
} |
| 224 |
|
| 225 |
$conditions = apply_filters( |
| 226 |
'wpsc_set_term_conditions', |
| 227 |
array( |
| 228 |
'allow-term-and-conditions' => isset( $_POST['allow-term-and-conditions'] ) ? intval( $_POST['allow-term-and-conditions'] ) : 1, |
| 229 |
'tandc-text' => isset( $_POST ) && isset( $_POST['tandc-text'] ) ? wp_kses_post( wp_unslash( $_POST['tandc-text'] ) ) : '', |
| 230 |
'editor' => isset( $_POST['editor'] ) ? sanitize_text_field( wp_unslash( $_POST['editor'] ) ) : 'html', |
| 231 |
'allow-term-and-conditions-reg-user' => isset( $_POST['allow-term-and-conditions-reg-user'] ) ? intval( $_POST['allow-term-and-conditions-reg-user'] ) : 1, |
| 232 |
'tandc-text-reg-user' => isset( $_POST ) && isset( $_POST['tandc-text-reg-user'] ) ? wp_kses_post( wp_unslash( $_POST['tandc-text-reg-user'] ) ) : '', |
| 233 |
'editor-reg-user' => isset( $_POST['editor-reg-user'] ) ? sanitize_text_field( wp_unslash( $_POST['editor-reg-user'] ) ) : 'html', |
| 234 |
) |
| 235 |
); |
| 236 |
update_option( 'wpsc-term-and-conditions', $conditions ); |
| 237 |
|
| 238 |
// remove string translations. |
| 239 |
WPSC_Translations::remove( 'wpsc-term-and-conditions' ); |
| 240 |
WPSC_Translations::remove( 'wpsc-term-and-conditions-reg-user' ); |
| 241 |
|
| 242 |
// add string translations. |
| 243 |
WPSC_Translations::add( 'wpsc-term-and-conditions', $conditions['tandc-text'] ); |
| 244 |
WPSC_Translations::add( 'wpsc-term-and-conditions-reg-user', $conditions['tandc-text-reg-user'] ); |
| 245 |
wp_die(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Reset settings to default |
| 250 |
* |
| 251 |
* @return void |
| 252 |
*/ |
| 253 |
public static function reset_settings() { |
| 254 |
|
| 255 |
if ( check_ajax_referer( 'wpsc_reset_ms_term_and_conditions', '_ajax_nonce', false ) != 1 ) { |
| 256 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 257 |
} |
| 258 |
|
| 259 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 260 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 261 |
} |
| 262 |
self::reset(); |
| 263 |
wp_die(); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Print ticket form field |
| 268 |
* |
| 269 |
* @return void |
| 270 |
*/ |
| 271 |
public static function print_tff() { |
| 272 |
|
| 273 |
$tc = get_option( 'wpsc-term-and-conditions' ); |
| 274 |
if ( intval( $tc['allow-term-and-conditions'] ) === 1 ) : |
| 275 |
?> |
| 276 |
<div class="wpsc-tff term-and-conditions wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12 required wpsc-visible" data-cft="term-and-conditions"> |
| 277 |
<div class="checkbox-container"> |
| 278 |
<?php $unique_id = uniqid( 'wpsc_' ); ?> |
| 279 |
<input id="<?php echo esc_attr( $unique_id ); ?>" type="checkbox" value="1"/> |
| 280 |
<?php |
| 281 |
$name = WPSC_Translations::get( 'wpsc-term-and-conditions', stripslashes( $tc['tandc-text'] ) ); |
| 282 |
?> |
| 283 |
<label for="<?php echo esc_attr( $unique_id ); ?>"><?php echo wp_kses_post( $name ); ?></label> |
| 284 |
</div> |
| 285 |
</div> |
| 286 |
<?php |
| 287 |
endif; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Validate this type field in create ticket |
| 292 |
* |
| 293 |
* @return void |
| 294 |
*/ |
| 295 |
public static function js_validate_ticket_form() { |
| 296 |
?> |
| 297 |
|
| 298 |
case 'term-and-conditions': |
| 299 |
var checkbox = customField.find('input:checked'); |
| 300 |
if (checkbox.length === 0) { |
| 301 |
isValid = false; |
| 302 |
alert(supportcandy.translations.req_term_cond); |
| 303 |
} |
| 304 |
break; |
| 305 |
<?php |
| 306 |
echo PHP_EOL; |
| 307 |
} |
| 308 |
} |
| 309 |
endif; |
| 310 |
|
| 311 |
WPSC_MS_TAC::init(); |
| 312 |
|