Plugin Directory

Changeset 2172174


Ignore:
Timestamp:
10/12/2019 01:08:51 PM (6 years ago)
Author:
christianperfect
Message:

version 2.0

Location:
simple-mathjax
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • simple-mathjax/tags/2.0/readme.txt

    r2093199 r2172174  
    55Tags: mathjax, latex
    66Requires at least: 3.0
    7 Tested up to: 4.9.4
    8 Stable tag: 1.1
     7Tested up to: 5.2.3
     8Stable tag: 2.0
    99
    1010Yet another plugin to add MathJax support to your wordpress blog.
    11 Just wrap your equations inside $ signs and MathJax will render
    12 them visually.
     11Just wrap your equations inside $ signs and MathJax will render them visually.
    1312
    1413== Description ==
    1514
    16 This wordpress plugin is yet another simple plugin to load the
    17 [MathJax](http://www.mathjax.org) scripts at the bottom of all of your
    18 pages.  It uses a very all-inclusive mathjax configuration by default,
    19 with $'s and $$'s the default delimeters for in-line and displayed
    20 equations.
     15This wordpress plugin is yet another simple plugin to load the [MathJax](http://www.mathjax.org) scripts at the bottom of all of your pages. It uses a very all-inclusive mathjax configuration by default, with $'s and $$'s the default delimeters for in-line and displayed equations.
    2116
    22 A preference pane is added to the "Settings" group where you can
    23 modify the MathJax server location (CDN) and the MathJax configuration
    24 settings.  (See [this
    25 page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options)
    26 for details on the options available.)  You can also specify a LaTeX
    27 "preamble" of newcommands which will be loaded in a hidden element
    28 near the top of each page.
     17A preference pane is added to the "Settings" group where you can choose whether to use MathJax version 2 or 3, change the MathJax server location (CDN) and the MathJax configuration settings. (See [this page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options) for details on the options available.)
     18You can also specify a LaTeX "preamble" of newcommands which will be loaded in a hidden element near the top of each page.
    2919
    30 Fork this plugin on
    31 [GitHub](https://github.com/scoskey/Simple-Mathjax-wordpress-plugin)
     20Fork this plugin on [GitHub](https://github.com/boolesrings/Simple-Mathjax-wordpress-plugin).
    3221
    3322== Installation ==
     
    3726== Changelog ==
    3827
    39 `1.1` update latest mathjax version, load mathjax in footer instead of header
     28= 2.0 =
     29MathJax 3 support, and the default config makes MathJax ignore the new block editor.
    4030
    41 `1.0` send default url to new cdn
     31= 1.1 =
     32update latest mathjax version, load mathjax in footer instead of header
    4233
    43 `0.5` minor code cleanup, allow mathjax in admin screens
     34= 1.0 =
     35send default url to new cdn
    4436
    45 `0.4` use safe mode (prevents evil scripts) by default
     37= 0.5 =
     38minor code cleanup, allow mathjax in admin screens
    4639
    47 `0.3` use wp_enqueue_script to allow others to use mathjax as a dependency
    48 (christianp).  removed disqus compatibility due to reports of it no longer
    49 working.
     40= 0.4 =
     41use safe mode (prevents evil scripts) by default
    5042
    51 `0.2` added disqus compatibility. enclosed the preamble in a hidden span to
     43= 0.3 =
     44use wp_enqueue_script to allow others to use mathjax as a dependency (christianp). removed disqus compatibility due to reports of it no longer working.
     45
     46= 0.2 =
     47added disqus compatibility. enclosed the preamble in a hidden span to
    5248cover a small space created by mathjax v.2
    5349
    54 `0.1` improved loading of the LaTeX preamble so that it appears just
     50= 0.1 =
     51improved loading of the LaTeX preamble so that it appears just
    5552below the body tag, rather than in the header
    5653
    57 `0.0` initial release
     54= 0.0 =
     55initial release
  • simple-mathjax/tags/2.0/simple-mathjax.php

    r1840306 r2172174  
    33 * Plugin Name: Simple Mathjax
    44 * Description: Load the mathjax scripts across your wordpress blog
    5  * Version: 1.1
    6  * Author: Samuel Coskey, Peter Krautzberger
     5 * Version: 2.0
     6 * Author: Samuel Coskey, Peter Krautzberger, Christian Lawson-Perfect
    77 * Author URI: https://boolesrings.org
    88*/
    99
    1010
     11$default_options = array(
     12  'major_version' => 3,
     13  'mathjax_in_admin' => false,
     14  'custom_mathjax_cdn' => '',
     15  'custom_mathjax_config' => '',
     16  'latex_preamble' => ''
     17);
     18$default_configs = array(
     19  2 => "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n",
     20  3 => "MathJax = {tex: {inlineMath: [['$','$'],['\\\\(','\\\\)']], processEscapes: true}};\n"
     21);
     22$default_cdns = array(
     23  2 => "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js",
     24  3 => "//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"
     25);
     26
     27function load_options() {
     28  global $default_options;
     29  $options = array_merge($default_options,array());
     30
     31  // restore options from old versions of this plugin
     32  $old_keys = array('custom_mathjax_cdn', 'custom_mathjax_config', 'latex_preamble', 'mathjax_in_admin');
     33  $has_old_values = false;
     34  foreach($old_keys as $key) {
     35    if(($value = get_option($key)) !== false) {
     36      $options[$key] = $value;
     37      $has_old_values = true;
     38    }
     39  }
     40
     41
     42  // apply options set locally
     43  $set_options = get_option('simple_mathjax_options');
     44  if(is_array($set_options)) {
     45    foreach($set_options as $key=>$value) {
     46      if(array_key_exists($key,$options)) {
     47        $options[$key] = $value;
     48      }
     49    }
     50  }
     51
     52  if($has_old_values) {
     53    $options['major_version'] = 2;
     54    foreach($old_keys as $key) {
     55      delete_option($key);
     56    }
     57    add_option('simple_mathjax_options',$options);
     58  }
     59
     60  return $options;
     61}
     62
     63
    1164/*
    1265 * inserts the mathjax configuration
    1366 */
     67
    1468add_action('wp_head','configure_mathjax',1);
    1569function configure_mathjax() {
    16   $custom_config = wp_kses( get_option('custom_mathjax_config'), array() );
    17   $config = $custom_config ? $custom_config : "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n";
    18   echo "\n<script type='text/x-mathjax-config'>\n" . $config . "</script>\n";
     70  global $default_configs;
     71  $options = load_options();
     72  $version = $options['major_version'];
     73  $custom_config = wp_kses( $options['custom_mathjax_config'], array() );
     74  $config = $custom_config ? $custom_config : $default_configs[$version];
     75  if($version==2) {
     76    echo "\n<script type='text/x-mathjax-config'>\n{$config}\n</script>\n";
     77  } else {
     78    echo "\n<script>\n{$config}\n</script>\n";
     79  }
    1980}
    2081
     
    2485add_action('wp_enqueue_scripts', 'add_mathjax');
    2586function add_mathjax() {
    26   $custom_cdn = esc_url( get_option('custom_mathjax_cdn') );
    27   $cdn = $custom_cdn ? $custom_cdn : "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js";
     87  global $default_cdns;
     88  $options = load_options();
     89  $version = $options['major_version'];
     90  $custom_cdn = esc_url( $options['custom_mathjax_cdn'] );
     91  $cdn = $custom_cdn ? $custom_cdn : $default_cdns[$version];
    2892  wp_enqueue_script('mathjax', $cdn, array(), false, true);
    2993}
     
    3498add_action('wp_footer', 'add_preamble_adder');
    3599function add_preamble_adder() {
    36   $preamble = get_option('latex_preamble');
     100  $options = load_options();
     101  $version = $options['major_version'];
     102  $preamble = $options['latex_preamble'];
    37103  if ( $preamble ) {
    38     $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble);
    39 ?>
    40 <script type='text/javascript'>
    41   newContainer = document.createElement('span');
    42   newContainer.style.setProperty("display","none","");
    43   newNode = document.createElement('script');
    44   newNode.type = "math/tex";
    45   newNode.innerHTML = '<?php echo esc_js($preamble); ?>';
    46   newContainer.appendChild(newNode);
    47   document.body.insertBefore(newContainer,document.body.firstChild);
     104    if($version==2) {
     105      $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble);
     106
     107?>
     108<script>
     109  (function() {
     110    var newContainer = document.createElement('span');
     111    newContainer.style.setProperty("display","none","");
     112    var newNode = document.createElement('script');
     113    newNode.type = "math/tex";
     114    var preamble = '<?php echo esc_js($preamble); ?>';
     115    newNode.innerHTML = preamble;
     116    newContainer.appendChild(newNode);
     117    document.body.insertBefore(newContainer,document.body.firstChild);
     118  })();
    48119</script>
    49120<?php
     121
     122    } else if($version==3) {
     123
     124?>
     125<script>
     126  (function() {
     127    var newNode = document.createElement('span');
     128    newNode.style.setProperty("display","none","");
     129    var preamble = '\\( <?= esc_js(addslashes($preamble)); ?> \\)';
     130    newNode.innerHTML = preamble;
     131    document.body.insertBefore(newNode,document.body.firstChild);
     132  })();
     133</script>
     134<?php
     135
     136    }
    50137  }
    51138}
     
    53140/*
    54141 * Perform all three actions in admin pages too, if the option is set (CP)
    55 */
    56 if ( get_option( 'mathjax_in_admin' ) ) {
    57     add_action('admin_head', 'configure_mathjax', 1);
    58     add_action('admin_enqueue_scripts', 'add_mathjax');
    59     add_action('admin_footer', 'add_preamble_adder');
     142 */
     143$options = load_options();
     144if ( $options['mathjax_in_admin'] ) {
     145   add_action('admin_head', 'configure_mathjax', 1);
     146   add_action('admin_enqueue_scripts', 'add_mathjax');
     147   add_action('admin_footer', 'add_preamble_adder');
    60148}
    61149
     
    70158    'Simple Mathjax',           // Label in menu
    71159    'manage_options',           // Capability required
    72     'simple_mathjax_identifier',  // Menu slug, used to uniquely identify the page
    73     'simple_mathjax_options'    // Function that renders the options page
     160    'simple_mathjax_options',  // Menu slug, used to uniquely identify the page
     161    'simple_mathjax_options_page'    // Function that renders the options page
    74162  );
    75163
     
    80168  add_action( 'admin_init', 'register_simple_mathjax_settings' );
    81169}
    82 function register_simple_mathjax_settings() {
    83   register_setting( 'simple_mathjax_group', 'custom_mathjax_cdn' );
    84   register_setting( 'simple_mathjax_group', 'custom_mathjax_config' );
    85   register_setting( 'simple_mathjax_group', 'latex_preamble' );
    86   register_setting( 'simple_mathjax_group', 'mathjax_in_admin' );
    87 }
    88 function simple_mathjax_options() {
    89 ?>
    90 <div class="wrap">
    91 <h2>Simple Mathjax options</h2>
    92 <p>
    93   (Please note that this still needs to be tested.  There may be a problem with string-escaping.  For instance, I'm not sure if special characters such as &lt; will be properly processed.)
    94 </p>
     170
     171function simple_mathjax_options_page() {
     172?>
     173<div>
     174<h1>Simple Mathjax options</h1>
    95175<form method="post" action="options.php">
    96     <?php settings_fields( 'simple_mathjax_group' ); ?>
    97     <table class="form-table">
    98         <tr valign="top">
    99         <th scope="row">Custom mathjax CDN</th>
    100         <td><input type="text" name="custom_mathjax_cdn" size="50" value="<?php echo esc_url( get_option('custom_mathjax_cdn') ); ?>" /></td>
    101     <td><p>If you leave this blank, the default will be used: <code>//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js</code></p></td>
    102         </tr>
    103         <tr valign="top">
    104         <th scope="row">Custom mathjax config</th>
    105         <td><textarea name="custom_mathjax_config" cols="50" rows="10"/><?php echo esc_textarea(get_option('custom_mathjax_config')); ?></textarea></td>
    106     <td><p>This text will be placed inside the <code>&lt;script x-mathjax-config&gt;</code> tag</p><p>If you leave this blank, the default will be used: <code>MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});</code></p></td>
    107         </tr>
    108         <tr valign="top">
    109         <th scope="row">Custom LaTeX preamble</th>
    110         <td><textarea name="latex_preamble" cols="50" rows="10"/><?php echo esc_textarea(get_option('latex_preamble')); ?></textarea></td>
    111     <td><p>A good place to put \newcommand's and \renewcommand's</p><p><strong>Do not us $ signs</strong>, they will be added for you</p><p>E.g.<br/><code>\newcommand{\NN}{\mathbb N}<br/>\newcommand{\abs}[1]{\left|#1\right|}</code></p></td>
    112         </tr>
    113     <tr valign="top">
    114     <th scope="row">Load MathJax on admin pages</th>
    115     <td><input type="checkbox" name="mathjax_in_admin" value="yes" <?php if( get_option('mathjax_in_admin') ) { echo "checked"; } ?> /></td>
    116     <td><p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p></td>
    117     </tr>
    118       </table>
    119     <p class="submit">
    120     <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    121     </p>
     176  <?php settings_fields( 'simple_mathjax_options' ); ?>
     177  <?php do_settings_sections('simple_mathjax'); ?>
     178
     179  <button type="submit"><?php _e('Save Changes'); ?></button>
    122180</form>
    123181</div>
    124182<?php }
    125183
    126 
    127 ?>
     184function register_simple_mathjax_settings() {
     185  global $default_options;
     186  register_setting(
     187    'simple_mathjax_options',
     188    'simple_mathjax_options',
     189    array(
     190      'sanitize_callback' => 'simple_mathjax_options_validate'
     191    )
     192  );
     193
     194  add_settings_section(
     195    'simple_mathjax_main',
     196    'Main Settings',
     197    'simple_mathjax_main_text',
     198    'simple_mathjax'
     199  );
     200  add_settings_section('simple_mathjax_config',
     201    'Configuration',
     202    'simple_mathjax_config_text',
     203    'simple_mathjax'
     204  );
     205
     206  add_settings_field(
     207    'major_version',
     208    'MathJax major version',
     209    'simple_mathjax_major_version_input',
     210    'simple_mathjax',
     211    'simple_mathjax_main',
     212    array(
     213      'label_for' => 'major_version'
     214    )
     215  );
     216  add_settings_field(
     217    'mathjax_in_admin',
     218    'Load MathJax on admin pages?',
     219    'simple_mathjax_in_admin_input',
     220    'simple_mathjax',
     221    'simple_mathjax_main',
     222    array(
     223      'label_for' => 'mathjax_in_admin'
     224    )
     225  );
     226
     227  add_settings_field(
     228    'custom_mathjax_cdn',
     229    'Custom MathJax CDN',
     230    'simple_mathjax_cdn_input',
     231    'simple_mathjax',
     232    'simple_mathjax_config',
     233    array(
     234      'label_for' => 'custom_mathjax_cdn'
     235    )
     236  );
     237  add_settings_field(
     238    'custom_mathjax_config',
     239    'Custom MathJax config',
     240    'simple_mathjax_config_input',
     241    'simple_mathjax',
     242    'simple_mathjax_config',
     243    array(
     244      'label_for' => 'custom_mathjax_config'
     245    )
     246  );
     247  add_settings_field(
     248    'latex_preamble',
     249    'Custom LaTeX preamble',
     250    'simple_mathjax_latex_preamble_input',
     251    'simple_mathjax',
     252    'simple_mathjax_config',
     253    array(
     254      'label_for' => 'latex_preamble'
     255    )
     256  );
     257
     258}
     259
     260function simple_mathjax_options_validate($options) {
     261  global $default_options;
     262  $cleaned = array();
     263  foreach($default_options as $key => $value) {
     264    $cleaned[$key] = $options[$key];
     265  }
     266  $cleaned['mathjax_in_admin'] = $cleaned['mathjax_in_admin'] ? true : false;
     267  return $cleaned;
     268}
     269
     270function simple_mathjax_main_text() {
     271}
     272
     273function simple_mathjax_config_text() {
     274}
     275
     276function simple_mathjax_major_version_input() {
     277  $options = load_options();
     278?>
     279  <select id="major_version" name="simple_mathjax_options[major_version]">
     280    <option value="2" <?= $options['major_version']==2 ? 'selected' : '' ?>>2</option>
     281    <option value="3" <?= $options['major_version']==3 ? 'selected' : '' ?>>3</option>
     282  </select>
     283  <p>MathJax versions 2 and 3 work very differently. See the <a href="http://docs.mathjax.org/en/latest/upgrading/v2.html">MathJax documentation</a>.</p>
     284<?php
     285}
     286
     287function simple_mathjax_in_admin_input() {
     288  $options = load_options();
     289?>
     290  <input type="checkbox" id="mathjax_in_admin" name="simple_mathjax_options[mathjax_in_admin]" <?= $options['mathjax_in_admin'] ? 'checked' : '' ?>>
     291  <p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p>
     292<?php
     293}
     294
     295function simple_mathjax_cdn_input() {
     296  global $default_cdns;
     297  $options = load_options();
     298?>
     299  <input type="text" id="custom_mathjax_cdn" size="50" name="simple_mathjax_options[custom_mathjax_cdn]" value="<?= $options['custom_mathjax_cdn'] ?>">
     300  <p>If you leave this blank, the default will be used, depending on the major version of MathJax:</p>
     301  <dl>
     302    <dt>Version 2</dt>
     303    <dd><code><?= $default_cdns[2] ?></code></dd>
     304    <dt>Version 3</dt>
     305    <dd><code><?= $default_cdns[3] ?></code></dd>
     306  </dl>
     307<?php
     308}
     309
     310function simple_mathjax_config_input() {
     311  global $default_configs;
     312  $options = load_options();
     313?>
     314  <textarea id="custom_mathjax_config" cols="50" rows="10" name="simple_mathjax_options[custom_mathjax_config]"><?= $options['custom_mathjax_config'] ?></textarea>
     315  <p>This text will be used to configure MathJax. See <a href="https://docs.mathjax.org/en/latest/options/index.html">the documentation on configuring MathJax</a>.</p>
     316  <p>If you leave this blank, the default will be used, according to the major version of MathJax:</p>
     317  <dl>
     318    <dt>Version 2</dt>
     319    <dd><pre><?= $default_configs[2] ?></pre></dd>
     320    <dt>Version 3</dt>
     321    <dd><pre><?= $default_configs[3] ?></pre></dd>
     322  </dl>
     323<?php
     324}
     325
     326function simple_mathjax_latex_preamble_input() {
     327  $options = load_options();
     328?>
     329  <textarea id="latex_preamble" cols="50" rows="10" name="simple_mathjax_options[latex_preamble]"><?= $options['latex_preamble'] ?></textarea>
     330  <p>This LaTeX will be run invisibly before any other LaTeX on the page. A good place to put \newcommand's and \renewcommand's</p>
     331  <p><strong>Do not us $ signs</strong>, they will be added for you</p>
     332  <p>E.g.</p>
     333  <pre>\newcommand{\NN}{\mathbb N}
     334\newcommand{\abs}[1]{\left|#1\right|}</pre>
     335<?php
     336}
  • simple-mathjax/trunk/readme.txt

    r2093199 r2172174  
    55Tags: mathjax, latex
    66Requires at least: 3.0
    7 Tested up to: 4.9.4
    8 Stable tag: 1.1
     7Tested up to: 5.2.3
     8Stable tag: 2.0
    99
    1010Yet another plugin to add MathJax support to your wordpress blog.
    11 Just wrap your equations inside $ signs and MathJax will render
    12 them visually.
     11Just wrap your equations inside $ signs and MathJax will render them visually.
    1312
    1413== Description ==
    1514
    16 This wordpress plugin is yet another simple plugin to load the
    17 [MathJax](http://www.mathjax.org) scripts at the bottom of all of your
    18 pages.  It uses a very all-inclusive mathjax configuration by default,
    19 with $'s and $$'s the default delimeters for in-line and displayed
    20 equations.
     15This wordpress plugin is yet another simple plugin to load the [MathJax](http://www.mathjax.org) scripts at the bottom of all of your pages. It uses a very all-inclusive mathjax configuration by default, with $'s and $$'s the default delimeters for in-line and displayed equations.
    2116
    22 A preference pane is added to the "Settings" group where you can
    23 modify the MathJax server location (CDN) and the MathJax configuration
    24 settings.  (See [this
    25 page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options)
    26 for details on the options available.)  You can also specify a LaTeX
    27 "preamble" of newcommands which will be loaded in a hidden element
    28 near the top of each page.
     17A preference pane is added to the "Settings" group where you can choose whether to use MathJax version 2 or 3, change the MathJax server location (CDN) and the MathJax configuration settings. (See [this page](https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options) for details on the options available.)
     18You can also specify a LaTeX "preamble" of newcommands which will be loaded in a hidden element near the top of each page.
    2919
    30 Fork this plugin on
    31 [GitHub](https://github.com/scoskey/Simple-Mathjax-wordpress-plugin)
     20Fork this plugin on [GitHub](https://github.com/boolesrings/Simple-Mathjax-wordpress-plugin).
    3221
    3322== Installation ==
     
    3726== Changelog ==
    3827
    39 `1.1` update latest mathjax version, load mathjax in footer instead of header
     28= 2.0 =
     29MathJax 3 support, and the default config makes MathJax ignore the new block editor.
    4030
    41 `1.0` send default url to new cdn
     31= 1.1 =
     32update latest mathjax version, load mathjax in footer instead of header
    4233
    43 `0.5` minor code cleanup, allow mathjax in admin screens
     34= 1.0 =
     35send default url to new cdn
    4436
    45 `0.4` use safe mode (prevents evil scripts) by default
     37= 0.5 =
     38minor code cleanup, allow mathjax in admin screens
    4639
    47 `0.3` use wp_enqueue_script to allow others to use mathjax as a dependency
    48 (christianp).  removed disqus compatibility due to reports of it no longer
    49 working.
     40= 0.4 =
     41use safe mode (prevents evil scripts) by default
    5042
    51 `0.2` added disqus compatibility. enclosed the preamble in a hidden span to
     43= 0.3 =
     44use wp_enqueue_script to allow others to use mathjax as a dependency (christianp). removed disqus compatibility due to reports of it no longer working.
     45
     46= 0.2 =
     47added disqus compatibility. enclosed the preamble in a hidden span to
    5248cover a small space created by mathjax v.2
    5349
    54 `0.1` improved loading of the LaTeX preamble so that it appears just
     50= 0.1 =
     51improved loading of the LaTeX preamble so that it appears just
    5552below the body tag, rather than in the header
    5653
    57 `0.0` initial release
     54= 0.0 =
     55initial release
  • simple-mathjax/trunk/simple-mathjax.php

    r1840306 r2172174  
    33 * Plugin Name: Simple Mathjax
    44 * Description: Load the mathjax scripts across your wordpress blog
    5  * Version: 1.1
    6  * Author: Samuel Coskey, Peter Krautzberger
     5 * Version: 2.0
     6 * Author: Samuel Coskey, Peter Krautzberger, Christian Lawson-Perfect
    77 * Author URI: https://boolesrings.org
    88*/
    99
    1010
     11$default_options = array(
     12  'major_version' => 3,
     13  'mathjax_in_admin' => false,
     14  'custom_mathjax_cdn' => '',
     15  'custom_mathjax_config' => '',
     16  'latex_preamble' => ''
     17);
     18$default_configs = array(
     19  2 => "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n",
     20  3 => "MathJax = {tex: {inlineMath: [['$','$'],['\\\\(','\\\\)']], processEscapes: true}};\n"
     21);
     22$default_cdns = array(
     23  2 => "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js",
     24  3 => "//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"
     25);
     26
     27function load_options() {
     28  global $default_options;
     29  $options = array_merge($default_options,array());
     30
     31  // restore options from old versions of this plugin
     32  $old_keys = array('custom_mathjax_cdn', 'custom_mathjax_config', 'latex_preamble', 'mathjax_in_admin');
     33  $has_old_values = false;
     34  foreach($old_keys as $key) {
     35    if(($value = get_option($key)) !== false) {
     36      $options[$key] = $value;
     37      $has_old_values = true;
     38    }
     39  }
     40
     41
     42  // apply options set locally
     43  $set_options = get_option('simple_mathjax_options');
     44  if(is_array($set_options)) {
     45    foreach($set_options as $key=>$value) {
     46      if(array_key_exists($key,$options)) {
     47        $options[$key] = $value;
     48      }
     49    }
     50  }
     51
     52  if($has_old_values) {
     53    $options['major_version'] = 2;
     54    foreach($old_keys as $key) {
     55      delete_option($key);
     56    }
     57    add_option('simple_mathjax_options',$options);
     58  }
     59
     60  return $options;
     61}
     62
     63
    1164/*
    1265 * inserts the mathjax configuration
    1366 */
     67
    1468add_action('wp_head','configure_mathjax',1);
    1569function configure_mathjax() {
    16   $custom_config = wp_kses( get_option('custom_mathjax_config'), array() );
    17   $config = $custom_config ? $custom_config : "MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']], processEscapes: true}});\n";
    18   echo "\n<script type='text/x-mathjax-config'>\n" . $config . "</script>\n";
     70  global $default_configs;
     71  $options = load_options();
     72  $version = $options['major_version'];
     73  $custom_config = wp_kses( $options['custom_mathjax_config'], array() );
     74  $config = $custom_config ? $custom_config : $default_configs[$version];
     75  if($version==2) {
     76    echo "\n<script type='text/x-mathjax-config'>\n{$config}\n</script>\n";
     77  } else {
     78    echo "\n<script>\n{$config}\n</script>\n";
     79  }
    1980}
    2081
     
    2485add_action('wp_enqueue_scripts', 'add_mathjax');
    2586function add_mathjax() {
    26   $custom_cdn = esc_url( get_option('custom_mathjax_cdn') );
    27   $cdn = $custom_cdn ? $custom_cdn : "//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js";
     87  global $default_cdns;
     88  $options = load_options();
     89  $version = $options['major_version'];
     90  $custom_cdn = esc_url( $options['custom_mathjax_cdn'] );
     91  $cdn = $custom_cdn ? $custom_cdn : $default_cdns[$version];
    2892  wp_enqueue_script('mathjax', $cdn, array(), false, true);
    2993}
     
    3498add_action('wp_footer', 'add_preamble_adder');
    3599function add_preamble_adder() {
    36   $preamble = get_option('latex_preamble');
     100  $options = load_options();
     101  $version = $options['major_version'];
     102  $preamble = $options['latex_preamble'];
    37103  if ( $preamble ) {
    38     $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble);
    39 ?>
    40 <script type='text/javascript'>
    41   newContainer = document.createElement('span');
    42   newContainer.style.setProperty("display","none","");
    43   newNode = document.createElement('script');
    44   newNode.type = "math/tex";
    45   newNode.innerHTML = '<?php echo esc_js($preamble); ?>';
    46   newContainer.appendChild(newNode);
    47   document.body.insertBefore(newContainer,document.body.firstChild);
     104    if($version==2) {
     105      $preamble = preg_replace('/\\\\/','\\\\\\\\',$preamble);
     106
     107?>
     108<script>
     109  (function() {
     110    var newContainer = document.createElement('span');
     111    newContainer.style.setProperty("display","none","");
     112    var newNode = document.createElement('script');
     113    newNode.type = "math/tex";
     114    var preamble = '<?php echo esc_js($preamble); ?>';
     115    newNode.innerHTML = preamble;
     116    newContainer.appendChild(newNode);
     117    document.body.insertBefore(newContainer,document.body.firstChild);
     118  })();
    48119</script>
    49120<?php
     121
     122    } else if($version==3) {
     123
     124?>
     125<script>
     126  (function() {
     127    var newNode = document.createElement('span');
     128    newNode.style.setProperty("display","none","");
     129    var preamble = '\\( <?= esc_js(addslashes($preamble)); ?> \\)';
     130    newNode.innerHTML = preamble;
     131    document.body.insertBefore(newNode,document.body.firstChild);
     132  })();
     133</script>
     134<?php
     135
     136    }
    50137  }
    51138}
     
    53140/*
    54141 * Perform all three actions in admin pages too, if the option is set (CP)
    55 */
    56 if ( get_option( 'mathjax_in_admin' ) ) {
    57     add_action('admin_head', 'configure_mathjax', 1);
    58     add_action('admin_enqueue_scripts', 'add_mathjax');
    59     add_action('admin_footer', 'add_preamble_adder');
     142 */
     143$options = load_options();
     144if ( $options['mathjax_in_admin'] ) {
     145   add_action('admin_head', 'configure_mathjax', 1);
     146   add_action('admin_enqueue_scripts', 'add_mathjax');
     147   add_action('admin_footer', 'add_preamble_adder');
    60148}
    61149
     
    70158    'Simple Mathjax',           // Label in menu
    71159    'manage_options',           // Capability required
    72     'simple_mathjax_identifier',  // Menu slug, used to uniquely identify the page
    73     'simple_mathjax_options'    // Function that renders the options page
     160    'simple_mathjax_options',  // Menu slug, used to uniquely identify the page
     161    'simple_mathjax_options_page'    // Function that renders the options page
    74162  );
    75163
     
    80168  add_action( 'admin_init', 'register_simple_mathjax_settings' );
    81169}
    82 function register_simple_mathjax_settings() {
    83   register_setting( 'simple_mathjax_group', 'custom_mathjax_cdn' );
    84   register_setting( 'simple_mathjax_group', 'custom_mathjax_config' );
    85   register_setting( 'simple_mathjax_group', 'latex_preamble' );
    86   register_setting( 'simple_mathjax_group', 'mathjax_in_admin' );
    87 }
    88 function simple_mathjax_options() {
    89 ?>
    90 <div class="wrap">
    91 <h2>Simple Mathjax options</h2>
    92 <p>
    93   (Please note that this still needs to be tested.  There may be a problem with string-escaping.  For instance, I'm not sure if special characters such as &lt; will be properly processed.)
    94 </p>
     170
     171function simple_mathjax_options_page() {
     172?>
     173<div>
     174<h1>Simple Mathjax options</h1>
    95175<form method="post" action="options.php">
    96     <?php settings_fields( 'simple_mathjax_group' ); ?>
    97     <table class="form-table">
    98         <tr valign="top">
    99         <th scope="row">Custom mathjax CDN</th>
    100         <td><input type="text" name="custom_mathjax_cdn" size="50" value="<?php echo esc_url( get_option('custom_mathjax_cdn') ); ?>" /></td>
    101     <td><p>If you leave this blank, the default will be used: <code>//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML,Safe.js</code></p></td>
    102         </tr>
    103         <tr valign="top">
    104         <th scope="row">Custom mathjax config</th>
    105         <td><textarea name="custom_mathjax_config" cols="50" rows="10"/><?php echo esc_textarea(get_option('custom_mathjax_config')); ?></textarea></td>
    106     <td><p>This text will be placed inside the <code>&lt;script x-mathjax-config&gt;</code> tag</p><p>If you leave this blank, the default will be used: <code>MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});</code></p></td>
    107         </tr>
    108         <tr valign="top">
    109         <th scope="row">Custom LaTeX preamble</th>
    110         <td><textarea name="latex_preamble" cols="50" rows="10"/><?php echo esc_textarea(get_option('latex_preamble')); ?></textarea></td>
    111     <td><p>A good place to put \newcommand's and \renewcommand's</p><p><strong>Do not us $ signs</strong>, they will be added for you</p><p>E.g.<br/><code>\newcommand{\NN}{\mathbb N}<br/>\newcommand{\abs}[1]{\left|#1\right|}</code></p></td>
    112         </tr>
    113     <tr valign="top">
    114     <th scope="row">Load MathJax on admin pages</th>
    115     <td><input type="checkbox" name="mathjax_in_admin" value="yes" <?php if( get_option('mathjax_in_admin') ) { echo "checked"; } ?> /></td>
    116     <td><p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p></td>
    117     </tr>
    118       </table>
    119     <p class="submit">
    120     <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    121     </p>
     176  <?php settings_fields( 'simple_mathjax_options' ); ?>
     177  <?php do_settings_sections('simple_mathjax'); ?>
     178
     179  <button type="submit"><?php _e('Save Changes'); ?></button>
    122180</form>
    123181</div>
    124182<?php }
    125183
    126 
    127 ?>
     184function register_simple_mathjax_settings() {
     185  global $default_options;
     186  register_setting(
     187    'simple_mathjax_options',
     188    'simple_mathjax_options',
     189    array(
     190      'sanitize_callback' => 'simple_mathjax_options_validate'
     191    )
     192  );
     193
     194  add_settings_section(
     195    'simple_mathjax_main',
     196    'Main Settings',
     197    'simple_mathjax_main_text',
     198    'simple_mathjax'
     199  );
     200  add_settings_section('simple_mathjax_config',
     201    'Configuration',
     202    'simple_mathjax_config_text',
     203    'simple_mathjax'
     204  );
     205
     206  add_settings_field(
     207    'major_version',
     208    'MathJax major version',
     209    'simple_mathjax_major_version_input',
     210    'simple_mathjax',
     211    'simple_mathjax_main',
     212    array(
     213      'label_for' => 'major_version'
     214    )
     215  );
     216  add_settings_field(
     217    'mathjax_in_admin',
     218    'Load MathJax on admin pages?',
     219    'simple_mathjax_in_admin_input',
     220    'simple_mathjax',
     221    'simple_mathjax_main',
     222    array(
     223      'label_for' => 'mathjax_in_admin'
     224    )
     225  );
     226
     227  add_settings_field(
     228    'custom_mathjax_cdn',
     229    'Custom MathJax CDN',
     230    'simple_mathjax_cdn_input',
     231    'simple_mathjax',
     232    'simple_mathjax_config',
     233    array(
     234      'label_for' => 'custom_mathjax_cdn'
     235    )
     236  );
     237  add_settings_field(
     238    'custom_mathjax_config',
     239    'Custom MathJax config',
     240    'simple_mathjax_config_input',
     241    'simple_mathjax',
     242    'simple_mathjax_config',
     243    array(
     244      'label_for' => 'custom_mathjax_config'
     245    )
     246  );
     247  add_settings_field(
     248    'latex_preamble',
     249    'Custom LaTeX preamble',
     250    'simple_mathjax_latex_preamble_input',
     251    'simple_mathjax',
     252    'simple_mathjax_config',
     253    array(
     254      'label_for' => 'latex_preamble'
     255    )
     256  );
     257
     258}
     259
     260function simple_mathjax_options_validate($options) {
     261  global $default_options;
     262  $cleaned = array();
     263  foreach($default_options as $key => $value) {
     264    $cleaned[$key] = $options[$key];
     265  }
     266  $cleaned['mathjax_in_admin'] = $cleaned['mathjax_in_admin'] ? true : false;
     267  return $cleaned;
     268}
     269
     270function simple_mathjax_main_text() {
     271}
     272
     273function simple_mathjax_config_text() {
     274}
     275
     276function simple_mathjax_major_version_input() {
     277  $options = load_options();
     278?>
     279  <select id="major_version" name="simple_mathjax_options[major_version]">
     280    <option value="2" <?= $options['major_version']==2 ? 'selected' : '' ?>>2</option>
     281    <option value="3" <?= $options['major_version']==3 ? 'selected' : '' ?>>3</option>
     282  </select>
     283  <p>MathJax versions 2 and 3 work very differently. See the <a href="http://docs.mathjax.org/en/latest/upgrading/v2.html">MathJax documentation</a>.</p>
     284<?php
     285}
     286
     287function simple_mathjax_in_admin_input() {
     288  $options = load_options();
     289?>
     290  <input type="checkbox" id="mathjax_in_admin" name="simple_mathjax_options[mathjax_in_admin]" <?= $options['mathjax_in_admin'] ? 'checked' : '' ?>>
     291  <p>If you tick this box, MathJax will be loaded on admin pages as well as the actual site.</p>
     292<?php
     293}
     294
     295function simple_mathjax_cdn_input() {
     296  global $default_cdns;
     297  $options = load_options();
     298?>
     299  <input type="text" id="custom_mathjax_cdn" size="50" name="simple_mathjax_options[custom_mathjax_cdn]" value="<?= $options['custom_mathjax_cdn'] ?>">
     300  <p>If you leave this blank, the default will be used, depending on the major version of MathJax:</p>
     301  <dl>
     302    <dt>Version 2</dt>
     303    <dd><code><?= $default_cdns[2] ?></code></dd>
     304    <dt>Version 3</dt>
     305    <dd><code><?= $default_cdns[3] ?></code></dd>
     306  </dl>
     307<?php
     308}
     309
     310function simple_mathjax_config_input() {
     311  global $default_configs;
     312  $options = load_options();
     313?>
     314  <textarea id="custom_mathjax_config" cols="50" rows="10" name="simple_mathjax_options[custom_mathjax_config]"><?= $options['custom_mathjax_config'] ?></textarea>
     315  <p>This text will be used to configure MathJax. See <a href="https://docs.mathjax.org/en/latest/options/index.html">the documentation on configuring MathJax</a>.</p>
     316  <p>If you leave this blank, the default will be used, according to the major version of MathJax:</p>
     317  <dl>
     318    <dt>Version 2</dt>
     319    <dd><pre><?= $default_configs[2] ?></pre></dd>
     320    <dt>Version 3</dt>
     321    <dd><pre><?= $default_configs[3] ?></pre></dd>
     322  </dl>
     323<?php
     324}
     325
     326function simple_mathjax_latex_preamble_input() {
     327  $options = load_options();
     328?>
     329  <textarea id="latex_preamble" cols="50" rows="10" name="simple_mathjax_options[latex_preamble]"><?= $options['latex_preamble'] ?></textarea>
     330  <p>This LaTeX will be run invisibly before any other LaTeX on the page. A good place to put \newcommand's and \renewcommand's</p>
     331  <p><strong>Do not us $ signs</strong>, they will be added for you</p>
     332  <p>E.g.</p>
     333  <pre>\newcommand{\NN}{\mathbb N}
     334\newcommand{\abs}[1]{\left|#1\right|}</pre>
     335<?php
     336}
Note: See TracChangeset for help on using the changeset viewer.