-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathjax-block.php
86 lines (71 loc) · 2.8 KB
/
mathjax-block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* Plugin Name: MathJax Gutenberg Block
* Plugin URI: https://github.com/ClassCube/wordpress-mathjax-block
* Description: Embed static images from MathJax code instead of needing the MathJax JavaScript library loaded client side
* Version: 0.1.0
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: ClassCube
* Author URI: httpsClassCube.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: classcube-mathjax-block
*/
namespace ClassCube\WordPress\MathJax;
Block::init();
class Block {
/**
* Bootstrap that sets up all the other actions for this plugin.
*/
public static function init() {
add_action( 'init', [ self::class, 'register_block' ] );
if ( ! class_exists( 'ClassCube\WordPress\MathJax\Smashing_Updater' ) ) {
include_once( plugin_dir_path( __FILE__ ) . 'smashing_updater.php' );
}
$updater = new \ClassCube\WordPress\MathJax\Smashing_Updater( __FILE__ );
$updater->set_username( 'classcube' );
$updater->set_repository( 'wordpress-mathjax-block' );
$updater->initialize();
}
public static function register_block() {
$script_name = true || defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? 'gutenberg-block.js' : 'gutenberg-block.min.js';
wp_register_script( 'classcube-mathjax', 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js', [], null );
// wp_register_script('classcube-mathjax', 'https://cdn.mathjax.org/mathjax/latest/MathJax.js', [], null);
wp_register_script(
'classcube-mathjax-block',
plugins_url( 'js/dist/' . $script_name, __FILE__ ),
[ 'jquery', 'wp-blocks' ],
self::version()
);
wp_register_style(
'classcube-mathjax-block',
plugins_url( 'css/dist/style.min.css', __FILE__ ),
[],
self::version()
);
register_block_type( 'classcube/mathjax-block', [
'editor_script' => [ 'classcube-mathjax-block', 'classcube-mathjax' ],
'editor_style' => 'classcube-mathjax-block',
'render_callback' => [ self::class, 'render_callback' ]
] );
}
/**
* Gets the version from package.json
*/
public static function version() {
$json = json_decode( file_get_contents( __DIR__ . '/package.json' ), true );
if ( empty( $json ) || empty( $json[ 'version' ] ) ) {
return false;
}
return $json[ 'version' ];
}
public static function render_callback( $props ) {
if ( strlen( $props[ 'imageData' ] ) < 10 ) {
if ( define( 'WP_DEBUG' ) && WP_DEBUG ) {
return '<p>' . __( 'Image data not available for MathJax equation', 'classcube-mathjax-block' ) . '</p>';
}
}
return '<img src="' . $props[ 'imageData' ] . '">';
}
}