From 8e0a7b0095466d9cd51ac9599363e7b4aebc6139 Mon Sep 17 00:00:00 2001 From: Matthias Konitzny <konitzny@ibr.cs.tu-bs.de> Date: Wed, 6 Oct 2021 15:02:09 +0200 Subject: [PATCH] Working ajax demo. --- editor/js/graph.js | 13 ++++++++----- knowledge-space.php | 27 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/editor/js/graph.js b/editor/js/graph.js index fe60920..ef62c30 100644 --- a/editor/js/graph.js +++ b/editor/js/graph.js @@ -47,15 +47,18 @@ export class Graph extends ManagedData { this.calculateLinkTypes(); this.onChangeCallbacks = []; + let payload = { + action: "update_space", + name: "space", + payload: data, + }; + jQuery.ajax({ type: "POST", url: ajax_object.ajax_url, // The key needs to match your method's input parameter (case-sensitive). - data: { - action: "update_space", - whatever: data, - }, - //contentType: "application/json; charset=utf-8", + data: payload, + // contentType: "application/json; charset=utf-8", // contentType: false, // processData: false, // dataType: "json", diff --git a/knowledge-space.php b/knowledge-space.php index b9363db..38c6b27 100644 --- a/knowledge-space.php +++ b/knowledge-space.php @@ -84,16 +84,25 @@ function get_space_id_from_atts($atts) { } } -add_action("wp_ajax_update_space", "update_space"); -add_action("wp_ajax_nopriv_update_space", 'update_space' ); +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() { - global $wpdb; - - //$arr_img_ext = array('image/png', 'image/jpeg', 'image/jpg', 'image/gif'); - //$upload = wp_upload_bits($_FILES['file']['name'], null, file_get_contents($_FILES['file']['tmp_name'])); - //echo "Hello"; - echo $_POST["whatever"]; - wp_die(); + // Check user capabilities + if (current_user_can("edit_posts")) { + $plugin_dir = plugin_dir_path(__FILE__); + // Use json encoding. + $filename = $plugin_dir . $_POST["name"] . ".json"; + $result = file_put_contents($filename, json_encode($_POST["payload"])); + + //echo print_r($_POST); + echo "Saved file at "; + echo $filename; + echo $result; + + wp_die(); + } else { + echo "Insufficient permissions!"; + } } add_action('wp_enqueue_scripts', 'kg_load_css'); -- GitLab