Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 104 additions & 58 deletions classes/Learnosity/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,121 @@

class Plugin extends BasePlugin
{
/**
* @var bool determines if rtl should be enabled or not
*/
private $is_rtl;

public function __construct()
{
parent::__construct();
new ShortcodesGenerator();
}

public function add_menu()
{
add_options_page(
'Learnosity API Settings',
'Learnosity API',
'manage_options',
'lrn_api',
array(&$this, 'render_plugin_settings_page')
);
}

public function enqueue_scripts()
{
$lrn_items_api_url = get_option('lrn_items_api_url','https://items-va.learnosity.com/?v1');
$lrn_reports_api_url = get_option('lrn_reports_api_url','https://reports-va.learnosity.com/?v1');
$lrn_author_api_url = get_option('lrn_author_api_url','https://authorapi-or.learnosity.com/?v1');
wp_enqueue_script(
'learnosity-items',
$lrn_items_api_url,
array(),
null,
false
);
wp_enqueue_script(
'learnosity-reports',
$lrn_reports_api_url,
array(),
null,
false
);
public function __construct()
{
parent::__construct();
$this->is_rtl = false;
new ShortcodesGenerator(array($this, 'set_rtl_callback'));
}

public function add_menu()
{
add_options_page(
'Learnosity API Settings',
'Learnosity API',
'manage_options',
'lrn_api',
array(&$this, 'render_plugin_settings_page')
);
}

public function enqueue_scripts()
{
$lrn_items_api_url = get_option('lrn_items_api_url','https://items-va.learnosity.com/?v1');
$lrn_reports_api_url = get_option('lrn_reports_api_url','https://reports-va.learnosity.com/?v1');
$lrn_author_api_url = get_option('lrn_author_api_url','https://authorapi-or.learnosity.com/?v1');

/*
* For items and author, in_footer is set to true. This is on purpose as the information, whether
* the read direction is right to left is set in the plugin short-code. If we loaded the scripts
* in the header, they'd be loaded before the short-code scripts are encountered, thus we
* wouldn't know whether to add the rtl flag in the src tag.
*/
wp_enqueue_script(
'learnosity-items',
$lrn_items_api_url,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the indentation change here was checked in by accident? It looked correct before here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation before was partially not with spaces, fixed it in new commit

array(),
null,
true

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to change this in_footer variable to true? If so, how come?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, adding comment:
/*
* For items and author, in_footer is set to true. This is on purpose as the information, whether
* the read direction is right to left is set in the plugin short-code. If we loaded the scripts
* in the header, they'd be loaded before the short-code scripts are encountered, thus we
* wouldn't know whether to add the rtl flag in the src tag.
*/

);
wp_enqueue_script(
'learnosity-reports',
$lrn_reports_api_url,
array(),
null,
false
);
wp_enqueue_script(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation of this function call is wrong, so would be good to fix here thanks

'learnosity-author',
$lrn_author_api_url,
array(),
null,
false
true
);
}

public function init_settings()
{
register_setting('lrn_api_group', 'lrn_consumer_key');
register_setting('lrn_api_group', 'lrn_consumer_secret');
register_setting('lrn_api_group', 'lrn_author_api_url');
register_setting('lrn_api_group', 'lrn_items_api_url');
register_setting('lrn_api_group', 'lrn_reports_api_url');

// Before the script tags are added to page, add the rtl data attribute

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation wrong here, too

add_filter(
'script_loader_tag',
array($this, 'add_rtl_data'),
10,
3
);
}

/**
* Adds data-lrn-dir="rtl" to the source tag if $this->is_rtl is true
*
* @param $tag
* @param $handle
* @param $src
* @return string
*/
public function add_rtl_data($tag, $handle, $src)
{
if ($this->is_rtl) {
$tag = '<script type="text/javascript" src="' . esc_url($src) . '" data-lrn-dir="rtl"></script>';
}
return $tag;
}

/**
* Sets $this->is_rtl to $value. Used as a callback in Generator
*
* @param $value
*/
public function set_rtl_callback($value)
{
$this->is_rtl = $value;
}

public function init_settings()
{
register_setting('lrn_api_group', 'lrn_consumer_key');
register_setting('lrn_api_group', 'lrn_consumer_secret');
register_setting('lrn_api_group', 'lrn_author_api_url');
register_setting('lrn_api_group', 'lrn_items_api_url');
register_setting('lrn_api_group', 'lrn_reports_api_url');
register_setting('lrn_api_group', 'lrn_default_type');
register_setting('lrn_api_group', 'lrn_student_prefix');
}
}

/**
* Menu Callback
*/
public function render_plugin_settings_page()
{
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
/**
* Menu Callback
*/
public function render_plugin_settings_page()
{
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}

// Render the settings template
include(__DIR__ . '../../../templates/settings.php');
// Render the settings template
include(__DIR__ . '../../../templates/settings.php');

}
}

}
19 changes: 17 additions & 2 deletions classes/Learnosity/Shortcodes/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

class Generator
{
/**
* @var callable callback to set is_rtl in Learnosity/Plugin
*/
private $set_rtl_callback;

public function __construct()
public function __construct(callable $set_rtl_callback)
{
add_shortcode('lrn-items', array(&$this, 'render_items'));
add_shortcode('lrn-item', array(&$this, 'render_item'));
add_shortcode('lrn-submit', array(&$this, 'render_submit'));
add_shortcode('lrn-assess', array(&$this, 'render_assess'));
add_shortcode('lrn-report', array(&$this, 'render_report'));
add_shortcode('lrn-author', array(&$this, 'render_author'));
$this->set_rtl_callback = $set_rtl_callback;
}

public function render_item($attrs)
Expand All @@ -36,6 +41,7 @@ public function render_item($attrs)
public function render_items($attrs, $content)
{
$items_embed = new ItemsEmbed($attrs, 'inline', $content);
$this->set_rtl_if_required($attrs);
return $items_embed->render();
}

Expand All @@ -48,6 +54,7 @@ public function render_submit($attrs)
public function render_assess($attrs, $content)
{
$assess_embed = new ItemsEmbed($attrs, 'assess', $content);
$this->set_rtl_if_required($attrs);
return $assess_embed->render();
}

Expand All @@ -60,6 +67,14 @@ public function render_report($attrs, $content)
public function render_author($attrs, $content)
{
$author_embed = new AuthorEmbed($attrs, $content);
$this->set_rtl_if_required($attrs);
return $author_embed->render();
}
}

private function set_rtl_if_required($attrs)
{
if (isset($attrs['rtl']) && $attrs['rtl'] === 'true') {
call_user_func($this->set_rtl_callback, true);
}
}
}