← All changes
|
inc/admin/views/meta-boxes/lp-meta-box-functions.php
+623
-623
4.3.9
→
4.4.9
View file →
| @@ -1,623 +1,623 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * Resolve post ID for meta box rendering. | |
| 4 | - * | |
| 5 | - * In some admin/AJAX contexts, global $post can be null. | |
| 6 | - * Returning 0 allows callers to safely fall back to default values. | |
| 7 | - * | |
| 8 | - * @return int | |
| 9 | - */ | |
| 10 | -function lp_meta_box_get_post_id() { | |
| 11 | - global $thepostid, $post; | |
| 12 | - | |
| 13 | - if ( ! empty( $thepostid ) ) { | |
| 14 | - return absint( $thepostid ); | |
| 15 | - } | |
| 16 | - | |
| 17 | - if ( is_object( $post ) && ! empty( $post->ID ) ) { | |
| 18 | - $thepostid = absint( $post->ID ); | |
| 19 | - | |
| 20 | - return $thepostid; | |
| 21 | - } | |
| 22 | - | |
| 23 | - $resolved_post_id = absint( get_the_ID() ); | |
| 24 | - | |
| 25 | - if ( $resolved_post_id > 0 ) { | |
| 26 | - $thepostid = $resolved_post_id; | |
| 27 | - } | |
| 28 | - | |
| 29 | - return $resolved_post_id; | |
| 30 | -} | |
| 31 | - | |
| 32 | -/** | |
| 33 | - * Output a text input box. | |
| 34 | - * | |
| 35 | - * @param array $field | |
| 36 | - */ | |
| 37 | -function lp_meta_box_text_input_field( $field ) { | |
| 38 | - global $thepostid, $post; | |
| 39 | - | |
| 40 | - $thepostid = lp_meta_box_get_post_id(); | |
| 41 | - $field['placeholder'] = esc_attr( $field['placeholder'] ?? '' ); | |
| 42 | - $field['class'] = esc_attr( $field['class'] ?? '' ); | |
| 43 | - $field['style'] = esc_attr( $field['style'] ?? '' ); | |
| 44 | - $wrapper_class = esc_attr( $field['wrapper_class'] ?? '' ); | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * If you want to set default value for input text | |
| 48 | - * You must us hook default_{$meta_type}_metadata | Read more get_metadata_default() function | |
| 49 | - */ | |
| 50 | - $value_exists = $thepostid > 0 ? LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ) : false; | |
| 51 | - $value = $thepostid > 0 ? get_post_meta( $thepostid, $field['id'], true ) : ''; | |
| 52 | - $field['value'] = $value_exists ? $value : ( $field['default'] ?? '' ); | |
| 53 | - $field['id'] = esc_attr( $field['id'] ?? '' ); | |
| 54 | - $field['type_input'] = esc_attr( $field['type_input'] ?? 'text' ); | |
| 55 | - $field['desc_tip'] = esc_attr( $field['desc_tip'] ?? '' ); | |
| 56 | - | |
| 57 | - // Custom attribute handling | |
| 58 | - $custom_attributes = array(); | |
| 59 | - | |
| 60 | - if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 61 | - foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 62 | - $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 63 | - } | |
| 64 | - } | |
| 65 | - | |
| 66 | - $custom_attributes_str = implode( ' ', $custom_attributes ); | |
| 67 | - ?> | |
| 68 | - | |
| 69 | - <div class="form-field <?php echo esc_attr( $field['id'] . '_field ' . $wrapper_class ); ?>"> | |
| 70 | - <label for="<?php echo esc_attr( $field['id'] ); ?>"> | |
| 71 | - <?php echo wp_kses_post( $field['label'] ); ?> | |
| 72 | - </label> | |
| 73 | - <input type="<?php echo esc_attr( $field['type_input'] ); ?>" | |
| 74 | - class="<?php echo esc_attr( $field['class'] ); ?>" | |
| 75 | - style="<?php echo esc_attr( $field['style'] ); ?>" | |
| 76 | - name="<?php echo esc_attr( $field['id'] ); ?>" | |
| 77 | - id="<?php echo esc_attr( $field['id'] ); ?>" | |
| 78 | - value="<?php echo esc_attr( $field['value'] ); ?>" | |
| 79 | - placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" | |
| 80 | - <?php echo learn_press_echo_vuejs_write_on_php( $custom_attributes_str ); ?> /> | |
| 81 | - <?php | |
| 82 | - if ( ! empty( $field['description'] ) ) { | |
| 83 | - echo '<p class="description">'; | |
| 84 | - echo '<span>' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 85 | - | |
| 86 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 87 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 88 | - } | |
| 89 | - echo '</p>'; | |
| 90 | - } | |
| 91 | - ?> | |
| 92 | - </div> | |
| 93 | - <?php | |
| 94 | -} | |
| 95 | - | |
| 96 | -/** | |
| 97 | - * Output a textarea input box. | |
| 98 | - * | |
| 99 | - * @param array $field | |
| 100 | - */ | |
| 101 | -function lp_meta_box_textarea_field( $field ) { | |
| 102 | - global $thepostid, $post; | |
| 103 | - | |
| 104 | - $thepostid = lp_meta_box_get_post_id(); | |
| 105 | - $field['id'] = esc_attr( $field['id'] ?? '' ); | |
| 106 | - $field['placeholder'] = esc_attr( $field['placeholder'] ?? '' ); | |
| 107 | - $field['class'] = esc_attr( $field['class'] ?? 'short' ); | |
| 108 | - $field['style'] = esc_attr( $field['style'] ?? '' ); | |
| 109 | - $value_exists = $thepostid > 0 ? LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ) : false; | |
| 110 | - $value = $thepostid > 0 ? get_post_meta( $thepostid, $field['id'], true ) : ''; | |
| 111 | - $field['value'] = esc_textarea( $value_exists ? $value : ( $field['default'] ?? '' ) ); | |
| 112 | - $field['desc_tip'] = esc_attr( $field['desc_tip'] ?? '' ); | |
| 113 | - $field['name'] = esc_attr( $field['name'] ?? $field['id'] ); | |
| 114 | - | |
| 115 | - // Custom attribute handling | |
| 116 | - $custom_attributes = array(); | |
| 117 | - if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 118 | - foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 119 | - $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | - $custom_attributes_str = implode( ' ', $custom_attributes ); | |
| 124 | - ?> | |
| 125 | - | |
| 126 | - <p class="form-field <?php echo esc_attr( $field['id'] . '_field ' ); ?>"> | |
| 127 | - <label for="<?php echo esc_attr( $field['id'] ); ?>>"> | |
| 128 | - <?php echo wp_kses_post( $field['label'] ); ?> | |
| 129 | - </label> | |
| 130 | - <textarea class="<?php echo esc_attr( $field['class'] ); ?>" | |
| 131 | - style="<?php echo esc_attr( $field['style'] ); ?>" | |
| 132 | - name="<?php echo esc_attr( $field['name'] ); ?>" | |
| 133 | - id="<?php esc_attr( $field['id'] ); ?>" | |
| 134 | - placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" | |
| 135 | - rows="5" <?php echo esc_attr( $custom_attributes_str ); ?>><?php echo wp_kses_post( $field['value'] ); ?></textarea> | |
| 136 | - <?php | |
| 137 | - if ( ! empty( $field['description'] ) ) { | |
| 138 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 139 | - | |
| 140 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 141 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 142 | - } | |
| 143 | - } | |
| 144 | - ?> | |
| 145 | - </p> | |
| 146 | - <?php | |
| 147 | -} | |
| 148 | - | |
| 149 | -/** | |
| 150 | - * Output a checkbox input box. | |
| 151 | - * | |
| 152 | - * @param array $field | |
| 153 | - */ | |
| 154 | -function lp_meta_box_checkbox_field( $field ) { | |
| 155 | - global $thepostid, $post; | |
| 156 | - | |
| 157 | - $thepostid = lp_meta_box_get_post_id(); | |
| 158 | - $field['id'] = esc_attr( $field['id'] ?? '' ); | |
| 159 | - $field['class'] = esc_attr( $field['class'] ?? '' ); | |
| 160 | - $field['style'] = esc_attr( $field['style'] ?? '' ); | |
| 161 | - $wrapper_class = esc_attr( $field['wrapper_class'] ?? '' ); | |
| 162 | - $name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] ); | |
| 163 | - | |
| 164 | - $value_db = get_post_meta( $thepostid, $field['id'], true ); | |
| 165 | - | |
| 166 | - $checked = ''; | |
| 167 | - if ( 'yes' === $value_db ) { | |
| 168 | - $checked = 'checked="checked"'; | |
| 169 | - } | |
| 170 | - | |
| 171 | - // Custom attribute handling | |
| 172 | - $custom_attributes = array(); | |
| 173 | - if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 174 | - foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 175 | - $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 176 | - } | |
| 177 | - } | |
| 178 | - | |
| 179 | - $custom_attributes_str = implode( ' ', $custom_attributes ); | |
| 180 | - ?> | |
| 181 | - | |
| 182 | - <div class="form-field <?php echo esc_attr( $field['id'] . '_field' . $wrapper_class ); ?>"> | |
| 183 | - <label for="<?php echo esc_attr( $field['id'] ); ?>"> | |
| 184 | - <?php echo wp_kses_post( $field['label'] ); ?> | |
| 185 | - </label> | |
| 186 | - | |
| 187 | - <input type="checkbox" | |
| 188 | - class="<?php echo esc_attr( $field['class'] ); ?>" | |
| 189 | - style="<?php echo esc_attr( $field['style'] ); ?>" | |
| 190 | - name="<?php echo esc_attr( $name ); ?>" | |
| 191 | - id="<?php echo esc_attr( $field['id'] ); ?>" | |
| 192 | - <?php learn_press_echo_vuejs_write_on_php( $checked ); ?> | |
| 193 | - <?php learn_press_echo_vuejs_write_on_php( $custom_attributes_str ); ?> /> | |
| 194 | - | |
| 195 | - <?php | |
| 196 | - if ( ! empty( $field['description'] ) ) { | |
| 197 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 198 | - | |
| 199 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 200 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 201 | - } | |
| 202 | - } | |
| 203 | - ?> | |
| 204 | - </div> | |
| 205 | - <?php | |
| 206 | -} | |
| 207 | - | |
| 208 | -/** | |
| 209 | - * Output a select input box. | |
| 210 | - * | |
| 211 | - * @param array $field Data about the field to render. | |
| 212 | - */ | |
| 213 | -function lp_meta_box_select_field( $field = array() ) { | |
| 214 | - global $thepostid, $post; | |
| 215 | - | |
| 216 | - $thepostid = lp_meta_box_get_post_id(); | |
| 217 | - $default = ( ! get_post_meta( | |
| 218 | - $thepostid, | |
| 219 | - $field['id'], | |
| 220 | - true | |
| 221 | - ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 222 | - $thepostid, | |
| 223 | - $field['id'], | |
| 224 | - true | |
| 225 | - ); | |
| 226 | - | |
| 227 | - $field = wp_parse_args( | |
| 228 | - $field, | |
| 229 | - array( | |
| 230 | - 'class' => 'select', | |
| 231 | - 'style' => '', | |
| 232 | - 'wrapper_class' => '', // Use "lp-select-2" for select2. | |
| 233 | - 'value' => isset( $field['value'] ) ? $field['value'] : $default, | |
| 234 | - 'name' => $field['id'], | |
| 235 | - 'desc_tip' => false, | |
| 236 | - 'multiple' => false, | |
| 237 | - 'custom_attributes' => array(), | |
| 238 | - ) | |
| 239 | - ); | |
| 240 | - | |
| 241 | - $label_attributes = array( | |
| 242 | - 'for' => $field['id'], | |
| 243 | - ); | |
| 244 | - | |
| 245 | - $field_attributes = (array) $field['custom_attributes']; | |
| 246 | - $field_attributes['style'] = $field['style'] ?? ''; | |
| 247 | - $field_attributes['id'] = $field['id'] ?? ''; | |
| 248 | - $field_attributes['name'] = $field['multiple'] ? $field['name'] . '[]' : $field['name']; | |
| 249 | - $field_attributes['class'] = $field['class'] ?? ''; | |
| 250 | - | |
| 251 | - if ( $field['multiple'] ) { | |
| 252 | - $field['wrapper_class'] = 'lp-select-2'; | |
| 253 | - $field_attributes['multiple'] = true; | |
| 254 | - } | |
| 255 | - | |
| 256 | - $tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : ''; | |
| 257 | - $description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : ''; | |
| 258 | - ?> | |
| 259 | - | |
| 260 | - <p class="form-field <?php echo esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ); ?>"> | |
| 261 | - <label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo wp_kses_post( $field['label'] ); ?></label> | |
| 262 | - <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> | |
| 263 | - <?php | |
| 264 | - foreach ( $field['options'] as $key => $value ) { | |
| 265 | - echo '<option value="' . esc_attr( $key ) . '"' . ( is_array( $field['value'] ) ? selected( in_array( (string) $key, $field['value'], true ), true ) : selected( $key, $field['value'], false ) ) . '>' . esc_html( $value ) . '</option>'; | |
| 266 | - } | |
| 267 | - ?> | |
| 268 | - </select> | |
| 269 | - <?php | |
| 270 | - if ( ! empty( $field['description'] ) ) { | |
| 271 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 272 | - | |
| 273 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 274 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 275 | - } | |
| 276 | - } | |
| 277 | - ?> | |
| 278 | - </p> | |
| 279 | - <?php | |
| 280 | -} | |
| 281 | - | |
| 282 | -/** | |
| 283 | - * Output a radio input box. | |
| 284 | - * | |
| 285 | - * @param array $field | |
| 286 | - */ | |
| 287 | -function lp_meta_box_radio_field( $field ) { | |
| 288 | - global $thepostid, $post; | |
| 289 | - | |
| 290 | - $thepostid = lp_meta_box_get_post_id(); | |
| 291 | - $field['class'] = $field['class'] ?? 'select'; | |
| 292 | - $field['style'] = $field['style'] ?? ''; | |
| 293 | - $field['wrapper_class'] = $field['wrapper_class'] ?? ''; | |
| 294 | - $field['default'] = ( ! get_post_meta( | |
| 295 | - $thepostid, | |
| 296 | - $field['id'], | |
| 297 | - true | |
| 298 | - ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 299 | - $thepostid, | |
| 300 | - $field['id'], | |
| 301 | - true | |
| 302 | - ); | |
| 303 | - $field['value'] = $field['value'] ?? $field['default']; | |
| 304 | - $field['name'] = $field['name'] ?? $field['id']; | |
| 305 | - $field['desc_tip'] = $field['desc_tip'] ?? false; | |
| 306 | - | |
| 307 | - echo '<fieldset class="form-field ' . esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ) . '"><h4>' . wp_kses_post( $field['label'] ) . '</h4>'; | |
| 308 | - | |
| 309 | - if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) { | |
| 310 | - learn_press_quick_tip( $field['description'] ); | |
| 311 | - } | |
| 312 | - | |
| 313 | - echo '<ul class="lp-radios-field-meta-box">'; | |
| 314 | - | |
| 315 | - foreach ( $field['options'] as $key => $value ) { | |
| 316 | - echo '<li><label><input | |
| 317 | - name="' . esc_attr( $field['name'] ) . '" | |
| 318 | - value="' . esc_attr( $key ) . '" | |
| 319 | - type="radio" | |
| 320 | - class="' . esc_attr( $field['class'] ) . '" | |
| 321 | - style="' . esc_attr( $field['style'] ) . '" | |
| 322 | - ' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' | |
| 323 | - /> ' . ( $value ) . '</label> | |
| 324 | - </li>'; | |
| 325 | - } | |
| 326 | - echo '</ul>'; | |
| 327 | - | |
| 328 | - if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) { | |
| 329 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 330 | - } | |
| 331 | - | |
| 332 | - echo '</fieldset>'; | |
| 333 | -} | |
| 334 | - | |
| 335 | -function lp_meta_box_file_input_field( $field ) { | |
| 336 | - global $thepostid, $post; | |
| 337 | - | |
| 338 | - $thepostid = lp_meta_box_get_post_id(); | |
| 339 | - $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; | |
| 340 | - $field['style'] = isset( $field['style'] ) ? $field['style'] : ''; | |
| 341 | - $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; | |
| 342 | - $field['default'] = ( ! get_post_meta( | |
| 343 | - $thepostid, | |
| 344 | - $field['id'], | |
| 345 | - true | |
| 346 | - ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 347 | - $thepostid, | |
| 348 | - $field['id'], | |
| 349 | - true | |
| 350 | - ); | |
| 351 | - $field['value'] = isset( $field['value'] ) ? $field['value'] : $field['default']; | |
| 352 | - $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; | |
| 353 | - $field['mime_type'] = isset( $field['mime_type'] ) ? implode( ',', $field['mime_type'] ) : ''; | |
| 354 | - $field['multil'] = ( isset( $field['multil'] ) && $field['multil'] ) ? true : false; | |
| 355 | - $field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false; | |
| 356 | - | |
| 357 | - // Custom attribute handling | |
| 358 | - $custom_attributes = array(); | |
| 359 | - | |
| 360 | - if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 361 | - foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 362 | - $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 363 | - } | |
| 364 | - } | |
| 365 | - | |
| 366 | - echo '<div class="form-field ' . esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ) . '"> | |
| 367 | - <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; | |
| 368 | - | |
| 369 | - echo '<div id="' . esc_attr( $field['id'] ) . '" class="lp-meta-box__file ' . esc_attr( $field['class'] ) . '" data-mime="' . $field['mime_type'] . '" data-multil="' . $field['multil'] . '" style="' . esc_attr( $field['style'] ) . '" ' . implode( | |
| 370 | - ' ', | |
| 371 | - $custom_attributes | |
| 372 | - ) . '>'; | |
| 373 | - echo '<ul class="lp-meta-box__file_list">'; | |
| 374 | - | |
| 375 | - if ( ! empty( $field['value'] ) ) { | |
| 376 | - foreach ( (array) $field['value'] as $attachment_id ) { | |
| 377 | - $url = wp_get_attachment_url( $attachment_id ); | |
| 378 | - | |
| 379 | - if ( $url ) { | |
| 380 | - $check_file = wp_check_filetype( $url ); | |
| 381 | - | |
| 382 | - echo '<li class="lp-meta-box__file_list-item image" data-attachment_id="' . $attachment_id . '">'; | |
| 383 | - | |
| 384 | - if ( in_array( $check_file['ext'], array( 'jpg', 'png', 'gif', 'bmp', 'tif', 'jpeg' ), true ) ) { | |
| 385 | - echo wp_get_attachment_image( $attachment_id, 'thumbnail' ); | |
| 386 | - } else { | |
| 387 | - echo '<img class="is_file" src="' . wp_mime_type_icon( $check_file['type'] ) . '" />'; | |
| 388 | - echo '<span>' . wp_basename( get_attached_file( $attachment_id ) ) . '</span>'; | |
| 389 | - } | |
| 390 | - echo '<ul class="actions"><li><a href="#" class="delete"></a></li></ul>'; | |
| 391 | - echo '</li>'; | |
| 392 | - } | |
| 393 | - } | |
| 394 | - } | |
| 395 | - | |
| 396 | - echo '</ul>'; | |
| 397 | - echo '<input class="lp-meta-box__file_input" type="hidden" name="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( | |
| 398 | - ( ! empty( $field['value'] ) && is_array( $field['value'] ) ) ? implode( | |
| 399 | - ',', | |
| 400 | - $field['value'] | |
| 401 | - ) : $field['value'] | |
| 402 | - ) . '" />'; | |
| 403 | - echo '<p>'; | |
| 404 | - echo '<a href="#" class="button btn-upload">' . esc_html__( '+ Add media', 'learnpress' ) . '</a>'; | |
| 405 | - if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) { | |
| 406 | - learn_press_quick_tip( $field['description'] ); | |
| 407 | - } | |
| 408 | - echo '</p>'; | |
| 409 | - | |
| 410 | - if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) { | |
| 411 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 412 | - } | |
| 413 | - echo '</div>'; | |
| 414 | - echo '</div>'; | |
| 415 | -} | |
| 416 | - | |
| 417 | -/** | |
| 418 | - * Output a duration input box. | |
| 419 | - * | |
| 420 | - * @param array $field | |
| 421 | - */ | |
| 422 | -function lp_meta_box_duration_field( $field ) { | |
| 423 | - global $thepostid, $post; | |
| 424 | - | |
| 425 | - $thepostid = lp_meta_box_get_post_id(); | |
| 426 | - $field['placeholder'] = $field['placeholder'] ?? ''; | |
| 427 | - $field['class'] = $field['class'] ?? 'short'; | |
| 428 | - $field['style'] = $field['style'] ?? ''; | |
| 429 | - $field['wrapper_class'] = $field['wrapper_class'] ?? ''; | |
| 430 | - $field['default'] = ( ! get_post_meta( | |
| 431 | - $thepostid, | |
| 432 | - $field['id'], | |
| 433 | - true | |
| 434 | - ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 435 | - $thepostid, | |
| 436 | - $field['id'], | |
| 437 | - true | |
| 438 | - ); | |
| 439 | - $field['value'] = $field['value'] ?? $field['default']; | |
| 440 | - $field['name'] = $field['name'] ?? $field['id']; | |
| 441 | - $data_type = empty( $field['data_type'] ) ? '' : $field['data_type']; | |
| 442 | - $duration = learn_press_get_course_duration_support(); | |
| 443 | - | |
| 444 | - $duration_keys = array_keys( $duration ); | |
| 445 | - $default_time = ! empty( $field['default_time'] ) ? $field['default_time'] : end( $duration_keys ); | |
| 446 | - | |
| 447 | - if ( preg_match_all( '!([0-9]+)\s*(' . join( '|', $duration_keys ) . ')?!', $field['value'], $matches ) ) { | |
| 448 | - $a1 = $matches[1][0]; | |
| 449 | - $a2 = in_array( $matches[2][0], $duration_keys ) ? $matches[2][0] : $default_time; | |
| 450 | - } else { | |
| 451 | - $a1 = absint( $field['value'] ); | |
| 452 | - $a2 = $default_time; | |
| 453 | - } | |
| 454 | - | |
| 455 | - // Custom attribute handling | |
| 456 | - $custom_attributes = array(); | |
| 457 | - | |
| 458 | - if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 459 | - foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 460 | - $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 461 | - } | |
| 462 | - } | |
| 463 | - | |
| 464 | - $html_option = ''; | |
| 465 | - foreach ( $duration as $k => $v ) { | |
| 466 | - $html_option .= sprintf( '<option value="%s" %s>%s</option>', $k, selected( $k, $a2, false ), $v ); | |
| 467 | - } | |
| 468 | - | |
| 469 | - echo '<p class="lp-meta-box__duration form-field ' . esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ) . '"> | |
| 470 | - <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; | |
| 471 | - | |
| 472 | - echo '<input type="number" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '[]" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $a1 ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( | |
| 473 | - ' ', | |
| 474 | - $custom_attributes | |
| 475 | - ) . ' /> '; | |
| 476 | - | |
| 477 | - echo '<select name="' . esc_attr( $field['name'] ) . '[]" class="lp-meta-box__duration-select">' . $html_option . '</select>'; | |
| 478 | - | |
| 479 | - if ( ! empty( $field['description'] ) ) { | |
| 480 | - echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 481 | - | |
| 482 | - if ( ! empty( $field['desc_tip'] ) ) { | |
| 483 | - learn_press_quick_tip( $field['desc_tip'] ); | |
| 484 | - } | |
| 485 | - } | |
| 486 | - | |
| 487 | - echo '</p>'; | |
| 488 | -} | |
| 489 | - | |
| 490 | -/** | |
| 491 | - * Use for Type: custom_fields in LP4. | |
| 492 | - * | |
| 493 | - * @param [type] $field get ID, options.... | |
| 494 | - * @param [type] $values get_option() value | |
| 495 | - * @param [type] $key | |
| 496 | - * | |
| 497 | - * @return void | |
| 498 | - */ | |
| 499 | -function lp_metabox_custom_fields( $field, $values, $key ) { | |
| 500 | - ?> | |
| 501 | - <tr> | |
| 502 | - <td class="sort"> | |
| 503 | - <input class="count" type="hidden" value="<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $field['id'] ) . '[' . $key . ']' . '[sort]'; ?>"> | |
| 504 | - <input type="hidden" value="<?php echo ! empty( $values['id'] ) ? $values['id'] : wp_rand( 1, 10000 ) . $key; ?>" name="<?php echo esc_attr( $field['id'] ) . '[' . $key . ']' . '[id]'; ?>"> | |
| 505 | - </td> | |
| 506 | - <?php | |
| 507 | - if ( $field['options'] ) { | |
| 508 | - foreach ( $field['options'] as $cfk => $val ) { | |
| 509 | - $name = $field['id'] . '[' . $key . ']' . '[' . $cfk . ']'; | |
| 510 | - | |
| 511 | - switch ( $val['type'] ) { | |
| 512 | - case 'text': | |
| 513 | - case 'password': | |
| 514 | - case 'datetime': | |
| 515 | - case 'datetime-local': | |
| 516 | - case 'date': | |
| 517 | - case 'month': | |
| 518 | - case 'time': | |
| 519 | - case 'week': | |
| 520 | - case 'number': | |
| 521 | - case 'email': | |
| 522 | - case 'url': | |
| 523 | - case 'tel': | |
| 524 | - ?> | |
| 525 | - <td> | |
| 526 | - <input name="<?php echo esc_attr( $name ); ?>" type="<?php echo esc_attr( $val['type'] ); ?>" | |
| 527 | - class="input-text" | |
| 528 | - placeholder="<?php echo isset( $val['placeholder'] ) ? esc_attr( $val['placeholder'] ) : ''; ?>" | |
| 529 | - value="<?php echo ! empty( $values[ $cfk ] ) ? esc_attr( $values[ $cfk ] ) : ''; ?>"> | |
| 530 | - </td> | |
| 531 | - <?php | |
| 532 | - break; | |
| 533 | - | |
| 534 | - case 'select': | |
| 535 | - ?> | |
| 536 | - <td> | |
| 537 | - <select name="<?php echo esc_attr( $name ); ?>"> | |
| 538 | - <?php | |
| 539 | - if ( isset( $val['options'] ) ) { | |
| 540 | - foreach ( $val['options'] as $cfks => $cfselect ) { | |
| 541 | - ?> | |
| 542 | - <option | |
| 543 | - value="<?php echo esc_attr( $cfks ); ?>" | |
| 544 | - <?php | |
| 545 | - echo ! empty( $values[ $cfk ] ) ? selected( | |
| 546 | - $values[ $cfk ], | |
| 547 | - (string) $cfks | |
| 548 | - ) : ''; | |
| 549 | - ?> | |
| 550 | - ><?php echo wp_kses_post( $cfselect ); ?></option> | |
| 551 | - <?php | |
| 552 | - } | |
| 553 | - } | |
| 554 | - ?> | |
| 555 | - </select> | |
| 556 | - </td> | |
| 557 | - <?php | |
| 558 | - break; | |
| 559 | - | |
| 560 | - case 'checkbox': | |
| 561 | - ?> | |
| 562 | - <td> | |
| 563 | - <input name="<?php echo esc_attr( $name ); ?>" type="checkbox" name="" value="1" <?php echo ! empty( $values[ $cfk ] ) ? checked( $values[ $cfk ], 'yes' ) : ''; ?>> | |
| 564 | - </td> | |
| 565 | - <?php | |
| 566 | - break; | |
| 567 | - } | |
| 568 | - } | |
| 569 | - } | |
| 570 | - ?> | |
| 571 | - <td width="2%"><a href="#" class="delete"></a></td> | |
| 572 | - </tr> | |
| 573 | - <?php | |
| 574 | -} | |
| 575 | - | |
| 576 | -function lp_implode_html_attributes( $raw_attributes ) { | |
| 577 | - $attributes = array(); | |
| 578 | - foreach ( $raw_attributes as $name => $value ) { | |
| 579 | - $attributes[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"'; | |
| 580 | - } | |
| 581 | - | |
| 582 | - return implode( ' ', $attributes ); | |
| 583 | -} | |
| 584 | - | |
| 585 | -function lp_meta_box_output( $metaboxes = array() ) { | |
| 586 | - if ( ! empty( $metaboxes ) ) { | |
| 587 | - foreach ( $metaboxes as $id => $field ) { | |
| 588 | - $field['id'] = $id; | |
| 589 | - | |
| 590 | - switch ( $field['type'] ) { | |
| 591 | - case 'text': | |
| 592 | - case 'number': | |
| 593 | - case 'url': | |
| 594 | - lp_meta_box_text_input_field( $field ); | |
| 595 | - break; | |
| 596 | - | |
| 597 | - case 'textarea': | |
| 598 | - lp_meta_box_textarea_field( $field ); | |
| 599 | - break; | |
| 600 | - | |
| 601 | - case 'checkbox': | |
| 602 | - lp_meta_box_checkbox_field( $field ); | |
| 603 | - break; | |
| 604 | - | |
| 605 | - case 'duration': | |
| 606 | - lp_meta_box_duration_field( $field ); | |
| 607 | - break; | |
| 608 | - | |
| 609 | - case 'select': | |
| 610 | - lp_meta_box_select_field( $field ); | |
| 611 | - break; | |
| 612 | - | |
| 613 | - case 'radio': | |
| 614 | - lp_meta_box_radio_field( $field ); | |
| 615 | - break; | |
| 616 | - | |
| 617 | - case 'file': | |
| 618 | - lp_meta_box_file_input_field( $field ); | |
| 619 | - break; | |
| 620 | - } | |
| 621 | - } | |
| 622 | - } | |
| 623 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Resolve post ID for meta box rendering. | |
| 4 | + * | |
| 5 | + * In some admin/AJAX contexts, global $post can be null. | |
| 6 | + * Returning 0 allows callers to safely fall back to default values. | |
| 7 | + * | |
| 8 | + * @return int | |
| 9 | + */ | |
| 10 | +function lp_meta_box_get_post_id() { | |
| 11 | + global $thepostid, $post; | |
| 12 | + | |
| 13 | + if ( ! empty( $thepostid ) ) { | |
| 14 | + return absint( $thepostid ); | |
| 15 | + } | |
| 16 | + | |
| 17 | + if ( is_object( $post ) && ! empty( $post->ID ) ) { | |
| 18 | + $thepostid = absint( $post->ID ); | |
| 19 | + | |
| 20 | + return $thepostid; | |
| 21 | + } | |
| 22 | + | |
| 23 | + $resolved_post_id = absint( get_the_ID() ); | |
| 24 | + | |
| 25 | + if ( $resolved_post_id > 0 ) { | |
| 26 | + $thepostid = $resolved_post_id; | |
| 27 | + } | |
| 28 | + | |
| 29 | + return $resolved_post_id; | |
| 30 | +} | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * Output a text input box. | |
| 34 | + * | |
| 35 | + * @param array $field | |
| 36 | + */ | |
| 37 | +function lp_meta_box_text_input_field( $field ) { | |
| 38 | + global $thepostid, $post; | |
| 39 | + | |
| 40 | + $thepostid = lp_meta_box_get_post_id(); | |
| 41 | + $field['placeholder'] = esc_attr( $field['placeholder'] ?? '' ); | |
| 42 | + $field['class'] = esc_attr( $field['class'] ?? '' ); | |
| 43 | + $field['style'] = esc_attr( $field['style'] ?? '' ); | |
| 44 | + $wrapper_class = esc_attr( $field['wrapper_class'] ?? '' ); | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * If you want to set default value for input text | |
| 48 | + * You must us hook default_{$meta_type}_metadata | Read more get_metadata_default() function | |
| 49 | + */ | |
| 50 | + $value_exists = $thepostid > 0 ? LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ) : false; | |
| 51 | + $value = $thepostid > 0 ? get_post_meta( $thepostid, $field['id'], true ) : ''; | |
| 52 | + $field['value'] = $value_exists ? $value : ( $field['default'] ?? '' ); | |
| 53 | + $field['id'] = esc_attr( $field['id'] ?? '' ); | |
| 54 | + $field['type_input'] = esc_attr( $field['type_input'] ?? 'text' ); | |
| 55 | + $field['desc_tip'] = esc_attr( $field['desc_tip'] ?? '' ); | |
| 56 | + | |
| 57 | + // Custom attribute handling | |
| 58 | + $custom_attributes = array(); | |
| 59 | + | |
| 60 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 61 | + foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 62 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 63 | + } | |
| 64 | + } | |
| 65 | + | |
| 66 | + $custom_attributes_str = implode( ' ', $custom_attributes ); | |
| 67 | + ?> | |
| 68 | + | |
| 69 | + <div class="form-field <?php echo esc_attr( $field['id'] . '_field ' . $wrapper_class ); ?>"> | |
| 70 | + <label for="<?php echo esc_attr( $field['id'] ); ?>"> | |
| 71 | + <?php echo wp_kses_post( $field['label'] ); ?> | |
| 72 | + </label> | |
| 73 | + <input type="<?php echo esc_attr( $field['type_input'] ); ?>" | |
| 74 | + class="<?php echo esc_attr( $field['class'] ); ?>" | |
| 75 | + style="<?php echo esc_attr( $field['style'] ); ?>" | |
| 76 | + name="<?php echo esc_attr( $field['id'] ); ?>" | |
| 77 | + id="<?php echo esc_attr( $field['id'] ); ?>" | |
| 78 | + value="<?php echo esc_attr( $field['value'] ); ?>" | |
| 79 | + placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" | |
| 80 | + <?php echo learn_press_echo_vuejs_write_on_php( $custom_attributes_str ); ?> /> | |
| 81 | + <?php | |
| 82 | + if ( ! empty( $field['description'] ) ) { | |
| 83 | + echo '<p class="description">'; | |
| 84 | + echo '<span>' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 85 | + | |
| 86 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 87 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 88 | + } | |
| 89 | + echo '</p>'; | |
| 90 | + } | |
| 91 | + ?> | |
| 92 | + </div> | |
| 93 | + <?php | |
| 94 | +} | |
| 95 | + | |
| 96 | +/** | |
| 97 | + * Output a textarea input box. | |
| 98 | + * | |
| 99 | + * @param array $field | |
| 100 | + */ | |
| 101 | +function lp_meta_box_textarea_field( $field ) { | |
| 102 | + global $thepostid, $post; | |
| 103 | + | |
| 104 | + $thepostid = lp_meta_box_get_post_id(); | |
| 105 | + $field['id'] = esc_attr( $field['id'] ?? '' ); | |
| 106 | + $field['placeholder'] = esc_attr( $field['placeholder'] ?? '' ); | |
| 107 | + $field['class'] = esc_attr( $field['class'] ?? 'short' ); | |
| 108 | + $field['style'] = esc_attr( $field['style'] ?? '' ); | |
| 109 | + $value_exists = $thepostid > 0 ? LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $field['id'] ) : false; | |
| 110 | + $value = $thepostid > 0 ? get_post_meta( $thepostid, $field['id'], true ) : ''; | |
| 111 | + $field['value'] = esc_textarea( $value_exists ? $value : ( $field['default'] ?? '' ) ); | |
| 112 | + $field['desc_tip'] = esc_attr( $field['desc_tip'] ?? '' ); | |
| 113 | + $field['name'] = esc_attr( $field['name'] ?? $field['id'] ); | |
| 114 | + | |
| 115 | + // Custom attribute handling | |
| 116 | + $custom_attributes = array(); | |
| 117 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 118 | + foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 119 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + $custom_attributes_str = implode( ' ', $custom_attributes ); | |
| 124 | + ?> | |
| 125 | + | |
| 126 | + <p class="form-field <?php echo esc_attr( $field['id'] . '_field ' ); ?>"> | |
| 127 | + <label for="<?php echo esc_attr( $field['id'] ); ?>>"> | |
| 128 | + <?php echo wp_kses_post( $field['label'] ); ?> | |
| 129 | + </label> | |
| 130 | + <textarea class="<?php echo esc_attr( $field['class'] ); ?>" | |
| 131 | + style="<?php echo esc_attr( $field['style'] ); ?>" | |
| 132 | + name="<?php echo esc_attr( $field['name'] ); ?>" | |
| 133 | + id="<?php esc_attr( $field['id'] ); ?>" | |
| 134 | + placeholder="<?php echo esc_attr( $field['placeholder'] ); ?>" | |
| 135 | + rows="5" <?php echo esc_attr( $custom_attributes_str ); ?>><?php echo wp_kses_post( $field['value'] ); ?></textarea> | |
| 136 | + <?php | |
| 137 | + if ( ! empty( $field['description'] ) ) { | |
| 138 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 139 | + | |
| 140 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 141 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + ?> | |
| 145 | + </p> | |
| 146 | + <?php | |
| 147 | +} | |
| 148 | + | |
| 149 | +/** | |
| 150 | + * Output a checkbox input box. | |
| 151 | + * | |
| 152 | + * @param array $field | |
| 153 | + */ | |
| 154 | +function lp_meta_box_checkbox_field( $field ) { | |
| 155 | + global $thepostid, $post; | |
| 156 | + | |
| 157 | + $thepostid = lp_meta_box_get_post_id(); | |
| 158 | + $field['id'] = esc_attr( $field['id'] ?? '' ); | |
| 159 | + $field['class'] = esc_attr( $field['class'] ?? '' ); | |
| 160 | + $field['style'] = esc_attr( $field['style'] ?? '' ); | |
| 161 | + $wrapper_class = esc_attr( $field['wrapper_class'] ?? '' ); | |
| 162 | + $name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] ); | |
| 163 | + | |
| 164 | + $value_db = get_post_meta( $thepostid, $field['id'], true ); | |
| 165 | + | |
| 166 | + $checked = ''; | |
| 167 | + if ( 'yes' === $value_db ) { | |
| 168 | + $checked = 'checked="checked"'; | |
| 169 | + } | |
| 170 | + | |
| 171 | + // Custom attribute handling | |
| 172 | + $custom_attributes = array(); | |
| 173 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 174 | + foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 175 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 176 | + } | |
| 177 | + } | |
| 178 | + | |
| 179 | + $custom_attributes_str = implode( ' ', $custom_attributes ); | |
| 180 | + ?> | |
| 181 | + | |
| 182 | + <div class="form-field <?php echo esc_attr( $field['id'] . '_field' . $wrapper_class ); ?>"> | |
| 183 | + <label for="<?php echo esc_attr( $field['id'] ); ?>"> | |
| 184 | + <?php echo wp_kses_post( $field['label'] ); ?> | |
| 185 | + </label> | |
| 186 | + | |
| 187 | + <input type="checkbox" | |
| 188 | + class="<?php echo esc_attr( $field['class'] ); ?>" | |
| 189 | + style="<?php echo esc_attr( $field['style'] ); ?>" | |
| 190 | + name="<?php echo esc_attr( $name ); ?>" | |
| 191 | + id="<?php echo esc_attr( $field['id'] ); ?>" | |
| 192 | + <?php learn_press_echo_vuejs_write_on_php( $checked ); ?> | |
| 193 | + <?php learn_press_echo_vuejs_write_on_php( $custom_attributes_str ); ?> /> | |
| 194 | + | |
| 195 | + <?php | |
| 196 | + if ( ! empty( $field['description'] ) ) { | |
| 197 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 198 | + | |
| 199 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 200 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 201 | + } | |
| 202 | + } | |
| 203 | + ?> | |
| 204 | + </div> | |
| 205 | + <?php | |
| 206 | +} | |
| 207 | + | |
| 208 | +/** | |
| 209 | + * Output a select input box. | |
| 210 | + * | |
| 211 | + * @param array $field Data about the field to render. | |
| 212 | + */ | |
| 213 | +function lp_meta_box_select_field( $field = array() ) { | |
| 214 | + global $thepostid, $post; | |
| 215 | + | |
| 216 | + $thepostid = lp_meta_box_get_post_id(); | |
| 217 | + $default = ( ! get_post_meta( | |
| 218 | + $thepostid, | |
| 219 | + $field['id'], | |
| 220 | + true | |
| 221 | + ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 222 | + $thepostid, | |
| 223 | + $field['id'], | |
| 224 | + true | |
| 225 | + ); | |
| 226 | + | |
| 227 | + $field = wp_parse_args( | |
| 228 | + $field, | |
| 229 | + array( | |
| 230 | + 'class' => 'select', | |
| 231 | + 'style' => '', | |
| 232 | + 'wrapper_class' => '', // Use "lp-select-2" for select2. | |
| 233 | + 'value' => isset( $field['value'] ) ? $field['value'] : $default, | |
| 234 | + 'name' => $field['id'], | |
| 235 | + 'desc_tip' => false, | |
| 236 | + 'multiple' => false, | |
| 237 | + 'custom_attributes' => array(), | |
| 238 | + ) | |
| 239 | + ); | |
| 240 | + | |
| 241 | + $label_attributes = array( | |
| 242 | + 'for' => $field['id'], | |
| 243 | + ); | |
| 244 | + | |
| 245 | + $field_attributes = (array) $field['custom_attributes']; | |
| 246 | + $field_attributes['style'] = $field['style'] ?? ''; | |
| 247 | + $field_attributes['id'] = $field['id'] ?? ''; | |
| 248 | + $field_attributes['name'] = $field['multiple'] ? $field['name'] . '[]' : $field['name']; | |
| 249 | + $field_attributes['class'] = $field['class'] ?? ''; | |
| 250 | + | |
| 251 | + if ( $field['multiple'] ) { | |
| 252 | + $field['wrapper_class'] = 'lp-select-2'; | |
| 253 | + $field_attributes['multiple'] = true; | |
| 254 | + } | |
| 255 | + | |
| 256 | + $tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : ''; | |
| 257 | + $description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : ''; | |
| 258 | + ?> | |
| 259 | + | |
| 260 | + <p class="form-field <?php echo esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ); ?>"> | |
| 261 | + <label for="<?php echo esc_attr( $field['id'] ); ?>"><?php echo wp_kses_post( $field['label'] ); ?></label> | |
| 262 | + <select <?php echo lp_implode_html_attributes( $field_attributes ); ?>> | |
| 263 | + <?php | |
| 264 | + foreach ( $field['options'] as $key => $value ) { | |
| 265 | + echo '<option value="' . esc_attr( $key ) . '"' . ( is_array( $field['value'] ) ? selected( in_array( (string) $key, $field['value'], true ), true ) : selected( $key, $field['value'], false ) ) . '>' . esc_html( $value ) . '</option>'; | |
| 266 | + } | |
| 267 | + ?> | |
| 268 | + </select> | |
| 269 | + <?php | |
| 270 | + if ( ! empty( $field['description'] ) ) { | |
| 271 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 272 | + | |
| 273 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 274 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 275 | + } | |
| 276 | + } | |
| 277 | + ?> | |
| 278 | + </p> | |
| 279 | + <?php | |
| 280 | +} | |
| 281 | + | |
| 282 | +/** | |
| 283 | + * Output a radio input box. | |
| 284 | + * | |
| 285 | + * @param array $field | |
| 286 | + */ | |
| 287 | +function lp_meta_box_radio_field( $field ) { | |
| 288 | + global $thepostid, $post; | |
| 289 | + | |
| 290 | + $thepostid = lp_meta_box_get_post_id(); | |
| 291 | + $field['class'] = $field['class'] ?? 'select'; | |
| 292 | + $field['style'] = $field['style'] ?? ''; | |
| 293 | + $field['wrapper_class'] = $field['wrapper_class'] ?? ''; | |
| 294 | + $field['default'] = ( ! get_post_meta( | |
| 295 | + $thepostid, | |
| 296 | + $field['id'], | |
| 297 | + true | |
| 298 | + ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 299 | + $thepostid, | |
| 300 | + $field['id'], | |
| 301 | + true | |
| 302 | + ); | |
| 303 | + $field['value'] = $field['value'] ?? $field['default']; | |
| 304 | + $field['name'] = $field['name'] ?? $field['id']; | |
| 305 | + $field['desc_tip'] = $field['desc_tip'] ?? false; | |
| 306 | + | |
| 307 | + echo '<fieldset class="form-field ' . esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ) . '"><h4>' . wp_kses_post( $field['label'] ) . '</h4>'; | |
| 308 | + | |
| 309 | + if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) { | |
| 310 | + learn_press_quick_tip( $field['description'] ); | |
| 311 | + } | |
| 312 | + | |
| 313 | + echo '<ul class="lp-radios-field-meta-box">'; | |
| 314 | + | |
| 315 | + foreach ( $field['options'] as $key => $value ) { | |
| 316 | + echo '<li><label><input | |
| 317 | + name="' . esc_attr( $field['name'] ) . '" | |
| 318 | + value="' . esc_attr( $key ) . '" | |
| 319 | + type="radio" | |
| 320 | + class="' . esc_attr( $field['class'] ) . '" | |
| 321 | + style="' . esc_attr( $field['style'] ) . '" | |
| 322 | + ' . checked( esc_attr( $field['value'] ), esc_attr( $key ), false ) . ' | |
| 323 | + /> ' . ( $value ) . '</label> | |
| 324 | + </li>'; | |
| 325 | + } | |
| 326 | + echo '</ul>'; | |
| 327 | + | |
| 328 | + if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) { | |
| 329 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 330 | + } | |
| 331 | + | |
| 332 | + echo '</fieldset>'; | |
| 333 | +} | |
| 334 | + | |
| 335 | +function lp_meta_box_file_input_field( $field ) { | |
| 336 | + global $thepostid, $post; | |
| 337 | + | |
| 338 | + $thepostid = lp_meta_box_get_post_id(); | |
| 339 | + $field['class'] = isset( $field['class'] ) ? $field['class'] : 'short'; | |
| 340 | + $field['style'] = isset( $field['style'] ) ? $field['style'] : ''; | |
| 341 | + $field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : ''; | |
| 342 | + $field['default'] = ( ! get_post_meta( | |
| 343 | + $thepostid, | |
| 344 | + $field['id'], | |
| 345 | + true | |
| 346 | + ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 347 | + $thepostid, | |
| 348 | + $field['id'], | |
| 349 | + true | |
| 350 | + ); | |
| 351 | + $field['value'] = isset( $field['value'] ) ? $field['value'] : $field['default']; | |
| 352 | + $field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id']; | |
| 353 | + $field['mime_type'] = isset( $field['mime_type'] ) ? implode( ',', $field['mime_type'] ) : ''; | |
| 354 | + $field['multil'] = ( isset( $field['multil'] ) && $field['multil'] ) ? true : false; | |
| 355 | + $field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false; | |
| 356 | + | |
| 357 | + // Custom attribute handling | |
| 358 | + $custom_attributes = array(); | |
| 359 | + | |
| 360 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 361 | + foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 362 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + echo '<div class="form-field ' . esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ) . '"> | |
| 367 | + <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; | |
| 368 | + | |
| 369 | + echo '<div id="' . esc_attr( $field['id'] ) . '" class="lp-meta-box__file ' . esc_attr( $field['class'] ) . '" data-mime="' . $field['mime_type'] . '" data-multil="' . $field['multil'] . '" style="' . esc_attr( $field['style'] ) . '" ' . implode( | |
| 370 | + ' ', | |
| 371 | + $custom_attributes | |
| 372 | + ) . '>'; | |
| 373 | + echo '<ul class="lp-meta-box__file_list">'; | |
| 374 | + | |
| 375 | + if ( ! empty( $field['value'] ) ) { | |
| 376 | + foreach ( (array) $field['value'] as $attachment_id ) { | |
| 377 | + $url = wp_get_attachment_url( $attachment_id ); | |
| 378 | + | |
| 379 | + if ( $url ) { | |
| 380 | + $check_file = wp_check_filetype( $url ); | |
| 381 | + | |
| 382 | + echo '<li class="lp-meta-box__file_list-item image" data-attachment_id="' . $attachment_id . '">'; | |
| 383 | + | |
| 384 | + if ( in_array( $check_file['ext'], array( 'jpg', 'png', 'gif', 'bmp', 'tif', 'jpeg' ), true ) ) { | |
| 385 | + echo wp_get_attachment_image( $attachment_id, 'thumbnail' ); | |
| 386 | + } else { | |
| 387 | + echo '<img class="is_file" src="' . wp_mime_type_icon( $check_file['type'] ) . '" />'; | |
| 388 | + echo '<span>' . wp_basename( get_attached_file( $attachment_id ) ) . '</span>'; | |
| 389 | + } | |
| 390 | + echo '<ul class="actions"><li><a href="#" class="delete"></a></li></ul>'; | |
| 391 | + echo '</li>'; | |
| 392 | + } | |
| 393 | + } | |
| 394 | + } | |
| 395 | + | |
| 396 | + echo '</ul>'; | |
| 397 | + echo '<input class="lp-meta-box__file_input" type="hidden" name="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( | |
| 398 | + ( ! empty( $field['value'] ) && is_array( $field['value'] ) ) ? implode( | |
| 399 | + ',', | |
| 400 | + $field['value'] | |
| 401 | + ) : $field['value'] | |
| 402 | + ) . '" />'; | |
| 403 | + echo '<p>'; | |
| 404 | + echo '<a href="#" class="button btn-upload">' . esc_html__( '+ Add media', 'learnpress' ) . '</a>'; | |
| 405 | + if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) { | |
| 406 | + learn_press_quick_tip( $field['description'] ); | |
| 407 | + } | |
| 408 | + echo '</p>'; | |
| 409 | + | |
| 410 | + if ( ! empty( $field['description'] ) && false === $field['desc_tip'] ) { | |
| 411 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 412 | + } | |
| 413 | + echo '</div>'; | |
| 414 | + echo '</div>'; | |
| 415 | +} | |
| 416 | + | |
| 417 | +/** | |
| 418 | + * Output a duration input box. | |
| 419 | + * | |
| 420 | + * @param array $field | |
| 421 | + */ | |
| 422 | +function lp_meta_box_duration_field( $field ) { | |
| 423 | + global $thepostid, $post; | |
| 424 | + | |
| 425 | + $thepostid = lp_meta_box_get_post_id(); | |
| 426 | + $field['placeholder'] = $field['placeholder'] ?? ''; | |
| 427 | + $field['class'] = $field['class'] ?? 'short'; | |
| 428 | + $field['style'] = $field['style'] ?? ''; | |
| 429 | + $field['wrapper_class'] = $field['wrapper_class'] ?? ''; | |
| 430 | + $field['default'] = ( ! get_post_meta( | |
| 431 | + $thepostid, | |
| 432 | + $field['id'], | |
| 433 | + true | |
| 434 | + ) && isset( $field['default'] ) ) ? $field['default'] : get_post_meta( | |
| 435 | + $thepostid, | |
| 436 | + $field['id'], | |
| 437 | + true | |
| 438 | + ); | |
| 439 | + $field['value'] = $field['value'] ?? $field['default']; | |
| 440 | + $field['name'] = $field['name'] ?? $field['id']; | |
| 441 | + $data_type = empty( $field['data_type'] ) ? '' : $field['data_type']; | |
| 442 | + $duration = learn_press_get_course_duration_support(); | |
| 443 | + | |
| 444 | + $duration_keys = array_keys( $duration ); | |
| 445 | + $default_time = ! empty( $field['default_time'] ) ? $field['default_time'] : end( $duration_keys ); | |
| 446 | + | |
| 447 | + if ( preg_match_all( '!([0-9]+)\s*(' . join( '|', $duration_keys ) . ')?!', $field['value'], $matches ) ) { | |
| 448 | + $a1 = $matches[1][0]; | |
| 449 | + $a2 = in_array( $matches[2][0], $duration_keys ) ? $matches[2][0] : $default_time; | |
| 450 | + } else { | |
| 451 | + $a1 = absint( $field['value'] ); | |
| 452 | + $a2 = $default_time; | |
| 453 | + } | |
| 454 | + | |
| 455 | + // Custom attribute handling | |
| 456 | + $custom_attributes = array(); | |
| 457 | + | |
| 458 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 459 | + foreach ( $field['custom_attributes'] as $attribute => $custom_attribute ) { | |
| 460 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $custom_attribute ) . '"'; | |
| 461 | + } | |
| 462 | + } | |
| 463 | + | |
| 464 | + $html_option = ''; | |
| 465 | + foreach ( $duration as $k => $v ) { | |
| 466 | + $html_option .= sprintf( '<option value="%s" %s>%s</option>', $k, selected( $k, $a2, false ), $v ); | |
| 467 | + } | |
| 468 | + | |
| 469 | + echo '<p class="lp-meta-box__duration form-field ' . esc_attr( $field['id'] . '_field ' . $field['wrapper_class'] ) . '"> | |
| 470 | + <label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>'; | |
| 471 | + | |
| 472 | + echo '<input type="number" class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '[]" id="' . esc_attr( $field['id'] ) . '" value="' . esc_attr( $a1 ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" ' . implode( | |
| 473 | + ' ', | |
| 474 | + $custom_attributes | |
| 475 | + ) . ' /> '; | |
| 476 | + | |
| 477 | + echo '<select name="' . esc_attr( $field['name'] ) . '[]" class="lp-meta-box__duration-select">' . $html_option . '</select>'; | |
| 478 | + | |
| 479 | + if ( ! empty( $field['description'] ) ) { | |
| 480 | + echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>'; | |
| 481 | + | |
| 482 | + if ( ! empty( $field['desc_tip'] ) ) { | |
| 483 | + learn_press_quick_tip( $field['desc_tip'] ); | |
| 484 | + } | |
| 485 | + } | |
| 486 | + | |
| 487 | + echo '</p>'; | |
| 488 | +} | |
| 489 | + | |
| 490 | +/** | |
| 491 | + * Use for Type: custom_fields in LP4. | |
| 492 | + * | |
| 493 | + * @param [type] $field get ID, options.... | |
| 494 | + * @param [type] $values get_option() value | |
| 495 | + * @param [type] $key | |
| 496 | + * | |
| 497 | + * @return void | |
| 498 | + */ | |
| 499 | +function lp_metabox_custom_fields( $field, $values, $key ) { | |
| 500 | + ?> | |
| 501 | + <tr> | |
| 502 | + <td class="sort"> | |
| 503 | + <input class="count" type="hidden" value="<?php echo esc_attr( $key ); ?>" name="<?php echo esc_attr( $field['id'] ) . '[' . $key . ']' . '[sort]'; ?>"> | |
| 504 | + <input type="hidden" value="<?php echo ! empty( $values['id'] ) ? $values['id'] : wp_rand( 1, 10000 ) . $key; ?>" name="<?php echo esc_attr( $field['id'] ) . '[' . $key . ']' . '[id]'; ?>"> | |
| 505 | + </td> | |
| 506 | + <?php | |
| 507 | + if ( $field['options'] ) { | |
| 508 | + foreach ( $field['options'] as $cfk => $val ) { | |
| 509 | + $name = $field['id'] . '[' . $key . ']' . '[' . $cfk . ']'; | |
| 510 | + | |
| 511 | + switch ( $val['type'] ) { | |
| 512 | + case 'text': | |
| 513 | + case 'password': | |
| 514 | + case 'datetime': | |
| 515 | + case 'datetime-local': | |
| 516 | + case 'date': | |
| 517 | + case 'month': | |
| 518 | + case 'time': | |
| 519 | + case 'week': | |
| 520 | + case 'number': | |
| 521 | + case 'email': | |
| 522 | + case 'url': | |
| 523 | + case 'tel': | |
| 524 | + ?> | |
| 525 | + <td> | |
| 526 | + <input name="<?php echo esc_attr( $name ); ?>" type="<?php echo esc_attr( $val['type'] ); ?>" | |
| 527 | + class="input-text" | |
| 528 | + placeholder="<?php echo isset( $val['placeholder'] ) ? esc_attr( $val['placeholder'] ) : ''; ?>" | |
| 529 | + value="<?php echo ! empty( $values[ $cfk ] ) ? esc_attr( $values[ $cfk ] ) : ''; ?>"> | |
| 530 | + </td> | |
| 531 | + <?php | |
| 532 | + break; | |
| 533 | + | |
| 534 | + case 'select': | |
| 535 | + ?> | |
| 536 | + <td> | |
| 537 | + <select name="<?php echo esc_attr( $name ); ?>"> | |
| 538 | + <?php | |
| 539 | + if ( isset( $val['options'] ) ) { | |
| 540 | + foreach ( $val['options'] as $cfks => $cfselect ) { | |
| 541 | + ?> | |
| 542 | + <option | |
| 543 | + value="<?php echo esc_attr( $cfks ); ?>" | |
| 544 | + <?php | |
| 545 | + echo ! empty( $values[ $cfk ] ) ? selected( | |
| 546 | + $values[ $cfk ], | |
| 547 | + (string) $cfks | |
| 548 | + ) : ''; | |
| 549 | + ?> | |
| 550 | + ><?php echo wp_kses_post( $cfselect ); ?></option> | |
| 551 | + <?php | |
| 552 | + } | |
| 553 | + } | |
| 554 | + ?> | |
| 555 | + </select> | |
| 556 | + </td> | |
| 557 | + <?php | |
| 558 | + break; | |
| 559 | + | |
| 560 | + case 'checkbox': | |
| 561 | + ?> | |
| 562 | + <td> | |
| 563 | + <input name="<?php echo esc_attr( $name ); ?>" type="checkbox" name="" value="1" <?php echo ! empty( $values[ $cfk ] ) ? checked( $values[ $cfk ], 'yes' ) : ''; ?>> | |
| 564 | + </td> | |
| 565 | + <?php | |
| 566 | + break; | |
| 567 | + } | |
| 568 | + } | |
| 569 | + } | |
| 570 | + ?> | |
| 571 | + <td width="2%"><a href="#" class="delete"></a></td> | |
| 572 | + </tr> | |
| 573 | + <?php | |
| 574 | +} | |
| 575 | + | |
| 576 | +function lp_implode_html_attributes( $raw_attributes ) { | |
| 577 | + $attributes = array(); | |
| 578 | + foreach ( $raw_attributes as $name => $value ) { | |
| 579 | + $attributes[] = esc_attr( $name ) . '="' . esc_attr( $value ) . '"'; | |
| 580 | + } | |
| 581 | + | |
| 582 | + return implode( ' ', $attributes ); | |
| 583 | +} | |
| 584 | + | |
| 585 | +function lp_meta_box_output( $metaboxes = array() ) { | |
| 586 | + if ( ! empty( $metaboxes ) ) { | |
| 587 | + foreach ( $metaboxes as $id => $field ) { | |
| 588 | + $field['id'] = $id; | |
| 589 | + | |
| 590 | + switch ( $field['type'] ) { | |
| 591 | + case 'text': | |
| 592 | + case 'number': | |
| 593 | + case 'url': | |
| 594 | + lp_meta_box_text_input_field( $field ); | |
| 595 | + break; | |
| 596 | + | |
| 597 | + case 'textarea': | |
| 598 | + lp_meta_box_textarea_field( $field ); | |
| 599 | + break; | |
| 600 | + | |
| 601 | + case 'checkbox': | |
| 602 | + lp_meta_box_checkbox_field( $field ); | |
| 603 | + break; | |
| 604 | + | |
| 605 | + case 'duration': | |
| 606 | + lp_meta_box_duration_field( $field ); | |
| 607 | + break; | |
| 608 | + | |
| 609 | + case 'select': | |
| 610 | + lp_meta_box_select_field( $field ); | |
| 611 | + break; | |
| 612 | + | |
| 613 | + case 'radio': | |
| 614 | + lp_meta_box_radio_field( $field ); | |
| 615 | + break; | |
| 616 | + | |
| 617 | + case 'file': | |
| 618 | + lp_meta_box_file_input_field( $field ); | |
| 619 | + break; | |
| 620 | + } | |
| 621 | + } | |
| 622 | + } | |
| 623 | +} | |