Skip to content

Commit ff1f35c

Browse files
committed
global registration link; acf code cleanup
1 parent 56c48d7 commit ff1f35c

File tree

7 files changed

+177
-112
lines changed

7 files changed

+177
-112
lines changed

acf-json/group_5fbb731271706.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"key": "group_5fbb731271706",
3+
"title": "Registration Settings",
4+
"fields": [
5+
{
6+
"key": "field_5fbb731991708",
7+
"label": "Registration Link",
8+
"name": "registration_link",
9+
"type": "url",
10+
"instructions": "Enter the default registration link. Can be overwritten per-course if necessary.",
11+
"required": 0,
12+
"conditional_logic": 0,
13+
"wrapper": {
14+
"width": "",
15+
"class": "",
16+
"id": ""
17+
},
18+
"default_value": "",
19+
"placeholder": "https:\/\/my-registration-link.com\/register"
20+
}
21+
],
22+
"location": [
23+
[
24+
{
25+
"param": "options_page",
26+
"operator": "==",
27+
"value": "acf-options"
28+
}
29+
]
30+
],
31+
"menu_order": 0,
32+
"position": "normal",
33+
"style": "default",
34+
"label_placement": "top",
35+
"instruction_placement": "label",
36+
"hide_on_screen": "",
37+
"active": true,
38+
"description": "",
39+
"modified": 1606120299
40+
}

functions.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
/* Include local files */
2222
require STYLESHEETPATH . '/inc/certificates-functions.php';
23+
require STYLESHEETPATH . '/inc/acf.php';
2324
require STYLESHEETPATH . '/inc/widgets.php';
2425

2526
/**

inc/acf.php

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Functionality related to Advanced Custom Fields, which is used to
4+
* register metaboxes per template, page, or on a global settings page.
5+
*/
6+
class Certificates_ACF {
7+
public static function init( ) {
8+
// Register global settings page
9+
if( function_exists('acf_add_options_page') ) {
10+
acf_add_options_page();
11+
}
12+
}
13+
14+
/**
15+
* This only runs on the back-end, when editing a page using the page-home.php template.
16+
*
17+
* @todo Maybe add a short transient here? Not sure if necessary since on the back-end only.
18+
* @todo Maybe need to load more than 50 most recent posts? Download multiple pages and build list.
19+
*/
20+
public static function acf_load_blog_posts( $field ) {
21+
// reset choices
22+
$field['choices'] = array();
23+
$blog_posts = load_org_blog_posts();
24+
25+
foreach ( $blog_posts as $post ) {
26+
$field['choices'][ $post->id ] = $post->title->rendered;
27+
}
28+
29+
// return the field
30+
return $field;
31+
}
32+
33+
/**
34+
* Populate the 'Featured FAQs' dropdown on the homepage edit page with the actual FAQs.
35+
*/
36+
public static function acf_load_featured_faq_choices( $field ) {
37+
$field['choices'] = array();
38+
39+
// @todo: FAQ page id is hardcoded here, find a way to make this dynamic
40+
$faq_groups = get_field( 'faq_group', 32 );
41+
42+
foreach ( $faq_groups as $faqGroup ) {
43+
foreach ( $faqGroup['questions'] as $question ) {
44+
$field['choices'][ $question['question'] ] = $question['question'];
45+
}
46+
}
47+
48+
return $field;
49+
}
50+
51+
/**
52+
* Retrieve the list of featured FAQs from a list of FAQ titles.
53+
* Basically, FAQ string titles are being treated as a unique ID for retrieval.
54+
**/
55+
public static function get_faqs_by_titles( $titles = array() ) {
56+
// @todo: FAQ page id is hardcoded here, find a way to make this dynamic
57+
$faq_groups = get_field( 'faq_group', 32 );
58+
$filtered_faqs = array();
59+
60+
foreach ( $faq_groups as $faq_group ) {
61+
foreach ( $faq_group['questions'] as $question ) {
62+
if ( in_array( $question['question'], $titles ) ) {
63+
array_push( $filtered_faqs, $question );
64+
}
65+
}
66+
}
67+
68+
return $filtered_faqs;
69+
}
70+
71+
72+
// @todo: Also store in transient (even if short)
73+
public static function load_home_featured_posts() {
74+
$url = 'https://creativecommons.org/wp-json/wp/v2/posts?per_page=50';
75+
$media_url = 'https://creativecommons.org/wp-json/wp/v2/media';
76+
$author_url = 'https://creativecommons.org/wp-json/wp/v2/users';
77+
78+
$transient_key = 'home_posts' . $url;
79+
80+
foreach ( get_field( 'featured_news', get_option( 'page_on_front' ) ) as $news ) {
81+
if ( $news['post_id'] ) {
82+
$url .= '&include[]=' . $news['post_id'];
83+
}
84+
}
85+
86+
if ( false === ( $results = get_transient( $transient_key ) ) ) {
87+
88+
$posts = query_api( $url );
89+
90+
foreach ( $posts as $post ) {
91+
// Attach featured image info
92+
if ( ! empty( $post->featured_media ) ) {
93+
$api_response = query_api( $media_url . '/' . $post->featured_media );
94+
95+
if ( ! empty( $api_response ) ) {
96+
$post->featured_media_url = $api_response->media_details->sizes->cc_list_post_thumbnail->source_url;
97+
$post->alt_text = $api_response->alt_text;
98+
}
99+
}
100+
101+
// Attach author info
102+
if ( ! empty( $post->author ) ) {
103+
$api_response = query_api( $author_url . '/' . $post->author );
104+
105+
if ( ! empty( $api_response ) ) {
106+
$post->author_name = $api_response->name;
107+
$post->author_url = $api_response->link;
108+
}
109+
}
110+
}
111+
112+
$results = array();
113+
foreach ( $posts as $post ) {
114+
$results[ $post->id ] = $post;
115+
}
116+
117+
set_transient( $transient_key, $results, HOUR_IN_SECONDS ); // @todo Maybe longer?
118+
}
119+
120+
return $results;
121+
}
122+
}
123+
124+
add_action( 'acf/init', array( 'Certificates_ACF', 'init' ) );
125+
add_filter( 'acf/load_field/name=blog_posts', array( 'Certificates_ACF', 'acf_load_blog_posts' ) );
126+
add_filter( 'acf/load_field/name=featured_faqs', array( 'Certificates_ACF', 'acf_load_featured_faq_choices' ) );

inc/certificates-functions.php

+2-107
Original file line numberDiff line numberDiff line change
@@ -70,93 +70,9 @@ public static function add_alumni_login_button( $items, $args ) {
7070
add_shortcode( 'stat', array( 'Certificates_Website', 'register_stat_shortcode' ) );
7171
add_shortcode( 'box', array( 'Certificates_Website', 'register_box_shortcode' ) );
7272

73-
/**
74-
* Populate the 'Featured FAQs' dropdown on the homepage edit page with the actual FAQs.
75-
*/
76-
function acf_load_featured_faq_choices( $field ) {
77-
$field['choices'] = array();
7873

79-
// @todo: FAQ page id is hardcoded here, find a way to make this dynamic
80-
$faq_groups = get_field( 'faq_group', 32 );
81-
82-
foreach ( $faq_groups as $faqGroup ) {
83-
foreach ( $faqGroup['questions'] as $question ) {
84-
$field['choices'][ $question['question'] ] = $question['question'];
85-
}
86-
}
87-
88-
return $field;
89-
};
90-
91-
add_filter( 'acf/load_field/name=featured_faqs', 'acf_load_featured_faq_choices' );
92-
93-
94-
function get_faqs_by_titles( $titles = array(), $faqs = array() ) {
95-
// @todo: FAQ page id is hardcoded here, find a way to make this dynamic
96-
$faq_groups = get_field( 'faq_group', 32 );
97-
$filtered_faqs = array();
98-
99-
foreach ( $faq_groups as $faq_group ) {
100-
foreach ( $faq_group['questions'] as $question ) {
101-
if ( in_array( $question['question'], $titles ) ) {
102-
array_push( $filtered_faqs, $question );
103-
}
104-
}
105-
}
106-
107-
return $filtered_faqs;
108-
}
109-
110-
// @todo: Also store in transient (even if short)
111-
function load_home_featured_posts() {
112-
$url = 'https://creativecommons.org/wp-json/wp/v2/posts?per_page=50';
113-
$media_url = 'https://creativecommons.org/wp-json/wp/v2/media';
114-
$author_url = 'https://creativecommons.org/wp-json/wp/v2/users';
115-
116-
$transient_key = 'home_posts' . $url;
117-
118-
foreach ( get_field( 'featured_news', get_option( 'page_on_front' ) ) as $news ) {
119-
if ( $news['post_id'] ) {
120-
$url .= '&include[]=' . $news['post_id'];
121-
}
122-
}
123-
124-
if ( false === ( $results = get_transient( $transient_key ) ) ) {
125-
126-
$posts = query_api( $url );
127-
128-
foreach ( $posts as $post ) {
129-
// Attach featured image info
130-
if ( ! empty( $post->featured_media ) ) {
131-
$api_response = query_api( $media_url . '/' . $post->featured_media );
132-
133-
if ( ! empty( $api_response ) ) {
134-
$post->featured_media_url = $api_response->media_details->sizes->cc_list_post_thumbnail->source_url;
135-
$post->alt_text = $api_response->alt_text;
136-
}
137-
}
138-
139-
// Attach author info
140-
if ( ! empty( $post->author ) ) {
141-
$api_response = query_api( $author_url . '/' . $post->author );
142-
143-
if ( ! empty( $api_response ) ) {
144-
$post->author_name = $api_response->name;
145-
$post->author_url = $api_response->link;
146-
}
147-
}
148-
}
149-
150-
$results = array();
151-
foreach ( $posts as $post ) {
152-
$results[ $post->id ] = $post;
153-
}
154-
155-
set_transient( $transient_key, $results, HOUR_IN_SECONDS ); // @todo Maybe longer?
156-
}
157-
158-
return $results;
159-
}
74+
// Helper functions
75+
// @todo: Move these somewhere else?
16076
16177
function query_api( $url ) {
16278
$response = wp_remote_get( $url );
@@ -179,24 +95,3 @@ function load_org_blog_posts() {
17995

18096
return $posts;
18197
}
182-
183-
/**
184-
* This only runs on the back-end, when editing a page using the page-home.php template.
185-
*
186-
* @todo Maybe add a short transient here? Not sure if necessary since on the back-end only.
187-
* @todo Maybe need to load more than 50 most recent posts? Download multiple pages and build list.
188-
*/
189-
function acf_load_blog_posts( $field ) {
190-
// reset choices
191-
$field['choices'] = array();
192-
$blog_posts = load_org_blog_posts();
193-
194-
foreach ( $blog_posts as $post ) {
195-
$field['choices'][ $post->id ] = $post->title->rendered;
196-
}
197-
198-
// return the field
199-
return $field;
200-
}
201-
202-
add_filter( 'acf/load_field/name=blog_posts', 'acf_load_blog_posts' );

inc/partials/home-faq.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
$featured_faq_titles = get_field( 'featured_faqs' );
7-
$faqs = get_faqs_by_titles( $featured_faq_titles );
7+
$faqs = Certificates_ACF::get_faqs_by_titles( $featured_faq_titles );
88
?>
99

1010
<div>

page-home.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
*/
1515

1616
get_header();
17-
$featured_posts = load_home_featured_posts();
17+
$featured_posts = Certificates_ACF::load_home_featured_posts();
1818
?>
1919

2020
<main>
2121
<?php
2222
// Hero Image and featured courses
23-
require get_template_part( 'inc/partials/home', 'hero' );
23+
include get_template_part( 'inc/partials/home', 'hero' );
2424
?>
2525

2626
<section class="has-background-grey-lighter padding-top-big padding-bottom-xl">
2727
<div class="container">
2828

2929
<div class="columns is-variable is-6 padding-bottom-large">
3030
<div class="column is-two-thirds">
31-
<?php require get_template_part( 'inc/partials/home', 'faq' ); ?>
31+
<?php include get_template_part( 'inc/partials/home', 'faq' ); ?>
3232
</div>
3333
<?php // Links Section (Hardcoded for now) ?>
3434
<div class="column after-faq">

single-cc_course.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22
get_header();
33
the_post();
4+
5+
// Use the local registration link, OR fallback to the global registration link
6+
$registration_link = get_field('registration_link') ?: get_field('registration_link', 'options');
47
?>
58
<section class="main-content">
69
<header class="single-header single-header--course">
@@ -37,7 +40,7 @@
3740
}
3841
?>
3942

40-
<a href="" class="button register margin-top-bigger">Register Here</a>
43+
<a href="<?php echo $registration_link; ?>" class="button register margin-top-bigger">Register Here</a>
4144
</div>
4245
</div>
4346
<div class="column is-9">

0 commit comments

Comments
 (0)