-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrze-glossary.php
208 lines (174 loc) · 7.4 KB
/
rrze-glossary.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
Plugin Name: RRZE Glossary
Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-glossary
Description: Plugin, um Glossar-Einträge zu erstellen und aus dem FAU-Netzwerk zu synchronisieren. Verwendbar als Shortcode, Block oder Widget.
Version: 2.1.2
Requires at least: 6.1
Requires PHP: 8.0
Author: RRZE Webteam
Author URI: https://blogs.fau.de/webworking/
License: GNU General Public License v3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Domain Path: /languages
Text Domain: rrze-glossary
*/
namespace RRZE\Glossary;
defined('ABSPATH') || exit;
require_once 'config/config.php';
use RRZE\Glossary\Main;
$s = array(
'/^((http|https):\/\/)?(www.)+/i',
'/\//',
'/[^A-Za-z0-9\-]/'
);
$r = array(
'',
'-',
'-'
);
define( 'GLOSSARYLOGFILE', plugin_dir_path( __FILE__) . 'rrze-glossary-' . preg_replace( $s, $r, get_bloginfo( 'url' ) ) . '.log' );
const RRZE_PHP_VERSION = '8.0';
const RRZE_WP_VERSION = '6.1';
const RRZE_PLUGIN_FILE = __FILE__;
const RRZE_SCHEMA_START = '<div style="display:none" itemscope itemtype="https://schema.org/DefinedTerm">';
const RRZE_SCHEMA_END = '</div>';
const RRZE_SCHEMA_TITLE_START = '<div style="display:none" itemscope itemprop="name" itemtype="https://schema.org/name"><div style="display:none" itemprop="name">';
const RRZE_SCHEMA_TITLE_END = '</div>';
const RRZE_SCHEMA_CONTENT_START = '<div style="display:none" itemscope itemprop="description" itemtype="https://schema.org/description"><div style="display:none" itemprop="text">';
const RRZE_SCHEMA_CONTENT_END = '</div></div></div>';
// Automatische Laden von Klassen.
spl_autoload_register(function ($class) {
$prefix = __NAMESPACE__;
$base_dir = __DIR__ . '/includes/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Registriert die Plugin-Funktion, die bei Aktivierung des Plugins ausgeführt werden soll.
register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');
// Registriert die Plugin-Funktion, die ausgeführt werden soll, wenn das Plugin deaktiviert wird.
register_deactivation_hook(__FILE__, __NAMESPACE__ . '\deactivation');
// Wird aufgerufen, sobald alle aktivierten Plugins geladen wurden.
add_action('plugins_loaded', __NAMESPACE__ . '\loaded');
/**
* Einbindung der Sprachdateien.
*/
function load_textdomain()
{
load_plugin_textdomain('rrze-glossary', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__))));
}
/**
* Überprüft die minimal erforderliche PHP- u. WP-Version.
*/
function system_requirements()
{
$error = '';
if (version_compare(PHP_VERSION, RRZE_PHP_VERSION, '<')) {
/* translators: 1: current PHP version, 2: required PHP version */
$error = sprintf(__('The server is running PHP version %1$s. The Plugin requires at least PHP version %2$s.', 'rrze-typesettings'), PHP_VERSION, RRZE_PHP_VERSION);
} elseif (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {
/* translators: 1: current WordPress version, 2: required WordPress version */
$error = sprintf(__('The server is running WordPress version %1$s. The Plugin requires at least WordPress version %2$s.', 'rrze-typesettings'), $GLOBALS['wp_version'], RRZE_WP_VERSION);
}
return $error;
}
function addMetadata(){
if (post_type_exists('glossary')){
// add metadata for posts with CPT glossary
$postIds = get_posts([
'post_type' => 'glossary',
'nopaging' => true,
'fields' => 'ids'
]);
$lang = substr( get_locale(), 0, 2);
foreach( $postIds as $postID ){
if (metadata_exists('post', $postID, 'source') === false){
update_post_meta($postID, 'source', 'website');
update_post_meta($postID, 'remoteID', $postID);
update_post_meta($postID, 'lang', $lang);
$remoteChanged = get_post_timestamp( $postID, 'modified' );
update_post_meta( $postID, 'remoteChanged', $remoteChanged );
}
}
if (taxonomy_exists('glossary_category')){
// add metadata for glossary_category including their children
$terms = get_terms([
'taxonomy' => 'glossary_category',
'hide_empty' => true
]);
foreach( $terms as $term ){
if (metadata_exists('term', $term->term_id, 'source') === false){
update_term_meta($term->term_id, 'source', 'website');
update_term_meta($term->term_id, 'lang', $lang);
}
}
}
}
}
/**
* Wird durchgeführt, nachdem das Plugin aktiviert wurde.
*/
function activation() {
// Sprachdateien werden eingebunden.
load_textdomain();
// Überprüft die minimal erforderliche PHP- u. WP-Version.
// Wenn die Überprüfung fehlschlägt, dann wird das Plugin automatisch deaktiviert.
if ($error = system_requirements()) {
deactivate_plugins(plugin_basename(__FILE__), false, true);
wp_die(esc_html($error));
}
// Ab hier können die Funktionen hinzugefügt werden,
// die bei der Aktivierung des Plugins aufgerufen werden müssen.
// Bspw. wp_schedule_event, flush_rewrite_rules, etc.
// Einmaliger Aufruf: vom Theme gespeicherte Daten zum CPT "glossary" und zur taxonomy "glossary_category" um Metadaten ergaenzen, damit rrze-glossary funktioniert:
if ( get_option( 'rrze-glossary-metadata' ) != 'added' ) {
addMetadata();
update_option( 'rrze-glossary-metadata', 'added' );
}
}
/**
* Wird durchgeführt, nachdem das Plugin deaktiviert wurde.
*/
function deactivation() {
// Hier können die Funktionen hinzugefügt werden, die
// bei der Deaktivierung des Plugins aufgerufen werden müssen.
// Bspw. delete_option, wp_clear_scheduled_hook, flush_rewrite_rules, etc.
// delete_option(Options::get_option_name());
wp_clear_scheduled_hook( 'rrze_glossary_auto_sync' );
flush_rewrite_rules();
}
function rrze_glossary_init() {
register_block_type( __DIR__ . '/build' );
$script_handle = generate_block_asset_handle( 'create-block/rrze-glossary', 'editorScript' );
wp_set_script_translations( $script_handle, 'rrze-glossary', plugin_dir_path( __FILE__ ) . 'languages' );
}
/**
* Wird durchgeführt, nachdem das WP-Grundsystem hochgefahren
* und alle Plugins eingebunden wurden.
*/
function loaded() {
// Sprachdateien werden eingebunden.
load_textdomain();
// Überprüft die minimal erforderliche PHP- u. WP-Version.
if ($error = system_requirements()) {
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
$plugin_data = get_plugin_data(__FILE__);
$plugin_name = $plugin_data['Name'];
$tag = is_network_admin() ? 'network_admin_notices' : 'admin_notices';
add_action($tag, function () use ($plugin_name, $error) {
printf('<div class="notice notice-error"><p>%1$s: %2$s</p></div>', esc_html($plugin_name), esc_html($error));
});
} else {
// Hauptklasse (Main) wird instanziiert.
$main = new Main(__FILE__);
$main->onLoaded();
}
add_action( 'init', __NAMESPACE__ . '\rrze_glossary_init' );
}