| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_GS_Page_Settings' ) ) : |
| 7 |
|
| 8 |
final class WPSC_GS_Page_Settings { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// Search WP pages. |
| 18 |
add_action( 'wp_ajax_wpsc_search_wp_pages', array( __CLASS__, 'search_wp_pages' ) ); |
| 19 |
|
| 20 |
// User interface. |
| 21 |
add_action( 'wp_ajax_wpsc_get_gs_page_settings', array( __CLASS__, 'load_settings_ui' ) ); |
| 22 |
add_action( 'wp_ajax_wpsc_set_gs_page_settings', array( __CLASS__, 'save_settings' ) ); |
| 23 |
add_action( 'wp_ajax_wpsc_reset_gs_page_settings', array( __CLASS__, 'reset_settings' ) ); |
| 24 |
|
| 25 |
// After page delete in WP. |
| 26 |
add_action( 'delete_post', array( __CLASS__, 'after_delete_post' ), 10, 2 ); |
| 27 |
|
| 28 |
// WP_Query search by title. |
| 29 |
add_filter( 'posts_where', array( __CLASS__, 'search_wp_page_title' ), 10, 2 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Reset settings |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function reset() { |
| 38 |
|
| 39 |
$page_settings = apply_filters( |
| 40 |
'wpsc_gs_page_settings', |
| 41 |
array( |
| 42 |
'support-page' => 0, |
| 43 |
'open-ticket-page' => 0, |
| 44 |
'ticket-url-page' => 'support-page', |
| 45 |
'new-ticket-page' => 'default', |
| 46 |
'new-ticket-url' => '', |
| 47 |
'user-login' => 'default', |
| 48 |
'custom-login-url' => '', |
| 49 |
'user-registration' => 'disable', |
| 50 |
'custom-registration-url' => '', |
| 51 |
'otp-login' => 1, |
| 52 |
'load-scripts' => 'all-pages', |
| 53 |
'load-script-pages' => array(), |
| 54 |
) |
| 55 |
); |
| 56 |
update_option( 'wpsc-gs-page-settings', $page_settings ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Settings user interface |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public static function load_settings_ui() { |
| 65 |
|
| 66 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 67 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 68 |
} |
| 69 |
$settings = get_option( 'wpsc-gs-page-settings', array() );?> |
| 70 |
|
| 71 |
<form action="#" onsubmit="return false;" class="wpsc-frm-gs-ps"> |
| 72 |
<div class="wpsc-dock-container"> |
| 73 |
<?php |
| 74 |
printf( |
| 75 |
/* translators: Click here to see the documentation */ |
| 76 |
esc_attr__( '%s to see the documentation!', 'supportcandy' ), |
| 77 |
'<a href="https://supportcandy.net/docs/page-settings/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 78 |
); |
| 79 |
?> |
| 80 |
</div> |
| 81 |
<div class="wpsc-input-group"> |
| 82 |
<div class="label-container"> |
| 83 |
<label for=""><?php esc_attr_e( 'Support page', 'supportcandy' ); ?></label> |
| 84 |
</div> |
| 85 |
<select id="wpsc-support-page" class="wpsc-select-wp-page" name="support-page"> |
| 86 |
<?php |
| 87 |
if ( $settings['support-page'] ) { |
| 88 |
$page = get_post( $settings['support-page'] ) |
| 89 |
?> |
| 90 |
<option value="<?php echo esc_attr( $page->ID ); ?>"><?php echo esc_attr( $page->post_title ); ?></option> |
| 91 |
<?php |
| 92 |
} else { |
| 93 |
?> |
| 94 |
<option value="0"></option> |
| 95 |
<?php |
| 96 |
} |
| 97 |
?> |
| 98 |
</select> |
| 99 |
</div> |
| 100 |
<div class="wpsc-input-group"> |
| 101 |
<div class="label-container"> |
| 102 |
<label for=""><?php esc_attr_e( 'Open ticket page', 'supportcandy' ); ?></label> |
| 103 |
</div> |
| 104 |
<select id="wpsc-ot-page" class="wpsc-select-wp-page" name="open-ticket-page"> |
| 105 |
<?php |
| 106 |
if ( $settings['open-ticket-page'] ) { |
| 107 |
$page = get_post( $settings['open-ticket-page'] ) |
| 108 |
?> |
| 109 |
<option value="<?php echo esc_attr( $page->ID ); ?>"><?php echo esc_attr( $page->post_title ); ?></option> |
| 110 |
<?php |
| 111 |
} else { |
| 112 |
?> |
| 113 |
<option value="0"></option> |
| 114 |
<?php |
| 115 |
} |
| 116 |
?> |
| 117 |
</select> |
| 118 |
</div> |
| 119 |
<div class="wpsc-input-group"> |
| 120 |
<div class="label-container"> |
| 121 |
<label for=""><?php esc_attr_e( 'Ticket url page', 'supportcandy' ); ?></label> |
| 122 |
</div> |
| 123 |
<select id="wpsc-tu-page" name="ticket-url-page"> |
| 124 |
<option <?php selected( $settings['ticket-url-page'], 'support-page' ); ?> value="support-page"><?php esc_attr_e( 'Support page', 'supportcandy' ); ?></option> |
| 125 |
<option <?php selected( $settings['ticket-url-page'], 'open-ticket-page' ); ?> value="open-ticket-page"><?php esc_attr_e( 'Open ticket page', 'supportcandy' ); ?></option> |
| 126 |
</select> |
| 127 |
</div> |
| 128 |
<div class="wpsc-input-group"> |
| 129 |
<div class="label-container"> |
| 130 |
<label for=""><?php esc_attr_e( 'New ticket page', 'supportcandy' ); ?></label> |
| 131 |
</div> |
| 132 |
<select class="wpsc-login-url" name="new-ticket-page"> |
| 133 |
<option <?php selected( $settings['new-ticket-page'], 'default' ); ?> value="default"><?php esc_attr_e( 'Support page', 'supportcandy' ); ?></option> |
| 134 |
<option <?php selected( $settings['new-ticket-page'], 'custom' ); ?> value="custom"><?php esc_attr_e( 'Custom', 'supportcandy' ); ?></option> |
| 135 |
</select> |
| 136 |
</div> |
| 137 |
<div class="wpsc-input-group" style="<?php echo $settings['new-ticket-page'] != 'custom' ? 'display:none' : ''; ?>"> |
| 138 |
<input type="text" name="new-ticket-url" value="<?php echo esc_url( $settings['new-ticket-url'] ); ?>" placeholder="e.g. https://yourdomain.com/create-ticket" autocomplete="off"> |
| 139 |
</div> |
| 140 |
<div class="wpsc-input-group"> |
| 141 |
<div class="label-container"> |
| 142 |
<label for=""><?php esc_attr_e( 'User login', 'supportcandy' ); ?></label> |
| 143 |
</div> |
| 144 |
<select class="wpsc-login-url" name="user-login"> |
| 145 |
<option <?php selected( $settings['user-login'], 'default' ); ?> value="default"><?php esc_attr_e( 'Default', 'supportcandy' ); ?></option> |
| 146 |
<option <?php selected( $settings['user-login'], 'wp-default' ); ?> value="wp-default"><?php esc_attr_e( 'WP Default Login', 'supportcandy' ); ?></option> |
| 147 |
<option <?php selected( $settings['user-login'], 'custom' ); ?> value="custom"><?php esc_attr_e( 'Custom', 'supportcandy' ); ?></option> |
| 148 |
</select> |
| 149 |
</div> |
| 150 |
<div class="wpsc-input-group" style="<?php echo $settings['user-login'] != 'custom' ? 'display:none' : ''; ?>"> |
| 151 |
<input type="text" name="custom-login-url" value="<?php echo esc_url( $settings['custom-login-url'] ); ?>" placeholder="e.g. https://yourdomain.com/login" autocomplete="off"> |
| 152 |
</div> |
| 153 |
<div class="wpsc-input-group"> |
| 154 |
<div class="label-container"> |
| 155 |
<label for=""><?php esc_attr_e( 'User registration', 'supportcandy' ); ?></label> |
| 156 |
</div> |
| 157 |
<select class="wpsc-login-url" name="user-registration"> |
| 158 |
<option <?php selected( $settings['user-registration'], 'disable' ); ?> value="disable"><?php esc_attr_e( 'Disable', 'supportcandy' ); ?></option> |
| 159 |
<option <?php selected( $settings['user-registration'], 'default' ); ?> value="default"><?php esc_attr_e( 'Default', 'supportcandy' ); ?></option> |
| 160 |
<option <?php selected( $settings['user-registration'], 'wp-default' ); ?> value="wp-default"><?php esc_attr_e( 'WP Default Registration', 'supportcandy' ); ?></option> |
| 161 |
<option <?php selected( $settings['user-registration'], 'custom' ); ?> value="custom"><?php esc_attr_e( 'Custom', 'supportcandy' ); ?></option> |
| 162 |
</select> |
| 163 |
</div> |
| 164 |
<div class="wpsc-input-group" style="<?php echo $settings['user-registration'] != 'custom' ? 'display:none' : ''; ?>"> |
| 165 |
<input type="text" name="custom-registration-url" value="<?php echo esc_url( $settings['custom-registration-url'] ); ?>" placeholder="e.g. https://yourdomain.com/register" autocomplete="off"> |
| 166 |
</div> |
| 167 |
<div class="wpsc-input-group"> |
| 168 |
<div class="label-container"> |
| 169 |
<label for=""><?php esc_attr_e( 'OTP Login', 'supportcandy' ); ?></label> |
| 170 |
</div> |
| 171 |
<select name="otp-login"> |
| 172 |
<option <?php selected( $settings['otp-login'], 0 ); ?> value="0"><?php esc_attr_e( 'Disable', 'supportcandy' ); ?></option> |
| 173 |
<option <?php selected( $settings['otp-login'], 1 ); ?> value="1"><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></option> |
| 174 |
</select> |
| 175 |
</div> |
| 176 |
|
| 177 |
<div class="wpsc-input-group"> |
| 178 |
<div class="label-container"> |
| 179 |
<label for=""><?php esc_attr_e( 'Load scripts', 'supportcandy' ); ?></label> |
| 180 |
</div> |
| 181 |
<select class="wpsc-load-scripts" name="load-scripts"> |
| 182 |
<option <?php selected( $settings['load-scripts'], 'all-pages' ); ?> value="all-pages"><?php esc_attr_e( 'All Pages', 'supportcandy' ); ?></option> |
| 183 |
<option <?php selected( $settings['load-scripts'], 'custom' ); ?> value="custom"><?php esc_attr_e( 'Custom', 'supportcandy' ); ?></option> |
| 184 |
</select> |
| 185 |
</div> |
| 186 |
<div class="wpsc-input-group" style="<?php echo $settings['load-scripts'] != 'custom' ? 'display:none' : ''; ?>"> |
| 187 |
<select id="wpsc-load-script-pages" class="wpsc-select-wp-page" name="load-script-pages[]" multiple> |
| 188 |
<?php |
| 189 |
foreach ( $settings['load-script-pages'] as $page_id ) { |
| 190 |
?> |
| 191 |
<option selected value="<?php echo esc_attr( $page_id ); ?>"><?php echo esc_attr( get_the_title( $page_id ) ); ?></option> |
| 192 |
<?php |
| 193 |
} |
| 194 |
?> |
| 195 |
</select> |
| 196 |
</div> |
| 197 |
|
| 198 |
<?php do_action( 'wpsc_gs_page_settings' ); ?> |
| 199 |
|
| 200 |
<script> |
| 201 |
jQuery('.wpsc-login-url, .wpsc-load-scripts').change(function(){ |
| 202 |
var urlInput = jQuery(this).closest('.wpsc-input-group').next(); |
| 203 |
if (jQuery(this).val() === 'custom') { |
| 204 |
urlInput.show(); |
| 205 |
} else { |
| 206 |
urlInput.hide(); |
| 207 |
} |
| 208 |
}); |
| 209 |
jQuery('.wpsc-select-wp-page').selectWoo({ |
| 210 |
ajax: { |
| 211 |
url: supportcandy.ajax_url, |
| 212 |
dataType: 'json', |
| 213 |
delay: 250, |
| 214 |
data: function (params) { |
| 215 |
return { |
| 216 |
q: params.term, // search term |
| 217 |
page: params.page, |
| 218 |
action: 'wpsc_search_wp_pages', |
| 219 |
_ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_search_wp_pages' ) ); ?>' |
| 220 |
}; |
| 221 |
}, |
| 222 |
processResults: function (data, params) { |
| 223 |
var terms = []; |
| 224 |
if ( data ) { |
| 225 |
jQuery.each( data, function( id, text ) { |
| 226 |
terms.push( { id: text.id, text: text.title } ); |
| 227 |
}); |
| 228 |
} |
| 229 |
return { |
| 230 |
results: terms |
| 231 |
}; |
| 232 |
}, |
| 233 |
cache: true |
| 234 |
}, |
| 235 |
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work |
| 236 |
minimumInputLength: 1, |
| 237 |
allowClear: true, |
| 238 |
placeholder: "" |
| 239 |
}); |
| 240 |
</script> |
| 241 |
<input type="hidden" name="action" value="wpsc_set_gs_page_settings"> |
| 242 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_gs_page_settings' ) ); ?>"> |
| 243 |
|
| 244 |
</form> |
| 245 |
<div class="setting-footer-actions"> |
| 246 |
<button |
| 247 |
class="wpsc-button normal primary margin-right" |
| 248 |
onclick="wpsc_set_gs_page_settings(this);"> |
| 249 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button> |
| 250 |
<button |
| 251 |
class="wpsc-button normal secondary" |
| 252 |
onclick="wpsc_reset_gs_page_settings(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_reset_gs_page_settings' ) ); ?>');"> |
| 253 |
<?php esc_attr_e( 'Reset default', 'supportcandy' ); ?></button> |
| 254 |
</div> |
| 255 |
<?php |
| 256 |
wp_die(); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Save settings |
| 261 |
* |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
public static function save_settings() { |
| 265 |
|
| 266 |
if ( check_ajax_referer( 'wpsc_set_gs_page_settings', '_ajax_nonce', false ) != 1 ) { |
| 267 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 268 |
} |
| 269 |
|
| 270 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 271 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 272 |
} |
| 273 |
|
| 274 |
$new_ticket_url = isset( $_POST['new-ticket-url'] ) && filter_var( wp_unslash( $_POST['new-ticket-url'] ), FILTER_VALIDATE_URL ) ? sanitize_text_field( wp_unslash( $_POST['new-ticket-url'] ) ) : ''; |
| 275 |
$new_ticket_page = isset( $_POST['new-ticket-page'] ) ? sanitize_text_field( wp_unslash( $_POST['new-ticket-page'] ) ) : 'default'; |
| 276 |
if ( $new_ticket_page == 'custom' && ! $new_ticket_url ) { |
| 277 |
$new_ticket_page = 'default'; |
| 278 |
} |
| 279 |
|
| 280 |
$custom_login_url = isset( $_POST['custom-login-url'] ) && filter_var( wp_unslash( $_POST['custom-login-url'] ), FILTER_VALIDATE_URL ) ? sanitize_text_field( wp_unslash( $_POST['custom-login-url'] ) ) : ''; |
| 281 |
$user_login = isset( $_POST['user-login'] ) ? sanitize_text_field( wp_unslash( $_POST['user-login'] ) ) : 'default'; |
| 282 |
if ( $user_login == 'custom' && ! $custom_login_url ) { |
| 283 |
$user_login = 'default'; |
| 284 |
} |
| 285 |
|
| 286 |
$custom_registration_url = isset( $_POST['custom-registration-url'] ) && filter_var( wp_unslash( $_POST['custom-registration-url'] ), FILTER_VALIDATE_URL ) ? sanitize_text_field( wp_unslash( $_POST['custom-registration-url'] ) ) : ''; |
| 287 |
$user_registration = isset( $_POST['user-registration'] ) ? sanitize_text_field( wp_unslash( $_POST['user-registration'] ) ) : 'disable'; |
| 288 |
if ( $user_registration == 'custom' && ! $custom_registration_url ) { |
| 289 |
$user_registration = 'disable'; |
| 290 |
} |
| 291 |
|
| 292 |
$page_settings = apply_filters( |
| 293 |
'wpsc_set_gs_page_settings', |
| 294 |
array( |
| 295 |
'support-page' => isset( $_POST['support-page'] ) ? intval( $_POST['support-page'] ) : 0, |
| 296 |
'open-ticket-page' => isset( $_POST['open-ticket-page'] ) ? intval( $_POST['open-ticket-page'] ) : 0, |
| 297 |
'ticket-url-page' => isset( $_POST['ticket-url-page'] ) ? sanitize_text_field( wp_unslash( $_POST['ticket-url-page'] ) ) : 'support-page', |
| 298 |
'new-ticket-page' => $new_ticket_page, |
| 299 |
'new-ticket-url' => $new_ticket_url, |
| 300 |
'user-login' => $user_login, |
| 301 |
'custom-login-url' => $custom_login_url, |
| 302 |
'user-registration' => $user_registration, |
| 303 |
'custom-registration-url' => $custom_registration_url, |
| 304 |
'otp-login' => isset( $_POST['otp-login'] ) ? intval( $_POST['otp-login'] ) : 0, |
| 305 |
'load-scripts' => isset( $_POST['load-scripts'] ) ? sanitize_text_field( wp_unslash( $_POST['load-scripts'] ) ) : 'all-pages', |
| 306 |
'load-script-pages' => isset( $_POST['load-script-pages'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['load-script-pages'] ) ) ) : array(), |
| 307 |
) |
| 308 |
); |
| 309 |
update_option( 'wpsc-gs-page-settings', $page_settings ); |
| 310 |
wp_die(); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Reset settings to default |
| 315 |
* |
| 316 |
* @return void |
| 317 |
*/ |
| 318 |
public static function reset_settings() { |
| 319 |
|
| 320 |
if ( check_ajax_referer( 'wpsc_reset_gs_page_settings', '_ajax_nonce', false ) != 1 ) { |
| 321 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 322 |
} |
| 323 |
|
| 324 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 325 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 326 |
} |
| 327 |
self::reset(); |
| 328 |
wp_die(); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Search WP pages |
| 333 |
*/ |
| 334 |
public static function search_wp_pages() { |
| 335 |
|
| 336 |
if ( check_ajax_referer( 'wpsc_search_wp_pages', '_ajax_nonce', false ) != 1 ) { |
| 337 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 338 |
} |
| 339 |
|
| 340 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 341 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 342 |
} |
| 343 |
|
| 344 |
$term = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : ''; |
| 345 |
|
| 346 |
$args = array( |
| 347 |
'post_type' => 'page', |
| 348 |
'post_status' => 'publish', |
| 349 |
'orderby' => array( 'title' => 'ASC' ), |
| 350 |
'search_wpsc_title' => $term, |
| 351 |
); |
| 352 |
$query = new WP_Query( $args ); |
| 353 |
|
| 354 |
$response = array(); |
| 355 |
foreach ( $query->posts as $page ) { |
| 356 |
$response[] = array( |
| 357 |
'id' => $page->ID, |
| 358 |
'title' => $page->post_title, |
| 359 |
); |
| 360 |
} |
| 361 |
wp_send_json( $response ); |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Remove support page if already set when post is deleted |
| 366 |
* |
| 367 |
* @param integer $postid - post id. |
| 368 |
* @param WP_Post $post - post object. |
| 369 |
* @return void |
| 370 |
*/ |
| 371 |
public static function after_delete_post( $postid, $post ) { |
| 372 |
|
| 373 |
$page_settings = get_option( 'wpsc-gs-page-settings' ); |
| 374 |
|
| 375 |
if ( $page_settings['support-page'] == $postid ) { |
| 376 |
$page_settings['support-page'] = 0; |
| 377 |
} |
| 378 |
|
| 379 |
if ( $page_settings['open-ticket-page'] == $postid ) { |
| 380 |
$page_settings['open-ticket-page'] = 0; |
| 381 |
} |
| 382 |
|
| 383 |
update_option( 'wpsc-gs-page-settings', $page_settings ); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Search wp posts by title. |
| 388 |
* |
| 389 |
* @param array $where - where array in wp_query. |
| 390 |
* @param array $wp_query - wp query array. |
| 391 |
* @return array |
| 392 |
*/ |
| 393 |
public static function search_wp_page_title( $where, $wp_query ) { |
| 394 |
global $wpdb; |
| 395 |
$search_term = $wp_query->get( 'search_wpsc_title' ); |
| 396 |
if ( $search_term ) { |
| 397 |
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $wpdb->esc_like( $search_term ) ) . '%\''; |
| 398 |
} |
| 399 |
return $where; |
| 400 |
} |
| 401 |
} |
| 402 |
endif; |
| 403 |
|
| 404 |
WPSC_GS_Page_Settings::init(); |
| 405 |
|