Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 26, 2017 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/a9e092bd60d758af1794870675c6ea36 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/a9e092bd60d758af1794870675c6ea36 to your computer and use it in GitHub Desktop.
[CoursePress] - Provides a set of functions that can create Courses and Units. These functions can be called from wp-cli with eval
<?php
/*
Plugin Name: [CoursePress] - Set of functions that ca nbe used in wp-cli
Plugin URI: https://premium.wpmudev.org/
Description: Provides a set of functions that can create Courses and Units. These functions can be called from wp-cli with eval
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if( ! class_exists( 'WPMUDEV_CP_CLI' ) ){
class WPMUDEV_CP_CLI{
public static function duplicate_course( $course_id = null ){
if ( ! defined( 'WP_CLI' ) || ! WP_CLI || is_null( $course_id ) ) {
return false;
}
$course_id = absint( $course_id );
if ( ! CoursePress_Data_Course::is_course( $course_id ) ) {
return;
}
$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' );
$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() );
//If yo uneed to publish the course
wp_publish_post( (int)$json_data['course_id'] );
}
}
public static function add_unit( $course_id = null, $data = array() ){
if ( ! defined( 'WP_CLI' ) || ! WP_CLI || is_null( $course_id ) ) {
return false;
}
$course_id = absint( $course_id );
if ( ! CoursePress_Data_Course::is_course( $course_id ) ) {
return;
}
$unit = array(
'post_title' => __( 'Untitled Unit', 'cp' ),
'post_type' => CoursePress_Data_Unit::get_post_type_name(),
'post_status' => 'draft',
'post_parent' => $course_id,
);
if( isset( $data['title'] ) ){
$unit['post_title'] = sanitize_text_field( $data['title'] );
}
if( isset( $data['status'] ) && $data['status'] = 'publish' ){
$unit['post_status'] = 'publish';
}
$unit_id = wp_insert_post( $unit );
$unit['ID'] = $unit_id;
$unit['meta'] = array(
'unit_order' => 1,
'page_title' => array(
'page_1' => '',
),
'show_page_title' => array( true ),
);
foreach ( $unit['meta'] as $key => $value ) {
$success = add_post_meta( $unit_id, $key, $value, true );
if ( ! $success ) {
update_post_meta( $unit_id, $key, $value );
}
}
}
public static function add_unit_page( $unit_id = null, $page_title ){
if ( ! defined( 'WP_CLI' ) || ! WP_CLI || is_null( $unit_id ) ) {
return false;
}
$unit_id = (int)$unit_id;
$page_num = 1;
$unit_pages = get_post_meta( $unit_id, 'page_title', true );
if( ! empty( $unit_pages ) ){
$page_num = count( $unit_pages );
$page_num ++;
}
$unit_pages[ 'page_' . $page_num ] = sanitize_text_field( $page_title );
update_post_meta( $unit_id, 'page_title', $unit_pages );
}
public static function add_module( $unit_id = null, $module_data = array(), $module_type = null, $answers = array(), $answers_selected = 0, $module_page = 1, $module_meta = array() ){
if ( ! defined( 'WP_CLI' ) || ! WP_CLI || is_null( $unit_id ) ) {
return false;
}
//[TODO] Check available module_types
if(
! isset( $module_data['title'] ) || $module_data['title'] == '' ||
is_null( $module_type ) ||
! is_array( $answers ) || empty( $answers )
){
return false;
}
$unit_id = (int)$unit_id;
$module = array(
'post_title' => sanitize_text_field( $module_data['title'] ),
'post_type' => CoursePress_Data_Module::get_post_type_name(),
'post_status' => ( isset( $module_data['status'] ) && $module_data['status'] == 'publish' ) ? 'publish' : 'draft',
'post_parent' => $unit_id,
'post_author' => 1
);
$module_id = wp_insert_post( $module );
$module['ID'] = $module_id;
$module['meta'] = array(
'module_type' => $module_type,
'answers' => serialize( $answers ),
'answers_selected' => $answers_selected,
'module_page' => $module_page,
'duration' => '0:00',
'mandatory' => false,
'show_title' => true,
'assessable' => '',
'minimum_grade' => '100',
'allow_retries' => true,
'retry_attempts' => 0,
'order' => 0,
'module_order' => 1,
'module_page' => $module_page,
'use_timer' => '',
'legacy_updated' => true
);
$module_meta = array_merge( $module['meta'], $module_meta );
foreach ( $module_meta as $key => $value ) {
$success = add_post_meta( $module_id, $key, $value, true );
if ( ! $success ) {
update_post_meta( $module_id, $key, $value );
}
}
}
}
}
/**
* Duplicate Course
* wp eval "WPMUDEV_CP_CLI::duplicate_course( $course_id );" --url=http://site.com/subsite
*
* Create Unit
* wp eval "WPMUDEV_CP_CLI::add_unit( $course_id, array( 'title'=>'Unit Title', 'status'=>'publish' ) );" --url=http://site.com/subsite
*
* Create Page
* wp eval "WPMUDEV_CP_CLI::add_unit_page( $unit_id, 'Page Three' );" --url=http://site.com/subsite
*
* Add Module:
* wp eval "WPMUDEV_CP_CLI::add_module( $unit_id, array( 'title' => 'Module title', 'status'=>'publish' ), 'input-radio', array( 'Answer A', 'Answer B' ), 0, 0 );" --url=http://site.com/subsite
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment