<?php

// Prevent direct access to the file
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Check write permissions and display friendly error messages if needed.
 */
function zutalw_check_folder_permissions() {
    // Only check when the user is an Admin and is in the Admin area
    if ( ! is_admin() || ! current_user_can( 'update_plugins' ) ) {
        return;
    }

    // Critical directories that require write permissions
    $directories_to_check = array(
        WP_PLUGIN_DIR => __( 'Plugins directory (wp-content/plugins)', 'zuta-lucky-wheel' ),
        WP_CONTENT_DIR . '/upgrade' => __( 'Temporary upgrade directory (wp-content/upgrade)', 'zuta-lucky-wheel' )
    );

    $error_messages = array();

    foreach ( $directories_to_check as $dir => $label ) {
        // If the upgrade directory does not exist, try to create it (WordPress usually does this during updates)
        if ( ! file_exists( $dir ) ) {
            $created = @wp_mkdir_p( $dir );
            if ( ! $created ) {
                /* translators: %s: Directory name */
                $error_messages[] = sprintf( __( 'Cannot create <strong>%s</strong>.', 'zuta-lucky-wheel' ), esc_html( $label ) );
                continue; // Skip the is_writable check if creation failed
            }
        }

        // Check write permissions
        if ( ! is_writable( $dir ) ) {
            /* translators: %s: Directory name */
            $error_messages[] = sprintf( __( '<strong>%s</strong> is not writable.', 'zuta-lucky-wheel' ), esc_html( $label ) );
        }
    }

    // If there are errors, hook into admin_notices to display them
    if ( ! empty( $error_messages ) ) {
        // Use a closure (PHP 5.3+) to pass variables into the hook cleanly
        add_action( 'admin_notices', function() use ( $error_messages ) {
            ?>
            <div class="notice notice-error is-dismissible">
                <p><strong><?php esc_html_e( 'Zuta Lucky Wheel: Server Configuration Error!', 'zuta-lucky-wheel' ); ?></strong></p>
                <p><?php esc_html_e( 'Our automatic update system detected that your website has file permission issues, which will prevent automatic plugin updates.', 'zuta-lucky-wheel' ); ?></p>
                <ul style="list-style-type: disc; padding-left: 20px;">
                    <?php foreach ( $error_messages as $message ) : ?>
                        <li><?php echo wp_kses_post( $message ); ?></li>
                    <?php endforeach; ?>
                </ul>
                <p>
                    <strong><?php esc_html_e( 'How to fix:', 'zuta-lucky-wheel' ); ?></strong> <?php _e( 'Please contact your Hosting/VPS provider or use SSH/Control Panel to grant <code>755</code> permissions to the folders and set the correct ownership (owner) for the web server user (usually <code>www-data</code>).', 'zuta-lucky-wheel' ); ?>
                </p>
            </div>
            <?php
        } );
    }
}

// Run the permission check when loading the admin page
add_action( 'admin_init', 'zutalw_check_folder_permissions' );