Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Knowledge Space WP Plugin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
alg
Knowledge Space WP Plugin
Commits
fd2881ba
Commit
fd2881ba
authored
3 years ago
by
Maximilian Giller
Browse files
Options
Downloads
Patches
Plain Diff
Implemented proper authorized save option for editor
parent
ec69ea86
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
datasets/datasets.js
+23
-21
23 additions, 21 deletions
datasets/datasets.js
datasets/datasets.php
+28
-20
28 additions, 20 deletions
datasets/datasets.php
editor/js/graph.js
+0
-26
0 additions, 26 deletions
editor/js/graph.js
knowledge-space.php
+2
-21
2 additions, 21 deletions
knowledge-space.php
with
53 additions
and
88 deletions
datasets/datasets.js
+
23
−
21
View file @
fd2881ba
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
:
space
Id
,
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
,
});
}
This diff is collapsed.
Click to expand it.
datasets/datasets.php
+
28
−
20
View file @
fd2881ba
<?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
();
This diff is collapsed.
Click to expand it.
editor/js/graph.js
+
0
−
26
View file @
fd2881ba
import
ManagedData
from
"
./manageddata
"
;
import
{
PLUGIN_PATH
,
COLOR_PALETTE
}
from
"
../../config
"
;
import
jQuery
from
"
jquery
"
;
const
LINK_NAME_CONNECTOR
=
"
→
"
;
...
...
@@ -46,31 +45,6 @@ export class Graph extends ManagedData {
this
.
calculateLinkTypes
();
this
.
onChangeCallbacks
=
[];
let
payload
=
{
action
:
"
update_space
"
,
graph
:
JSON
.
stringify
(
data
),
space
:
space_id
,
};
jQuery
.
ajax
({
type
:
"
POST
"
,
url
:
ajax_object
.
ajax_url
,
// The key needs to match your method's input parameter (case-sensitive).
data
:
payload
,
// contentType: "application/json; charset=utf-8",
// contentType: false,
// processData: false,
// dataType: "json",
success
:
function
(
data
)
{
console
.
log
(
data
);
alert
(
"
Success
"
);
},
error
:
function
(
errMsg
)
{
console
.
log
(
errMsg
);
alert
(
"
Failure
"
);
},
});
}
triggerOnChange
()
{
...
...
This diff is collapsed.
Click to expand it.
knowledge-space.php
+
2
−
21
View file @
fd2881ba
...
...
@@ -84,27 +84,8 @@ function get_space_id_from_atts($atts) {
}
}
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"
))
{
$plugin_dir
=
plugin_dir_path
(
__FILE__
);
// Use json encoding.
$payload
=
json_encode
(
$_POST
[
"graph"
]);
$filename
=
$plugin_dir
.
$_POST
[
"space"
]
.
".json"
;
$result
=
file_put_contents
(
$filename
,
$payload
);
//echo print_r($_POST);
echo
"Saved file at "
;
echo
$filename
;
echo
$result
;
wp_die
();
}
else
{
echo
"Insufficient permissions!"
;
}
}
require_once
(
__DIR__
.
'/datasets/datasets.php'
);
add_action
(
'wp_enqueue_scripts'
,
'kg_load_css'
);
add_shortcode
(
'knowledge-space'
,
'ks_add_graph'
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment