getFileName(); $startLine = $reflector->getStartLine(); $endLine = $reflector->getEndLine(); $code = file_get_contents($filename); $tokens = token_get_all($code); $functionCode = ""; $insideFunction = false; $bracesCount = null; $currentLine = 1; foreach ($tokens as $token) { if (is_array($token)) { $currentLine = $token[2]; // Zeilennummer aus dem Token // Funktion erst ab der Start-Zeile suchen if ($currentLine < $startLine) { continue; } // Start der gesuchten Funktion finden if ($token[0] === T_FUNCTION) { $insideFunction = true; } } if ($insideFunction) { $functionCode .= is_array($token) ? $token[1] : $token; // Geschweifte Klammern zählen if ($token === '{') $bracesCount = ($bracesCount === null) ? 1 : $bracesCount + 1; if ($token === '}') $bracesCount--; // Ende der Funktion erreicht if ($bracesCount === 0 && $insideFunction) { break; } } // Falls wir über das Ende der Funktion hinaus sind, abbrechen if ($currentLine > $endLine) { break; } } return trim($functionCode); } catch (ReflectionException $e) { return false; } } /** * Generate the cache file with active features * * @return bool Whether the file was successfully generated */ public function generate_cache_file() { $settings = get_option('advanced_settings_settings', []); // Get all features that should be executed $active_features = []; foreach ($settings as $feature_id => $feature_settings) { $feature = advset_get_feature($feature_id); if ($feature && isset($feature['execution_handler'])) { if (!empty($feature_settings) && (!isset($feature['execution_condition']) || call_user_func($feature['execution_condition'], $feature_settings) === true)) { $active_features[$feature_id] = [ 'handler' => $feature['execution_handler'], 'settings' => $feature_settings ]; } } } // Start building the cache file content $content = " $feature) { $content .= "\n\n\n"; $content .= "/**\n"; $content .= " * FEATURE: " . $feature_id . "\n"; $content .= " */\n"; // Get the function source $handler = $feature['handler']; if (is_string($handler)) { $content .= "" . $handler . "();\n"; } else { $source = $this->get_function_source($handler); if ($source) { // Add the function with settings $content .= "call_user_func(" . preg_replace('/\n /', "\n", $source) . ", " . preg_replace('/\n( +)?/', "\n$1$1", var_export($feature['settings'], true)) . ");\n"; } } } $content .= "\n\n\n"; $content .= "return true;\n"; // Try to write the file if (!file_exists(dirname(ADVSET_CACHE_FILE))) { mkdir(dirname(ADVSET_CACHE_FILE), 0755, true); } $result = file_put_contents(ADVSET_CACHE_FILE, $content); return $result !== false; } /** * Execute active features directly (fallback) */ public function execute_active_features_fallback() { $settings = get_option('advanced_settings_settings', []); foreach ($settings as $feature_id => $feature_settings) { $feature = advset_get_feature($feature_id); if ($feature && isset($feature['execution_handler'])) { if (!empty($feature_settings) && (!isset($feature['execution_condition']) || call_user_func($feature['execution_condition'], $feature_settings) === true)) { call_user_func($feature['execution_handler'], $feature_settings); } } } } /** * Clean up cache on plugin deactivation */ public function cleanup_cache() { if (file_exists(ADVSET_CACHE_FILE)) { unlink(ADVSET_CACHE_FILE); } // Try to remove cache directory if empty $cache_dir = dirname(ADVSET_CACHE_FILE); if (file_exists($cache_dir) && basename($cache_dir) === 'advanced-settings') { @rmdir($cache_dir); } } }