plugin

WPTurbo » Snippets » plugin
0

Created with:

Visibility: 

public

Creator: Wole

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

<?php

namespace WPTurbo;

function wpturbo_display_latest_recipes( int $number_of_posts = 5 ): void {

    $args = [
        'post_type'      => 'recipes',
        'posts_per_page' => $number_of_posts,
        'post_status'    => 'publish',
    ];

    $latest_recipes = new WP_Query( $args );

    if ( $latest_recipes->have_posts() ) {
        echo '<ul>';
        while ( $latest_recipes->have_posts() ) {
            $latest_recipes->the_post();
            printf(
                '<li><a href="%1$s">%2$s</a></li>',
                esc_url( get_permalink() ),
                esc_html( get_the_title() )
            );
        }
        echo '</ul>';
        wp_reset_postdata();
    } else {
        echo '<p>' . esc_html__( 'No recipes found.', 'wpturbo' ) . '</p>';
    }
}

function wpturbo_add_student_marks( int $ca1, int $ca2 ): int {
    return $ca1 + $ca2; // Example of adding CA1 and CA2
}

function wpturbo_add_highest_mark_column( array $students ): void {
    foreach ( $students as &$student ) {
        $student['highest_mark'] = max( $student['ca1'], $student['ca2'] ); // Assuming CA1 and CA2 are keys in the array
    }
    // Output students with highest marks
    foreach ( $students as $student ) {
        echo 'Student: ' . esc_html( $student['name'] ) . ' - Highest Mark: ' . esc_html( $student['highest_mark'] ) . '<br>';
    }
}

function wpturbo_generate_pins( int $student_id, int $usage_limit = 1 ): string {
    $pin = wp_generate_password( 6, false, false ); // Generate a 6-digit PIN
    // Store the PIN and limit in a database or transient for the student
    update_user_meta( $student_id, 'student_pin', $pin );
    update_user_meta( $student_id, 'pin_usage_limit', $usage_limit );

    return $pin;
}

function wpturbo_check_pin_usage( int $student_id ): bool {
    $usage = get_user_meta( $student_id, 'pin_usage_limit', true );
    if ( $usage > 0 ) {
        // Decrease the usage limit
        update_user_meta( $student_id, 'pin_usage_limit', --$usage );
        return true; // PIN can still be used
    }
    return false; // PIN usage limit exceeded
}

// Example usage
wpturbo_display_latest_recipes();
$student_marks = [
    ['name' => 'John', 'ca1' => 85, 'ca2' => 90],
    ['name' => 'Jane', 'ca1' => 78, 'ca2' => 88],
];

wpturbo_add_highest_mark_column( $student_marks );
$pin = wpturbo_generate_pins( 1, 3 ); // Generate PIN for student with ID 1, usable 3 times
$is_pin_valid = wpturbo_check_pin_usage( 1 ); // Check if PIN can still be used
				

These functions collectively provide a simplified version of the features you requested. You may need to adjust the database storage and retrieval methods according to your needs. Always ensure to follow best practices for security and data management.

This code can be further expanded and adapted to fit your entire plugin structure and specific requirements.

Register an account to save your snippets or go Pro to get more features.