Move PConfig::get() to DI::pConfig()->get()
[friendica-addons.git/.git] / mathjax / mathjax.php
1 <?php
2 /**
3  * Name: MathJax
4  * Description: Addon for Friendica to include MathJax (LaTeX math syntax)
5  * Version: 2.0
6  * Author: Tobias Diekershoff <https://social.diekershoff.de/profile/tobias>
7  * Author: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
8  * License: 3-clause BSD license
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
16 use Friendica\DI;
17
18 function mathjax_install()
19 {
20         Hook::register('footer'             , __FILE__, 'mathjax_footer');
21         Hook::register('addon_settings'     , __FILE__, 'mathjax_settings');
22         Hook::register('addon_settings_post', __FILE__, 'mathjax_settings_post');
23 }
24
25 function mathjax_uninstall()
26 {
27         Hook::unregister('footer'             , __FILE__, 'mathjax_footer');
28         Hook::unregister('addon_settings'     , __FILE__, 'mathjax_settings');
29         Hook::unregister('addon_settings_post', __FILE__, 'mathjax_settings_post');
30
31         // Legacy hooks
32         Hook::unregister('load_config'        , __FILE__, 'mathjax_load_config');
33         Hook::unregister('page_header'        , __FILE__, 'mathjax_page_header');
34         Hook::unregister('template_vars'      , __FILE__, 'mathjax_template_vars');
35 }
36
37 function mathjax_settings_post($a)
38 {
39         if (!local_user() || empty($_POST['mathjax-submit'])) {
40                 return;
41         }
42
43         PConfig::set(local_user(), 'mathjax', 'use', intval($_POST['mathjax_use']));
44 }
45
46 function mathjax_settings(App $a, &$s)
47 {
48         if (!local_user()) {
49                 return;
50         }
51
52         $use = DI::pConfig()->get(local_user(), 'mathjax', 'use', false);
53
54         $tpl = Renderer::getMarkupTemplate('settings.tpl', __DIR__);
55         $s .= Renderer::replaceMacros($tpl, [
56                 '$title'        => 'MathJax',
57                 '$description'  => L10n::t('The MathJax addon renders mathematical formulae written using the LaTeX syntax surrounded by the usual $$ or an eqnarray block in the postings of your wall,network tab and private mail.'),
58                 '$mathjax_use'  => ['mathjax_use', L10n::t('Use the MathJax renderer'), $use, ''],
59                 '$savesettings' => L10n::t('Save Settings'),
60         ]);
61 }
62
63 function mathjax_footer(App $a, &$b)
64 {
65         //  if the visitor of the page is not a local_user, use MathJax
66         //  otherwise check the users settings.
67         if (!local_user() || DI::pConfig()->get(local_user(), 'mathjax', 'use', false)) {
68                 DI::page()->registerFooterScript(__DIR__ . '/asset/MathJax.js?config=TeX-MML-AM_CHTML');
69                 DI::page()->registerFooterScript(__DIR__ . '/mathjax.js');
70         }
71 }