Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save content state in browser local storage (settings) #112

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions admin/class-h5p-plugin-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ public function display_settings_page() {
$save_content_frequency = filter_input(INPUT_POST, 'save_content_frequency', FILTER_VALIDATE_INT);
update_option('h5p_save_content_frequency', $save_content_frequency);

$save_content_storages = filter_input(INPUT_POST, 'save_content_storages', FILTER_VALIDATE_INT);
update_option('h5p_save_content_storages', $save_content_storages);

$show_toggle_view_others_h5p_contents = filter_input(INPUT_POST, 'show_toggle_view_others_h5p_contents', FILTER_VALIDATE_INT);
update_option('h5p_show_toggle_view_others_h5p_contents', $show_toggle_view_others_h5p_contents);

Expand Down Expand Up @@ -496,6 +499,7 @@ public function display_settings_page() {
$track_user = get_option('h5p_track_user', TRUE);
$save_content_state = get_option('h5p_save_content_state', FALSE);
$save_content_frequency = get_option('h5p_save_content_frequency', 30);
$save_content_storages = get_option('h5p_save_content_storages', 1);
$show_toggle_view_others_h5p_contents = get_option('h5p_show_toggle_view_others_h5p_contents', 0);
$insert_method = get_option('h5p_insert_method', 'id');
$enable_lrs_content_types = get_option('h5p_enable_lrs_content_types', FALSE);
Expand Down
27 changes: 21 additions & 6 deletions admin/views/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,28 @@
<th scope="row"><?php _e("Save Content State", $this->plugin_slug); ?></th>
<td>
<label>
<input name="save_content_state" type="checkbox" value="true"<?php if ($save_content_state): ?> checked="checked"<?php endif; ?>/>
<?php _e("Allow logged-in users to resume tasks", $this->plugin_slug); ?>
<input class="h5p-visibility-toggler" data-h5p-visibility-subject-selector=".h5p-save-content-options" name="save_content_state" type="checkbox" value="true"<?php if ($save_content_state): ?> checked="checked"<?php endif; ?>/>
<?php _e("Allow users to resume tasks", $this->plugin_slug); ?>
</label>
<p class="h5p-auto-save-freq">
<label for="h5p-freq"><?php _e("Auto-save frequency (in seconds)", $this->plugin_slug); ?></label>
<input id="h5p-freq" name="save_content_frequency" type="text" value="<?php print $save_content_frequency ?>"/>
</p>
</td>
</tr>
<tr valign="top" class="h5p-save-content-options">
<th scope="row"><?php _e("Auto-save frequency (in seconds)", $this->plugin_slug); ?></th>
<td>
<input id="h5p-freq" name="save_content_frequency" type="text" value="<?php print $save_content_frequency ?>"/>
</td>
</tr>
<tr valign="top" class="h5p-save-content-options">
<th scope="row"><?php _e("Users who may resume tasks", $this->plugin_slug); ?></th>
<td>
<select id="h5p_save_content_storages" name="save_content_storages">
<option value="<?php echo H5PSaveContentStorages::DATABASE; ?>" <?php if ($save_content_storages == H5PSaveContentStorages::DATABASE): ?>selected="selected"<?php endif; ?>>
<?php _e("Only users who are logged-in", $this->plugin_slug); ?>
</option>
<option value="<?php echo H5PSaveContentStorages::DATABASE_LOCALSTORAGE; ?>" <?php if ($save_content_storages == H5PSaveContentStorages::DATABASE_LOCALSTORAGE): ?>selected="selected"<?php endif; ?>>
<?php _e("All users", $this->plugin_slug); ?>
</option>
</select>
</td>
</tr>
</tr>
Expand Down
28 changes: 28 additions & 0 deletions public/class-h5p-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ public static function update_database() {
add_option('h5p_track_user', TRUE);
add_option('h5p_save_content_state', FALSE);
add_option('h5p_save_content_frequency', 30);
add_option('h5p_save_content_storages', 1);
add_option('h5p_site_key', get_option('h5p_h5p_site_uuid', FALSE));
add_option('h5p_show_toggle_view_others_h5p_contents', 0);
add_option('h5p_content_type_cache_updated_at', 0);
Expand Down Expand Up @@ -798,6 +799,31 @@ public function get_h5p_url($absolute = FALSE) {
return $absolute ? $url[$id]['abs'] : $url[$id]['rel'];
}

/**
* Get the storages for save content state.
*
* @since 1.15.1
* @return string
*/
public function get_h5p_save_content_storages() {
$save_content_storages = array();

if (get_option('h5p_save_content_state', FALSE) != TRUE) {
return $save_content_storages;
}

$option = get_option('h5p_save_content_storages', FALSE);

if (($option & H5PSaveContentStorages::DATABASE) === H5PSaveContentStorages::DATABASE) {
$save_content_storages['database'] = TRUE;
}
if (($option & H5PSaveContentStorages::LOCALSTORAGE) === H5PSaveContentStorages::LOCALSTORAGE) {
$save_content_storages['localStorage'] = 'WP-bid-' . get_current_blog_id() . '-';
}

return $save_content_storages;
}

/**
* Get H5P language code from WordPress.
*
Expand Down Expand Up @@ -1174,6 +1200,7 @@ public function get_core_settings() {
'contentUserData' => admin_url('admin-ajax.php?token=' . wp_create_nonce('h5p_contentuserdata') . '&action=h5p_contents_user_data&content_id=:contentId&data_type=:dataType&sub_content_id=:subContentId')
),
'saveFreq' => get_option('h5p_save_content_state', FALSE) ? get_option('h5p_save_content_frequency', 30) : FALSE,
'saveContentStorages' => $this->get_h5p_save_content_storages(),
'siteUrl' => get_site_url(),
'l10n' => array(
'H5P' => $core->getLocalization(),
Expand Down Expand Up @@ -1604,6 +1631,7 @@ public static function uninstall() {
delete_option('h5p_ext_communication');
delete_option('h5p_save_content_state');
delete_option('h5p_save_content_frequency');
delete_option('h5p_save_content_storages');
delete_option('h5p_show_toggle_view_others_h5p_contents');
delete_option('h5p_update_available');
delete_option('h5p_current_update');
Expand Down