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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [CoursePress 2] - 订单单元部分 - 提供自定义管理页面以重新排序单元的部分相关的知识,希望对你有一定的参考价值。

<?php
/*
Plugin Name: [CoursePress 2] - Order Unit Sections
Plugin URI: https://premium.wpmudev.org/
Description: Provides a custom admin page to reorder Sections of a Unit
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/


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

if ( ! class_exists( 'WPMUDEV_CP_Reorder_Sections' ) ) {
	
	class WPMUDEV_CP_Reorder_Sections {
		private static $_instance = null;
	
		public static function get_instance() {
			if( is_null( self::$_instance ) ){
				self::$_instance = new WPMUDEV_CP_Reorder_Sections();
			}
			return self::$_instance;
		}

		public function __construct() {	

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

			add_action( 'admin_menu', array( &$this, 'register_sub_menu' ), 20 );
			add_action( 'admin_head', array( &$this, 'admin_style' ), 20 );
			add_action( 'wp_ajax_wpmudev_cp_fetch_course_units', array( &$this, 'ajax_fetch_course_units' ), 10 );
			add_action( 'wp_ajax_wpmudev_cp_reorder_course_units', array( &$this, 'ajax_save_sections_order' ), 10 );
			
		}

		public function register_sub_menu(){

			$post_type = CoursePress_Data_Course::get_post_type_name();
			$parent_slug = 'edit.php?post_type=' . $post_type;
			
			$suffix = add_submenu_page(
				$parent_slug,
				__( 'Reorder sections', 'cp' ),
				__( 'Reorder sections', 'cp' ),
				'manage_options',
				'reorder-sections',
				array( $this, 'menu_page' )
			);

			add_action( "admin_print_footer_scripts-$suffix", array( $this, 'admin_scripts' ) );
		}


		public function menu_page(){

			$courses = self::get_courses();
			?>
			<h1>Reorder Units Sections</h1>

			<div style="width: 30%; float: left;">
				<h3>Courses</h3>
				<ul id="cp-course-selector">
				<?php
				foreach( $courses as $course ){
				?>
				<li><h4><a href="#" data-course="<?php echo $course->ID ?>"><?php echo $course->post_title ?></a></h4></li>
				<?php
				}

				?>
				</ul>
			</div>

			<div style="width: 65%; float: right;" id="cp-course-units-list">

			</div>

			<div class="overlay"></div>

			<?php

		}

		public function admin_scripts(){

			wp_enqueue_script( 'jquery-ui-sortable', false, array('jquery') );

			?>
			<script type="text/javascript">
				(function($){

					$(document).ready(function(){

						const 	courses 		= $( '#cp-course-selector a' ),
								response_area 	= $( '#cp-course-units-list' );

						courses.on( 'click', function(){
							let course = $(this);

							data = {
								action: 'wpmudev_cp_fetch_course_units',
								nonce: '<?php echo wp_create_nonce( 'wpmudev_cp_fetch_course_units' ); ?>',
								course_id: course.data( 'course' )
							};

							$.post(ajaxurl, data, function(response) {

								response_area.html( '<img src="/wp-admin/images/spinner-2x.gif">' );
								if( response.success ){
									response_area.html( response.content );
									$( ".sortable" ).sortable();
									$( ".sortable" ).sortable("refresh");
    								$( ".sortable" ).disableSelection();

								}
							});

							

						} );
						
						$( '#cp-course-units-list' ).on( 'click', '.save-sections-btn', function(e){
							e.preventDefault();
							let button 			= $( this ),
								unit_id 		= $(this).data('unit'),
								overlay 		= $( '.overlay' ),
								sections 		= $('.unit-sections-' + unit_id + ' .section-item' )
								sections_order 	= [];

							overlay.fadeIn( 300 );

							sections.each(function(){								
								sections_order.push( $(this).data('section') );
							});
							
							data = {
								action 			: 'wpmudev_cp_reorder_course_units',
								nonce 			: '<?php echo wp_create_nonce( 'wpmudev_cp_reorder_course_units' ); ?>',
								unit_id 		: unit_id,
								sections_order 	: sections_order
							};

							$.post(ajaxurl, data, function(response){
								overlay.hide( 300 );
								let msg = '',
									info = $('<div />', {										
										'class' : 'ajax-info'
									});

								if( response.success ){
									msg = '<i class="dashicons dashicons-yes"></i>' + response.message;
									info.addClass( 'success' );
								}
								else{
									msg = '<i class="dashicons dashicons-no"></i>Something went wrong';
									info.addClass( 'fail' );
								}

								info.html( msg );
								button.before( info );
								setTimeout(function() { info.hide( 
									300, function() {
    									$(this).remove();
  									}
								); }, 5000);

							});

						});


					});
				})(jQuery);

			</script>
			<?php

		}


		public function ajax_fetch_course_units() {

			check_ajax_referer( 'wpmudev_cp_fetch_course_units', 'nonce' );

			$data = $_POST;
			if( ! isset( $data['course_id'] ) ) {
				wp_send_json(
					array(
					    'success'	=> false,
					    'message'	=> 'No course selected'
					)
				);
			}

			$out = '';
			$course_id = (int)$data['course_id'];
			$course_units = CoursePress_Data_Course::get_units( $course_id );

			foreach ( $course_units as $unit ) {
				$out .= "<h3>{$unit->post_title}</h3>";
				

				$page_titles = get_post_meta( $unit->ID, 'page_title', true );
				$page_descriptions = get_post_meta( $unit->ID, 'page_description', true );
				$unit_out = '';

				if( is_array( $page_titles ) && ! empty( $page_titles ) ) {
					
					$row_cout = 1;
					$unit_out .= "<ul class='unit-sections unit-sections-{$unit->ID} sortable' data-unit='{$unit->ID}'>";
					foreach( $page_titles as $page_key => $page_title ) {
						if( '' == $page_title ) {
							$page_title = $page_key;
						}
						$page_description = isset( $page_descriptions[ $page_key ] )? wp_strip_all_tags( str_replace( array( '&lt;p&gt;', '&lt;/p&gt;' ), array( '', '' ), $page_descriptions[ $page_key ] ) ) : '';
						$unit_out .= "<li class='section-item' data-section='{$page_key}'><strong>{$page_title}</strong><div>{$page_description}</div></li>";
						$row_cout++;
					}
					$unit_out .= '</ul>';

					if( ! empty( $unit_out ) ) {
						$unit_out .= "<a class='button-primary save-sections-btn' data-unit='{$unit->ID}'>Save <strong>{$unit->post_title}</strong> sections</a>";
					}

				}

				$out .= $unit_out;
			}

			$return = array(
		        'success'	    => true,
		        'content'		=> $out
		    );

		    wp_send_json($return);

		}


		public function ajax_save_sections_order(){

			check_ajax_referer( 'wpmudev_cp_reorder_course_units', 'nonce' );

			$data = $_POST;
			if( ! isset( $data['unit_id'] ) ) {
				wp_send_json(
					array(
					    'success'	=> false,
					    'message'	=> 'No course selected'
					)
				);
			}

			$unit_id 				= (int)$data['unit_id'];
			$sections_order 		= $data['sections_order'];
			$section_count 			= 1;

			$page_titles 			= get_post_meta( $unit_id, 'page_title', true );
			$page_descriptions 		= get_post_meta( $unit_id, 'page_description', true );
			$show_page_title 		= get_post_meta( $unit_id, 'show_page_title', true );

			$new_page_titles 		= array();
			$new_page_descriptions 	= array();
			$new_show_page_title 	= array();

			foreach( $sections_order as $old_section_key ){
				$section_key = 'page_' . $section_count;

				if( isset( $page_titles[$old_section_key] ) ){
					$new_page_titles[$section_key] = $page_titles[$old_section_key];
				}

				if( isset( $page_descriptions[$old_section_key] ) ){
					$new_page_descriptions[$section_key] = $page_descriptions[$old_section_key];
				}

				$show_page_count = $section_count - 1;
				$old_show_key = ( (int)str_replace( 'page_', '', $old_section_key ) - 1 );
				if( isset( $show_page_title[$old_show_key] ) ) {
					$new_show_page_title[$show_page_count] = $show_page_title[$old_show_key];
				}

				$section_count++;
			}

			update_post_meta( $unit_id, 'page_title', $new_page_titles );
			update_post_meta( $unit_id, 'page_description', $new_page_descriptions );
			update_post_meta( $unit_id, 'show_page_title', $new_show_page_title );

			wp_send_json(
				array(
				    'success'	=> true,
				    'message'	=> 'Sections updated'
				)
			);

		}


		public function get_courses( $args = array() ){

			$default_args = array(
				'post_type' => CoursePress_Data_Course::get_post_type_name(),
			);

			$args = array_merge( $default_args, $args );

			$courses = new WP_Query( $args );
			return $courses->posts;

		}

		public function admin_style(){
			$screen = get_current_screen();

			if( 'course_page_reorder-sections' != $screen->id ){
				return;
			}
			?>
			<style type="text/css">
				.sortable:hover{
					cursor: move;
				}

				.overlay{
					position: fixed;top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					background-color: #000;
					filter:alpha(opacity=50);
					-moz-opacity:0.5;
					-khtml-opacity: 0.5;
					opacity: 0.5;
					z-index: 10000;
					display: none;
				}

				.ajax-info{
					margin: 10px 0px;
					padding:12px;
					width: 400px;
				}

				.ajax-info.success{
					color: #4F8A10;
    				background-color: #DFF2BF;
				}

				.ajax-info.fail{
					color: #D8000C;
    				background-color: #FFD2D2;
				}

				.ajax-info i.dashicons{
					margin:10px 22px;
				    font-size:2em;
				    vertical-align:middle;
				}

			</style>
			<?php
		}


	}

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

}

以上是关于php [CoursePress 2] - 订单单元部分 - 提供自定义管理页面以重新排序单元的部分的主要内容,如果未能解决你的问题,请参考以下文章

php [CoursePress] - 证书中的回复

php [CoursePress] - Cloner Shrortcode

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

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

ecshop程序(php内核)怎么来实现 下订单后3小时不付款,自动取消订单

PHP 订单延时处理:延迟队列