| 1 |
<?php |
| 2 |
/** |
| 3 |
* LP_Meta_Box_Material_Fields |
| 4 |
* |
| 5 |
* @author khanhbd |
| 6 |
* @version 1.0.0 |
| 7 |
* @since 4.2.2 |
| 8 |
*/ |
| 9 |
if ( ! class_exists( 'LP_Meta_Box_Material_Fields' ) ) { |
| 10 |
class LP_Meta_Box_Material_Fields extends LP_Meta_Box_Field { |
| 11 |
/** |
| 12 |
* Constructor. |
| 13 |
* |
| 14 |
* @param string $id |
| 15 |
* @param string $label |
| 16 |
* @param string $description |
| 17 |
* @param mixed $default |
| 18 |
* @param array $extra |
| 19 |
*/ |
| 20 |
private static $instance = null; |
| 21 |
|
| 22 |
public function __construct( $label = '', $description = '', $default = '', $extra = array() ) { |
| 23 |
parent::__construct( $label, $description, $default, $extra ); |
| 24 |
// delete materials in post when post is deleted |
| 25 |
add_action( 'delete_post', array( $this, 'clear_material_in_post' ) ); |
| 26 |
} |
| 27 |
/** |
| 28 |
* @author khanhbd |
| 29 |
* @version 1.0.0 |
| 30 |
* @since 4.2.2 |
| 31 |
* [output Downloadable Material Tab content in Course Setting Meta Box] |
| 32 |
* @param [int] $thepostid [course's post_id] |
| 33 |
* @return [html] [content of Download material tab] |
| 34 |
*/ |
| 35 |
public function output( $thepostid ) { |
| 36 |
$material_init = LP_Material_Files_DB::getInstance(); |
| 37 |
$course_materials = $material_init->get_material_by_item_id( $thepostid, 0, 0, 1 ) ?? []; |
| 38 |
$max_file_size = (int) LP_Settings::get_option( 'material_max_file_size', 2 ); |
| 39 |
$allow_upload_amount = (int) LP_Settings::get_option( 'material_upload_files', 2 ); |
| 40 |
// check file was uploaded |
| 41 |
$uploaded_files = count( $course_materials ); |
| 42 |
// check file amount which can upload |
| 43 |
$can_upload = $allow_upload_amount - $uploaded_files; |
| 44 |
$allow_file_type = LP_Settings::get_option( 'material_allow_file_type', array( 'pdf', 'txt' ) ); |
| 45 |
$accept = ''; |
| 46 |
$accept_file_type = implode( ', ', $allow_file_type ); |
| 47 |
$material_mime_types = LP_Settings::lp_material_file_types(); |
| 48 |
foreach ( $allow_file_type as $ext ) { |
| 49 |
$accept .= $material_mime_types[ $ext ] . ','; |
| 50 |
} |
| 51 |
$accept = rtrim( $accept, ',' ); |
| 52 |
?> |
| 53 |
<div id="lp-material-container"> |
| 54 |
<?php if ( $allow_upload_amount == 0 ) : ?> |
| 55 |
<?php if ( get_post_type( $thepostid ) == LP_COURSE_CPT ) : ?> |
| 56 |
<div> <?php esc_html_e( 'Downloadable Materials is not allowed!', 'learnpress' ); ?> </div> |
| 57 |
<?php endif ?> |
| 58 |
<?php else : ?> |
| 59 |
<div> |
| 60 |
<?php |
| 61 |
echo '+ '; |
| 62 |
echo sprintf( |
| 63 |
__( 'Maximum amount of files you can upload more: %1$d files (maximum file size is %2$s MB)', 'learnpress' ), |
| 64 |
$can_upload, |
| 65 |
$max_file_size |
| 66 |
); |
| 67 |
?> |
| 68 |
</div> |
| 69 |
<div> |
| 70 |
<?php |
| 71 |
echo '+ '; |
| 72 |
echo sprintf( __( 'And allow upload only these types: %s.', 'learnpress' ), $accept_file_type ); |
| 73 |
?> |
| 74 |
</div> |
| 75 |
<hr> |
| 76 |
<div class="lp-material-btn-wrap"> |
| 77 |
<button class="button button-primary" id="btn-lp--add-material" type="button" |
| 78 |
can-upload="<?php esc_attr_e( $can_upload ); ?>"> |
| 79 |
<?php esc_html_e( 'Add Course Materials', 'learnpress' ); ?> |
| 80 |
</button> |
| 81 |
<button class="button button-primary" id="btn-lp--save-material" |
| 82 |
type="button"><?php esc_html_e( 'Save', 'learnpress' ); ?></button> |
| 83 |
</div> |
| 84 |
<input type="hidden" id="material-max-file-size" value="<?php esc_attr_e( $max_file_size ); ?>"/> |
| 85 |
<div id="lp-material--add-material-template" hidden> |
| 86 |
<div class="lp-material--group"> |
| 87 |
<div class="lp-material--field-wrap"> |
| 88 |
<label><?php esc_html_e( 'File Title', 'learnpress' ); ?></label> |
| 89 |
<input type="text" class="lp-material--field-title" value="" |
| 90 |
placeholder="<?php esc_attr_e( 'Enter File Title', 'learnpress' ); ?>"/> |
| 91 |
</div> |
| 92 |
<div class="lp-material--field-wrap"> |
| 93 |
<label><?php esc_html_e( 'Method', 'learnpress' ); ?></label> |
| 94 |
<select class="lp-material--field-method"> |
| 95 |
<option value="upload" |
| 96 |
selected><?php esc_html_e( 'Upload', 'learnpress' ); ?></option> |
| 97 |
<option value="external"><?php esc_html_e( 'External', 'learnpress' ); ?></option> |
| 98 |
</select> |
| 99 |
</div> |
| 100 |
<div class="lp-material--field-wrap lp-material--upload-wrap"> |
| 101 |
<label><?php esc_html_e( 'Choose File ', 'learnpress' ); ?><input type="file" |
| 102 |
class="lp-material--field-upload" |
| 103 |
value="" |
| 104 |
accept="<?php esc_attr_e( $accept ); ?>"/></label> |
| 105 |
</div> |
| 106 |
<div class="lp-material--field-wrap field-action-wrap"> |
| 107 |
<button type="button" |
| 108 |
class="button lp-material-save-field"><?php esc_html_e( 'Save field', 'learnpress' ); ?></button> |
| 109 |
<button class="button lp-material--delete" |
| 110 |
type="button"><?php esc_html_e( 'Remove', 'learnpress' ); ?></button> |
| 111 |
</div> |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
<div id="lp-material--upload-field-template" hidden> |
| 115 |
<div class="lp-material--field-wrap lp-material--upload-wrap"> |
| 116 |
<label> |
| 117 |
<?php esc_html_e( 'Choose File ', 'learnpress' ); ?> |
| 118 |
<input type="file" class="lp-material--field-upload" value="" |
| 119 |
accept="<?php esc_attr_e( $accept ); ?>"/> |
| 120 |
</label> |
| 121 |
</div> |
| 122 |
</div> |
| 123 |
<div id="lp-material--external-field-template" hidden> |
| 124 |
<div class="lp-material--field-wrap lp-material--external-wrap"> |
| 125 |
<label><?php esc_html_e( 'File URL', 'learnpress' ); ?></label> |
| 126 |
<input type="url" class="lp-material--field-external-link" value="" |
| 127 |
placeholder="<?php esc_attr_e( 'Enter File URL', 'learnpress' ); ?>"/> |
| 128 |
</div> |
| 129 |
</div> |
| 130 |
<input type="hidden" id="current-material-post-id" value="<?php echo esc_attr( $thepostid ); ?>"> |
| 131 |
<input type="hidden" id="delete-material-message" |
| 132 |
value="<?php esc_attr_e( 'Do you want to delete this file?', 'learnpress' ); ?>"> |
| 133 |
<input type="hidden" id="delete-material-row-text" |
| 134 |
value="<?php esc_attr_e( 'Delete', 'learnpress' ); ?>"> |
| 135 |
<table class="lp-material--table"> |
| 136 |
<?php |
| 137 |
$class_hidden_thead = ''; |
| 138 |
if ( empty( $uploaded_files ) ) { |
| 139 |
$class_hidden_thead = 'hidden'; |
| 140 |
} |
| 141 |
?> |
| 142 |
<thead class="<?php echo esc_attr( $class_hidden_thead ); ?>"> |
| 143 |
<tr> |
| 144 |
<th><?php esc_html_e( 'File Title', 'learnpress' ); ?></th> |
| 145 |
<th><?php esc_html_e( 'Method', 'learnpress' ); ?></th> |
| 146 |
<th><?php esc_html_e( 'Action', 'learnpress' ); ?></th> |
| 147 |
</tr> |
| 148 |
</thead> |
| 149 |
<tbody> |
| 150 |
|
| 151 |
<!-- <?php if ( $course_materials ) : ?> |
| 152 |
<?php foreach ( $course_materials as $row ) : ?> |
| 153 |
<tr data-id="<?php esc_attr_e( $row->file_id ); ?>" data-sort="<?php esc_attr_e( $row->orders ); ?>"> |
| 154 |
<td class="sort"><?php esc_attr_e( $row->file_name ); ?></td> |
| 155 |
<td><?php esc_attr_e( ucfirst( $row->method ) ); ?></td> |
| 156 |
<td><a href="javascript:void(0)" class="delete-material-row" data-id="<?php esc_attr_e( $row->file_id ); ?>"><?php esc_html_e( 'Delete', 'learnpress' ); ?></a></td> |
| 157 |
</tr> |
| 158 |
<?php endforeach; ?> |
| 159 |
<?php endif; ?> --> |
| 160 |
</tbody> |
| 161 |
|
| 162 |
</table> |
| 163 |
<?php if ( $course_materials ) : ?> |
| 164 |
<?php lp_skeleton_animation_html( 3, 100 ); ?> |
| 165 |
<?php endif ?> |
| 166 |
<div id="lp-material--group-container"> |
| 167 |
|
| 168 |
</div> |
| 169 |
<?php endif; ?> |
| 170 |
</div> |
| 171 |
<?php |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Remove material when delete course or lesson |
| 176 |
* |
| 177 |
* @param int $post_id id of course or lesson |
| 178 |
* |
| 179 |
* @author khanhbd |
| 180 |
* @version 1.0.1 |
| 181 |
* @since 4.2.2 |
| 182 |
*/ |
| 183 |
public function clear_material_in_post( $post_id ) { |
| 184 |
try { |
| 185 |
$post_type = get_post_type( $post_id ); |
| 186 |
if ( ! in_array( $post_type, [ LP_COURSE_CPT, LP_LESSON_CPT ] ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
$material_init = LP_Material_Files_DB::getInstance(); |
| 191 |
$material_init->delete_material_by_item_id( $post_id ); |
| 192 |
} catch ( Throwable $e ) { |
| 193 |
error_log( __METHOD__ . ': ' . $e->getMessage() ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get instance |
| 199 |
* |
| 200 |
* @return LP_Addon_Custom_Tab_Meta_Field |
| 201 |
*/ |
| 202 |
public static function instance(): LP_Meta_Box_Material_Fields { |
| 203 |
if ( ! self::$instance ) { |
| 204 |
self::$instance = new self(); |
| 205 |
} |
| 206 |
|
| 207 |
return self::$instance; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
LP_Meta_Box_Material_Fields::instance(); |
| 212 |
} |
| 213 |
|