| 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 FloatingButton\Admin; |
| 16 |
|
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
use FloatingButton\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 |
$info = $request['info']; |
| 67 |
|
| 68 |
|
| 69 |
$id = isset( $info['tool_id'] ) ? absint( $info['tool_id'] ) : 0; |
| 70 |
|
| 71 |
$settings = apply_filters( WOWP_Plugin::PREFIX . '_save_settings', $info['param'] ); |
| 72 |
|
| 73 |
$data = [ |
| 74 |
'title' => isset( $info['title'] ) ? sanitize_text_field( wp_unslash( $info['title'] ) ) : '', |
| 75 |
'status' => isset( $info['status'] ) ? 1 : 0, |
| 76 |
'mode' => isset( $info['mode'] ) ? 1 : 0, |
| 77 |
'tag' => isset( $info['tag'] ) ? sanitize_text_field( wp_unslash( $info['tag'] ) ) : '', |
| 78 |
'param' => maybe_serialize( $settings ), |
| 79 |
]; |
| 80 |
$formats = [ |
| 81 |
'%s', |
| 82 |
'%d', |
| 83 |
'%d', |
| 84 |
'%s', |
| 85 |
'%s' |
| 86 |
]; |
| 87 |
|
| 88 |
if ( empty( $id ) ) { |
| 89 |
$id_item = DBManager::insert( $data, $formats ); |
| 90 |
} else { |
| 91 |
$where = [ |
| 92 |
'id' => absint( $id ), |
| 93 |
]; |
| 94 |
DBManager::update( $data, $where, $formats ); |
| 95 |
$id_item = $id; |
| 96 |
} |
| 97 |
|
| 98 |
wp_send_json_success(['id' => absint($id_item) ]); |
| 99 |
exit; |
| 100 |
// phpcs:enable |
| 101 |
} |
| 102 |
|
| 103 |
public static function deactivate_item( $id = 0 ): void { |
| 104 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 105 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id; |
| 106 |
|
| 107 |
if ( ! empty( $id ) ) { |
| 108 |
DBManager::update( [ 'status' => '1' ], [ 'ID' => $id ], [ '%d' ] ); |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
public static function activate_item( $id = 0 ): void { |
| 114 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 115 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id; |
| 116 |
|
| 117 |
if ( ! empty( $id ) ) { |
| 118 |
DBManager::update( [ 'status' => '' ], [ 'ID' => $id ], [ '%d' ] ); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
public static function deactivate_mode( $id = 0 ): void { |
| 124 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 125 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id; |
| 126 |
|
| 127 |
if ( ! empty( $id ) ) { |
| 128 |
DBManager::update( [ 'mode' => '' ], [ 'ID' => $id ], [ '%d' ] ); |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
public static function activate_mode( $id = 0 ): void { |
| 134 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 135 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id; |
| 136 |
|
| 137 |
if ( ! empty( $id ) ) { |
| 138 |
DBManager::update( [ 'mode' => '1' ], [ 'ID' => $id ], [ '%d' ] ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public static function get_options() { |
| 143 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 144 |
$id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0; |
| 145 |
|
| 146 |
if ( empty( $id ) ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 151 |
$action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : 'update'; |
| 152 |
$result = DBManager::get_data_by_id( $id ); |
| 153 |
|
| 154 |
if ( empty( $result ) ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
$param = ( ! empty( $result->param ) ) ? maybe_unserialize( $result->param ) : []; |
| 159 |
|
| 160 |
$param['tag'] = $result->tag; |
| 161 |
$param['status'] = $result->status; |
| 162 |
$param['mode'] = $result->mode; |
| 163 |
|
| 164 |
if ( $action === 'duplicate' ) { |
| 165 |
$param['id'] = ''; |
| 166 |
$param['title'] = ''; |
| 167 |
} else { |
| 168 |
$param['id'] = $id; |
| 169 |
$param['title'] = $result->title; |
| 170 |
} |
| 171 |
|
| 172 |
return $param; |
| 173 |
} |
| 174 |
|
| 175 |
public static function option( $name, $options ) { |
| 176 |
return $options[ $name ] ?? ''; |
| 177 |
} |
| 178 |
|
| 179 |
} |