| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Post_Type_Actions |
| 5 |
* |
| 6 |
* @since 3.1.0 |
| 7 |
*/ |
| 8 |
class LP_Post_Type_Actions { |
| 9 |
|
| 10 |
/** |
| 11 |
* @var LP_Post_Type_Actions |
| 12 |
*/ |
| 13 |
protected static $instance = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* @var array |
| 17 |
*/ |
| 18 |
protected $data = array(); |
| 19 |
|
| 20 |
/** |
| 21 |
* LP_Post_Type_Actions constructor. |
| 22 |
*/ |
| 23 |
protected function __construct() { |
| 24 |
//add_action( 'save_post', array( $this, 'save_post' ) ); |
| 25 |
//add_action( 'learn-press/added-item-to-section', array( $this, 'added_item_to_section' ), 10, 3 ); |
| 26 |
//add_action( 'learn-press/removed-item-from-section', array( $this, 'removed_item_from_section' ), 10, 2 ); |
| 27 |
|
| 28 |
add_filter( 'pre_trash_post', array( $this, 'pre_trash_post' ), 10, 2 ); |
| 29 |
add_filter( 'trashed_post', array( $this, 'trashed_post' ), 1000, 1 ); |
| 30 |
|
| 31 |
//add_filter( 'transition_post_status', array( $this, 'transition_post_status' ), 1000, 3 ); |
| 32 |
} |
| 33 |
|
| 34 |
public function __get( $key ) { |
| 35 |
return $this->get( $key ); |
| 36 |
} |
| 37 |
|
| 38 |
public function __set( $key, $value ) { |
| 39 |
$this->data[ $key ] = $value; |
| 40 |
} |
| 41 |
|
| 42 |
public function get( $key ) { |
| 43 |
return array_key_exists( $key, $this->data ) ? $this->data[ $key ] : false; |
| 44 |
} |
| 45 |
|
| 46 |
public function add( $key, $value, $single = true ) { |
| 47 |
if ( empty( $this->data[ $key ] ) ) { |
| 48 |
$this->data[ $key ] = array(); |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! $single || ! is_array( $value ) ) { |
| 52 |
$this->data[ $key ][] = $value; |
| 53 |
} else { |
| 54 |
if ( is_array( $value ) ) { |
| 55 |
foreach ( $value as $v ) { |
| 56 |
$this->add( $key, $v ); |
| 57 |
} |
| 58 |
} else { |
| 59 |
$this->data[ $key ][] = $value; |
| 60 |
} |
| 61 |
|
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if ( is_array( $value ) ) { |
| 66 |
$this->data[ $key ][] = $value; |
| 67 |
} else { |
| 68 |
if ( array_search( $value, $this->data[ $key ] ) === false ) { |
| 69 |
$this->data[ $key ][] = $value; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function delete( $key ) { |
| 75 |
if ( array_key_exists( $key, $this->data ) ) { |
| 76 |
unset( $this->data[ $key ] ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @param mixed $null |
| 82 |
* @param WP_Post $post |
| 83 |
* |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public function pre_trash_post( $null, $post ) { |
| 87 |
if ( $this->is_course_item( $post ) ) { |
| 88 |
$curd = new LP_Course_CURD(); |
| 89 |
$course_ids = $curd->get_course_by_item( $post->ID ); |
| 90 |
|
| 91 |
if ( $course_ids ) { |
| 92 |
$this->add( 'item-courses-' . $post->ID, $course_ids ); |
| 93 |
} |
| 94 |
} elseif ( $this->is_course( $post ) ) { |
| 95 |
$this->add( 'trashed-course', $post->ID ); |
| 96 |
} |
| 97 |
|
| 98 |
return $null; |
| 99 |
} |
| 100 |
|
| 101 |
public function trashed_post( $post_id ) { |
| 102 |
if ( $this->is_course_item( $post_id ) ) { |
| 103 |
$course_ids = $this->get( 'item-courses-' . $post_id ); |
| 104 |
|
| 105 |
if ( $course_ids ) { |
| 106 |
$curd = new LP_Course_CURD(); |
| 107 |
|
| 108 |
foreach ( $course_ids as $course_id ) { |
| 109 |
$curd->remove_item( $post_id, $course_id ); |
| 110 |
//do_action( 'learn-press/removed-course-item', $course_id ); |
| 111 |
} |
| 112 |
|
| 113 |
$this->delete( 'item-courses-' . $post_id ); |
| 114 |
} |
| 115 |
} elseif ( $this->is_course( $post_id ) ) { |
| 116 |
do_action( 'learn-press/trashed-course', $post_id ); |
| 117 |
} elseif ( get_post_type( $post_id ) == LP_QUESTION_CPT ) { |
| 118 |
global $wpdb; |
| 119 |
$wpdb->delete( $wpdb->prefix . 'learnpress_quiz_questions', array( 'question_id' => $post_id ), array( '%d' ) ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @editor tungnx |
| 125 |
* @modify 4.1.4.1 - comment - not use |
| 126 |
*/ |
| 127 |
/*public function transition_post_status( $old, $new, $post ) { |
| 128 |
if ( $this->is_course( $post ) ) { |
| 129 |
|
| 130 |
} elseif ( $this->is_course_item( $post ) ) { |
| 131 |
$curd = new LP_Course_CURD(); |
| 132 |
$course_ids = $curd->get_course_by_item( $post->ID ); |
| 133 |
|
| 134 |
if ( $course_ids ) { |
| 135 |
foreach ( $course_ids as $course_id ) { |
| 136 |
do_action( 'learn-press/transition-course-item-status', $post->ID, $course_id, $old, $new ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
}*/ |
| 141 |
|
| 142 |
/*public function added_item_to_section( $item, $section_id, $course_id ) { |
| 143 |
do_action( 'learn-press/added-course-item', $item['id'], $course_id ); |
| 144 |
}*/ |
| 145 |
|
| 146 |
/*public function removed_item_from_section( $item, $course_id ) { |
| 147 |
do_action( 'learn-press/removed-course-item', $course_id ); |
| 148 |
}*/ |
| 149 |
|
| 150 |
/** |
| 151 |
* @editor tungnx |
| 152 |
* @modify 4.1.4.1 comment - not use |
| 153 |
*/ |
| 154 |
/*public function save_post( $post_id ) { |
| 155 |
$post_type = get_post_type( $post_id ); |
| 156 |
|
| 157 |
if ( LP_COURSE_CPT === $post_type ) { |
| 158 |
do_action( 'learn-press/save-course', $post_id ); |
| 159 |
} elseif ( learn_press_is_support_course_item_type( $post_type ) ) { |
| 160 |
$this->add( 'update-post', $post_id ); |
| 161 |
} |
| 162 |
}*/ |
| 163 |
|
| 164 |
public function is_course( $post ) { |
| 165 |
if ( is_numeric( $post ) ) { |
| 166 |
$post_id = absint( $post ); |
| 167 |
} else { |
| 168 |
$post_id = absint( $post->ID ); |
| 169 |
} |
| 170 |
|
| 171 |
return LP_COURSE_CPT === get_post_type( $post_id ); |
| 172 |
} |
| 173 |
|
| 174 |
public function is_course_item( $post ) { |
| 175 |
if ( is_numeric( $post ) ) { |
| 176 |
$post_id = absint( $post ); |
| 177 |
} else { |
| 178 |
$post_id = absint( $post->ID ); |
| 179 |
} |
| 180 |
|
| 181 |
$post_type = get_post_type( $post_id ); |
| 182 |
|
| 183 |
return learn_press_is_support_course_item_type( $post_type ); |
| 184 |
} |
| 185 |
|
| 186 |
public static function instance() { |
| 187 |
if ( ! self::$instance ) { |
| 188 |
self::$instance = new self(); |
| 189 |
} |
| 190 |
|
| 191 |
return self::$instance; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return LP_Post_Type_Actions::instance(); |
| 196 |
|