Skip to content
Snippets Groups Projects
knowledge-space.php 4.39 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /*
    Plugin Name: Knowledge Space
    Description: This plugin adds a knowledge space to the website via a shortcode.
    Version: 1.0
    Author: Matthias Konitzny
    */
    
    function ks_add_graph(): string
    {
        $graph = '<script src="//unpkg.com/3d-force-graph"></script>';
    
    Matthias Konitzny's avatar
    Matthias Konitzny committed
        $three = '<script src="//unpkg.com/three@0.130.0"></script>';
    
        $renderer = '<script src="//unpkg.com/three@0.130.0/examples/js/renderers/CSS2DRenderer.js"></script>';
        $renderer2 = '<script src="//unpkg.com/three@0.130.0/examples/js/renderers/CSS3DRenderer.js"></script>';
    
        $div = '<div id="3d-graph"></div>';
        $plugin_dir = plugin_dir_url(__FILE__);
    
        //$dataset = $plugin_dir.'datasets/miserables.json';
    
        $variables = "<script> 
    
                      var plugin_path = '$plugin_dir';
    
    Matthias Konitzny's avatar
    Matthias Konitzny committed
        // Yeah this is pretty ugly - need packaging asap.
    
        $script_path0 = $plugin_dir.'config.js';
        $script_path1 = $plugin_dir.'display'.DIRECTORY_SEPARATOR.'helpers.js';
        $script_path2 = $plugin_dir.'display'.DIRECTORY_SEPARATOR.'infooverlay.js';
        $script_path3 = $plugin_dir.'display'.DIRECTORY_SEPARATOR.'linkoverlay.js';
        $script_path4 = $plugin_dir.'display'.DIRECTORY_SEPARATOR.'graph.js';
    
        $script0 = "<script src='$script_path0'></script>";
    
        $script1 = "<script src='$script_path1'></script>";
        $script2 = "<script src='$script_path2'></script>";
        $script3 = "<script src='$script_path3'></script>";
    
    Matthias Konitzny's avatar
    Matthias Konitzny committed
        $script4 = "<script src='$script_path4'></script>";
    
        
        
        return $three . $renderer . $renderer2 . $graph . $div . $variables . $script0 . $script1 . $script2 . $script3 . $script4;
    }
    
    function ks_add_editor()
    {
        ks_add_editor_dependencies();
        require_once(__DIR__.'/editor/editor.php');
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    {
    
        wp_enqueue_script('jquery');    // Should be wp_enqueue_script('jquery'), but doesn't work
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    
        ks_enqueue_script('dataset.js');
        ks_enqueue_script('editor/js/manageddata.js');
        ks_enqueue_script('editor/js/graph.js', ["manageddata"]);
        ks_enqueue_script('editor/js/tools/tool.js');
        ks_enqueue_script('editor/js/tools/undotool.js', ["tool"]);
        ks_enqueue_script('editor/js/tools/redotool.js', ["tool"]);
        ks_enqueue_script('editor/js/tools/selecttool.js', ["tool"]);
        ks_enqueue_script('editor/js/tools/collecttool.js', ["tool"]);
        ks_enqueue_script('editor/js/tools/deletetool.js', ["tool"]);
        ks_enqueue_script('editor/js/tools/addnodetool.js', ["tool"]);
        ks_enqueue_script('editor/js/tools/connecttool.js', ["tool"]);
        ks_enqueue_script('editor/js/display.js', []);
        ks_enqueue_script('editor/js/state.js', ["tool", "display"]);
        ks_enqueue_script('editor/js/editor.js', ["state", "graph", "dataset"]);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    
    function ks_enqueue_script($relative_path, $dependencies = array())
    {
        $prefix = "ks";
        $type = "script";
        $script_name = end(explode("/", $relative_path));
        $script_name = explode(".", $script_name)[0];
        
        for ($i = 0; $i < sizeof($dependencies); $i++) {
            $dependencies[$i] = $prefix . "-" . $dependencies[$i] . "-" . $type;
        }
        
        // Source: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ a comment from Andrija Naglic
        // $file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . $relative_path));
        
        wp_enqueue_script($prefix .  "-" . $script_name . "-" . $type, plugins_url($relative_path, __FILE__), $dependencies, false);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    }
    
    
    function ks_enqueue_style($relative_path, $dependencies = array())
    {
        $prefix = "ks";
        $type = "style";
        $style_name = end(explode("/", $relative_path));
        $style_name = explode(".", $style_name)[0];
        
        for ($i = 0; $i < sizeof($dependencies); $i++) {
            $dependencies[$i] = $prefix . "-" . $dependencies[$i] . "-" . $type;
        }
        
        // Source: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ a comment from Andrija Naglic
        $file_version = date("ymd-Gis", filemtime(plugin_dir_path(__FILE__) . $relative_path));
        
        wp_enqueue_style($prefix . "-" . $style_name . "-" . $type, plugins_url($relative_path, __FILE__), $dependencies, $file_version);
    }
    
    function kg_load_css()
    {
    
        $plugin_dir = plugin_dir_url(__FILE__);
    
        wp_enqueue_style('kg-style', $plugin_dir . 'kg-style.css');
    
    }
    
    add_action('wp_enqueue_scripts', 'kg_load_css');
    
    add_shortcode('knowledge-space', 'ks_add_graph');
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    add_shortcode('knowledge-space-editor', 'ks_add_editor');