Skip to content

Commit

Permalink
adds related courses plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kasprzyk-sz committed Aug 11, 2023
1 parent aeba1ca commit 52f592a
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 0 deletions.
1 change: 1 addition & 0 deletions wp-content/plugins/vf-related-courses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Related Courses block
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"key": "group_64d615352f1a3",
"title": "Related Courses",
"fields": [
{
"key": "field_64d61570693c4",
"label": "Related courses",
"name": "vf_related_courses",
"type": "relationship",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"post_type": [
"training"
],
"taxonomy": "",
"filters": [
"search"
],
"elements": "",
"min": "",
"max": "",
"return_format": "object"
}
],
"location": [
[
{
"param": "block",
"operator": "==",
"value": "acf\/vf-related-courses"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "seamless",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": ""
}
112 changes: 112 additions & 0 deletions wp-content/plugins/vf-related-courses/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/*
Plugin Name: VF-WP Related Courses
Description: VF-WP theme global container.
Version: 1.0.0-beta.1
Author: EMBL-EBI Web Development
Plugin URI: https://github.com/visual-framework/vf-wp
Text Domain: vfwp
*/
if ( ! defined( 'ABSPATH' ) ) exit;

if ( ! class_exists('VF_Related_Courses') ) :

class VF_Related_Courses {

/**
* Return the block name
*/
static public function get_name() {
return 'vf-related-courses';
}

/**
* Constructor - add hooks
*/
public function __construct() {
add_action('after_setup_theme',
array($this, 'after_setup_theme')
);
add_filter(
'acf/settings/load_json',
array($this, 'acf_settings_load_json')
);
}

/**
* Return Gutenberg block registration configuration
* https://www.advancedcustomfields.com/resources/acf_register_block_type/
* https://developer.wordpress.org/block-editor/developers/block-api/block-registration/
*/
public function get_config() {
$category = 'vf/wp';
if (class_exists('VF_Blocks')) {
$category = VF_Blocks::block_category();
}
return array(
'name' => $this->get_name(),
'title' => __('Related Courses', 'vfwp'),
'category' => $category,
'supports' => array(
'align' => false,
'customClassName' => false
)
);
}

/**
* Return block render template path
*/
public function get_template() {
// Allow themes to provide a custom template
$template = locate_template(
"blocks/{$this->get_name()}.php",
false, false
);
// Otherwise default to the plugin template
if ( ! file_exists($template)) {
$template = plugin_dir_path(__FILE__) . 'template.php';
}
return $template;
}

/**
* Action: `after_setup_theme`
*/
public function after_setup_theme() {
// Setup render callback using VF Gutenberg plugin or fallback
$callback = function() {
$args = func_get_args();
$template = $this->get_template();
if (class_exists('VF_Gutenberg')) {
VF_Gutenberg::acf_render_template($args, $template);
} else {
$block = $args[0];
include($template);
}
};
// Register the Gutenberg block with ACF
acf_register_block_type(array_merge(
$this->get_config(),
array(
'render_callback' => $callback
)
));
}

/**
* Filter: `acf/settings/load_json`
*/
public function acf_settings_load_json($paths) {
$paths[] = plugin_dir_path(__FILE__);
return $paths;
}

} // VF_Related_Courses

// Initialize one instance
$vf_related_courses = new VF_Related_Courses();

endif;

?>
161 changes: 161 additions & 0 deletions wp-content/plugins/vf-related-courses/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

$relatedCourses = get_field('vf_related_courses');


// Block preview in Gutenberg editor
$is_preview = isset($is_preview) && $is_preview;

// Function to output a banner message in the Gutenberg editor only
$admin_banner = function($message, $modifier = 'info') use ($is_preview) {
if ( ! $is_preview) {
return;
}
?>
<div class="vf-banner vf-banner--alert vf-banner--<?php echo $modifier; ?>">
<div class="vf-banner__content">
<p class="vf-banner__text">
<?php echo $message; ?>
</p>
</div>
</div>
<!--/vf-banner-->
<?php
}; ?>

<?php

// alternative dates

if( $relatedCourses ): ?>
<div class="vf-u-margin__top--400">
<?php foreach( $relatedCourses as $relatedCourse ):
$now = new DateTime();
$current_date = $now->format('Y-m-d');
$organiser = get_the_terms( $relatedCourse->ID , 'training-organiser' );
$location = get_the_terms( $relatedCourse->ID , 'event-location' );
$start_date = get_field('vf-wp-training-start_date',$relatedCourse->ID);
$start_time = get_field('vf-wp-training-start_time',$relatedCourse->ID);
$start = DateTime::createFromFormat('j M Y', $start_date);
$start_time_format = DateTime::createFromFormat('H:i', $start_time);
$end_date = get_field('vf-wp-training-end_date',$relatedCourse->ID);
$end_time = get_field('vf-wp-training-end_time',$relatedCourse->ID);
$end_time_format = DateTime::createFromFormat('H:i', $end_time);
$end = DateTime::createFromFormat('j M Y', $end_date);
$end_date_format = DateTime::createFromFormat('j M Y', $end_date);
$registrationStatus = get_field('vf-wp-training-registration-status',$relatedCourse->ID);
$registrationDeadline = get_field('vf-wp-training-registration-deadline',$relatedCourse->ID);
$deadlineDate = new DateTime($registrationDeadline);
$registrationDeadlineFormatted = $deadlineDate->format('Y-m-d');
$venue = get_field('vf-wp-training-venue',$relatedCourse->ID);
$additionalInfo = get_field('vf-wp-training-info',$relatedCourse->ID);
$relatedCoursePermalink = get_permalink( $relatedCourse->ID );
$relatedCourseTitle = get_the_title($relatedCourse->ID );


if (!empty($start_time)) {
$calendar_start_time = 'T' . $start_time_format->format('Hi') . '00';
}
else {
$calendar_start_time = '';
}

if (!empty($end_time)) {
$calendar_end_time = 'T' . $end_time_format->format('Hi') . '00';
}
elseif (empty($end_time) && !empty($start_time)) {
$calendar_end_time = 'T' . $start_time_format->format('Hi') . '00';
}
else {
$calendar_end_time = '';
}

if (!empty($end_date)) {
$calendar_end_date = '/' . $end->format('Ymd');
}
else {
$calendar_end_date = '/' . $start->format('Ymd');
}
?>
<article class="vf-summary vf-summary--event | vf-u-margin__bottom--400">
<?php if ( ! empty($start_date)) { ?>
<p class="vf-summary__date">
<?php // Event dates
if ($end_date) {
if ($start->format('M') == $end->format('M')) {
echo $start->format('j'); ?> - <?php echo $end->format('j F Y'); }
else {
echo $start->format('j M'); ?> - <?php echo $end->format('j F Y'); }
}
else {
echo $start->format('j F Y');
} ?>
</p>
<?php } ?>

<h3 class="vf-summary__title | vf-u-margin__bottom--100 vf-u-margin__top--200 | search-data">
<a href="<?php echo $relatedCoursePermalink; ?>" class="vf-summary__link"><?php echo $relatedCourseTitle ?></a>
</h3>
<div>
<div class="vf-content | wysiwyg-training-info | search-data">
<?php echo $additionalInfo; ?>
</div>
<p class="vf-summary__meta | vf-u-margin__bottom--600" id="trainingMeta">
<?php if (($organiser)) { ?>
<span class="vf-u-text-color--grey | vf-u-margin__right--600 | organiser | organiser-<?php $org_list = [];
foreach( $organiser as $org ) {
$org_list[] = strtolower(str_replace(' ', '-', $org->name)); }
echo implode(', ', $org_list); ?>">
<?php $org_list = [];
foreach( $organiser as $org ) {
$org_list[] = strtoupper($org->name); }
echo implode(', ', $org_list); ?></span>
<?php } ?>
<?php if (($location)) { ?>
<span>Location:</span>&nbsp;
<span class="vf-u-text-color--grey | vf-u-margin__right--600 | location |
<?php $loc_list = [];
foreach( $location as $loc ) {
$locClass = 'location-' . strtolower($loc->name);;
$loc_list[] = $locClass; }
echo implode(' ', $loc_list); ?>">
<?php // if (!empty($venue)) {
// echo esc_html($venue) . ', '; } ?>
<?php $loc_list = [];
foreach( $location as $loc ) {
$loc_list[] = $loc->name; }
echo implode(', ', $loc_list); ?></span>
<?php } ?>
<span>Registration:</span>&nbsp;
<?php
if (empty($registrationDeadline)) {
if ($registrationStatus == 'Open') {
echo '<span class="vf-u-text-color--green">Open</span>';
}
else if ($registrationStatus == 'Closed') {
echo '<span class="vf-u-text-color--red">Closed</span>';
}
else if ($registrationStatus == 'Waiting list only') {
echo '<span class="vf-u-text-color--orange">Waiting list only</span>';
}
else {
echo '<span class="vf-u-text-color--grey">' . $registrationStatus . '</span>';
}
}
else {
if ($registrationDeadlineFormatted >= $current_date) {
echo '<span class="vf-u-text-color--green">Open</span>';
}
else {
echo '<span class="vf-u-text-color--red">Closed</span>';
}
}
?>
</p>
</div>
</article>

<?php endforeach; ?>
<?php endif; ?>

</div>
1 change: 1 addition & 0 deletions wp-content/themes/vf-wp/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ function allowed_block_types($allowed_blocks) {
'acf/vf-members-internal',
'acf/vf-person',
'acf/vf-person-internal',
'acf/vf-related-courses',
'acf/vf-events-list',
'acf/vf-group-header',
'acf/vf-jobs',
Expand Down

0 comments on commit 52f592a

Please sign in to comment.