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']); } // 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() { // 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 simple 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'); echo ' ' . esc_html($title) . '
🚧

' . esc_html($title) . '

' . 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 = addslashes($meta_info->meta_value); $sql_query_sel[] = "SELECT $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; } /** * 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();