php [CoursePress] - Cloner Shrortcode

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [CoursePress] - Cloner Shrortcode相关的知识,希望对你有一定的参考价值。

<?php
/*
Plugin Name: [CoursePress] - Cloner Shrortcode
Plugin URI: https://premium.wpmudev.org/
Description: Provides a shortcode to clone courses via ajax
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/



if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_CP_Course_Cloner' ) ) {

	class WPMUDEV_CP_Course_Cloner {

		private static $_instance = null;
		private static $_shortcode = 'wpmudev_cp_course_clone_button';

		public static function get_instance() {

			if( is_null( self::$_instance ) ){
				self::$_instance = new WPMUDEV_CP_Course_Cloner();
			}
			return self::$_instance;
		}


		public function __construct() {

			if( ! class_exists('CoursePress_Data_Course') ){
				return;
			}

			add_shortcode( self::$_shortcode, array( $this, 'course_cloner_button' ) );

			//Ideally instead wp_footer a script file could be enqueued and localized properly in a plugin. This is a one pager example :)
			add_action( 'wp_footer', array( $this, 'cloner_script' ), 10 );

			add_action( 'wp_ajax_wpmudev_cp_front_course_cloner', array( $this, 'call_cp_cloner' ), 10 );
			add_action( 'wp_ajax_nopriv_wpmudev_cp_front_course_cloner', array( $this, 'call_cp_cloner' ), 10 );


		}


		public function call_cp_cloner(){
						
			if( ! is_user_logged_in() ){
				return false;
			}						

			check_ajax_referer( 'wpudev-cp-cloner-nonce', 'security' );
			$course_id = absint( $_POST['course_id'] );
			$course_data = array( 'course_id' => $course_id );
			$data = array(
				'data' => (object)$course_data
			);

			$json_data = CoursePress_Data_Course::duplicate_course( (object)$data );
			$success = (bool) $json_data['success'];

			if ( $success ) {

				// force removal of MP meta stuffs
				delete_post_meta( $json_data['course_id'], 'cp_mp_product_id' );
				delete_post_meta( $json_data['course_id'], 'cp_mp_sku' );
				delete_post_meta( $json_data['course_id'], 'cp_mp_auto_sku' );

				//if( ! current_user_can( 'manage_options' ) ){

					//If not administrator end add user as instructor
					$course_instructors = array();
					$instructors = (array) CoursePress_Data_Course::get_setting( $course_id, 'instructors', array() );

					if( ! empty( $instructors ) ){
						foreach ( $instructors as $instructor_id ) {
							CoursePress_Data_Course::remove_instructor( (int)$json_data['course_id'], $instructor_id) ;
						}
					}

					CoursePress_Data_Course::add_instructor( $json_data['course_id'], get_current_user_id() );
				//}
			}
			
			wp_send_json( $success );
		}


		public function course_cloner_button( $atts ){

			if( ! is_user_logged_in() ){
				return false;
			}

			$atts = shortcode_atts(
			array(
				'course_id' => null,
				'description' => null
			), $atts, 'WPMUDEV_CP_Course_Cloner/cloner_button_atts' );

			if( is_null( $atts['course_id'] ) || ! is_numeric( $atts['course_id'] ) ){
				return;
			}

			$course_id = absint( $atts['course_id'] );
			$course = get_post( $course_id );

			if( $course->post_type != CoursePress_Data_Course::get_post_type_name() ){
				return;
			}

			if( is_null( $atts['description'] ) ){
				$atts['description'] = __( 'Clone ', 'textdomain' ) . $course->post_title;
			}

			ob_start();
			?>
			<span class="wpmudev_cloner_button_wrap course-id-<?php echo $course_id; ?>">
				<a class="button button-primary wpmudev-course-cloner-caller" data-course-id="<?php echo $course->ID; ?>" href="#"><?php echo $atts['description']; ?></a>
				<span class="cloner_button_spinner" style="display: none;">
					<img src="<?php echo includes_url(); ?>/images/spinner.gif"/>
				</span>
			</span>
			<?php
			return ob_get_clean();

		}


		public function cloner_script(){

			global $post;

			if( ! has_shortcode( $post->post_content, self::$_shortcode ) ){
				return;
			}

			$ajax_url = admin_url( 'admin-ajax.php' );
			$ajax_nonce = wp_create_nonce( "wpudev-cp-cloner-nonce" );

			?>
			<script type="text/javascript">
			(function($){
				$(document).ready(function(){
					$(document).on( 'click', '.wpmudev-course-cloner-caller', function(e){
						e.preventDefault();

						var ajaxurl = '<?php echo $ajax_url ?>';
						var course_id = $(this).data( 'course-id' );
						var spinner = $( this ).parent().find( '.cloner_button_spinner' );

						spinner.show( 250 );

						var data = {
							action 			: 'wpmudev_cp_front_course_cloner',
							security		: '<?php echo $ajax_nonce; ?>',
							course_id		: course_id,
						};

						$.post(ajaxurl, data, function(response) {
							spinner.fadeOut( 250 );
						});

					});
				});
			})(jQuery);
			</script>
			<?php

		}

	}

	add_action( 'plugins_loaded', function(){
		$GLOBALS['WPMUDEV_CP_Course_Cloner'] = WPMUDEV_CP_Course_Cloner::get_instance();
	}, 10 );

}

以上是关于php [CoursePress] - Cloner Shrortcode的主要内容,如果未能解决你的问题,请参考以下文章

php [CoursePress 2] - 回应历史。帮助及时监控学生的反应

php [CoursePress 2] - 订单单元部分 - 提供自定义管理页面以重新排序单元的部分

php 一个自定义函数,返回一个数组,其中包含CoursePress的每个用户ID的讲师和学生角色

php [CoursePress] - 提供一组可以创建课程和单元的功能。可以使用eval从wp-cli调用这些函数

1116: [POI2008]CLO

BZOJ 1116--CLO(并查集)