delete a specific array element when date expired of it

Question

I have user levels for access content. I made a start date and end date input for each other. Each level has its own history. Whichever level date exceeds today’s date, it will be removed from user levels array, but all of them are removed in my function. How can I do that?

add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );

function crf_show_extra_profile_fields( $user ) {
  $choices = [
    '+15 minutes'  => '2 Hafta',
    '+9 months' => '9 Ay',
    '+12 months' => '12 Ay',
    '+2 minutes' => 'Uzat',
];
$rua_user = rua_get_user( $user->ID );
$user_levels = $rua_user->get_level_ids();
$current_date = date("d/m/y H:i:s");

  foreach ( $user_levels as $user_level ) {

    $start_time_meta = 'start-time-'.$user_level;
    $start_time = get_the_author_meta( $start_time_meta, $user->ID);

    $end_time_meta = 'end-time-'.$user_level;
    $end_time = get_the_author_meta( $end_time_meta, $user->ID);

    $effectiveDate = strtotime($end_time, strtotime($start_time));
    $time = date("d/m/y H:i:s", $effectiveDate);

    ob_start();

    if ($current_date > $time) {
        $f = "EXPIRED";
    }

    echo esc_html( get_the_title( $user_level ) ) . ' -> ';
    ?>
    <input type="datetime-local" id="start-time-<?php echo esc_attr( $user_level ); ?>" name="start-time-<?php echo esc_attr( $user_level ); ?>" value="<?php echo esc_attr( $start_time ); ?>"> - 

    <select name='end-time-<?php echo esc_attr( $user_level ); ?>' id='end-time-<?php echo esc_attr( $user_level ); ?>' style='width:180px;'>
        <option value='default'>Süre Seçiniz</option>
        <?php
        foreach ( $choices as $value => $label ) {
            ?>
            <option value="<?php echo esc_attr( $value ); ?>"
                <?php selected( $value, $end_time ); ?>>
                <?php echo esc_html( $label ); ?>
            </option>
            <?php
        }
        ?>
    </select> <?php echo $time; ?>
    <br>
    <br>
    <?php
    $html = ob_get_clean();
    $elements[] = $html;
  } ?>

  <h3><?php esc_html_e( 'Yetki Süresi', 'crf' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="year_of_birth"><?php esc_html_e( 'Bitiş Tarihi', 'crf' ); ?></label></th>
            <td>
                <input type="date" id="year_of_birth" name="year_of_birth" value="<?php
                    echo esc_attr( get_the_author_meta( 'year_of_birth', $user->ID ) ); ?>">
            </td>
        </tr>
    </table>
    <table class="form-table">
        <tr>
            <th><?php esc_html_e( 'Kitaplar', 'crf' ); ?></th>
            <td>
                <?php
                    sort($elements);
                    echo (vsprintf(__('%s'),implode("",$elements )));
                ?>
            </td>
        </tr>
    </table>


    <?php
}

function my_demo_cronjob_action () {
    $present_date = date("Y-m-d H:i:s");
    $user = get_user_by('id',26);
    $user_levels = rua_get_user($user)->get_level_ids(false, false, true);

    foreach ($user_levels as $level) {

        $end_time_meta = 'end-time-'.$level;
        $end_time = get_the_author_meta( $end_time_meta, $user);

        $effectiveDate = strtotime($end_time, strtotime($present_date));
        $time = date("d/m/y H:i:s", $effectiveDate);

        if ($present_date > $time) {
            rua_get_user($user)->remove_level($level);
        }
    }
}
add_action('my_demo_cronjob_action', 'my_demo_cronjob_action');

the cronjob function is executing by a cron plugin

0
freedom667 2 months 0 Answers 9 views 0

Leave an answer