Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 2a7bfab

Browse files
authored
Merge pull request #4 from Learnosity/LRN-28950/FEATURE/support-rtl-plugin
[FEATURE] Support rtl mode
2 parents 9485ba4 + caaa519 commit 2a7bfab

File tree

2 files changed

+121
-60
lines changed

2 files changed

+121
-60
lines changed

classes/Learnosity/Plugin.php

Lines changed: 104 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,121 @@
1010

1111
class Plugin extends BasePlugin
1212
{
13+
/**
14+
* @var bool determines if rtl should be enabled or not
15+
*/
16+
private $is_rtl;
1317

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+
);
5062
wp_enqueue_script(
5163
'learnosity-author',
5264
$lrn_author_api_url,
5365
array(),
5466
null,
55-
false
67+
true
5668
);
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');
66112
register_setting('lrn_api_group', 'lrn_default_type');
67113
register_setting('lrn_api_group', 'lrn_student_prefix');
68-
}
114+
}
69115

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+
}
78124

79-
// Render the settings template
80-
include(__DIR__ . '../../../templates/settings.php');
125+
// Render the settings template
126+
include(__DIR__ . '../../../templates/settings.php');
81127

82-
}
128+
}
83129

84130
}

classes/Learnosity/Shortcodes/Generator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616

1717
class Generator
1818
{
19+
/**
20+
* @var callable callback to set is_rtl in Learnosity/Plugin
21+
*/
22+
private $set_rtl_callback;
1923

20-
public function __construct()
24+
public function __construct(callable $set_rtl_callback)
2125
{
2226
add_shortcode('lrn-items', array(&$this, 'render_items'));
2327
add_shortcode('lrn-item', array(&$this, 'render_item'));
2428
add_shortcode('lrn-submit', array(&$this, 'render_submit'));
2529
add_shortcode('lrn-assess', array(&$this, 'render_assess'));
2630
add_shortcode('lrn-report', array(&$this, 'render_report'));
2731
add_shortcode('lrn-author', array(&$this, 'render_author'));
32+
$this->set_rtl_callback = $set_rtl_callback;
2833
}
2934

3035
public function render_item($attrs)
@@ -36,6 +41,7 @@ public function render_item($attrs)
3641
public function render_items($attrs, $content)
3742
{
3843
$items_embed = new ItemsEmbed($attrs, 'inline', $content);
44+
$this->set_rtl_if_required($attrs);
3945
return $items_embed->render();
4046
}
4147

@@ -48,6 +54,7 @@ public function render_submit($attrs)
4854
public function render_assess($attrs, $content)
4955
{
5056
$assess_embed = new ItemsEmbed($attrs, 'assess', $content);
57+
$this->set_rtl_if_required($attrs);
5158
return $assess_embed->render();
5259
}
5360

@@ -60,6 +67,14 @@ public function render_report($attrs, $content)
6067
public function render_author($attrs, $content)
6168
{
6269
$author_embed = new AuthorEmbed($attrs, $content);
70+
$this->set_rtl_if_required($attrs);
6371
return $author_embed->render();
6472
}
65-
}
73+
74+
private function set_rtl_if_required($attrs)
75+
{
76+
if (isset($attrs['rtl']) && $attrs['rtl'] === 'true') {
77+
call_user_func($this->set_rtl_callback, true);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)