options = get_option('aio_options', []);
// 1. Maintenance Mode
if ( !empty($this->options['optEnableMaintenance']) ) {
add_action('template_redirect', [$this, 'handle_maintenance_mode'], 0);
}
// 2. Duplicate Post / Page
if ( !empty($this->options['optEnableDuplicatePost']) ) {
add_filter('post_row_actions', [$this, 'add_duplicate_post_link'], 10, 2);
add_filter('page_row_actions', [$this, 'add_duplicate_post_link'], 10, 2);
add_action('admin_action_aio_duplicate_post_as_draft', [$this, 'duplicate_post_as_draft']);
}
// 2.5 Duplicate Menu
if ( !empty($this->options['optEnableDuplicateMenu']) ) {
add_action('admin_footer-nav-menus.php', [$this, 'add_duplicate_menu_button']);
add_action('admin_init', [$this, 'handle_duplicate_menu']);
}
// 3. 404 Logging & Redirection
if ( !empty($this->options['optEnableRedirection']) ) {
add_action('template_redirect', [$this, 'handle_404_and_redirects'], 1);
}
}
/**
* Maintenance Mode Logic
*/
public function handle_maintenance_mode() {
// Bypass Logic
$bypass_token = $this->options['optMaintenanceBypass'] ?? '';
if (!empty($bypass_token)) {
if (isset($_GET['aio_bypass']) && $_GET['aio_bypass'] === $bypass_token) {
setcookie('aio_maintenance_bypass', $bypass_token, time() + (86400 * 30), '/');
wp_safe_redirect(remove_query_arg('aio_bypass'));
exit;
}
if (isset($_COOKIE['aio_maintenance_bypass']) && $_COOKIE['aio_maintenance_bypass'] === $bypass_token) {
return; // Allow access
}
}
// Do not block admin users or login pages
if ( is_user_logged_in() && current_user_can('manage_options') ) {
return;
}
// Do not block wp-login.php
if ( in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) {
return;
}
// Return 503 Service Unavailable
header('HTTP/1.1 503 Service Temporarily Unavailable');
header('Status: 503 Service Temporarily Unavailable');
header('Retry-After: 3600');
// Render maintenance template
$title = $this->options['txtMaintenanceTitle'] ?? __('Site Under Maintenance', 'aio-tools');
$message = $this->options['txtMaintenanceMessage'] ?? __('We are currently updating our website. Please check back later.', 'aio-tools');
$bg_image = $this->options['optMaintenanceBgImage'] ?? '';
$bg_color = $this->options['optMaintenanceColor'] ?? '#3b82f6';
$body_style = '';
$overlay = '';
$text_color = '#334155';
$bg_card = '#ffffff';
if (!empty($bg_image)) {
$body_style = "background: url('" . esc_url($bg_image) . "') no-repeat center center fixed; background-size: cover;";
$overlay = '
';
$text_color = '#ffffff';
$bg_card = 'rgba(255,255,255,0.1)';
} else {
$body_style = "background-color: #f8fafc;";
}
echo '
' . esc_html($title) . '
' . $overlay . '
🚧
' . esc_html($title) . '
' . nl2br(esc_html($message)) . '
';
exit;
}
/**
* Add Duplicate Link in Admin
*/
public function add_duplicate_post_link($actions, $post) {
if (current_user_can('edit_posts')) {
$nonce = wp_create_nonce('aio_duplicate_post_' . $post->ID);
$duplicate_url = admin_url("admin.php?action=aio_duplicate_post_as_draft&post={$post->ID}&_wpnonce={$nonce}");
$actions['duplicate'] = '' . esc_html__('Duplicate', 'aio-tools') . '';
}
return $actions;
}
public function duplicate_post_as_draft() {
if (!isset($_GET['post']) || !isset($_GET['_wpnonce'])) {
wp_die(esc_html__('No post to duplicate has been supplied!', 'aio-tools'));
}
$post_id = intval($_GET['post']);
if (!wp_verify_nonce($_GET['_wpnonce'], 'aio_duplicate_post_' . $post_id)) {
wp_die(esc_html__('Security check failed', 'aio-tools'));
}
$post = get_post($post_id);
if (!$post) wp_die(esc_html__('Post creation failed, could not find original post:', 'aio-tools') . ' ' . esc_html($post_id));
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name . '-copy',
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title . ' (' . __('Copy', 'aio-tools') . ')',
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
$new_post_id = wp_insert_post($args);
// Copy taxonomies
$taxonomies = get_object_taxonomies($post->post_type);
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
// Copy meta
global $wpdb;
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos) != 0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
if ($meta_key == '_wp_old_slug') continue;
$meta_value = $meta_info->meta_value;
$sql_query_sel[] = $wpdb->prepare("SELECT %d, %s, %s", $new_post_id, $meta_key, $meta_value);
}
$sql_query .= implode(" UNION ALL ", $sql_query_sel);
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
$wpdb->query($sql_query);
}
wp_safe_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
exit;
}
/**
* Add Duplicate button to nav-menus.php
*/
public function add_duplicate_menu_button() {
if (!isset($_GET['menu'])) return;
$menu_id = intval($_GET['menu']);
if ($menu_id <= 0) return;
$nonce = wp_create_nonce('aio_duplicate_menu_' . $menu_id);
$url = admin_url("nav-menus.php?action=aio_duplicate_menu&menu={$menu_id}&_wpnonce={$nonce}");
?>
name . ' (' . __('Copy', 'aio-tools') . ' ' . time() . ')';
$new_menu_id = wp_create_nav_menu($new_menu_name);
if (is_wp_error($new_menu_id)) {
wp_die($new_menu_id->get_error_message());
}
$menu_items = wp_get_nav_menu_items($menu_id);
if (!empty($menu_items)) {
$item_id_map = []; // Map old ID to new ID
// First pass
foreach ($menu_items as $item) {
$args = array(
'menu-item-object-id' => $item->object_id,
'menu-item-object' => $item->object,
'menu-item-parent-id' => isset($item_id_map[$item->menu_item_parent]) ? $item_id_map[$item->menu_item_parent] : 0,
'menu-item-position' => $item->menu_order,
'menu-item-type' => $item->type,
'menu-item-title' => $item->title,
'menu-item-url' => $item->url,
'menu-item-description' => $item->description,
'menu-item-attr-title' => $item->attr_title,
'menu-item-target' => $item->target,
'menu-item-classes' => implode(' ', $item->classes),
'menu-item-xfn' => $item->xfn,
'menu-item-status' => $item->post_status
);
$new_item_id = wp_update_nav_menu_item($new_menu_id, 0, $args);
if (!is_wp_error($new_item_id)) {
$item_id_map[$item->ID] = $new_item_id;
}
}
// Second pass for hierarchy
foreach ($menu_items as $item) {
if ($item->menu_item_parent && isset($item_id_map[$item->menu_item_parent]) && isset($item_id_map[$item->ID])) {
update_post_meta($item_id_map[$item->ID], '_menu_item_menu_item_parent', $item_id_map[$item->menu_item_parent]);
}
}
}
wp_safe_redirect(admin_url("nav-menus.php?menu={$new_menu_id}"));
exit;
}
/**
* 404 Logging & Redirection Logic
*/
public function handle_404_and_redirects() {
global $wp;
$current_url = home_url(add_query_arg(array(), $wp->request));
$path = '/' . ltrim($wp->request, '/');
// Check if there is a redirect rule for this path
$redirects = get_option('aio_redirect_rules', []);
if (isset($redirects[$path])) {
wp_safe_redirect(home_url($redirects[$path]), 301);
exit;
}
// Log 404
if (is_404()) {
global $wpdb;
$table_name = $wpdb->prefix . 'aio_404_logs';
$path_safe = substr($path, 0, 255);
$existing = $wpdb->get_row($wpdb->prepare("SELECT id, hits FROM $table_name WHERE url = %s", $path_safe));
if ($existing) {
$wpdb->update(
$table_name,
[
'hits' => $existing->hits + 1,
'last_hit' => current_time('mysql')
],
['id' => $existing->id],
['%d', '%s'],
['%d']
);
} else {
$wpdb->insert(
$table_name,
[
'url' => $path_safe,
'hits' => 1,
'last_hit' => current_time('mysql')
],
['%s', '%d', '%s']
);
}
}
}
}
// Init Module
new AIO_Utility();