$permalink ) );
return $results.$content;
}
return $content;
}
function am_query_tenon( $post, $format = true ) {
// creates the $opts array from the $post data
// only sets items that are non-blank. This allows Tenon to revert to defaults
$expectedPost = array( 'src', 'url', 'level', 'certainty', 'priority',
'docID', 'systemID', 'reportID', 'viewPortHeight', 'viewPortWidth',
'uaString', 'importance', 'ref', 'importance', 'fragment', 'store' );
foreach ( $post AS $k => $v ) {
if ( in_array($k, $expectedPost ) ) {
if ( strlen( trim( $v ) ) > 0 ) {
$opts[$k] = $v;
}
}
}
$settings = get_option( 'am_settings' );
$key = $settings['tenon_api_key'];
if ( $key ) {
$opts['key'] = $key;
$tenon = new tenon( TENON_API_URL, $opts );
$tenon->submit( AM_DEBUG );
$body = $tenon->tenonResponse['body'];
if ( $format == true ) {
$results = am_format_tenon( $body );
} else {
$object = json_decode( $body );
if ( property_exists( $object, 'resultSet' ) ) {
$results = $object->resultSet;
} else {
$results = array();
}
}
return $results;
} else {
return false;
}
}
function am_format_tenon( $body ) {
if ( $body === false ) {
return __( 'No Tenon API Key provided', 'access-monitor' );
}
$object = json_decode( $body );
if ( property_exists( $object, 'resultSummary' ) ) {
// unchecked object references
$errors = $object->resultSummary->issues->totalIssues;
} else {
$errors = 0;
}
if ( property_exists( $object, 'resultSet' ) ) {
$results = $object->resultSet;
} else {
$results = array();
}
$return = am_format_tenon_array( $results, $errors );
return $return;
}
function am_format_tenon_array( $results, $errors ) {
$return = " $result->errorDescription $ref Congratulations! Tenon didn't find any issues on this page.".sprintf( __( '%s accessibility issues identified.', 'access-monitor' ), "$errors" )."
";
$i = 0;
if ( !empty( $results ) ) {
foreach( $results as $result ) {
$i++;
switch( $result->certainty ) {
case ( $result->certainty >= 80 ) : $cert = 'high'; break;
case ( $result->certainty >= 40 ) : $cert = 'medium'; break;
default: $cert = 'low';
}
switch( $result->priority ) {
case ( $result->priority >= 80 ) : $prio = 'high'; break;
case ( $result->priority >= 40 ) : $prio = 'medium'; break;
default: $prio = 'low';
}
// right now, tenon.io is broken over https. So, strip back to non-https.
$href = str_replace( 'https', 'http', $result->ref );
$ref = "" . __( 'Read more:', 'access-monitor' ) . " $result->resultTitle";
$return .= "
$i. $result->errorTitle
Error Source
".$result->errorSnippet."
Xpath:
$result->xpath
Credits Remaining: $credits • Test at WAVE
$report".print_r( $data, 1 )."";
echo '" . __( 'No related reports.', 'access-monitor' ) . "
"; } } function am_add_about_box() { global $post; $urls = $parameters = ''; $pages = get_post_meta( $post->ID, '_tenon_pages', true ); $params = get_post_meta( $post->ID, '_tenon_params', true ); $total = get_post_meta( $post->ID, '_tenon_total', true ); echo "" . sprintf( __( '%s unique errors', 'access-monitor' ), "$total" ) . "
"; foreach ( $pages as $url ) { $page = str_replace( array( 'http://', 'https://', 'http://www.', 'https://www.' ), '', $url ); $urls .= "" . __( 'Tenon Request Parameters', 'access-monitor' ) . "
"; } add_action('admin_menu', 'am_remove_menu_item'); function am_remove_menu_item() { global $submenu; unset( $submenu['edit.php?post_type=tenon-report'][10] ); // Removes 'Add New'. } function am_set_report( $name = false ) { if ( !$name ) { $name = date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) ); } $report_id = wp_insert_post( array( 'post_content'=>'', 'post_title'=>$name, 'post_status'=>'draft', 'post_type'=>'tenon-report' ) ); //$reports = ( get_option( 'am_reports' ) != '' ) ? get_option( 'am_reports' ) : array(); return $report_id; } register_deactivation_hook( __FILE__, 'am_deactivate_cron' ); function am_deactivate_cron() { wp_clear_scheduled_hook( 'amcron' ); } add_action( 'amcron', 'am_schedule_report', 10, 4 ); function am_schedule_report( $report_id, $pages, $name, $params ) { $new_report = am_generate_report( $name, $pages, $report_id, $params ); // 'none' to prevent this from being auto-scheduled again $url = admin_url( "post.php?post=$new_report&action=edit" ); update_post_meta( $new_report, '_tenon_parent', $report_id ); add_post_meta( $report_id, '_tenon_child', $new_report ); wp_mail( apply_filters( 'am_cron_notification_email', get_option( 'admin_email' ), $name ), sprintf( __( 'Scheduled Accessibility Report on %s', 'access-monitor' ), get_option( 'blogname' ) ), sprintf( __( "View accessibility report: %s", 'access-monitor' ), $url ) ); } function am_generate_report( $name, $pages = false, $schedule = 'none', $params = array() ) { $report_id = am_set_report( $name ); if ( is_array( $pages ) ) { $pages = $pages; } else { $pages = array( home_url() ); } if ( $schedule != 'none' ) { if ( ! is_numeric( $schedule ) ) { $timestamp = ( $schedule == 'weekly' ) ? current_time( 'timestamp' ) + 60*60*24*7 : current_time( 'timestamp' ) + ( 60*60*24*30.5 ); $args = array( 'report_id'=>$report_id, 'pages'=>$pages, 'name'=>$name, 'params'=>$params ); wp_schedule_event( $timestamp, $schedule, 'amcron', $args ); update_post_meta( $report_id, '_tenon_schedule', $schedule ); } else { update_post_meta( $report_id, '_tenon_schedule', $schedule ); } } foreach ( $pages as $page ) { if ( is_numeric( $page ) ) { $url = get_permalink( $page ); } else { $url = $page; } if ( esc_url( $url ) ) { $params['url'] = $url; $report = am_query_tenon( $params, false ); $saved[$url] = $report; } else { continue; } } $data = am_format_tenon_report( $saved, $name ); $total = $data['total']; $formatted = $data['html']; remove_action( 'save_post', 'am_run_report' ); wp_update_post( array( 'ID'=>$report_id, 'post_content'=> $formatted ) ); update_post_meta( $report_id, '_tenon_total', $total ); update_post_meta( $report_id, '_tenon_json', $saved ); if ( isset( $params['reportID'] ) && $params['reportID'] != '' ) { update_post_meta( $report_id, '_tenon_reportID', $params['reportID'] ); } if ( isset( $params['systemID'] ) && $params['systemID'] != '' ) { update_post_meta( $report_id, '_tenon_systemID', $params['systemID'] ); } update_post_meta( $report_id, '_tenon_params', $params ); update_post_meta( $report_id, '_tenon_pages', $pages ); wp_publish_post( $report_id ); add_action( 'save_post', 'am_run_report' ); return $report_id; } add_action( 'save_post', 'am_run_report' ); /** * Re-run a test cycle when updating post. */ function am_run_report( $id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || wp_is_post_revision( $id ) || !( get_post_type( $id ) == 'tenon-report' ) ) { return; } $post = get_post( $id ); if ( $post->post_status != 'publish' ) { return; } $name = get_the_title( $id ); $pages = get_post_meta( $id, '_tenon_pages', true ); $params = get_post_meta( $id, '_tenon_params', true ); $params = ( empty( $params ) ) ? array() : $params; if ( empty( $pages ) ) { return; } remove_action( 'save_post', 'am_run_report' ); $report_id = am_generate_report( $name, $pages, 'none', $params ); add_post_meta( $id, '_tenon_child', $report_id ); update_post_meta( $report_id, '_tenon_parent', $id ); add_action( 'save_post', 'am_run_report' ); } function am_show_report( $report_id = false ) { $report_id = ( isset( $_GET['report'] ) && is_numeric( $_GET['report'] ) ) ? $_GET['report'] : false; $output = $name = ''; if ( $report_id ) { $report = get_post( $report_id ); $output = $report->post_content; $name = $report->post_title; } else { $reports = wp_get_recent_posts( array( 'numberposts'=>1, 'post_type'=>'tenon-report', 'post_status'=>'publish' ), 'OBJECT' ); $report = end( $reports ); if ( $report ) { $output = $report->post_content; $name = $report->post_title; } } if ( $output != '' ) { echo $output; } else { $data = am_format_tenon_report( get_post_meta( $report_id, '_tenon_json', true ), $name ); $formatted = $data['html']; $total = $data['total']; wp_update_post( array( 'ID'=>$report_id, 'post_content' => $formatted ) ); update_post_meta( $report_id, '_tenon_total', $total ); echo $formatted; } } function am_column($cols) { $cols['am_total'] = __( 'Errors', 'access-monitor' ); $cols['am_schedule'] = __( 'Schedule', 'access-monitor' ); $cols['am_tested'] = __( 'Level', 'access-monitor' ); return $cols; } // Echo the ID for the new column function am_custom_column( $column_name, $id ) { switch ( $column_name ) { case 'am_total' : $total = get_post_meta( $id, '_tenon_total', true ); echo $total; break; case 'am_tested' : $params = get_post_meta( $id, '_tenon_params', true ); echo $params['level']; break; case 'am_schedule' : $schedule = get_post_meta( $id, '_tenon_schedule', true ); if ( is_numeric( $schedule ) ) { $edit_url = admin_url( "post.php?post=$schedule&action=edit" ); $edit_link = " " . __( 'View Original Test', 'access-monitor' ) . ""; echo $edit_link; } else { if ( $schedule ) { echo ucfirst( $schedule ); } else { _e( 'One-time report', 'access-monitor' ); } } break; } } function am_return_value( $value, $column_name, $id ) { if ( $column_name == 'am_total' || $column_name == 'am_schedule' || $column_name == 'am_level' ) { $value = $id; } return $value; } // Actions/Filters for message tables and css output add_action('admin_init', 'am_add'); function am_add() { add_filter( "manage_tenon-report_posts_columns", 'am_column' ); add_action( "manage_tenon-report_posts_custom_column", 'am_custom_column', 10, 2 ); } function am_format_tenon_report( $results, $name ) { $header = "$result->resultTitle; $result->errorDescription
$result->errorSnippet$result->xpath" . sprintf( __( 'The %d errors found on this page were also found on other pages tested.', 'access-monitor' ), $result_count ) . "
"; } else { $thead = "| ".__( 'Issue', 'access-monitor' )." | ".__( 'Certainty', 'access-monitor' )." | ".__( 'Priority', 'access-monitor' )." | ".__( 'Source', 'access-monitor' )." | ".__( 'Xpath', 'access-monitor' )." |
|---|---|---|---|---|
| ".__( 'Issue', 'access-monitor' )." | ".__( 'Certainty', 'access-monitor' )." | ".__( 'Priority', 'access-monitor' )." | ".__( 'Source', 'access-monitor' )." | ".__( 'Xpath', 'access-monitor' )." |
".sprintf( __( "No errors found on %s.", 'access-monitor' ), "$url" )."
"; } } } else { $return .= "Congratulations! Tenon didn't find any issues on this page.
"; } $header = sprintf( $header, $count ); return array( 'total'=>$total, 'html' => $header . $return ); } add_filter( 'cron_schedules', 'am_custom_schedules' ); function am_custom_schedules( $schedules ) { // Adds once weekly to the existing schedules. $schedules['weekly'] = array( 'interval' => 604800, 'display' => __( 'Once Weekly', 'access-monitor' ) ); $schedules['monthly'] = array( 'interval' => 2635200, 'display' => __( 'Once Monthly', 'access-monitor' ) ); return $schedules; } function am_update_settings() { if ( isset( $_POST['am_settings'] ) ) { $nonce=$_REQUEST['_wpnonce']; if (! wp_verify_nonce($nonce,'access-monitor-nonce') ) die( "Security check failed" ); $tenon_api_key = ( isset( $_POST['tenon_api_key'] ) ) ? $_POST['tenon_api_key'] : ''; $wave_api_key = ( isset( $_POST['wave_api_key'] ) ) ? $_POST['wave_api_key'] : ''; update_option( 'am_settings', array( 'tenon_api_key'=>$tenon_api_key, 'wave_api_key'=>$wave_api_key ) ); echo "" . __( 'Access Monitor Settings Updated', 'access-monitor' ) . "
$message
" . __( 'Sign up with Tenon to get an API key', 'access-monitor' ) . " • " . __( 'Add your API key', 'access-monitor' ) . "
"; } else { $disabled = $message = ''; } echo am_setup_report(); $theme = wp_get_theme(); $theme_name = $theme->Name; $theme_version = $theme->Version; $name = $theme_name . ' ' . $theme_version; echo "$message "; } function am_setup_report() { if ( isset( $_POST['am_generate'] ) ) { $name = ( isset( $_POST['am_report_name'] ) ) ? sanitize_text_field( $_POST['am_report_name'] ) : false; $pages = ( isset( $_POST['am_report_pages'] ) && !empty( $_POST['am_report_pages'] ) ) ? $_POST['am_report_pages'] : false; $schedule = ( isset( $_POST['report_schedule'] ) ) ? $_POST['report_schedule'] : 'none'; $store = ( isset( $_POST['store'] ) ) ? 1 : 0; $reportID = ( isset( $_POST['reportID'] ) ) ? sanitize_text_field( $_POST['reportID'] ) : ''; $systemID = ( isset( $_POST['systemID'] ) ) ? sanitize_text_field( $_POST['systemID'] ) : ''; $viewport = ( isset( $_POST['viewport'] ) ) ? explode( 'x', $_POST['viewport'] ) : array( '1024', '768' ); $viewportHeight = $viewport[1]; $viewportWidth = $viewport[0]; $level = ( isset( $_POST['level'] ) ) ? $_POST['level'] : 'AA'; $priority = ( isset( $_POST['priority'] ) ) ? (int) $_POST['priority'] : 0; $certainty = ( isset( $_POST['certainty'] ) ) ? (int) $_POST['certainty'] : 0; $args = array( 'store' => $store, 'reportID' => $reportID, 'systemID' => $systemID, 'viewPortHeight' => $viewportHeight, 'viewPortWidth' => $viewportWidth, 'level' => $level, 'priority' => $priority, 'certainty' => $certainty ); /* Set up arguments here; pass to am_generate_report importance (not present yet; field specific; associate with URL) */ am_generate_report( $name, $pages, $schedule, $args ); am_show_report(); } } function am_list_reports( $count = 10 ) { $count = (int) $count; $reports = wp_get_recent_posts( array( 'post_type'=>'tenon-report', 'numberposts'=>$count, 'post_status'=>'publish' ), 'OBJECT' ); if ( is_array( $reports ) ) { echo "".__( 'No accessibility reports created yet.', 'access-monitor' )."
"; } } function am_support_page() { ?>