hooks(); } return self::$instance; } /** * Register hooks. */ private function hooks(): void { // The register_settings_ui method is called by the ModuleManager. add_action( 'wp_ajax_' . self::REBUILD_AJAX_ACTION, [ $this, 'handle_rebuild_request' ] ); add_filter( 'adpresso_adblocker_tab_sections', [ $this, 'add_section' ] ); add_filter( 'adpresso_adblocker_tab_options', [ $this, 'add_options' ] ); add_action( 'admin_enqueue_scripts', function( $hook_suffix ) { $tab = Params::get()->getKey( 'tab' ); if ( $tab !== 'adblocker' ) { return; } wp_add_inline_script( 'adpresso-admin-main', $this->inline_js ); } ); } /** * Adds a section to the Adblocker tab. * * @param $sections * * @return array */ public function add_section( $sections ): array { $sections['adblocker-asset-masking'] = [ 'title' => __( 'Asset Masking', 'adpresso' ), 'description' => '', 'controlled_by' => '', 'initially_open' => true, ]; return $sections; } /** * Adds options to the Adblocker tab. * * @param $options * * @return array */ public function add_options( $options ): array { $options[ 'enable_module_' . $this->get_id() ] = [ 'type' => 'toggle', 'label' => __( 'Enable Asset Masking', 'adpresso' ), 'description' => __( 'Hide your ads from ad blockers by serving plugin assets (CSS, JS) from a randomized, disguised folder within your uploads directory.', 'adpresso' ), 'default' => 0, 'section' => 'adblocker-asset-masking', 'highlighted' => true, ]; $options['asset-masking-status'] = [ 'type' => 'html-content', 'label' => __( 'Status', 'adpresso' ), 'description' => __( 'Asset Masking is not yet configured. Please rebuild the asset folder to complete the setup.', 'adpresso' ), 'html' => $this->render_status_field(), 'section' => 'adblocker-asset-masking', 'depends_on' => [ [ 'field_id' => 'enable_module_' . $this->get_id(), 'operator' => 'is', 'value' => 1, ], ], ]; $options['asset-masking-rebuild'] = [ 'type' => 'html-content', 'label' => __( 'Actions', 'adpresso' ), 'description' => __( 'Rebuild Asset Folder', 'adpresso' ), 'html' => $this->render_rebuild_button(), 'section' => 'adblocker-asset-masking', 'depends_on' => [ [ 'field_id' => 'enable_module_' . $this->get_id(), 'operator' => 'is', 'value' => 1, ], ], ]; return $options; } /** * Renders the status information for the Asset Masking feature. */ public function render_status_field(): string { $config = Settings::get_direct_option( self::CONFIG_OPTION_NAME, [] ); $folder_name = $config['folder_name'] ?? ''; $last_generated = $config['last_generated'] ?? 0; $output = ''; if ( ! $folder_name || ! $last_generated ) { $output .= '

' . esc_html__( 'Asset Masking is not yet configured. Please rebuild the asset folder.', 'adpresso' ) . '

'; return $output; } $upload_dir = wp_upload_dir(); $output .= '

' . esc_html__( 'Current Asset Path', 'adpresso' ) . '

'; $output .= '' . esc_html( trailingslashit( $upload_dir['baseurl'] ) . $folder_name ) . ''; $output .= '

' . sprintf( // translators: %s is a human-readable date. esc_html__( 'Last rebuilt on: %s', 'adpresso' ), esc_html( wp_date( get_option( 'date_format' ) . ' H:i:s', $last_generated ) ) ) . '

'; return $output; } /** * Renders the "Rebuild" button and the necessary JS for the AJAX call. */ public function render_rebuild_button(): string { ob_start(); ?>

inline_js = " document.addEventListener( 'DOMContentLoaded', function () { const button = document.getElementById( 'adpresso-rebuild-assets-btn' ); const spinner = button.nextElementSibling; const feedback = document.getElementById( 'adpresso-rebuild-assets-feedback' ); console.log('hier'); button.addEventListener( 'click', function () { button.disabled = true; spinner.style.visibility = 'visible'; feedback.textContent = '" . esc_js( __( 'Rebuilding...', 'adpresso' ) ) . "'; feedback.style.color = ''; const data = new URLSearchParams(); data.append( 'action', '" . esc_js( self::REBUILD_AJAX_ACTION ) . "' ); data.append( '_ajax_nonce', '" . esc_js( wp_create_nonce( self::REBUILD_AJAX_ACTION ) ) . "' ); fetch( ajaxurl, { method: 'POST', body: data } ) .then( response => response.json() ) .then( result => { if ( result.success ) { feedback.textContent = result.data.message; feedback.style.color = '#46b450'; // Reload the page to show the new status. setTimeout( () => window.location.reload(), 1500 ); } else { feedback.textContent = '" . esc_js( __( 'Error:', 'adpresso' ) ) . " ' + result.data.message; feedback.style.color = '#d63638'; } } ) .catch( error => { feedback.textContent = '" . esc_js( __( 'An unexpected error occurred.', 'adpresso' ) ) . "'; feedback.style.color = '#d63638'; console.error( 'Asset Rebuild Error:', error ); } ) .finally( () => { button.disabled = false; spinner.style.visibility = 'hidden'; } ); } ); } ); "; return ob_get_clean(); } /** * Handles the AJAX request to rebuild the disguised assets. */ public function handle_rebuild_request(): void { // Security checks check_ajax_referer( self::REBUILD_AJAX_ACTION ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( [ 'message' => __( 'Permission denied.', 'adpresso' ) ], 403 ); } $result = $this->rebuild_assets(); if ( is_wp_error( $result ) ) { wp_send_json_error( [ 'message' => $result->get_error_message() ], 500 ); } wp_send_json_success( [ 'message' => __( 'Asset folder rebuilt successfully!', 'adpresso' ) ] ); } /** * Rebuilds the disguised assets. * * @return bool|\WP_Error True on success, WP_Error on failure. */ public function rebuild_assets() { $upload_dir = wp_upload_dir(); if ( ! empty( $upload_dir['error'] ) ) { return new \WP_Error( 'upload_dir_error', $upload_dir['error'] ); } $config = Settings::get_direct_option( self::CONFIG_OPTION_NAME, [] ); $old_folder = $config['folder_name'] ?? null; // Initialize WP Filesystem global $wp_filesystem; if ( ! $wp_filesystem ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } // 1. Delete the old directory if it exists if ( $old_folder && $wp_filesystem->is_dir( trailingslashit( $upload_dir['basedir'] ) ) . $old_folder ) { $wp_filesystem->rmdir( trailingslashit( $upload_dir['basedir'] ) . $old_folder, true ); } // 2. Create a new random directory $new_folder_name = 'adpr_assets_' . wp_generate_password( 8, false ); $new_folder_path = trailingslashit( $upload_dir['basedir'] ) . $new_folder_name; if ( ! $wp_filesystem->mkdir( $new_folder_path, FS_CHMOD_DIR ) ) { return new \WP_Error( 'mkdir_error', __( 'Could not create new asset directory.', 'adpresso' ) ); } $wp_filesystem->put_contents( trailingslashit( $new_folder_path ) . 'index.php', 'globals[ Assets::class ] ?? null; if ( ! $assets_service ) { $assets_service = new Assets(); $assets_service->hooks(); } $assets_to_mask = $assets_service->get_disguisable_assets(); foreach ( $assets_to_mask as $asset_data ) { $original_path = $asset_data['path'] ?? null; if ( ! $original_path || ! $wp_filesystem->is_file( $original_path ) ) { continue; } // Generate new filename based on content hash $file_hash = substr( md5_file( $original_path ), 0, 10 ); $extension = pathinfo( $original_path, PATHINFO_EXTENSION ); $new_filename = $file_hash . '.' . $extension; // Copy the file $new_file_path = trailingslashit( $new_folder_path ) . $new_filename; if ( ! $wp_filesystem->copy( $original_path, $new_file_path, true, FS_CHMOD_FILE ) ) { RuntimeDebugger::add_server_error_log_entry( 'Asset Disguise: Failed to copy ' . $original_path ); continue; } // Map the original URL path to the new URL path $normalized_original = wp_normalize_path( $original_path ); $plugin_path = wp_normalize_path( plugin_dir_path( ADPRESSO_FILE ) ); $original_url_path = ltrim( str_replace( $plugin_path, '', $normalized_original ), '/' ); // Prefix with the plugin folder path as seen in URLs (e.g. wp-content/plugins/adpresso/) $plugin_url_path = wp_normalize_path( ltrim( wp_parse_url( plugin_dir_url( ADPRESSO_FILE ), PHP_URL_PATH ), '/' ) ); $original_url_path = $plugin_url_path . $original_url_path; $lookup_table[ $original_url_path ] = $new_filename; } // 4. Save the new configuration $new_config = [ 'folder_name' => $new_folder_name, 'lookup_table' => $lookup_table, 'last_generated' => time(), ]; update_option( \AdPresso\Constants::OPTION_PREFIX . self::CONFIG_OPTION_NAME, $new_config, true ); return true; } }