register_ajax_handlers(); } /** * Summary Ajax * * @return void * * - '-1' means that nonce could not be varified * - '-2' means that the post ID was not specified * - '-3' means that there isn't any summary data to return */ public function summary() { // nonce security. if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'ajax-nonce' ) ) { $error = new \WP_Error( '-1', 'Permission Denied' ); wp_send_json_error( $error ); } if ( ! isset( $_REQUEST['post_id'] ) ) { $error = new \WP_Error( '-2', 'The post ID was not set' ); wp_send_json_error( $error ); } $html = []; $html['content'] = ''; // password check. if ( (bool) get_option( 'edac_password_protected' ) === true ) { $admin_notices = new \EDAC\Admin\Admin_Notices(); $notice_text = $admin_notices->edac_password_protected_notice_text(); $html['password_protected'] = $notice_text; $html['content'] .= '
' . $notice_text . '
'; } $post_id = (int) $_REQUEST['post_id']; $summary = ( new Summary_Generator( $post_id ) )->generate_summary(); $simplified_summary_text = ''; $simplified_summary_prompt = get_option( 'edac_simplified_summary_prompt' ); $simplified_summary = get_post_meta( $post_id, '_edac_simplified_summary', true ) ? get_post_meta( $post_id, '_edac_simplified_summary', true ) : ''; $simplified_summary_grade = 0; if ( class_exists( 'DaveChild\TextStatistics\TextStatistics' ) ) { $text_statistics = new \DaveChild\TextStatistics\TextStatistics(); $simplified_summary_grade = (int) floor( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) ); } $simplified_summary_grade_failed = ( $simplified_summary_grade > 9 ) ? true : false; $simplified_summary_text = esc_html__( 'A Simplified summary has not been included for this content.', 'accessibility-checker' ); if ( 'none' !== $simplified_summary_prompt ) { if ( $summary['content_grade'] <= 9 ) { $simplified_summary_text = esc_html__( 'Your content has a reading level at or below 9th grade and does not require a simplified summary.', 'accessibility-checker' ); } elseif ( $summary['simplified_summary'] ) { if ( $simplified_summary_grade_failed ) { $simplified_summary_text = esc_html__( 'The reading level of the simplified summary is too high.', 'accessibility-checker' ); } else { $simplified_summary_text = esc_html__( 'A simplified summary has been included for this content.', 'accessibility-checker' ); } } } $html['content'] .= '
' . $summary['readability'] . '
Reading
Level
' . $simplified_summary_text . '
* True accessibility requires manual testing in addition to automated scans. Learn how to manually test for accessibility.
'; if ( ! $html ) { $error = new \WP_Error( '-3', 'No summary to return' ); wp_send_json_error( $error ); } wp_send_json_success( wp_json_encode( $html ) ); } /** * Details Ajax * * @return void * * - '-1' means that nonce could not be varified * - '-2' means that the post ID was not specified * - '-3' means that the table name is not valid * - '-4' means that there isn't any details to return */ public function details() { // nonce security. if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'ajax-nonce' ) ) { $error = new \WP_Error( '-1', 'Permission Denied' ); wp_send_json_error( $error ); } if ( ! isset( $_REQUEST['post_id'] ) ) { $error = new \WP_Error( '-2', 'The post ID was not set' ); wp_send_json_error( $error ); } $html = ''; global $wpdb; $table_name = edac_get_valid_table_name( $wpdb->prefix . 'accessibility_checker' ); $postid = (int) $_REQUEST['post_id']; $siteid = get_current_blog_id(); // Send error if table name is not valid. if ( ! $table_name ) { $error = new \WP_Error( '-3', 'Invalid table name' ); wp_send_json_error( $error ); } $rules = edac_register_rules(); if ( $rules ) { // if ANWW is active remove link_blank for details meta box. if ( EDAC_ANWW_ACTIVE ) { $rules = edac_remove_element_with_value( $rules, 'slug', 'link_blank' ); } // separate rule types. $passed_rules = []; $error_rules = edac_remove_element_with_value( $rules, 'rule_type', 'warning' ); $warning_rules = edac_remove_element_with_value( $rules, 'rule_type', 'error' ); // add count, unset passed error rules and add passed rules to array. if ( $error_rules ) { foreach ( $error_rules as $key => $error_rule ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation. $count = count( $wpdb->get_results( $wpdb->prepare( 'SELECT id, postid, object, ruletype, ignre, ignre_user, ignre_date, ignre_comment FROM %i where postid = %d and rule = %s and siteid = %d and ignre = %d', $table_name, $postid, $error_rule['slug'], $siteid, 0 ), ARRAY_A ) ); if ( $count ) { $error_rules[ $key ]['count'] = $count; } else { $error_rule['count'] = 0; $passed_rules[] = $error_rule; unset( $error_rules[ $key ] ); } } } // add count, unset passed warning rules and add passed rules to array. if ( $warning_rules ) { foreach ( $warning_rules as $key => $error_rule ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation. $count = count( $wpdb->get_results( $wpdb->prepare( 'SELECT id, postid, object, ruletype, ignre, ignre_user, ignre_date, ignre_comment FROM %i where postid = %d and rule = %s and siteid = %d and ignre = %d', $table_name, $postid, $error_rule['slug'], $siteid, 0 ), ARRAY_A ) ); if ( $count ) { $warning_rules[ $key ]['count'] = $count; } else { $error_rule['count'] = 0; $passed_rules[] = $error_rule; unset( $warning_rules[ $key ] ); } } } } // sort error rules by count. usort( $error_rules, function ( $a, $b ) { return strcmp( $b['count'], $a['count'] ); } ); // sort warning rules by count. usort( $warning_rules, function ( $a, $b ) { return strcmp( $b['count'], $a['count'] ); } ); // sort passed rules array by title. usort( $passed_rules, function ( $a, $b ) { return strcmp( $b['title'], $a['title'] ); } ); // merge rule arrays together. $rules = array_merge( $error_rules, $warning_rules, $passed_rules ); if ( $rules ) { /** * Filters if a user can ignore issues. * * @since 1.4.0 * * @allowed bool True if allowed, false if not */ $ignore_permission = apply_filters( 'edac_ignore_permission', true ); foreach ( $rules as $rule ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation. $results = $wpdb->get_results( $wpdb->prepare( 'SELECT id, postid, object, ruletype, ignre, ignre_user, ignre_date, ignre_comment, ignre_global FROM %i where postid = %d and rule = %s and siteid = %d', $table_name, $postid, $rule['slug'], $siteid ), ARRAY_A ); $count_classes = ( 'error' === $rule['rule_type'] ) ? ' edac-details-rule-count-error' : ' edac-details-rule-count-warning'; $count_classes .= ( 0 !== $rule['count'] ) ? ' active' : ''; $count_ignored = 0; $ignores = array_column( $results, 'ignre' ); if ( $ignores ) { foreach ( $ignores as $ignore ) { if ( true === (bool) $ignore ) { ++$count_ignored; } } } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Using direct query for interacting with custom database, safe variable used for table name, caching not required for one time operation. $expand_rule = count( $wpdb->get_results( $wpdb->prepare( 'SELECT id FROM %i where postid = %d and rule = %s and siteid = %d', $table_name, $postid, $rule['slug'], $siteid ), ARRAY_A ) ); $tool_tip_link = edac_documentation_link( $rule ); $html .= '
'; $html .= '
'; $html .= '

'; $html .= '' . $rule['count'] . ' '; $html .= esc_html( $rule['title'] ); if ( $count_ignored > 0 ) { $html .= '' . $count_ignored . ' Ignored Items'; } $html .= '

'; $html .= ''; $html .= ( $expand_rule ) ? '' : ''; $html .= '
'; if ( $results ) { $html .= '
'; $fixes_for_item = []; if ( isset( $rule['fixes'] ) && current_user_can( apply_filters( 'edac_filter_settings_capability', 'manage_options' ) ) ) { foreach ( $rule['fixes'] as $fix_slug ) { $fixes_for_item[] = FixesManager::get_instance()->get_fix( $fix_slug ); } $controls_id = 'edac-fix-modal-' . $rule['slug'] . '__' . implode( '__', $rule['fixes'] ); ob_start(); // NOTE: This is markup to be cloned into a thickbox modal. It gets cloned from the inner div. ?>
%s %s

', esc_html__( 'These settings enable global fixes across your entire site.', 'accessibility-checker' ), esc_html__( 'Pages may need to be resaved or a full site scan run to see fixes reflected in reports.', 'accessibility-checker' ) ) ?>
$fix ) : ?>

get_nicename() ); ?>

get_fields_array() as $name => $field ) { $field['name'] = $name; $field['location'] = 'details-panel'; FixesPage::{$field['type']}( $field ); } ?>
'; foreach ( $results as $row ) { $id = (int) $row['id']; $ignore = (int) $row['ignre']; $ignore_class = $ignore ? ' active' : ''; $ignore_label = $ignore ? 'Ignored' : 'Ignore'; $ignore_user = (int) $row['ignre_user']; $ignore_user_info = get_userdata( $ignore_user ); $ignore_username = is_object( $ignore_user_info ) ? 'Username: ' . $ignore_user_info->user_login : ''; $ignore_date = ( $row['ignre_date'] && '0000-00-00 00:00:00' !== $row['ignre_date'] ) ? 'Date: ' . gmdate( 'F j, Y g:i a', strtotime( esc_html( $row['ignre_date'] ) ) ) : ''; $ignore_comment = esc_html( $row['ignre_comment'] ); $ignore_action = $ignore ? 'disable' : 'enable'; $ignore_type = $rule['rule_type']; $ignore_submit_label = $ignore ? 'Stop Ignoring' : 'Ignore This ' . $ignore_type; $ignore_comment_disabled = $ignore ? 'disabled' : ''; $ignore_global = (int) $row['ignre_global']; // check for images and svgs in object code. $object_img = null; $object_svg = null; $object_img_html = str_get_html( htmlspecialchars_decode( $row['object'], ENT_QUOTES ) ); if ( $object_img_html ) { $object_img_elements = $object_img_html->find( 'img' ); $object_svg_elements = $object_img_html->find( 'svg' ); if ( $object_img_elements ) { foreach ( $object_img_elements as $element ) { $object_img = $element->getAttribute( 'src' ); if ( $object_img ) { break; } } } elseif ( $object_svg_elements ) { foreach ( $object_svg_elements as $element ) { $object_svg = $element; break; } } } $html .= '

Issue ID ' . $id . '

'; $html .= '
'; $html .= '
'; $html .= '' . esc_html( $row['object'] ) . ''; $html .= '
'; $html .= '
'; if ( $object_img ) { $html .= 'image for issue ' . $id . ''; } elseif ( $object_svg ) { $html .= $object_svg; } $html .= '
'; $html .= '
'; if ( ! isset( $rule['viewable'] ) || $rule['viewable'] ) { $url = add_query_arg( [ 'edac' => $id, 'edac_nonce' => wp_create_nonce( 'edac_highlight' ), ], get_the_permalink( $postid ) ); // Translators: %d is the issue ID. $aria_label = sprintf( __( 'View Issue ID %d on website, opens a new window', 'accessibility-checker' ), $id ); $html .= '' . __( 'View on page', 'accessibility-checker' ) . ''; } if ( true === $ignore_permission ) { $html .= ''; } if ( ! empty( $fixes_for_item ) ) { $html .= sprintf( '', esc_attr( $controls_id ), esc_attr( __( 'Fix: ', 'accessibility-checker' ) . $fixes_for_item[0]->get_nicename() ), esc_html__( 'Fix', 'accessibility-checker' ) ); } $html .= '
'; $html .= '
'; $html .= '
'; $html .= '' . $ignore_username . ''; $html .= ' ' . $ignore_date . ''; $html .= '
'; $html .= ( true === $ignore_permission || ! empty( $ignore_comment ) ) ? '
' : ''; $html .= ( true === $ignore_permission || ! empty( $ignore_comment ) ) ? '' : ''; if ( $ignore_global ) { $html .= ( true === $ignore_permission ) ? 'Manage Globally Ignored' : ''; } else { $html .= ( true === $ignore_permission ) ? '' : ''; } $html .= ( false === $ignore_permission && false === $ignore ) ? __( 'Your user account doesn\'t have permission to ignore this issue.', 'accessibility-checker' ) : ''; $html .= '
'; $html .= '
'; } $html .= '
'; } $html .= ''; } } if ( ! $html ) { $error = new \WP_Error( '-4', 'No details to return' ); wp_send_json_error( $error ); } wp_send_json_success( wp_json_encode( $html ) ); } /** * Readability Ajax * * @return void * * - '-1' means that nonce could not be varified * - '-2' means that the post ID was not specified * - '-3' means that there isn't any readability data to return */ public function readability() { // nonce security. if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'ajax-nonce' ) ) { $error = new \WP_Error( '-1', 'Permission Denied' ); wp_send_json_error( $error ); } if ( ! isset( $_REQUEST['post_id'] ) ) { $error = new \WP_Error( '-2', 'The post ID was not set' ); wp_send_json_error( $error ); } $post_id = (int) $_REQUEST['post_id']; $html = ''; $simplified_summary = get_post_meta( $post_id, '_edac_simplified_summary', true ) ? get_post_meta( $post_id, '_edac_simplified_summary', true ) : ''; $simplified_summary_position = get_option( 'edac_simplified_summary_position', $default = false ); $content_post = get_post( $post_id ); $content = $content_post->post_content; $content = apply_filters( 'the_content', $content ); $oxygen_builder_shortcodes_meta = get_post_meta( $post_id, 'ct_builder_shortcodes', true ); // add oxygen builder shortcode content to readability scan. if ( $oxygen_builder_shortcodes_meta ) { $oxygen_builder_shortcodes = do_shortcode( $oxygen_builder_shortcodes_meta ); if ( $oxygen_builder_shortcodes ) { $content .= $oxygen_builder_shortcodes; } } /** * Filter the content used for reading grade readability analysis. * * @since 1.4.0 * * @param string $content The content to be filtered. * @param int $post_id The post ID. */ $content = apply_filters( 'edac_filter_readability_content', $content, $post_id ); $content = wp_filter_nohtml_kses( $content ); $content = str_replace( ']]>', ']]>', $content ); // get readability metadata and determine if a simplified summary is required. $edac_summary = get_post_meta( $post_id, '_edac_summary', true ); $post_grade_readability = ( isset( $edac_summary['readability'] ) ) ? $edac_summary['readability'] : 0; $post_grade = (int) filter_var( $post_grade_readability, FILTER_SANITIZE_NUMBER_INT ); $post_grade_failed = ( $post_grade < 9 ) ? false : true; $simplified_summary_grade = 0; if ( class_exists( 'DaveChild\TextStatistics\TextStatistics' ) ) { $text_statistics = new \DaveChild\TextStatistics\TextStatistics(); $simplified_summary_grade = (int) floor( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) ); } $simplified_summary_grade_failed = ( $simplified_summary_grade > 9 ) ? true : false; $simplified_summary_prompt = get_option( 'edac_simplified_summary_prompt' ); $html .= ''; if ( ( $post_grade_failed || 'always' === $simplified_summary_prompt ) && ( 'none' !== $simplified_summary_prompt ) ) { $html .= '
'; } global $wp_version; $html .= 'Learn more about improving readability and simplified summary requirements'; if ( ! $html ) { $error = new \WP_Error( '-3', 'No readability data to return' ); wp_send_json_error( $error ); } wp_send_json_success( wp_json_encode( $html ) ); } /** * Insert ignore data into database * * @return void * * - '-1' means that nonce could not be varified * - '-2' means that there isn't any ignore data to return */ public function add_ignore() { // nonce security. if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['nonce'] ), 'ajax-nonce' ) ) { $error = new \WP_Error( '-1', 'Permission Denied' ); wp_send_json_error( $error ); } global $wpdb; $table_name = $wpdb->prefix . 'accessibility_checker'; $raw_ids = isset( $_REQUEST['ids'] ) ? $_REQUEST['ids'] : []; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitization handled below. $ids = array_map( function ( $value ) { return (int) $value; }, $raw_ids ); // Sanitizing array elements to integers. $action = isset( $_REQUEST['ignore_action'] ) ? sanitize_text_field( $_REQUEST['ignore_action'] ) : ''; $type = isset( $_REQUEST['ignore_type'] ) ? sanitize_text_field( $_REQUEST['ignore_type'] ) : ''; $siteid = get_current_blog_id(); $ignre = ( 'enable' === $action ) ? 1 : 0; $ignre_user = ( 'enable' === $action ) ? get_current_user_id() : null; $ignre_user_info = ( 'enable' === $action ) ? get_userdata( $ignre_user ) : ''; $ignre_username = ( 'enable' === $action ) ? $ignre_user_info->user_login : ''; $ignre_date = ( 'enable' === $action ) ? gmdate( 'Y-m-d H:i:s' ) : null; $ignre_date_formatted = ( 'enable' === $action ) ? gmdate( 'F j, Y g:i a', strtotime( $ignre_date ) ) : ''; $ignre_comment = ( 'enable' === $action && isset( $_REQUEST['comment'] ) ) ? sanitize_textarea_field( $_REQUEST['comment'] ) : null; $ignore_global = ( 'enable' === $action && isset( $_REQUEST['ignore_global'] ) ) ? sanitize_textarea_field( $_REQUEST['ignore_global'] ) : 0; // If largeBatch is set and 'true', we need to perform an update using the 'object' // instead of IDs. It is a much less efficient query than by IDs - but many IDs run // into request size limits which caused this to not function at all. if ( isset( $_REQUEST['largeBatch'] ) && 'true' === $_REQUEST['largeBatch'] ) { // Get the 'object' from the first id. $first_id = $ids[0]; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- We need to get the latest value, not a cached value. $object = $wpdb->get_var( $wpdb->prepare( 'SELECT object FROM %i WHERE id = %d', $table_name, $first_id ) ); if ( ! $object ) { $error = new \WP_Error( '-2', 'No ignore data to return' ); wp_send_json_error( $error ); } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Safe variable used for table name, caching not required for one time operation. $wpdb->query( $wpdb->prepare( 'UPDATE %i SET ignre = %d, ignre_user = %d, ignre_date = %s, ignre_comment = %s, ignre_global = %d WHERE siteid = %d and object = %s', $table_name, $ignre, $ignre_user, $ignre_date, $ignre_comment, $ignore_global, $siteid, $object ) ); } else { // For small batches of IDs, we can just loop through. foreach ( $ids as $id ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Safe variable used for table name, caching not required for one time operation. $wpdb->query( $wpdb->prepare( 'UPDATE %i SET ignre = %d, ignre_user = %d, ignre_date = %s, ignre_comment = %s, ignre_global = %d WHERE siteid = %d and id = %d', $table_name, $ignre, $ignre_user, $ignre_date, $ignre_comment, $ignore_global, $siteid, $id ) ); } } $data = [ 'ids' => $ids, 'action' => $action, 'type' => $type, 'user' => $ignre_username, 'date' => $ignre_date_formatted, ]; if ( ! $data ) { $error = new \WP_Error( '-2', 'No ignore data to return' ); wp_send_json_error( $error ); } wp_send_json_success( wp_json_encode( $data ) ); } /** * Update simplified summary * * @return void * * - '-1' means that nonce could not be varified * - '-2' means that the post ID was not specified * - '-3' means that the summary was not specified */ public function simplified_summary() { // nonce security. if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['nonce'] ), 'ajax-nonce' ) ) { $error = new \WP_Error( '-1', 'Permission Denied' ); wp_send_json_error( $error ); } if ( ! isset( $_REQUEST['post_id'] ) ) { $error = new \WP_Error( '-2', 'The post ID was not set' ); wp_send_json_error( $error ); } if ( ! isset( $_REQUEST['summary'] ) ) { $error = new \WP_Error( '-3', 'The summary was not set' ); wp_send_json_error( $error ); } $post_id = (int) $_REQUEST['post_id']; update_post_meta( $post_id, '_edac_simplified_summary', sanitize_text_field( $_REQUEST['summary'] ) ); $edac_simplified_summary = get_post_meta( $post_id, '_edac_simplified_summary', $single = true ); $simplified_summary = $edac_simplified_summary ? $edac_simplified_summary : ''; wp_send_json_success( wp_json_encode( $simplified_summary ) ); } /** * Handle AJAX request to dismiss Welcome CTA * * @return void */ public function dismiss_welcome_cta() { update_user_meta( get_current_user_id(), 'edac_welcome_cta_dismissed', true ); wp_send_json( 'success' ); } /** * Handle AJAX request to dismiss dashboard CTA * * @return void */ public function dismiss_dashboard_cta() { update_user_meta( get_current_user_id(), 'edac_dashboard_cta_dismissed', true ); wp_send_json( 'success' ); } }