|
| 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' ) ); |
0 commit comments