ID && am_in_post_type( $post->ID ) ) {
$control = '';
if ( current_user_can( 'manage_options' ) ) {
$control = "
';
} else {
$settings = get_option( 'am_settings' );
$notify = ( isset( $settings['am_notify'] ) && is_email( $settings['am_notify'] ) );
if ( $notify ) {
$control = ' ' . "";
}
}
echo '
' .
$control . '
';
}
}
add_action( 'admin_enqueue_scripts', 'am_pre_publish' );
/**
* Enqueue JS that executes when 'Publish' pressed and executes the command if Tenon results pass.
*
* @param string $hook current screen.
*/
function am_pre_publish( $hook ) {
global $post;
$options = get_option( 'am_settings' );
$check = isset( $options['tenon_pre_publish'] ) ? $options['tenon_pre_publish'] : 0;
$args = isset( $options['am_criteria'] ) ? $options['am_criteria'] : array();
if ( 1 == $check ) {
if ( 'post-new.php' == $hook || 'post.php' == $hook ) {
if ( am_in_post_type( $post->ID ) ) {
wp_enqueue_script( 'tenon.inspector', plugins_url( 'js/inspector.js', __FILE__ ), array( 'jquery' ), '', true );
$settings = array(
'level' => ( isset( $args['level'] ) ) ? $args['level'] : 'AA',
'certainty' => ( isset( $args['certainty'] ) ) ? $args['certainty'] : '60',
'priority' => ( isset( $args['priority'] ) ) ? $args['priority'] : '20',
'container' => ( isset( $args['container'] ) && ! empty( $args['container'] ) ) ? $args['container'] : '.access-monitor-content',
'store' => ( isset( $args['store'] ) ) ? $args['store'] : '0',
'grade' => ( isset( $args['grade'] ) ) ? $args['grade'] : '90',
'hide' => __( 'Hide issues', 'access-monitor' ),
'show' => __( 'Show issues', 'access-monitor' ),
'failed' => __( 'Could not retrieve content from your content area. Set your content container in Access Monitor settings.', 'access-monitor' ),
'error' => __( 'Post may not be published: does not meet minimum accessibility requirements.', 'access-monitor' ),
'pass' => __( 'Post may be published!: meets your accessibility requirements.', 'access-monitor' ),
'ajaxerror' => __( 'There was an error sending your post to Tenon.io.', 'access-monitor' ),
);
wp_localize_script( 'tenon.inspector', 'am', $settings );
wp_localize_script( 'tenon.inspector', 'am_ajax_notify', 'am_ajax_notify' );
$user_ID = get_current_user_ID();
$post_ID = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : false;
$security = wp_create_nonce( 'am_notify_admin' );
$notify = array(
'user' => $user_ID,
'post_ID' => $post_ID,
'security' => $security,
'error' => __( 'Accessibility Review Request failed to send.', 'access-monitor' ),
);
wp_localize_script( 'tenon.inspector', 'amn', $notify );
}
}
}
}
/**
* Check whether a given post is in an allowed post type and has an update template configured.
*
* @param integer $id Post ID.
*
* @return boolean True if post is allowed, false otherwise.
*/
function am_in_post_type( $id ) {
$settings = get_option( 'am_settings' );
$post_type_settings = isset( $settings['am_post_types'] ) ? $settings['am_post_types'] : array();
if ( is_array( $post_type_settings ) && ! empty( $post_type_settings ) ) {
$type = get_post_type( $id );
if ( in_array( $type, $post_type_settings ) ) {
return true;
}
}
return false;
}
add_action( 'edit_form_top', 'am_edit_form_after_title' );
/**
* Add hidden notice to post edit page.
*
* @param object $post Post object.
*/
function am_edit_form_after_title( $post ) {
if ( am_in_post_type( $post->ID ) ) {
// Translators: Score container, message container, toggle to show results.
echo '' . sprintf( __( 'Score: %1$s / %2$s %3$s', 'access-monitor' ), '', '', 'Show Results' )
. "
";
}
}
/**
* Generate percentage grade for page. For now, use overall error density scoring. Later, add error-specific exits.
*
* @param object $results Tenon results object.
*
* @return mixed float/boolean Percentage or false.
*/
function am_percentage( $results ) {
$status = $results->status;
$results = (array) $results;
if ( 200 == $status ) {
$stats = (array) $results['globalStats'];
$max = $stats['allDensity'] + ( 3 * $stats['stdDev'] );
// test against all errors & warnings.
$score = (array) $results['resultSummary']->density;
$score = $score['allDensity'];
$min = 0;
if ( $score > $max ) {
$return = 0;
} elseif ( $score <= $min ) {
$return = 100;
} else {
$return = 100 - ( ( $score / $max ) * 100 );
}
} else {
$return = false;
}
return apply_filters( 'am_modify_grade', $return, $results );
}
add_action( 'wp_ajax_am_ajax_notify', 'am_ajax_notify' );
/**
* Send notification to a11y approver email that a post needs manual review.
*/
function am_ajax_notify() {
if ( isset( $_REQUEST['security'] ) ) {
if ( ! check_ajax_referer( 'am_notify_admin', 'security', false ) ) {
wp_send_json(
array(
'response' => 0,
'message' => __( 'Invalid Security Check', 'access-monitor' ),
)
);
} else {
$settings = get_option( 'am_settings' );
$notify = ( isset( $settings['am_notify'] ) && is_email( $settings['am_notify'] ) );
if ( $notify ) {
$email = $settings['am_notify'];
$post_ID = $_REQUEST['post_ID'];
$user_ID = $_REQUEST['user'];
$post = get_post( $post_ID );
$user = get_user_by( 'id', $user_ID );
$edit_link = get_edit_post_link( $post_ID );
// Translators: Post title, requesting user, edit link.
$body = sprintf( __( 'Accessibility review on "%1$s" requested by %2$s. Review post: %3$s', 'access-monitor' ), $post->post_title, $user->user_login, $edit_link );
$notice = wp_mail( $email, __( 'Request for Accessibility Review', 'access-monitor' ), $body );
wp_send_json(
array(
'response' => 1,
'message' => __( 'Review request sent!', 'access-monitor' ),
)
);
} else {
wp_send_json(
array(
'response' => 0,
'message' => __( 'Invalid Email provided for accessibility reviewer', 'access-monitor' ),
)
);
}
}
}
}
add_filter( 'the_content', 'am_testable_post_content' );
/**
* If custom value is not set for Access Monitor content container, add independent container and test that.
*
* @param string $content Post content to test.
*
* @return content
*/
function am_testable_post_content( $content ) {
global $post;
if ( is_object( $post ) ) {
if ( am_in_post_type( $post->ID ) ) {
$options = get_option( 'am_settings' );
$args = isset( $options['am_criteria'] ) ? $options['am_criteria'] : array();
$test = ( isset( $options['tenon_pre_publish'] ) && 1 == $options['tenon_pre_publish'] ) ? true : false;
$container = isset( $args['container'] ) ? $args['container'] : false;
if ( $test ) {
if ( ! $container || '.access-monitor-content' == $container ) {
$content = '' . $content . '
';
}
}
}
}
return $content;
}