Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 81 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -820,20 +820,97 @@ function metabox_order( $order ) {
);
}


/**
* Add ACF fields to WP REST API JSON output
* Add custom fields to 'post' API endpoint
*
* @link https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51
*
* @param Object $data The data being added to the post object.
* @param Object $post The post being augmented.
* @param String $context Unused.
*/
function wp_api_encode_acf( $data, $post, $context ) {
function mitlib_api_v1_alter( $data, $post, $context ) {
$customMeta = (array) get_fields( $post['ID'] );

$data['meta'] = array_merge( $data['meta'], $customMeta );
return $data;
}

if ( function_exists( 'get_fields' ) ) {
add_filter( 'json_prepare_post', 'wp_api_encode_acf', 10, 3 );
/**
* Adds custom fields to 'post' and 'experts' API endpoints
*
* @link http://v2.wp-api.org/extending/modifying/
* @link https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51#gistcomment-1622466
*/
function mitlib_api_v2_alter() {
// Add custom fields to posts endpoint.
register_rest_field( 'post',
'meta',
array(
'get_callback' => function( $data, $field, $request, $type ) {
if ( function_exists( 'get_fields' ) ) {
return get_fields( $data['id'] );
}
return array();
},
'update_callback' => null,
'schema' => null,
)
);

// Add custom fields to experts endpoint.
register_rest_field( 'experts',
'meta',
array(
'get_callback' => function( $data, $field, $request, $type ) {
if ( function_exists( 'get_fields' ) ) {
return get_fields( $data['id'] );
}
return array();
},
'update_callback' => null,
'schema' => null,
)
);

// Switch featured_media field from media ID to URL of the image.
register_rest_field( 'experts',
'featured_media',
array(
'get_callback' => 'mitlib_api_get_image',
'update_callback' => null,
'schema' => null,
)
);
}


/**
* This construct will swap between augmentation of the V1 and V2 API.
*/
if ( function_exists( 'register_rest_field' ) ) {
// The register_rest_field function was introduced in the v2 API.
// If that exists, then we call the function to augment that API.
add_action( 'rest_api_init', 'mitlib_api_v2_alter' );
} else {
// If that function does not exist, then we call the old API augmentation.
if ( function_exists( 'get_fields' ) ) {
add_filter( 'json_prepare_post', 'mitlib_api_v1_alter', 10, 3 );
}
}

/**
* Get the value of a specified field for use in the API
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
*
* @return mixed
*/
function mitlib_api_get_image( $object, $field_name ) {
$link = wp_get_attachment_image_src( $object[ $field_name ], 'thumbnail-size' );
return $link[0];
}

// Allows SVGs to be uploaded through media.
Expand Down
257 changes: 145 additions & 112 deletions js/alerts.js
Original file line number Diff line number Diff line change
@@ -1,118 +1,151 @@
// Loads alert-level posts on the top of all pages
$(function mitlib_alerts(){
var closable_alert = false,
local_storage;

// Check for localStorage
if (Modernizr.localstorage) {
local_storage = true;
} else {
local_storage = false;
}

$.ajax({
cache: false,
url: "/wp-json/posts",
dataType: "json"
})
.done(function(json){
var posts = json.length,
post,
alert_title,
post_meta,
is_alert,
confirm,
alert_posts_arr = [],
alert_ID,
alert_content,
alert_template;

for (var i = 0; i < posts; i++) {
// Each post
post = json[i];
// Make sure the field exists
if ($(post.meta).length) {
// Post meta fields
post_meta = post.meta;
// Make sure the field exists
if ($(post_meta.alert).length) {
// Post alert field
is_alert = post_meta.alert;
// If an alert post
if (is_alert === true) {
// Confirm alert field
confirm = post_meta.confirm_alert;
if (confirm === true) {
// Push alert posts to a unique array
alert_posts_arr.push(post);
}
}
}
}
};
// If there is an alert post
if (alert_posts_arr.length) {
// The alert title
alert_title = alert_posts_arr[0].title;
// Check for empty title
if (alert_title === '') {
alert_title = 'Alert!'
}
// Alert post content
alert_content = alert_posts_arr[0].content;
// Alert post ID
alert_ID = alert_posts_arr[0].ID;

// Alert HTML template
alert_template = '<div class="posts--preview--alerts transition-vertical transition-vertical--hide">' +
'<div class="post alert--critical flex-container">' +
'<i class="icon-exclamation-sign" aria-hidden="true"></i>' +
'<div class="content-post alertText">' +
'<h3>' + alert_title + '</h3> ' + alert_content +
'</div>' +
'</div>' +
'</div>';
// Closeable alert
closable_alert = alert_posts_arr[0].meta.closable;
// If localStorage
if (local_storage === true) {
// Check for the localStorage alert ID item
if (localStorage.getItem('alert_closed-' + alert_ID) !== 'true') {
// Append the template
$(alert_template).prependTo('.wrap-page');
$('.gldp-default').animate({"top":"292px"});
// Remove the necessary transition class with a timeout, so that the animation shows.
setTimeout(function() {
$('.posts--preview--alerts').removeClass('transition-vertical--hide');
}, 300);
}
} else { // No localStorage
// Append the template, etc.
$(alert_template).prependTo('.wrap-page');
setTimeout(function() {
$('.posts--preview--alerts').removeClass('transition-vertical--hide');
}, 300);
}
// If this is a closable alert
if (closable_alert === true) {
// Add a Close icon/svg/button
$('.posts--preview--alerts .post').append('<a href="#0" id="close" class="action-close"><i class="icon-remove-sign" aria-hidden="true"></i></a>');
// On click
$('#close').click(function(){
// Add the necessary transition hide class
$('.posts--preview--alerts').addClass('transition-vertical--hide');
$('.gldp-default').css({"top":"105px"});
// If localStorage
if (local_storage === true) {
// Set the localStorage item, using the post ID
localStorage.setItem('alert_closed-' + alert_ID, 'true');
}
});
}
function filterAlerts(posts) {
// This processes an array of posts for valid, confirmed, alerts
var filtered = [],
post,
post_meta,
i;

for (i = 0; i < posts.length; i++) {
// Each post
post = posts[i];
// Make sure the field exists
if ($(post.meta).length) {
// Post meta fields
post_meta = post.meta;
// Make sure the field exists, is an alert, and is confirmed
if ($(post_meta.alert).length && true === post_meta.alert && true === post_meta.confirm_alert ) {
filtered.push(post);
}
}
};

}).fail(function(){
console.log('Alert posts failed to load');
return filtered;
}

function renderAlert(markup,id) {
// If localStorage
if (Modernizr.localstorage) {
// Check for the localStorage alert ID item
if (localStorage.getItem('alert_closed-' + id) !== 'true') {
// Append the template
$(markup).prependTo('.wrap-page');
$('.gldp-default').animate({"top":"292px"});
// Remove the necessary transition class with a timeout, so that the animation shows.
setTimeout(function() {
$('.posts--preview--alerts').removeClass('transition-vertical--hide');
}, 300);
}
} else { // No localStorage
// Append the template, etc.
$(markup).prependTo('.wrap-page');
setTimeout(function() {
$('.posts--preview--alerts').removeClass('transition-vertical--hide');
}, 300);
}
}

function setClosable(alert_ID) {
// Add a Close icon/svg/button
$('.posts--preview--alerts .post').append('<a href="#0" id="close" class="action-close"><i class="icon-remove-sign" aria-hidden="true"></i></a>');
// On click
$('#close').click(function(){
// Add the necessary transition hide class
$('.posts--preview--alerts').addClass('transition-vertical--hide');
$('.gldp-default').css({"top":"105px"});
// If localStorage
if (Modernizr.localstorage) {
// Set the localStorage item, using the post ID
localStorage.setItem('alert_closed-' + alert_ID, 'true');
}
});
}

function showAlertsV1(json) {
var alert_posts_arr = [],
alert_ID,
alert_template;

alert_posts_arr = filterAlerts(json)

// If there is an alert post
if (alert_posts_arr.length) {

// Check for empty title
if ('' === alert_posts_arr[0].title) {
alert_posts_arr[0].title = 'Alert!';
}

// Alert post ID
alert_ID = alert_posts_arr[0].id;

// Alert HTML template
alert_template = '<div class="posts--preview--alerts transition-vertical transition-vertical--hide">' +
'<div class="post alert--critical flex-container">' +
'<i class="icon-exclamation-sign" aria-hidden="true"></i>' +
'<div class="content-post alertText">' +
'<h3>' + alert_posts_arr[0].title + '</h3> ' + alert_posts_arr[0].content +
'</div>' +
'</div>' +
'</div>';

renderAlert(alert_template,alert_ID);

// If this is a closable alert
if (true === alert_posts_arr[0].meta.closable) {
setClosable(alert_ID);
}
}
}

function showAlertsV2(json) {
var alert_posts_arr = [],
alert_ID,
alert_template;

alert_posts_arr = filterAlerts(json)

// If there is an alert post
if (alert_posts_arr.length) {

// Check for empty title
if ('' === alert_posts_arr[0].title.rendered) {
alert_posts_arr[0].title.rendered = 'Alert!';
}

// Alert HTML template
alert_template = '<div class="posts--preview--alerts transition-vertical transition-vertical--hide">' +
'<div class="post alert--critical flex-container">' +
'<i class="icon-exclamation-sign" aria-hidden="true"></i>' +
'<div class="content-post alertText">' +
'<h3>' + alert_posts_arr[0].title.rendered + '</h3> ' + alert_posts_arr[0].content.rendered +
'</div>' +
'</div>' +
'</div>';

// Alert post ID
alert_ID = alert_posts_arr[0].id;

renderAlert(alert_template,alert_ID);

// If this is a closable alert
if (true === alert_posts_arr[0].meta.closable) {
setClosable(alert_ID);
}
}
}

$(function(){
// This is a temporary construct to make transitioning between API endpoints seamless.
// It tries the v1 endpoint first, and if that fails then falls back to the v2 endpoint.
$.getJSON('/wp-json/posts')
.done(function(data){
showAlertsV1(data);
})
.fail(function(){
$.getJSON('/wp-json/wp/v2/posts')
.done(function(data){
showAlertsV2(data);
});
});
});
Loading