Skip to content
Snippets Groups Projects
Commit 998a54df authored by lucha's avatar lucha
Browse files

[auto] plugin: mathjax-latex 1.3.10

parent 89ddc9b5
No related branches found
No related tags found
No related merge requests found
<?php
/*
* The contents of this file are subject to the GPL License, Version 3.0.
*
* Copyright (C) 2013, Phillip Lord, Newcastle University
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class MathJax_Latex_Admin {
public static $admin_tags = [
'input' => [
'type' => [],
'name' => [],
'id' => [],
'disabled' => [],
'value' => [],
'checked' => [],
],
'select' => [
'name' => [],
'id' => [],
],
'option' => [
'value' => [],
'selected' => [],
],
];
public function __construct() {
add_action( 'admin_menu', [ $this, 'admin_page_init' ] );
}
public function admin_page_init() {
add_options_page( 'MathJax-LaTeX', 'MathJax-LaTeX', 'manage_options', 'kblog-mathjax-latex', [ $this, 'plugin_options_menu' ] );
}
public function plugin_options_menu() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); //xss ok
}
$this->table_head();
// save options if this is a valid post
if ( isset( $_POST['kblog_mathjax_latex_save_field'] ) && // input var okay
wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['kblog_mathjax_latex_save_field'] ) ), 'kblog_mathjax_latex_save_action' ) // input var okay
) {
echo "<div class='updated settings-error' id='etting-error-settings_updated'><p><strong>Settings saved.</strong></p></div>\n";
$this->admin_save();
}
$checked_force_load = '';
if ( get_option( 'kblog_mathjax_force_load' ) ) {
$checked_force_load = 'checked="true"';
}
$this->admin_table_row(
'Force Load',
'Force the MathJax JavaScript to be loaded on every post. This removes the need to use the [mathjax] shortcode.',
"<input type='checkbox' name='kblog_mathjax_force_load' id='kblog_mathjax_force_load' value='1' $checked_force_load />",
''
);
$selected_inline = get_option( 'kblog_mathjax_latex_inline' ) === 'inline' ? 'selected="true"' : '';
$selected_display = get_option( 'kblog_mathjax_latex_inline' ) === 'display' ? 'selected="true"' : '';
$syntax_input = <<<EOT
<select name="kblog_mathjax_latex_inline" id="kblog_mathjax_latex_inline">
<option value="inline" $selected_inline>Inline</option>
<option value="display" $selected_display>Display</option>
</select>
EOT;
$this->admin_table_row(
'Default [latex] syntax attribute.',
"By default, the [latex] shortcode renders equations using the MathJax 'inline' syntax.",
$syntax_input,
'kblog_mathjax_latex_inline'
);
$wp_latex_disabled = method_exists( 'WP_LaTeX', 'init' ) ? "disabled='disable'" : '';
$wp_latex_disabled_warning = method_exists( 'WP_LaTeX', 'init' ) ? 'Disable wp-latex to use this syntax.' : '';
$use_wp_latex_syntax = get_option( 'kblog_mathjax_use_wplatex_syntax', false ) ? "checked='true'" : '';
$this->admin_table_row(
'Use wp-latex syntax?',
"Allows use of the \$latex$ syntax, but conflicts with wp-latex. $wp_latex_disabled_warning",
"<input type='checkbox' name='kblog_mathjax_use_wplatex_syntax' id='kblog_mathjax_use_wplatex_syntax' $wp_latex_disabled $use_wp_latex_syntax value='1'/>",
'kblog_mathjax_use_wplatex_syntax'
);
$use_cdn = get_option( 'kblog_mathjax_use_cdn', true ) ? 'checked="true"' : '';
$this->admin_table_row(
'Use MathJax CDN Service?',
'Allows use of the MathJax hosted content delivery network. By using this, you are agreeing to the <a href="http://www.mathjax.org/download/mathjax-cdn-terms-of-service/">MathJax CDN Terms of Service</a>.',
"<input type='checkbox' name='kblog_mathjax_use_cdn' id='use_cdn' value='1' $use_cdn/>",
'use_cdn'
);
$custom_location_disabled = get_option( 'kblog_mathjax_use_cdn', true ) ? 'disabled="disabled"' : '';
$custom_location = "value='" . esc_attr( get_option( 'kblog_mathjax_custom_location', '' ) ) . "'";
$this->admin_table_row(
'Custom MathJax location?',
'If you are not using the MathJax CDN enter the location of your MathJax script.',
"<input type='textbox' name='kblog_mathjax_custom_location' id='kblog_mathjax_custom_location' $custom_location $custom_location_disabled>",
'kblog_mathjax_custom_location'
);
$options = $this->config_options();
$select_string = "<select name='kblog_mathjax_config' id='kblog_mathjax_config'>\n";
foreach ( $options as $i ) {
$selected = get_option( 'kblog_mathjax_config', 'default' ) === $i ? "selected='true'" : '';
$select_string .= "<option value='$i' " . esc_attr( $selected ) . ">$i</option>\n";
}
$select_string .= '</select>';
$this->admin_table_row(
'MathJax Configuration',
"See the <a href='http://docs.mathjax.org/en/v1.1-latest/configuration.html#loading'>MathJax documentation</a> for more details.",
$select_string,
'kblog_mathjax_config'
);
$this->table_foot();
}
public function config_options() {
$options = [
'default',
'Accessible',
'TeX-AMS_HTML',
'TeX-AMS-MML_HTMLorMML',
];
return $options;
}
public function admin_save() {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
check_ajax_referer( 'kblog_mathjax_latex_save_field', 'security' );
}
update_option( 'kblog_mathjax_force_load', array_key_exists( 'kblog_mathjax_force_load', $_POST ) ); // input var okay
if ( array_key_exists( 'kblog_mathjax_latex_inline', $_POST ) && isset( $_POST['kblog_mathjax_latex_inline'] ) && // input var okay
in_array( sanitize_text_field( wp_unslash( $_POST['kblog_mathjax_latex_inline'] ) ), [ 'inline', 'display' ], true ) // input var okay
) {
update_option( 'kblog_mathjax_latex_inline', sanitize_text_field( wp_unslash( $_POST['kblog_mathjax_latex_inline'] ) ) ); // input var okay
}
update_option( 'kblog_mathjax_use_wplatex_syntax', array_key_exists( 'kblog_mathjax_use_wplatex_syntax', $_POST ) ); // input var okay
update_option( 'kblog_mathjax_use_cdn', array_key_exists( 'kblog_mathjax_use_cdn', $_POST ) ); // input var okay
if ( array_key_exists( 'kblog_mathjax_custom_location', $_POST ) && isset( $_POST['kblog_mathjax_custom_location'] ) ) { // input var okay
update_option( 'kblog_mathjax_custom_location', esc_url_raw( wp_unslash( $_POST['kblog_mathjax_custom_location'] ) ) ); // input var okay
}
if ( array_key_exists( 'kblog_mathjax_config', $_POST ) && isset( $_POST['kblog_mathjax_config'] ) && // input var okay
in_array( sanitize_text_field( wp_unslash( $_POST['kblog_mathjax_config'] ) ), $this->config_options(), true ) // input var okay
) {
update_option( 'kblog_mathjax_config', sanitize_text_field( wp_unslash( $_POST['kblog_mathjax_config'] ) ) ); // input var okay
}
}
public function table_head() {
?>
<div class='wrap' id='mathjax-latex-options'>
<h2>Mathjax-Latex by Kblog</h2>
<form id='mathjaxlatex' name='mathjaxlatex' action='' method='POST'>
<?php wp_nonce_field( 'kblog_mathjax_latex_save_action', 'kblog_mathjax_latex_save_field', true ); ?>
<table class='form-table'>
<caption class='screen-reader-text'>The following lists configuration options for the MathJax-LaTeX plugin.</caption>
<?php
}
public function table_foot() {
?>
</table>
<p class="submit"><input type="submit" class="button button-primary" value="Save Changes"/></p>
</form>
</div>
<script type="text/javascript">
jQuery(function($) {
if (typeof($.fn.prop) !== 'function') {
return; // ignore this for sites with jquery < 1.6
}
// enable or disable the cdn input field when checking/unchuecking the "use cdn" checkbox
var cdn_check = $('#use_cdn'),
cdn_location = $('#kblog_mathjax_custom_location');
cdn_check.change(function() {
var checked = cdn_check.is(':checked');
cdn_location.prop('disabled', checked);
});
});
</script>
<?php
}
public function admin_table_row( $head, $comment, $input, $input_id ) {
?>
<tr valign="top">
<th scope="row">
<label for="<?php echo esc_attr( $input_id ); ?>"><?php echo esc_html( $head ); ?></label>
</th>
<td>
<?php echo wp_kses( $input, self::$admin_tags ); ?>
<p class="description"><?php echo wp_kses_post( $comment ); ?></p>
</td>
</tr>
<?php
}
} // class
function mathjax_latex_admin_init() {
global $mathjax_latex_admin;
$mathjax_latex_admin = new MathJax_Latex_Admin();
}
if ( is_admin() ) {
mathjax_latex_admin_init();
}
<?php
/*
* The contents of this file are subject to the LGPL License, Version 3.0.
*
* Copyright (C) 2010-2013, Phillip Lord, Newcastle University
* Copyright (C) 2010-2011, Simon Cockell, Newcastle University
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
define( 'MATHJAX_VERSION', '1.3.10' );
require_once __DIR__ . '/class-mathjax-latex-admin.php';
class MathJax_Latex {
public static $add_script;
public static $block_script;
public static $mathml_tags = [
'math' => [ 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor', 'display', 'overflow', 'xmlns' ],
'maction' => [ 'actiontype', 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'selection' ],
'maligngroup' => [],
'malignmark' => [],
'menclose' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'notation' ],
'merror' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ],
'mfenced' => [ 'class', 'id', 'style', 'close', 'href', 'mathbackground', 'mathcolor', 'open', 'separators' ],
'mfrac' => [ 'bevelled', 'class', 'id', 'style', 'denomalign', 'href', 'linethickness', 'mathbackground', 'mathcolor', 'numalign' ],
'mglyph' => [ 'alt', 'class', 'id', 'style', 'height', 'href', 'mathbackground', 'src', 'valign', 'width' ],
'mi' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant' ],
'mlabeledtr' => [ 'class', 'id', 'style', 'columnalign', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'rowalign' ],
'mlongdiv' => [],
'mmultiscripts' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'subscriptshift', 'superscriptshift' ],
'mn' => [ 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant' ],
'mo' => [ 'accent', 'class', 'id', 'style', 'dir', 'fence', 'form', 'href', 'largeop', 'lspace', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'maxsize', 'minsize', 'moveablelimits', 'rspace', 'separator', 'stretchy', 'symmetric' ],
'mover' => [ 'accent', 'align', 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ],
'mpadded' => [ 'class', 'id', 'style', 'depth', 'height', 'href', 'lspace', 'mathbackground', 'mathcolor', 'voffset', 'width' ],
'mphantom' => [ 'class', 'id', 'style', 'mathbackground' ],
'mroot' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ],
'mrow' => [ 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor' ],
'ms' => [ 'class', 'id', 'style', 'dir', 'lquote', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'rquote' ],
'mscarries' => [],
'mscarry' => [],
'msgroup' => [],
'msline' => [],
'mspace' => [ 'class', 'id', 'style', 'depth', 'height', 'linebreak', 'mathbackground', 'width' ],
'msqrt' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ],
'msrow' => [],
'mstack' => [],
'mstyle' => [ 'dir', 'decimalpoint', 'displaystyle', 'infixlinebreakstyle', 'scriptlevel', 'scriptminsize', 'scriptsizemultiplier' ],
'msub' => [ 'class', 'id', 'style', 'mathbackground', 'mathcolor', 'subscriptshift' ],
'msubsup' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'subscriptshift', 'superscriptshift' ],
'msup' => [ 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'superscriptshift' ],
'mtable' => [ 'class', 'id', 'style', 'align', 'alignmentscope', 'columnalign', 'columnlines', 'columnspacing', 'columnwidth', 'displaystyle', 'equalcolumns', 'equalrows', 'frame', 'framespacing', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'minlabelspacing', 'rowalign', 'rowlines', 'rowspacing', 'side', 'width' ],
'mtd' => [ 'class', 'id', 'style', 'columnalign', 'columnspan', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'rowalign', 'rowspan' ],
'mtext' => [ 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant' ],
'mtr' => [ 'class', 'id', 'style', 'columnalign', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'rowalign' ],
'munder' => [ 'accentunder', 'align', 'class', 'id', 'style', 'mathbackground', 'mathcolor' ],
'munderover' => [ 'accent', 'accentunder', 'align', 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ],
'semantics' => [ 'definitionURL', 'encoding', 'cd', 'name', 'src' ],
'annotation' => [ 'definitionURL', 'encoding', 'cd', 'name', 'src' ],
'annotation-xml' => [ 'definitionURL', 'encoding', 'cd', 'name', 'src' ],
];
public static function init() {
register_activation_hook( __FILE__, [ __CLASS__, 'mathjax_install' ] );
register_deactivation_hook( __FILE__, [ __CLASS__, 'mathjax_uninstall' ] );
if ( get_option( 'kblog_mathjax_force_load' ) ) {
self::$add_script = true;
}
add_shortcode( 'mathjax', [ __CLASS__, 'mathjax_shortcode' ] );
add_shortcode( 'nomathjax', [ __CLASS__, 'nomathjax_shortcode' ] );
add_shortcode( 'latex', [ __CLASS__, 'latex_shortcode' ] );
add_action( 'wp_footer', [ __CLASS__, 'add_script' ] );
add_filter( 'script_loader_tag', [ __CLASS__, 'script_loader_tag' ], 10, 3 );
if ( get_option( 'kblog_mathjax_use_wplatex_syntax' ) ) {
add_filter( 'the_content', [ __CLASS__, 'inline_to_shortcode' ] );
}
add_filter( 'plugin_action_links', [ __CLASS__, 'mathjax_settings_link' ], 9, 2 );
add_filter( 'the_content', [ __CLASS__, 'filter_br_tags_on_math' ] );
add_action( 'init', [ __CLASS__, 'allow_mathml_tags' ] );
add_filter( 'tiny_mce_before_init', [ __CLASS__, 'allow_mathml_tags_in_tinymce' ] );
}
// registers default options
public static function mathjax_install() {
add_option( 'kblog_mathjax_force_load', false );
add_option( 'kblog_mathjax_latex_inline', 'inline' );
add_option( 'kblog_mathjax_use_wplatex_syntax', false );
add_option( 'kblog_mathjax_use_cdn', true );
add_option( 'kblog_mathjax_custom_location', false );
add_option( 'kblog_mathjax_config', 'default' );
}
public static function mathjax_uninstall() {
delete_option( 'kblog_mathjax_force_load' );
delete_option( 'kblog_mathjax_latex_inline' );
delete_option( 'kblog_mathjax_use_wplatex_syntax' );
delete_option( 'kblog_mathjax_use_cdn' );
delete_option( 'kblog_mathjax_custom_location' );
delete_option( 'kblog_mathjax_config' );
}
public static function mathjax_shortcode( $atts, $content ) {
self::$add_script = true;
}
public static function nomathjax_shortcode( $atts, $content ) {
self::$block_script = true;
}
public static function latex_shortcode( $atts, $content ) {
self::$add_script = true;
// this gives us an optional "syntax" attribute, which defaults to "inline", but can also be "display"
$shortcode_atts = shortcode_atts(
[
'syntax' => get_option( 'kblog_mathjax_latex_inline' ),
], $atts
);
if ( 'inline' === $shortcode_atts['syntax'] ) {
return '\(' . $content . '\)';
} elseif ( 'display' === $shortcode_atts['syntax'] ) {
return '\[' . $content . '\]';
}
}
public static function add_script() {
if ( ! self::$add_script ) {
return;
}
if ( self::$block_script ) {
return;
}
// initialise option for existing MathJax-LaTeX users
if ( get_option( 'kblog_mathjax_use_cdn' ) || ! get_option( 'kblog_mathjax_custom_location' ) ) {
$mathjax_location = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js';
} else {
$mathjax_location = get_option( 'kblog_mathjax_custom_location' );
}
$mathjax_url = $mathjax_location . '?config=' . get_option( 'kblog_mathjax_config' );
wp_enqueue_script( 'mathjax', $mathjax_url, false, MATHJAX_VERSION, false );
$mathjax_config = apply_filters( 'mathjax_config', [] );
if ( $mathjax_config ) {
wp_add_inline_script( 'mathjax', 'MathJax.Hub.Config(' . wp_json_encode( $mathjax_config ) . ');' );
}
}
/**
* Set the script tag to have type text/x-mathjax-config
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*
* @return string $tag
*/
public static function script_loader_tag( $tag, $handle = null, $src = null ) {
if ( 'mathjax' === $handle ) {
// replace the <script> tag for the inline script, but not for the <script> tag with src=""
return str_replace( "<script type='text/javascript'>", "<script type='text/x-mathjax-config'>", $tag );
}
return $tag;
}
public static function inline_to_shortcode( $content ) {
if ( false === strpos( $content, '$latex' ) ) {
return $content;
}
self::$add_script = true;
return preg_replace_callback( '#\$latex[= ](.*?[^\\\\])\$#', [ __CLASS__, 'inline_to_shortcode_callback' ], $content );
}
public static function inline_to_shortcode_callback( $matches ) {
//
// Also support wp-latex syntax. This includes the ability to set background and foreground
// colour, which we can ignore.
//
if ( preg_match( '/.+((?:&#038;|&amp;)s=(-?[0-4])).*/i', $matches[1], $s_matches ) ) {
$matches[1] = str_replace( $s_matches[1], '', $matches[1] );
}
if ( preg_match( '/.+((?:&#038;|&amp;)fg=([0-9a-f]{6})).*/i', $matches[1], $fg_matches ) ) {
$matches[1] = str_replace( $fg_matches[1], '', $matches[1] );
}
if ( preg_match( '/.+((?:&#038;|&amp;)bg=([0-9a-f]{6})).*/i', $matches[1], $bg_matches ) ) {
$matches[1] = str_replace( $bg_matches[1], '', $matches[1] );
}
return "[latex]{$matches[1]}[/latex]";
}
// add a link to settings on the plugin management page
public static function mathjax_settings_link( $links, $file ) {
if ( 'mathjax-latex/mathjax-latex.php' === $file && function_exists( 'admin_url' ) ) {
$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=kblog-mathjax-latex' ) ) . '">' . esc_html__( 'Settings' ) . '</a>';
array_unshift( $links, $settings_link );
}
return $links;
}
/**
* Removes the <br /> tags inside math tags
*
* @param $content
* @return string without <br /> tags
*/
public static function filter_br_tags_on_math( $content ) {
$filtered_content = preg_replace_callback(
'/(<math.*>.*<\/math>)/isU',
function( $matches ) {
return str_replace( array( '<br/>', '<br />', '<br>' ), '', $matches[0] );
},
$content
);
return null === $filtered_content ? $content : $filtered_content;
}
/**
* Allow MathML tags within WordPress
* http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/
* https://developer.mozilla.org/en-US/docs/Web/MathML/Element
*/
public static function allow_mathml_tags() {
global $allowedposttags;
foreach ( self::$mathml_tags as $tag => $attributes ) {
$allowedposttags[ $tag ] = [];
foreach ( $attributes as $a ) {
$allowedposttags[ $tag ][ $a ] = true;
}
}
}
/**
* Ensure that the MathML tags will not be removed
* by the TinyMCE editor
*/
public static function allow_mathml_tags_in_tinymce( $options ) {
$extended_tags = [];
foreach ( self::$mathml_tags as $tag => $attributes ) {
if ( ! empty( $attributes ) ) {
$tag = $tag . '[' . implode( '|', $attributes ) . ']';
}
$extended_tags[] = $tag;
}
if ( ! isset( $options['extended_valid_elements'] ) ) {
$options['extended_valid_elements'] = '';
}
$options['extended_valid_elements'] .= ',' . implode( ',', $extended_tags );
$options['extended_valid_elements'] = trim( $options['extended_valid_elements'], ',' );
return $options;
}
}
MathJax_Latex::init();
......@@ -2,7 +2,7 @@
/**
* Plugin Name: MathJax-LaTeX
* Description: Transform latex equations in JavaScript using mathjax
* Version: 1.3.8
* Version: 1.3.10
* Author: Phillip Lord, Simon Cockell, Paul Schreiber
* Author URI: http://knowledgeblog.org
*
......@@ -12,292 +12,4 @@
* Paul Schreiber (paulschreiber@gmail.com)
*/
/*
* The contents of this file are subject to the LGPL License, Version 3.0.
*
* Copyright (C) 2010-2013, Phillip Lord, Newcastle University
* Copyright (C) 2010-2011, Simon Cockell, Newcastle University
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
define( 'MATHJAX_VERSION', '1.3.8' );
require_once dirname( __FILE__ ) . '/mathjax-latex-admin.php';
class MathJax {
public static $add_script;
public static $block_script;
public static $mathml_tags = array(
'math' => array( 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor', 'display', 'overflow', 'xmlns' ),
'maction' => array( 'actiontype', 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'selection' ),
'maligngroup' => array(),
'malignmark' => array(),
'menclose' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'notation' ),
'merror' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ),
'mfenced' => array( 'class', 'id', 'style', 'close', 'href', 'mathbackground', 'mathcolor', 'open', 'separators' ),
'mfrac' => array( 'bevelled', 'class', 'id', 'style', 'denomalign', 'href', 'linethickness', 'mathbackground', 'mathcolor', 'numalign' ),
'mglyph' => array( 'alt', 'class', 'id', 'style', 'height', 'href', 'mathbackground', 'src', 'valign', 'width' ),
'mi' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant' ),
'mlabeledtr' => array( 'class', 'id', 'style', 'columnalign', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'rowalign' ),
'mlongdiv' => array(),
'mmultiscripts' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'subscriptshift', 'superscriptshift' ),
'mn' => array( 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant' ),
'mo' => array( 'accent', 'class', 'id', 'style', 'dir', 'fence', 'form', 'href', 'largeop', 'lspace', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'maxsize', 'minsize', 'moveablelimits', 'rspace', 'separator', 'stretchy', 'symmetric' ),
'mover' => array( 'accent', 'align', 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ),
'mpadded' => array( 'class', 'id', 'style', 'depth', 'height', 'href', 'lspace', 'mathbackground', 'mathcolor', 'voffset', 'width' ),
'mphantom' => array( 'class', 'id', 'style', 'mathbackground' ),
'mroot' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ),
'mrow' => array( 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor' ),
'ms' => array( 'class', 'id', 'style', 'dir', 'lquote', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant', 'rquote' ),
'mscarries' => array(),
'mscarry' => array(),
'msgroup' => array(),
'msline' => array(),
'mspace' => array( 'class', 'id', 'style', 'depth', 'height', 'linebreak', 'mathbackground', 'width' ),
'msqrt' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ),
'msrow' => array(),
'mstack' => array(),
'mstyle' => array( 'dir', 'decimalpoint', 'displaystyle', 'infixlinebreakstyle', 'scriptlevel', 'scriptminsize', 'scriptsizemultiplier' ),
'msub' => array( 'class', 'id', 'style', 'mathbackground', 'mathcolor', 'subscriptshift' ),
'msubsup' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'subscriptshift', 'superscriptshift' ),
'msup' => array( 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor', 'superscriptshift' ),
'mtable' => array( 'class', 'id', 'style', 'align', 'alignmentscope', 'columnalign', 'columnlines', 'columnspacing', 'columnwidth', 'displaystyle', 'equalcolumns', 'equalrows', 'frame', 'framespacing', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'minlabelspacing', 'rowalign', 'rowlines', 'rowspacing', 'side', 'width' ),
'mtd' => array( 'class', 'id', 'style', 'columnalign', 'columnspan', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'rowalign', 'rowspan' ),
'mtext' => array( 'class', 'id', 'style', 'dir', 'href', 'mathbackground', 'mathcolor', 'mathsize', 'mathvariant' ),
'mtr' => array( 'class', 'id', 'style', 'columnalign', 'groupalign', 'href', 'mathbackground', 'mathcolor', 'rowalign' ),
'munder' => array( 'accentunder', 'align', 'class', 'id', 'style', 'mathbackground', 'mathcolor' ),
'munderover' => array( 'accent', 'accentunder', 'align', 'class', 'id', 'style', 'href', 'mathbackground', 'mathcolor' ),
'semantics' => array( 'definitionURL', 'encoding', 'cd', 'name', 'src' ),
'annotation' => array( 'definitionURL', 'encoding', 'cd', 'name', 'src' ),
'annotation-xml' => array( 'definitionURL', 'encoding', 'cd', 'name', 'src' ),
);
public static function init() {
register_activation_hook( __FILE__, array( __CLASS__, 'mathjax_install' ) );
register_deactivation_hook( __FILE__, array( __CLASS__, 'mathjax_uninstall' ) );
if ( get_option( 'kblog_mathjax_force_load' ) ) {
self::$add_script = true;
}
add_shortcode( 'mathjax', array( __CLASS__, 'mathjax_shortcode' ) );
add_shortcode( 'nomathjax', array( __CLASS__, 'nomathjax_shortcode' ) );
add_shortcode( 'latex', array( __CLASS__, 'latex_shortcode' ) );
add_action( 'wp_footer', array( __CLASS__, 'add_script' ) );
add_filter( 'script_loader_tag', array( __CLASS__, 'script_loader_tag' ), 10, 3 );
if ( get_option( 'kblog_mathjax_use_wplatex_syntax' ) ) {
add_filter( 'the_content', array( __CLASS__, 'inline_to_shortcode' ) );
}
add_filter( 'plugin_action_links', array( __CLASS__, 'mathjax_settings_link' ), 9, 2 );
add_filter( 'the_content', array( __CLASS__, 'filter_br_tags_on_math' ) );
add_action( 'init', array( __CLASS__, 'allow_mathml_tags' ) );
add_filter( 'tiny_mce_before_init', array( __CLASS__, 'allow_mathml_tags_in_tinymce' ) );
}
// registers default options
public static function mathjax_install() {
add_option( 'kblog_mathjax_force_load', false );
add_option( 'kblog_mathjax_latex_inline', 'inline' );
add_option( 'kblog_mathjax_use_wplatex_syntax', false );
add_option( 'kblog_mathjax_use_cdn', true );
add_option( 'kblog_mathjax_custom_location', false );
add_option( 'kblog_mathjax_config', 'default' );
}
public static function mathjax_uninstall() {
delete_option( 'kblog_mathjax_force_load' );
delete_option( 'kblog_mathjax_latex_inline' );
delete_option( 'kblog_mathjax_use_wplatex_syntax' );
delete_option( 'kblog_mathjax_use_cdn' );
delete_option( 'kblog_mathjax_custom_location' );
delete_option( 'kblog_mathjax_config' );
}
public static function mathjax_shortcode( $atts, $content ) {
self::$add_script = true;
}
public static function nomathjax_shortcode( $atts, $content ) {
self::$block_script = true;
}
public static function latex_shortcode( $atts, $content ) {
self::$add_script = true;
// this gives us an optional "syntax" attribute, which defaults to "inline", but can also be "display"
$shortcode_atts = shortcode_atts(
array(
'syntax' => get_option( 'kblog_mathjax_latex_inline' ),
), $atts
);
if ( 'inline' === $shortcode_atts['syntax'] ) {
return '\(' . $content . '\)';
} elseif ( 'display' === $shortcode_atts['syntax'] ) {
return '\[' . $content . '\]';
}
}
public static function add_script() {
if ( ! self::$add_script ) {
return;
}
if ( self::$block_script ) {
return;
}
// initialise option for existing MathJax-LaTeX users
if ( get_option( 'kblog_mathjax_use_cdn' ) || ! get_option( 'kblog_mathjax_custom_location' ) ) {
$mathjax_location = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js';
} else {
$mathjax_location = get_option( 'kblog_mathjax_custom_location' );
}
$mathjax_url = $mathjax_location . '?config=' . get_option( 'kblog_mathjax_config' );
wp_enqueue_script( 'mathjax', $mathjax_url, false, MATHJAX_VERSION, false );
$mathjax_config = apply_filters( 'mathjax_config', array() );
if ( $mathjax_config ) {
wp_add_inline_script( 'mathjax', 'MathJax.Hub.Config(' . wp_json_encode( $mathjax_config ) . ');' );
}
}
/**
* Set the script tag to have type text/x-mathjax-config
*
* @param string $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*
* @return string $tag
*/
public static function script_loader_tag( $tag, $handle = null, $src = null ) {
if ( 'mathjax' === $handle ) {
// replace the <script> tag for the inline script, but not for the <script> tag with src=""
return str_replace( "<script type='text/javascript'>", "<script type='text/x-mathjax-config'>", $tag );
}
return $tag;
}
public static function inline_to_shortcode( $content ) {
if ( false === strpos( $content, '$latex' ) ) {
return $content;
}
self::$add_script = true;
return preg_replace_callback( '#\$latex[= ](.*?[^\\\\])\$#', array( __CLASS__, 'inline_to_shortcode_callback' ), $content );
}
public static function inline_to_shortcode_callback( $matches ) {
//
// Also support wp-latex syntax. This includes the ability to set background and foreground
// colour, which we can ignore.
//
if ( preg_match( '/.+((?:&#038;|&amp;)s=(-?[0-4])).*/i', $matches[1], $s_matches ) ) {
$matches[1] = str_replace( $s_matches[1], '', $matches[1] );
}
if ( preg_match( '/.+((?:&#038;|&amp;)fg=([0-9a-f]{6})).*/i', $matches[1], $fg_matches ) ) {
$matches[1] = str_replace( $fg_matches[1], '', $matches[1] );
}
if ( preg_match( '/.+((?:&#038;|&amp;)bg=([0-9a-f]{6})).*/i', $matches[1], $bg_matches ) ) {
$matches[1] = str_replace( $bg_matches[1], '', $matches[1] );
}
return "[latex]{$matches[1]}[/latex]";
}
// add a link to settings on the plugin management page
public static function mathjax_settings_link( $links, $file ) {
if ( 'mathjax-latex/mathjax-latex.php' === $file && function_exists( 'admin_url' ) ) {
$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=kblog-mathjax-latex' ) ) . '">' . esc_html__( 'Settings' ) . '</a>';
array_unshift( $links, $settings_link );
}
return $links;
}
/**
* Removes the <br /> tags inside math tags
*
* @param $content
* @return string without <br /> tags
*/
public static function filter_br_tags_on_math( $content ) {
return preg_replace_callback(
'/(<math.*>.*<\/math>)/isU',
function( $matches ) {
return str_replace( array( '<br/>', '<br />', '<br>' ), '', $matches[0] );
},
$content
);
}
/**
* Allow MathML tags within WordPress
* http://vip.wordpress.com/documentation/register-additional-html-attributes-for-tinymce-and-wp-kses/
* https://developer.mozilla.org/en-US/docs/Web/MathML/Element
*/
public static function allow_mathml_tags() {
global $allowedposttags;
foreach ( self::$mathml_tags as $tag => $attributes ) {
$allowedposttags[ $tag ] = array();
foreach ( $attributes as $a ) {
$allowedposttags[ $tag ][ $a ] = true;
}
}
}
/**
* Ensure that the MathML tags will not be removed
* by the TinyMCE editor
*/
public static function allow_mathml_tags_in_tinymce( $options ) {
$extended_tags = array();
foreach ( self::$mathml_tags as $tag => $attributes ) {
if ( ! empty( $attributes ) ) {
$tag = $tag . '[' . implode( '|', $attributes ) . ']';
}
$extended_tags[] = $tag;
}
if ( ! isset( $options['extended_valid_elements'] ) ) {
$options['extended_valid_elements'] = '';
}
$options['extended_valid_elements'] .= ',' . implode( ',', $extended_tags );
$options['extended_valid_elements'] = trim( $options['extended_valid_elements'], ',' );
return $options;
}
}
MathJax::init();
require_once __DIR__ . '/class-mathjax-latex.php';
......@@ -4,8 +4,8 @@ Contributors: philliplord, sjcockell, knowledgeblog, d_swan, paulschreiber, jwen
Tags: mathematics, math, latex, mathml, mathjax, science, res-comms, scholar, academic
Requires at least: 3.0
Tested up to: 4.9.1
Stable tag: 1.3.8
Requires PHP: 5.4.0
Stable tag: 1.3.10
Requires PHP: 7.0.0
License: GPLv2
This plugin enables mathjax (http://www.mathjax.org) functionality for
......@@ -34,6 +34,16 @@ MathJax-LaTeX is developed on [GitHub](https://github.com/phillord/mathjax-latex
== Changelog ==
= 1.3.10 =
1. Rename class files, per PHPCS
2. Gracefully handle null values in filter_br_tags_on_math. Thanks to Yang Liu.
= 1.3.9 =
1. Code style changes, per PHPCS 3.3.0 and WPCS 0.14.1
1. Use PHP 7 short array syntax
= 1.3.8 =
1. Code style changes, per PHPCS 3.1.1 and WPCS 0.14
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment