From e0547b44a051c56db82cf961c72d940b6aff8dbb Mon Sep 17 00:00:00 2001 From: Mohammad Zubair Ali Date: Fri, 1 Mar 2024 16:07:22 +0530 Subject: [PATCH] update slash command design --- css/qsm-admin.css | 7 ++ js/qsm-admin.js | 106 ++++++-------------- mlw_quizmaster2.php | 12 ++- php/admin/admin-results-details-page.php | 2 +- php/admin/functions.php | 3 +- php/admin/options-page-email-tab.php | 25 ++++- php/admin/options-page-results-page-tab.php | 23 ++++- php/classes/class-qsm-emails.php | 9 +- php/classes/class-qsm-results-pages.php | 60 ++--------- 9 files changed, 107 insertions(+), 140 deletions(-) diff --git a/css/qsm-admin.css b/css/qsm-admin.css index d8b54ae8c..d26466f60 100644 --- a/css/qsm-admin.css +++ b/css/qsm-admin.css @@ -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 */ \ No newline at end of file diff --git a/js/qsm-admin.js b/js/qsm-admin.js index 056fb949c..70880fff5 100644 --- a/js/qsm-admin.js +++ b/js/qsm-admin.js @@ -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}); @@ -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(); } }); @@ -756,7 +738,6 @@ var QSMAdmin; container.removeClass('opened'); } }); - }(jQuery)); // result page @@ -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) { @@ -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, ''+ command.value.replace(/%/g, '') +' ' ); + editor.execCommand('mceInsertContent', false, command.value.replace(/%([^%]+)%/g, '$1 ') ); autocomplete.remove(); editor.getContainer().setAttribute('qsm_search', ''); @@ -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 ); @@ -1345,61 +1343,15 @@ if(current_id == 'qsm_variable_text'){ jQuery(".current_variable")[0].click();} }); - function sanitizeHtml(content) { - - // Match tags with src attributes - content = content.replace(//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 tag - } else { - return ''; // Invalid src, remove the 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>/gi, ''); - content = content.replace(/]*>.*?<\/svg>/gi, ''); - content = content.replace(/]*>.*?<\/title>/gi, ''); - content = content.replace(/]*>/gi, ''); - content = content.replace(/]*>/gi, ''); - content = content.replace(/]*>/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, '$1 ') ); } }); }); @@ -3851,7 +3803,7 @@ var import_button; 'operator': 'greater', 'value': '0' }]; - var page = '%QUESTIONS_ANSWERS%'; + var page = '%QUESTIONS_ANSWERS% '; QSMAdminResults.addResultsPage(conditions, page); } }; diff --git a/mlw_quizmaster2.php b/mlw_quizmaster2.php index cb700ffbe..7ef9b6dca 100644 --- a/mlw_quizmaster2.php +++ b/mlw_quizmaster2.php @@ -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'), @@ -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'), diff --git a/php/admin/admin-results-details-page.php b/php/admin/admin-results-details-page.php index 1c0f9e5c7..9f3cc1f2c 100644 --- a/php/admin/admin-results-details-page.php +++ b/php/admin/admin-results-details-page.php @@ -247,7 +247,7 @@ function qsm_generate_results_details_tab() { $template .= '
'; $template .= '

'.__('Responses','quiz-master-next').'

'; $template .= '
'; - $template .= '%QUESTIONS_ANSWERS%'; + $template .= '%QUESTIONS_ANSWERS% '; $template .= '
'; $template .= '
'; $template .= '
'; diff --git a/php/admin/functions.php b/php/admin/functions.php index e4b6d5ac2..4c0374e3d 100644 --- a/php/admin/functions.php +++ b/php/admin/functions.php @@ -973,8 +973,6 @@ 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' ), @@ -982,6 +980,7 @@ function qsm_text_template_variable_list() { '%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 ); diff --git a/php/admin/options-page-email-tab.php b/php/admin/options-page-email-tab.php index 053839247..cf1d3995e 100644 --- a/php/admin/options-page-email-tab.php +++ b/php/admin/options-page-email-tab.php @@ -80,9 +80,6 @@ function qsm_options_emails_tab_content() {
- + +

/

diff --git a/php/admin/options-page-results-page-tab.php b/php/admin/options-page-results-page-tab.php index 781e774f4..570f726c8 100644 --- a/php/admin/options-page-results-page-tab.php +++ b/php/admin/options-page-results-page-tab.php @@ -229,8 +229,27 @@ function qsm_options_results_tab_template(){

- -

+ +

/

diff --git a/php/classes/class-qsm-emails.php b/php/classes/class-qsm-emails.php index c9148fc54..602271f33 100644 --- a/php/classes/class-qsm-emails.php +++ b/php/classes/class-qsm-emails.php @@ -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>/', strip_tags('%$1%'), $emails[ $i ]['content'] ) ); + $emails[ $i ]['content'] = preg_replace_callback( + '/([^<]+)<\/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}" ); diff --git a/php/classes/class-qsm-results-pages.php b/php/classes/class-qsm-results-pages.php index cd3866154..c892712df 100644 --- a/php/classes/class-qsm-results-pages.php +++ b/php/classes/class-qsm-results-pages.php @@ -321,51 +321,6 @@ public static function convert_to_new_system( $quiz_id ) { return $pages; } - public static function sanitize_html( $html = '' ) { - - //Decode Html - $html = htmlspecialchars_decode( $html, ENT_QUOTES ); - - // Remove unwanted html tags - $html = preg_replace('/<(script|form|textarea|div|body|title|svg|link|meta)[^>]*>.*?<\/\1>/is', '', $html); - - // Remove styles attributes - $html = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $html); - - // Remove background attributes - $html = preg_replace('/(<[^>]+) background=".*?"/i', '$1', $html); - - // Remove input tags - $html = preg_replace('/]*>/i', '', $html); - - // Remove any on event attributes - $html = preg_replace('/\s*on\w+\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+)/i', '', $html); - - // Remove any alert, confirm, or prompt calls - $html = preg_replace('/\b(alert|confirm|prompt)\s*\(\s*[^;]*\s*\)\s*;?/i', '', $html); - - // Remove any javascript: URLs - $html = preg_replace('/javascript:/i', '', $html); - - // Filter image src for possible image types, safe URL, and no $_GET parameters - $html = preg_replace_callback('/]*>/i', function($matches) { - $src = $matches[1]; - $valid_image_types = array('jpg', 'jpeg', 'png', 'gif', 'webp' ); - $file_extension = pathinfo($src, PATHINFO_EXTENSION); - $url_parts = parse_url($src); - - if (in_array(strtolower($file_extension), $valid_image_types) && - isset($url_parts['scheme']) && in_array(strtolower($url_parts['scheme']), array('http', 'https')) && - empty($url_parts['query'])) { - return $matches[0]; - } else { - return ''; - } - }, $html); - - return $html; - } - /** * Saves the results pages for a quiz. * @@ -413,13 +368,14 @@ public static function save_pages( $quiz_id, $pages ) { // Sanitize template data if ( isset( $pages[ $i ]['page'] ) && $is_not_allow_html ) { - $pages[ $i ]['page'] = QSM_Results_Pages::sanitize_html( $pages[ $i ]['page'] ); - //Check if encoded html string given - if ( false === stripos( $pages[ $i ]['page'] ,'<span class="qsm-highlight-variables">') ) { - $pages[ $i ]['page'] = wp_kses_post( preg_replace( '/([^<]+)<\/qsmvariabletag>/', '%$1%', $pages[ $i ]['page'] ) ); - } else { - $pages[ $i ]['page'] = wp_kses_post( preg_replace('/<qsmvariabletag>([^&]+)<\/qsmvariabletag>/i', '%$1%', $pages[ $i ]['page'] ) ); - } + $pages[ $i ]['page'] = preg_replace_callback( + '/([^<]+)<\/qsmvariabletag>/u', + function( $matches ) { + $content = '%' . wp_strip_all_tags( preg_replace('/^\s+|\s+$/u', '', $matches[1] ) ) . '%'; + return $content; + }, + wp_kses_post( $pages[ $i ]['page'] ) + ); } $pages[ $i ]['default_mark'] = sanitize_text_field( $pages[ $i ]['default_mark'] );