Skip to content

Commit

Permalink
Merge pull request #2 from northeasterncup/dev
Browse files Browse the repository at this point in the history
Finalized new version
  • Loading branch information
beenbiishop authored Oct 11, 2022
2 parents 99d19ba + 2009778 commit 8a1f804
Show file tree
Hide file tree
Showing 8 changed files with 843 additions and 3 deletions.
145 changes: 145 additions & 0 deletions engage-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

/**
* Engage API for Displaying Events
*
* API Documentation: https://engage-api.campuslabs.com/
*/

// Returns a UTC timestamp
function utcTimestamp()
{
$time = new DateTime('now', new DateTimeZone('UTC'));
$timestamp = $time->format('c');
return $timestamp;
}

// Engage Request
// Using the Engage API, make an HTTP request using the provided parameters.
function engage_request($endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Get the Engage API Key
$engage_api_key = get_option('engage_api_key');

// Throw an error if the API Key is unset
if ($engage_api_key == NULL || $engage_api_key == false) {
throw new WP_Error('engage_api_key_unset', 'You must set the Engage API Key under Settings -> Engage/Event Settings to make a request to the Engage API.');
}

// Merge given arguments with default arguments
$allArgs = array_merge(array(
'take' => ENGAGE_PAGE_SIZE,
'skip' => '0'
), $args);

// Merge given headers with default arguments
$allHeaders = array_merge(
array(
'Accept' => 'application/json',
'X-Engage-Api-Key' => $engage_api_key
),
$headers
);

// Build the full endpoint URL
$full_url = ENGAGE_BASE_URL . $endpoint . '?' . http_build_query($allArgs);

// Make the request
$request = wp_remote_request(
$full_url,
array(
'method' => $method,
'httpversion' => '1.1',
'headers' => $allHeaders,
'body' => $body
)
);

// Retrieve the response body
$response_body = wp_remote_retrieve_body($request);

// Return the JSON decoded response body
$decoded_body = json_decode($response_body, true);
return $decoded_body;
}

// Cached Engage Request
// Make a request to the Engage API, or return a previously cached response.
function engage_request_cached($cacheName, $cacheExpires = 60, $endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Get any existing copy of our cached engage request
if (false === ($request = get_transient($cacheName))) {
// If a cached value does not exist, return the value of a new request and save that value as a new transient
$request = engage_request($endpoint, $args, $method, $body, $headers);
set_transient($cacheName, $request, $cacheExpires);
}

// Return the cached or new request value
return $request;
}

// Engage Request Concat
// Concat a paged response from the Engage API into a single array.
function engage_request_concat($endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Initialize variables
$allItems = array();
$saved = 0;

// Submit request to find total number of values
$baseReq = engage_request($endpoint, array_merge(
$args,
array(
'take' => ENGAGE_PAGE_SIZE,
'skip' => strval($saved)
)
), $method, $body, $headers);

// Add the first batch of items
$baseReqItems = $baseReq['items'];
foreach ($baseReqItems as $baseReqItem) {
$allItems[] = $baseReqItem;
$saved++;
}

// Save the total number of items
$totalItems = intval($baseReq['totalItems']);
$remaining = $totalItems - $saved;

// Iterate through additional pages
while ($remaining > 0) {
$request = engage_request($endpoint, array_merge($args, array(
'take' => ENGAGE_PAGE_SIZE,
'skip' => strval($saved)
)), $method, $body, $headers);
$items = $request['items'];
foreach ($items as $item) {
$allItems[] = $item;
$saved++;
}

$remaining = $totalItems - $saved;
}

// Put response into array
$response = array(
'totalItems' => $baseReq['totalItems'],
'items' => $allItems
);
return $response;
}

// Cached Concatenated Engage Request
// Make a request to the Engage API and concatenate paged values, or return a previously cached response.
function engage_request_concat_cached($cacheName, $cacheExpires = 60, $endpoint = '/organizations/organization', $args = array(), $method = 'GET', $body = '', $headers = array())
{
// Get any existing copy of our cached engage request
if (false === ($request = get_transient($cacheName))) {
// If a cached value does not exist, return the value of a new request and save that value as a new transient
$request = engage_request_concat($endpoint, $args, $method, $body, $headers);
set_transient($cacheName, $request, $cacheExpires);
}

// Return the cached or new request value
return $request;
}
204 changes: 204 additions & 0 deletions event-options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

// Hook for adding the engage settings submenu
add_action('admin_menu', 'engage_admin_add_page');

// Action function for above hook
function engage_admin_add_page()
{
// Add a new options page as a submenu of the Settings main menu
add_options_page('Engage/Event Settings', 'Engage/Event Settings', 'manage_options', 'engage', 'engage_options_page');
}

// Displays the page content for the Engage Settings submenu
function engage_options_page()
{
// Check whether the current user can manage options
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}

// Check whether the clear event cache button has been pressed AND also check the nonce
if (isset($_POST['clear_event_cache']) && check_admin_referer('clear_event_cache_clicked')) {
clear_event_cache();
}

echo '<div class="wrap">';
echo '<h1>CampusLabs Engage Settings</h1>';
echo '<p>Events are displayed throughout the site from the CampusLabs Engage Platform, using their ';
echo 'REST API. New events will be automatically pulled every 5 minutes. Configure the integration ';
echo 'settings below if needed, but these should not need to change often.</p>';

// Engage Settings Form
echo '<div class="notice notice-warning is-dismissible"><p>';
echo 'All settings must be set for all shortcodes to properly display.';
echo '</p></div>';
echo '<form method="post" action="options.php">';
settings_fields('engage_settings');
do_settings_sections('engage');
submit_button('Save Changes');
echo '</form>';

// Clear Event Cache Form
echo '<h2>Clear Event Cache</h2>';
echo '<p>Events are automatically pulled every 5 minutes. If you would like to pull new events now, ';
echo 'click the button below.';

echo '<form action="options-general.php?page=engage" method="post">';
wp_nonce_field('clear_event_cache_clicked');
echo '<input type="hidden" value="true" name="clear_event_cache" />';
submit_button('Clear Event Cache');
echo '</form>';

// Available Shortcodes Description
echo '<h2 class="title">Available Shortcodes</h2>';
echo '<p>Currently, three shortcodes are available:';
echo '<ol>';
echo '<li><code>[homepage_events]</code> - Displays the next three upcoming events</li>';
echo '<li><code>[upcoming_events]</code> - Displays all upcoming events</li>';
echo '<li><code>[past_events]</code> - Displays past events that take place after the specified cutoff date</li>';
echo '</ol>';

// Creating an Event Instructions
echo '<h2 class="title">Creating an Event</h2>';
echo '<p>To add new events, you must have an assigned position on CUP\'s roster. New events can be created ';
echo 'by going to <a href="https://neu.campuslabs.com/engage/">Engage</a>, clicking "CUP" under ';
echo '"My Organizations", and clicking "Events" under "Organization Tools". Once you go through the ';
echo 'event creation process, an email will be sent to the CUP advisor to confirm the event details are ';
echo 'valid, and once approved, the event will automatically posted on the CUP website, the Student Hub, ';
echo 'and other various Northeastern platforms.</p>';
echo '</div>';
}

// Hook for registering the engage settings
add_action('admin_init', 'engage_admin_init');

// Action function for the above hook
function engage_admin_init()
{

// Register API Key setting
register_setting('engage_settings', 'engage_api_key', array(
'type' => 'string',
'show_in_rest' => FALSE,
'default' => NULL
));

// Register CUP Organization ID setting
register_setting('engage_settings', 'engage_cup_org_id', array(
'type' => 'string',
'show_in_rest' => FALSE,
'default' => NULL
));

// Register Event Cutoff setting
register_setting('engage_settings', 'engage_event_cutoff', array(
'type' => 'string',
'show_in_rest' => FALSE,
'default' => NULL
));

// Register Engage API settings section
add_settings_section(
'engage_api',
'API Integration Settings',
'engage_api_text',
'engage'
);

// Register Event Display settings section
add_settings_section(
'engage_event_display',
'Event Display Settings',
'engage_event_display_text',
'engage'
);

// Register API Key field
add_settings_field(
'engage_api_key',
'API Key',
'engage_setting_api_key',
'engage',
'engage_api'
);

// Register API Key field
add_settings_field(
'engage_cup_org_id',
'CUP Organization ID',
'engage_setting_cup_org_id',
'engage',
'engage_api'
);

// Register Event Cutoff field
add_settings_field(
'engage_event_cutoff',
'Event Cutoff Date',
'engage_setting_event_cutoff',
'engage',
'engage_event_display'
);
}

// Callback for the API settings section text
function engage_api_text()
{
echo '<p>Settings used to pull events from the Engage API. Documentation from CampusLabs can be found <a href="">here</a>.</p>';
}

// Callback for the Event Display settings section text
function engage_event_display_text()
{
echo '<p>Settings for how events will be displayed throughout the site.</p>';
}

// Callback for the api key settings field
function engage_setting_api_key()
{
$setting = get_option('engage_api_key');
?>
<input type="text" name="engage_api_key" size="60" placeholder="e.g. esk_test_3ef94b252b22047586dc53307a10580e" value="<?php echo isset($setting) ? esc_attr($setting) : ''; ?>">
<p class="description">
The API key received from a CSI admin with at minimum <code>GET</code> access to the <code>/events</code> endpoint. Should not change unless an API key expires.
</p>
<?
}

// Callback for the CUP Organization ID settings field
function engage_setting_cup_org_id()
{
$setting = get_option('engage_cup_org_id');
?>
<input type="text" name="engage_cup_org_id" size="10" placeholder="e.g. 202334" value="<?php echo isset($setting) ? esc_attr($setting) : ''; ?>">
<p class="description">
CUP's Organization ID. Will almost certainly never change. Can be found by doing a <code>GET</code> request to the <code>/organizations/organization</code> endpoint.
</p>
<?
}

// Callback for the event cutoff settings field
function engage_setting_event_cutoff()
{
$setting = get_option('engage_event_cutoff');
?>
<input type="date" name="engage_event_cutoff" max="<?php echo date('Y-m-d'); ?>" value="<?php echo isset($setting) ? esc_attr($setting) : ''; ?>">
<p class="description">
Past events before this date will not be shown in the <code>[past_events]</code> shortcode.
</p>
<?
}

// Clear the event shortcode transients
function clear_event_cache()
{
// Delete the transients
delete_transient('homepage_events');
delete_transient('upcoming_events');
delete_transient('past_events');

// Tell the user that the cache has been cleared
echo '<div id="message" class="updated fade"><p>';
echo 'The event cache has been cleared.' . '</p></div>';
}
11 changes: 11 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

// style and scripts

use function PHPSTORM_META\map;

add_action('wp_enqueue_scripts', 'bootscore_child_enqueue_styles');
function bootscore_child_enqueue_styles()
{
Expand All @@ -15,3 +18,11 @@ function bootscore_child_enqueue_styles()
// custom.js
wp_enqueue_script('custom-js', get_stylesheet_directory_uri() . '/js/custom.js', false, '', true);
}

// Constants
define('ENGAGE_BASE_URL', 'https://engage-api.campuslabs.com/api/v3.0');
define('ENGAGE_PAGE_SIZE', '50');

// Import other functions
get_template_part('event-options', '');
get_template_part('shortcode-functions', '');
2 changes: 1 addition & 1 deletion header.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/img/favicon/favicon.ico">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-config" content="<?php echo get_stylesheet_directory_uri(); ?>/img/favicon/browserconfig.xml">
<meta name="theme-color" content="#090909">
<meta name="theme-color" content="#222222">
<!-- Adobe Fonts -->
<link rel="stylesheet" href="https://use.typekit.net/ckk1ujd.css">
<!-- Bootstrap Icons -->
Expand Down
Loading

0 comments on commit 8a1f804

Please sign in to comment.