Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update slash command design #2479

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions css/qsm-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -3383,4 +3383,11 @@ input#preferred-date-format-custom {
text-transform: none;
float: right;
}
.qsm-hightlight-text{
color: #ffffff;
background: #187FFA;
padding: 1px 3px 4px 5px;
border-radius: 2px;
margin-right: 2px;
}
/*TinyMCE Text-editor : end */
106 changes: 29 additions & 77 deletions js/qsm-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,27 +411,13 @@ var QSMAdmin;
// form_type (0, 1, 2).
function hide_show_quiz_options(form_type) {
if (0 == form_type) {
$('#score_roundoff').parents('tr').show();
$('#correct_answer_logic').show();
} else {
$('#score_roundoff').parents('tr').hide();
$('#correct_answer_logic').hide();
}
}

$(document).on('click', '.qsm_tab_content input[name="system"]', function () {
var value = $(this).val();
$('#correct_answer_logic, #score_roundoff').hide();
if (value == 0 || value == 3) {
$('#correct_answer_logic, #score_roundoff').show();
}
});
$(document).ready(function () {
var system_option = $("input[type=radio][name='system']:checked").val();
$('#correct_answer_logic, #score_roundoff').hide();
if (system_option == 0 || system_option == 3) {
$('#correct_answer_logic, #score_roundoff').show();
}
hide_show_quiz_options($("input[name='form_type']:checked").val());
if (jQuery('.qsm-date-picker').length) {
jQuery('.qsm-date-picker').datetimepicker({ format: 'm/d/Y H:i', step: 1});
Expand Down Expand Up @@ -608,20 +594,16 @@ var QSMAdmin;
$(document).on('change', '.qsm_page_qmn_global_settings input[name="qsm-quiz-settings[form_type]"]', function () {
if ( 0 == $(this).val() ) {
$('.global_setting_system').parents('tr').show();
$('#qsm-score-roundoff').parents('label').show();
$("#qsm-correct-answer-logic").show();
} else {
$('.global_setting_system').parents('tr').hide();
$('#qsm-score-roundoff').parents('label').hide();
$("#qsm-correct-answer-logic").hide();
}
});
$(document).on('change', '.global_setting_system input[name="qsm-quiz-settings[system]"]', function () {
if ( 1 != $(this).val() && 0 == $('.qsm_page_qmn_global_settings input[name="qsm-quiz-settings[form_type]"]:checked').val() ) {
$('#qsm-score-roundoff').parents('label').show();
$("#qsm-correct-answer-logic").show();
} else {
$('#qsm-score-roundoff').parents('label').hide();
$("#qsm-correct-answer-logic").hide();
}
});
Expand Down Expand Up @@ -756,7 +738,6 @@ var QSMAdmin;
container.removeClass('opened');
}
});

}(jQuery));

// result page
Expand Down Expand Up @@ -995,8 +976,11 @@ if(current_id == 'qsm_variable_text'){ jQuery(".current_variable")[0].click();}
}
}(jQuery));

// QSM - Quiz Wizard

function qsm_is_substring_in_array( text, array ) {
return array.some(function(item) {
return text.includes(item);
});
}
(function ($) {
if (jQuery('body').hasClass('post-type-qsm_quiz') || jQuery('body').hasClass('toplevel_page_qsm_dashboard')) {
$('#create-quiz-button').on('click', function (event) {
Expand Down Expand Up @@ -1274,7 +1258,7 @@ if(current_id == 'qsm_variable_text'){ jQuery(".current_variable")[0].click();}
editor.execCommand('Delete');
}
//editor.insertContent( command.description );
editor.execCommand('mceInsertContent', false, '<qsmvariabletag>'+ command.value.replace(/%/g, '') +'</qsmvariabletag>&nbsp;' );
editor.execCommand('mceInsertContent', false, command.value.replace(/%([^%]+)%/g, '<qsmvariabletag>$1</qsmvariabletag>&nbsp;') );

autocomplete.remove();
editor.getContainer().setAttribute('qsm_search', '');
Expand Down Expand Up @@ -1313,6 +1297,20 @@ if(current_id == 'qsm_variable_text'){ jQuery(".current_variable")[0].click();}
//on keydowm inside editor
editor.on('keydown', function (e) {

if (e.keyCode === 13) {
let selection = editor.selection;
let range = selection.getRng();
let tagText = range.startContainer.parentNode.textContent;
if ( 'qsmvariabletag' === range.startContainer.parentNode.nodeName.toLowerCase() && range.startOffset === tagText.length ) {
let newParagraph = editor.dom.create('p');
editor.dom.insertAfter(newParagraph, range.startContainer.parentNode);
range.setStartAfter(newParagraph);
range.collapse(true);
selection.setRng(range);
e.preventDefault();
}
}

if (e.keyCode === 191 && e.ctrlKey === false && e.altKey === false && e.shiftKey === false) {
// "/" key pressed, trigger autocomplete
showAutocomplete( editor, true );
Expand Down Expand Up @@ -1345,61 +1343,15 @@ if(current_id == 'qsm_variable_text'){ jQuery(".current_variable")[0].click();}

});

function sanitizeHtml(content) {

// Match <img> tags with src attributes
content = content.replace(/<img\b.*?src\s*=\s*['"]([^'"]+)['"].*?>/gi, function(match, src) {
src = ( 'undefined' === typeof src || null === src ) ? '': src.split('?')[0];
// Check if the src URL is valid (ends with .jpg, .jpeg, .png, or .gif)
if (src.match(/\.(jpg|jpeg|png|gif|webp)$/i)) {
return match; // Valid src, keep the <img> tag
} else {
return ''; // Invalid src, remove the <img> tag
}
editor.on('paste', function (event) {
let clipboardData = (event.originalEvent || event).clipboardData;
let pastedValue = clipboardData.getData('text');
var variables = commands.map(function(item) {
return item.value;
});

// Remove style attribute
content = content.replace(/style\s*=\s*(['"])(.*?)\1/gi, '');

// Remove background attribute
content = content.replace(/background\s*=\s*(['"])(.*?)\1/gi, '');

// Remove 'javascript:' injection, alert, prompt, confirm
content = content.replace(/javascript:/gi, '');
content = content.replace(/alert\(/gi, '');
content = content.replace(/prompt\(/gi, '');
content = content.replace(/confirm\(/gi, '');

// Remove unwanted HTML tags like script, svg, title, meta, input etc.
content = content.replace(/<script\b[^>]*>.*?<\/script>/gi, '');
content = content.replace(/<svg\b[^>]*>.*?<\/svg>/gi, '');
content = content.replace(/<title\b[^>]*>.*?<\/title>/gi, '');
content = content.replace(/<meta\b[^>]*>/gi, '');
content = content.replace(/<input\b[^>]*>/gi, '');
content = content.replace(/<link\b[^>]*>/gi, '');

// Remove any on event attributes
content = content.replace(/on\w+\s*=\s*(['"][^'"]*['"]|[^>\s]+)/gi, '');

return content;
}

// On change : sanitize content
editor.on('change', function(e) {

//Only for result template
if ( -1 != editor.id.indexOf('results-page') ) {

var content = editor.getContent();
var newContent = sanitizeHtml( content );
//if sanitize string and content are not same
if ( content != newContent ) {
//Set content
editor.setContent( newContent );
// Move the cursor to the end
editor.selection.select(editor.getBody(), true);
editor.selection.collapse(false);
}
if (variables.includes(pastedValue)) {
event.preventDefault();
editor.execCommand('mceInsertContent', false, pastedValue.replace(/%([^%]+)%/g, '<qsmvariabletag>$1</qsmvariabletag>&nbsp;') );
}
});
});
Expand Down Expand Up @@ -3851,7 +3803,7 @@ var import_button;
'operator': 'greater',
'value': '0'
}];
var page = '%QUESTIONS_ANSWERS%';
var page = '%QUESTIONS_ANSWERS% ';
QSMAdminResults.addResultsPage(conditions, page);
}
};
Expand Down
12 changes: 11 additions & 1 deletion mlw_quizmaster2.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ public function qsm_admin_scripts_style( $hook ) {
wp_enqueue_style( 'jquer-multiselect-css', QSM_PLUGIN_CSS_URL . '/jquery.multiselect.min.css', array(), $this->version );
wp_enqueue_script( 'qsm-jquery-multiselect-js', QSM_PLUGIN_JS_URL . '/jquery.multiselect.min.js', array( 'jquery' ), $this->version, true );
wp_enqueue_script( 'micromodal_script', plugins_url( 'js/micromodal.min.js', __FILE__ ), array( 'jquery', 'qsm_admin_js' ), $this->version, true );
$qsm_variables = function_exists( 'qsm_text_template_variable_list' ) ? qsm_text_template_variable_list() : array();
$qsm_variables_name = array();
foreach ( $qsm_variables as $key => $value ) {
// Iterate over each key of the nested object
foreach ( $value as $nestedKey => $nestedValue ) {
// Add the nested key to the array
$qsm_variables_name[] = $nestedKey;
}
}
$qsm_admin_messages = array(
'error' => __('Error', 'quiz-master-next'),
'success' => __('Success', 'quiz-master-next'),
Expand Down Expand Up @@ -452,7 +461,8 @@ public function qsm_admin_scripts_style( $hook ) {
'questions_not_found' => __("Question not found!", 'quiz-master-next'),
'add_more' => __("Add", 'quiz-master-next'),
'_X_validation_fails' => __("Please enter an appropriate value for 'X'", 'quiz-master-next'),
'qsm_variables' => function_exists( 'qsm_text_template_variable_list' ) ? qsm_text_template_variable_list() : '',
'qsm_variables' => $qsm_variables,
'qsm_variables_name' => $qsm_variables_name,
'no_variables' => __("No Variable Found", 'quiz-master-next'),
'slash_command' => __("slash command", 'quiz-master-next'),
'insert_variable' => __("Insert QSM variables", 'quiz-master-next'),
Expand Down
2 changes: 1 addition & 1 deletion php/admin/admin-results-details-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function qsm_generate_results_details_tab() {
$template .= '<div class="response-inner-wrap">';
$template .= '<div id="submitdiv" class="postbox "><h2 class="hndle ui-sortable-handle"><span>'.__('Responses','quiz-master-next').'</span></h2>';
$template .= '<div class="inside check">';
$template .= '%QUESTIONS_ANSWERS%';
$template .= '%QUESTIONS_ANSWERS% ';
$template .= '</div>';
$template .= '</div>';
$template .= '</div>';
Expand Down
3 changes: 1 addition & 2 deletions php/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,15 +973,14 @@ function qsm_text_template_variable_list() {
'%CATEGORY_AVERAGE_POINTS%' => __( 'The average points from all categories.', 'quiz-master-next' ),
'%CATEGORY_AVERAGE_SCORE%' => __( 'The average score from all categories.', 'quiz-master-next' ),
'%QUESTION_MAX_POINTS%' => __( 'Maximum points of the question', 'quiz-master-next' ),
'%FACEBOOK_SHARE%' => __( 'Displays button to share on Facebook.', 'quiz-master-next' ),
'%TWITTER_SHARE%' => __( 'Displays button to share on Twitter.', 'quiz-master-next' ),
'%RESULT_LINK%' => __( 'The link of the result page.', 'quiz-master-next' ),
'%CONTACT_X%' => __( 'Value user entered into contact field. X is # of contact field. For example, first contact field would be %CONTACT_1%', 'quiz-master-next' ),
'%CONTACT_ALL%' => __( 'Value user entered into contact field. X is # of contact field. For example, first contact field would be %CONTACT_1%', 'quiz-master-next' ),
'%AVERAGE_CATEGORY_POINTS_X%' => __( 'X: Category name - The average amount of points a specific category earned.', 'quiz-master-next' ),
'%QUESTION_ANSWER_X%' => __( 'X = Question ID. It will show result of particular question.', 'quiz-master-next' ),
'%ANSWER_X%' => __( 'X = Question ID. It will show result of particular question.', 'quiz-master-next' ),
'%TIME_FINISHED%' => __( 'Display time after quiz submission.', 'quiz-master-next' ),
'%QUESTIONS_ANSWERS_EMAIL%' => __( 'Shows the question, the answer provided by user, and the correct answer.', 'quiz-master-next' ),
),
);
$variable_list = apply_filters( 'qsm_text_variable_list', $variable_list );
Expand Down
25 changes: 21 additions & 4 deletions php/admin/options-page-email-tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ function qsm_options_emails_tab_content() {
<main class="qsm-popup__content" id="show-all-variable-content">
<?php
$variable_list = qsm_text_template_variable_list();
$variable_list['Core']['%QUESTIONS_ANSWERS_EMAIL%'] = __( 'Shows the question, the answer provided by user, and the correct answer.', 'quiz-master-next' );
unset( $variable_list['Core']['%FACEBOOK_SHARE%'] );
unset( $variable_list['Core']['%TWITTER_SHARE%'] );
$variable_list = qsm_extra_template_and_leaderboard($variable_list);
// filter to add or remove variables from variable list for email tab
$variable_list = apply_filters( 'qsm_text_variable_list_email', $variable_list );
Expand Down Expand Up @@ -209,7 +206,27 @@ function qsm_options_emails_tab_template() {
<label><?php esc_html_e( 'Email Subject', 'quiz-master-next' ); ?></label>
<input type="text" class="qsm-email-subject" value="{{ data.subject }}">
<label><?php esc_html_e( 'Email Content', 'quiz-master-next' ); ?></label>
<textarea id="email-template-{{ data.id }}" class="email-template">{{{ data.content.replace(/%([^%]+)%/g, '<qsmvariabletag>$1</qsmvariabletag>') }}}</textarea>
<textarea id="email-template-{{ data.id }}" class="email-template">
{{{ data.content.replace(/%([^%]+)%/g, function(match, capturedValue) {
let qsm_varaible_list = qsm_admin_messages.qsm_variables_name;
for (let qsm_variable in qsm_admin_messages.qsm_variables_name) {
variable_name = qsm_admin_messages.qsm_variables_name[qsm_variable];
if( variable_name.includes('%%') ){
var arrayValues = variable_name.split("%%");
qsm_varaible_list = jQuery.merge(jQuery.merge([], arrayValues), qsm_varaible_list);
};
if( variable_name.includes('_X%') ){
qsm_varaible_list[qsm_variable] = variable_name.slice(0, -2);
}
}
if (qsm_is_substring_in_array(match, qsm_varaible_list)) {
return '<qsmvariabletag>' + capturedValue + '</qsmvariabletag>';
}else{
return match;
}
}) }}}
</textarea>
<p><?php esc_html_e( 'Type', 'quiz-master-next' );?> <span class="qsm-hightlight-text"> / </span> <?php esc_html_e( ' to insert template variables', 'quiz-master-next' ); ?></p>
<?php do_action( 'qsm_email_page_after', $quiz_id, $categories ); ?>
</div>
</main>
Expand Down
23 changes: 21 additions & 2 deletions php/admin/options-page-results-page-tab.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,27 @@ function qsm_options_results_tab_template(){
<h4><?php esc_html_e( '...Show', 'quiz-master-next' ); ?></h4>
<p><?php esc_html_e( 'The following result page.', 'quiz-master-next' ); ?></p>
</div>
<textarea id="results-page-{{ data.id }}" class="results-page-template">{{{ data.page.replace(/%([^%]+)%/g, '<qsmvariabletag>$1</qsmvariabletag>') }}}</textarea>
<span><?php esc_html_e( 'Type / to insert template variables', 'quiz-master-next' ); ?></p>
<textarea id="results-page-{{ data.id }}" class="results-page-template">
{{{ data.page.replace(/%([^%]+)%/g, function(match, capturedValue) {
let qsm_varaible_list = qsm_admin_messages.qsm_variables_name;
for (let qsm_variable in qsm_varaible_list) {
variable_name = qsm_varaible_list[qsm_variable];
if( variable_name.includes('%%') ){
var arrayValues = variable_name.split("%%");
qsm_varaible_list = jQuery.merge(jQuery.merge([], arrayValues), qsm_varaible_list);
};
if( variable_name.includes('_X%') ){
qsm_varaible_list[qsm_variable] = variable_name.slice(0, -2);
}
}
if (qsm_is_substring_in_array(match, qsm_varaible_list)) {
return '<qsmvariabletag>' + capturedValue + '</qsmvariabletag>';
}else{
return match;
}
}) }}}
</textarea>
<p><?php esc_html_e( 'Type', 'quiz-master-next' );?> <span class="qsm-hightlight-text"> / </span> <?php esc_html_e( ' to insert template variables', 'quiz-master-next' ); ?></p>
<?php do_action( 'qsm_result_page_before_redirect_input', $quiz_id, $categories ); ?>
<p><?php esc_html_e( 'Or, redirect the user by entering the URL below:', 'quiz-master-next' ); ?></p>
<input type="text" class="results-page-redirect" value="<# if ( data.redirect ) { #>{{ data.redirect }}<# } #>">
Expand Down
9 changes: 8 additions & 1 deletion php/classes/class-qsm-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,14 @@ public static function save_emails( $quiz_id, $emails ) {
$emails[ $i ]['replyTo'] = false;
}
if ( isset( $emails[ $i ]['content'] ) ) {
$emails[ $i ]['content'] = wp_kses_post( preg_replace( '/<qsmvariabletag>([^<]+)<\/qsmvariabletag>/', strip_tags('%$1%'), $emails[ $i ]['content'] ) );
$emails[ $i ]['content'] = preg_replace_callback(
'/<qsmvariabletag>([^<]+)<\/qsmvariabletag>/u',
function( $matches ) {
$content = '%' . wp_strip_all_tags( preg_replace('/^\s+|\s+$/u', '', $matches[1] ) ) . '%';
return $content;
},
wp_kses_post( $emails[ $i ]['content'] )
);
}
$mlwQuizMasterNext->pluginHelper->qsm_register_language_support( $emails[ $i ]['subject'], "quiz-email-subject-{$i}-{$quiz_id}" );
$mlwQuizMasterNext->pluginHelper->qsm_register_language_support( $emails[ $i ]['content'], "quiz-email-content-{$i}-{$quiz_id}" );
Expand Down
Loading
Loading