Skip to content

Commit

Permalink
feat: settings section for openedx plugin configuration (#23)
Browse files Browse the repository at this point in the history
* feat: submenu in settings section for openedx config

* fix: added visibility to functions

* fix: jwt token input disabled

* feat: custom sanitize function for url

* docs: add documentation for custom sanitize url

* refactor: deleted temporary value in jwt token input

* refactor: deleted spanish comment in css file

* refactor: sanitazing fields with native wordpress function

* refactor: direct call to sanitize function
  • Loading branch information
julianramirez2 authored Aug 23, 2023
1 parent a54a653 commit 10a31c3
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 9 deletions.
13 changes: 13 additions & 0 deletions admin/css/openedx-woocommerce-plugin-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@
.wp-core-ui .button, .wp-core-ui .button-secondary{
vertical-align: unset;
}

.openedx-jwt-token-input {
flex: 1;
}

#generate-jwt-token {
margin-left: 10px;
}

input[type="text"],
input[type="password"] {
width: 30%;
}
223 changes: 223 additions & 0 deletions admin/views/Openedx_Woocommerce_Plugin_Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php

namespace App\admin\views;

class Openedx_Woocommerce_Plugin_Settings
{

/**
* Add the plugin settings submenu page.
*
* Registers a new administration submenu page under the Settings menu
* for the Open edX plugin settings.
*
* @return void
*/
public function openedx_settings_submenu()
{
add_submenu_page(
'options-general.php',
'Open edX Settings',
'Open edX Sync',
'manage_options',
'openedx-settings',
array($this, 'openedx_settings_page')
);
}

/**
* Output the plugin settings page.
*
* Renders the form and fields for the Open edX plugin settings page.
*
* @return void
*/
public function openedx_settings_page()
{
?>
<div class="wrap">
<h2>Open edX Settings</h2>
<form method="post" action="options.php">
<?php settings_fields('openedx-settings-group'); ?>
<?php do_settings_sections('openedx-settings'); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}

/**
* Register settings and fields.
*
* Uses the Settings API to register the settings section, fields, and options
* for the Open edX plugin configuration.
*
* @return void
*/
public function openedx_settings_init()
{
add_settings_section(
'openedx-settings-section',
'',
array($this, 'openedx_settings_section_callback'),
'openedx-settings'
);

add_settings_field(
'openedx-domain',
'Open edX Domain',
array($this, 'openedx_domain_callback'),
'openedx-settings',
'openedx-settings-section'
);

add_settings_field(
'openedx-client-id',
'Client id',
array($this, 'openedx_client_id_callback'),
'openedx-settings',
'openedx-settings-section'
);

add_settings_field(
'openedx-client-secret',
'Client secret',
array($this, 'openedx_client_secret_callback'),
'openedx-settings',
'openedx-settings-section'
);

add_settings_field(
'openedx-jwt-token',
'JWT Token',
array($this, 'openedx_jwt_token_callback'),
'openedx-settings',
'openedx-settings-section'
);

register_setting(
'openedx-settings-group',
'openedx-domain',
'sanitize_url'
);

register_setting(
'openedx-settings-group',
'openedx-client-id',
'sanitize_text_field'
);

register_setting(
'openedx-settings-group',
'openedx-client-secret',
'sanitize_text_field'
);

register_setting(
'openedx-settings-group',
'openedx-jwt-token',
'sanitize_text_field'
);
}

/**
* Output the domain settings field.
*
* Retrieves the saved domain value and outputs an input field and description text.
*
* @return void
*/
public function openedx_domain_callback()
{
$value = get_option( 'openedx-domain' ); ?>

<input type="text" name="openedx-domain" id="openedx-domain"
value="<?php echo esc_attr( $value ); ?>" required />

<p class='description'>Your Open edX platform's web address.</p>

<?php
}

/**
* Output the client ID settings field.
*
* Retrieves the saved client ID value and outputs an input field and description text.
*
* @return void
*/
public function openedx_client_id_callback()
{
$value = get_option( 'openedx-client-id' ); ?>

<input type="text" name="openedx-client-id" id="openedx-client-id"
value="<?php echo esc_attr( $value ); ?>" required />

<p class="description">Identifier for OAuth application in your Open edX
platform.</p>

<?php
}

/**
* Output the client secret settings field.
*
* Retrieves the saved client secret value and outputs a password input field and description text.
*
* @return void
*/
public function openedx_client_secret_callback()
{

$value = get_option( 'openedx-client-secret' ); ?>

<input type="text" name="openedx-client-secret" id="openedx-client-secret"
value="<?php echo esc_attr( $value ); ?>" required />

<p class="description">
Confidential key for OAuth application in your Open edX platform.
</p>

<?php
}

/**
* Output the JWT token settings field.
*
* Retrieves the saved JWT token value and outputs a text input field, generate token button,
* and description text.
*
* @return void
*/
public function openedx_jwt_token_callback()
{
?>

<div class="openedx-jwt-token-wrapper">

<input class="openedx-jwt-token-input" type="text" name="openedx-jwt-token" id="openedx-jwt-token"
value="" disabled/>

<button class="button" type="button" id="generate-jwt-token">Generate JWT Token</button>

</div>

<p class="description"> Select the Generate Token button to obtain a JWT Token. </p>

<?php
}

/**
* Output introductory text for the settings section.
*
* Echoes text prompting the user to fill in and save the settings.
*
* @return void
*/
public function openedx_settings_section_callback()
{
printf( 'Configuring the necessary parameters here to establish
the connection between this plugin and your Open edX platform.');
}

}
47 changes: 38 additions & 9 deletions includes/Openedx_Woocommerce_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\admin\Openedx_Woocommerce_Plugin_Admin;
use App\public\Openedx_Woocommerce_Plugin_Public;
use App\admin\views\Openedx_Woocommerce_Plugin_Settings;


/**
* The file that defines the core plugin class
Expand Down Expand Up @@ -85,6 +87,7 @@ public function __construct()
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_plugin_settings_hooks();
}

/**
Expand Down Expand Up @@ -161,6 +164,12 @@ private function load_dependencies()
*/
include_once plugin_dir_path(dirname(__FILE__))
. 'utils/Openedx_Utils.php';

/**
*
*/
include_once plugin_dir_path(dirname(__FILE__))
. 'admin/views/Openedx_Woocommerce_Plugin_Settings.php';
}

/**
Expand Down Expand Up @@ -209,20 +218,23 @@ private function define_admin_hooks()
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
$this->loader->add_filter('gettext', $this, 'openedx_plugin_custom_post_message', 10, 3);
$this->loader->wp_enqueue_style($this->plugin_name,
plugin_dir_url(__FILE__)
. '../admin/css/openedx-woocommerce-plugin-admin.css',
array(),
$this->version,
'all');
$this->loader->wp_enqueue_style(
$this->plugin_name,
plugin_dir_url(__FILE__) . '../admin/css/openedx-woocommerce-plugin-admin.css',
array(),
$this->version,
'all'
);

// Redirection from enrollment to order and enrollment to order
$this->loader->add_filter('woocommerce_admin_order_item_headers', $plugin_admin, 'add_custom_column_order_items');
$this->loader->add_action('woocommerce_admin_order_item_values', $plugin_admin, 'add_admin_order_item_values', 10, 3);
$this->loader->add_action('save_post_shop_order', $plugin_admin, 'save_order_meta_data');
$this->loader->add_action('woocommerce_product_options_general_product_data',
$plugin_admin,
'add_custom_product_fields');
$this->loader->add_action(
'woocommerce_product_options_general_product_data',
$plugin_admin,
'add_custom_product_fields'
);
$this->loader->add_action('woocommerce_process_product_meta',
$plugin_admin,
'save_custom_product_fields');
Expand All @@ -246,6 +258,23 @@ private function define_public_hooks()
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
}

/**
* Define the plugin settings hooks.
*
* Initializes the Openedx_Woocommerce_Plugin_Settings class
* and registers its admin menu and settings hooks using the loader.
*
* @return void
*/
private function define_plugin_settings_hooks()
{

$plugin_settings = new Openedx_Woocommerce_Plugin_Settings();

$this->loader->add_action('admin_menu', $plugin_settings, 'openedx_settings_submenu');
$this->loader->add_action('admin_init', $plugin_settings, 'openedx_settings_init');
}

/**
* Modify the message displayed when a custom-post-type is updated
*
Expand Down

0 comments on commit 10a31c3

Please sign in to comment.