|
10 | 10 |
|
11 | 11 | class Plugin extends BasePlugin
|
12 | 12 | {
|
| 13 | + /** |
| 14 | + * @var bool determines if rtl should be enabled or not |
| 15 | + */ |
| 16 | + private $is_rtl; |
13 | 17 |
|
14 |
| - public function __construct() |
15 |
| - { |
16 |
| - parent::__construct(); |
17 |
| - new ShortcodesGenerator(); |
18 |
| - } |
19 |
| - |
20 |
| - public function add_menu() |
21 |
| - { |
22 |
| - add_options_page( |
23 |
| - 'Learnosity API Settings', |
24 |
| - 'Learnosity API', |
25 |
| - 'manage_options', |
26 |
| - 'lrn_api', |
27 |
| - array(&$this, 'render_plugin_settings_page') |
28 |
| - ); |
29 |
| - } |
30 |
| - |
31 |
| - public function enqueue_scripts() |
32 |
| - { |
33 |
| - $lrn_items_api_url = get_option('lrn_items_api_url','https://items-va.learnosity.com/?v1'); |
34 |
| - $lrn_reports_api_url = get_option('lrn_reports_api_url','https://reports-va.learnosity.com/?v1'); |
35 |
| - $lrn_author_api_url = get_option('lrn_author_api_url','https://authorapi-or.learnosity.com/?v1'); |
36 |
| - wp_enqueue_script( |
37 |
| - 'learnosity-items', |
38 |
| - $lrn_items_api_url, |
39 |
| - array(), |
40 |
| - null, |
41 |
| - false |
42 |
| - ); |
43 |
| - wp_enqueue_script( |
44 |
| - 'learnosity-reports', |
45 |
| - $lrn_reports_api_url, |
46 |
| - array(), |
47 |
| - null, |
48 |
| - false |
49 |
| - ); |
| 18 | + public function __construct() |
| 19 | + { |
| 20 | + parent::__construct(); |
| 21 | + $this->is_rtl = false; |
| 22 | + new ShortcodesGenerator(array($this, 'set_rtl_callback')); |
| 23 | + } |
| 24 | + |
| 25 | + public function add_menu() |
| 26 | + { |
| 27 | + add_options_page( |
| 28 | + 'Learnosity API Settings', |
| 29 | + 'Learnosity API', |
| 30 | + 'manage_options', |
| 31 | + 'lrn_api', |
| 32 | + array(&$this, 'render_plugin_settings_page') |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function enqueue_scripts() |
| 37 | + { |
| 38 | + $lrn_items_api_url = get_option('lrn_items_api_url','https://items-va.learnosity.com/?v1'); |
| 39 | + $lrn_reports_api_url = get_option('lrn_reports_api_url','https://reports-va.learnosity.com/?v1'); |
| 40 | + $lrn_author_api_url = get_option('lrn_author_api_url','https://authorapi-or.learnosity.com/?v1'); |
| 41 | + |
| 42 | + /* |
| 43 | + * For items and author, in_footer is set to true. This is on purpose as the information, whether |
| 44 | + * the read direction is right to left is set in the plugin short-code. If we loaded the scripts |
| 45 | + * in the header, they'd be loaded before the short-code scripts are encountered, thus we |
| 46 | + * wouldn't know whether to add the rtl flag in the src tag. |
| 47 | + */ |
| 48 | + wp_enqueue_script( |
| 49 | + 'learnosity-items', |
| 50 | + $lrn_items_api_url, |
| 51 | + array(), |
| 52 | + null, |
| 53 | + true |
| 54 | + ); |
| 55 | + wp_enqueue_script( |
| 56 | + 'learnosity-reports', |
| 57 | + $lrn_reports_api_url, |
| 58 | + array(), |
| 59 | + null, |
| 60 | + false |
| 61 | + ); |
50 | 62 | wp_enqueue_script(
|
51 | 63 | 'learnosity-author',
|
52 | 64 | $lrn_author_api_url,
|
53 | 65 | array(),
|
54 | 66 | null,
|
55 |
| - false |
| 67 | + true |
56 | 68 | );
|
57 |
| - } |
58 |
| - |
59 |
| - public function init_settings() |
60 |
| - { |
61 |
| - register_setting('lrn_api_group', 'lrn_consumer_key'); |
62 |
| - register_setting('lrn_api_group', 'lrn_consumer_secret'); |
63 |
| - register_setting('lrn_api_group', 'lrn_author_api_url'); |
64 |
| - register_setting('lrn_api_group', 'lrn_items_api_url'); |
65 |
| - register_setting('lrn_api_group', 'lrn_reports_api_url'); |
| 69 | + |
| 70 | + // Before the script tags are added to page, add the rtl data attribute |
| 71 | + add_filter( |
| 72 | + 'script_loader_tag', |
| 73 | + array($this, 'add_rtl_data'), |
| 74 | + 10, |
| 75 | + 3 |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Adds data-lrn-dir="rtl" to the source tag if $this->is_rtl is true |
| 81 | + * |
| 82 | + * @param $tag |
| 83 | + * @param $handle |
| 84 | + * @param $src |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + public function add_rtl_data($tag, $handle, $src) |
| 88 | + { |
| 89 | + if ($this->is_rtl) { |
| 90 | + $tag = '<script type="text/javascript" src="' . esc_url($src) . '" data-lrn-dir="rtl"></script>'; |
| 91 | + } |
| 92 | + return $tag; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets $this->is_rtl to $value. Used as a callback in Generator |
| 97 | + * |
| 98 | + * @param $value |
| 99 | + */ |
| 100 | + public function set_rtl_callback($value) |
| 101 | + { |
| 102 | + $this->is_rtl = $value; |
| 103 | + } |
| 104 | + |
| 105 | + public function init_settings() |
| 106 | + { |
| 107 | + register_setting('lrn_api_group', 'lrn_consumer_key'); |
| 108 | + register_setting('lrn_api_group', 'lrn_consumer_secret'); |
| 109 | + register_setting('lrn_api_group', 'lrn_author_api_url'); |
| 110 | + register_setting('lrn_api_group', 'lrn_items_api_url'); |
| 111 | + register_setting('lrn_api_group', 'lrn_reports_api_url'); |
66 | 112 | register_setting('lrn_api_group', 'lrn_default_type');
|
67 | 113 | register_setting('lrn_api_group', 'lrn_student_prefix');
|
68 |
| - } |
| 114 | + } |
69 | 115 |
|
70 |
| - /** |
71 |
| - * Menu Callback |
72 |
| - */ |
73 |
| - public function render_plugin_settings_page() |
74 |
| - { |
75 |
| - if (!current_user_can('manage_options')) { |
76 |
| - wp_die(__('You do not have sufficient permissions to access this page.')); |
77 |
| - } |
| 116 | + /** |
| 117 | + * Menu Callback |
| 118 | + */ |
| 119 | + public function render_plugin_settings_page() |
| 120 | + { |
| 121 | + if (!current_user_can('manage_options')) { |
| 122 | + wp_die(__('You do not have sufficient permissions to access this page.')); |
| 123 | + } |
78 | 124 |
|
79 |
| - // Render the settings template |
80 |
| - include(__DIR__ . '../../../templates/settings.php'); |
| 125 | + // Render the settings template |
| 126 | + include(__DIR__ . '../../../templates/settings.php'); |
81 | 127 |
|
82 |
| - } |
| 128 | + } |
83 | 129 |
|
84 | 130 | }
|
0 commit comments