| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use WPCoder\WPCoder; |
| 8 |
|
| 9 |
class SaveGlobal { |
| 10 |
|
| 11 |
public static function init(): void { |
| 12 |
self::send(); |
| 13 |
$content = self::get_content(); |
| 14 |
$enabled = get_option( '_wpcoder_enable_php' ); |
| 15 |
?> |
| 16 |
<div class="wrap wowp-wrap wpcoder-global-php"> |
| 17 |
|
| 18 |
<form method="post"> |
| 19 |
<fieldset> |
| 20 |
<legend> |
| 21 |
<?php |
| 22 |
esc_html_e( 'PHP code', 'wpcoder' ); ?> |
| 23 |
</legend> |
| 24 |
<div class="wowp-field is-full"> |
| 25 |
<ol id="phpNavigationMenu" class="wowp-php-nav-menu"></ol> |
| 26 |
<span class="button-editor button" id="phpglobalnav">Add NAV Comment</span> |
| 27 |
</div> |
| 28 |
|
| 29 |
<div class="wowp-field is-full"> |
| 30 |
<textarea name="wp_coder_global_php" id="wpcoder-global-php" cols="40" rows="5"><?php |
| 31 |
echo esc_textarea( $content ); ?></textarea> |
| 32 |
</div> |
| 33 |
<div class="wowp-field"> |
| 34 |
<label> |
| 35 |
<input type="checkbox" value="1" name="wp_coder_global_php_enable" |
| 36 |
id="wpcoder-global-php-enable"<?php |
| 37 |
checked( $enabled ); ?>> |
| 38 |
<?php |
| 39 |
esc_html_e( 'Enable PHP code', 'wpcoder' ); ?> |
| 40 |
</label> |
| 41 |
</div> |
| 42 |
<div class="wowp-field is-full"> |
| 43 |
<?php |
| 44 |
submit_button( __( 'Save', 'wpcoder' ), 'primary large', 'submit', false ); ?> |
| 45 |
</div> |
| 46 |
|
| 47 |
<?php |
| 48 |
wp_nonce_field( WPCoder::PREFIX . '_global_action', WPCoder::PREFIX . '_save_name' ); ?> |
| 49 |
</fieldset> |
| 50 |
</form> |
| 51 |
</div> |
| 52 |
|
| 53 |
<?php |
| 54 |
} |
| 55 |
|
| 56 |
private static function get_content() { |
| 57 |
$content = ''; |
| 58 |
$path_global = FolderManager::path_upload_dir() . 'global-php.php'; |
| 59 |
|
| 60 |
if ( file_exists( $path_global ) ) { |
| 61 |
$content_file = file_get_contents( $path_global ); |
| 62 |
$lines = explode( PHP_EOL, $content_file ); |
| 63 |
$lines = array_slice( $lines, 3 ); |
| 64 |
$content = implode( PHP_EOL, $lines ); |
| 65 |
} |
| 66 |
|
| 67 |
return $content; |
| 68 |
} |
| 69 |
|
| 70 |
private static function send(): void { |
| 71 |
if ( ! self::verify() ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
$checked = ! empty( $_POST['wp_coder_global_php_enable'] ) ? 1 : 0; |
| 75 |
update_option( '_wpcoder_enable_php', $checked ); |
| 76 |
|
| 77 |
$path_global = FolderManager::path_upload_dir() . 'global-php.php'; |
| 78 |
|
| 79 |
if ( isset( $_POST['wp_coder_global_php'] ) ) { |
| 80 |
file_put_contents( $path_global, |
| 81 |
"<?php\ndefined( 'ABSPATH' ) || exit;\n\n" . wp_unslash( $_POST['wp_coder_global_php'] ) ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private static function verify(): bool { |
| 86 |
$wp_coder = $_POST['wp_coder_global_php'] ?? ''; |
| 87 |
$nonce_name = WPCoder::PREFIX . '_save_name'; |
| 88 |
$nonce_action = WPCoder::PREFIX . '_global_action'; |
| 89 |
if ( empty( $_POST[ $nonce_name ] ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
return isset( $wp_coder ) && wp_verify_nonce( $_POST[ $nonce_name ], |
| 94 |
$nonce_action ) && current_user_can( 'unfiltered_html' ); |
| 95 |
} |
| 96 |
} |
| 97 |
|