Created
November 27, 2024 21:07
-
-
Save bhubbard/a6a01f1e3065c5f81fe5c17eba50ff9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WP Figma API | |
* Plugin URI: https://example.com/wp-figma-api | |
* Description: A WordPress plugin that provides an API wrapper for Figma. | |
* 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: wp-figma-api | |
* Domain Path: /languages | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WP_Figma_API' ) ) : | |
/** | |
* Main WP_Figma_API Class. | |
* | |
* @since 1.0.0 | |
*/ | |
final class WP_Figma_API { | |
/** | |
* Version of the plugin. | |
* | |
* @var string | |
*/ | |
public $version = '1.0.0'; | |
/** | |
* Instance of this class. | |
* | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* The Figma API base URL. | |
* | |
* @var string | |
*/ | |
public $api_base_url = 'https://api.figma.com/v1/'; | |
/** | |
* Figma API access token. | |
* | |
* @var string | |
*/ | |
private $access_token; | |
/** | |
* Initialize the plugin public actions. | |
*/ | |
private function __construct() { | |
// Load plugin text domain. | |
add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); | |
// Set the Figma API access token. | |
$this->access_token = 'YOUR_FIGMA_ACCESS_TOKEN'; // Replace with your actual Figma access token | |
// Register any necessary hooks or actions. | |
} | |
/** | |
* Return an instance of this class. | |
* | |
* @return object A single instance of this class. | |
*/ | |
public static function get_instance() { | |
// If the single instance hasn't been set, set it now. | |
if ( null === self::$instance ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
/** | |
* Load the plugin text domain for translation. | |
*/ | |
public function load_plugin_textdomain() { | |
load_plugin_textdomain( 'wp-figma-api', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | |
} | |
/** | |
* Magic getter for our object. Allows getting but not setting. | |
* | |
* @param string $field | |
* @return mixed | |
*/ | |
public function __get( $field ) { | |
return $this->$field; | |
} | |
/** | |
* Make a request to the Figma 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() ) { | |
$url = $this->api_base_url . $endpoint; | |
$args = array( | |
'method' => $method, | |
'headers' => array( | |
'X-Figma-Token' => $this->access_token, | |
), | |
); | |
if ( 'GET' === $method ) { | |
$url = add_query_arg( $params, $url ); | |
} else { | |
$args['body'] = json_encode( $params ); | |
} | |
$response = wp_remote_request( $url, $args ); | |
if ( is_wp_error( $response ) ) { | |
// Handle WP_Error. | |
error_log( 'Figma API Error: ' . $response->get_error_message() ); | |
return $response; | |
} | |
$status_code = wp_remote_retrieve_response_code( $response ); | |
if ( $status_code < 200 || $status_code >= 300 ) { | |
// Handle API errors. | |
$error_message = wp_remote_retrieve_body( $response ); | |
error_log( 'Figma API Error (' . $status_code . '): ' . $error_message ); | |
return new WP_Error( 'figma_api_error', $error_message, array( 'status_code' => $status_code ) ); | |
} | |
return json_decode( wp_remote_retrieve_body( $response ) ); | |
} | |
/** | |
* Get a list of files. | |
* | |
* @param array $params Optional parameters (e.g., page, count). | |
* @return array The list of files. | |
*/ | |
public function get_files( $params = array() ) { | |
return $this->make_request( 'files', 'GET', $params ); | |
} | |
/** | |
* Get a single file by key. | |
* | |
* @param string $file_key The key of the file. | |
* @return array The file data. | |
*/ | |
public function get_file( $file_key ) { | |
return $this->make_request( 'files/' . $file_key ); | |
} | |
/** | |
* Get image fills for a file. | |
* | |
* @param string $file_key The key of the file. | |
* @param array $params Optional parameters (e.g., ids, scale). | |
* @return array The image fills. | |
*/ | |
public function get_image_fills( $file_key, $params = array() ) { | |
return $this->make_request( 'images/' . $file_key, 'GET', $params ); | |
} | |
// Add other helper methods for specific Figma API functionalities, e.g.,: | |
// public function get_comments( $file_key ) { ... } | |
// public function post_comment( $file_key, $message ) { ... } | |
// public function get_team_projects( $team_id ) { ... } | |
// ... and so on | |
} | |
endif; | |
/** | |
* Returns the main instance of WP_Figma_API. | |
* | |
* @return WP_Figma_API The main instance. | |
*/ | |
function WP_Figma_API() { | |
return WP_Figma_API::get_instance(); | |
} | |
// Get WP_Figma_API Running. | |
WP_Figma_API(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment