diff --git a/config.js b/config.js index e1c17d8234348b6cd6f6144ab94e9fc188f7271c..2e077a94ba26edfc6753676e5b88fad0930b9df8 100644 --- a/config.js +++ b/config.js @@ -9,4 +9,5 @@ export const COLOR_PALETTE = [ ]; // Just renaming a variable which is given by the PHP script. This avoids errors in all other files. -export const PLUGIN_PATH = plugin_path; +export const PLUGIN_PATH = plugin.path; +export const SPACE = space.id; diff --git a/datasets/datasets.js b/datasets/datasets.js index b2bbde0e2e098e5ca17331884626aed6efa73e66..3b14ee37adab75faa5acafefc5d8ac3f3bb3d4fa 100644 --- a/datasets/datasets.js +++ b/datasets/datasets.js @@ -1,17 +1,26 @@ import { PLUGIN_PATH } from "../config"; - -export const DATASETS_URL = PLUGIN_PATH + "datasets/datasets.php"; +import jQuery from "jquery"; /** * Returns the json object from the stored graph as promise. * * @param {String} spaceId Identification of graph to load. - * + * * @returns Promise returning graph object */ export function loadGraphJson(spaceId) { - return fetch(DATASETS_URL + "?space_id=" + spaceId) - .then((r) => r.json()); + let payload = { + action: "get_space", + space: spaceId, + }; + + return jQuery + .ajax({ + type: "POST", + url: ajax_object.ajax_url, + data: payload, + }) + .then((data) => JSON.parse(data)); } /** @@ -21,23 +30,16 @@ export function loadGraphJson(spaceId) { * @param {object} json Graph object */ export function saveGraphJson(spaceId, json) { - var payload = { - space_id: spaceId, + let payload = { + action: "update_space", graph: JSON.stringify(json), + space: spaceId, }; - var auth = getAuthPayload(); - if (auth === undefined) { - return undefined; - } - - return fetch(DATASETS_URL, { - method: "POST", - body: JSON.stringify(Object.assign(payload, auth)), - }); -} - -function getAuthPayload() { - //! TODO: Implement auth - return {}; + return jQuery + .ajax({ + type: "POST", + url: ajax_object.ajax_url, + data: payload, + }); } diff --git a/datasets/datasets.php b/datasets/datasets.php index cc93dc309c76165f19093ba5b2e24276199e883b..d147b0d52547804e3fa73f72e8bf86e337efa0be 100644 --- a/datasets/datasets.php +++ b/datasets/datasets.php @@ -1,34 +1,42 @@ <?php -function handle_request() -{ - if ($_SERVER["REQUEST_METHOD"] == "GET") { - handle_get($_GET); - } else if ($_SERVER["REQUEST_METHOD"] == "POST") { - handle_post(get_post_data()); - } -} +add_action("wp_ajax_get_space", "get_space"); // Fires only for logged-in-users +add_action("wp_ajax_nopriv_get_space", 'get_space' ); // Fires for everyone +function get_space() { + $file_path = get_space_file_path($_POST["space"]); + $content = file_get_contents($file_path); + echo $content; -function get_post_data() -{ - return json_decode(file_get_contents('php://input'), true); + wp_die(); } -function handle_get($request_data) { - $file_path = get_space_file_path($request_data["space_id"]); +add_action("wp_ajax_update_space", "update_space"); // Fires only for logged-in-users +//add_action("wp_ajax_nopriv_update_space", 'update_space' ); // Fires for everyone +function update_space() { + // Check user capabilities + if (current_user_can("edit_posts")) { + // Use json encoding. + $graph = stripslashes($_POST["graph"]); - $content = file_get_contents($file_path); - echo $content; + store_new_graph($graph, $_POST["space"]); + + wp_die(); + } else { + echo "Insufficient permissions!"; + } } -function handle_post($request_data) { - $file_path = get_space_file_path($request_data["space_id"]); +function store_new_graph($graph, $space_id) { + $file_path = get_space_file_path($space_id); + $result = file_put_contents($file_path, $graph); - file_put_contents($file_path, $request_data["graph"]); + //echo print_r($_POST); + echo "Saved file at "; + echo $file_path; + echo " "; + echo $result; } function get_space_file_path($space_id) { return __DIR__."/".$space_id.".json"; } - -handle_request(); diff --git a/display/graph.js b/display/graph.js index cd791e51d9dc3059e85082bc349acb3719296808..e1c22e063000bf6ff0f382cc5c5c362d1acb07d2 100644 --- a/display/graph.js +++ b/display/graph.js @@ -525,7 +525,7 @@ let infoOverlay = null; // Only execute, if corresponding dom is present if (document.getElementById("3d-graph") !== null) { - G = new Graph(space_id); // space_id defined globaly through knowledge-space.php + G = new Graph(Config.SPACE); // space_id defined globaly through knowledge-space.php filterOverlay = new FilterOverlay(G, "node"); infoOverlay = new NodeInfoOverlay(G); G.infoOverlay = infoOverlay; diff --git a/editor/js/editor.js b/editor/js/editor.js index 74e3e3d7effac9fd399bba82b24360ebf6732c55..b0ecfed15f2b2172feb8d41c622e551bbfd0b375 100644 --- a/editor/js/editor.js +++ b/editor/js/editor.js @@ -3,6 +3,7 @@ import * as Graph from "./graph"; import { loadGraphJson } from "../../datasets/datasets"; import ForceGraph from "force-graph"; import * as Interactions from "./interactions"; +import { SPACE } from "../../config"; export var state; @@ -20,7 +21,7 @@ window.onload = function () { Interactions.initInteractions(); - loadGraphJson(space_id) // space_id defined globaly through knowledge-space.php + loadGraphJson(SPACE) // space_id defined globaly through knowledge-space.php .then((graphConfig) => { state = new State(); graph = new Graph.Graph(graphConfig); diff --git a/knowledge-space.php b/knowledge-space.php index cdf7f309cbaed6eaec5cc17ca5f3a7c44deeed0d..53dc5191b94463c035e5f64f9096673368776271 100644 --- a/knowledge-space.php +++ b/knowledge-space.php @@ -14,17 +14,21 @@ function ks_add_graph($atts = []): string $space_id = get_space_id_from_atts($atts); $div = '<div id="3d-graph"></div>'; // The id "3d-graph" indicates, that the javascript associated with this should automatically be executed $plugin_dir = plugin_dir_url(__FILE__); - //$dataset = $plugin_dir.'datasets/miserables.json'; - $variables = "<script> - var plugin_path = '$plugin_dir'; - var space_id = '$space_id'; - </script>"; - - $script_path = $plugin_dir . 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; - $script = "<script src='$script_path'></script>"; + + $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; + // $script = "<script src='$script_path'></script>"; //wp_enqueue_script('kg-script', $script_path); + wp_enqueue_script("ks-display-js", plugins_url($script_path, __FILE__), array('jquery'), false); + wp_localize_script( + 'ks-display-js', + 'ajax_object', + array('ajax_url' => admin_url('admin-ajax.php')) + ); + wp_localize_script("ks-display-js", "space", array('id' => $space_id)); + wp_localize_script("ks-display-js", "plugin", array('path' => $plugin_dir)); - return $div . $variables . $script; + return $div . $variables; + // return $div . $variables . $script; } function ks_add_editor($atts = []) @@ -33,10 +37,8 @@ function ks_add_editor($atts = []) $space_id = get_space_id_from_atts($atts); $plugin_dir = plugin_dir_url(__FILE__); - echo "<script> - var plugin_path = '$plugin_dir'; - var space_id = '$space_id'; - </script>"; + wp_localize_script("ks-editor-js", "space", array('id' => $space_id)); + wp_localize_script("ks-editor-js", "plugin", array('path' => $plugin_dir)); require_once(__DIR__ . '/editor/editor.php'); } @@ -45,8 +47,16 @@ function ks_add_editor_dependencies() { $script_path = 'build' . DIRECTORY_SEPARATOR . $GLOBALS['build'] . DIRECTORY_SEPARATOR . 'graph.js'; - wp_enqueue_script('jquery'); - wp_enqueue_script("ks-editor-js", plugins_url($script_path, __FILE__), array(), false); + // wp_enqueue_script('jquery'); + wp_enqueue_script("ks-editor-js", plugins_url($script_path, __FILE__), array('jquery'), false); + //wp_register_script("ks-editor-js", plugins_url($script_path, __FILE__), array('jquery'), false); + + wp_localize_script( + 'ks-editor-js', + 'ajax_object', + array('ajax_url' => admin_url('admin-ajax.php')) + ); + //wp_enqueue_script("ks-editor-js"); $style_file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . "editor/css/editor.css")); wp_enqueue_style("ks-editor-css", plugins_url("editor/css/editor.css", __FILE__), array(), $style_file_version); @@ -71,7 +81,8 @@ function escape_space_id($id) ); } -function get_space_id_from_atts($atts) { +function get_space_id_from_atts($atts) +{ if ($atts != "" && array_key_exists("space", $atts)) { return escape_space_id($atts["space"]); } else { @@ -79,6 +90,9 @@ function get_space_id_from_atts($atts) { } } + +require_once(__DIR__ . '/datasets/datasets.php'); + add_action('wp_enqueue_scripts', 'kg_load_css'); add_shortcode('knowledge-space', 'ks_add_graph'); add_shortcode('knowledge-space-editor', 'ks_add_editor');