function ( $value ) { if ( ! is_array( $value ) ) { return []; } return array_map( 'sanitize_text_field', $value ); }, ] ); }); add_action("admin_menu", function () { // Add a submenu for settings add_submenu_page( "wordpress-toolkit-plugin", // Parent menu slug "Models", // Page title "Models", // Menu title "edit_theme_options", "toolkit-manage-models", // Menu slug [self::class, "display_settings_page"], // Callback function to display the settings page ); }); self::enable(); } public static function display_settings_page() { $plugin_models_path = HITHTO_DIR . "/models/custom"; // Path to plugin's models $theme_models_path = HITHTO_THEME_PATH . "/models/custom"; // Path to theme's models // Initialize arrays $plugin_models = []; $theme_models = []; // Check if plugin models directory exists before scanning if (file_exists($plugin_models_path) && is_dir($plugin_models_path)) { $plugin_models = scandir($plugin_models_path); // Filter out non-PHP files and the index.php directory guard $plugin_models = array_filter($plugin_models, function ($file) { return strpos($file, ".php") !== false && $file !== "index.php"; }); } // Check if theme models directory exists before scanning if (file_exists($theme_models_path) && is_dir($theme_models_path)) { $theme_models = scandir($theme_models_path); // Filter out non-PHP files and the index.php directory guard $theme_models = array_filter($theme_models, function ($file) { return strpos($file, ".php") !== false && $file !== "index.php"; }); } // Merge models from both locations $models = array_merge($plugin_models, $theme_models); // Show notice if no models found if (empty($models)) { echo "

No models found. Please create a 'models/custom' directory in your plugin or theme and add your model files there.

"; } $options = get_option("toolkit_enabled_models", []); $post_data = wp_unslash($_POST); if (isset($post_data["submit"])) { if (!current_user_can("edit_theme_options")) { wp_die( esc_html__( "You are not allowed to update model settings.", "hi-theme-toolkit", ), ); } check_admin_referer( "toolkit_model_settings_save", "toolkit_model_settings_nonce", ); // Process form submission $options = []; foreach ($models as $model) { $model_key = basename($model, ".php"); $options[$model_key] = isset($post_data[$model_key]) ? 1 : 0; } update_option("toolkit_enabled_models", $options); } // Display the settings form ?>

Model Settings


$enabled) { // Model names are PHP class names: anything else (e.g. "../") is never used to build a file path if (!is_string($model) || !preg_match('/^[A-Za-z_][A-Za-z0-9_]*$/', $model)) { continue; } if ($enabled) { // Define the file path based on the model name $file_path = HITHTO_THEME_PATH . "/models/custom/$model.php"; // Check if the file exists before including it if (file_exists($file_path)) { $class = "\\Toolkit\\models\\custom\\$model"; require_once $file_path; // Register the class if (method_exists($class, "register")) { // Register the class $class::register(); } } else { $file_path = HITHTO_DIR . "/models/custom/$model.php"; // Check if the file exists before including it if (file_exists($file_path)) { $class = "\\Toolkit\\models\\custom\\$model"; require_once $file_path; // Register the class if (method_exists($class, "register")) { // Register the class $class::register(); } } } } } } }