__CLASS__,
'save-key' => 'id',
);
return $classes;
}
/**
* Print operators for ticket form filter
*
* @param WPSC_Custom_Field $cf - custom field object.
* @param array $filter - Existing filters (if any).
* @return void
*/
public static function get_operators( $cf, $filter = array() ) {
?>
array(
'relation' => 'AND',
array(
'slug' => 'ticket',
'compare' => '=',
'val' => $ticket->id,
),
array(
'slug' => 'type',
'compare' => '=',
'val' => 'report',
),
),
);
$threads = WPSC_Thread::find( $filters );
$thread = isset( $threads['results'][0] ) ? $threads['results'][0] : array();
if ( $thread && $thread->is_active ) {
$value = stripslashes( $thread->body );
$terms = explode( PHP_EOL, $condition['operand_val_1'] );
$response = false;
foreach ( $terms as $term ) {
$index = strpos( $value, trim( stripslashes( $term ) ) );
if ( is_numeric( $index ) ) {
$response = true;
break;
}
}
return $response;
} else {
return false;
}
}
/**
* Print ticket form field
*
* @param WPSC_Custom_Field $cf - Custom field object.
* @param array $tff - Array of ticket form field settings for this field.
* @return string
*/
public static function print_tff( $cf, $tff ) {
$unique_id = uniqid( 'wpsc_' );
$val = $cf->is_auto_fill && $cf->default_value ? stripslashes( htmlspecialchars( $cf->default_value[0] ) ) : '';
$current_user = WPSC_Current_User::$current_user;
ob_start();
?>
case '':
var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
if (is_tinymce && tinymce.get('description')){
var val = tinyMCE.get('description').getContent();
} else {
var val = jQuery('#description').val().trim();
}
if (customField.hasClass('required') && !val) {
isValid = false;
alert(supportcandy.translations.req_fields_missing);
}
break;
slug, $tff ) ) {
?>
var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
if (is_tinymce && tinymce.get('description')){
var description = tinyMCE.get('description').getContent();
} else {
var description = jQuery('#description').val().trim();
}
dataform.append('description', description);
default_value ? stripslashes( $cf->default_value[0] ) : '';
}
/**
* Return custom field value in $_POST
* Ignore phpcs nonce issue as we already checked where it is called from.
*
* @param string $slug - Custom field slug.
* @param mixed $cf - Custom field object or false.
* @return mixed
*/
public static function get_tff_value( $slug, $cf = false ) {
$cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
$default_val = self::get_default_value( $cf );
$description = isset( $_POST[ $slug ] ) ? wp_kses_post( wp_unslash( $_POST[ $slug ] ) ) : ''; // phpcs:ignore
// replace new line with br if no text editor.
$is_editor = isset( $_POST['is_editor'] ) ? intval( $_POST['is_editor'] ) : 0; // phpcs:ignore
if ( $description && ! $is_editor ) {
$description = nl2br( $description );
}
return $description ? $description : $default_val;
}
/**
* Check and return custom field value for new ticket to be created.
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
* Ignore phpcs nonce issue as we already checked where it is called from.
*
* @param array $data - Array of values to to stored in ticket in an insert function.
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
* @param boolean $is_my_profile - Whether it or not it is created from my-profile. This function is used by create ticket as well as my-profile. Due to customer fields handling is done same way, this flag gives apportunity to identify where it being called.
* @return array
*/
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
$data['description'] = self::get_tff_value( 'description' );
$description_attachments = isset( $_POST['description_attachments'] ) ? array_filter( array_map( 'intval', $_POST['description_attachments'] ) ) : array(); // phpcs:ignore
foreach ( $description_attachments as $key => $id ) {
$attachment = new WPSC_Attachment( $id );
// Check if attachment is already active and linked to a ticket.
if ( $attachment->is_active && $attachment->ticket_id ) {
unset( $description_attachments[ $key ] );
continue;
}
$attachment->is_active = 1;
$attachment->save();
}
$data['description_attachments'] = $description_attachments ? implode( '|', $description_attachments ) : '';
return $data;
}
/**
* Set create ticket data for rest api request
*
* @param array $data - create ticket data array.
* @param WP_REST_Request $request - rest request object.
* @param array $custom_fields - custom field objects indexed by unique custom field types.
* @return array
*/
public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
$cf = WPSC_Custom_Field::get_cf_by_slug( 'description' );
$description = $request->get_param( 'description' );
$is_html = $description != wp_strip_all_tags( $description ) ? true : false;
$description = $is_html ? wp_kses_post( $description ) : str_replace( PHP_EOL, '
', $description );
$description = $description ? $description : self::get_default_value( $cf );
// check description is present.
if ( ! $description ) {
$data['errors']->add( 'req_fields_missing', 'description is missing!', 'description' );
return $data;
}
$data['description'] = $description;
// description attachments.
$attachments = sanitize_text_field( $request->get_param( 'description_attachments' ) );
if ( $attachments ) {
$attachments = implode(
'|',
array_filter(
array_map(
function ( $id ) {
$attachment = new WPSC_Attachment( intval( $id ) );
if ( $attachment->id ) {
$attachment->is_active = 1;
$attachment->save();
return $attachment->id;
} else {
return false;
}
},
explode( ',', $attachments )
)
)
);
}
$data['description_attachments'] = $attachments;
return $data;
}
/**
* Return val field for meta query of this type of custom field
*
* @param array $condition - condition data.
* @return mixed
*/
public static function get_meta_value( $condition ) {
$operator = $condition['operator'];
switch ( $operator ) {
case 'LIKE':
return $condition['operand_val_1'];
}
return false;
}
/**
* Add ticket id to description attachments
*
* @param WPSC_Ticket $ticket - ticket object.
* @return void
*/
public static function add_attachment_ticket_id( $ticket ) {
$thread = $ticket->get_description_thread();
foreach ( $thread->attachments as $attachment ) {
$attachment->ticket_id = $ticket->id;
$attachment->source = 'report';
$attachment->source_id = $thread->id;
$attachment->save();
}
}
/**
* Print edit custom field properties
*
* @param WPSC_Custom_Fields $cf - custom field object.
* @param string $field_class - class name of field category.
* @return void
*/
public static function get_edit_custom_field_properties( $cf, $field_class ) {
if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
?>
default_value ? $cf->default_value[0] : '';
?>
extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
}
// default value.
if ( in_array( 'default_value', $field_class::$allowed_properties ) ) {
$default_value = isset( $_POST['default_value'] ) ? wp_kses_post( wp_unslash( $_POST['default_value'] ) ) : ''; // phpcs:ignore
$cf->default_value = $default_value ? array( $default_value ) : array();
}
// auto fill.
if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) {
$cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore
}
// save!
$cf->save();
}
/**
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
*
* @param WPSC_Custom_Field $cf - custom field object.
* @param WPSC_Ticket $ticket - ticket object.
* @param string $module - module name.
* @return string
*/
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
$filters = array(
'meta_query' => array(
'relation' => 'AND',
array(
'slug' => 'ticket',
'compare' => '=',
'val' => $ticket->id,
),
array(
'slug' => 'type',
'compare' => '=',
'val' => 'report',
),
),
);
$class_name = ( is_object( $ticket ) && is_a( $ticket, 'WPSC_Ticket' ) && ! empty( $ticket->id ) ) ? 'WPSC_Thread' : 'WPSC_Archive_Thread';
$threads = $class_name::find( $filters );
$thread = isset( $threads['results'][0] ) ? $threads['results'][0] : array();
return $thread && $thread->is_active ? $thread->get_printable_string() : '';
}
/**
* Clear value of hidden fields
*
* @return void
*/
public static function js_clear_value_hidden_fields() {
?>
case '':
var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
if (is_tinymce && tinymce.get('description')){
tinyMCE.get('description').setContent('');
} else {
jQuery('#description').val('');
}
break;