Skip to content

Commit 4bf95fa

Browse files
author
Chris Gårdenberg
committed
feat(Course details): Added limited LD-JSON support, to enable better SEO
fixes #293
1 parent 1efde83 commit 4bf95fa

5 files changed

Lines changed: 159 additions & 32 deletions

File tree

PLUGIN-CHECKSUM

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
01c7bb09341dcda909adb5d41ca4ad6c
1+
a5f3e819cdf5d99c5fdb02f405a1ce34

content/template/data/ld-json.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
global $wp_query;
3+
4+
if ( ! empty( $wp_query->query_vars['courseId'] ) ) {
5+
$course_id = $wp_query->query_vars['courseId'];
6+
} elseif ( ! empty( $attributes['courseid'] ) ) {
7+
$course_id = $attributes['courseid'];
8+
} else {
9+
$course_id = null;
10+
}
11+
$group_by_city = EDU()->is_checked( 'eduadmin-groupEventsByCity', false );
12+
$group_by_city_class = '';
13+
14+
$fetch_months = EDU()->get_option( 'eduadmin-monthsToFetch', 6 );
15+
if ( ! is_numeric( $fetch_months ) ) {
16+
$fetch_months = 6;
17+
}
18+
19+
$edo = EDUAPIHelper()->GetCourseDetailInfo( $course_id, $fetch_months, $group_by_city );
20+
21+
$organization = EDUAPIHelper()->GetOrganization();
22+
23+
$selected_course = false;
24+
$name = '';
25+
if ( $edo ) {
26+
$selected_course = json_decode( $edo, true );
27+
$name = ( ! empty( $selected_course['CourseName'] ) ? $selected_course['CourseName'] : $selected_course['InternalCourseName'] );
28+
29+
$is_ondemand = $selected_course['OnDemand'];
30+
31+
if ( $is_ondemand ) {
32+
$selected_course = json_decode( EDUAPIHelper()->GetOnDemandCourseDetailInfo( $course_id, $group_by_city ), true );
33+
}
34+
35+
$description = wp_strip_all_tags( ! empty( $selected_course['CourseDescriptionShort'] ) ? $selected_course['CourseDescriptionShort'] : $selected_course['CourseDescription'] );
36+
37+
if ( strlen( $description ) > 60 ) {
38+
$description = substr( $description, 0, 57 ) . "...";
39+
}
40+
41+
if ( strlen( $description ) == 0 ) {
42+
return;
43+
}
44+
45+
$org_name = trim( ! empty( $organization['LegalName'] ) ? $organization['LegalName'] : $organization['OrganisationName'] );
46+
47+
$events = null;
48+
49+
foreach ( $selected_course['Events'] as $event ) {
50+
$_event = [
51+
'@type' => 'CourseInstance',
52+
'identifier' => (string)$event['EventId']
53+
];
54+
55+
if ( $is_ondemand ) {
56+
$_event['courseMode'] = "online";
57+
$_event['location'] = "Online";
58+
$_event['description'] = 'On-demand';
59+
} else {
60+
$_event['location'] = $event['City'];
61+
$_event['startDate'] = $event['StartDate'];
62+
$_event['endDate'] = $event['EndDate'];
63+
}
64+
65+
if ( ! empty( $event['MaxParticipantNumber'] ) && $event['MaxParticipantNumber'] > 0 ) {
66+
$_event['maximumAttendeeCapacity'] = $event['MaxParticipantNumber'];
67+
$_event['remainingAttendeeCapacity'] = $event['ParticipantNumberLeft'];
68+
}
69+
70+
$events[] = $_event;
71+
}
72+
73+
$ld_object = [
74+
'@context' => 'https://schema.org',
75+
'@type' => 'Course',
76+
'description' => $description,
77+
'name' => $name,
78+
'image' => $selected_course['ImageUrl'],
79+
'coursePrerequisites' => $selected_course['Prerequisites'],
80+
'provider' => [
81+
'@type' => 'Organization',
82+
'name' => $org_name,
83+
'legalName' => trim( $organization['LegalName'] ),
84+
'telephone' => $organization['Phone'],
85+
'sameAs' => $organization['Web'],
86+
'image' => $organization['LogoUrl'],
87+
],
88+
'hasCourseInstance' => $events,
89+
];
90+
91+
echo '<script type="application/ld+json">
92+
' . json_encode( $ld_object, JSON_PRETTY_PRINT ) . '
93+
</script>';
94+
}

includes/edu-options.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,69 @@
2121
add_action( 'add_meta_boxes', 'eduadmin_shortcode_metabox' );
2222
add_action( 'wp_footer', 'eduadmin_custom_styles' );
2323
add_action( 'wp_footer', 'eduadmin_print_javascript' );
24+
add_action( 'wp_head', 'eduadmin_get_ld_json' );
25+
26+
function eduadmin_get_ld_json() {
27+
$t = EDU()->start_timer( __METHOD__ );
28+
global $wp_query;
29+
30+
if ( isset( $wp_query->queried_object ) ) {
31+
if ( stristr( $wp_query->queried_object->post_content, 'eduadmin-detail' ) !== false ) {
32+
include_once EDUADMIN_PLUGIN_PATH . '/content/template/data/ld-json.php';
33+
EDU()->stop_timer( $t );
34+
}
35+
}
36+
37+
EDU()->stop_timer( $t );
38+
}
39+
40+
/*function edu_set_oembed_endpoint_url( $url ) {
41+
$t = EDU()->start_timer( __METHOD__ );
42+
43+
global $wp_query;
44+
$detailpage = get_option( 'eduadmin-detailViewPage' );
45+
EDU()->stop_timer( $t );
46+
if ( isset( $wp_query->queried_object ) && ( isset( $wp_query->query['courseId'] ) || isset( $wp_query->query['edu_programme'] ) ) ) {
47+
$out_url = get_oembed_endpoint_url( get_home_url() . $_SERVER['REQUEST_URI'] );
48+
49+
return '<link rel="alternate" type="application/json+oembed" href="' . $out_url . '" />
50+
<link rel="alternate" type="text/xml+oembed" href="' . $out_url . '&format=xml" />';
51+
} else {
52+
return $url;
53+
}
54+
}
55+
56+
add_filter( 'oembed_discovery_links', 'edu_set_oembed_endpoint_url' );*/
57+
58+
function edu_set_canonical_url( $canonical_url ) {
59+
$t = EDU()->start_timer( __METHOD__ );
60+
61+
global $wp_query;
62+
$detailpage = get_option( 'eduadmin-detailViewPage' );
63+
if ( isset( $wp_query->queried_object ) && ( isset( $wp_query->query['courseId'] ) || isset( $wp_query->query['edu_programme'] ) ) ) {
64+
echo "<link rel=\"canonical\" href=\"" . get_home_url() . $_SERVER['REQUEST_URI'] . "\" />\n";
65+
} else {
66+
echo "<link rel=\"canonical\" href=\"" . $canonical_url . "\" />\n";
67+
}
68+
69+
EDU()->stop_timer( $t );
70+
}
71+
add_filter( 'get_canonical_url', 'edu_set_canonical_url' );
72+
73+
function edu_no_index() {
74+
$t = EDU()->start_timer( __METHOD__ );
75+
global $wp_query;
76+
$detailpage = intval( EDU()->get_option( 'eduadmin-detailViewPage' ) );
77+
if ( isset( $wp_query->queried_object ) ) {
78+
if ( $detailpage === $wp_query->queried_object->ID && ! isset( $wp_query->query['courseId'] ) ) {
79+
echo '<meta name="robots" content="noindex" />';
80+
}
81+
}
82+
EDU()->stop_timer( $t );
83+
}
84+
85+
add_action( 'wp_head', 'edu_no_index' );
86+
add_filter( 'get_shortlink', '__return_empty_string' );
2487

2588
function eduadmin_page_title( $title, $sep = '|' ) {
2689
global $wp;

includes/edu-shortcodes.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,36 +170,6 @@ function eduadmin_get_course_public_pricename( $attributes ) {
170170
return include_once EDUADMIN_PLUGIN_PATH . '/content/template/detailTemplate/course-price-names.php';
171171
}
172172

173-
function edu_no_index() {
174-
$t = EDU()->start_timer( __METHOD__ );
175-
global $wp_query;
176-
$detailpage = get_option( 'eduadmin-detailViewPage' );
177-
if ( isset( $wp_query->queried_object ) ) {
178-
if ( false !== $detailpage && $detailpage === $wp_query->queried_object->ID && ! isset( $wp_query->query['courseId'] ) ) {
179-
echo '<meta name="robots" content="noindex" />';
180-
}
181-
}
182-
EDU()->stop_timer( $t );
183-
}
184-
185-
function edu_set_canonical_url( $canonical_url ) {
186-
$t = EDU()->start_timer( __METHOD__ );
187-
188-
global $wp_query;
189-
$detailpage = get_option( 'eduadmin-detailViewPage' );
190-
if ( isset( $wp_query->queried_object ) && ( isset( $wp_query->query['courseId'] ) || isset( $wp_query->query['edu_programme'] ) ) ) {
191-
echo "<link rel=\"canonical\" href=\"" . get_home_url() . $_SERVER['REQUEST_URI'] . "\" />\n";
192-
} else {
193-
echo "<link rel=\"canonical\" href=\"" . $canonical_url . "\" />\n";
194-
}
195-
196-
EDU()->stop_timer( $t );
197-
}
198-
199-
add_action( 'wp_head', 'edu_no_index' );
200-
add_filter( 'get_canonical_url', 'edu_set_canonical_url' );
201-
add_filter( 'get_shortlink', '__return_empty_string' );
202-
203173
function eduadmin_get_booking_view( $attributes ) {
204174
$t = EDU()->start_timer( __METHOD__ );
205175
if ( ! defined( 'DONOTCACHEPAGE' ) ) {

0 commit comments

Comments
 (0)