Compare commits

..

No commits in common. "main" and "1.2" have entirely different histories.
main ... 1.2

View File

@ -1,348 +1,325 @@
<?php <?php
/** /**
* Plugin Name: WP-Multi Search * Plugin Name: WP-Multi Search
* Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Multi-Search * Plugin URI: https://git.viper.ipv64.net/M_Viper/WP-Multi-Search
* Description: Fügt eine Suchfunktion als Shortcode, Widget und Menüeintrag hinzu.(Shortcode: [custom_search]) * Description: Fügt eine Suchfunktion als Shortcode, Widget und Menüeintrag hinzu.(Shortcode: [display_search_form])
* Version: 1.4 * Version: 1.2
* Author: M_Viper * Author: M_Viper
* Author URI: https://m-viper.de * Author URI: https://m-viper.de
* Requires at least: 6.7.2 * Requires at least: 6.7.2
* Tested up to: 6.7.2 * Tested up to: 6.7.2
* License: GPL2 * License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html * License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-multi-search * Text Domain: wp-multi-search
* Tags: search Autor-search * Tags: search Autor-search
*/ */
if (!defined('ABSPATH')) { if (!defined('ABSPATH')) {
exit; // Sicherheitsmaßnahme exit; // Sicherheitsmaßnahme
} }
// Textdomain laden // 1⃣ **Suchformular-Funktion mit integriertem CSS**
function csp_load_textdomain() { function csp_search_form() {
load_plugin_textdomain('wp-multi-search', false, dirname(plugin_basename(__FILE__)) . '/languages'); ob_start();
} $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
add_action('plugins_loaded', 'csp_load_textdomain'); ?>
<style>
// 1⃣ **Suchformular-Funktion mit integriertem CSS** .csp-search-form {
function csp_search_form() { display: flex;
ob_start(); flex-direction: column;
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; gap: 8px;
?> max-width: auto;
<style> margin: 10px auto;
.csp-search-form { padding: 15px;
display: flex; background: #f8f9fa;
flex-direction: column; border-radius: 15px;
gap: 8px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
max-width: 500px; }
margin: 10px auto;
padding: 15px; .csp-search-fields {
background: #f8f9fa; display: flex;
border-radius: 15px; gap: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); }
}
.csp-search-form input[type="text"] {
.csp-search-fields { flex: 1;
display: flex; padding: 10px;
gap: 8px; border: 1px solid #ddd;
} border-radius: 20px;
font-size: 16px;
.csp-search-form input[type="text"] { }
flex: 1;
padding: 10px; .csp-search-type {
border: 1px solid #ddd; display: flex;
border-radius: 20px; gap: 10px;
font-size: 16px; margin-bottom: 10px;
} }
.csp-search-type { .csp-search-type label {
display: flex; display: flex;
gap: 10px; align-items: center;
margin-bottom: 10px; gap: 5px;
} cursor: pointer;
}
.csp-search-type label {
display: flex; .csp-search-type input[type="radio"] {
align-items: center; margin: 0;
gap: 5px; }
cursor: pointer;
} .csp-search-form input[type="submit"] {
background: #0073aa;
.csp-search-type input[type="radio"] { color: white;
margin: 0; border: none;
} padding: 10px 15px;
border-radius: 20px;
.csp-search-form input[type="submit"] { cursor: pointer;
background: #0073aa; font-size: 16px;
color: white; transition: background 0.3s;
border: none; }
padding: 10px 15px;
border-radius: 20px; .csp-search-form input[type="submit"]:hover {
cursor: pointer; background: #005f8d;
font-size: 16px; }
transition: background 0.3s; </style>
} <form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
<div class="csp-search-type">
.csp-search-form input[type="submit"]:hover { <label>
background: #005f8d; <input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>>
} <?php _e('Alle Inhalte', 'textdomain'); ?>
</style> </label>
<form class="csp-search-form" role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>"> <label>
<div class="csp-search-type"> <input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>>
<label> <?php _e('Nur Titel', 'textdomain'); ?>
<input type="radio" name="search_type" value="all" <?php checked($search_type, 'all'); ?>> </label>
<?php _e('Alle Inhalte', 'wp-multi-search'); ?> <label>
</label> <input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>>
<label> <?php _e('Nur Gastautoren', 'textdomain'); ?>
<input type="radio" name="search_type" value="title" <?php checked($search_type, 'title'); ?>> </label>
<?php _e('Titel', 'wp-multi-search'); ?> </div>
</label> <div class="csp-search-fields">
<label> <input type="text" name="s" placeholder="🔍 Suchbegriff eingeben" value="<?php echo esc_attr(get_search_query()); ?>">
<input type="radio" name="search_type" value="guest_author" <?php checked($search_type, 'guest_author'); ?>> <input type="submit" value="Suchen">
<?php _e('Autoren', 'wp-multi-search'); ?> </div>
</label> </form>
</div> <?php
<div class="csp-search-fields"> return ob_get_clean();
<input type="text" name="s" placeholder="🔍 Suchbegriff eingeben" value="<?php echo esc_attr(get_search_query()); ?>"> }
<input type="submit" value="<?php _e('Suchen', 'wp-multi-search'); ?>">
</div> // 2⃣ **Shortcode für das Suchformular**
</form> add_shortcode('custom_search', 'csp_search_form');
<?php
return ob_get_clean(); // 3⃣ **Suchfeld als Widget**
} class CSP_Search_Widget extends WP_Widget {
public function __construct() {
// 2⃣ **Shortcode für das Suchformular** parent::__construct(
add_shortcode('custom_search', 'csp_search_form'); 'csp_search_widget',
__('Suche Widget', 'textdomain'),
// 3⃣ **Suchfeld als Widget** array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'textdomain'))
class CSP_Search_Widget extends WP_Widget { );
public function __construct() { }
parent::__construct(
'csp_search_widget', public function widget($args, $instance) {
__('Suche Widget', 'wp-multi-search'), echo $args['before_widget'];
array('description' => __('Suchfeld mit Optionen für Titel- und Gastautoren-Suche', 'wp-multi-search')) echo '<h3 class="widget-title">' . __('Suche', 'textdomain') . '</h3>';
); echo csp_search_form();
} echo $args['after_widget'];
}
public function widget($args, $instance) { }
echo $args['before_widget'];
echo '<h3 class="widget-title">' . __('Suche', 'wp-multi-search') . '</h3>'; function csp_register_widget() {
echo csp_search_form(); register_widget('CSP_Search_Widget');
echo $args['after_widget']; }
} add_action('widgets_init', 'csp_register_widget');
}
// 4⃣ **Einstellungsseite hinzufügen**
function csp_register_widget() { function csp_add_menu_page() {
register_widget('CSP_Search_Widget'); add_options_page(
} 'WP-Multi-Search Settings',
add_action('widgets_init', 'csp_register_widget'); 'WP-Multi-Search',
'manage_options',
// 4⃣ **Einstellungsseite hinzufügen** 'custom-search-settings',
function csp_add_menu_page() { 'csp_render_settings_page'
add_options_page( );
'WP-Multi-Search Settings', }
'WP-Multi-Search', add_action('admin_menu', 'csp_add_menu_page');
'manage_options',
'custom-search-settings', // 5⃣ **Einstellungsseite rendern**
'csp_render_settings_page' function csp_render_settings_page() {
); ?>
} <div class="wrap">
add_action('admin_menu', 'csp_add_menu_page'); <h1>WP-Multi Search Settings</h1>
<form method="post" action="options.php">
// 5⃣ **Einstellungsseite rendern** <?php
function csp_render_settings_page() { settings_fields('csp_settings_group');
?> do_settings_sections('custom-search-settings');
<div class="wrap"> submit_button();
<h1><?php _e('WP-Multi Search Settings', 'wp-multi-search'); ?></h1> ?>
<form method="post" action="options.php"> </form>
<?php </div>
settings_fields('csp_settings_group'); <?php
do_settings_sections('custom-search-settings'); }
wp_nonce_field('csp_settings_save', 'csp_settings_nonce');
submit_button(); // 6⃣ **Einstellungen registrieren**
?> function csp_register_settings() {
</form> register_setting('csp_settings_group', 'csp_shiftnav_position');
</div>
<?php add_settings_section(
} 'csp_main_section',
'ShiftNav Position',
// 6⃣ **Einstellungen registrieren** 'csp_section_callback',
function csp_register_settings() { 'custom-search-settings'
register_setting('csp_settings_group', 'csp_shiftnav_position'); );
add_settings_section( add_settings_field(
'csp_main_section', 'csp_shiftnav_position_select',
'ShiftNav Position', 'Select ShiftNav Position',
'csp_section_callback', 'csp_shiftnav_position_select_callback',
'custom-search-settings' 'custom-search-settings',
); 'csp_main_section'
);
add_settings_field( }
'csp_shiftnav_position_select', add_action('admin_init', 'csp_register_settings');
'Select ShiftNav Position',
'csp_shiftnav_position_select_callback', // 7⃣ **Callback für die Sektion**
'custom-search-settings', function csp_section_callback() {
'csp_main_section' echo 'Choose where to display the search form in ShiftNav:';
); }
}
add_action('admin_init', 'csp_register_settings'); // 8⃣ **Callback für das Auswahlfeld**
function csp_shiftnav_position_select_callback() {
// 7⃣ **Callback für die Sektion** $positions = array(
function csp_section_callback() { 'top' => 'Top of ShiftNav',
echo __('Choose where to display the search form in ShiftNav:', 'wp-multi-search'); 'bottom' => 'Bottom of ShiftNav',
} 'none' => 'Do not display in ShiftNav'
);
// 8⃣ **Callback für das Auswahlfeld** $selected_position = get_option('csp_shiftnav_position', 'none');
function csp_shiftnav_position_select_callback() {
$positions = array( echo '<select name="csp_shiftnav_position">';
'top' => __('Top of ShiftNav', 'wp-multi-search'), foreach ($positions as $value => $label) {
'bottom' => __('Bottom of ShiftNav', 'wp-multi-search'), echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>';
'none' => __('Do not display in ShiftNav', 'wp-multi-search') }
); echo '</select>';
$selected_position = get_option('csp_shiftnav_position', 'none'); }
echo '<select name="csp_shiftnav_position">'; // 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav**
foreach ($positions as $value => $label) { function csp_insert_search_form_in_shiftnav($nav_menu, $args) {
echo '<option value="' . esc_attr($value) . '" ' . selected($selected_position, $value, false) . '>' . esc_html($label) . '</option>'; // Überprüfen, ob es sich um das ShiftNav-Menü handelt
} if ($args->theme_location !== 'shiftnav') {
echo '</select>'; return $nav_menu;
} }
// 9⃣ **Funktion zum Einfügen des Suchformulars in ShiftNav** $position = get_option('csp_shiftnav_position', 'none');
function csp_insert_search_form_in_shiftnav($nav_menu, $args) { if ($position === 'none') {
if ($args->theme_location !== 'shiftnav') { return $nav_menu;
return $nav_menu; }
}
$search_form = '<li class="menu-item">' . csp_search_form() . '</li>';
$position = get_option('csp_shiftnav_position', 'none');
if ($position === 'none') { if ($position === 'top') {
return $nav_menu; return $search_form . $nav_menu;
} } elseif ($position === 'bottom') {
return $nav_menu . $search_form;
$search_form = '<li class="menu-item">' . csp_search_form() . '</li>'; }
if ($position === 'top') { return $nav_menu;
return $search_form . $nav_menu; }
} elseif ($position === 'bottom') { add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2);
return $nav_menu . $search_form;
} // 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung**
function csp_custom_search_results($query) {
return $nav_menu; if (!is_admin() && $query->is_search() && $query->is_main_query()) {
} $search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all';
add_filter('wp_nav_menu_items', 'csp_insert_search_form_in_shiftnav', 10, 2); $search_term = $query->get('s');
// 🔟 **Aktualisierte Suchfunktion mit Suchtyp-Unterscheidung** switch($search_type) {
function csp_custom_search_results($query) { case 'title':
if (!is_admin() && $query->is_search() && $query->is_main_query()) { $query->set('post_title_like', $search_term);
$search_type = isset($_GET['search_type']) ? sanitize_text_field($_GET['search_type']) : 'all'; $query->set('s', '');
$search_term = $query->get('s'); add_filter('posts_where', 'csp_title_search_where', 10, 2);
break;
switch($search_type) {
case 'title': case 'guest_author':
$query->set('post_title_like', $search_term); $meta_query = array(
$query->set('s', ''); array(
add_filter('posts_where', 'csp_title_search_where', 10, 2); 'key' => '_guest_author',
break; 'value' => $search_term,
'compare' => '='
case 'guest_author': )
$meta_query = array( );
array( $query->set('meta_query', $meta_query);
'key' => '_guest_author', $query->set('s', '');
'value' => $search_term, $query->set('post_type', 'post');
'compare' => '=' break;
)
); default: // 'all'
$query->set('meta_query', $meta_query); $query->set('_meta_or_title', $search_term);
$query->set('s', ''); add_filter('posts_search', 'csp_custom_search_where', 10, 2);
$query->set('post_type', 'post'); add_filter('posts_join', 'csp_custom_search_join', 10, 2);
break; add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2);
break;
default: // 'all' }
$query->set('_meta_or_title', $search_term); }
add_filter('posts_search', 'csp_custom_search_where', 10, 2); }
add_filter('posts_join', 'csp_custom_search_join', 10, 2); add_action('pre_get_posts', 'csp_custom_search_results');
add_filter('posts_groupby', 'csp_custom_search_groupby', 10, 2);
break; // 1⃣1⃣ **Hilfsfunktionen für verschiedene Suchtypen**
} function csp_title_search_where($where, $query) {
global $wpdb;
// Filter nach der Abfrage entfernen if ($search_term = $query->get('post_title_like')) {
add_action('wp', function() use ($search_type) { $where .= $wpdb->prepare(
switch($search_type) { " AND {$wpdb->posts}.post_title LIKE %s",
case 'title': '%' . $wpdb->esc_like($search_term) . '%'
remove_filter('posts_where', 'csp_title_search_where', 10); );
break; }
case 'guest_author': return $where;
// Keine zusätzlichen Filter zum Entfernen }
break;
default: function csp_custom_search_where($where, $query) {
remove_filter('posts_search', 'csp_custom_search_where', 10); global $wpdb;
remove_filter('posts_join', 'csp_custom_search_join', 10); if ($search_term = $query->get('_meta_or_title')) {
remove_filter('posts_groupby', 'csp_custom_search_groupby', 10); $search = '%' . $wpdb->esc_like($search_term) . '%';
break; $where .= $wpdb->prepare(
} " OR ({$wpdb->posts}.post_title LIKE %s)
}); OR ({$wpdb->posts}.post_content LIKE %s)
} OR ({$wpdb->posts}.post_excerpt LIKE %s)
} OR (guest_author.meta_value LIKE %s)
add_action('pre_get_posts', 'csp_custom_search_results'); OR ({$wpdb->users}.display_name LIKE %s)",
$search, $search, $search, $search, $search
// 1⃣1⃣ **Hilfsfunktionen für verschiedene Suchtypen** );
function csp_title_search_where($where, $query) { }
global $wpdb; return $where;
if ($search_term = $query->get('post_title_like')) { }
$where .= $wpdb->prepare(
" AND {$wpdb->posts}.post_title LIKE %s", function csp_custom_search_join($join, $query) {
'%' . $wpdb->esc_like($search_term) . '%' global $wpdb;
); if ($query->get('_meta_or_title')) {
} $join .= " LEFT JOIN {$wpdb->postmeta} guest_author ON ({$wpdb->posts}.ID = guest_author.post_id AND guest_author.meta_key = '_guest_author')";
return $where; $join .= " LEFT JOIN {$wpdb->users} ON ({$wpdb->posts}.post_author = {$wpdb->users}.ID)";
} }
return $join;
function csp_custom_search_where($where, $query) { }
global $wpdb;
if ($search_term = $query->get('_meta_or_title')) { function csp_custom_search_groupby($groupby, $query) {
$search = '%' . $wpdb->esc_like($search_term) . '%'; global $wpdb;
$where .= $wpdb->prepare( if ($query->get('_meta_or_title')) {
" OR ({$wpdb->posts}.post_title LIKE %s) $groupby = "{$wpdb->posts}.ID";
OR ({$wpdb->posts}.post_content LIKE %s) }
OR ({$wpdb->posts}.post_excerpt LIKE %s) return $groupby;
OR (guest_author.meta_value LIKE %s) }
OR ({$wpdb->users}.display_name LIKE %s)",
$search, $search, $search, $search, $search // 1⃣2⃣ **Hilfsfunktion für Gastautoren-Suche**
); function csp_guest_author_search_where($where, $query) {
} global $wpdb;
return $where; if ($query->get('meta_query')) {
} $where .= " AND EXISTS (
SELECT 1 FROM {$wpdb->postmeta}
function csp_custom_search_join($join, $query) { WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
global $wpdb; AND {$wpdb->postmeta}.meta_key = '_guest_author'
if ($query->get('_meta_or_title')) { AND {$wpdb->postmeta}.meta_value != ''
$join .= " LEFT JOIN {$wpdb->postmeta} guest_author ON ({$wpdb->posts}.ID = guest_author.post_id AND guest_author.meta_key = '_guest_author')"; )";
$join .= " LEFT JOIN {$wpdb->users} ON ({$wpdb->posts}.post_author = {$wpdb->users}.ID)"; }
} return $where;
return $join; }
} add_filter('posts_where', 'csp_guest_author_search_where', 10, 2);
function csp_custom_search_groupby($groupby, $query) {
global $wpdb;
if ($query->get('_meta_or_title')) {
$groupby = "{$wpdb->posts}.ID";
}
return $groupby;
}
// 1⃣2⃣ **Hilfsfunktion für Gastautoren-Suche**
function csp_guest_author_search_where($where, $query) {
global $wpdb;
if ($query->get('meta_query')) {
$where .= " AND EXISTS (
SELECT 1 FROM {$wpdb->postmeta}
WHERE {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
AND {$wpdb->postmeta}.meta_key = '_guest_author'
AND {$wpdb->postmeta}.meta_value != ''
)";
}
return $where;
}
add_filter('posts_where', 'csp_guest_author_search_where', 10, 2);
?> ?>