<?php
/*
- This file the functionality of ajax for course add and update.
*/
if(!defined(‘ABSPATH’)) exit; // Exit if accessed directly
/**
- WP_Event_Manager_Ajax class.
/ class Course_Ajax { /*- The single instance of the class.
* - @var self
- @since 2.5
*/
private static $_instance = null;
- Allows for accessing single instance of class. Class should only be constructed once per call.
* - @since 2.5
- @static
- @return self Main instance.
*/
public static function instance() {
if(is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
- Constructor.
*/
public function __construct() { // ajax handlers
add_action(‘wp_ajax_nopriv_add_course’, array($this, ‘add_course’));
add_action(‘wp_ajax_add_course’, array($this, ‘add_course’)); add_action(‘wp_ajax_nopriv_edit_course’, array($this, ‘edit_course’));
add_action(‘wp_ajax_edit_course’, array($this, ‘edit_course’)); add_action(‘wp_ajax_delete_course’, array($this, ‘delete_course’));
add_action(‘wp_ajax_nopriv_delete_course’, array($this, ‘delete_course’));
}
- Add course.
- add course with ajax action
- @access public
- @param
- @return array
- @since 1
*/
public function add_course() { parse_str( $_REQUEST[‘form_data’], $params); $courseName = sanitize_text_field($params[‘courseName’]);
$description = sanitize_textarea_field($params[‘description’]);
$duration = intval($params[‘duration’]);
$price = floatval($params[‘price’]); $response = []; if( $courseName !=” && $description !=” && $duration > 0 && $price >= 0){global $wpdb; $table_name = $wpdb->prefix . 'course_detail'; $inserted = $wpdb->insert( $table_name, [ 'course_name' => $courseName, 'description' => $description, 'duration' => $duration, 'price' => $price ] ); if ($inserted) {$response = [ 'success' => true, 'message' => 'Course has been added successfully', ];}else {$response = [ 'success' => false, 'message' => 'Please check all the fields', ];}
}else {$response = [ 'success' => false, 'message' => 'Please check all the fields' ];
} wp_send_json($response);
wp_die();
- update course.
- update course with ajax action
- @access public
- @param
- @return array
- @since 1
*/
public function edit_course() { parse_str($_REQUEST[‘form_data’], $params);
$edit_id = intval($params[‘edit_id’]); $courseName = sanitize_text_field($params[‘courseName’]);
$description = sanitize_text_field($params[‘description’]);
$duration = intval($params[‘duration’]);
$price = floatval($params[‘price’]); if( $courseName !=” && $description !=” && $duration > 0 && $price >= 0 ){global $wpdb; $data = array( 'course_name' => $courseName, 'description' => $description, 'duration' => $duration, 'price' => $price ); $where = array('id' => $edit_id); $updated = $wpdb->update($wpdb->prefix . 'course_detail', $data, $where); if ($updated !== false) { $response = [ 'success' => true, 'message' => 'Course has been updated successfully' ]; } else { $response = [ 'success' => false, 'message' => 'Failed to update course' ]; }
} else {
$response = [
‘success’ => false,
‘message’ => ‘Please check all the fields’
];
} wp_send_json($response);
wp_die();
}
- delete course.
- delete course with ajax action
- @access public
- @param
- @return array
- @since 1
*/
public function delete_course() { $course_id = isset($_POST[‘course_id’]) ? intval($_POST[‘course_id’]) : 0;
global $wpdb; if ($course_id > 0) {// Prepare the SQL delete query $table_name = $wpdb->prefix . 'course_detail'; $sql = $wpdb->prepare("DELETE FROM $table_name WHERE id = %d", $course_id); // Execute the delete query $result = $wpdb->query($sql); if ($result !== false) {$response = [ 'success' => true, 'message' => 'Course has been deleted successfully' ];} else { $response = [ 'success' => false, 'message' => 'Failed to delete course' ]; }
} else {$response = [ 'success' => false, 'message' => 'Course ID is not found' ];
} wp_send_json($response);
wp_die();
}
}
Course_Ajax::instance();
- The single instance of the class.