239 lines
9.6 KiB
PHP
239 lines
9.6 KiB
PHP
<?php
|
||
/**
|
||
* WP Multi – Modul: Custom Shortcodes
|
||
*
|
||
* Automatisch aus wp-multi.php ausgelagert. Wird über den Loader in
|
||
* wp-multi.php eingebunden (nur wenn das WP Multi Toolkit aktiv ist).
|
||
*/
|
||
|
||
if (!defined('ABSPATH')) { exit; }
|
||
|
||
/*
|
||
* custom shortcodes
|
||
*/
|
||
|
||
|
||
// Funktion, um die Datenbanktabelle für Shortcodes zu erstellen
|
||
function wp_multi_create_shortcodes_table() {
|
||
global $wpdb;
|
||
|
||
$table_name = $wpdb->prefix . 'wp_multi_shortcodes'; // Name der Tabelle mit Präfix
|
||
$charset_collate = $wpdb->get_charset_collate();
|
||
|
||
// SQL-Abfrage zum Erstellen der Tabelle
|
||
$sql = "CREATE TABLE $table_name (
|
||
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||
shortcode_name varchar(255) NOT NULL,
|
||
shortcode_content text NOT NULL,
|
||
PRIMARY KEY (id),
|
||
UNIQUE KEY shortcode_name (shortcode_name)
|
||
) $charset_collate;";
|
||
|
||
// Datenbank abfragen und ausführen
|
||
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||
dbDelta( $sql );
|
||
}
|
||
register_activation_hook( WP_MULTI_FILE, 'wp_multi_create_shortcodes_table' );
|
||
|
||
// Der Shortcodes-Menüpunkt wird zentral in wp_multi_register_admin_menus() registriert.
|
||
|
||
// Callback-Funktion für das Shortcode-Verwaltungs-Interface
|
||
function wp_multi_shortcode_page() {
|
||
global $wpdb;
|
||
|
||
$message = ''; // Variable für benutzerdefinierte Nachrichten
|
||
|
||
// Verarbeite das Speichern von Shortcodes
|
||
if (isset($_POST['wp_multi_shortcode_name']) && isset($_POST['wp_multi_shortcode_content'])) {
|
||
check_admin_referer('wp_multi_save_shortcode', 'wp_multi_shortcode_nonce');
|
||
|
||
// Hole die übermittelten Shortcodes
|
||
$name = sanitize_key($_POST['wp_multi_shortcode_name']);
|
||
$content = wp_kses_post(wp_unslash($_POST['wp_multi_shortcode_content']));
|
||
|
||
// Prüfen, ob der Shortcode bereits existiert
|
||
$existing_shortcode = $wpdb->get_var($wpdb->prepare("SELECT id FROM {$wpdb->prefix}wp_multi_shortcodes WHERE shortcode_name = %s", $name));
|
||
|
||
if ($existing_shortcode) {
|
||
// Aktualisiere den Shortcode, falls er bereits existiert
|
||
$wpdb->update(
|
||
$wpdb->prefix . 'wp_multi_shortcodes',
|
||
['shortcode_content' => $content],
|
||
['shortcode_name' => $name]
|
||
);
|
||
$message = 'Shortcode wurde aktualisiert!';
|
||
} else {
|
||
// Andernfalls einen neuen Shortcode einfügen
|
||
$wpdb->insert(
|
||
$wpdb->prefix . 'wp_multi_shortcodes',
|
||
[
|
||
'shortcode_name' => $name,
|
||
'shortcode_content' => $content
|
||
]
|
||
);
|
||
$message = 'Shortcode wurde hinzugefügt!';
|
||
}
|
||
}
|
||
|
||
// Shortcode löschen
|
||
if (isset($_GET['delete_shortcode']) && !empty($_GET['delete_shortcode'])) {
|
||
check_admin_referer('wp_multi_delete_shortcode_' . intval($_GET['delete_shortcode']));
|
||
$delete_id = intval($_GET['delete_shortcode']);
|
||
$wpdb->delete(
|
||
$wpdb->prefix . 'wp_multi_shortcodes',
|
||
['id' => $delete_id]
|
||
);
|
||
$message = 'Shortcode wurde gelöscht!';
|
||
}
|
||
|
||
// Holen der gespeicherten Shortcodes aus der Datenbank
|
||
$custom_shortcodes = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wp_multi_shortcodes");
|
||
|
||
wpmt_admin_page_open(
|
||
__('Shortcodes', 'wp-multi'),
|
||
__('Eigene Shortcodes erstellen, die im Editor eingefügt werden können', 'wp-multi')
|
||
);
|
||
|
||
if (!empty($message)) {
|
||
wpmt_admin_notice('success', $message);
|
||
}
|
||
|
||
wpmt_admin_card_open(__('Shortcode anlegen / aktualisieren', 'wp-multi'));
|
||
?>
|
||
<form method="post">
|
||
<?php wp_nonce_field('wp_multi_save_shortcode', 'wp_multi_shortcode_nonce'); ?>
|
||
<div class="wpm-field">
|
||
<label class="wpm-field__label" for="wp_multi_shortcode_name"><?php _e('Name des Shortcodes', 'wp-multi'); ?>
|
||
<small class="wpm-field__hint"><?php _e('Nur Kleinbuchstaben, Zahlen und Unterstriche.', 'wp-multi'); ?></small>
|
||
</label>
|
||
<input type="text" name="wp_multi_shortcode_name" id="wp_multi_shortcode_name" required />
|
||
</div>
|
||
<div class="wpm-field">
|
||
<label class="wpm-field__label" for="wp_multi_shortcode_content"><?php _e('Inhalt des Shortcodes', 'wp-multi'); ?></label>
|
||
<textarea name="wp_multi_shortcode_content" id="wp_multi_shortcode_content" rows="5" required></textarea>
|
||
</div>
|
||
<?php submit_button(__('Shortcode speichern', 'wp-multi')); ?>
|
||
</form>
|
||
<?php
|
||
wpmt_admin_card_close();
|
||
|
||
wpmt_admin_card_open(__('Verfügbare Shortcodes', 'wp-multi'));
|
||
if (!empty($custom_shortcodes)) {
|
||
?>
|
||
<div class="wpm-table-wrap">
|
||
<table class="wpm-table">
|
||
<thead>
|
||
<tr>
|
||
<th><?php _e('Shortcode', 'wp-multi'); ?></th>
|
||
<th><?php _e('Inhalt', 'wp-multi'); ?></th>
|
||
<th><?php _e('Aktionen', 'wp-multi'); ?></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<?php foreach ($custom_shortcodes as $shortcode) :
|
||
$delete_url = wp_nonce_url(
|
||
admin_url('admin.php?page=wp_multi_shortcodes&delete_shortcode=' . $shortcode->id),
|
||
'wp_multi_delete_shortcode_' . $shortcode->id
|
||
);
|
||
?>
|
||
<tr>
|
||
<td><code>[<?php echo esc_html($shortcode->shortcode_name); ?>]</code></td>
|
||
<td><?php echo esc_html(wp_trim_words($shortcode->shortcode_content, 15)); ?></td>
|
||
<td><a href="<?php echo esc_url($delete_url); ?>" class="wpm-btn wpm-btn--danger" onclick="return confirm('<?php esc_attr_e('Möchten Sie diesen Shortcode wirklich löschen?', 'wp-multi'); ?>');"><?php _e('Löschen', 'wp-multi'); ?></a></td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<?php
|
||
} else {
|
||
wpmt_admin_notice('info', __('Keine benutzerdefinierten Shortcodes gefunden.', 'wp-multi'));
|
||
}
|
||
wpmt_admin_card_close();
|
||
|
||
wpmt_admin_page_close();
|
||
}
|
||
|
||
// Shortcode-Verwaltung: Ermöglicht Benutzern das Erstellen eigener Shortcodes
|
||
function wp_multi_register_custom_shortcodes() {
|
||
global $wpdb;
|
||
|
||
// Holen der gespeicherten Shortcodes aus der Datenbank
|
||
$custom_shortcodes = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}wp_multi_shortcodes");
|
||
|
||
// Wenn keine benutzerdefinierten Shortcodes vorhanden sind, abbrechen
|
||
if (empty($custom_shortcodes)) {
|
||
return;
|
||
}
|
||
|
||
// Definiere die Shortcodes in WordPress
|
||
foreach ($custom_shortcodes as $shortcode) {
|
||
add_shortcode($shortcode->shortcode_name, function() use ($shortcode) {
|
||
return wp_kses_post($shortcode->shortcode_content);
|
||
});
|
||
}
|
||
}
|
||
add_action('init', 'wp_multi_register_custom_shortcodes');
|
||
|
||
// Inhalt der Meta-Box anzeigen
|
||
function wp_multi_render_shortcode_meta_box($post) {
|
||
global $wpdb;
|
||
|
||
// Alle gespeicherten Shortcodes aus der Datenbank holen
|
||
$shortcodes = $wpdb->get_results("SELECT shortcode_name FROM {$wpdb->prefix}wp_multi_shortcodes");
|
||
|
||
if (!empty($shortcodes)) {
|
||
echo '<select id="wp_multi_shortcode_dropdown">';
|
||
echo '<option value="">-- Shortcode auswählen --</option>';
|
||
|
||
foreach ($shortcodes as $shortcode) {
|
||
echo '<option value="' . esc_attr($shortcode->shortcode_name) . '">' . esc_html($shortcode->shortcode_name) . '</option>';
|
||
}
|
||
|
||
echo '</select>';
|
||
echo '<button type="button" class="button button-primary" id="wp_multi_insert_shortcode">Einfügen</button>';
|
||
} else {
|
||
echo '<p>Keine Shortcodes vorhanden.</p>';
|
||
}
|
||
}
|
||
|
||
// JavaScript für Meta-Box einbinden (Classic Editor: fügt den gewählten Shortcode ein)
|
||
function wp_multi_enqueue_admin_scripts($hook) {
|
||
if ('post.php' === $hook || 'post-new.php' === $hook) {
|
||
wp_enqueue_script('wp-multi-shortcode', plugin_dir_url(WP_MULTI_FILE) . 'js/classic-editor-shortcodes.js', array('jquery'), '1.0', true);
|
||
}
|
||
}
|
||
add_action('admin_enqueue_scripts', 'wp_multi_enqueue_admin_scripts');
|
||
|
||
// Funktion zum Registrieren des TinyMCE Plugins
|
||
function wp_multi_add_shortcode_button() {
|
||
add_filter('mce_external_plugins', 'wp_multi_register_tinymce_plugin');
|
||
add_filter('mce_buttons', 'wp_multi_add_tinymce_button');
|
||
}
|
||
add_action('admin_head', 'wp_multi_add_shortcode_button');
|
||
|
||
// Plugin für TinyMCE registrieren (angepasster Pfad zum JS-File)
|
||
function wp_multi_register_tinymce_plugin($plugins) {
|
||
$plugins['wp_multi_shortcodes'] = plugin_dir_url(WP_MULTI_FILE) . 'js/tinymce-shortcodes.js';
|
||
return $plugins;
|
||
}
|
||
|
||
// Button zur TinyMCE Toolbar hinzufügen
|
||
function wp_multi_add_tinymce_button($buttons) {
|
||
array_push($buttons, 'wp_multi_shortcodes');
|
||
return $buttons;
|
||
}
|
||
|
||
// Shortcodes aus der Datenbank für das JavaScript bereitstellen
|
||
function wp_multi_localize_shortcodes() {
|
||
global $wpdb;
|
||
$shortcodes = $wpdb->get_results("SELECT shortcode_name FROM {$wpdb->prefix}wp_multi_shortcodes", ARRAY_A);
|
||
|
||
// Shortcodes als JSON an das JS-File übergeben
|
||
wp_enqueue_script('wp-multi-tinymce', plugin_dir_url(WP_MULTI_FILE) . 'js/tinymce-shortcodes.js', array('jquery'), null, true);
|
||
wp_localize_script('wp-multi-tinymce', 'wpMultiShortcodes', $shortcodes);
|
||
}
|
||
add_action('admin_enqueue_scripts', 'wp_multi_localize_shortcodes');
|
||
|
||
|