diff --git a/wp-multi-kategorie.php b/wp-multi-kategorie.php index 022affc..e9ea9ae 100644 --- a/wp-multi-kategorie.php +++ b/wp-multi-kategorie.php @@ -3,7 +3,7 @@ * Plugin Name: WP Multi Kategorie * Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Multi-Kategorie * Description: Blende einzelne Kategorien aus verschiedenen Bereichen deiner Website (Startseite, Feeds, Archive, Suche) aus. Teil der WP Multi-Reihe. - * Version: 1.1 + * Version: 1.3 * Author: M_Viper * Author URI: https://m-viper.de * Requires at least: 6.7.2 @@ -14,10 +14,558 @@ defined('ABSPATH') or die('Kein direkter Zugriff erlaubt.'); + + +/* + * Passwortschutz für Kategorie + */ +class Password_Protected_Category { + private $option_name = 'ppc_settings'; + + public function __construct() { + // Admin-Menü hinzufügen + add_action('admin_menu', [$this, 'add_admin_menu']); + // Admin-Skripte und -Styles laden + add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']); + // Frontend-Skripte und -Styles laden + add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_scripts']); + // Einstellungen speichern + add_action('admin_init', [$this, 'register_settings']); + // Kategoriezugriff prüfen + add_action('template_redirect', [$this, 'restrict_category_access']); + // Suche manipulieren + add_filter('pre_get_posts', [$this, 'exclude_protected_category_from_search']); + // AJAX-Handler für Nachricht + add_action('wp_ajax_ppc_send_request', [$this, 'handle_access_request']); + add_action('wp_ajax_nopriv_ppc_send_request', [$this, 'handle_access_request']); + add_action('wp_ajax_ppc_check_password', [$this, 'check_password']); + add_action('wp_ajax_nopriv_ppc_check_password', [$this, 'check_password']); + } + // Admin-Menü als Submenü unter Beiträge + public function add_admin_menu() { + add_submenu_page( + 'edit.php', + 'Password Protected Category', + 'Protected Categories', + 'manage_options', + 'password-protected-category', + [$this, 'settings_page'] + ); + } + // Einstellungen registrieren + public function register_settings() { + register_setting('ppc_settings_group', $this->option_name, [ + 'sanitize_callback' => [$this, 'sanitize_settings'] + ]); + } + // Einstellungen validieren + public function sanitize_settings($input) { + $new_input = []; + $new_input['protected_categories'] = []; + $new_input['admin_email'] = isset($input['admin_email']) ? sanitize_email($input['admin_email']) : get_option('admin_email'); + if (isset($input['protected_categories'])) { + foreach ($input['protected_categories'] as $cat_id => $settings) { + $cat_id = absint($cat_id); + if ($cat_id && isset($settings['enabled']) && $settings['enabled'] === '1') { + $new_input['protected_categories'][$cat_id] = [ + 'enabled' => '1', + 'password' => isset($settings['password']) ? sanitize_text_field($settings['password']) : '', + 'popup_image' => isset($settings['popup_image']) ? esc_url_raw($settings['popup_image']) : '' + ]; + error_log('Sanitized Password for Cat ' . $cat_id . ': ' . $new_input['protected_categories'][$cat_id]['password']); // Debugging + } + } + } + return $new_input; + } + // Admin-Skripte laden + public function enqueue_admin_scripts($hook) { + if ($hook !== 'posts_page_password-protected-category') { + return; + } + wp_enqueue_media(); + // Inline CSS für Admin-Grid + wp_add_inline_style('admin-menu', ' + .ppc-categories-grid { + display: grid; + grid-template-columns: repeat(3, 1fr); + gap: 20px; + } + .ppc-category-card { + background: #fff; + padding: 15px; + border: 1px solid #ddd; + border-radius: 8px; + box-shadow: 0 2px 5px rgba(0,0,0,0.1); + } + .ppc-category-card label { + display: block; + margin-bottom: 10px; + font-weight: bold; + } + .ppc-category-card input[type="text"], + .ppc-category-card button { + width: 100%; + margin-bottom: 10px; + } + .ppc-category-card button { + margin-top: 5px; + } + @media (max-width: 960px) { + .ppc-categories-grid { + grid-template-columns: repeat(2, 1fr); + } + } + @media (max-width: 600px) { + .ppc-categories-grid { + grid-template-columns: 1fr; + } + } + '); + } + // Frontend-Skripte und -Styles laden + public function enqueue_frontend_scripts() { + $options = get_option($this->option_name); + $protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : []; + if (!empty($protected_categories) && (is_category($protected_categories) || (is_single() && has_category($protected_categories)))) { + wp_enqueue_script('jquery-cookie', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js', ['jquery'], '1.4.1', true); + wp_localize_script('jquery', 'ppcSettings', [ + 'ajax_url' => admin_url('admin-ajax.php'), + 'category_id' => $this->get_current_protected_category(), + 'popup_image' => $this->get_popup_image(), + 'nonce' => wp_create_nonce('ppc_nonce'), + 'home_url' => home_url() + ]); + } + } + // Aktuelle geschützte Kategorie ermitteln + private function get_current_protected_category() { + $options = get_option($this->option_name); + $protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : []; + if (is_category()) { + $current_cat = get_queried_object_id(); + if (in_array($current_cat, $protected_categories)) { + return $current_cat; + } + } elseif (is_single()) { + $categories = wp_get_post_categories(get_the_ID()); + foreach ($categories as $cat_id) { + if (in_array($cat_id, $protected_categories)) { + return $cat_id; + } + } + } + return 0; + } + // Popup-Bild für aktuelle Kategorie holen + private function get_popup_image() { + $options = get_option($this->option_name); + $cat_id = $this->get_current_protected_category(); + return isset($options['protected_categories'][$cat_id]['popup_image']) ? $options['protected_categories'][$cat_id]['popup_image'] : ''; + } + // Kategoriezugriff einschränken + public function restrict_category_access() { + $options = get_option($this->option_name); + $protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : []; + if (!empty($protected_categories) && (is_category($protected_categories) || (is_single() && has_category($protected_categories)))) { + $current_cat = $this->get_current_protected_category(); + if ($current_cat && (!isset($_COOKIE['ppc_access_granted_' . $current_cat]) || $_COOKIE['ppc_access_granted_' . $current_cat] !== '1')) { + if (is_single()) { + wp_redirect(home_url()); + exit; + } + } + } + } + // Geschützte Kategorien aus Suche ausschließen + public function exclude_protected_category_from_search($query) { + if (!is_admin() && $query->is_main_query() && $query->is_search()) { + $options = get_option($this->option_name); + $protected_categories = isset($options['protected_categories']) ? array_keys($options['protected_categories']) : []; + if (!empty($protected_categories)) { + $query->set('category__not_in', $protected_categories); + } + } + return $query; + } + // Einstellungsseite rendern + public function settings_page() { + $options = get_option($this->option_name); + $categories = get_categories(['hide_empty' => false]); + ?> +