2023-06-22 08:23:43 +02:00
< ? php
/**
* Class Codepopular_WMUFS
*/
2025-06-09 09:58:01 +02:00
class MaxUploader_Admin {
2025-12-12 13:13:07 +01:00
static function init () {
2023-06-22 08:23:43 +02:00
if ( is_admin () ) {
add_action ( 'admin_enqueue_scripts' , array ( __CLASS__ , 'wmufs_style_and_script' ));
add_action ( 'admin_menu' , array ( __CLASS__ , 'upload_max_file_size_add_pages' ));
add_filter ( 'plugin_action_links_' . WMUFS_PLUGIN_BASENAME , array ( __CLASS__ , 'plugin_action_links' ));
add_filter ( 'plugin_row_meta' , array ( __CLASS__ , 'plugin_meta_links' ), 10 , 2 );
add_filter ( 'admin_footer_text' , array ( __CLASS__ , 'admin_footer_text' ));
2025-06-09 09:58:01 +02:00
// Handle form submission
2025-12-12 13:13:07 +01:00
add_action ( 'admin_init' , array ( __CLASS__ , 'easy_media_form_submission' ));
2025-06-09 09:58:01 +02:00
add_action ( 'admin_head' , array ( __CLASS__ , 'show_admin_notice' ));
2025-12-12 13:13:07 +01:00
// AJAX handlers
add_action ( 'wp_ajax_wmufs_restore_default_settings' , array ( __CLASS__ , 'restore_default_settings_ajax' ));
2023-06-22 08:23:43 +02:00
}
2025-06-09 09:58:01 +02:00
// Set Upload Limit
self :: upload_max_increase_upload ();
2023-06-22 08:23:43 +02:00
}
/**
2025-06-09 09:58:01 +02:00
* Handle form submission for max uploader settings .
* @ return void
2023-06-22 08:23:43 +02:00
*/
2025-12-12 13:13:07 +01:00
static function easy_media_form_submission () {
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
if (
! isset ( $_POST [ 'easy_media_set_size_limit' ]) ||
! wp_verify_nonce ( sanitize_text_field ( $_POST [ 'easy_media_set_size_limit' ]), 'easy_media_set_size_action' )
) {
return ;
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
$settings = get_option ( 'wmufs_settings' , []);
// Ensure settings is always an array
if ( ! is_array ( $settings )) {
$settings = [];
}
// Save Type of Limit
if ( isset ( $_POST [ 'type' ])) {
$settings [ 'limit_type' ] = sanitize_text_field ( $_POST [ 'type' ]);
}
// 🧩 Base limit for all users
if ( isset ( $_POST [ 'max_file_size_field' ])) {
$limit = ( int ) sanitize_text_field ( $_POST [ 'max_file_size_field' ]) * 1024 * 1024 ;
if ( ! isset ( $settings [ 'max_limits' ])) {
$settings [ 'max_limits' ] = [];
}
$settings [ 'max_limits' ][ 'all' ] = $limit ;
}
// 🧩 Per-role upload limits (optional)
if ( isset ( $_POST [ 'role_limits' ]) && is_array ( $_POST [ 'role_limits' ])) {
if ( ! isset ( $settings [ 'max_limits' ])) {
$settings [ 'max_limits' ] = [];
}
foreach ( $_POST [ 'role_limits' ] as $role => $size ) {
$settings [ 'max_limits' ][ $role ] = ( int ) sanitize_text_field ( $size ) * 1024 * 1024 ;
}
}
// ⏱ Execution time
if ( isset ( $_POST [ 'max_execution_time_field' ])) {
$settings [ 'max_execution_time' ] = ( int ) sanitize_text_field ( $_POST [ 'max_execution_time_field' ]);
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
// 💾 Memory limit
if ( isset ( $_POST [ 'max_memory_limit_field' ])) {
$settings [ 'max_memory_limit' ] = ( int ) sanitize_text_field ( $_POST [ 'max_memory_limit_field' ]) * 1024 * 1024 ;
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
update_option ( 'wmufs_settings' , $settings );
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
set_transient ( 'wmufs_settings_updated' , 'Settings saved successfully.' , 30 );
wp_safe_redirect ( admin_url ( 'admin.php?page=easy_media' ));
exit ;
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
/**
* AJAX handler for restoring default settings
*/
static function restore_default_settings_ajax () {
// Verify nonce
if ( ! isset ( $_POST [ 'nonce' ]) || ! wp_verify_nonce ( $_POST [ 'nonce' ], 'wmufs_restore_defaults' )) {
wp_send_json_error ( array ( 'message' => __ ( 'Security check failed.' , 'wp-maximum-upload-file-size' )));
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
// Check user capabilities
if ( ! current_user_can ( 'manage_options' )) {
wp_send_json_error ( array ( 'message' => __ ( 'You do not have permission to perform this action.' , 'wp-maximum-upload-file-size' )));
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
try {
// Delete main plugin settings (this is the primary setting)
delete_option ( 'wmufs_settings' );
// Delete legacy settings only if they exist (for backward compatibility)
if ( get_option ( 'wmufs_maximum_execution_time' ) !== false ) {
delete_option ( 'wmufs_maximum_execution_time' );
}
if ( get_option ( 'wmufs_memory_limit' ) !== false ) {
delete_option ( 'wmufs_memory_limit' );
}
if ( get_option ( 'wmufs_notice_disable_time' ) !== false ) {
delete_option ( 'wmufs_notice_disable_time' );
}
// Clear any transients (safe to call even if they don't exist)
delete_transient ( 'wmufs_settings_updated' );
delete_transient ( 'codepopular_promo_data' );
delete_transient ( 'codepopular_blog_posts' );
// Also clear any Appsero tracking settings if they exist
$appsero_options = array (
'wp_maximum_upload_file_size_allow_tracking' ,
'wp_maximum_upload_file_size_tracking_notice' ,
'wp_maximum_upload_file_size_tracking_last_send' ,
'wp_maximum_upload_file_size_tracking_skipped'
);
foreach ( $appsero_options as $option ) {
if ( get_option ( $option ) !== false ) {
delete_option ( $option );
}
}
wp_send_json_success ( array (
'message' => __ ( 'Settings have been restored to default values successfully.' , 'wp-maximum-upload-file-size' )
));
} catch ( Exception $e ) {
wp_send_json_error ( array (
'message' => __ ( 'An error occurred while restoring settings: ' , 'wp-maximum-upload-file-size' ) . $e -> getMessage ()
));
}
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
static function show_admin_notice () {
2025-06-09 09:58:01 +02:00
if ( $message = get_transient ( 'wmufs_settings_updated' ) ) {
2025-12-12 13:13:07 +01:00
echo '<div class="notice notice-success is-dismissible wmufs-notice"><p>' . esc_html ( $message ) . '</p></div>' ;
2025-06-09 09:58:01 +02:00
delete_transient ( 'wmufs_settings_updated' );
}
}
2025-12-12 13:13:07 +01:00
static function wmufs_style_and_script () {
2025-06-09 09:58:01 +02:00
wp_enqueue_style ( 'wmufs-admin-style' , WMUFS_PLUGIN_URL . 'assets/css/wmufs.css' , array (), WMUFS_PLUGIN_VERSION );
2023-06-22 08:23:43 +02:00
2025-06-09 09:58:01 +02:00
// Ensure jQuery is loaded
wp_enqueue_script ( 'jquery' );
2023-06-22 08:23:43 +02:00
2025-06-09 09:58:01 +02:00
// Enqueue your script with explicit dependency on jQuery
2025-12-12 13:13:07 +01:00
wp_enqueue_script ( 'wmufs-admin' , WMUFS_PLUGIN_URL . 'assets/js/admin.js' , array ( 'jquery' ), WMUFS_PLUGIN_VERSION , true );
2023-06-22 08:23:43 +02:00
wp_localize_script (
'wmufs-admin' ,
'wmufs_admin_notice_ajax_object' ,
array (
'wmufs_admin_notice_ajax_url' => admin_url ( 'admin-ajax.php' ),
2025-06-09 09:58:01 +02:00
'nonce' => wp_create_nonce ( 'wmufs_notice_status' ),
'plugin_url' => WMUFS_PLUGIN_URL ,
'active_tab' => isset ( $_GET [ 'tab' ]) ? sanitize_text_field ( $_GET [ 'tab' ]) : 'general' ,
2023-06-22 08:23:43 +02:00
)
);
2025-12-12 13:13:07 +01:00
// Add ajaxurl for inline scripts
wp_add_inline_script ( 'wmufs-admin' , 'var ajaxurl = "' . admin_url ( 'admin-ajax.php' ) . '";' , 'before' );
2023-06-22 08:23:43 +02:00
}
2025-12-12 13:13:07 +01:00
static function get_plugin_version () {
2025-06-09 09:58:01 +02:00
$plugin_data = get_file_data ( __FILE__ , array ( 'version' => 'Version' ), 'plugin' );
2023-06-22 08:23:43 +02:00
return $plugin_data [ 'version' ];
2025-06-09 09:58:01 +02:00
}
2025-12-12 13:13:07 +01:00
static function is_plugin_page () {
2023-06-22 08:23:43 +02:00
$current_screen = get_current_screen ();
2025-12-12 13:13:07 +01:00
return ( $current_screen -> id === 'media_page_easy_media' );
2025-06-09 09:58:01 +02:00
}
2023-06-22 08:23:43 +02:00
2025-12-12 13:13:07 +01:00
/**
* Add plugin action links ( Settings + Upgrade to Pro ) .
*
* @ param array $links Existing plugin action links .
* @ return array Modified plugin action links .
*/
public static function plugin_action_links ( $links ) {
// Settings link (always show).
$settings_link = sprintf (
'<a href="%s">%s</a>' ,
esc_url ( admin_url ( 'admin.php?page=easy_media' ) ),
esc_html__ ( 'Settings' , 'easy-media' )
);
// Add the Settings link first.
array_unshift ( $links , $settings_link );
// Only show "Upgrade to Pro" if premium is NOT active.
if ( ! WMUFS_Helper :: is_premium_active () ) {
$upgrade_link = sprintf (
'<a href="%s" target="_blank" style="color:#ff6600;font-weight:bold;">%s</a>' ,
esc_url ( 'https://codepopular.com/product/easymedia/?utm_source=upgrade-pro-button' ),
esc_html__ ( 'Upgrade to Pro' , 'easy-media' )
);
array_unshift ( $links , $upgrade_link );
}
2023-06-22 08:23:43 +02:00
return $links ;
2025-06-09 09:58:01 +02:00
}
2023-06-22 08:23:43 +02:00
2025-12-12 13:13:07 +01:00
/**
* Add plugin meta links ( Support , etc . ) under plugin name in Plugins list .
*
* @ param array $links Existing plugin meta links .
* @ param string $file Plugin file path .
* @ return array Modified plugin meta links .
*/
public static function plugin_meta_links ( $links , $file ) {
if ( $file === plugin_basename ( __FILE__ ) ) {
$links [] = sprintf (
'<a target="_blank" href="%s">%s</a>' ,
esc_url ( 'https://wordpress.org/support/plugin/wp-maximum-upload-file-size/' ),
esc_html__ ( 'Support' , 'easy-media' )
);
2023-06-22 08:23:43 +02:00
}
2025-12-12 13:13:07 +01:00
2023-06-22 08:23:43 +02:00
return $links ;
2025-06-09 09:58:01 +02:00
}
2023-06-22 08:23:43 +02:00
2025-12-12 13:13:07 +01:00
2023-06-22 08:23:43 +02:00
static function admin_footer_text ( $text ) {
if ( ! self :: is_plugin_page () ) {
return $text ;
}
2025-12-12 13:13:07 +01:00
return '<span id="footer-thankyou">If you like <strong><ins>EasyMedia</ins></strong> please leave us a <a target="_blank" style="color:#f9b918" href="https://wordpress.org/support/view/plugin-reviews/wp-maximum-upload-file-size?rate=5#postform">★★★★★</a> rating. A huge thank you in advance!</span>' ;
2025-06-09 09:58:01 +02:00
}
2023-06-22 08:23:43 +02:00
static function upload_max_file_size_add_pages () {
2025-12-12 13:13:07 +01:00
add_submenu_page (
'upload.php' , // Parent Slug.
'EasyMedia - Increase Max Upload File Size' ,
'EasyMedia' ,
2023-06-22 08:23:43 +02:00
'manage_options' ,
2025-12-12 13:13:07 +01:00
'easy_media' ,
array ( __CLASS__ , 'upload_max_file_size_dash' )
2023-06-22 08:23:43 +02:00
);
}
static function upload_max_file_size_dash () {
2025-06-09 09:58:01 +02:00
$active_tab = isset ( $_GET [ 'tab' ]) ? sanitize_text_field ( $_GET [ 'tab' ]) : 'general' ;
2025-12-12 13:13:07 +01:00
$tabs = array (
'general' => __ ( 'General' , 'wp-maximum-upload-file-size' ),
'system_status' => __ ( 'System Status' , 'wp-maximum-upload-file-size' )
);
if ( ! WMUFS_Helper :: is_premium_active ()) {
$tabs [ 'upload_logs' ] = __ ( 'Pro <span class="easymedia-pro-badge">PRO</span>' , 'wp-maximum-upload-file-size' );
}
$tabs = apply_filters ( 'wmufs_admin_tabs' , $tabs );
2025-06-09 09:58:01 +02:00
?>
< div class = " wmufs-wrap " >
< h2 class = " nav-tab-wrapper " >
2025-12-12 13:13:07 +01:00
< ? php foreach ( $tabs as $tab_key => $tab_label ) : ?>
< a href = " # " data - tab = " <?php echo esc_attr( $tab_key ); ?> " class = " nav-tab max-uploader-tab-link <?php echo $active_tab === $tab_key ? 'nav-tab-active' : ''; ?> " >
< ? php if ( $tab_key === 'general' ) : ?>
< span class = " dashicons dashicons-admin-generic " ></ span >
< ? php elseif ( $tab_key === 'system_status' ) : ?>
< span class = " dashicons dashicons-chart-bar " ></ span >
< ? php elseif ( $tab_key === 'upload_logs' ) : ?>
< span class = " dashicons dashicons-list-view " ></ span >
< ? php elseif ( $tab_key === 'user_limits' ) : ?>
< span class = " dashicons dashicons-groups " ></ span >
< ? php elseif ( $tab_key === 'statistics' ) : ?>
< span class = " dashicons dashicons-chart-area " ></ span >
< ? php elseif ( $tab_key === 'media_manager' ) : ?>
< span class = " dashicons dashicons-category " ></ span >
< ? php elseif ( $tab_key === 'license' ) : ?>
< span class = " dashicons dashicons-admin-network " ></ span >
< ? php endif ; ?>
< ? php echo wp_kses_post ( $tab_label ); ?>
</ a >
< ? php endforeach ; ?>
2025-06-09 09:58:01 +02:00
</ h2 >
< div id = " max-uploader-tab-content " >
< ? php include_once WMUFS_PLUGIN_PATH . 'inc/MaxUploaderSystemStatus.php' ; ?>
2025-12-12 13:13:07 +01:00
< ? php foreach ( $tabs as $tab_key => $tab_label ) : ?>
< div id = " max-uploader-tab-<?php echo esc_attr( $tab_key ); ?> " class = " max-uploader-tab-content " < ? php echo $active_tab !== $tab_key ? 'style="display:none;"' : '' ; ?> >
< ? php
if ( $tab_key === 'general' ) {
include WMUFS_PLUGIN_PATH . 'admin/templates/MaxUploaderForm.php' ;
} elseif ( $tab_key === 'system_status' ) {
include WMUFS_PLUGIN_PATH . 'admin/templates/ClassSystemHealth.php' ;
} elseif ( in_array ( $tab_key , [ 'upload_logs' , 'user_limits' , 'statistics' ]) && ! WMUFS_Helper :: is_premium_active ()) {
include WMUFS_PLUGIN_PATH . 'admin/templates/UpgradePro.php' ;
} else {
do_action ( 'wmufs_admin_tab_content' , $tab_key );
}
?>
</ div >
< ? php endforeach ; ?>
2025-06-09 09:58:01 +02:00
</ div >
</ div >
< ? php
2023-06-22 08:23:43 +02:00
add_action ( 'admin_head' , [ __CLASS__ , 'wmufs_remove_admin_action' ]);
}
2025-12-12 13:13:07 +01:00
static function wmufs_remove_admin_action () {
2023-06-22 08:23:43 +02:00
remove_all_actions ( 'user_admin_notices' );
remove_all_actions ( 'admin_notices' );
}
2025-12-12 13:13:07 +01:00
/**
* @ return void
*/
static function upload_max_increase_upload () {
// Get plugin settings
$settings = get_option ( 'wmufs_settings' , []);
// Only proceed if settings exist
if ( empty ( $settings )) {
return ;
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
// Get limit type (global or role_based)
$limit_type = isset ( $settings [ 'limit_type' ]) ? $settings [ 'limit_type' ] : 'global' ;
$max_limits = isset ( $settings [ 'max_limits' ]) ? $settings [ 'max_limits' ] : [];
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
// Apply execution time setting
$max_execution_time = ( int ) ( isset ( $settings [ 'max_execution_time' ]) ? $settings [ 'max_execution_time' ] : get_option ( 'wmufs_maximum_execution_time' ));
if ( $max_execution_time > 0 && function_exists ( 'set_time_limit' )) {
@ set_time_limit ( $max_execution_time );
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
// Apply memory limit setting
$memory_limit = ( int ) ( isset ( $settings [ 'max_memory_limit' ]) ? $settings [ 'max_memory_limit' ] : get_option ( 'wmufs_memory_limit' ));
if ( $memory_limit > 0 ) {
$memory_limit_mb = round ( $memory_limit / 1048576 );
@ ini_set ( 'memory_limit' , $memory_limit_mb . 'M' );
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
// Apply upload size limits based on a limit type
if ( $limit_type === 'global' ) {
// Global limit for all users
$global_limit = ( int ) ( isset ( $max_limits [ 'all' ]) ? $max_limits [ 'all' ] : 0 );
if ( $global_limit > 0 ) {
add_filter ( 'upload_size_limit' , function ( $size ) use ( $global_limit ) {
return $global_limit ;
});
}
} elseif ( $limit_type === 'role_based' ) {
$role_limits = $max_limits ;
add_filter ( 'upload_size_limit' , function ( $size ) use ( $role_limits ) {
if ( is_user_logged_in ()) {
$user = wp_get_current_user ();
foreach ( $user -> roles as $role ) {
if ( isset ( $role_limits [ $role ]) && $role_limits [ $role ] > 0 ) {
return ( int ) $role_limits [ $role ];
}
}
}
return $size ;
});
}
2025-06-09 09:58:01 +02:00
2025-12-12 13:13:07 +01:00
}
2023-06-22 08:23:43 +02:00
}
2025-06-09 09:58:01 +02:00
add_action ( 'init' , array ( 'MaxUploader_Admin' , 'init' ));