file_cache->get( 'docs', DAY_IN_SECONDS ); if ( false === $docs_data ) { $fetched = $this->load_from_server(); if ( ! empty( $fetched ) ) { wpconsent()->file_cache->set( 'docs', $fetched ); $docs_data = $fetched; } else { // Fall back to stale cache on fetch failure so the overlay keeps working. $docs_data = wpconsent()->file_cache->get( 'docs', 0 ); } } $this->categories = isset( $docs_data['categories'] ) ? $docs_data['categories'] : array(); $this->docs = isset( $docs_data['docs'] ) ? $docs_data['docs'] : array(); } /** * Get the docs. * * @return array */ public function get_docs() { if ( ! isset( $this->docs ) ) { $this->load_docs_data(); } return $this->docs; } /** * Get the docs categories. * * @return array */ public function get_categories() { if ( ! isset( $this->categories ) ) { $this->load_docs_data(); } return $this->categories; } /** * Load the docs data from the server. * * @return array */ public function load_from_server() { $request = wp_remote_get( $this->url ); if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) > 299 ) { return array(); } return json_decode( wp_remote_retrieve_body( $request ), true ); } /** * Go through all the docs and retrieve just those for this category. * * @param string $slug The category slug. * * @return array */ public function get_docs_for_category( $slug ) { $docs = $this->get_docs(); ksort( $docs ); $category_docs = array(); foreach ( $docs as $doc ) { if ( ! in_array( $slug, $doc['categories'], true ) ) { continue; } $category_docs[] = $doc; } return $category_docs; } /** * Output the docs categories markup. * * @return void */ public function get_categories_accordion() { ?>