Example #1
0
 /**
  * Edit payment action.
  */
 public static function edit_payment()
 {
     $payment_id = isset($_GET['payment_id']) ? absint($_GET['payment_id']) : 0;
     $payment = edr_get_payment($payment_id);
     $errors = array();
     if (count($_POST)) {
         // Verify nonce.
         check_admin_referer('ib_educator_edit_payment_' . $payment_id);
         // Capability check.
         if (!current_user_can('manage_educator')) {
             return;
         }
         // Payment type.
         if (isset($_POST['payment_type']) && array_key_exists($_POST['payment_type'], edr_get_payment_types())) {
             $payment->payment_type = $_POST['payment_type'];
         }
         // Student ID.
         if (empty($payment->user_id)) {
             if (!empty($_POST['student_id']) && is_numeric($_POST['student_id'])) {
                 $payment->user_id = $_POST['student_id'];
             } else {
                 $errors[] = 'empty_student_id';
             }
         }
         // Course ID.
         if (empty($payment->course_id)) {
             if (!empty($_POST['course_id']) && is_numeric($_POST['course_id'])) {
                 $payment->course_id = $_POST['course_id'];
             } elseif ('course' == $payment->payment_type) {
                 $errors[] = 'empty_course_id';
             }
         }
         // Object ID.
         if (isset($_POST['object_id']) && is_numeric($_POST['object_id'])) {
             $payment->object_id = $_POST['object_id'];
         }
         // Tax.
         if (isset($_POST['tax']) && is_numeric($_POST['tax'])) {
             $payment->tax = $_POST['tax'];
         }
         // Amount.
         if (isset($_POST['amount']) && is_numeric($_POST['amount'])) {
             $payment->amount = $_POST['amount'];
         }
         if (isset($_POST['currency'])) {
             $payment->currency = sanitize_text_field($_POST['currency']);
         }
         // Transaction ID.
         if (isset($_POST['txn_id'])) {
             $payment->txn_id = sanitize_text_field($_POST['txn_id']);
         }
         // Payment status.
         if (isset($_POST['payment_status']) && array_key_exists($_POST['payment_status'], edr_get_payment_statuses())) {
             $payment->payment_status = $_POST['payment_status'];
         }
         // Payment gateway.
         if (isset($_POST['payment_gateway'])) {
             $payment->payment_gateway = sanitize_title($_POST['payment_gateway']);
         }
         // First Name.
         if (isset($_POST['first_name'])) {
             $payment->first_name = sanitize_text_field($_POST['first_name']);
         }
         // Last Name.
         if (isset($_POST['last_name'])) {
             $payment->last_name = sanitize_text_field($_POST['last_name']);
         }
         // Address.
         if (isset($_POST['address'])) {
             $payment->address = sanitize_text_field($_POST['address']);
         }
         // Address Line 2.
         if (isset($_POST['address_2'])) {
             $payment->address_2 = sanitize_text_field($_POST['address_2']);
         }
         // City.
         if (isset($_POST['city'])) {
             $payment->city = sanitize_text_field($_POST['city']);
         }
         // Postcode.
         if (isset($_POST['postcode'])) {
             $payment->postcode = sanitize_text_field($_POST['postcode']);
         }
         // State / Province.
         if (isset($_POST['state'])) {
             $payment->state = sanitize_text_field($_POST['state']);
         }
         // Country.
         if (isset($_POST['country'])) {
             $payment->country = sanitize_text_field($_POST['country']);
         }
         if (!empty($errors)) {
             ib_edu_message('edit_payment_errors', $errors);
             return;
         }
         if ($payment->save()) {
             // Update payment meta.
             if (isset($_POST['line_id']) && is_array($_POST['line_id'])) {
                 foreach ($_POST['line_id'] as $key => $line_id) {
                     if (!is_numeric($line_id)) {
                         continue;
                     }
                     $payment->update_line(array('ID' => $line_id, 'object_id' => isset($_POST['line_object_id'][$key]) ? intval($_POST['line_object_id'][$key]) : 0, 'line_type' => isset($_POST['line_type'][$key]) ? sanitize_text_field($_POST['line_type'][$key]) : '', 'amount' => isset($_POST['line_amount'][$key]) ? sanitize_text_field($_POST['line_amount'][$key]) : 0.0, 'tax' => isset($_POST['line_tax'][$key]) ? sanitize_text_field($_POST['line_tax'][$key]) : 0.0, 'name' => isset($_POST['line_name'][$key]) ? sanitize_text_field($_POST['line_name'][$key]) : ''));
                 }
             }
             $api = IB_Educator::get_instance();
             $entry_saved = true;
             // Create entry for the student.
             // Implemented for the "course" payment type.
             if (isset($_POST['create_entry']) && !$api->get_entry(array('payment_id' => $payment->ID))) {
                 $entry = edr_get_entry();
                 $entry->course_id = $payment->course_id;
                 $entry->user_id = $payment->user_id;
                 $entry->payment_id = $payment->ID;
                 $entry->entry_status = 'inprogress';
                 $entry->entry_date = date('Y-m-d H:i:s');
                 $entry_saved = $entry->save();
                 if ($entry_saved) {
                     // Send notification email to the student.
                     $student = get_user_by('id', $payment->user_id);
                     $course = get_post($payment->course_id, OBJECT, 'display');
                     if ($student && $course) {
                         ib_edu_send_notification($student->user_email, 'student_registered', array('course_title' => $course->post_title), array('student_name' => $student->display_name, 'course_title' => $course->post_title, 'course_excerpt' => $course->post_excerpt));
                     }
                 }
             }
             // Setup membership for the student.
             if (isset($_POST['setup_membership']) && 'membership' == $payment->payment_type) {
                 $ms = Edr_Memberships::get_instance();
                 // Setup membership.
                 $ms->setup_membership($payment->user_id, $payment->object_id);
                 // Send notification email.
                 $student = get_user_by('id', $payment->user_id);
                 $membership = $ms->get_membership($payment->object_id);
                 if ($student && $membership) {
                     $user_membership = $ms->get_user_membership($student->ID);
                     $membership_meta = $ms->get_membership_meta($membership->ID);
                     $expiration = $user_membership ? $user_membership['expiration'] : 0;
                     ib_edu_send_notification($student->user_email, 'membership_register', array(), array('student_name' => $student->display_name, 'membership' => $membership->post_title, 'expiration' => $expiration ? date_i18n(get_option('date_format'), $expiration) : __('None', 'ibeducator'), 'price' => $ms->format_price($membership_meta['price'], $membership_meta['duration'], $membership_meta['period'], false)));
                 }
             }
             if ($entry_saved) {
                 wp_redirect(admin_url('admin.php?page=ib_educator_payments&edu-action=edit-payment&payment_id=' . $payment->ID . '&edu-message=saved'));
                 exit;
             }
         }
     }
 }
    /**
     * Send the membership expiration emails to users.
     */
    public static function send_expiration_notifications()
    {
        global $wpdb;
        $days_notify = ib_edu_get_option('days_notify', 'memberships');
        if (null === $days_notify) {
            $days_notify = 5;
        } else {
            $days_notify = absint($days_notify);
        }
        $expires_date = date('Y-m-d', strtotime('+ ' . $days_notify . ' days'));
        $tables = ib_edu_table_names();
        $users = $wpdb->get_results($wpdb->prepare('SELECT u.ID, u.user_email, u.display_name, m.expiration, m.membership_id
			FROM ' . $tables['members'] . ' m
			INNER JOIN ' . $wpdb->users . ' u ON u.ID = m.user_id
			WHERE m.`expiration` LIKE %s AND m.`status` = %s', $expires_date . '%', 'active'));
        if (empty($users)) {
            return;
        }
        // Get memberships.
        $membership_ids = array();
        foreach ($users as $user) {
            if (!in_array($user->membership_id, $membership_ids)) {
                $membership_ids[] = $user->membership_id;
            }
        }
        $memberships = get_posts(array('post_type' => 'ib_edu_membership', 'include' => $membership_ids, 'post_status' => 'publish', 'posts_per_page' => -1));
        if ($memberships) {
            foreach ($memberships as $key => $membership) {
                $memberships[$membership->ID] = $membership;
                unset($memberships[$key]);
            }
            foreach ($users as $user) {
                ib_edu_send_notification($user->user_email, 'membership_renew', array(), array('student_name' => $user->display_name, 'membership' => isset($memberships[$user->membership_id]) ? $memberships[$user->membership_id]->post_title : '', 'expiration' => date_i18n(get_option('date_format'), strtotime($user->expiration)), 'membership_payment_url' => ib_edu_get_endpoint_url('edu-membership', $user->membership_id, get_permalink(ib_edu_page_id('payment')))));
            }
        }
    }
Example #3
0
 /**
  * Setup payment item (e.g. course, membership).
  *
  * @param IB_Educator_Payment $payment
  */
 public function setup_payment_item($payment)
 {
     if ('course' == $payment->payment_type) {
         // Setup course entry.
         $entry = $this->get_entry(array('payment_id' => $payment->ID));
         if (!$entry) {
             $entry = IB_Educator_Entry::get_instance();
             $entry->course_id = $payment->course_id;
             $entry->user_id = $payment->user_id;
             $entry->payment_id = $payment->ID;
             $entry->entry_status = 'inprogress';
             $entry->entry_date = date('Y-m-d H:i:s');
             $entry->save();
             // Send notification email to the student.
             $student = get_user_by('id', $payment->user_id);
             $course = get_post($payment->course_id, OBJECT, 'display');
             if ($student && $course) {
                 ib_edu_send_notification($student->user_email, 'student_registered', array('course_title' => $course->post_title), array('student_name' => $student->display_name, 'course_title' => $course->post_title, 'course_excerpt' => $course->post_excerpt));
             }
         }
     } elseif ('membership' == $payment->payment_type) {
         // Setup membership.
         $ms = IB_Educator_Memberships::get_instance();
         $ms->setup_membership($payment->user_id, $payment->object_id);
         $student = get_user_by('id', $payment->user_id);
         $membership = $ms->get_membership($payment->object_id);
         if ($student && $membership) {
             $user_membership = $ms->get_user_membership($student->ID);
             $membership_meta = $ms->get_membership_meta($membership->ID);
             $expiration = $user_membership ? $user_membership['expiration'] : 0;
             ib_edu_send_notification($student->user_email, 'membership_register', array(), array('student_name' => $student->display_name, 'membership' => $membership->post_title, 'expiration' => $expiration ? date_i18n(get_option('date_format'), $expiration) : __('None', 'ibeducator'), 'price' => $ms->format_price($membership_meta['price'], $membership_meta['duration'], $membership_meta['period'], false)));
         }
     }
 }
 /**
  * AJAX: add grade for a quiz.
  */
 public static function quiz_grade()
 {
     global $wpdb;
     $api = IB_Educator::get_instance();
     $entry_id = isset($_POST['entry_id']) ? absint($_POST['entry_id']) : 0;
     $lesson_id = isset($_POST['lesson_id']) ? absint($_POST['lesson_id']) : 0;
     // Verify nonce.
     if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'ibedu_edit_progress_' . $entry_id)) {
         exit;
     }
     // Verify capabilities.
     if (!current_user_can('edit_ib_educator_lesson', $lesson_id)) {
         exit;
     }
     $quiz_grade = $api->get_quiz_grade($lesson_id, $entry_id);
     if (!$quiz_grade) {
         exit;
     }
     $grade = isset($_POST['grade']) ? floatval($_POST['grade']) : 0;
     $api->update_quiz_grade($quiz_grade->ID, array('grade' => $grade, 'status' => 'approved'));
     // Send notification email to the student.
     $entry = IB_Educator_Entry::get_instance($entry_id);
     $student = get_user_by('id', $entry->user_id);
     if ($student) {
         $lesson_title = get_the_title($lesson_id);
         ib_edu_send_notification($student->user_email, 'quiz_grade', array('lesson_title' => $lesson_title), array('student_name' => $student->display_name, 'lesson_title' => $lesson_title, 'grade' => ib_edu_format_grade($grade)));
     }
     echo json_encode(array('status' => 'success'));
     exit;
 }