rrze-private-site.php 3.66 KB
Newer Older
Rolf Forst's avatar
Rolf Forst committed
1
2
3
4
5
6
<?php

/*
Plugin Name:     RRZE Private Site
Plugin URI:      https://gitlab.rrze.fau.de/rrze-webteam/rrze-private-site
Description:     Restricts access to the website.
Rolf Forst's avatar
Rolf Forst committed
7
Version:         2.5.3
Rolf Forst's avatar
Rolf Forst committed
8
9
10
11
12
13
14
15
16
17
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-private-site
*/

namespace RRZE\PrivateSite;

18
19
defined('ABSPATH') || exit;

Rolf Forst's avatar
Rolf Forst committed
20
21
22
use RRZE\PrivateSite\Main;

const RRZE_PHP_VERSION = '7.4';
Rolf Forst's avatar
Rolf Forst committed
23
const RRZE_WP_VERSION = '5.8';
Rolf Forst's avatar
Rolf Forst committed
24
25

const PRIVATE_SITE_OPTION = 'private_site';
Rolf Forst's avatar
Rolf Forst committed
26
const PRIVATE_SITE_PASSWORD_OPTION = 'private_site_password';
Rolf Forst's avatar
Rolf Forst committed
27
28
29
30
31
32
const PRIVATE_SITE_ALLOW_IPADDR_OPTION = 'private_site_allow_ipaddr';
const PRIVATE_SITE_SSO_OPTION = 'private_site_sso';
const PRIVATE_SITE_ALLOW_AFFILIATION_OPTION = 'private_site_allow_affiliation';
const PRIVATE_SITE_ALLOW_ENTITLEMENT_OPTION = 'private_site_allow_entitlement';
const PRIVATE_SITE_FEED_TOKEN_OPTION = 'private_site_feed_token';

Rolf Forst's avatar
Rolf Forst committed
33
// Autoloader (PSR-4)
Rolf Forst's avatar
Rolf Forst committed
34
35
36
37
38
39
40
41
42
spl_autoload_register(function ($class) {
    $prefix = __NAMESPACE__;
    $base_dir = __DIR__ . '/includes/';

    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }

Rolf Forst's avatar
Rolf Forst committed
43
44
    $relativeClass = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relativeClass) . '.php';
Rolf Forst's avatar
Rolf Forst committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58

    if (file_exists($file)) {
        require $file;
    }
});

register_activation_hook(__FILE__, __NAMESPACE__ . '\activation');

add_action('plugins_loaded', __NAMESPACE__ . '\loaded');

/**
 * Loads languages files into the list of text domains.
 * @return void
 */
Rolf Forst's avatar
Rolf Forst committed
59
function loadTextDomain()
Rolf Forst's avatar
Rolf Forst committed
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
{
    load_plugin_textdomain('rrze-private-site', false, sprintf('%s/languages/', dirname(plugin_basename(__FILE__))));
}

/**
 * System requirements.
 * @return string The error text.
 */
function systemRequirements()
{
    $error = '';
    if (version_compare(PHP_VERSION, RRZE_PHP_VERSION, '<')) {
        /* Translator: 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-private-site'), PHP_VERSION, RRZE_PHP_VERSION);
    } elseif (version_compare($GLOBALS['wp_version'], RRZE_WP_VERSION, '<')) {
        /* Translator: 1: current WP version, 2: required WP version */
        $error = sprintf(__('The server is running WordPress version %1$s. The Plugin requires at least WordPress version %2$s.', 'rrze-private-site'), $GLOBALS['wp_version'], RRZE_WP_VERSION);
    }
    return $error;
}

/**
 * Runs when the plugin is registered.
 * @return void
 */
function activation()
{
Rolf Forst's avatar
Rolf Forst committed
87
    loadTextdomain();
Rolf Forst's avatar
Rolf Forst committed
88

89
    if ($error = systemRequirements()) {
Rolf Forst's avatar
Rolf Forst committed
90
91
        deactivate_plugins(plugin_basename(__FILE__));
        wp_die(sprintf(__('Plugins: %1$s: %2$s', 'rrze-private-site'), plugin_basename(__FILE__), $error));
Rolf Forst's avatar
Rolf Forst committed
92
93
94
95
96
97
98
99
100
    }
}

/**
 * Runs when the plugin has been loaded.
 * @return void
 */
function loaded()
{
Rolf Forst's avatar
Rolf Forst committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    loadTextDomain();

    if ($error = systemRequirements()) {
        add_action('admin_init', function () use ($error) {
            $pluginData = get_plugin_data(__FILE__);
            $pluginName = $pluginData['Name'];
            $tag = is_plugin_active_for_network(plugin_basename(__FILE__)) ? 'network_admin_notices' : 'admin_notices';
            add_action($tag, function () use ($pluginName, $error) {
                printf(
                    '<div class="notice notice-error"><p>' . __('Plugins: %1$s: %2$s', 'rrze-private-site') . '</p></div>',
                    esc_html($pluginName),
                    esc_html($error)
                );
            });
115
        });
Rolf Forst's avatar
Rolf Forst committed
116
        return;
Rolf Forst's avatar
Rolf Forst committed
117
118
119
120
    }

    new Main;
}