function course_signup($atts)
        {
            ob_start();
            $allowed = array('signup', 'login');
            extract(shortcode_atts(array('page' => isset($_REQUEST['page']) ? $_REQUEST['page'] : '', 'failed_login_text' => __('Invalid login.', 'cp'), 'failed_login_class' => 'red', 'logout_url' => '', 'signup_tag' => 'h3', 'signup_title' => __('Signup', 'cp'), 'login_tag' => 'h3', 'login_title' => __('Login', 'cp'), 'signup_url' => '', 'login_url' => '', 'redirect_url' => ''), $atts, 'course_signup'));
            $failed_login_text = sanitize_text_field($failed_login_text);
            $failed_login_class = sanitize_html_class($failed_login_class);
            $logout_url = esc_url_raw($logout_url);
            $signup_tag = sanitize_html_class($signup_tag);
            $signup_title = sanitize_text_field($signup_title);
            $login_tag = sanitize_html_class($login_tag);
            $login_title = sanitize_text_field($login_title);
            $signup_url = esc_url_raw($signup_url);
            $redirect_url = esc_url_raw($redirect_url);
            $page = in_array($page, $allowed) ? $page : 'signup';
            $signup_prefix = empty($signup_url) ? '&' : '?';
            $login_prefix = empty($login_url) ? '&' : '?';
            $signup_url = empty($signup_url) ? CoursePress::instance()->get_signup_slug(true) : $signup_url;
            $login_url = empty($login_url) ? CoursePress::instance()->get_login_slug(true) : $login_url;
            if (!empty($redirect_url)) {
                $signup_url = $signup_url . $signup_prefix . 'redirect_url=' . urlencode($redirect_url);
                $login_url = $login_url . $login_prefix . 'redirect_url=' . urlencode($redirect_url);
            }
            if (!empty($_POST['redirect_url'])) {
                $signup_url = CoursePress::instance()->get_signup_slug(true) . '?redirect_url=' . $_POST['redirect_url'];
                $login_url = CoursePress::instance()->get_login_slug(true) . '?redirect_url=' . $_POST['redirect_url'];
            }
            //Set a cookie now to see if they are supported by the browser.
            setcookie(TEST_COOKIE, 'WP Cookie check', 0, COOKIEPATH, COOKIE_DOMAIN);
            if (SITECOOKIEPATH != COOKIEPATH) {
                setcookie(TEST_COOKIE, 'WP Cookie check', 0, SITECOOKIEPATH, COOKIE_DOMAIN);
            }
            //Set a redirect for the logout form
            if (!empty($logout_url)) {
                update_option('cp_custom_login_url', $logout_url);
            }
            $form_message = '';
            $form_message_class = '';
            // Attempt a login if submitted
            if (isset($_POST['log']) && isset($_POST['pwd'])) {
                $auth = wp_authenticate_username_password(null, $_POST['log'], $_POST['pwd']);
                if (!is_wp_error($auth)) {
                    // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
                    $user = get_user_by('login', $_POST['log']);
                    $user_id = $user->ID;
                    wp_set_current_user($user_id);
                    wp_set_auth_cookie($user_id);
                    if (!empty($redirect_url)) {
                        wp_redirect(urldecode($redirect_url));
                    } else {
                        wp_redirect(CoursePress::instance()->get_student_dashboard_slug(true));
                    }
                    exit;
                } else {
                    $form_message = $failed_login_text;
                    $form_message_class = $failed_login_class;
                }
            }
            switch ($page) {
                case 'signup':
                    if (!is_user_logged_in()) {
                        if (cp_user_can_register()) {
                            ?>

							<?php 
                            $form_message_class = '';
                            $form_message = '';
                            $student = new Student(0);
                            if (isset($_POST['student-settings-submit'])) {
                                check_admin_referer('student_signup');
                                $min_password_length = apply_filters('coursepress_min_password_length', 6);
                                $student_data = array();
                                $form_errors = 0;
                                do_action('coursepress_before_signup_validation');
                                if ($_POST['username'] != '' && $_POST['first_name'] != '' && $_POST['last_name'] != '' && $_POST['email'] != '' && $_POST['password'] != '' && $_POST['password_confirmation'] != '') {
                                    if (!username_exists($_POST['username'])) {
                                        if (!email_exists($_POST['email'])) {
                                            if ($_POST['password'] == $_POST['password_confirmation']) {
                                                if (!preg_match("#[0-9]+#", $_POST['password']) || !preg_match("#[a-zA-Z]+#", $_POST['password']) || strlen($_POST['password']) < $min_password_length) {
                                                    $form_message = sprintf(__('Your password must be at least %d characters long and have at least one letter and one number in it.', 'cp'), $min_password_length);
                                                    $form_message_class = 'red';
                                                    $form_errors++;
                                                } else {
                                                    if ($_POST['password_confirmation']) {
                                                        $student_data['user_pass'] = $_POST['password'];
                                                    } else {
                                                        $form_message = __("Passwords don't match", 'cp');
                                                        $form_message_class = 'red';
                                                        $form_errors++;
                                                    }
                                                }
                                            } else {
                                                $form_message = __('Passwords don\'t match', 'cp');
                                                $form_message_class = 'red';
                                                $form_errors++;
                                            }
                                            $student_data['role'] = 'student';
                                            $student_data['user_login'] = $_POST['username'];
                                            $student_data['user_email'] = $_POST['email'];
                                            $student_data['first_name'] = $_POST['first_name'];
                                            $student_data['last_name'] = $_POST['last_name'];
                                            if (!is_email($_POST['email'])) {
                                                $form_message = __('E-mail address is not valid.', 'cp');
                                                $form_message_class = 'red';
                                                $form_errors++;
                                            }
                                            if (isset($_POST['tos_agree'])) {
                                                if ($_POST['tos_agree'] == '0') {
                                                    $form_message = __('You must agree to the Terms of Service in order to signup.', 'cp');
                                                    $form_message_class = 'red';
                                                    $form_errors++;
                                                }
                                            }
                                            if ($form_errors == 0) {
                                                if ($student_id = $student->add_student($student_data) !== 0) {
                                                    //$form_message = __( 'Account created successfully! You may now <a href="' . ( get_option( 'use_custom_login_form', 1 ) ? trailingslashit( site_url() . '/' . $this->get_login_slug() ) : wp_login_url() ) . '">log into your account</a>.', 'cp' );
                                                    //$form_message_class = 'regular';
                                                    $email_args['email_type'] = 'student_registration';
                                                    $email_args['student_id'] = $student_id;
                                                    $email_args['student_email'] = $student_data['user_email'];
                                                    $email_args['student_first_name'] = $student_data['first_name'];
                                                    $email_args['student_last_name'] = $student_data['last_name'];
                                                    $email_args['student_username'] = $student_data['user_login'];
                                                    $email_args['student_password'] = $student_data['user_pass'];
                                                    coursepress_send_email($email_args);
                                                    $creds = array();
                                                    $creds['user_login'] = $student_data['user_login'];
                                                    $creds['user_password'] = $student_data['user_pass'];
                                                    $creds['remember'] = true;
                                                    $user = wp_signon($creds, false);
                                                    if (is_wp_error($user)) {
                                                        $form_message = $user->get_error_message();
                                                        $form_message_class = 'red';
                                                    }
                                                    // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
                                                    if (isset($_POST['course_id']) && is_numeric($_POST['course_id'])) {
                                                        $course = new Course($_POST['course_id']);
                                                        wp_redirect($course->get_permalink());
                                                    } else {
                                                        if (!empty($redirect_url)) {
                                                            wp_redirect(apply_filters('coursepress_redirect_after_signup_redirect_url', $redirect_url));
                                                        } else {
                                                            wp_redirect(apply_filters('coursepress_redirect_after_signup_url', CoursePress::instance()->get_student_dashboard_slug(true)));
                                                        }
                                                    }
                                                    exit;
                                                } else {
                                                    $form_message = __('An error occurred while creating the account. Please check the form and try again.', 'cp');
                                                    $form_message_class = 'red';
                                                }
                                            }
                                        } else {
                                            $form_message = __('Sorry, that email address is already used!', 'cp');
                                            $form_message_class = 'error';
                                        }
                                    } else {
                                        $form_message = __('Username already exists. Please choose another one.', 'cp');
                                        $form_message_class = 'red';
                                    }
                                } else {
                                    $form_message = __('All fields are required.', 'cp');
                                    $form_message_class = 'red';
                                }
                            } else {
                                $form_message = __('All fields are required.', 'cp');
                            }
                            ?>
							<?php 
                            //ob_start();
                            if (!empty($signup_title)) {
                                echo '<' . $signup_tag . '>' . $signup_title . '</' . $signup_tag . '>';
                            }
                            ?>

							<p class="form-info-<?php 
                            echo apply_filters('signup_form_message_class', sanitize_text_field($form_message_class));
                            ?>
"><?php 
                            echo apply_filters('signup_form_message', sanitize_text_field($form_message));
                            ?>
</p>

							<?php 
                            do_action('coursepress_before_signup_form');
                            ?>

							<form id="student-settings" name="student-settings" method="post" class="student-settings">

								<?php 
                            do_action('coursepress_before_all_signup_fields');
                            ?>

								<input type="hidden" name="course_id" value="<?php 
                            esc_attr_e(isset($_GET['course_id']) ? $_GET['course_id'] : ' ');
                            ?>
"/>
								<input type="hidden" name="redirect_url" value="<?php 
                            echo $redirect_url;
                            ?>
"/>

								<label>
									<?php 
                            _e('First Name', 'cp');
                            ?>
:
									<input type="text" name="first_name" value="<?php 
                            echo isset($_POST['first_name']) ? $_POST['first_name'] : '';
                            ?>
"/>
								</label>

								<?php 
                            do_action('coursepress_after_signup_first_name');
                            ?>

								<label>
									<?php 
                            _e('Last Name', 'cp');
                            ?>
:
									<input type="text" name="last_name" value="<?php 
                            echo isset($_POST['last_name']) ? $_POST['last_name'] : '';
                            ?>
"/>
								</label>

								<?php 
                            do_action('coursepress_after_signup_last_name');
                            ?>

								<label>
									<?php 
                            _e('Username', 'cp');
                            ?>
:
									<input type="text" name="username" value="<?php 
                            echo isset($_POST['username']) ? $_POST['username'] : '';
                            ?>
"/>
								</label>

								<?php 
                            do_action('coursepress_after_signup_username');
                            ?>

								<label>
									<?php 
                            _e('E-mail', 'cp');
                            ?>
:
									<input type="text" name="email" value="<?php 
                            echo isset($_POST['email']) ? $_POST['email'] : '';
                            ?>
"/>
								</label>

								<?php 
                            do_action('coursepress_after_signup_email');
                            ?>

								<label>
									<?php 
                            _e('Password', 'cp');
                            ?>
:
									<input type="password" name="password" value=""/>
								</label>

								<?php 
                            do_action('coursepress_after_signup_password');
                            ?>

								<label class="right">
									<?php 
                            _e('Confirm Password', 'cp');
                            ?>
:
									<input type="password" name="password_confirmation" value=""/>
								</label>
								<br clear="both"/><br/>

								<?php 
                            if (shortcode_exists('signup-tos')) {
                                if (get_option('show_tos', 0) == '1') {
                                    ?>
										<label class="full"><?php 
                                    echo do_shortcode('[signup-tos]');
                                    ?>
</label>
									<?php 
                                }
                            }
                            ?>

								<?php 
                            do_action('coursepress_after_all_signup_fields');
                            ?>

								<label class="full">
									<?php 
                            printf(__('Already have an account? %s%s%s!', 'cp'), '<a href="' . $login_url . '">', __('Login to your account', 'cp'), '</a>');
                            ?>
								</label>

								<label class="full-right">
									<input type="submit" name="student-settings-submit" class="apply-button-enrolled" value="<?php 
                            _e('Create an Account', 'cp');
                            ?>
"/>
								</label>

								<?php 
                            do_action('coursepress_after_submit');
                            ?>

								<?php 
                            wp_nonce_field('student_signup');
                            ?>
							</form>
							<div class="clearfix" style="clear: both;"></div>

							<?php 
                            do_action('coursepress_after_signup_form');
                            ?>
							<?php 
                            //$content = ob_get_clean();
                            // Return the html in the buffer.
                            //return $content;
                        } else {
                            _e('Registrations are not allowed.', 'cp');
                        }
                    } else {
                        if (!empty($redirect_url)) {
                            wp_redirect(urldecode($redirect_url));
                        } else {
                            wp_redirect(CoursePress::instance()->get_student_dashboard_slug(true));
                        }
                        exit;
                    }
                    break;
                case 'login':
                    ?>
					<?php 
                    if (!empty($login_title)) {
                        echo '<' . $login_tag . '>' . $login_title . '</' . $login_tag . '>';
                    }
                    ?>
					<p class="form-info-<?php 
                    echo apply_filters('signup_form_message_class', sanitize_text_field($form_message_class));
                    ?>
"><?php 
                    echo apply_filters('signup_form_message', sanitize_text_field($form_message));
                    ?>
</p>
					<?php 
                    do_action('coursepress_before_login_form');
                    ?>
					<form name="loginform" id="student-settings" class="student-settings" method="post">
						<?php 
                    do_action('coursepress_after_start_form_fields');
                    ?>

						<label>
							<?php 
                    _e('Username', 'cp');
                    ?>
:
							<input type="text" name="log" value="<?php 
                    echo isset($_POST['log']) ? $_POST['log'] : '';
                    ?>
"/>
						</label>

						<label>
							<?php 
                    _e('Password', 'cp');
                    ?>
:
							<input type="password" name="pwd" value="<?php 
                    echo isset($_POST['pwd']) ? $_POST['pwd'] : '';
                    ?>
"/>
						</label>

						<?php 
                    do_action('coursepress_form_fields');
                    ?>

						<label class="full">
							<?php 
                    if (cp_user_can_register()) {
                        printf(__('Don\'t have an account? %s%s%s now!', 'cp'), '<a href="' . $signup_url . '">', __('Create an Account', 'cp'), '</a>');
                    }
                    ?>
						</label>

						<label class="half-left">
							<a href="<?php 
                    echo wp_lostpassword_url();
                    ?>
"><?php 
                    _e('Forgot Password?', 'cp');
                    ?>
</a>
						</label>
						<label class="half-right">
							<input type="submit" name="wp-submit" id="wp-submit" class="apply-button-enrolled" value="<?php 
                    _e('Log In', 'cp');
                    ?>
"><br>
						</label>
						<br clear="all"/>

						<input name="redirect_to" value="<?php 
                    echo CoursePress::instance()->get_student_dashboard_slug(true);
                    ?>
" type="hidden">
						<input name="testcookie" value="1" type="hidden">
						<input name="course_signup_login" value="1" type="hidden">
						<?php 
                    do_action('coursepress_before_end_form_fields');
                    ?>
					</form>

					<?php 
                    do_action('coursepress_after_login_form');
                    ?>
					<?php 
                    break;
            }
            $content = ob_get_clean();
            // Return the html in the buffer.
            return $content;
        }
} else {
    $class_meta_query_key = 'enrolled_course_class_' . $course_id;
}
/* Invite a Student */
if (isset($_POST['invite_student'])) {
    check_admin_referer('student_invitation');
    if (CoursePress_Capabilities::can_assign_course_student($course_id)) {
        $email_args['email_type'] = 'student_invitation';
        $email_args['course_id'] = $course_id;
        $email_args['student_first_name'] = $_POST['first_name'];
        $email_args['student_last_name'] = $_POST['last_name'];
        $email_args['student_email'] = $_POST['email'];
        $email_args['enroll_type'] = $course->details->enroll_type;
        // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
        if (is_email($_POST['email'])) {
            coursepress_send_email($email_args);
            //ob_start();
            wp_redirect(admin_url('admin.php?page=course_details&tab=students&course_id=' . $course_id . '&ms=is'));
            exit;
        } else {
            //ob_start();
            wp_redirect(admin_url('admin.php?page=course_details&tab=students&course_id=' . $course_id . '&ems=wrong_email'));
            exit;
        }
    }
}
/* Enroll student or move to a different class */
if (isset($_POST['students']) && is_numeric($_POST['students'])) {
    // if( defined('DOING_AJAX') && DOING_AJAX ) { cp_write_log('doing ajax'); }
    check_admin_referer('student_details');
    $student = new Student($_POST['students']);
Exemplo n.º 3
0
 function send_instructor_invite()
 {
     $user_id = (int) $_POST['user_id'];
     $course_id = (int) $_POST['course_id'];
     $email = sanitize_email($_POST['email']);
     $nonce_check = wp_verify_nonce($_POST['instructor_nonce'], 'manage-instructors-' . $user_id);
     $cap = CoursePress_Capabilities::can_assign_course_instructor($course_id, $user_id);
     // same capability as adding
     $doing_ajax = defined('DOING_AJAX') && DOING_AJAX ? true : false;
     $ajax_response = array();
     if ($nonce_check && $cap && $doing_ajax) {
         $email_args['email_type'] = 'instructor_invitation';
         $email_args['first_name'] = sanitize_text_field($_POST['first_name']);
         $email_args['last_name'] = sanitize_text_field($_POST['last_name']);
         $email_args['instructor_email'] = $email;
         $user = get_user_by('email', $email_args['instructor_email']);
         if ($user) {
             $email_args['user'] = $user;
         }
         $email_args['course_id'] = $course_id;
         $ajax_status = 1;
         //success
         // Get the invite meta for this course and add the new invite
         $invite_exists = false;
         if ($instructor_invites = get_post_meta($email_args['course_id'], 'instructor_invites', true)) {
             foreach ($instructor_invites as $i) {
                 $invite_exists = array_search($email_args['instructor_email'], $i);
             }
         } else {
             $instructor_invites = array();
         }
         if (!$invite_exists) {
             // Generate invite code.
             $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
             $invite_code = '';
             for ($i = 0; $i < 20; $i++) {
                 $invite_code .= $characters[rand(0, strlen($characters) - 1)];
             }
             // Save the invite in the course meta. Hash will be used for user authentication.
             $email_args['invite_code'] = $invite_code;
             $invite_hash = sha1($email_args['instructor_email'] . $email_args['invite_code']);
             $email_args['invite_hash'] = $invite_hash;
             if (coursepress_send_email($email_args)) {
                 $invite = array('first_name' => $email_args['first_name'], 'last_name' => $email_args['last_name'], 'email' => $email_args['instructor_email'], 'code' => $email_args['invite_code'], 'hash' => $email_args['invite_hash']);
                 $instructor_invites[$email_args['invite_code']] = $invite;
                 update_post_meta($email_args['course_id'], 'instructor_invites', $instructor_invites);
                 $course = new Course($course_id);
                 if (current_user_can('coursepress_assign_and_assign_instructor_course_cap') || current_user_can('coursepress_assign_and_assign_instructor_my_course_cap') && $course->details->post_author == get_current_user_id()) {
                     $ajax_response['capability'] = true;
                 } else {
                     $ajax_response['capability'] = false;
                 }
                 $ajax_response['data'] = $invite;
                 $ajax_response['content'] = '<i class ="fa fa-check status status-success"></i> ' . __('Invitation successfully sent.', 'cp');
                 /**
                  * Instructor has been invited.
                  *
                  * @since 1.2.1
                  *
                  * @param int course_id The course instructor was added to.
                  * @param string email The email invite was sent to.
                  *
                  */
                 do_action('coursepress_instructor_invite_sent', $course_id, $email);
             } else {
                 $ajax_status = new WP_Error('mail_fail', __('Email failed to send.', 'cp'));
                 $ajax_response['content'] = '<i class ="fa fa-exclamation status status-fail"></i> ' . __('Email failed to send.', 'cp');
                 /**
                  * Instructor invite not sent.
                  *
                  * @since 1.2.1
                  *
                  * @param int course_id The course instructor was added to.
                  * @param int instructor_id The user ID of the new instructor.
                  *
                  */
                 do_action('coursepress_instructor_invite_mail_fail', $course_id, $email);
             }
         } else {
             $ajax_response['content'] = '<i class ="fa fa-info-circle status status-exist"></i> ' . __('Invitation already exists.', 'cp');
             /**
              * Instructor already invited.
              *
              * @since 1.2.1
              *
              * @param int course_id The course instructor was added to.
              * @param int instructor_id The user ID of the new instructor.
              *
              */
             do_action('coursepress_instructor_invite_exists', $course_id, $email);
         }
     } else {
         $ajax_status = new WP_Error('nonce_fail', __('Invalid request. Security check failed.', 'cp'));
         $ajax_response['content'] = '<i class ="fa fa-exclamation status status-fail"></i> ' . __('Invalid request. Security check failed.', 'cp');
     }
     $response = array('what' => 'instructor_invite', 'action' => 'instructor_invite', 'id' => $ajax_status, 'data' => json_encode($ajax_response));
     ob_end_clean();
     ob_start();
     $xmlResponse = new WP_Ajax_Response($response);
     $xmlResponse->send();
     ob_end_flush();
 }
 public static function enroll($course_id, $student_id, $class = '', $group = '')
 {
     $current_time = current_time('mysql');
     $global_option = !is_multisite();
     /**
      * Update metadata with relevant details.
      */
     update_user_option($student_id, 'enrolled_course_date_' . $course_id, $current_time, $global_option);
     //Link courses and student ( in order to avoid custom tables ) for easy MySql queries ( get courses stats, student courses, etc. )
     update_user_option($student_id, 'enrolled_course_class_' . $course_id, $class, $global_option);
     update_user_option($student_id, 'enrolled_course_group_' . $course_id, $group, $global_option);
     update_user_option($student_id, 'role', 'student', $global_option);
     //alternative to roles used
     /**
      * Filter can be used to override email details.
      */
     $user_info = get_userdata($student_id);
     $email_args = apply_filters('coursepress_student_enrollment_email_args', array('email_type' => 'enrollment_confirmation', 'course_id' => $course_id, 'dashboard_address' => CoursePress::instance()->get_student_dashboard_slug(true), 'student_first_name' => $user_info->first_name, 'student_last_name' => $user_info->last_name, 'student_email' => $user_info->user_email));
     /**
      * If a valid email address is given, use it to email the student with enrollment information.
      */
     if (is_email($email_args['student_email'])) {
         coursepress_send_email($email_args);
     }
     /**
      * Setup actions for when a student enrolls.
      * Can be used to create notifications or tracking student actions.
      */
     $instructors = Course::get_course_instructors_ids($course_id);
     do_action('student_enrolled_instructor_notification', $student_id, $course_id, $instructors);
     do_action('student_enrolled_student_notification', $student_id, $course_id);
     /**
      * Perform action after a Student is enrolled.
      *
      * @since 1.2.2
      */
     do_action('coursepress_student_enrolled', $student_id, $course_id);
     return true;
 }