file_cache->get( 'docs', DAY_IN_SECONDS ); if ( false === $docs_data ) { $docs_data = $this->load_from_server(); wpcode()->file_cache->set( 'docs', $docs_data ); } $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 ( 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(); // Until we drop PHP 5.2 support to use closure. 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() { ?>