-
-
Save Lonsdale201/cc13a338d99321bc902b7161c4912e65 to your computer and use it in GitHub Desktop.
JetEngine - Dynamic Visibility - Get user registration date condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// place the code in the child theme functions.php or a custom code snippets plugin like Fluent Snippts etc.. | |
// This conditions only work with logged in useres | |
// How to work & what is it | |
// With this condition, you can configure whether an element becomes visible to a user after a | |
// specified amount of time (hours or days) has passed since the current user's registration, or conversely, to hide it. | |
// Choose whether you want to specify the time in days or hours, then provide the value in the input field. | |
// Finally, set whether the element should be shown or hidden based on the condition relative to the current user's registration date. | |
// The condition is named User Registered Date and can be found under the User category. | |
// (If you think there's a better name for it, feel free to rename it in the code.) | |
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) { | |
class User_Registered_Date_Visibility extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base { | |
/** | |
* Get condition ID | |
*/ | |
public function get_id() { | |
return 'user-registered-date'; | |
} | |
/** | |
* Get condition name | |
*/ | |
public function get_name() { | |
return __( 'User Registered Date', 'jet-engine' ); | |
} | |
/** | |
* Get condition group | |
*/ | |
public function get_group() { | |
return 'user'; | |
} | |
/** | |
* Check the condition | |
*/ | |
public function check( $args = array() ) { | |
// Get current user | |
$current_user = wp_get_current_user(); | |
if ( ! $current_user || 0 === $current_user->ID ) { | |
return false; // If no user is logged in, return false | |
} | |
// Get user registration date | |
$registered_date = strtotime( $current_user->user_registered ); | |
// Get settings | |
$unit = isset( $args['condition_settings']['time_unit'] ) ? $args['condition_settings']['time_unit'] : 'hours'; | |
$value = isset( $args['condition_settings']['value'] ) ? intval( $args['condition_settings']['value'] ) : 0; | |
// Calculate the target timestamp based on the unit | |
$time_offset = ( $unit === 'days' ) ? $value * DAY_IN_SECONDS : $value * HOUR_IN_SECONDS; | |
$target_time = $registered_date + $time_offset; | |
// Get current timestamp | |
$current_time = current_time( 'timestamp' ); | |
// Check condition type (show/hide) | |
$type = isset( $args['type'] ) ? $args['type'] : 'show'; | |
return ( 'hide' === $type ) ? $current_time < $target_time : $current_time >= $target_time; | |
} | |
/** | |
* Get custom controls for the condition | |
*/ | |
public function get_custom_controls() { | |
return array( | |
'time_unit' => array( | |
'label' => __( 'Time Unit', 'jet-engine' ), | |
'type' => 'select', | |
'options' => array( | |
'hours' => __( 'Hours', 'jet-engine' ), | |
'days' => __( 'Days', 'jet-engine' ), | |
), | |
'default' => 'hours', | |
), | |
'value' => array( | |
'label' => __( 'Value', 'jet-engine' ), | |
'type' => 'number', | |
'default' => 0, | |
), | |
); | |
} | |
/** | |
* Determine if the condition is for fields | |
*/ | |
public function is_for_fields() { | |
return false; // Not a field-specific condition | |
} | |
/** | |
* Determine if the condition needs value detection | |
*/ | |
public function need_value_detect() { | |
return false; // No value detection needed | |
} | |
} | |
$conditions_manager->register_condition( new User_Registered_Date_Visibility() ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment