PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / Databases / class-lp-material-db.php

class-lp-material-db.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.4, at inc/Databases/class-lp-material-db.php

273 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Material_Files_DB
4 *
5 * @version 1.0.0
6 * @since 4.2.2
7 */
8 defined( 'ABSPATH' ) || exit();
9
10 if ( class_exists( 'LP_Material_Files_DB' ) ) {
11 return;
12 }
13 class LP_Material_Files_DB extends LP_Database {
14
15 private static $_instance;
16 public $table_name;
17 protected function __construct() {
18 parent::__construct();
19 $this->table_name = $this->tb_lp_files;
20 }
21
22 public static function getInstance() {
23 if ( is_null( self::$_instance ) ) {
24 self::$_instance = new self();
25 }
26
27 return self::$_instance;
28 }
29
30 /**
31 * @version 1.0.0
32 * @since 4.2.2
33 * [create_material create new material]
34 * @param [array] $data [data to create a material: file name, file type, the post id, post type, save method, file path or file url, created date ]
35 * @return [int] new record id(file_id)
36 */
37 public function create_material( $data ) {
38 if ( ! is_array( $data ) ) {
39 return;
40 }
41 if ( ! is_int( $data['item_id'] ) ) {
42 return;
43 }
44 $insert_file = $this->wpdb->insert(
45 $this->table_name,
46 $data,
47 array(
48 '%s',
49 '%s',
50 '%d',
51 '%s',
52 '%s',
53 '%s',
54 '%d',
55 '%s',
56 )
57 );
58 $this->check_execute_has_error();
59 return $insert_file ? $this->wpdb->insert_id : false;
60 }
61
62 /**
63 * @version 1.0.0
64 * @since 4.2.2
65 * [get_single_material get a material]
66 * @param [int] $file_id [file_id]
67 * @return [object||null] [A material or null]
68 */
69 public function get_material( $file_id = 0 ) {
70 if ( ! is_int( $file_id ) ) {
71 return;
72 }
73 $row = $this->wpdb->get_row(
74 $this->wpdb->prepare(
75 "SELECT * FROM $this->table_name WHERE file_id = %d",
76 $file_id
77 )
78 );
79 $this->check_execute_has_error();
80 return $row;
81 }
82 /**
83 * @version 1.0.0
84 * @since 4.2.2
85 * [get_material_by_item_id get all material files of a post( course or lesson )]
86 * @param integer $item_id [post_id]
87 * @param integer $per_page [file amount for each get files]
88 * @param integer $offset [query offset]
89 * @param boolean $is_admin [check if is admin page, use for course to get only course's files ( don't include lesson's files )]
90 * @return [array] [post's material files]
91 */
92 public function get_material_by_item_id( $item_id = 0, $perpage = 0, $offset = 0, $is_admin = false ) {
93 if ( ! is_int( $item_id ) ) {
94 return;
95 }
96 $result = array();
97 if ( get_post_type( $item_id ) == LP_COURSE_CPT && ! $is_admin ) {
98 $sql = "SELECT * FROM $this->table_name WHERE item_id
99 IN ( SELECT si.item_id FROM $this->tb_lp_section_items AS si
100 INNER JOIN $this->tb_lp_sections AS s ON s.section_id = si.section_id
101 WHERE s.section_course_id=%d )
102 OR item_id=%d ORDER BY item_id, orders";
103 if ( $perpage > 0 ) {
104 $sql .= ' LIMIT ' . intval( $perpage );
105 }
106 if ( $offset > 0 && $perpage > 0 ) {
107 $sql .= ' OFFSET ' . intval( $offset );
108 }
109 $result = $this->wpdb->get_results(
110 $this->wpdb->prepare(
111 $sql,
112 $item_id,
113 $item_id
114 )
115 );
116 } else {
117 $sql = "SELECT * FROM $this->table_name WHERE item_id = %d ORDER BY orders";
118 if ( $perpage > 0 ) {
119 $sql .= ' LIMIT ' . intval( $perpage );
120 }
121 if ( $offset > 0 && $perpage > 0 ) {
122 $sql .= ' OFFSET ' . intval( $offset );
123 }
124 $result = $this->wpdb->get_results(
125 $this->wpdb->prepare(
126 $sql,
127 $item_id
128 )
129 );
130 }
131 $this->check_execute_has_error();
132 return $result;
133 }
134
135 /**
136 * [get_total get total file amount of an item]
137 * @param [type] $item_id [description]
138 * @return [type] [description]
139 */
140 public function get_total( $item_id ) {
141 if ( ! $item_id ) {
142 return;
143 }
144 $item_id = (int) $item_id;
145 if ( get_post_type( $item_id ) == LP_COURSE_CPT ) {
146 $sql = "SELECT COUNT(file_id) FROM $this->table_name WHERE item_id
147 IN ( SELECT si.item_id FROM $this->tb_lp_section_items AS si
148 INNER JOIN $this->tb_lp_sections AS s ON s.section_id = si.section_id
149 WHERE s.section_course_id=%d )
150 OR item_id=%d ORDER BY item_id";
151 $result = $this->wpdb->get_var(
152 $this->wpdb->prepare(
153 $sql,
154 $item_id,
155 $item_id
156 )
157 );
158 } else {
159 $sql = "SELECT COUNT(file_id) FROM $this->table_name WHERE item_id = %d";
160 $result = $this->wpdb->get_var(
161 $this->wpdb->prepare(
162 $sql,
163 $item_id
164 )
165 );
166 }
167 $this->check_execute_has_error();
168 return (int) $result;
169 }
170
171 /**
172 * [update_material_orders update order of material]
173 * @param array $orders [array or materials]
174 * @param integer $item_id [item (course/lesson ID)]
175 * @return [type] [update or false]
176 */
177 public function update_material_orders( $orders = [], $item_id = 0 ) {
178 if ( empty( $orders ) ) {
179 return;
180 }
181 if ( ! $item_id ) {
182 return;
183 }
184 $prepare_arr = [];
185 $sql = "UPDATE $this->table_name SET orders = (CASE ";
186 foreach ( $orders as $id => $val ) {
187 $sql .= 'when file_id = %d then %d ';
188 $prepare_arr[] = (int) $val['file_id'];
189 $prepare_arr[] = (int) $val['orders'];
190 }
191 $prepare_arr[] = $item_id;
192 $sql .= 'END) ';
193 $sql .= 'WHERE item_id = %d';
194 $update = $this->wpdb->query( $this->wpdb->prepare( $sql, $prepare_arr ) );
195 $this->check_execute_has_error();
196 return $update ? $update : 0;
197 }
198 /**
199 * @version 1.0.0
200 * @since 4.2.2
201 * [delete_material delete a material]
202 * @param [int] $file_id [file id]
203 * @return [boolean] [description]
204 */
205 public function delete_material( $file_id = 0 ) {
206 if ( ! is_int( $file_id ) ) {
207 return;
208 }
209 $material = $this->get_material( $file_id );
210 if ( ! $material ) {
211 return;
212 }
213 $delete = $this->wpdb->delete(
214 $this->table_name,
215 array( 'file_id' => $file_id ),
216 array( '%d' )
217 );
218 $this->check_execute_has_error();
219
220 if ( $material->method == 'upload' && $delete ) {
221 $file_path = wp_upload_dir()['basedir'] . $material->file_path;
222 $this->delete_local_file( $file_path );
223 }
224 return $delete;
225 }
226 /**
227 * @version 1.0.0
228 * @since 4.2.2
229 * [delete_material_by_item_id delete all material file of an item]
230 * @param [int] $item_id [the post id]
231 * @return [boolean] [description]
232 */
233 public function delete_material_by_item_id( $item_id = 0 ) {
234 if ( ! is_int( $item_id ) ) {
235 return;
236 }
237 $materials = $this->get_material_by_item_id( $item_id );
238 if ( ! $materials ) {
239 return;
240 }
241 $delete = $this->wpdb->delete(
242 $this->table_name,
243 array( 'item_id' => $item_id ),
244 array( '%d' )
245 );
246 $this->check_execute_has_error();
247 if ( $delete ) {
248 foreach ( $materials as $m ) {
249 if ( $m->method == 'upload' ) {
250 $file_path = wp_upload_dir()['basedir'] . $m->file_path;
251 $this->delete_local_file( $file_path );
252 }
253 }
254 }
255 return $delete;
256 }
257 /**
258 * @version 1.0.0
259 * @since 4.2.2
260 * [delete_local_file delete file when record is deleted]
261 * @param string $file_path [description]
262 */
263 public function delete_local_file( $file_path = '' ) {
264 $file_init = LP_WP_Filesystem::instance();
265 if ( $file_init->file_exists( $file_path ) ) {
266 $file_init->unlink( $file_path );
267 }
268 }
269 }
270
271 LP_Material_Files_DB::getInstance();
272
273