PluginProbe
Float menu – awesome floating side menu / trunk
Float menu – awesome floating side menu vtrunk
7.2.5 trunk 2.1 2.2 3.0.1 3.1 3.2.2 3.3.1 3.5 3.5.1 3.5.2 3.5.3. 3.5.4 4.0 4.1 4.1.1 4.2 4.3 4.3.1 4.3.2 5.0 5.0.1 5.0.2 5.0.3 5.1 All 57 releases
float-menu / classes / Admin / Settings.php

Settings.php in Float menu – awesome floating side menu trunk, at classes/Admin/Settings.php

184 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Settings
5 *
6 * Handles the settings functionality for the plugin.
7 *
8 * @package WowPlugin
9 * @subpackage Admin
10 * @author Dmytro Lobov <dev@wow-company.com>, Wow-Company
11 * @copyright 2024 Dmytro Lobov
12 * @license GPL-2.0+
13 */
14
15 namespace FloatMenuLite\Admin;
16
17 defined( 'ABSPATH' ) || exit;
18
19 use FloatMenuLite\WOWP_Plugin;
20
21 class Settings {
22
23 public function __construct() {
24 // add_action('wp_ajax_'.WOWP_Plugin::PREFIX . '_ajax_settings', [$this, 'save_item']);
25 }
26
27 public static function init(): void {
28
29 $pages = DashboardHelper::get_files( 'settings' );
30 $options = self::get_options();
31 $checked = $options['setting_tab'] ?? 1;
32
33 echo '<h3 class="wpie-tabs">';
34 foreach ( $pages as $key => $page ) {
35 $class = ( absint( $checked ) === $key ) ? ' selected' : '';
36 echo '<label class="wpie-tab-label' . esc_attr( $class ) . '" for="setting_tab_' . absint( $key ) . '">' . esc_html( $page['name'] ) . '</label>';
37 }
38 echo '</h3>';
39
40 echo '<div class="wpie-tabs-contents">';
41 foreach ( $pages as $key => $page ) {
42 $file = DashboardHelper::get_folder_path( 'settings' ) . '/' . $key . '.' . $page['file'] . '.php';
43 echo '<input type="radio" class="wpie-tab-toggle" name="param[setting_tab]" value="' . absint( $key ) . '" id="setting_tab_' . absint( $key ) . '" ' . checked( $key, $checked, false ) . '>';
44 if ( file_exists( $file ) ) {
45 echo '<div class="wpie-tab-content">';
46 require_once $file;
47 echo '</div>';
48 }
49 }
50 echo '</div>';
51
52 }
53
54 public static function save_item() {
55 $raw_data = file_get_contents('php://input');
56 $request = json_decode($raw_data, true);
57
58 if (json_last_error() !== JSON_ERROR_NONE) {
59 wp_send_json_error(['message' => 'Invalid JSON']);
60 }
61
62 if (!isset($request['security']) || !wp_verify_nonce($request['security'], WOWP_Plugin::PREFIX . '_settings')) {
63 wp_send_json_error(['message' => 'Invalid nonce'], 400);
64 }
65
66 if ( ! current_user_can( 'manage_options' ) ) {
67 wp_send_json_error( [ 'message' => 'Permission denied' ], 403 );
68 }
69
70 $info = ( isset( $request['info'] ) && is_array( $request['info'] ) ) ? $request['info'] : [];
71
72 $id = isset( $info['tool_id'] ) ? absint( $info['tool_id'] ) : 0;
73
74 $param = ( isset( $info['param'] ) && is_array( $info['param'] ) ) ? $info['param'] : [];
75
76 $settings = apply_filters( WOWP_Plugin::PREFIX . '_save_settings', $param ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
77
78 $data = [
79 'title' => isset( $info['title'] ) ? sanitize_text_field( wp_unslash( $info['title'] ) ) : '',
80 'status' => isset( $info['status'] ) ? 1 : 0,
81 'mode' => isset( $info['mode'] ) ? 1 : 0,
82 'tag' => isset( $info['tag'] ) ? sanitize_text_field( wp_unslash( $info['tag'] ) ) : '',
83 'param' => maybe_serialize( $settings ),
84 ];
85 $formats = [
86 '%s',
87 '%d',
88 '%d',
89 '%s',
90 '%s'
91 ];
92
93 if ( empty( $id ) ) {
94 $id_item = DBManager::insert( $data, $formats );
95 } else {
96 $where = [
97 'id' => absint( $id ),
98 ];
99 DBManager::update( $data, $where, $formats );
100 $id_item = $id;
101 }
102
103 wp_send_json_success(['id' => absint($id_item) ]);
104 exit;
105 // phpcs:enable
106 }
107
108 public static function deactivate_item( $id = 0 ): void {
109 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
110 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
111
112 if ( ! empty( $id ) ) {
113 DBManager::update( [ 'status' => '1' ], [ 'id' => $id ], [ '%d' ] );
114 }
115
116 }
117
118 public static function activate_item( $id = 0 ): void {
119 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
120 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
121
122 if ( ! empty( $id ) ) {
123 DBManager::update( [ 'status' => '' ], [ 'id' => $id ], [ '%d' ] );
124 }
125
126 }
127
128 public static function deactivate_mode( $id = 0 ): void {
129 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
130 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
131
132 if ( ! empty( $id ) ) {
133 DBManager::update( [ 'mode' => '' ], [ 'id' => $id ], [ '%d' ] );
134 }
135
136 }
137
138 public static function activate_mode( $id = 0 ): void {
139 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
140 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
141
142 if ( ! empty( $id ) ) {
143 DBManager::update( [ 'mode' => '1' ], [ 'id' => $id ], [ '%d' ] );
144 }
145 }
146
147 public static function get_options() {
148 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
149 $id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
150
151 if ( empty( $id ) ) {
152 return false;
153 }
154
155 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
156 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : 'update';
157 $result = DBManager::get_data_by_id( $id );
158
159 if ( empty( $result ) ) {
160 return false;
161 }
162
163 $param = ( ! empty( $result->param ) ) ? maybe_unserialize( $result->param ) : [];
164
165 $param['tag'] = $result->tag;
166 $param['status'] = $result->status;
167 $param['mode'] = $result->mode;
168
169 if ( $action === 'duplicate' ) {
170 $param['id'] = '';
171 $param['title'] = '';
172 } else {
173 $param['id'] = $id;
174 $param['title'] = $result->title;
175 }
176
177 return $param;
178 }
179
180 public static function option( $name, $options ) {
181 return $options[ $name ] ?? '';
182 }
183
184 }