From 864b4578f8a6ebb6c65aa82a86e15b31d5006248 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 15:47:58 -0500 Subject: [PATCH 01/35] feat: operation logs for changes into post form --- ...oocommerce_Plugin_Enrollment_Info_Form.php | 72 +++++++++++++++---- .../Openedx_Woocommerce_Plugin_Enrollment.php | 64 +++++++++++++++-- openedx-woocommerce-plugin.php | 29 ++++++++ 3 files changed, 144 insertions(+), 21 deletions(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index 9752e21..607b708 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -51,6 +51,7 @@ public function render_enrollment_info_form( $post ) { if (! $course_id && ! $email) { $new_enrollment = true; } + ?>

Open edX enrollment request

@@ -197,30 +198,71 @@ public function render_enrollment_info_form( $post ) { prefix . 'enrollment_logs_req_table'; + + $logs = $wpdb->get_results( + $wpdb->prepare( + "SELECT * FROM $tabla_logs WHERE post_id = %d ORDER BY fecha_registro ASC", + $post_id + ), + ARRAY_A + ); + + $formatted_logs = ''; + foreach ($logs as $log) { + $formatted_logs .= "
"; + $formatted_logs .= "Timestamp: " . date('d-m-Y H:i:s', strtotime($log['fecha_registro'])) . "
"; + $formatted_logs .= "User: " . $log['user'] . "
"; + $formatted_logs .= "Action: " . $log['action_name'] . "
"; + $formatted_logs .= "Object Before: " . $log['object_before'] . "
"; + $formatted_logs .= "Object After: " . $log['object_after'] . "
"; + $formatted_logs .= "
"; + } + + return $formatted_logs; } + - - /** - * Renders the logs box for the edit post box + /** + * Render logs box * - * @return void + * @param WP_Post $post Current post object. */ - public function render_logs_box() { - $logs = $this->get_logs(); + public function render_logs_box($post) { + $post_id = $post->ID; + $logs = $this->get_logs($post_id); ?> +
-
+
unregister_save_hook(); + $this->unregisterSaveHook(); wp_update_post( $post ); - $this->register_save_hook(); + $this->registerSaveHook(); } /** @@ -210,6 +211,7 @@ public function insert_new( $enrollment_arr, $enrollment_action = '' ) { public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) { $post_id = $post->ID; + $old_post = $post; $enrollment_course_id = $enrollment_arr['enrollment_course_id']; $enrollment_email = $enrollment_arr['enrollment_email']; @@ -219,8 +221,19 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) { // 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_email = get_post_meta($post_id, 'email', true); $old_mode = get_post_meta($post_id, 'mode', true); + $old_request_type = get_post_meta($post_id, 'is_active', true); + $old_order_id = get_post_meta($post_id, 'order_id', true); + + // Array of old data + $old_data_array = array( + 'enrollment_course_id' => $old_course_id, + 'enrollment_email' => $old_email, + 'enrollment_mode' => $old_mode, + 'enrollment_request_type' => $old_request_type, + 'enrollment_order_id' => $old_order_id + ); // We need to have all 3 required params to continue. $enrollment_user_reference = $enrollment_email; @@ -241,17 +254,56 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) { update_post_meta( $post_id, 'is_active', false ); } - // Check if old post_meta tags are different from the new ones. + // Check if old post_meta tags are different from the new ones to change post name. if ($old_course_id !== $enrollment_course_id || $old_email !== $enrollment_email || $old_mode !== $enrollment_mode) { - $this->update_post($post_id); + $this->updatePost($post_id); } // 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->updatePost($post_id, 'enrollment-pending'); + } + + + $this->createChangeLog($post_id, $old_data_array, $enrollment_arr, $enrollment_action); + } + + + /** + * Creates a new log entry in the database. + * + * @param int $post_id The post ID. + * @param array $old_data_array The old data array. + * @param array $enrollment_arr The new data array. + * @param string $enrollment_action The API action to perform. + * + * @return void + */ + public function createChangeLog($post_id, $old_data_array, $enrollment_arr, $enrollment_action) { + global $wpdb; + $tabla_logs = $wpdb->prefix . 'enrollment_logs_req_table'; + $new_post = get_post($post_id); + + $fecha_registro = current_time('mysql', true); + $usuario_id = get_current_user_id(); + $usuario_nombre = get_user_by('id', $usuario_id)->user_login; + + $log_data = array( + 'post_id' => $post_id, + 'fecha_registro' => $fecha_registro, + 'user' => $usuario_nombre, + 'action_name' => $enrollment_action, + 'object_before' => json_encode($old_data_array), + 'object_after' => json_encode($enrollment_arr) + ); + + $wpdb->insert($tabla_logs, $log_data); + + if ($wpdb->last_error) { + error_log('there was an error generating a register: ' . $wpdb->last_error); } } diff --git a/openedx-woocommerce-plugin.php b/openedx-woocommerce-plugin.php index 95bbaa1..09bfc3d 100644 --- a/openedx-woocommerce-plugin.php +++ b/openedx-woocommerce-plugin.php @@ -59,7 +59,36 @@ function deactivate_openedx_woocommerce_plugin() { Openedx_Woocommerce_Plugin_Deactivator::deactivate(); } +/** + * Create the table for the logs on plugin activation + */ +function create_enrollment_logs_table() { + global $wpdb; + $tabla_logs = $wpdb->prefix . 'enrollment_logs_req_table'; + + if ($wpdb->get_var("SHOW TABLES LIKE '$tabla_logs'") != $tabla_logs) { + $charset_collate = $wpdb->get_charset_collate(); + + // Definición de la estructura de la tabla + $sql = "CREATE TABLE $tabla_logs ( + id INT NOT NULL AUTO_INCREMENT, + post_id INT NOT NULL, + fecha_registro DATETIME NOT NULL, + user VARCHAR(255) NOT NULL, + action_name VARCHAR(255) NOT NULL, + object_before LONGTEXT NOT NULL, + object_after LONGTEXT NOT NULL, + PRIMARY KEY (id) + ) $charset_collate;"; + + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + + dbDelta( $sql ); + } +} + register_activation_hook( __FILE__, 'activate_openedx_woocommerce_plugin' ); +register_activation_hook( __FILE__, 'create_enrollment_logs_table' ); register_deactivation_hook( __FILE__, 'deactivate_openedx_woocommerce_plugin' ); /** From 90de839a47a406b509f1b11567d68e8f8b9da8d0 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 16:00:41 -0500 Subject: [PATCH 02/35] fix: enrollment log for when post is created --- .../Openedx_Woocommerce_Plugin_Enrollment.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 133d86f..a0a562c 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -282,7 +282,8 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) { * * @return void */ - public function createChangeLog($post_id, $old_data_array, $enrollment_arr, $enrollment_action) { + public function createChangeLog( $post_id, $old_data_array, $enrollment_arr, $enrollment_action ) { + global $wpdb; $tabla_logs = $wpdb->prefix . 'enrollment_logs_req_table'; $new_post = get_post($post_id); @@ -290,7 +291,17 @@ public function createChangeLog($post_id, $old_data_array, $enrollment_arr, $enr $fecha_registro = current_time('mysql', true); $usuario_id = get_current_user_id(); $usuario_nombre = get_user_by('id', $usuario_id)->user_login; - + + // Cambiar el valor de "action_name" para un nuevo post o cuando se envía a la papelera + if (empty($old_data_array['enrollment_course_id'])) { + $enrollment_action = 'Object created'; + } + + // Cambiar el valor de "object_before" si está vacío + if (empty($old_data_array['enrollment_course_id'])) { + $old_data_array = '--'; + } + $log_data = array( 'post_id' => $post_id, 'fecha_registro' => $fecha_registro, @@ -303,7 +314,7 @@ public function createChangeLog($post_id, $old_data_array, $enrollment_arr, $enr $wpdb->insert($tabla_logs, $log_data); if ($wpdb->last_error) { - error_log('there was an error generating a register: ' . $wpdb->last_error); + error_log('There was an error generating a register: ' . $wpdb->last_error); } } From 3a45aeb6bb743558dc96ed451f35d4a3b20f3e85 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 16:05:58 -0500 Subject: [PATCH 03/35] fix: mayus fix in object created message --- includes/model/Openedx_Woocommerce_Plugin_Enrollment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index a0a562c..1f0649b 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -294,7 +294,7 @@ public function createChangeLog( $post_id, $old_data_array, $enrollment_arr, $en // Cambiar el valor de "action_name" para un nuevo post o cuando se envía a la papelera if (empty($old_data_array['enrollment_course_id'])) { - $enrollment_action = 'Object created'; + $enrollment_action = 'Object Created'; } // Cambiar el valor de "object_before" si está vacío From 207fab66b2a4954ae407909145070bf7ecb429cc Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 19:54:46 -0500 Subject: [PATCH 04/35] fix: deleted comment in spanish --- admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index 607b708..681dc4b 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -252,7 +252,7 @@ public function render_logs_box($post) { overflow: auto; } .log_entry { - background-color: #f2f2f2; /* Color de fondo gris claro */ + background-color: #f2f2f2; border: 1px solid #ccc; padding: 10px; margin-bottom: 10px; From 5d63b4e4293160e356e4a5b6bad70da3c7a4b194 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 19:58:27 -0500 Subject: [PATCH 05/35] fix: logs in desc order --- admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index 681dc4b..c9cf371 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -211,7 +211,7 @@ public function get_logs($post_id) { $logs = $wpdb->get_results( $wpdb->prepare( - "SELECT * FROM $tabla_logs WHERE post_id = %d ORDER BY fecha_registro ASC", + "SELECT * FROM $tabla_logs WHERE post_id = %d ORDER BY fecha_registro DESC", $post_id ), ARRAY_A From b4aded006ac9d956bf9b3c4e3b64df0a4c9cad84 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 20:26:52 -0500 Subject: [PATCH 06/35] fix: phpcs in info form file --- ...oocommerce_Plugin_Enrollment_Info_Form.php | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index c9cf371..7d4521b 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -73,7 +73,8 @@ public function render_enrollment_info_form( $post ) {
@@ -122,7 +123,9 @@ public function render_enrollment_info_form( $post ) { - + + + - - - - - - + + + + + + @@ -188,7 +203,7 @@ public function render_enrollment_info_form( $post ) { - + @@ -275,7 +290,7 @@ public function render_logs_box($post) { public function replace_admin_meta_boxes() { remove_meta_box( 'submitdiv', $this->post_type, 'side' ); - add_meta_box( 'openedx_enrollment_request_actions', 'Enrollment Operation Logs', array( $this, 'render_logs_box' ), $this->post_type, 'side', 'high' ); + add_meta_box('openedx_enrollment_request_actions', 'Enrollment Operation Logs', array( $this, 'render_logs_box' ), $this->post_type, 'side', 'high'); } } From 4b2ac693ebabbf6212e1f0993b26d714426811a7 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 20:31:29 -0500 Subject: [PATCH 07/35] fix: phpcs in enrollment and plugin files --- .../Openedx_Woocommerce_Plugin_Enrollment.php | 14 ++++++++------ openedx-woocommerce-plugin.php | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 1f0649b..77317bc 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -69,8 +69,8 @@ public function register_enrollment_custom_post_type(){ ), ); - // Register post-type using wrapper custom-post-type function - $this->parent->register_post_type( 'openedx_enrollment', ' ', ' ', '', $enrollment_cpt_options ); + // Register post-type using wrapper custom-post-type function + $this->parent->register_post_type('openedx_enrollment', ' ', ' ', '', $enrollment_cpt_options); } /** @@ -78,8 +78,9 @@ public function register_enrollment_custom_post_type(){ * * @return void */ - public function unregisterSaveHook() { - remove_action( 'save_post', array($this, 'save_action'), 10, 3 ); + public function unregisterSaveHook() + { + remove_action('save_post', array($this, 'save_action'), 10, 3); } /** @@ -87,8 +88,9 @@ public function unregisterSaveHook() { * * @return void */ - public function registerSaveHook() { - add_action( 'save_post', array($this, 'save_action'), 10, 3 ); + public function registerSaveHook() + { + add_action('save_post', array($this, 'save_action'), 10, 3); } /** diff --git a/openedx-woocommerce-plugin.php b/openedx-woocommerce-plugin.php index 09bfc3d..2c98327 100644 --- a/openedx-woocommerce-plugin.php +++ b/openedx-woocommerce-plugin.php @@ -46,7 +46,7 @@ * This action is documented in includes/class-openedx-woocommerce-plugin-activator.php */ function activate_openedx_woocommerce_plugin() { - include_once plugin_dir_path( __FILE__ ) . 'includes/Openedx_Woocommerce_Plugin_Activator.php'; + include_once plugin_dir_path(__FILE__) . 'includes/Openedx_Woocommerce_Plugin_Activator.php'; Openedx_Woocommerce_Plugin_Activator::activate(); } @@ -55,7 +55,7 @@ function activate_openedx_woocommerce_plugin() { * This action is documented in includes/class-openedx-woocommerce-plugin-deactivator.php */ function deactivate_openedx_woocommerce_plugin() { - include_once plugin_dir_path( __FILE__) . 'includes/Openedx_Woocommerce_Plugin_Deactivator.php'; + include_once plugin_dir_path(__FILE__) . 'includes/Openedx_Woocommerce_Plugin_Deactivator.php'; Openedx_Woocommerce_Plugin_Deactivator::deactivate(); } From 6f8ccee7bca1e7e779db2d91f15311572e01c8b5 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 20:34:56 -0500 Subject: [PATCH 08/35] fix: phpcs fixes about spacing --- .../Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php | 4 ++-- includes/model/Openedx_Woocommerce_Plugin_Enrollment.php | 5 +++-- openedx-woocommerce-plugin.php | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index 7d4521b..a275437 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -69,9 +69,9 @@ public function render_enrollment_info_form( $post ) { -
+
sanitize_text_field( $_POST['enrollment_course_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( @@ -326,7 +326,8 @@ public function createChangeLog( $post_id, $old_data_array, $enrollment_arr, $en * @param string $status The status of the request. * @param int $post_id The post ID. */ - public function updatePost( $post_id, $status = null ) { + public function updatePost( $post_id, $status = null ) + { $enrollment_course_id = get_post_meta($post_id, 'course_id', true); $enrollment_email = get_post_meta($post_id, 'email', true); diff --git a/openedx-woocommerce-plugin.php b/openedx-woocommerce-plugin.php index 2c98327..5d9097a 100644 --- a/openedx-woocommerce-plugin.php +++ b/openedx-woocommerce-plugin.php @@ -95,7 +95,7 @@ function create_enrollment_logs_table() { * The core plugin class that is used to define internationalization, * admin-specific hooks, and public-facing site hooks. */ -require plugin_dir_path( __FILE__) . 'includes/Openedx_Woocommerce_Plugin.php'; +require plugin_dir_path(__FILE__) . 'includes/Openedx_Woocommerce_Plugin.php'; /** * Begins execution of the plugin. From 36e229e7e0db7875b10647cfadb90ce1836cb560 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 20:37:23 -0500 Subject: [PATCH 09/35] fix: phpcs fixes --- ...enedx_Woocommerce_Plugin_Enrollment_Info_Form.php | 12 ++++++------ .../model/Openedx_Woocommerce_Plugin_Enrollment.php | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index a275437..1949ad0 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -70,12 +70,12 @@ public function render_enrollment_info_form( $post ) {
- +
diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 7d39476..6b91794 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -258,9 +258,9 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) { // Check if old post_meta tags are different from the new ones to change post name. - if ($old_course_id !== $enrollment_course_id || - $old_email !== $enrollment_email || - $old_mode !== $enrollment_mode) { + if ($old_course_id !== $enrollment_course_id + || $old_email !== $enrollment_email + || $old_mode !== $enrollment_mode) { $this->updatePost($post_id); } From dfd5f88066d6841fdc7b2a73aafc9a4b379d10d4 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 20:39:37 -0500 Subject: [PATCH 10/35] fix: phpcs fixes about number of characters --- .../Openedx_Woocommerce_Plugin_Enrollment.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 6b91794..3306502 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -167,13 +167,13 @@ 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_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'] ?? ''); @@ -265,7 +265,9 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) { } // 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') { + if ($post->post_status !== 'enrollment-success' + && $post->post_status !== 'enrollment-pending' + && $post->post_status !== 'enrollment-error') { $this->updatePost($post_id, 'enrollment-pending'); } From 6c110ec4f1b8c4adbc6d22763f90959062753893 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Sun, 30 Jul 2023 20:41:33 -0500 Subject: [PATCH 11/35] fix: phpcs fixes --- includes/model/Openedx_Woocommerce_Plugin_Enrollment.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 3306502..b2e5083 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -338,7 +338,9 @@ public function updatePost( $post_id, $status = null ) $post_update = array( 'ID' => $post_id, 'post_title' - => $enrollment_course_id .' | '. $enrollment_email .' | Mode: '.$enrollment_mode, + => $enrollment_course_id. + ' | '.$enrollment_email. + ' | Mode: '.$enrollment_mode, ); if ($status) { From 2f2f6d9b6dd6fc7a590624b115f6fc4386819634 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Mon, 31 Jul 2023 15:32:23 -0500 Subject: [PATCH 12/35] fix: deleted general info field from form --- ...oocommerce_Plugin_Enrollment_Info_Form.php | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php index 1949ad0..527f4eb 100644 --- a/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php +++ b/admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php @@ -53,7 +53,7 @@ public function render_enrollment_info_form( $post ) { } ?> -
+

Open edX enrollment request

@@ -158,22 +158,6 @@ public function render_enrollment_info_form( $post ) { - - - -

Edited: - -

-

Last edited:

- - - @@ -259,7 +243,7 @@ public function render_logs_box($post) {
diff --git a/includes/Openedx_Woocommerce_Plugin.php b/includes/Openedx_Woocommerce_Plugin.php index 2df5198..7bb0c46 100644 --- a/includes/Openedx_Woocommerce_Plugin.php +++ b/includes/Openedx_Woocommerce_Plugin.php @@ -184,6 +184,7 @@ 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' ); } diff --git a/includes/Openedx_Woocommerce_Plugin_Loader.php b/includes/Openedx_Woocommerce_Plugin_Loader.php index 4315845..d08104e 100644 --- a/includes/Openedx_Woocommerce_Plugin_Loader.php +++ b/includes/Openedx_Woocommerce_Plugin_Loader.php @@ -43,6 +43,8 @@ class Openedx_Woocommerce_Plugin_Loader { */ protected $filters; + protected $styles; + /** * Initialize the collections used to maintain the actions and filters. * @@ -52,6 +54,7 @@ public function __construct() { $this->actions = array(); $this->filters = array(); + $this->styles = array(); } @@ -83,6 +86,21 @@ public function add_filter( $hook, $component, $callback, $priority = 10, $accep $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); } + /** + * Add a new style to the collection to be registered with WordPress. + * + * @since 1.1.1 + * @param string $handle Name of the stylesheet. Should be unique. + * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory. + * @param array $deps An array of registered stylesheet handles this stylesheet depends on. + * @param string $ver String specifying the stylesheet version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the stylesheet. + * @param string $media The media for which this stylesheet has been defined. + * @return array The collection of actions and filters registered with WordPress. + */ + public function wp_enqueue_style( $handle, $src, $deps, $ver, $media ) { + $this->styles = $this->add_style($handle, $src, $deps, $ver, $media); + } + /** * A utility function that is used to register the actions and hooks into a single * collection. @@ -111,6 +129,20 @@ private function add( $hooks, $hook, $component, $callback, $priority, $accepted } + private function add_style( $handle, $src, $deps, $ver, $media ) { + + $hooks[] = array( + 'handle' => $handle, + 'src' => $src, + 'deps' => $deps, + 'ver' => $ver, + 'media' => $media + ); + + return $hooks; + + } + /** * Register the filters and actions with WordPress. * @@ -126,6 +158,10 @@ public function run() { add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); } + foreach ( $this->styles as $hook ) { + wp_enqueue_style( $hook['handle'], $hook['src'], $hook['deps'], $hook['ver'], $hook['media'] ); + } + } } From bf0e4a5c27c8b5a2677ae65b6c17b52550b778dd Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Thu, 3 Aug 2023 14:36:38 -0500 Subject: [PATCH 30/35] fix: improved if eval for post status --- includes/model/Openedx_Woocommerce_Plugin_Enrollment.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 9295b25..71ed8d9 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -277,12 +277,10 @@ public function save_enrollment($post, $enrollment_arr, $enrollment_action) $this->updatePost($post_id); } + $nonValidStatuses = array('enrollment-success','enrollment-pending','enrollment-error'); + // 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' - ) { + if (!in_array($post->post_status, $nonValidStatuses, true)) { $this->updatePost($post_id, 'enrollment-pending'); } From 3a0c0f0c55edacfff594a9579ddd118753d1186f Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Thu, 3 Aug 2023 14:37:11 -0500 Subject: [PATCH 31/35] fix: logs table renamed in english --- openedx-woocommerce-plugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openedx-woocommerce-plugin.php b/openedx-woocommerce-plugin.php index 7eb3c1f..91a5281 100644 --- a/openedx-woocommerce-plugin.php +++ b/openedx-woocommerce-plugin.php @@ -64,12 +64,12 @@ function deactivate_openedx_woocommerce_plugin() { */ function create_enrollment_logs_table() { global $wpdb; - $tabla_logs = $wpdb->prefix . 'enrollment_logs_req_table'; + $logs_table = $wpdb->prefix . 'enrollment_logs_req_table'; - if ($wpdb->get_var("SHOW TABLES LIKE '$tabla_logs'") != $tabla_logs) { + if ($wpdb->get_var("SHOW TABLES LIKE '$logs_table'") != $logs_table) { $charset_collate = $wpdb->get_charset_collate(); - $sql = "CREATE TABLE $tabla_logs ( + $sql = "CREATE TABLE $logs_table ( id INT NOT NULL AUTO_INCREMENT, post_id INT NOT NULL, mod_date DATETIME NOT NULL, From 6e076f1e32c91556cb8a13be6ae941a7edc2b0ea Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Thu, 3 Aug 2023 14:40:07 -0500 Subject: [PATCH 32/35] refactor: check main enrollment data decoupled --- .../Openedx_Woocommerce_Plugin_Enrollment.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 71ed8d9..3bbe49d 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -253,11 +253,7 @@ public function save_enrollment($post, $enrollment_arr, $enrollment_action) $enrollment_data = $data['enrollment_arr']; // Check if the enrollment main data is empty. - if ( - !$enrollment_data['enrollment_course_id'] - || !$enrollment_data['enrollment_email'] - || !$enrollment_data['enrollment_mode'] - ) { + if (is_enrollment_data_empty($enrollment_data)) { return; } @@ -287,6 +283,16 @@ public function save_enrollment($post, $enrollment_arr, $enrollment_action) $this->log_manager->createChangeLog($post_id, $old_data, $enrollment_data, $enrollment_action); } + public function is_enrollment_data_empty($enrollment_data){ + if ( + !$enrollment_data['enrollment_course_id'] + || !$enrollment_data['enrollment_email'] + || !$enrollment_data['enrollment_mode'] + ){ + return true; + } + } + public function prepare_enrollment_data($post_id, $enrollment_arr) { From e81dabcd3b9e4476a891a7fb679a689f5763d340 Mon Sep 17 00:00:00 2001 From: Julian Ramirez Date: Thu, 3 Aug 2023 14:45:31 -0500 Subject: [PATCH 33/35] fix: check enrollment data method call fixed --- includes/model/Openedx_Woocommerce_Plugin_Enrollment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 3bbe49d..6069d94 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -253,7 +253,7 @@ public function save_enrollment($post, $enrollment_arr, $enrollment_action) $enrollment_data = $data['enrollment_arr']; // Check if the enrollment main data is empty. - if (is_enrollment_data_empty($enrollment_data)) { + if ($this->is_enrollment_data_empty($enrollment_data)) { return; } From ee3ca7b378e6bb617cfcbf7bda0bf32070084e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Ramirez=20Giraldo?= <52968528+julianramirez2@users.noreply.github.com> Date: Thu, 3 Aug 2023 17:22:26 -0500 Subject: [PATCH 34/35] refactor: added comments to new functions --- .../Openedx_Woocommerce_Plugin_Enrollment.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php index 6069d94..ad28de2 100644 --- a/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php +++ b/includes/model/Openedx_Woocommerce_Plugin_Enrollment.php @@ -283,6 +283,12 @@ public function save_enrollment($post, $enrollment_arr, $enrollment_action) $this->log_manager->createChangeLog($post_id, $old_data, $enrollment_data, $enrollment_action); } + + /** + * Check if important enrollment data is empty to stop operation + * + * @param array $enrollment_data An array containing the enrollment info. + */ public function is_enrollment_data_empty($enrollment_data){ if ( !$enrollment_data['enrollment_course_id'] @@ -293,6 +299,12 @@ public function is_enrollment_data_empty($enrollment_data){ } } + /** + * Prepare the array of information to use in the Enrollment process. + * + * @param string $post_id The Enrollment Request ID. + * @param array $enrollment_arr An array containing the enrollment info. + */ public function prepare_enrollment_data($post_id, $enrollment_arr) { @@ -319,6 +331,11 @@ public function prepare_enrollment_data($post_id, $enrollment_arr) ); } + /** + * Update the Enrollment Request metadata with the current information. + * @param string $post_id The Enrollment Request ID. + * @param array $enrollment_arr An array containing the enrollment info. + */ public function update_enrollment_meta_data($post_id, $enrollment_data) { // Update the $post metadata. From 1434f15a62ad1df07371f6b47bbab08123d70c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Ramirez=20Giraldo?= <52968528+julianramirez2@users.noreply.github.com> Date: Thu, 3 Aug 2023 17:26:56 -0500 Subject: [PATCH 35/35] fix: endline in css file --- admin/css/openedx-woocommerce-plugin-admin.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/css/openedx-woocommerce-plugin-admin.css b/admin/css/openedx-woocommerce-plugin-admin.css index 519fd6c..899a8fd 100644 --- a/admin/css/openedx-woocommerce-plugin-admin.css +++ b/admin/css/openedx-woocommerce-plugin-admin.css @@ -28,4 +28,4 @@ .logs_box strong { font-weight: bold; -} \ No newline at end of file +}