Skip to content

Commit

Permalink
= 4.2.7.9 =
Browse files Browse the repository at this point in the history
~ Added: option Store IP Guest to handle checkout
~ LP_Helper: clean code.
  • Loading branch information
tungnxt89 committed Feb 21, 2025
1 parent b73aeb5 commit d944c86
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 219 deletions.
6 changes: 3 additions & 3 deletions config/settings/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@
'type' => 'checkbox',
),
array(
'title' => esc_html__( 'Store guest buy course on SESSION', 'learnpress' ),
'desc' => esc_html__( 'Enable the option, data user not login buy/enroll course will store on $_SESSION instead of $_COOKIE, by some reason Cookie always change, so can not identifier user', 'learnpress' ),
'id' => 'store_in_session',
'title' => esc_html__( 'Store IP Guest to handle checkout', 'learnpress' ),
'desc' => esc_html__( 'Enable the option, IP of client is identifier user instead $_COOKIE', 'learnpress' ),
'id' => 'store_ip_customer_session',
'default' => 'no',
'type' => 'checkbox',
),
Expand Down
234 changes: 30 additions & 204 deletions inc/class-lp-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,6 @@ public static function shuffle_assoc( &$array ) {
return true;
}

/**
* Sanitize array by removing empty and/or duplicating values.
*
* @param array $array
*
* @return array
*/
public static function sanitize_array( $array ) {
$array = array_filter( $array );
$array = array_unique( $array );

return $array;
}

/**
* MD5 an array.
*
Expand All @@ -94,43 +80,13 @@ public static function array_to_md5( $array ) {
*
* @param array|int $ids
*
* @Todo: tungnx - need to review code
* @Todo: tungnx - need to review code - addon h5p v4.0.3 still use
* @deprecated 4.1.6.9
*/
public static function cache_posts( $ids ) {
//_deprecated_function( __FUNCTION__, '4.1.6.9' );
}

/**
* Sort an array by a field.
* Having some issue with default PHP usort function.
*
* @param array $array .
* @param string $field .
* @param int $default .
*/
public static function sort_by_priority( &$array, $field = 'priority', $default = 10 ) {
foreach ( $array as $k => $item ) {
if ( ! array_key_exists( $field, $item ) ) {
$array[ $k ][ $field ] = $default;
}
}

$priority = array_unique( wp_list_pluck( $array, $field ) );
sort( $priority );
$priority = array_fill_keys( $priority, array() );

foreach ( $array as $k => $item ) {
$priority[ $item[ $field ] ][] = $item;
}

$sorted = array();
foreach ( $priority as $item ) {
$sorted = array_merge( $sorted, $item );
}
$array = $sorted;
}

/**
* Merge two or more classes into one.
*
Expand Down Expand Up @@ -312,107 +268,6 @@ public static function create_page( array $args, string $key_option ) {
return $page_id;
}

/**
* Wrap function ksort of PHP itself and support recursive.
*
* @param array $array
* @param int $sort_flags
*
* @return bool
* @since 3.3.0
*/
public static function ksort( &$array, $sort_flags = SORT_REGULAR ) {
if ( ! is_array( $array ) ) {
return false;
}

ksort( $array, $sort_flags );

foreach ( $array as &$arr ) {
self::ksort( $arr, $sort_flags );
}

return true;
}

/**
* Return new array/object with the keys exists in list of props.
*
* @param array|string $props
* @param array|object $obj
*
* @return array|object
* @deprecated 4.2.5.3
*/
public function pick( $props, $obj ) {
_deprecated_function( __METHOD__, '4.2.5.3' );

return [];
$is_array = is_array( $obj );
$new_array = array();
settype( $props, 'array' );

foreach ( $props as $prop ) {
if ( $is_array && array_key_exists( $prop, $obj ) ) {
$new_array[ $prop ] = $obj[ $prop ];
} elseif ( ! $is_array && property_exists( $obj, $prop ) ) {
$new_array[ $prop ] = $obj->{$prop};
}
}

return $is_array ? $new_array : (object) $new_array;
}

public static function list_pluck( $list, $field, $index_key = null ) {
$newlist = array();

if ( ! $index_key ) {
/*
* This is simple. Could at some point wrap array_column()
* if we knew we had an array of arrays.
*/
foreach ( $list as $key => $value ) {
if ( is_callable( array( $value, $field ) ) ) {
$newlist[ $key ] = call_user_func( array( $value, $field ) );
} elseif ( is_object( $value ) ) {
$newlist[ $key ] = $value->$field;
} else {
$newlist[ $key ] = $value[ $field ];
}
}

return $newlist;
}

/*
* When index_key is not set for a particular item, push the value
* to the end of the stack. This is how array_column() behaves.
*/
foreach ( $list as $value ) {
if ( is_callable( array( $value, $field ) ) ) {
if ( isset( $value->$index_key ) ) {
$newlist[ $value->$index_key ] = call_user_func( array( $value, $field ) );
} else {
$newlist[] = call_user_func( array( $value, $field ) );
}
} elseif ( is_object( $value ) ) {
if ( isset( $value->$index_key ) ) {
$newlist[ $value->$index_key ] = $value->$field;
} else {
$newlist[] = $value->$field;
}
} else {
if ( isset( $value[ $index_key ] ) ) {
$newlist[ $value[ $index_key ] ] = $value[ $field ];
} else {
$newlist[] = $value[ $field ];
}
}
}

return $newlist;
}

/**
* Get the current url
*
Expand Down Expand Up @@ -488,40 +343,6 @@ public static function sanitize_params_submitted( $value, string $type_content =
return $value;
}

/**
* Wrap function $wpdb->prepare(...) to support arguments as
* array.
*
* @param string $query
* @param array|mixed $args
*
* @return string
* @example
*
* $this->prepare($sql, $one, $two, array($three, $four, $file))
* => $wpdb->prepare($sql, $one, $two, $three, $four, $file)
*/
public static function prepare( $query, $args ) {
_deprecated_function( __METHOD__, '4.2.5.3' );

return '';
global $wpdb;

$args = func_get_args();
array_shift( $args );
$new_args = array();

foreach ( $args as $arg ) {
if ( is_array( $arg ) ) {
$new_args = array_merge( $new_args, $arg );
} else {
$new_args[] = $arg;
}
}

return $wpdb->prepare( $query, $new_args );
}

public static function db_format_array( array $arr, string $format = '%d' ): string {
$arr_formatted = array_fill( 0, sizeof( $arr ), $format );

Expand Down Expand Up @@ -701,30 +522,6 @@ public static function print_inline_script_tag( string $name_variable_script, ar
wp_print_inline_script_tag( $script, $tag_args );
}

/**
* Get translation of value
*
* @param string $value
*
* @return string
* @since 4.2.7.4
* @version 1.0.0
*/
public static function get_i18n_of_value( string $value ): string {
switch ( $value ) {
case 'failed':
$i18n = esc_html__( 'Failed', 'learnpress' );
break;
case 'in-progress':
$i18n = esc_html__( 'In Progress', 'learnpress' );
break;
default:
$i18n = $value;
}

return apply_filters( 'learn-press/i18n/value', $i18n, $value );
}

/**
* Get translation string single/plural
*
Expand Down Expand Up @@ -785,4 +582,33 @@ public static function get_i18n_string_plural( float $number, string $string_val

return apply_filters( 'learn-press/i18n/plural', $plural, $number, $string_value, $include_number );
}

/**
* Get client ip address
*
* @return mixed|string
* @since 4.2.7.9
* @version 1.0.0
*/
public static function get_client_ip(): string {
$ipaddress = 'ip-unknown';
if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) {
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
} elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) {
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
} elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) {
$ipaddress = $_SERVER['HTTP_FORWARDED'];
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
$ipaddress = $_SERVER['REMOTE_ADDR'];
}

$ipaddress = (string) $ipaddress;
$ipaddress = 'gip-' . $ipaddress;

return $ipaddress;
}
}
11 changes: 2 additions & 9 deletions inc/class-lp-session-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,8 @@ protected function init(): LP_Session_Handler {
if ( ! is_user_logged_in() ) { // Generate data and set cookie for guest
$this->set_session_expiration( $expire_time_for_guest );

if ( LP_Settings::is_store_data_in_php_session() ) { // Store data in $_SESSION
$customer_id = $_SESSION['lp_customer_id'] ?? '';
if ( empty( $customer_id ) ) {
// Create new session for user Guest.
$customer_id = 'g-' . uniqid();
$_SESSION['lp_customer_id'] = $customer_id;
}

$this->_customer_id = $customer_id;
if ( LP_Settings::is_store_ip_customer() ) {
$this->_customer_id = LP_Helper::get_client_ip();
} else { // Store data in COOKIE
$cookie = $this->get_cookie_data();
// If cookie exists, set data from cookie for guest
Expand Down
4 changes: 2 additions & 2 deletions inc/class-lp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ public static function is_created_tb_thim_cache(): bool {
*
* @return bool
*/
public static function is_store_data_in_php_session(): bool {
return self::get_option( 'store_in_session', 'no' ) === 'yes';
public static function is_store_ip_customer(): bool {
return self::get_option( 'store_ip_customer_session', 'no' ) === 'yes';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"react-easy-crop": "^4.7.4",
"refx": "^3.1.1",
"sortablejs": "^1.15.0",
"tom-select": "2.4.1",
"tom-select": "2.4.3",
"chart.js": "4.4.7",
"cropperjs": "1.6.2",
"style-loader": "3.3.4",
Expand Down

0 comments on commit d944c86

Please sign in to comment.