Skip to content
Snippets Groups Projects
Select Git revision
  • 114efd931354fb658b0184629954c8cbfa370abd
  • master default protected
  • references-and-visibility
  • merge-graph-data-structures
  • select-box
  • typescript-graph-object-from-editor
  • react-filter-menu
  • react-neighbor-menu-2
  • react-force-graph
  • react-neighbor-menu
  • typescript-graph-object
  • react-typescript-upgrade
  • searchbar
  • mathjax-test
  • build-pipeline-upgrade
  • feature-database
  • revert-7f444182
  • feature-box-select
  • feat-link-width
  • v2.0.0
  • v1.1.2
  • v1.1.1
  • v1.1.0
  • v1.0.4
  • v1.0rc3
  • v1.0rc2
  • v1.0rc1
  • v1.0rc0
28 results

knowledge-space.php

Blame
  • knowledge-space.php 2.28 KiB
    <?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>';
        $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';
                      </script>";
    
        // 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>";
        $script4 = "<script src='$script_path4'></script>";
    
    
        return $three . $renderer .$renderer2 . $graph . $div . $variables . $script0 . $script1 . $script2 .$script3 .$script4;
    }
    
    function ks_add_editor(): string
    {
        // Proper, secure script loading in the future
        // Reference https://stackoverflow.com/a/16823761/7376120
        wp_enqueue_script('jquery');
    
        $plugin_url = plugin_dir_url(__FILE__);
    
        $raw_html = file_get_contents(__DIR__.DIRECTORY_SEPARATOR."editor".DIRECTORY_SEPARATOR."editor.html");
        $ready_html = str_replace("%WWW%", $plugin_url, $raw_html);
    
        return $ready_html;
    }
    
    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');
    add_shortcode('knowledge-space-editor', 'ks_add_editor');