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

feat: enable redirection button to order or enrollment request #20

Merged
merged 11 commits into from
Aug 15, 2023
Merged
340 changes: 205 additions & 135 deletions admin/Openedx_Woocommerce_Plugin_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,41 @@ class Openedx_Woocommerce_Plugin_Admin
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
* @param string $test Flag variable to know if it is a test.
*/
public function __construct($plugin_name, $version, $test = null)
{

$this->plugin_name = $plugin_name;
$this->version = $version;
if (!$test) {
$this->createEnrollmentClass();
}
}

/**
* Create an instance of the Openedx_Woocommerce_Plugin_Enrollment class.
*
* @since 1.0.0
* @return void
*/
public function createEnrollmentClass()
{

$this->openedx_enrollment = new Openedx_Woocommerce_Plugin_Enrollment($this);
}
* @since 1.0.0
*
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
* @param string $test Flag variable to know if it is a test.
*/
public function __construct($plugin_name, $version, $test = null)
{

$this->plugin_name = $plugin_name;
$this->version = $version;
if (!$test) {
$this->createEnrollmentClass();
}
}

/**
* Create an instance of the Openedx_Woocommerce_Plugin_Enrollment class.
*
* @since 1.0.0
*
* @return void
*/
public function createEnrollmentClass()
{
$this->openedx_enrollment = new Openedx_Woocommerce_Plugin_Enrollment($this);
}

/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function enqueue_styles()
{
public function enqueue_styles()
{

/**
* This function is provided for demonstration purposes only.
Expand All @@ -109,137 +110,206 @@ public function enqueue_styles()

//wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/openedx-woocommerce-plugin-admin.css', array(), $this->version, 'all' );

}

/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*
* @return void
*/
public function enqueue_scripts()
{

/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Openedx_Woocommerce_Plugin_Loader as
* all of the hooks are defined in that particular class.
*
* The Openedx_Woocommerce_Plugin_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
}
}

/**
* Register the JavaScript for the admin area.
*
* @since 1.0.0
*
* @return void
*/
public function enqueue_scripts()
{

/**
* This function is provided for demonstration purposes only.
*
* An instance of this class should be passed to the run() function
* defined in Openedx_Woocommerce_Plugin_Loader as
* all of the hooks are defined in that particular class.
*
* The Openedx_Woocommerce_Plugin_Loader will then create the relationship
* between the defined hooks and the functions defined in this
* class.
*/
}

/**
* Register Enrollment Request custom post type
*
* @since 1.0.0
*/
public function register_enrollment_custom_post_type()
{
$this->openedx_enrollment->register_enrollment_custom_post_type();
}

/**
* Register Enrollment Request custom post type
* Render Enrollment Request info form
*
* @since 1.0.0
*/
public function register_enrollment_custom_post_type()
{
public function render_enrollment_info_form($post)
{
$this->openedx_enrollment_info_form = new Openedx_Woocommerce_Plugin_Enrollment_Info_Form($post);
}

/**
* Wrapper function to register a new post type
*
* @param string $post_type Post type name.
* @param string $plural Post type item plural name.
* @param string $single Post type item single name.
* @param string $description Description of post type.
* @return object Post type class object
*/
public function register_post_type($post_type = '', $plural = '', $single = '', $description = '', $options = array())
{

if (!$post_type || !$plural || !$single) {
return;
}

$post_type = $this->createPostType($post_type, $plural, $single, $description, $options);

return $post_type;
}

/**
* Create a new instance of the Openedx_Woocommerce_Plugin_Post_Type class and register a new post type.
*
* @param string $post_type Post type name.
* @param string $plural Post type item plural name.
* @param string $single Post type item single name.
* @param string $description Description of the post type.
* @param array $options Additional options for the post type.
* @return object Post type class object.
*/
public function createPostType(
$post_type = '',
$plural = '',
$single = '',
$description = '',
$options = array()
){
return new Openedx_Woocommerce_Plugin_Post_Type($post_type, $plural, $single, $description, $options);
}

/**
* Register course ID and mode fields for product
*
* @since 1.1.1
*/
function add_custom_product_fields()
{
global $post;

echo '<div class="options_group">';

echo '<p class="form-field">'
. __('Only use these fields if the product is an Open edX course.', 'woocommerce')
. '</p>';

woocommerce_wp_text_input(array(
'id' => '_course_id',
'label' => __('Open edX Course ID', 'woocommerce'),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __(
'Ex: course-v1:edX+DemoX+Demo_Course.
<br><br> You can find the Open edX Course ID
in the URL of your course in your LMS.',
'woocommerce')
));

woocommerce_wp_select(array(
'id' => '_mode',
'label' => __('Open edX Course Mode', 'woocommerce'),
'desc_tip' => 'true',
'description' => __(
'Select the mode for your course.
Make sure to set a mode that your course has.',
'woocommerce'),
'options' => utils\get_enrollment_options(),
));

echo '</div>';
}

$this->openedx_enrollment->register_enrollment_custom_post_type();
}

/**
* Render Enrollment Request info form
*
* @since 1.0.0
* Create a custom column in order items table inside an order
*
* @param array $columns Array of order items table columns
*
* @return void
*/
public function render_enrollment_info_form($post)
function add_custom_column_order_items($columns)
{

$this->openedx_enrollment_info_form = new Openedx_Woocommerce_Plugin_Enrollment_Info_Form($post);
$column_name = 'Related Enrollment Request';
echo '<th>' . $column_name . '</th>';
}

/**
* Wrapper function to register a new post type
*
* @param string $post_type Post type name.
* @param string $plural Post type item plural name.
* @param string $single Post type item single name.
* @param string $description Description of post type.
* @return object Post type class object
* Create a custom input in the new column in order items table
* to store the enrollment id and a link to the enrollment request
*
* @param array $_product Product object
* @param array $item Order item
* @param int $item_id Order item id
*
* @return void
*/
public function register_post_type($post_type = '', $plural = '', $single = '', $description = '', $options = array())
function add_admin_order_item_values($_product, $item, $item_id = null)
{
// Check if the product has a non-empty "_course_id" metadata
$_course_id = get_post_meta($_product->get_id(), '_course_id', true);

if (!$post_type || !$plural || !$single) {
return;
}
if (!empty($_course_id)) {
$order_id = method_exists($item, 'get_order_id') ? $item->get_order_id() : $item['order_id'];
$input_value = get_post_meta($order_id, 'enrollment_id' . $item_id, true);
$order_url = admin_url('post.php?post=' . intval($input_value) . '&action=edit');

$post_type = $this->createPostType($post_type, $plural, $single, $description, $options);
$html_output = '<td>';
$html_output .= '<input type="text" name="order_id_input' . $item_id . '" value="' . esc_attr($input_value) . '" pattern="\d*" />';
$html_output .= '<a href="' . esc_url($order_url) . '" class="button" style="margin-left: 5px; ' . ($input_value ? '' : 'pointer-events: none; opacity: 0.6;') . '">View Request</a>';
$html_output .= '</td>';

return $post_type;
echo $html_output;
}
}

/**
* Create a new instance of the Openedx_Woocommerce_Plugin_Post_Type class and register a new post type.
*
* @param string $post_type Post type name.
* @param string $plural Post type item plural name.
* @param string $single Post type item single name.
* @param string $description Description of the post type.
* @param array $options Additional options for the post type.
* @return object Post type class object.
* Save the enrollment id in the order meta data
*
* @param int $order_id Order id
*
* @return void
*/
public function createPostType($post_type = '', $plural = '', $single = '', $description = '', $options = array())
function save_order_meta_data($order_id)
{
$items = wc_get_order($order_id)->get_items();

return new Openedx_Woocommerce_Plugin_Post_Type($post_type, $plural, $single, $description, $options);
}

/**
* Register course ID and mode fields for product
*
* @since 1.1.1
*/
function add_custom_product_fields()
{
global $post;

echo '<div class="options_group">';

echo '<p class="form-field">' . __('Only use these fields if the product is an Open edX course.', 'woocommerce') . '</p>';

woocommerce_wp_text_input(array(
'id' => '_course_id',
'label' => __('Open edX Course ID', 'woocommerce'),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __('Ex: course-v1:edX+DemoX+Demo_Course.
<br><br> You can find the Open edX Course ID in the URL of your course in your LMS.',
'woocommerce')
));

woocommerce_wp_select(array(
'id' => '_mode',
'label' => __('Open edX Course Mode', 'woocommerce'),
'desc_tip' => 'true',
'description' => __('Select the mode for your course.
Make sure to set a mode that your course has.',
'woocommerce'),
'options' => utils\get_enrollment_options(),
));

echo '</div>';
foreach ($items as $item_id => $item) {
if (isset($_POST['order_id_input' . $item_id])) {
$input_value = sanitize_text_field($_POST['order_id_input' . $item_id]);
update_post_meta($order_id, 'enrollment_id' . $item_id, $input_value);
}
}
}

/**
* Save course ID and mode fields for product
*
* @since 1.1.1
*/
function save_custom_product_fields($post_id)
{
$course_id = isset($_POST['_course_id']) ? sanitize_text_field($_POST['_course_id']) : '';
$mode = isset($_POST['_mode']) ? sanitize_text_field($_POST['_mode']) : '';

update_post_meta($post_id, '_course_id', $course_id);
update_post_meta($post_id, '_mode', $mode);
}
/**
* Save course ID and mode fields for product
*
* @since 1.1.1
*/
function save_custom_product_fields($post_id)
{
$course_id = isset($_POST['_course_id']) ? sanitize_text_field($_POST['_course_id']) : '';
$mode = isset($_POST['_mode']) ? sanitize_text_field($_POST['_mode']) : '';

update_post_meta($post_id, '_course_id', $course_id);
update_post_meta($post_id, '_mode', $mode);
}
}
4 changes: 4 additions & 0 deletions admin/css/openedx-woocommerce-plugin-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
.logs_box strong {
font-weight: bold;
}

.wp-core-ui .button, .wp-core-ui .button-secondary{
vertical-align: unset;
}
Loading