<?php /* Plugin Name: Knowledge Space Description: This plugin adds a knowledge space to the website via a shortcode. Version: 1.0 Author: Matthias Konitzny */ $GLOBALS['build'] = 'debug'; 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>"; //wp_enqueue_script('kg-script', $script_path); return $div . $variables . $script; } function ks_add_editor($atts = []) { ks_add_editor_dependencies(); $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>"; require_once(__DIR__ . '/editor/editor.php'); } 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('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); } function kg_load_css() { $plugin_dir = plugin_dir_url(__FILE__); wp_enqueue_style('kg-style', $plugin_dir . 'kg-style.css'); } function escape_space_id($id) { return str_replace( "\\", "-", str_replace( "/", "-", str_replace(" ", "-", $id) ) ); } function get_space_id_from_atts($atts) { if ($atts != "" && array_key_exists("space", $atts)) { return escape_space_id($atts["space"]); } else { return "space"; } } 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');