-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrze-shorturl.php
157 lines (133 loc) · 5.17 KB
/
rrze-shorturl.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
<?php
/*
Plugin Name: RRZE ShortURL
Plugin URI: https://gitlab.rrze.fau.de/rrze-webteam/rrze-shorturl
Description: Plugin, um URLs zu verkürzen.
Version: 3.0.1
Requires at least: 6.4
Requires PHP: 8.2
Author: RRZE Webteam
Author URI: https://blogs.fau.de/webworking/
License: GNU General Public License v2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: rrze-shorturl
*/
namespace RRZE\ShortURL;
defined('ABSPATH') || exit;
require_once 'config/config.php';
const RRZE_PHP_VERSION = '8.2';
const RRZE_WP_VERSION = '6.4';
use RRZE\ShortURL\Main;
// 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-shorturl', 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);
} elseif (!class_exists('\RRZE\AccessControl\Permissions')) {
$error = __('Plugin RRZE-AC is mandatory.', 'rrze-shorturl');
}
return $error;
}
/**
* 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.
}
/**
* Wird durchgeführt, nachdem das Plugin deaktiviert wurde.
*/
function deactivation()
{
// delete the crons we've added in this plugin
wp_clear_scheduled_hook('rrze_shorturl_fetch_and_store_customerdomains');
wp_clear_scheduled_hook('rrze_shorturl_cleanup_inactive_idms');
wp_clear_scheduled_hook('rrze_shorturl_cleanup_invalid_links');
// delete all entries of all CPT
$custom_post_types = ['shorturl_idm', 'shorturl_domain', 'shorturl_service', 'shorturl_link', 'shorturl_category'];
foreach ($custom_post_types as $cpt) {
$post_ids = get_posts(array(
'post_type' => $cpt,
'numberposts' => -1,
'post_status' => 'any',
'fields' => 'ids'
));
foreach ($post_ids as $post_id) {
wp_delete_post($post_id, true);
}
}
// delete our options
delete_option('rrze_shorturl_option');
delete_option('rrze_shorturl_services_initialized');
delete_option('rrze_shorturl_migration_completed');
delete_option('rrze_shorturl_custom_tables_dropped');
}
/**
* 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();
}
}