Compare commits

..

1 Commits
1.2 ... main

Author SHA1 Message Date
20dc1c9524 wp-multi-kategorie.php aktualisiert 2025-04-15 19:52:58 +00:00

View File

@ -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]);
?>
<div class="wrap">
<h1>Password Protected Categories</h1>
<form method="post" action="options.php">
<?php settings_fields('ppc_settings_group'); ?>
<table class="form-table">
<tr>
<th>
<label for="ppc_admin_email">Admin-E-Mail für Anfragen</label>
</th>
<td>
<input type="email" id="ppc_admin_email" name="<?php echo $this->option_name; ?>[admin_email]" value="<?php echo isset($options['admin_email']) ? esc_attr($options['admin_email']) : esc_attr(get_option('admin_email')); ?>" class="regular-text"/>
<p class="description">E-Mail-Adresse, an die Zugangsanfragen gesendet werden.</p>
</td>
</tr>
</table>
<h2>Geschützte Kategorien</h2>
<div class="ppc-categories-grid">
<?php foreach ($categories as $category) : ?>
<div class="ppc-category-card">
<label>
<input type="checkbox" name="<?php echo $this->option_name; ?>[protected_categories][<?php echo $category->term_id; ?>][enabled]" value="1" <?php checked(isset($options['protected_categories'][$category->term_id]['enabled']) && $options['protected_categories'][$category->term_id]['enabled'] === '1'); ?>>
<?php echo esc_html($category->name); ?>
</label>
<p>
<label>Passwort</label>
<input type="text" name="<?php echo $this->option_name; ?>[protected_categories][<?php echo $category->term_id; ?>][password]" value="<?php echo isset($options['protected_categories'][$category->term_id]['password']) ? esc_attr($options['protected_categories'][$category->term_id]['password']) : ''; ?>" class="regular-text"/>
</p>
<p>
<label>Popup-Bild-URL</label>
<input type="text" name="<?php echo $this->option_name; ?>[protected_categories][<?php echo $category->term_id; ?>][popup_image]" value="<?php echo isset($options['protected_categories'][$category->term_id]['popup_image']) ? esc_attr($options['protected_categories'][$category->term_id]['popup_image']) : ''; ?>" class="regular-text ppc-popup-image"/>
<button type="button" class="button ppc-upload-image">Bild auswählen</button>
</p>
</div>
<?php endforeach; ?>
</div>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Zugangsanfrage verarbeiten
public function handle_access_request() {
check_ajax_referer('ppc_nonce', 'nonce');
$options = get_option($this->option_name);
$name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
$email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
$message = isset($_POST['message']) ? sanitize_textarea_field($_POST['message']) : '';
$category_id = isset($_POST['category_id']) ? absint($_POST['category_id']) : 0;
if (empty($name) || empty($email) || empty($message)) {
wp_send_json_error(['message' => 'Bitte füllen Sie alle Felder aus.']);
}
$admin_email = !empty($options['admin_email']) ? $options['admin_email'] : get_option('admin_email');
$category = get_term($category_id, 'category');
$category_name = $category ? $category->name : 'Unbekannte Kategorie';
$subject = 'Zugangsanfrage für geschützte Kategorie: ' . $category_name;
$body = "Name: $name\n";
$body .= "E-Mail: $email\n";
$body .= "Kategorie: $category_name\n";
$body .= "Nachricht:\n$message";
$headers = ['From: ' . $name . ' <' . $email . '>'];
$sent = wp_mail($admin_email, $subject, $body, $headers);
if ($sent) {
wp_send_json_success(['message' => 'Ihre Anfrage wurde gesendet.']);
} else {
wp_send_json_error(['message' => 'Fehler beim Senden der Anfrage.']);
}
}
public function check_password() {
check_ajax_referer('ppc_nonce', 'nonce');
$options = get_option('ppc_settings');
$input_password = isset($_POST['password']) ? sanitize_text_field($_POST['password']) : '';
$category_id = isset($_POST['category_id']) ? absint($_POST['category_id']) : 0;
if ($category_id && isset($options['protected_categories'][$category_id])) {
$stored_password = $options['protected_categories'][$category_id]['password'];
error_log('Input Password: ' . $input_password);
error_log('Stored Password: ' . $stored_password);
if ($input_password === $stored_password) {
// Cookie setzen
setcookie('ppc_access_granted_' . $category_id, '1', time() + 2 * 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true);
wp_send_json_success(['message' => 'Passwort korrekt']);
} else {
wp_send_json_error(['message' => 'Falsches Passwort']);
}
} else {
wp_send_json_error(['message' => 'Ungültige Kategorie oder Passwort nicht gesetzt']);
}
wp_die();
}
}
// Inline CSS für Frontend
add_action('wp_head', function () {
$options = get_option('ppc_settings');
$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)))) {
?>
<style id="ppc-frontend-style">
/* Unscharfer Hintergrund */
body.ppc-blurred > *:not(.ppc-overlay) {
filter: blur(10px);
pointer-events: none;
}
.ppc-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 9999;
display: none;
overflow: auto;
}
.ppc-popup-container {
min-height: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
box-sizing: border-box;
}
.ppc-popup {
background: #fff;
padding: 2rem;
border-radius: 12px;
max-width: 400px;
width: 100%;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
text-align: center;
animation: ppcFadeIn 0.3s ease-out;
}
@keyframes ppcFadeIn {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
.ppc-popup img {
max-width: 100%;
height: auto;
margin-bottom: 1rem;
border-radius: 8px;
}
.ppc-popup input[type="password"],
.ppc-popup input[type="text"],
.ppc-popup input[type="email"],
.ppc-popup textarea {
width: 100%;
padding: 0.75rem;
margin-bottom: 1rem;
border: 1px solid #ddd;
border-radius: 6px;
font-size: 1rem;
transition: border-color 0.3s;
}
.ppc-popup input:focus,
.ppc-popup textarea:focus {
border-color: #007bff;
outline: none;
}
.ppc-popup textarea {
height: 100px;
resize: vertical;
}
.ppc-popup button {
background: #007bff;
color: #fff;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s;
margin: 0.5rem;
}
.ppc-popup button:hover {
background: #0056b3;
}
.ppc-popup a.ppc-request-link {
display: block;
margin-top: 1rem;
color: #007bff;
text-decoration: none;
font-size: 0.9rem;
}
.ppc-popup a.ppc-request-link:hover {
text-decoration: underline;
}
.ppc-error,
.ppc-success {
margin-top: 0.5rem;
font-size: 0.9rem;
}
.ppc-error {
color: #dc3545;
}
.ppc-success {
color: #28a745;
}
.ppc-request-form {
display: none;
}
</style>
<?php
}
});
// Inline JS für Frontend
add_action('wp_footer', function () {
$options = get_option('ppc_settings');
$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)))) {
?>
<script id="ppc-frontend-script">
jQuery(document).ready(function ($) {
// Hintergrund unscharf machen
$('body').addClass('ppc-blurred');
// Popup HTML erstellen
const popupHtml = `
<div class="ppc-overlay">
<div class="ppc-popup-container">
<div class="ppc-popup">
${ppcSettings.popup_image ? `<img src="${ppcSettings.popup_image}" alt="Popup Image">` : ''}
<div class="ppc-password-form">
<input type="password" id="ppc-password" placeholder="Passwort eingeben">
<button id="ppc-submit">Login</button>
<a href="#" class="ppc-request-link">Kein Passwort? Zugang anfragen</a>
<div class="ppc-error" style="display: none;"></div>
</div>
<div class="ppc-request-form">
<input type="text" id="ppc-name" placeholder="Ihr Name">
<input type="email" id="ppc-email" placeholder="Ihre E-Mail">
<textarea id="ppc-message" placeholder="Ihre Nachricht"></textarea>
<button id="ppc-request-submit">Anfrage senden</button>
<button id="ppc-back">Zurück</button>
<div class="ppc-error" style="display: none;"></div>
<div class="ppc-success" style="display: none;"></div>
</div>
</div>
</div>
</div>
`;
$('body').append(popupHtml);
// Popup anzeigen
$('.ppc-overlay').fadeIn();
// Klick außerhalb des Popups
$('.ppc-overlay').on('click', function (e) {
if ($(e.target).hasClass('ppc-overlay') || $(e.target).hasClass('ppc-popup-container')) {
$('body').removeClass('ppc-blurred');
$('.ppc-overlay').fadeOut();
window.location.href = ppcSettings.home_url;
}
});
// Passwort prüfen
$('#ppc-submit').on('click', function () {
const password = $('#ppc-password').val();
console.log('Sending password:', password);
console.log('Category ID:', ppcSettings.category_id);
$.ajax({
url: ppcSettings.ajax_url,
type: 'POST',
data: {
action: 'ppc_check_password',
password: password,
category_id: ppcSettings.category_id,
nonce: ppcSettings.nonce
},
success: function (response) {
console.log('AJAX response:', response);
if (response.success) {
$('body').removeClass('ppc-blurred');
$('.ppc-overlay').fadeOut();
} else {
$('.ppc-password-form .ppc-error').text(response.data.message).show();
}
},
error: function (xhr, status, error) {
console.log('AJAX error:', error);
}
});
});
// Enter-Taste unterstützen
$('#ppc-password').on('keypress', function (e) {
if (e.which === 13) {
$('#ppc-submit').click();
}
});
// Zugangsanfrage anzeigen
$('.ppc-request-link').on('click', function (e) {
e.preventDefault();
$('.ppc-password-form').hide();
$('.ppc-request-form').show();
});
// Zurück zum Passwortformular
$('#ppc-back').on('click', function () {
$('.ppc-request-form').hide();
$('.ppc-password-form').show();
$('.ppc-error, .ppc-success').hide();
});
// Zugangsanfrage senden
$('#ppc-request-submit').on('click', function () {
const name = $('#ppc-name').val();
const email = $('#ppc-email').val();
const message = $('#ppc-message').val();
$.ajax({
url: ppcSettings.ajax_url,
type: 'POST',
data: {
action: 'ppc_send_request',
name: name,
email: email,
message: message,
category_id: ppcSettings.category_id,
nonce: ppcSettings.nonce
},
success: function (response) {
if (response.success) {
$('.ppc-request-form .ppc-success').text(response.data.message).show();
$('.ppc-request-form .ppc-error').hide();
$('#ppc-name, #ppc-email, #ppc-message').val('');
} else {
$('.ppc-request-form .ppc-error').text(response.data.message).show();
$('.ppc-request-form .ppc-success').hide();
}
}
});
});
});
</script>
<?php
}
});
// Inline JS für Admin
add_action('admin_footer', function () {
if (get_current_screen()->id !== 'posts_page_password-protected-category') {
return;
}
?>
<script id="ppc-admin-script">
jQuery(document).ready(function ($) {
$('.ppc-upload-image').click(function (e) {
e.preventDefault();
const button = $(this);
const input = button.prev('.ppc-popup-image');
const frame = wp.media({
title: 'Bild auswählen',
button: {
text: 'Bild verwenden'
},
multiple: false
});
frame.on('select', function () {
const attachment = frame.state().get('selection').first().toJSON();
input.val(attachment.url);
});
frame.open();
});
});
</script>
<?php
});
// Plugin initialisieren
new Password_Protected_Category();
/*
* WP Multi Toolkit Prüfung
*/
// Funktion zur Überprüfung des WP Multi Toolkit Plugins
function wp_multi_kategorie_check_dependency() {
if (!function_exists('is_plugin_active')) {