From 077bf165c7b025d30d2fd1b9aa3a6f3d88a1f3eb Mon Sep 17 00:00:00 2001 From: mntmn Date: Mon, 11 May 2020 18:45:51 +0200 Subject: [PATCH] integrations: first version of Spacedeck integration for WordPress --- .../wordpress/plugins/spacedeck/spacedeck.php | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 integrations/wordpress/plugins/spacedeck/spacedeck.php diff --git a/integrations/wordpress/plugins/spacedeck/spacedeck.php b/integrations/wordpress/plugins/spacedeck/spacedeck.php new file mode 100644 index 0000000..97210aa --- /dev/null +++ b/integrations/wordpress/plugins/spacedeck/spacedeck.php @@ -0,0 +1,202 @@ + 'application/json', + 'X-Spacedeck-API-Token' => $spacedeck_api_key + ); + + $payload = array( + 'method' => $method, + 'timeout' => 10, + 'blocking' => true, + 'headers' => $headers, + 'body' => $data_string + ); + + // echo("

payload:

");
+	// print_r($payload);
+	// echo("
"); + + $result = wp_remote_post($url, $payload); + + if (is_wp_error($result)) { + return $result; + } + + $result = json_decode($result[body], true); + + // echo("

decoded:

");
+	// print_r($result);
+	// echo("
"); + + return $result; +} + +function spacedeck_embed_space($slug, $width = '90%', $height = '800', $parent_space_id = null) { + $spacedeck_frontend_base_uri = get_option("spacedeck_settings")[spacedeck_frontend_base_uri]; + + // try to find the space identified by slug + $space = spacedeck_apicall("GET", "/spaces/" . $slug, array()); + + if (is_wp_error($space)) { + $error = $response->get_error_message(); + return("

Spacedeck: WP Error looking up Space: $error

"); + } else if ($space[error] && $space[error]!="space_not_found") { + return("

Spacedeck: Error looking up Space: $space[error]

"); + } + + // if it doesn't exist, create it: + if ($space[error]=="space_not_found") { + $data = array( + "name" => $slug, + "edit_slug" => $slug + ); + + if ($parent_space_id) { + $data[parent_space_id] = $parent_space_id; + } + + $space = spacedeck_apicall("POST", "/spaces", $data); + + if (is_wp_error($space)) { + $error = $response->get_error_message(); + return("

Spacedeck: WP Error creating Space: $error

"); + } else if ($space[error]) { + return("

Spacedeck: Error creating Space: $space[error]

"); + } + } + + if (is_wp_error($space)) { + $error = $response->get_error_message(); + return("

Spacedeck: WP Error embedding Space: $error

"); + } else if (!$space || $space[error]) { + return("

Spacedeck: Error embedding Space. Is your API key set up correctly?

"); + } + + $space_auth = $space[edit_hash]; + + // return a piece of html (iframe) embedding the space + $uri = $spacedeck_frontend_base_uri . '/spaces/' . $slug . '?embedded=1&spaceAuth=' . $space_auth; + + $html = ""; + + return $html; +} + +function spacedeck_shortcode($attrs) { + extract(shortcode_atts(array( + 'id' => 'none', + 'parent_space_id' => null, + 'width' => '100%', + 'height' => '800' + ), $attrs)); + + $w = $attrs[width]; + $h = $attrs[height]; + if (!$w) $w = '100%'; + if (!$h) $h = 800; + + return spacedeck_embed_space($attrs[id],$w,$h,$attrs[parent_space_id]); +} + +add_shortcode('spacedeck_space', 'spacedeck_shortcode'); + +add_action('admin_menu', 'spacedeck_add_admin_menu'); +add_action('admin_init', 'spacedeck_settings_init'); + +function spacedeck_add_admin_menu() { + add_options_page('spacedeck', 'Spacedeck', 'manage_options', 'spacedeck', 'spacedeck_options_page'); +} + +function spacedeck_settings_init() { + register_setting('pluginPage', 'spacedeck_settings'); + + add_settings_section( + 'spacedeck_pluginPage_section', + 'Spacedeck Settings', + 'spacedeck_settings_section_callback', + 'pluginPage' + ); + + add_settings_field( + 'spacedeck_text_field_0', + 'API key', + 'spacedeck_text_field_0_render', + 'pluginPage', + 'spacedeck_pluginPage_section' + ); + + add_settings_field( + 'spacedeck_text_field_1', + 'API base URL', + 'spacedeck_text_field_1_render', + 'pluginPage', + 'spacedeck_pluginPage_section' + ); + + add_settings_field( + 'spacedeck_text_field_2', + 'Frontend base URL', + 'spacedeck_text_field_2_render', + 'pluginPage', + 'spacedeck_pluginPage_section' + ); +} + +function spacedeck_text_field_0_render() { + $opts = get_option('spacedeck_settings'); + ?> + + + + + + +
+ +
+