2023-06-22 08:23:43 +02:00
< ? php
/**
* Class Codepopular_WMUFS
*/
2025-06-09 09:58:01 +02:00
class MaxUploader_Admin {
static function init () : void {
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
add_action ( 'admin_init' , array ( __CLASS__ , 'max_uploader_form_submission' ));
add_action ( 'admin_head' , array ( __CLASS__ , 'show_admin_notice' ));
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-06-09 09:58:01 +02:00
static function max_uploader_form_submission () : void {
if (
! isset ( $_POST [ 'upload_max_file_size_nonce' ]) ||
! wp_verify_nonce ( sanitize_text_field ( $_POST [ 'upload_max_file_size_nonce' ]), 'upload_max_file_size_action' )
) {
return ;
}
$settings = [];
if ( isset ( $_POST [ 'max_file_size_field' ]) ) {
$limit = ( int ) sanitize_text_field ( $_POST [ 'max_file_size_field' ]) * 1024 * 1024 ;
$settings [ 'max_limits' ] = [
'all' => $limit ,
];
}
if ( isset ( $_POST [ 'max_execution_time_field' ]) ) {
$settings [ 'max_execution_time' ] = ( int ) sanitize_text_field ( $_POST [ 'max_execution_time_field' ]);
}
if ( isset ( $_POST [ 'max_memory_limit_field' ]) ) {
$settings [ 'max_memory_limit' ] = ( int ) sanitize_text_field ( $_POST [ 'max_memory_limit_field' ]) * 1024 * 1024 ;
}
// Save as JSON string or array. WordPress can handle arrays (auto-serialized).
update_option ( 'wmufs_settings' , $settings );
set_transient ( 'wmufs_settings_updated' , 'Settings saved successfully.' , 30 );
wp_safe_redirect ( admin_url ( 'admin.php?page=max_uploader' ));
exit ;
}
static function show_admin_notice () : void {
if ( $message = get_transient ( 'wmufs_settings_updated' ) ) {
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html ( $message ) . '</p></div>' ;
delete_transient ( 'wmufs_settings_updated' );
}
}
static function wmufs_style_and_script () : void {
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
wp_enqueue_script ( 'wmufs-admin' , WMUFS_PLUGIN_URL . 'assets/js/admin.js' , array ( 'jquery' ), time (), 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-06-09 09:58:01 +02:00
static function get_plugin_version () : string {
$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
}
static function is_plugin_page () : bool {
2023-06-22 08:23:43 +02:00
$current_screen = get_current_screen ();
2025-06-09 09:58:01 +02:00
return ( $current_screen -> id === 'media_page_max_uploader' );
}
2023-06-22 08:23:43 +02:00
static function plugin_action_links ( $links ) {
2025-06-09 09:58:01 +02:00
$settings_link = '<a href="' . admin_url ( 'admin.php?page=max_uploader' ) . '">Settings</a>' ;
2023-06-22 08:23:43 +02:00
array_unshift ( $links , $settings_link );
return $links ;
2025-06-09 09:58:01 +02:00
}
2023-06-22 08:23:43 +02:00
static function plugin_meta_links ( $links , $file ) {
2025-06-09 09:58:01 +02:00
if ( $file === plugin_basename ( __FILE__ ) ) {
$links [] = '<a target="_blank" href="https://wordpress.org/support/plugin/wp-maximum-upload-file-size/">Support</a>' ;
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
static function admin_footer_text ( $text ) {
if ( ! self :: is_plugin_page () ) {
return $text ;
}
2025-06-09 09:58:01 +02:00
return '<span id="footer-thankyou">If you like <strong><ins>WP Maximum Upload File Size</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>' ;
}
2023-06-22 08:23:43 +02:00
static function upload_max_file_size_add_pages () {
2025-06-09 09:58:01 +02:00
add_submenu_page (
'upload.php' , // Parent Slug.
'Increase Max Upload File Size' ,
'MaxUploader' ,
2023-06-22 08:23:43 +02:00
'manage_options' ,
2025-06-09 09:58:01 +02:00
'max_uploader' ,
[ __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' ;
?>
< div class = " wmufs-wrap " >
< h2 class = " nav-tab-wrapper " >
< a href = " # " data - tab = " general " class = " nav-tab max-uploader-tab-link <?php echo $active_tab === 'general' ? 'nav-tab-active' : ''; ?> " >
< span class = " dashicons dashicons-admin-generic " ></ span > General
</ a >
< a href = " # " data - tab = " system_status " class = " nav-tab max-uploader-tab-link <?php echo $active_tab === 'system_status' ? 'nav-tab-active' : ''; ?> " >
< span class = " dashicons dashicons-chart-bar " ></ span > System Status
</ a >
< a href = " # " data - tab = " pro " class = " nav-tab max-uploader-tab-link <?php echo $active_tab === 'pro' ? 'nav-tab-active' : ''; ?> " >
< span class = " dashicons dashicons-star-filled " ></ span > Pro
</ a >
</ h2 >
< div id = " max-uploader-tab-content " >
< ? php include_once WMUFS_PLUGIN_PATH . 'inc/MaxUploaderSystemStatus.php' ; ?>
< div id = " max-uploader-tab-general " class = " max-uploader-tab-content " < ? php echo $active_tab !== 'general' ? 'style="display:none;"' : '' ; ?> >
< ? php
include WMUFS_PLUGIN_PATH . 'admin/templates/MaxUploaderForm.php' ;
?>
</ div >
< div id = " max-uploader-tab-system_status " class = " max-uploader-tab-content " < ? php echo $active_tab !== 'system_status' ? 'style="display:none;"' : '' ; ?> >
< ? php
include WMUFS_PLUGIN_PATH . 'admin/templates/ClassSystemHealth.php' ;
?>
</ div >
< div id = " max-uploader-tab-pro " class = " max-uploader-tab-content " < ? php echo $active_tab !== 'pro' ? 'style="display:none;"' : '' ; ?> >
< ? php
include WMUFS_PLUGIN_PATH . 'admin/templates/FreeVsPro.php' ;
?>
</ div >
</ div >
</ div >
< ? php
2023-06-22 08:23:43 +02:00
add_action ( 'admin_head' , [ __CLASS__ , 'wmufs_remove_admin_action' ]);
}
2025-06-09 09:58:01 +02:00
static function wmufs_remove_admin_action () : void {
2023-06-22 08:23:43 +02:00
remove_all_actions ( 'user_admin_notices' );
remove_all_actions ( 'admin_notices' );
}
2025-06-09 09:58:01 +02:00
/**
* @ return void
*/
static function upload_max_increase_upload () : void {
2023-06-22 08:23:43 +02:00
2025-06-09 09:58:01 +02:00
$settings = get_option ( 'wmufs_settings' ) ? ? [];
$max_upload_size = ( int ) ( $settings [ 'max_limits' ][ 'all' ] ? ? get_option ( 'max_file_size' )); // bytes
$max_execution_time = ( int ) ( $settings [ 'max_execution_time' ] ? ? get_option ( 'wmufs_maximum_execution_time' ));
$memory_limit = ( int ) ( $settings [ 'max_memory_limit' ] ? ? get_option ( 'wmufs_memory_limit' ));
// Set max upload size
add_filter ( 'upload_size_limit' , function ( $data ) use ( $max_upload_size ) {
return $max_upload_size > 0 ? $max_upload_size : $data ;
});
// Set max execution time
if ( ! empty ( $max_execution_time ) && $max_execution_time > 0 ) {
// Only try to set a time limit if the function is available
if ( function_exists ( 'set_time_limit' )) {
@ set_time_limit ( $max_execution_time ); // Suppress errors if the host restricts
}
}
// Set a memory limit
if ( ! empty ( $memory_limit ) && $memory_limit > 0 ) {
$memory_limit_mb = round ( $memory_limit / 1048576 ); // convert to MB ex: 2048
@ ini_set ( 'memory_limit' , (( int ) $memory_limit_mb ) . 'M' ); // e.g., 512M, 1024M
}
}
2023-06-22 08:23:43 +02:00
}
2025-06-09 09:58:01 +02:00
add_action ( 'init' , array ( 'MaxUploader_Admin' , 'init' ));
?>