From ab972ecbbe51d1a17e557eb0805bbd9b7f0f2db2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juli=C3=A1n=20Ramirez=20Giraldo?=
<52968528+julianramirez2@users.noreply.github.com>
Date: Fri, 21 Jul 2023 14:25:35 -0500
Subject: [PATCH] fix: enrollment crud operations working correctly (#15)
* fix: crud saving information and allowing to modify it
* fix: checking the POST variables exist in the form
* fix: all enrollment form fields can be changed
* refactor: api code cleanup
* fix: deleted email form field
* fix: update request title when updating name, course id or mode
* fix: update post method name updated based on its functionality
* fix: revert email field and remove username field
* fix: phpcs requirements in admin file
* fix: fixed number of characters and deleted unused line
---
admin/Openedx_Woocommerce_Plugin_Admin.php | 96 +++--
...oocommerce_Plugin_Enrollment_Info_Form.php | 31 +-
.../Openedx_Woocommerce_Plugin_Enrollment.php | 351 ++----------------
3 files changed, 83 insertions(+), 395 deletions(-)
diff --git a/admin/Openedx_Woocommerce_Plugin_Admin.php b/admin/Openedx_Woocommerce_Plugin_Admin.php
index 335df61..b435609 100644
--- a/admin/Openedx_Woocommerce_Plugin_Admin.php
+++ b/admin/Openedx_Woocommerce_Plugin_Admin.php
@@ -57,28 +57,27 @@ 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) {
+ * @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
- */
- public function createEnrollmentClass() {
-
+ if( !$test ) {
+ $this->createEnrollmentClass();
+ }
+ }
+
+ /**
+ * Create an instance of the Openedx_Woocommerce_Plugin_Enrollment class.
+ *
+ * @since 1.0.0
+ */
+ public function createEnrollmentClass() {
+
$this->openedx_enrollment = new Openedx_Woocommerce_Plugin_Enrollment( $this );
}
@@ -109,7 +108,9 @@ public function enqueue_styles() {
/**
* Register the JavaScript for the admin area.
*
- * @since 1.0.0
+ * @since 1.0.0
+ *
+ * @return void
*/
public function enqueue_scripts() {
@@ -117,24 +118,22 @@ 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.
+ * 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.
*/
-
- //wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/openedx-woocommerce-plugin-admin.js', array( 'jquery' ), $this->version, false );
-
+
}
- /**
- * Register Enrollment Request custom post type
- *
- * @since 1.0.0
- */
- public function register_enrollment_custom_post_type() {
+ /**
+ * 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();
@@ -145,9 +144,9 @@ public function register_enrollment_custom_post_type() {
*
* @since 1.0.0
*/
- public function render_enrollment_info_form() {
+ public function render_enrollment_info_form($post) {
- $this->openedx_enrollment_info_form = new Openedx_Woocommerce_Plugin_Enrollment_Info_Form($this->openedx_enrollment);
+ $this->openedx_enrollment_info_form = new Openedx_Woocommerce_Plugin_Enrollment_Info_Form($post);
}
@@ -166,26 +165,25 @@ public function register_post_type( $post_type = '', $plural = '', $single = '',
return;
}
- $post_type = $this->createPostType( $post_type, $plural, $single, $description, $options );
-
+ $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 );
+ /**
+ * 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);
+ }
}
diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php
index f23e04b..8f65071 100644
--- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php
+++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php
@@ -43,13 +43,12 @@ public function render_enrollment_info_form( $post ) {
$course_id = get_post_meta( $post_id, 'course_id', true );
$email = get_post_meta( $post_id, 'email', true );
- $username = get_post_meta( $post_id, 'username', true );
$mode = get_post_meta( $post_id, 'mode', true );
$is_active = get_post_meta( $post_id, 'is_active', true );
$order_id = get_post_meta( $post_id, 'order_id', true );
$new_enrollment = false;
- if ( ! $course_id && ! $email && ! $username ) {
+ if ( ! $course_id && ! $email ) {
$new_enrollment = true;
}
?>
@@ -63,35 +62,17 @@ public function render_enrollment_info_form( $post ) {
value="">
-
-
-
- value="">
-
-
-
-
- title="You only need to fill one. Either the email or username"
- value="">
+
+
+
diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php
index 437993c..21cc719 100644
--- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php
+++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php
@@ -67,6 +67,10 @@ public function register_enrollment_custom_post_type(){
public function unregister_save_hook() {
remove_action( 'save_post', array( $this, 'save_action' ), 10, 3 );
}
+
+ public function register_save_hook() {
+ add_action( 'save_post', array( $this, 'save_action' ), 10, 3 );
+ }
/**
* Creates specific status for the post type
@@ -142,15 +146,14 @@ public function save_action( $post_id, $post, $update ) {
}
$enrollment_arr = array(
- 'enrollment_course_id' => sanitize_text_field( $_POST['enrollment_course_id'] ),
- 'enrollment_email' => sanitize_text_field( $_POST['enrollment_email'] ),
- 'enrollment_username' => sanitize_text_field( $_POST['enrollment_username'] ),
- 'enrollment_mode' => sanitize_text_field( $_POST['enrollment_mode'] ),
- 'enrollment_request_type' => sanitize_text_field( $_POST['enrollment_request_type'] ),
- 'enrollment_order_id' => sanitize_text_field( $_POST['enrollment_order_id'] ),
+ 'enrollment_course_id' => sanitize_text_field($_POST['enrollment_course_id'] ?? ''),
+ 'enrollment_email' => sanitize_text_field($_POST['enrollment_email'] ?? ''),
+ 'enrollment_mode' => sanitize_text_field($_POST['enrollment_mode'] ?? ''),
+ 'enrollment_request_type' => sanitize_text_field($_POST['enrollment_request_type'] ?? ''),
+ 'enrollment_order_id' => sanitize_text_field($_POST['enrollment_order_id'] ?? ''),
);
-
- $enrollment_action = sanitize_text_field( $_POST['enrollment_action'] );
+
+ $enrollment_action = sanitize_text_field( $_POST['enrollment_action'] ?? '' );
$this->save_enrollment( $post, $enrollment_arr, $enrollment_action );
}
@@ -190,13 +193,17 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {
$enrollment_course_id = $enrollment_arr['enrollment_course_id'];
$enrollment_email = $enrollment_arr['enrollment_email'];
- $enrollment_username = $enrollment_arr['enrollment_username'];
$enrollment_mode = $enrollment_arr['enrollment_mode'];
$enrollment_request_type = $enrollment_arr['enrollment_request_type'];
$enrollment_order_id = $enrollment_arr['enrollment_order_id'];
+ // Get the old post metadata.
+ $old_course_id = get_post_meta($post_id, 'course_id', true);
+ $old_email = get_post_meta($post_id, 'email', true);
+ $old_mode = get_post_meta($post_id, 'mode', true);
+
// We need to have all 3 required params to continue.
- $enrollment_user_reference = $enrollment_email || $enrollment_username;
+ $enrollment_user_reference = $enrollment_email;
if ( ! $enrollment_course_id || ! $enrollment_user_reference || ! $enrollment_mode ) {
return;
}
@@ -204,7 +211,6 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {
// Update the $post metadata.
update_post_meta( $post_id, 'course_id', $enrollment_course_id );
update_post_meta( $post_id, 'email', $enrollment_email );
- update_post_meta( $post_id, 'username', $enrollment_username );
update_post_meta( $post_id, 'mode', $enrollment_mode );
update_post_meta( $post_id, 'order_id', $enrollment_order_id );
@@ -215,175 +221,16 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {
update_post_meta( $post_id, 'is_active', false );
}
- // Only update the post status if it has no custom status yet.
- if ( $post->post_status !== 'enrollment-success' && $post->post_status !== 'enrollment-pending' && $post->post_status !== 'enrollment-error' ) {
- $this->update_post_status( 'enrollment-pending', $post_id );
- }
- }
-
- /**
- * Save post metadata when a post is saved.
- *
- * @param int $post_id The post ID.
- * @param bool $force Does this order need processing by force?.
- */
- public function process_request( $post_id, $force, $do_pre_enroll = true ) {
-
- $user_args = $this->prepare_args( $post_id, 'user' );
- $user = WP_EoxCoreApi()->get_user_info( $user_args );
-
- // If the user doesn't exist create pre-enrollment with the email provided.
- if ( is_wp_error( $user ) && $do_pre_enroll ) {
- if ( ! empty( $user_args['email'] ) ) {
- $pre_enrollment_args = $this->prepare_args( $post_id, 'pre-enrollment' );
- $this->create_pre_enrollment( $post_id, $pre_enrollment_args );
- return;
- } else {
- // TODO Polish error message display.
- update_post_meta( $post_id, 'errors', 'A valid username or email is needed.' );
- $this->update_post_status( 'enrollment-error', $post_id );
- return;
- }
- }
- $enrollment_args = $this->prepare_args( $post_id, 'enrollment' );
- $enrollment_args['force'] = $force;
-
- // When we reach this line we already have the user as a response from the API.
- // Better use that username in case anything from before does not match.
- $enrollment_args['username'] = $user->username;
-
- $enrollment = WP_EoxCoreApi()->get_enrollment( $enrollment_args );
-
- // If the enrollment already exists update it.
- if ( is_wp_error( $enrollment ) ) {
- $this->create_enrollment( $post_id, $enrollment_args );
- } else {
- $this->update_enrollment( $post_id, $enrollment_args );
- }
- }
-
- /**
- * Prepare args to be passed to the api calls
- *
- * @param int $post_id The post ID.
- * @param string $type The args type to be prepared (user, enrollment, ..).
- *
- * @return array args args ready to be pass
- */
- public function prepare_args( $post_id, $type ) {
-
- $args = array();
- $user_args = array_filter(
- array(
- 'email' => get_post_meta( $post_id, 'email', true ),
- 'username' => get_post_meta( $post_id, 'username', true ),
- )
- );
-
- $enrollment_args = array(
- 'course_id' => get_post_meta( $post_id, 'course_id', true ),
- );
-
- $enrollment_opts_args = array(
- 'mode' => get_post_meta( $post_id, 'mode', true ),
- 'is_active' => ( get_post_meta( $post_id, 'is_active', true ) ? true : false ),
- );
-
- switch ( $type ) {
- case 'user':
- return $user_args;
- case 'enrollment':
- // By the API design enrollment operations work on usernames.
- unset( $user_args['email'] );
- return array_merge( $user_args, $enrollment_args, $enrollment_opts_args );
- case 'pre-enrollment' or 'basic enrollment':
- return array_merge( $user_args, $enrollment_args );
- }
-
- return $args;
- }
-
- /**
- * Update post metadata when a post is synced.
- *
- * @param int $post_id The post ID.
- */
- public function sync_request( $post_id ) {
+ // Check if old post_meta tags are different from the new ones.
- $args = $this->prepare_args( $post_id, 'basic enrollment' );
- $response = WP_EoxCoreApi()->get_enrollment( $args );
-
- if ( is_wp_error( $response ) ) {
- update_post_meta( $post_id, 'errors', $response->get_error_message() );
-
- // Update Status.
- $this->update_post_status( 'enrollment-error', $post_id );
-
- } else {
- delete_post_meta( $post_id, 'errors' );
-
- // Only this fields can be updated.
- update_post_meta( $post_id, 'mode', $response->mode );
- update_post_meta( $post_id, 'is_active', $response->is_active );
-
- // This fields should be updated if empty.
- if ( ! get_post_meta( $post_id, 'username', true ) ) {
- update_post_meta( $post_id, 'username', $response->username );
- }
- if ( ! get_post_meta( $post_id, 'email', true ) ) {
- $user_args = $this->prepare_args( $post_id, 'user' );
- $user = WP_EoxCoreApi()->get_user_info( $user_args );
- update_post_meta( $post_id, 'email', $user->email );
- }
-
- // Update Status.
- $this->update_post_status( 'enrollment-success', $post_id );
- }
- }
-
-
- /**
- * Create enrollment.
- *
- * @param int $post_id The post ID.
- * @param array $args The request parameters to be sent to the api.
- */
- public function create_enrollment( $post_id, $args ) {
-
- $response = WP_EoxCoreApi()->create_enrollment( $args );
-
- if ( is_wp_error( $response ) ) {
- update_post_meta( $post_id, 'errors', $response->get_error_message() );
- $status = 'enrollment-error';
- } else {
- delete_post_meta( $post_id, 'errors' );
- // This field should be updated if empty.
- if ( ! get_post_meta( $post_id, 'username', true ) ) {
- update_post_meta( $post_id, 'username', $response->username );
- }
- $status = 'enrollment-success';
+ if ($old_course_id !== $enrollment_course_id || $old_email !== $enrollment_email || $old_mode !== $enrollment_mode) {
+ $this->update_post($post_id);
}
- $this->update_post_status( $status, $post_id );
- }
-
- /**
- * Create pre-enrollment.
- *
- * @param int $post_id The post ID.
- * @param array $args The request parameters to be sent to the api.
- */
- public function create_pre_enrollment( $post_id, $args ) {
- $response = WP_EoxCoreApi()->create_pre_enrollment( $args );
-
- if ( is_wp_error( $response ) ) {
- update_post_meta( $post_id, 'errors', $response->get_error_message() );
- $status = 'enrollment-error';
- } else {
- update_post_meta( $post_id, 'errors', 'The provided user does not exist. A pre-enrollment for ' . $response->email . ' was created instead. ' );
- $status = 'enrollment-success';
+ // Only update the post status if it has no custom status yet.
+ if ( $post->post_status !== 'enrollment-success' && $post->post_status !== 'enrollment-pending' && $post->post_status !== 'enrollment-error' ) {
+ $this->update_post( $post_id, 'enrollment-pending' );
}
- $this->update_post_status( $status, $post_id );
}
/**
@@ -392,159 +239,21 @@ public function create_pre_enrollment( $post_id, $args ) {
* @param string $status The status of the request.
* @param int $post_id The post ID.
*/
- public function update_post_status( $status, $post_id ) {
+ public function update_post( $post_id, $status = null ) {
$enrollment_course_id = get_post_meta( $post_id, 'course_id', true );
- $enrollment_username = get_post_meta( $post_id, 'username', true );
+ $enrollment_email = get_post_meta( $post_id, 'email', true );
$enrollment_mode = get_post_meta( $post_id, 'mode', true );
$post_update = array(
'ID' => $post_id,
- 'post_status' => $status,
- 'post_title' => $enrollment_course_id . ' | ' . $enrollment_username . ' | Mode: ' . $enrollment_mode,
+ 'post_title' => $enrollment_course_id . ' | ' . $enrollment_email . ' | Mode: ' . $enrollment_mode,
);
-
- $this->wp_update_post( $post_update );
- }
-
-
- /**
- * Update enrollment.
- *
- * @param int $post_id The post ID.
- * @param array $args The request parameters to be sent to the api.
- */
- public function update_enrollment( $post_id, $args ) {
- $response = WP_EoxCoreApi()->update_enrollment( $args );
- if ( is_wp_error( $response ) ) {
- update_post_meta( $post_id, 'errors', $response->get_error_message() );
- $this->update_post_status( 'enrollment-error', $post_id );
- } else {
- delete_post_meta( $post_id, 'errors' );
- update_post_meta( $post_id, 'mode', $response->mode );
- update_post_meta( $post_id, 'is_active', $response->is_active );
- $this->update_post_status( 'enrollment-success', $post_id );
- }
- }
-
- /**
- * Filters the list of actions available on the list view below each object
- *
- * @return actions
- */
- public function remove_table_row_actions( $actions ) {
-
- unset( $actions['edit'] );
- unset( $actions['trash'] );
- unset( $actions['view'] );
- unset( $actions['inline hide-if-no-js'] );
-
- return $actions;
- }
-
- /**
- * Adds the cpt columns to the list view
- *
- * @return array $column
- */
- public function add_columns_to_list_view( $column ) {
- $column['enrollment_status'] = 'Status';
- $column['enrollment_type'] = 'Type';
- $column['date'] = 'Date created';
- $column['enrollment_order_id'] = 'Order';
- $column['enrollment_email'] = 'Email';
- $column['enrollment_messages'] = 'Last Message';
- return $column;
- }
-
- /**
- * Fills the values of the custom columns in the list view
- *
- * @return void
- */
- public function fill_custom_columns_in_list_view( $column_name, $post_id ) {
- switch ( $column_name ) {
- case 'enrollment_status':
- if ( get_post( $post_id )->post_status === 'enrollment-success' ) {
- echo 'Success';
- }
- if ( get_post( $post_id )->post_status === 'enrollment-error' ) {
- echo 'Error';
- }
- if ( get_post( $post_id )->post_status === 'enrollment-pending' ) {
- echo 'Pending';
- }
- break;
- case 'enrollment_type':
- if ( get_post_meta( $post_id, 'is_active', true ) ) {
- echo 'Enroll';
- } else {
- echo 'Unenroll';
- }
- break;
- case 'enrollment_order_id':
- $order_id = get_post_meta( $post_id, 'order_id', true );
- if ( $order_id ) {
- echo edit_post_link( '# ' . $order_id, '
', '
', $order_id );
- }
- break;
- case 'enrollment_email':
- $email = get_post_meta( $post_id, 'email', true );
- if ( $email ) {
- echo $email;
- }
- break;
- case 'enrollment_messages':
- echo( get_post_meta( $post_id, 'errors', true ) );
- break;
- default:
+
+ if ($status) {
+ $post_update['post_status'] = $status;
}
- }
- /**
- * Prepare the site to work with the Enrollment object as a CPT
- *
- * @return void
- */
- public function set_up_admin() {
- // List view.
- add_filter( 'post_row_actions', array( $this, 'remove_table_row_actions' ) );
- add_filter( 'manage_posts_custom_column', array( $this, 'fill_custom_columns_in_list_view' ), 10, 3 );
- add_filter( 'manage_openedx_enrollment_posts_columns', array( $this, 'add_columns_to_list_view' ) );
+ $this->wp_update_post( $post_update );
}
-
- /**
- * Main WP_Openedx_Enrollment Instance
- *
- * Ensures only one instance of WP_Openedx_Enrollment is loaded or can be loaded.
- *
- * @static
- * @see WP_Openedx_Enrollment()
- * @return Main WP_Openedx_Enrollment instance
- */
- public static function instance( $parent ) {
- if ( is_null( self::$_instance ) ) {
- self::$_instance = new self( $parent );
- }
- return self::$_instance;
- } // End instance()
-
- /**
- * Cloning is forbidden.
- *
- * @since 1.0.0
- */
- public function __clone() {
- _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ), $this->parent->_version );
- } // End __clone()
-
- /**
- * Unserializing instances of this class is forbidden.
- *
- * @since 1.0.0
- */
- public function __wakeup() {
- _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ), $this->parent->_version );
- } // End __wakeup()
-
}