From 2d74d66bc0374b2713ba1a239b62f4383d42ba9b Mon Sep 17 00:00:00 2001 From: Julien Liabeuf Date: Wed, 30 Jul 2014 22:34:19 +0700 Subject: [PATCH] First draft of the main class --- includes/openpeek-class.php | 135 ++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 includes/openpeek-class.php diff --git a/includes/openpeek-class.php b/includes/openpeek-class.php new file mode 100644 index 0000000..f15a0e5 --- /dev/null +++ b/includes/openpeek-class.php @@ -0,0 +1,135 @@ +app_id = $app_id; + $this->api_key = $api_key; + $this->route = 'https://www.kimonolabs.com/api/'; + $this->cache = __FILE__ . '/cache/photos_list.json'; + + // CHECK CREDENTIALS + + $this->load_photo_providers(); + + } + + protected function load_photo_providers() { + + $includes = realpath( dirname( __FILE__) ); + $files = scandir( $includes ); + + foreach ( $files as $file ) { + + if ( in_array( $file, array( '.', '..' ) ) ) + continue; + + $ext = pathinfo( $file, PATHINFO_EXTENSION ); + $filename = str_replace( ".$ext", '', $file ); + $breakdown = explode( '-', $filename ); + + if ( in_array( 'class', $breakdown ) ) { + + $key = array_search( 'class', $breakdown ); + unset( $breakdown[$key] ); + + } + + if ( is_array( $breakdown ) && count( $breakdown ) >= 2 && 'openpeek' == $breakdown[0] ) { + + $key = array_search( 'openpeek', $breakdown ); + unset( $breakdown[$key] ); + $breakdown = array_map( 'ucwords', $breakdown ); + + /** + * Get class name. + * + * Get the provider class name based on the filename structure + * defined in the OpenPeeks GihHub repo. + * + * @link https://github.com/ThemeAvenue/OpenPeeks + * @var string Class name + */ + $classname = implode( '_', $breakdown ); + + if( class_exists( $classname ) ) + new $classname(); + + } + + } + + } + + /** + * Get full URL to query. + * + * Get the full query URL including application ID + * and API key. + * + * @since 0.1.0 + * @return string URL to query + */ + protected function get_endpoint_url() { + + $args = http_build_query( array( 'apikey' => $this->api_key ) ); + $base = $this->route . $this->app_id; + $url = "$base?$args"; + + return $url; + } + + public function get_results() { + + $url = $this->get_endpoint_url(); + $response = file_get_contents( $url ); + $body = json_decode( $response, TRUE ); + + /* Check if the content was correctly decoded */ + if ( JSON_ERROR_NONE !== json_last_error() ) { + return false; + } + + return $body; + + } + + protected function get_library() { + + /* Get library from the cache */ + if( file_exists( $this->cache ) ) { + + $contents = file_get_contents( $this->cache ); + $contents = json_decode( $contents ); + + /* Check if the content was correctly decoded */ + if ( JSON_ERROR_NONE !== json_last_error() ) { + return $contents; + } + + } + + /* Otherwise query all APIs */ + + // CACHE RESULTS + + } + + protected function cache_library( $contents ) { + + $file = $this->cache; + $contents = json_encode( $contents ); + + file_put_contents( $file, $contents ); + + } + +} \ No newline at end of file