| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Snippets; |
| 4 |
|
| 5 |
use WPCoder\WPCoder; |
| 6 |
|
| 7 |
class SnippetManager { |
| 8 |
|
| 9 |
public static function init(): void { |
| 10 |
self::send(); |
| 11 |
|
| 12 |
$snippets = [ |
| 13 |
'disable' => [ |
| 14 |
'html-code', |
| 15 |
__( 'Disable snippets', 'wpcoder' ) |
| 16 |
], |
| 17 |
'enable' => [ |
| 18 |
'css-code', |
| 19 |
__( 'Enabled snippets', 'wpcoder' ) |
| 20 |
], |
| 21 |
'change' => [ |
| 22 |
'js-code', |
| 23 |
__( 'Changed snippets', 'wpcoder' ) |
| 24 |
], |
| 25 |
]; |
| 26 |
|
| 27 |
?> |
| 28 |
<div class="wrap wowp-wrap wpcoder-snippets-wrap wowp-settings"> |
| 29 |
<form method="post"> |
| 30 |
<fieldset style="display: block;"> |
| 31 |
<legend> |
| 32 |
<?php esc_html_e( 'Snippets', 'wpcoder' ); ?> |
| 33 |
</legend> |
| 34 |
|
| 35 |
<h3 class="nav-tab-wrapper wowp-tab" id="settings-tab"> |
| 36 |
<?php |
| 37 |
foreach ( $snippets as $page => $args ) { |
| 38 |
$first = ( $page === array_key_first( $snippets ) ) ? ' nav-tab-active' : ''; |
| 39 |
echo '<a class="nav-tab' . esc_attr( $first ) . '" data-tab="' . esc_attr( $args[0] ) . '">' . esc_attr( $args[1] ) . '</a>'; |
| 40 |
} |
| 41 |
?> |
| 42 |
</h3> |
| 43 |
|
| 44 |
<div class="tab-content-wrapper wowp-tab-content" id="settings-content"> |
| 45 |
<?php |
| 46 |
foreach ( $snippets as $page => $args ) { |
| 47 |
$first = ( $page === array_key_first( $snippets ) ) ? ' tab-content-active' : ''; |
| 48 |
echo '<div class="tab-content' . esc_attr( $first ) . '" data-content="' . esc_attr( $args[0] ) . '">'; |
| 49 |
echo '<h4>' . esc_attr( $args[1] ) . '</h4>'; |
| 50 |
require_once plugin_dir_path( __FILE__ ) . 'pages/' . esc_attr( $page ) . '.php'; |
| 51 |
echo '</div>'; |
| 52 |
} |
| 53 |
?> |
| 54 |
</div> |
| 55 |
|
| 56 |
<div class="wowp-field is-full has-mt"> |
| 57 |
<?php |
| 58 |
submit_button( __( 'Save', 'wpcoder' ), 'primary large', 'submit', false ); ?> |
| 59 |
</div> |
| 60 |
|
| 61 |
<?php |
| 62 |
wp_nonce_field( WPCoder::PREFIX . '_snippets_action', WPCoder::PREFIX . '_save_snippet' ); ?> |
| 63 |
</fieldset> |
| 64 |
</form> |
| 65 |
|
| 66 |
</div> |
| 67 |
<?php |
| 68 |
} |
| 69 |
|
| 70 |
private static function create_options( $settings ) { |
| 71 |
$options = get_option( '_wp_coder_snippets', [] ); |
| 72 |
echo '<dl class="wowp-snippet__list">'; |
| 73 |
foreach ( $settings as $name => $args ) { |
| 74 |
$checked = array_key_exists( $name, $options ) ? 'checked' : ''; |
| 75 |
echo '<dt><label><input type="checkbox" name="wp_coder_snippet[' . esc_attr( $name ) . ']" ' . esc_attr( $checked ) . ' value="1">' . esc_attr( $args[0] ) . '</label></dt>'; |
| 76 |
echo '<dd>' . esc_attr( $args[1] ) . '</dd>'; |
| 77 |
} |
| 78 |
echo '</dl>'; |
| 79 |
} |
| 80 |
|
| 81 |
private static function field( $type = 'checkbox', $name = '', $def = '', $placeholder = '' ) { |
| 82 |
$options = get_option( '_wp_coder_snippets', [] ); |
| 83 |
if ( $type === 'checkbox' ) { |
| 84 |
$checked = array_key_exists( $name, $options ) ? 'checked' : ''; |
| 85 |
echo '<input type="checkbox" name="wp_coder_snippet[' . esc_attr( $name ) . ']" ' . esc_attr( $checked ) . ' value="1">'; |
| 86 |
} elseif ( $type === 'text' ) { |
| 87 |
$value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 88 |
echo '<input type="text" name="wp_coder_snippet[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '">'; |
| 89 |
} elseif ( $type === 'textarea' ) { |
| 90 |
$value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 91 |
echo '<textarea name="wp_coder_snippet[' . esc_attr( $name ) . ']" placeholder="' . esc_attr( $placeholder ) . '">' . esc_attr( $value ) . '</textarea>'; |
| 92 |
} elseif ( $type === 'number' ) { |
| 93 |
$value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 94 |
echo '<input type="number" name="wp_coder_snippet[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '">'; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private static function send(): void { |
| 99 |
if ( ! self::verify() ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
if ( empty( $_POST['wp_coder_snippet'] ) ) { |
| 104 |
$snippets = []; |
| 105 |
} else { |
| 106 |
$snippets = map_deep( $_POST['wp_coder_snippet'], 'sanitize_text_field' ); |
| 107 |
} |
| 108 |
|
| 109 |
update_option( '_wp_coder_snippets', $snippets ); |
| 110 |
} |
| 111 |
|
| 112 |
private static function verify(): bool { |
| 113 |
$wp_coder = $_POST['wp_coder_snippet'] ?? ''; |
| 114 |
$nonce_name = WPCoder::PREFIX . '_save_snippet'; |
| 115 |
$nonce_action = WPCoder::PREFIX . '_snippets_action'; |
| 116 |
if ( empty( $_POST[ $nonce_name ] ) ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
return isset( $wp_coder ) && wp_verify_nonce( $_POST[ $nonce_name ], |
| 121 |
$nonce_action ) && current_user_can( 'manage_options' ); |
| 122 |
} |
| 123 |
|
| 124 |
} |