Skip to content

Commit

Permalink
= 4.2.6.9.3 =
Browse files Browse the repository at this point in the history
~ Added: condition dependency otpion meta-box.
  • Loading branch information
tungnxt89 committed Jul 31, 2024
1 parent d1dfc40 commit d5c6316
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 22 deletions.
40 changes: 38 additions & 2 deletions assets/src/js/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ import { defaultInitTomSelect, searchUserOnListPost } from './init-tom-select.js
}

// Show/hide meta-box field with type checkbox
$( 'input' ).on( 'click', function( e ) {
/*$( 'input' ).on( 'click', function( e ) {
const el = $( e.target );
if ( ! el.length ) {
return;
Expand All @@ -205,12 +205,48 @@ import { defaultInitTomSelect, searchUserOnListPost } from './init-tom-select.js
} else {
elHide.hide();
}
} );
} );*/
};

$( document ).ready( onReady );
}( jQuery ) );

const showHideOptionsDependency = ( e, target ) => {
if ( target.tagName === 'INPUT' ) {
if ( target.closest( '.forminp ' ) ) {
const nameInput = target.name;
const classDependency = nameInput.replace( 'learn_press_', '' );

const elClassDependency = document.querySelectorAll( `.show_if_${ classDependency }` );
if ( elClassDependency ) {
elClassDependency.forEach( ( el ) => {
if ( target.checked ) {
el.classList.remove( 'lp-option-disabled' );
} else {
el.classList.add( 'lp-option-disabled' );
}
} );
}
} else if ( target.closest( '.lp-meta-box' ) ) {
const elLPMetaBox = target.closest( '.lp-meta-box' );
const nameInput = target.name;

const elClassDependency = elLPMetaBox.querySelectorAll( `[data-dependency=${ nameInput }]` );
if ( elClassDependency ) {
elClassDependency.forEach( ( el ) => {
el.classList.toggle( 'lp-option-disabled' );
} );
}
}
}
};

// Events
document.addEventListener( 'click', ( e ) => {
const target = e.target;
showHideOptionsDependency( e, target );
} );

document.addEventListener( 'DOMContentLoaded', () => {
searchUserOnListPost();
defaultInitTomSelect();
Expand Down
6 changes: 5 additions & 1 deletion assets/src/scss/admin/_meta-box.scss
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@

// Use on LP4.
.lp-metabox__table {

.titledesc {

label {
Expand Down Expand Up @@ -269,6 +268,11 @@
}
}

.lp-option-disabled {
pointer-events: none;
opacity: 0.5;
}

.lp-metabox__custom-fields {

table {
Expand Down
6 changes: 4 additions & 2 deletions inc/admin/class-lp-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ protected function _get_scripts(): array {
LP_LESSON_CPT,
LP_QUIZ_CPT,
LP_COURSE_CPT,
LP_ORDER_CPT,
//LP_ORDER_CPT,
'learnpress_page_learn-press-settings',
),
0,
1
1,
'',
[ 'strategy' => 'defer' ]
),
'lp-duplicate-post' => new LP_Asset_Key(
$this->url( self::$_folder_source . 'js/admin/lp-duplicate-post' . self::$_min_assets . '.js' ),
Expand Down
2 changes: 1 addition & 1 deletion inc/admin/meta-box/fields/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$visibility_class[] = 'show_if_' . $value['show_if_checked'];

if ( 'no' === LP_Settings::get_option( $value['show_if_checked'] ) ) {
$visibility_class[] = 'hidden';
$visibility_class[] = 'lp-option-disabled';
}
}
?>
Expand Down
2 changes: 1 addition & 1 deletion inc/admin/meta-box/fields/select.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$visibility_class[] = 'show_if_' . $value['show_if_checked'];

if ( 'no' === LP_Settings::get_option( $value['show_if_checked'] ) ) {
$visibility_class[] = 'hidden';
$visibility_class[] = 'lp-option-disabled';
}
}
?>
Expand Down
2 changes: 1 addition & 1 deletion inc/admin/meta-box/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
$visibility_class[] = 'show_if_' . $value['show_if_checked'];

if ( 'no' === LP_Settings::get_option( $value['show_if_checked'] ) ) {
$visibility_class[] = 'hidden';
$visibility_class[] = 'lp-option-disabled';
}
}
?>
Expand Down
22 changes: 19 additions & 3 deletions inc/admin/views/meta-boxes/course/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ public function metabox( $post_id ) {
}

public function general( $post_id ) {
$repurchase_option_desc = sprintf( '1. %s', __( 'Reset course progress: The course progress and results of student will be removed.' ) );
$repurchase_option_desc = sprintf( '1. %s', __( 'Reset course progress: The course progress and results of student will be removed.' ) );
$repurchase_option_desc .= '<br/>' . sprintf( '2. %s', __( 'Keep course progress: The course progress and results of student will remain.' ) );
$repurchase_option_desc .= '<br/>' . sprintf( '3. %s', __( 'Open popup: The student can decide whether their course progress will be reset with the confirm popup.' ) );

$is_enable_allow_course_repurchase = get_post_meta( $post_id, '_lp_allow_course_repurchase', true ) === 'yes';

return apply_filters(
'lp/course/meta-box/fields/general',
array(
Expand Down Expand Up @@ -122,7 +124,11 @@ public function general( $post_id ) {
'keep' => esc_html__( 'Keep course progress', 'learnpress' ),
'popup' => esc_html__( 'Open popup', 'learnpress' ),
),
'show' => array( '_lp_allow_course_repurchase', '=', 'yes' ), // use 'show' or 'hide'
'dependency' => [
'name' => '_lp_allow_course_repurchase',
'is_disable' => ! $is_enable_allow_course_repurchase
],
//'show' => array( '_lp_allow_course_repurchase', '=', 'yes' ), // use 'show' or 'hide'
)
),
'_lp_level' => new LP_Meta_Box_Select_Field(
Expand Down Expand Up @@ -214,6 +220,8 @@ public function lp_price( $post_id ): array {
$regular_price = $key_exists ? get_post_meta( $post_id, '_lp_regular_price', true ) : $price;
$sale_price = get_post_meta( $post_id, '_lp_sale_price', true );

$is_enable_no_required_enroll = get_post_meta( $post_id, '_lp_no_required_enroll', true ) === 'yes' ? 1 : 0;

return apply_filters(
'lp/course/meta-box/fields/price',
array(
Expand All @@ -229,6 +237,10 @@ public function lp_price( $post_id ): array {
),
'style' => 'width: 70px;',
'class' => 'lp_meta_box_regular_price',
'dependency' => [
'name' => '_lp_no_required_enroll',
'is_disable' => $is_enable_no_required_enroll
],
)
),
'_lp_sale_price' => new LP_Meta_Box_Text_Field(
Expand All @@ -243,6 +255,10 @@ public function lp_price( $post_id ): array {
),
'style' => 'width: 70px;',
'class' => 'lp_meta_box_sale_price',
'dependency' => [
'name' => '_lp_no_required_enroll',
'is_disable' => $is_enable_no_required_enroll
],
)
),
'_lp_sale_start' => new LP_Meta_Box_Date_Field(
Expand Down Expand Up @@ -484,7 +500,7 @@ public function output( $post ) {
?>
<?php if ( isset( $tab_content['content'] ) ) { ?>
<div id="<?php echo esc_attr( $tab_content['target'] ); ?>"
class="lp-meta-box-course-panels">
class="lp-meta-box-course-panels">
<?php
do_action( 'learnpress/course-settings/before-' . $key );

Expand Down
19 changes: 17 additions & 2 deletions inc/admin/views/meta-boxes/fields/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function output( $thepostid ) {
$class = ! empty( $field['class'] ) ? 'class="' . esc_attr( $field['class'] ) . '"' : '';
$style = ! empty( $field['style'] ) ? 'style="' . esc_attr( $field['style'] ) . '"' : '';
$wrapper_class = ! empty( $field['wrapper_class'] ) ? esc_attr( $field['wrapper_class'] ) : '';
$wrapper_attr = $extra['wrapper_attr'] ?? [];
$name = ! empty( $field['name'] ) ? esc_attr( $field['name'] ) : esc_attr( $field['id'] );
$name = 'name="' . $name . '"';

Expand All @@ -62,8 +63,22 @@ public function output( $thepostid ) {
}
}

echo '<div class="form-field ' . esc_attr( $field['id'] . '_field ' . $wrapper_class ) . '">
<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
$dependency_check = $extra['dependency'] ?? [];
if ( ! empty( $dependency_check ) ) {
if ( $dependency_check['is_disable'] ) {
$wrapper_class .= ' lp-option-disabled';
}

$wrapper_attr[] = 'data-dependency=' . $dependency_check['name'];
}

printf(
'<div class="form-field %s" %s><label for="%s">%s</label>',
esc_attr( $this->id . '_field ' . $wrapper_class ),
implode( ' ', $wrapper_attr ),
esc_attr( $this->id ),
wp_kses_post( $this->label )
);

echo '<input type="checkbox" ' . $class . ' ' . $style . ' ' . $name . ' ' . $checked . ' id="' . esc_attr( $field['id'] ) . '" ' . implode( ' ', $custom_attributes ) . '/> ';

Expand Down
22 changes: 19 additions & 3 deletions inc/admin/views/meta-boxes/fields/radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class LP_Meta_Box_Radio_Field extends LP_Meta_Box_Field {
* @param string $id
* @param string $label
* @param string $description
* @param mixed $default
* @param array $extra
* @param mixed $default
* @param array $extra
*/
public function __construct( $label = '', $description = '', $default = '', $extra = array() ) {
parent::__construct( $label, $description, $default, $extra );
Expand All @@ -36,12 +36,28 @@ public function output( $thepostid ) {
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select';
$field['style'] = isset( $field['style'] ) ? $field['style'] : '';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$wrapper_attr = $field['wrapper_attr'] ?? [];
$field['default'] = ( ! $this->meta_value( $thepostid ) && isset( $field['default'] ) ) ? $field['default'] : $this->meta_value( $thepostid );
$field['value'] = isset( $field['value'] ) ? $field['value'] : $field['default'];
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;

echo '<fieldset class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '" ' . $this->condition . '><h4>' . wp_kses_post( $field['label'] ) . '</h4>';
$dependency_check = $extra['dependency'] ?? [];
if ( ! empty( $dependency_check ) ) {
if ( $dependency_check['is_disable'] ) {
$field['wrapper_class'] .= ' lp-option-disabled';
}

$wrapper_attr[] = 'data-dependency=' . $dependency_check['name'];
}

printf(
'<fieldset class="form-field %s" %s><label for="%s">%s</label>',
esc_attr( $this->id . '_field ' . $field['wrapper_class'] ),
implode( ' ', $wrapper_attr ),
esc_attr( $this->id ),
wp_kses_post( $this->label )
);

if ( ! empty( $field['description'] ) && false !== $field['desc_tip'] ) {
learn_press_quick_tip( $field['description'] );
Expand Down
14 changes: 12 additions & 2 deletions inc/admin/views/meta-boxes/fields/select.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function output( $post_id ) {
'multiple' => false,
'custom_attributes' => array(),
'tom_select' => false,
'wrapper_attr' => [],
)
);

Expand Down Expand Up @@ -90,9 +91,19 @@ public function output( $post_id ) {

$tooltip = ! empty( $field['description'] ) && false !== $field['desc_tip'] ? $field['description'] : '';
$description = ! empty( $field['description'] ) && false === $field['desc_tip'] ? $field['description'] : '';

$dependency_check = $field['dependency'] ?? [];
if ( ! empty( $dependency_check ) ) {
if ( $dependency_check['is_disable'] ) {
$field['wrapper_class'] .= ' lp-option-disabled';
}

$field['wrapper_attr'][] = 'data-dependency=' . $dependency_check['name'];
}
?>

<p class="form-field <?php echo esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ); ?>"
<?php echo esc_attr( implode( ' ', $field['wrapper_attr'] ) ) ?>
<?php learn_press_echo_vuejs_write_on_php( $this->condition ? $this->condition : '' ); ?>>
<label for="<?php echo esc_attr( $field['id'] ); ?>">
<?php echo wp_kses_post( $field['label'] ); ?>
Expand All @@ -101,7 +112,6 @@ public function output( $post_id ) {
<option value="" hidden style="display: none"></option>
<?php
foreach ( $field['options'] as $key => $value ) {
$selected = '';
if ( is_array( $field['value'] ) ) {
$selected = in_array( $key, $field['value'] ) ? 'selected="selected"' : '';
} else {
Expand All @@ -110,7 +120,7 @@ public function output( $post_id ) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr( $key ),
esc_attr( $selected),
esc_attr( $selected ),
esc_html( $value )
);
}
Expand Down
13 changes: 12 additions & 1 deletion inc/admin/views/meta-boxes/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function output( $thepostid ) {
$class = ! empty( $extra['class'] ) ? 'class="' . esc_attr( $extra['class'] ) . '"' : '';
$style = ! empty( $extra['style'] ) ? 'style="' . esc_attr( $extra['style'] ) . '"' : '';
$wrapper_class = ! empty( $extra['wrapper_class'] ) ? esc_attr( $extra['wrapper_class'] ) : '';
$wrapper_attr = $extra['wrapper_attr'] ?? [];

$meta_exists = LP_Database::getInstance()->check_key_postmeta_exists( $thepostid, $this->id );
$meta = get_post_meta( $thepostid, $this->id, true );
Expand All @@ -47,9 +48,19 @@ public function output( $thepostid ) {
}
}

$dependency_check = $extra['dependency'] ?? [];
if ( ! empty( $dependency_check ) ) {
if ( $dependency_check['is_disable'] ) {
$wrapper_class .= ' lp-option-disabled';
}

$wrapper_attr[] = 'data-dependency=' . $dependency_check['name'];
}

printf(
'<div class="form-field %s"><label for="%s">%s</label>',
'<div class="form-field %s" %s><label for="%s">%s</label>',
esc_attr( $this->id . '_field ' . $wrapper_class ),
implode( ' ', $wrapper_attr ),
esc_attr( $this->id ),
wp_kses_post( $this->label )
);
Expand Down
22 changes: 19 additions & 3 deletions inc/admin/views/meta-boxes/fields/textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function output( $thepostid ) {
$field['value'] = isset( $field['value'] ) ? $field['value'] : $field['default'];
$field['desc_tip'] = isset( $field['desc_tip'] ) ? $field['desc_tip'] : false;
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$wrapper_attr = $field['wrapper_attr'] ?? [];
$wrapper_class = $extra['wrapper_class'] ?? '';

// Custom attribute handling
$custom_attributes = array();
Expand All @@ -48,8 +50,22 @@ public function output( $thepostid ) {
}
}

echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . '">
<label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label>';
$dependency_check = $extra['dependency'] ?? [];
if ( ! empty( $dependency_check ) ) {
if ( $dependency_check['is_disable'] ) {
$wrapper_class .= ' lp-option-disabled';
}

$wrapper_attr[] = 'data-dependency=' . $dependency_check['name'];
}

printf(
'<div class="form-field %s" %s><label for="%s">%s</label>',
esc_attr( $this->id . '_field ' . $wrapper_class ),
implode( ' ', $wrapper_attr ),
esc_attr( $this->id ),
wp_kses_post( $this->label )
);

echo '<textarea class="' . esc_attr( $field['class'] ) . '" style="' . esc_attr( $field['style'] ) . '" name="' . esc_attr( $field['name'] ) . '" id="' . esc_attr( $field['id'] ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" rows="5" ' . implode(
' ',
Expand All @@ -64,7 +80,7 @@ public function output( $thepostid ) {
}
}

echo '</p>';
echo '</div>';
}

public function save( $post_id ) {
Expand Down

0 comments on commit d5c6316

Please sign in to comment.