-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgopherpress.php
More file actions
162 lines (132 loc) · 6.62 KB
/
gopherpress.php
File metadata and controls
162 lines (132 loc) · 6.62 KB
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
<?php
/**
* Plugin Name: GopherPress
* Plugin URI: https://github.com/emrikol/gopherpress
* Description: Serve your WordPress site over the Gopher protocol (RFC 1436). Because the web peaked in 1991.
* Version: 1.0.0
* Requires at least: 5.9
* Requires PHP: 8.0
* Author: Derrick Tennant
* Author URI: https://derrick.blog
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: gopherpress
*
* @package GopherPress
*/
declare(strict_types=1);
// Bail if called directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// ── Constants ──────────────────────────────────────────────────────────────
define( 'GOPHERPRESS_VERSION', '1.0.0' );
define( 'GOPHERPRESS_DIR', plugin_dir_path( __FILE__ ) );
define( 'GOPHERPRESS_DEFAULT_PORT', 7070 );
/*
* GOPHERPRESS_UPLOAD_DIR, GOPHERPRESS_PID_FILE, and GOPHERPRESS_LOG_FILE are
* defined in gopherpress_init() because they depend on wp_upload_dir(), which
* must not be called before the 'init' hook.
*/
// ── Polyfills ──────────────────────────────────────────────────────────────
require_once GOPHERPRESS_DIR . 'inc/polyfills.php';
// ── Class loading ──────────────────────────────────────────────────────────
require_once GOPHERPRESS_DIR . 'inc/class-aalib.php';
require_once GOPHERPRESS_DIR . 'inc/class-figlet.php';
require_once GOPHERPRESS_DIR . 'inc/class-content.php';
require_once GOPHERPRESS_DIR . 'inc/class-daemon.php';
require_once GOPHERPRESS_DIR . 'inc/class-rest.php';
require_once GOPHERPRESS_DIR . 'inc/class-admin.php';
// ── Activation / deactivation ──────────────────────────────────────────────
register_activation_hook( __FILE__, 'gopherpress_activate' );
register_deactivation_hook( __FILE__, 'gopherpress_deactivate' );
/**
* Plugin activation: creates the upload directory, sets default options,
* and detects whether exec()/proc_open() are available.
*
* @return void
*/
function gopherpress_activate(): void {
$upload = wp_upload_dir();
$upload_dir = trailingslashit( $upload['basedir'] ) . 'gopherpress';
wp_mkdir_p( $upload_dir );
// Default port — preserves existing value if re-activating.
add_option( 'gopherpress_port', GOPHERPRESS_DEFAULT_PORT );
// Default wrap width — preserves existing value if re-activating.
add_option( 'gopherpress_wrap_width', 70 );
// Detect exec capability once at activation.
$can_exec = function_exists( 'exec' ) || function_exists( 'proc_open' );
update_option( 'gopherpress_can_exec', $can_exec );
// Create empty log file if it doesn't exist.
$log = $upload_dir . '/gopherpress.log';
if ( ! file_exists( $log ) ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
file_put_contents( $log, '' );
}
}
/**
* Plugin deactivation: stops the Gopher daemon if it is running.
*
* @return void
*/
function gopherpress_deactivate(): void {
// Constants may not be defined yet if deactivating before init fires.
if ( ! defined( 'GOPHERPRESS_PID_FILE' ) ) {
$upload = wp_upload_dir();
$upload_dir = trailingslashit( $upload['basedir'] ) . 'gopherpress';
define( 'GOPHERPRESS_UPLOAD_DIR', $upload_dir );
define( 'GOPHERPRESS_PID_FILE', $upload_dir . '/gopherpress.pid' );
define( 'GOPHERPRESS_LOG_FILE', $upload_dir . '/gopherpress.log' );
}
GopherPress\Daemon::instance()->stop();
}
// ── Core hooks ─────────────────────────────────────────────────────────────
add_action( 'init', 'gopherpress_init' );
/**
* Defines the upload-directory constants (requires wp_upload_dir()).
*
* @return void
*/
function gopherpress_init(): void {
$upload = wp_upload_dir();
$upload_dir = trailingslashit( $upload['basedir'] ) . 'gopherpress';
if ( ! defined( 'GOPHERPRESS_UPLOAD_DIR' ) ) {
define( 'GOPHERPRESS_UPLOAD_DIR', $upload_dir );
define( 'GOPHERPRESS_PID_FILE', $upload_dir . '/gopherpress.pid' );
define( 'GOPHERPRESS_LOG_FILE', $upload_dir . '/gopherpress.log' );
}
}
// ── Admin hooks ────────────────────────────────────────────────────────────
add_action( 'admin_menu', array( GopherPress\Admin::instance(), 'register_menu' ) );
add_action( 'admin_enqueue_scripts', array( GopherPress\Admin::instance(), 'enqueue_assets' ) );
add_action( 'admin_notices', 'gopherpress_capability_notice' );
add_action( 'wp_ajax_gopherpress_start', array( GopherPress\Admin::instance(), 'ajax_start' ) );
add_action( 'wp_ajax_gopherpress_stop', array( GopherPress\Admin::instance(), 'ajax_stop' ) );
add_action( 'wp_ajax_gopherpress_restart', array( GopherPress\Admin::instance(), 'ajax_restart' ) );
add_action( 'wp_ajax_gopherpress_status', array( GopherPress\Admin::instance(), 'ajax_status' ) );
add_action( 'wp_ajax_gopherpress_save_port', array( GopherPress\Admin::instance(), 'ajax_save_port' ) );
add_action( 'wp_ajax_gopherpress_save_ascii_filter', array( GopherPress\Admin::instance(), 'ajax_save_ascii_filter' ) );
add_action( 'wp_ajax_gopherpress_save_wrap_width', array( GopherPress\Admin::instance(), 'ajax_save_wrap_width' ) );
/**
* Shows an admin notice when exec()/proc_open() are unavailable.
*
* @return void
*/
function gopherpress_capability_notice(): void {
if ( get_option( 'gopherpress_can_exec' ) ) {
return;
}
$screen = get_current_screen();
if ( ! $screen || 'toplevel_page_gopherpress' !== $screen->id ) {
return;
}
echo '<div class="notice notice-warning"><p>';
echo '<strong>' . esc_html__( 'GopherPress:', 'gopherpress' ) . '</strong> ';
esc_html_e(
'exec() and proc_open() are disabled on this host. The live Gopher daemon cannot start. Your content is still available over HTTP via the REST endpoint below.',
'gopherpress'
);
echo '</p></div>';
}
// ── REST API ───────────────────────────────────────────────────────────────
add_action( 'rest_api_init', array( GopherPress\Rest::instance(), 'register_routes' ) );