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'] .= '
';
$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' )
)
?>
Your post has a reading level higher than 9th grade. Web Content Accessibility Guidelines (WCAG) at the AAA level require a simplified summary of your post that is 9th grade or below.
';
} elseif ( 0 === $post_grade ) {
$html .= '
Your post does not contain enough content to calculate its reading level.
';
} else {
$html .= '
A simplified summary is not necessary when content reading level is 9th grade or below. Choose when to prompt for a simplified summary on the settings page.
';
}
$html .= '
';
if ( $post_grade_failed ) {
if ( $simplified_summary && 'none' !== $simplified_summary_prompt ) {
$html .= '
Simplified summary is not being automatically inserted into the content.
Your Prompt for Simplified Summary is set to "never." If you would like the simplified summary to be displayed automatically, you can change this on the settings page.
Simplified summary is being automatically inserted ' . $simplified_summary_position . ' the content.
Set where the Simplified Summary is inserted into the content on the settings page.
';
} else {
$html .=
'
Simplified summary is not being automatically inserted into the content.
Your Simplified Summary location is set to "manually" which requires a function be added to your page template. If you would like the simplified summary to be displayed automatically, you can change this on the settings page.
';
}
}
$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' );
}
}