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

style: ergonomic design form #16

Merged
merged 18 commits into from
Jul 25, 2023
Merged
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
100 changes: 53 additions & 47 deletions admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function render_enrollment_info_form( $post ) {
$order_id = get_post_meta( $post_id, 'order_id', true );

$new_enrollment = false;
if ( ! $course_id && ! $email ) {
if (! $course_id && ! $email) {
$new_enrollment = true;
}
?>
Expand All @@ -66,14 +66,15 @@ public function render_enrollment_info_form( $post ) {
</td>
</tr>
<tr>
<td class="first"><label>User</label></td>
<td class="first"><label>User Email</label></td>
<td>
<div style="width: 49%; display: inline-table;">
<label for="openedx_enrollment_email">Email:</label>
<input type="email" id="openedx_enrollment_email" name="enrollment_email"
title="You only need to fill one. Either the email or username"
value="<?php echo( $email ); ?>">
</div>
<div style="width: 49%; display: inline-table;">
<input type="email"
id="openedx_enrollment_email"
name="enrollment_email"
title="You only need to fill one. Either the email or username"
value="<?php echo( $email ); ?>">
</div>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -120,7 +121,7 @@ public function render_enrollment_info_form( $post ) {
</td>
</tr>
<tr>
<td class="first"><label for="openedx_enrollment_is_active">Request type</label></td>
<td class="first"><label for="openedx_enrollment_is_active">Request Type</label></td>
<td>

<select id="openedx_enrollment_is_active" name="enrollment_request_type">
Expand Down Expand Up @@ -153,23 +154,8 @@ public function render_enrollment_info_form( $post ) {
</td>
</tr>

<?php if ( get_post_meta( $post_id, 'errors', true ) ) : ?>
<!-- Temporal display of errors, TODO: move this to a polished div -->
<tr>
<td class="first"><label for="openedx_enrollment_errors">Errors</label></td>
<td>
<p><?php echo( get_post_meta( $post_id, 'errors', true ) ); ?></p>
</td>
</tr>
<?php else : ?>
<td class="first"><label for="openedx_enrollment_errors">Operation log</label></td>
<td>
<p>No errors ocurred processing this request</p>
</td>
<?php endif; ?>

<tr>
<td class="first"><label>General info</label></td>
<td class="first"><label>General Info</label></td>
<td>
<p>Edited:
<?php
Expand All @@ -183,39 +169,59 @@ public function render_enrollment_info_form( $post ) {
<p>Last edited: <?php echo( get_the_modified_time( '', $post_id ) . ' ' . get_the_modified_date( '', $post_id ) ); ?></p>
</td>
</tr>

<tr>
<td class="first"><label>Choose an Action</label></td>
<td>
<select name="enrollment_action" id="actions-select">
<option value="save_no_process"><?php esc_html_e( 'Save without processing', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_sync"><?php esc_html_e( 'Synchronize (pull information)', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_process" selected><?php esc_html_e( 'Process request', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_no_pre"><?php esc_html_e( 'Process no pre-enrollment', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_force"><?php esc_html_e( 'Process --force', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_no_pre_force"><?php esc_html_e( 'Process no pre-enrollment --force', 'wp-edunext-marketing-site' ); ?></option>
</select>
</td>
</tr>

<tr>
<td class="first"><label>Create/Update Enrollment</label></td>
<td>
<button class="button save_order button-primary"><span><?php esc_html_e( 'Apply action', 'wp-edunext-marketing-site' ); ?></span></button>
</td>
</tr>
</tbody>
</table>
</fieldset>
</div>
<?php
}

/**
* Temporary function to get test logs for the logs box
*
* @return string Test logs
*/
public function get_logs() {
$logs = "Log 1\n";
$logs .= "Log 2\n";
$logs .= "Log 3\n";
$logs .= "No errors ocurred\n";
return $logs;
}


/**
* Renders the actions box for the edit post box
* Renders the logs box for the edit post box
*
* @return void
*/
public function render_actions_box() {
public function render_logs_box() {
$logs = $this->get_logs();
?>
<ul class="enrollment_actions submitbox">

<label for="actions-select"><?php esc_html_e( 'Choose an action...', 'wp-edunext-marketing-site' ); ?></label>
<li class="wide" id="actions">
<select name="enrollment_action" id="actions-select">
<option value="save_no_process"><?php esc_html_e( 'Save without processing', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_sync"><?php esc_html_e( 'Synchronize (pull information)', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_process" selected><?php esc_html_e( 'Process request', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_no_pre"><?php esc_html_e( 'Process no pre-enrollment', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_force"><?php esc_html_e( 'Process --force', 'wp-edunext-marketing-site' ); ?></option>
<option value="enrollment_no_pre_force"><?php esc_html_e( 'Process no pre-enrollment --force', 'wp-edunext-marketing-site' ); ?></option>
</select>
</li>

<li class="wide">
<button class="button save_order button-primary"><span><?php esc_html_e( 'Apply action', 'wp-edunext-marketing-site' ); ?></span></button>
</li>

</ul>
<div class="logs_box">
<pre><?php echo esc_html( $logs ); ?></pre>
</div>
<?php
}

Expand All @@ -227,7 +233,7 @@ public function render_actions_box() {
public function replace_admin_meta_boxes() {
remove_meta_box( 'submitdiv', $this->post_type, 'side' );

add_meta_box( 'openedx_enrollment_request_actions', sprintf( __( '%s actions', '' ), 'Open edX Enrollment Requests' ), array( $this, 'render_actions_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' );
}

}
31 changes: 24 additions & 7 deletions includes/Openedx_Woocommerce_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,41 @@ private function load_dependencies() {
* The class responsible for orchestrating the actions and filters of the
* core plugin.
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'includes/Openedx_Woocommerce_Plugin_Loader.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'includes/Openedx_Woocommerce_Plugin_Loader.php';

/**
* The class responsible for defining internationalization functionality
* of the plugin.
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'includes/Openedx_Woocommerce_Plugin_i18n.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'includes/Openedx_Woocommerce_Plugin_i18n.php';

/**
* The class responsible for defining all actions that occur in the admin area.
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'admin/Openedx_Woocommerce_Plugin_Admin.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'admin/Openedx_Woocommerce_Plugin_Admin.php';

/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'public/Openedx_Woocommerce_Plugin_Public.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'public/Openedx_Woocommerce_Plugin_Public.php';

$this->loader = new Openedx_Woocommerce_Plugin_Loader();

/**
* The class responsible for defining the enrollment object
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'includes/model/Openedx_Woocommerce_Plugin_Enrollment.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'includes/model/Openedx_Woocommerce_Plugin_Enrollment.php';

/**
* The class responsible for defining the custom-post-type object
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'includes/model/Openedx_Woocommerce_Plugin_Post_Type.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'includes/model/Openedx_Woocommerce_Plugin_Post_Type.php';

/**
* The class responsible for rendering the enrollment info form
*/
include_once plugin_dir_path( dirname( __FILE__ )) . 'admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php';
include_once plugin_dir_path( dirname(__FILE__)) . 'admin/views/Openedx_Woocommerce_Plugin_Enrollment_Info_Form.php';

}

Expand Down Expand Up @@ -181,6 +181,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 );

}

Expand All @@ -200,6 +201,22 @@ private function define_public_hooks() {

}

/**
* Modify the message displayed when a custom-post-type is updated
*
* @param string $translated_text translation text
* @param string $text text to be translated
* @param string $domain text domain
* @return string $translated_text post updated message
*/
function openedx_plugin_custom_post_message( $translated_text, $text, $domain ) {

if ( $domain === 'default' && $text === 'Post updated.' ) {
$translated_text = 'Enrollment action executed';
}
return $translated_text;
}

/**
* Run the loader to execute all of the hooks with WordPress.
*
Expand Down
67 changes: 45 additions & 22 deletions includes/model/Openedx_Woocommerce_Plugin_Enrollment.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,36 @@ public function register_enrollment_custom_post_type(){
'show_in_nav_menus' => true,
'supports' => array( '' ),
'menu_icon' => 'dashicons-admin-post',
'labels' => array(
'name' => 'Open edX Enrollment Requests',
'singular_name' => 'Open edX Enrollment Request',
'menu_name' => 'Open edX Sync',
'all_items' => 'Enrollments Manager',
'add_new_item' => 'Add New Enrollment Request',
'edit_item' => 'Edit Enrollment Request',
),
);

// Register post-type using wrapper custom-post-type function
$this->parent->register_post_type( 'openedx_enrollment', 'Open edX Enrollment Requests', 'Open edX Enrollment Request', '', $enrollment_cpt_options );
$this->parent->register_post_type( 'openedx_enrollment', ' ', ' ', '', $enrollment_cpt_options );
}

public function unregister_save_hook() {
remove_action( 'save_post', array( $this, 'save_action' ), 10, 3 );
/**
* Unregister the save hook to prevent an infinite cycle of hook recursion
*
* @return void
*/
public function unregisterSaveHook() {
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 );

/**
* Register the save hook to prevent an infinite cycle of hook recursion
*
* @return void
*/
public function registerSaveHook() {
add_action( 'save_post', array($this, 'save_action'), 10, 3 );
}

/**
Expand Down Expand Up @@ -146,14 +164,16 @@ 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'] ?? '' );
$enrollment_action = sanitize_text_field($_POST['enrollment_action'] ?? '');

$this->save_enrollment( $post, $enrollment_arr, $enrollment_action );
}
Expand Down Expand Up @@ -223,13 +243,15 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {

// Check if old post_meta tags are different from the new ones.

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->update_post($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' );
if ($post->post_status !== 'enrollment-success' && $post->post_status !== 'enrollment-pending' && $post->post_status !== 'enrollment-error') {
$this->update_post($post_id, 'enrollment-pending');
}
}

Expand All @@ -239,21 +261,22 @@ public function save_enrollment( $post, $enrollment_arr, $enrollment_action ) {
* @param string $status The status of the request.
* @param int $post_id The post ID.
*/
public function update_post( $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 );
$enrollment_mode = get_post_meta( $post_id, 'mode', true );
$enrollment_course_id = get_post_meta($post_id, 'course_id', 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_title' => $enrollment_course_id . ' | ' . $enrollment_email . ' | Mode: ' . $enrollment_mode,
'ID' => $post_id,
'post_title'
=> $enrollment_course_id .' | '. $enrollment_email .' | Mode: '.$enrollment_mode,
);

if ($status) {
$post_update['post_status'] = $status;
}

$this->wp_update_post( $post_update );
$this->wp_update_post($post_update);
}
}