Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Last active November 27, 2024 20:58
Show Gist options
  • Save bhubbard/c3bb6cdf84f5ad9896859ada1f8f40fc to your computer and use it in GitHub Desktop.
Save bhubbard/c3bb6cdf84f5ad9896859ada1f8f40fc to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: WP Box API
* Plugin URI: https://example.com/wp-box-api
* Description: A WordPress plugin that provides an API wrapper for Box.com. https://developer.box.com/reference/
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: 1 wp-box-api
* Domain Path: /languages
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Box_API' ) ) :
/**
* Main WP_Box_API Class.
*
* @since 1.0.0
*/
final class WP_Box_API {
// ... (previous code remains the same) ...
/**
* Make a request to the Box API.
*
* @param string $endpoint The API endpoint.
* @param string $method The HTTP method (GET, POST, PUT, DELETE).
* @param array $params The request parameters.
* @return array The API response.
*/
public function make_request( $endpoint, $method = 'GET', $params = array() ) {
try {
switch ( $method ) {
case 'GET':
$response = $this->client->get( $endpoint, $params );
break;
case 'POST':
$response = $this->client->post( $endpoint, $params );
break;
case 'PUT':
$response = $this->client->put( $endpoint, $params );
break;
case 'DELETE':
$response = $this->client->delete( $endpoint, $params );
break;
default:
throw new Exception( 'Invalid HTTP method.' );
}
// Handle API errors.
if ( isset( $response->error ) ) {
throw new Exception( $response->error_description );
}
return json_decode( wp_remote_retrieve_body( $response ) );
} catch ( Exception $e ) {
// Handle exceptions (e.g., log errors, return error message).
error_log( 'Box API Error: ' . $e->getMessage() );
return new WP_Error( 'box_api_error', $e->getMessage() );
}
}
/**
* Get folder contents.
*
* @param string $folder_id The ID of the folder.
* @return array The folder contents.
*/
public function get_folder_contents( $folder_id ) {
return $this->make_request( '/folders/' . $folder_id . '/items' );
}
/**
* Upload a file.
*
* @param string $file_path The path to the file.
* @param string $folder_id The ID of the folder to upload to.
* @return array The uploaded file information.
*/
public function upload_file( $file_path, $folder_id ) {
$params = array(
'file' => fopen( $file_path, 'r' ),
'parent' => array( 'id' => $folder_id ),
);
return $this->make_request( '/files/content', 'POST', $params );
}
/**
* Create a folder.
*
* @param string $folder_name The name of the folder.
* @param string $parent_folder_id The ID of the parent folder.
* @return array The created folder information.
*/
public function create_folder( $folder_name, $parent_folder_id ) {
$params = array(
'name' => $folder_name,
'parent' => array( 'id' => $parent_folder_id ),
);
return $this->make_request( '/folders', 'POST', $params );
}
}
endif;
/**
* Returns the main instance of WP_Box_API.
*
* @return WP_Box_API The main instance.
*/
function WP_Box_API() {
return WP_Box_API::get_instance();
}
// Get WP_Box_API Running.
WP_Box_API();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment