@@ -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}
0 commit comments