| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Tools; |
| 4 |
|
| 5 |
use WPCoder\WPCoder; |
| 6 |
|
| 7 |
class ToolsManager { |
| 8 |
|
| 9 |
public static function init(): void { |
| 10 |
self::send(); |
| 11 |
|
| 12 |
?> |
| 13 |
<div class="wrap wowp-wrap wpcoder-tools-wrap wowp-settings"> |
| 14 |
<form method="post"> |
| 15 |
<fieldset style="display: block;"> |
| 16 |
<legend> |
| 17 |
<?php esc_html_e( 'Tools', 'wpcoder' ); ?> |
| 18 |
</legend> |
| 19 |
<div class="tab-content-wrapper wowp-tab-content"> |
| 20 |
<?php require_once plugin_dir_path( __FILE__ ) . '/page.php'; ?> |
| 21 |
</div> |
| 22 |
<div class="wowp-field is-full has-mt"> |
| 23 |
<?php |
| 24 |
submit_button( __( 'Save', 'wpcoder' ), 'primary large', 'submit', false ); ?> |
| 25 |
</div> |
| 26 |
|
| 27 |
<?php |
| 28 |
wp_nonce_field( WPCoder::PREFIX . '_tools_action', WPCoder::PREFIX . '_save_tools' ); ?> |
| 29 |
</fieldset> |
| 30 |
</form> |
| 31 |
</div> |
| 32 |
<?php |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
private static function option( $name = '', $def = '' ) { |
| 37 |
$options = get_option( '_wp_coder_tools', [] ); |
| 38 |
$res = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 39 |
|
| 40 |
return $res; |
| 41 |
} |
| 42 |
|
| 43 |
private static function expand( $name ) { |
| 44 |
$res = self::option( $name ); |
| 45 |
if ( ! empty( $res ) ) { |
| 46 |
echo ' open'; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
private static function field( $type = 'checkbox', $name = '', $def = '', $placeholder = '' ) { |
| 51 |
$options = get_option( '_wp_coder_tools', [] ); |
| 52 |
if ( $type === 'checkbox' ) { |
| 53 |
$checked = array_key_exists( $name, $options ) ? 'checked' : ''; |
| 54 |
echo '<input type="checkbox" name="wp_coder_tool[' . esc_attr( $name ) . ']" ' . esc_attr( $checked ) . ' value="1">'; |
| 55 |
} elseif ( $type === 'textarea' ) { |
| 56 |
$value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 57 |
echo '<textarea name="wp_coder_tool[' . esc_attr( $name ) . ']" placeholder="' . esc_attr( $placeholder ) . '">' . esc_attr( $value ) . '</textarea>'; |
| 58 |
} else { |
| 59 |
$value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 60 |
echo '<input type="' . esc_attr( $type ) . '" name="wp_coder_tool[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '">'; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private static function send(): void { |
| 65 |
if ( ! self::verify() ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
if ( empty( $_POST['wp_coder_tool'] ) ) { |
| 70 |
$snippets = []; |
| 71 |
} else { |
| 72 |
$snippets = map_deep( $_POST['wp_coder_tool'], 'sanitize_text_field' ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
update_option( '_wp_coder_tools', $snippets ); |
| 77 |
} |
| 78 |
|
| 79 |
private static function verify(): bool { |
| 80 |
$wp_coder = $_POST['wp_coder_tool'] ?? ''; |
| 81 |
$nonce_name = WPCoder::PREFIX . '_save_tools'; |
| 82 |
$nonce_action = WPCoder::PREFIX . '_tools_action'; |
| 83 |
if ( empty( $_POST[ $nonce_name ] ) ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
return wp_verify_nonce( $_POST[ $nonce_name ], |
| 88 |
$nonce_action ) && current_user_can( 'unfiltered_html' ); |
| 89 |
} |
| 90 |
} |