<?php
/**
 * Plugin Name: RG Upload Limits
 * Description: Sets PHP upload limits to 100MB via .htaccess
 * Version: 1.0
 */

// Run early to ensure .htaccess exists before uploads
add_action('init', 'rg_ensure_htaccess_upload_limits', 1);

function rg_ensure_htaccess_upload_limits() {
    $htaccess_file = ABSPATH . '.htaccess';
    $marker = '# RG Upload Limits';

    // Check if our settings are already in .htaccess
    if (file_exists($htaccess_file)) {
        $content = file_get_contents($htaccess_file);
        if (strpos($content, $marker) !== false) {
            return; // Already configured
        }
    }

    // PHP upload settings
    $upload_rules = <<<HTACCESS
$marker
php_value upload_max_filesize 100M
php_value post_max_size 100M
php_value memory_limit 512M
php_value max_execution_time 300
# END RG Upload Limits

HTACCESS;

    // Prepend to existing .htaccess or create new
    $existing = file_exists($htaccess_file) ? file_get_contents($htaccess_file) : '';
    file_put_contents($htaccess_file, $upload_rules . "\n" . $existing);
}
