PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.5
WP Coder – Insert & Manage Code Snippets v4.5
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Snippets / SnippetManager.php

SnippetManager.php in WP Coder – Insert & Manage Code Snippets 4.5, at classes/Snippets/SnippetManager.php

133 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPCoder\Snippets;
4
5 use WPCoder\Dashboard\Field;
6 use WPCoder\Dashboard\FieldHelper;
7 use WPCoder\WPCoder;
8
9 class SnippetManager {
10
11 public static function init(): void {
12 self::send();
13
14 $options = get_option( '_wp_coder_snippets', [] );
15
16 $snippets = [
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' ),
24 ];
25
26 ?>
27 <div class="wowp-settings wowp-snippets">
28 <form method="post">
29
30
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 ) {
43
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 }
49
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 ?>
56 </form>
57
58 </div>
59 <?php
60 }
61
62 private static function create_options( $settings ) {
63 $options = get_option( '_wp_coder_snippets', [] );
64 echo '<div class="wowp-snippet__list">';
65 $i = 1;
66 foreach ( $settings as $name => $args ) {
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 ++;
84 }
85 echo '</div>';
86 }
87
88
89 private static function field( $type = 'checkbox', $name = '', $def = '', $placeholder = '' ) {
90 $options = get_option( '_wp_coder_snippets', [] );
91 if ( $type === 'checkbox' ) {
92 $checked = array_key_exists( $name, $options ) ? 'checked' : '';
93 echo '<input type="checkbox" name="wp_coder_snippet[' . esc_attr( $name ) . ']" ' . esc_attr( $checked ) . ' value="1" id="' . esc_attr( $name ) . '">';
94 echo '';
95 } elseif ( $type === 'text' ) {
96 $value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def;
97 echo '<input type="text" name="wp_coder_snippet[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '">';
98 } elseif ( $type === 'textarea' ) {
99 $value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def;
100 echo '<textarea name="wp_coder_snippet[' . esc_attr( $name ) . ']" placeholder="' . esc_attr( $placeholder ) . '">' . esc_attr( $value ) . '</textarea>';
101 } elseif ( $type === 'number' ) {
102 $value = ! empty( $options[ $name ] ) ? $options[ $name ] : $def;
103 echo '<input type="number" name="wp_coder_snippet[' . esc_attr( $name ) . ']" value="' . esc_attr( $value ) . '" placeholder="' . esc_attr( $placeholder ) . '">';
104 }
105 }
106
107 private static function send(): void {
108 if ( ! self::verify() ) {
109 return;
110 }
111
112 if ( empty( $_POST['wp_coder_snippet'] ) ) {
113 $snippets = [];
114 } else {
115 $snippets = map_deep( wp_unslash( $_POST['wp_coder_snippet'] ), 'sanitize_text_field' );
116 }
117
118 update_option( '_wp_coder_snippets', $snippets );
119 }
120
121 private static function verify(): bool {
122 $wp_coder = $_POST['wp_coder_snippet'] ?? '';
123 $nonce_name = WPCoder::PREFIX . '_save_snippet';
124 $nonce_action = WPCoder::PREFIX . '_snippets_action';
125 if ( empty( $_POST[ $nonce_name ] ) ) {
126 return false;
127 }
128
129 return isset( $_POST['wp_coder_snippet'] ) && wp_verify_nonce( $_POST[ $nonce_name ],
130 $nonce_action ) && current_user_can( 'unfiltered_html' );
131 }
132
133 }