Skip to content

Commit e314e54

Browse files
committed
Allow to override default WP index.php
1 parent e5f8527 commit e314e54

4 files changed

Lines changed: 147 additions & 3 deletions

File tree

admin/class-crowdhandler-admin.php

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public function settings_page_html()
141141
__('Settings Saved', 'crowdhandler'),
142142
'updated'
143143
);
144+
145+
$this->handleIndexFilesOverrides();
144146
}
145147

146148
// show error/update messages
@@ -183,6 +185,18 @@ public function settings_init()
183185
)
184186
);
185187

188+
add_settings_field(
189+
'crowdhandler_settings_field_override_index',
190+
__('Override index.php', 'crowdhandler'),
191+
array($this, 'settings_field_override_index_callback'),
192+
'crowdhandler',
193+
'crowdhandler_settings_section',
194+
array(
195+
'label_for' => 'crowdhandler_settings_field_override_index',
196+
'class' => 'crowdhandler_row',
197+
)
198+
);
199+
186200
add_settings_field(
187201
'crowdhandler_settings_field_public_key',
188202
__('Public Key', 'crowdhandler'),
@@ -227,12 +241,99 @@ public function settings_field_is_enabled_callback($args)
227241
<?php echo isset($options[$args['label_for']]) ? (checked( $options[$args['label_for']], 'on', false )) : ( '' ); ?>
228242
id="<?php echo esc_attr( $args['label_for'] ); ?>"
229243
name="crowdhandler_settings[<?php echo esc_attr( $args['label_for'] ); ?>]"
230-
class="crowdhandler-input crowdhandler-input--textarea"
244+
class="crowdhandler-input"
231245
>
232246
<p class="description">
233247
<?php esc_html_e( 'Enabled field description', 'crowdhandler' ); ?>
234248
</p>
235249
<?php
236250
}
237251

252+
public function settings_field_override_index_callback($args)
253+
{
254+
$options = get_option('crowdhandler_settings');
255+
$canOverrideIndexFile = $this->isIndexFileWritable();
256+
$checked = '';
257+
if (isset($options[$args['label_for']])) {
258+
$checked = checked($options[$args['label_for']], 'on', false);
259+
}
260+
?>
261+
<input
262+
type="checkbox"
263+
<?php echo $checked; ?>
264+
id="<?php echo esc_attr( $args['label_for'] ); ?>"
265+
name="crowdhandler_settings[<?php echo esc_attr( $args['label_for'] ); ?>]"
266+
class="crowdhandler-input"
267+
<?php echo !$canOverrideIndexFile ? 'disabled' : ''; ?>
268+
>
269+
<?php if (!$canOverrideIndexFile): ?>
270+
<p class="description">
271+
<strong><?php esc_html_e('Main index.php file is not writable.', 'crowdhandler'); ?></strong>
272+
</p>
273+
<?php endif; ?>
274+
<p class="description">
275+
<?php esc_html_e('Override index field description', 'crowdhandler'); ?>
276+
</p>
277+
<?php
278+
}
279+
280+
public function handleIndexFilesOverrides()
281+
{
282+
$options = get_option('crowdhandler_settings');
283+
284+
if (
285+
$this->isIndexFileWritable() &&
286+
isset($options['crowdhandler_settings_field_is_enabled']) &&
287+
$options['crowdhandler_settings_field_is_enabled'] === 'on' &&
288+
isset($options['crowdhandler_settings_field_override_index']) &&
289+
$options['crowdhandler_settings_field_override_index'] === 'on'
290+
) {
291+
$data = var_export(
292+
array(
293+
'plugin_path' => CROWDHANDLER_PLUGIN_BASE_PATH,
294+
'options' => $options,
295+
),
296+
true
297+
);
298+
299+
if (!file_exists(CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH)) {
300+
rename(CROWDHANDLER_PLUGIN_INDEX_FILE_PATH, CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH);
301+
}
302+
303+
$fp = fopen(CROWDHANDLER_PLUGIN_INDEX_FILE_PATH, 'w');
304+
fwrite($fp, <<<PHP
305+
<?php
306+
307+
\$config = {$data};
308+
309+
require_once \$config['plugin_path'] . 'vendor/autoload.php';
310+
311+
\$ch = new CrowdHandlerGateKeeper(\$config['options']['crowdhandler_settings_field_public_key']);
312+
\$ch->checkRequest();
313+
314+
include 'wp-index.php';
315+
316+
\$ch->recordPerformance(http_response_code());
317+
318+
PHP
319+
);
320+
fclose($fp);
321+
} elseif (file_exists(CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH)) {
322+
rename(CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH, CROWDHANDLER_PLUGIN_INDEX_FILE_PATH);
323+
}
324+
}
325+
326+
private function isIndexFileWritable()
327+
{
328+
if (!is_writable(ABSPATH)) {
329+
return false;
330+
}
331+
332+
if (!is_writable(CROWDHANDLER_PLUGIN_INDEX_FILE_PATH)) {
333+
return false;
334+
}
335+
336+
return true;
337+
}
338+
238339
}

consts.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* The plugin bootstrap file
5+
*
6+
* This file is read by WordPress to generate the plugin information in the plugin
7+
* admin area. This file also includes all of the dependencies used by the plugin,
8+
* registers the activation and deactivation functions, and defines a function
9+
* that starts the plugin.
10+
*
11+
* @link https://www.crowdhandler.com/
12+
* @since 1.0.0
13+
* @package Crowdhandler
14+
*
15+
* @wordpress-plugin
16+
* Plugin Name: CrowdHandler™ Virtual Waiting Room - Protect your Website
17+
* Plugin URI: https://www.crowdhandler.com/
18+
* Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area.
19+
* Version: 1.0.0
20+
* Author: CROWDHANDLER LTD
21+
* Author URI: https://www.crowdhandler.com/
22+
* License: GPL-2.0+
23+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
24+
* Text Domain: crowdhandler
25+
* Domain Path: /languages
26+
*/
27+
28+
// If this file is called directly, abort.
29+
if (!defined('WPINC')) {
30+
die;
31+
}
32+
33+
define('CROWDHANDLER_PLUGIN_BASE_PATH', plugin_dir_path(__FILE__));
34+
define('CROWDHANDLER_PLUGIN_BASE_URL', plugin_dir_url(__FILE__));
35+
36+
define('CROWDHANDLER_PLUGIN_INDEX_FILE_PATH', ABSPATH . 'index.php');
37+
define('CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH', ABSPATH . 'wp-index.php');

crowdhandler.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
die;
3131
}
3232

33-
define('CROWDHANDLER_PLUGIN_BASE_PATH', plugin_dir_path(__FILE__));
34-
define('CROWDHANDLER_PLUGIN_BASE_URL', plugin_dir_url(__FILE__));
33+
require_once 'consts.php';
3534

3635
require_once CROWDHANDLER_PLUGIN_BASE_PATH . 'vendor/autoload.php';
3736

uninstall.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@
3232

3333
check_ajax_referer('updates');
3434

35+
36+
require_once 'consts.php';
37+
38+
if (file_exists(CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH)) {
39+
rename(CROWDHANDLER_PLUGIN_INDEX_COPY_FILE_PATH, CROWDHANDLER_PLUGIN_INDEX_FILE_PATH);
40+
}
41+
3542
delete_option('crowdhandler_settings');

0 commit comments

Comments
 (0)