| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace WPCoder\Snippets; |
| 4 | 4 | |
| 5 | +use WPCoder\Dashboard\Field; | |
| 6 | +use WPCoder\Dashboard\FieldHelper; | |
| 5 | 7 | use WPCoder\WPCoder; |
| 6 | 8 | |
| 7 | 9 | class SnippetManager { |
| 8 | 10 | |
| @@ -8,60 +10,50 @@ | ||
| 8 | 10 | |
| 9 | 11 | public static function init(): void { |
| 10 | 12 | self::send(); |
| 11 | 13 | |
| 14 | + $options = get_option( '_wp_coder_snippets', [] ); | |
| 15 | + | |
| 12 | 16 | $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 | - ], | |
| 17 | + 'content' => __( 'Editor & Content', 'wp-coder' ), | |
| 18 | + 'admin' => __( 'Admin Interface Tweaks', 'wp-coder' ), | |
| 19 | + 'login' => __( 'Login & User Access', 'wp-coder' ), | |
| 20 | + 'media' => __( 'Media & Embeds', 'wp-coder' ), | |
| 21 | + 'core' => __( 'Core Functionality', 'wp-coder' ), | |
| 22 | + 'comments' => __( 'Comments & Feedback', 'wp-coder' ), | |
| 23 | + 'optimization' => __( ' Cleanup & Optimization', 'wp-coder' ), | |
| 25 | 24 | ]; |
| 26 | 25 | |
| 27 | 26 | ?> |
| 28 | - <div class="wrap wowp-wrap wpcoder-snippets-wrap wowp-settings"> | |
| 27 | + <div class="wowp-settings wowp-snippets"> | |
| 29 | 28 | <form method="post"> |
| 30 | - <fieldset style="display: block;"> | |
| 31 | - <legend> | |
| 32 | - <?php esc_html_e( 'Snippets', 'wpcoder' ); ?> | |
| 33 | - </legend> | |
| 34 | 29 | |
| 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 | 30 | |
| 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> | |
| 31 | + <div class="wowp-snippets__tabs" id="wowp-snippets-tabs"> | |
| 32 | + <?php | |
| 33 | + foreach ( $snippets as $key => $value ) { | |
| 34 | + $tab = ! empty( $options['snippet_tab'] ) ? $options['snippet_tab'] : 'content'; | |
| 35 | + echo '<input type="radio" class="wowp-snippets__tabs-radio" name="wp_coder_snippet[snippet_tab]" value="' . esc_attr( $key ) . '" id="wowp-tab-' . esc_attr( $key ) . '" ' . checked( $tab, $key, false ) . '>'; | |
| 36 | + } | |
| 37 | + echo '<div class="wowp-snippets__tabs-labels">'; | |
| 38 | + foreach ( $snippets as $key => $value ) { | |
| 39 | + echo '<label class="wowp-snippets__tabs-tab" for="wowp-tab-' . esc_attr( $key ) . '"><span class="icon icon-' . esc_attr( $key ) . '"></span>' . esc_html( $value ) . '</label>'; | |
| 40 | + } | |
| 41 | + echo '</div>'; | |
| 42 | + foreach ( $snippets as $key => $value ) { | |
| 55 | 43 | |
| 56 | - <div class="wowp-field is-full has-mt"> | |
| 57 | - <?php | |
| 58 | - submit_button( __( 'Save', 'wpcoder' ), 'primary large', 'submit', false ); ?> | |
| 59 | - </div> | |
| 44 | + echo '<div class="wowp-snippets__tabs-content" data-content="wowp-tab-' . esc_attr( $key ) . '">'; | |
| 45 | + require_once plugin_dir_path( __FILE__ ) . 'pages/' . esc_attr( $key ) . '.php'; | |
| 46 | + echo '<input type="submit" name="submit" class="wowp-button button button-dark button-hero" value="' . esc_attr__( 'Save', 'wp-coder' ) . '">'; | |
| 47 | + echo '</div>'; | |
| 48 | + } | |
| 60 | 49 | |
| 61 | - <?php | |
| 62 | - wp_nonce_field( WPCoder::PREFIX . '_snippets_action', WPCoder::PREFIX . '_save_snippet' ); ?> | |
| 63 | - </fieldset> | |
| 50 | + ?> | |
| 51 | + </div> | |
| 52 | + <?php | |
| 53 | + // submit_button( __( 'Save', 'wp-coder' ), 'wowp-button button-dark button-hero', 'submit', false ); | |
| 54 | + wp_nonce_field( WPCoder::PREFIX . '_snippets_action', WPCoder::PREFIX . '_save_snippet' ); | |
| 55 | + ?> | |
| 64 | 56 | </form> |
| 65 | 57 | |
| 66 | 58 | </div> |
| 67 | 59 | <?php |
| @@ -68,22 +60,39 @@ | ||
| 68 | 60 | } |
| 69 | 61 | |
| 70 | 62 | private static function create_options( $settings ) { |
| 71 | 63 | $options = get_option( '_wp_coder_snippets', [] ); |
| 72 | - echo '<dl class="wowp-snippet__list">'; | |
| 64 | + echo '<div class="wowp-snippet__list">'; | |
| 65 | + $i = 1; | |
| 73 | 66 | 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>'; | |
| 67 | + $checked = array_key_exists( $name, $options ) ? 'checked' : ''; ?> | |
| 68 | + <div class="wowp-snippet__item"> | |
| 69 | + <div class="wowp-snippet__item-header"> | |
| 70 | + <label for="wp_coder_snippet-<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $args[0] ); ?></label> | |
| 71 | + <p class="wowp-snippet__item-description"><?php echo esc_html( $args[1] ); ?></p> | |
| 72 | + </div> | |
| 73 | + <div class="wowp-field has-checkbox"> | |
| 74 | + <label class="switch"> | |
| 75 | + <input type="checkbox" | |
| 76 | + name="wp_coder_snippet[<?php echo esc_attr( $name ); ?>]" <?php echo esc_attr( $checked ); ?> | |
| 77 | + value="1" id="wp_coder_snippet-<?php echo esc_attr( $name ); ?>"> | |
| 78 | + <span class="slider"></span> | |
| 79 | + </label> | |
| 80 | + </div> | |
| 81 | + </div> | |
| 82 | + <?php | |
| 83 | + $i ++; | |
| 77 | 84 | } |
| 78 | - echo '</dl>'; | |
| 85 | + echo '</div>'; | |
| 79 | 86 | } |
| 80 | 87 | |
| 88 | + | |
| 81 | 89 | private static function field( $type = 'checkbox', $name = '', $def = '', $placeholder = '' ) { |
| 82 | 90 | $options = get_option( '_wp_coder_snippets', [] ); |
| 83 | 91 | if ( $type === 'checkbox' ) { |
| 84 | 92 | $checked = array_key_exists( $name, $options ) ? 'checked' : ''; |
| 85 | - echo '<input type="checkbox" name="wp_coder_snippet[' . esc_attr( $name ) . ']" ' . esc_attr( $checked ) . ' value="1">'; | |
| 93 | + echo '<input type="checkbox" name="wp_coder_snippet[' . esc_attr( $name ) . ']" ' . esc_attr( $checked ) . ' value="1" id="' . esc_attr( $name ) . '">'; | |
| 94 | + echo ''; | |
| 86 | 95 | } elseif ( $type === 'text' ) { |
| 87 | 96 | $value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def; |
| 88 | 97 | echo '<input type="text" name="wp_coder_snippet[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '">'; |
| 89 | 98 | } elseif ( $type === 'textarea' ) { |
| @@ -102,9 +111,9 @@ | ||
| 102 | 111 | |
| 103 | 112 | if ( empty( $_POST['wp_coder_snippet'] ) ) { |
| 104 | 113 | $snippets = []; |
| 105 | 114 | } else { |
| 106 | - $snippets = map_deep( $_POST['wp_coder_snippet'], 'sanitize_text_field' ); | |
| 115 | + $snippets = map_deep( wp_unslash( $_POST['wp_coder_snippet'] ), 'sanitize_text_field' ); | |
| 107 | 116 | } |
| 108 | 117 | |
| 109 | 118 | update_option( '_wp_coder_snippets', $snippets ); |
| 110 | 119 | } |
| @@ -116,9 +125,9 @@ | ||
| 116 | 125 | if ( empty( $_POST[ $nonce_name ] ) ) { |
| 117 | 126 | return false; |
| 118 | 127 | } |
| 119 | 128 | |
| 120 | - return isset( $wp_coder ) && wp_verify_nonce( $_POST[ $nonce_name ], | |
| 129 | + return isset( $_POST['wp_coder_snippet'] ) && wp_verify_nonce( $_POST[ $nonce_name ], | |
| 121 | 130 | $nonce_action ) && current_user_can( 'unfiltered_html' ); |
| 122 | 131 | } |
| 123 | 132 | |
| 124 | 133 | } |