-
Notifications
You must be signed in to change notification settings - Fork 29
/
actions.php
executable file
·190 lines (142 loc) · 5.4 KB
/
actions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Get icons from admin ajax
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! function_exists( 'ulf_get_icons' ) ) {
function ulf_get_icons() {
$nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'ulf_icon_nonce' ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'ulf' ) ) );
}
ob_start();
$icon_library = ( apply_filters( 'ulf_fa4', false ) ) ? 'fa4' : 'fa5';
ULF::include_plugin_file( 'fields/icon/'. $icon_library .'-icons.php' );
$icon_lists = apply_filters( 'ulf_field_icon_add_icons', ulf_get_default_icons() );
if ( ! empty( $icon_lists ) ) {
foreach ( $icon_lists as $list ) {
echo ( count( $icon_lists ) >= 2 ) ? '<div class="ulf-icon-title">'. esc_attr( $list['title'] ) .'</div>' : '';
foreach ( $list['icons'] as $icon ) {
echo '<i title="'. esc_attr( $icon ) .'" class="'. esc_attr( $icon ) .'"></i>';
}
}
} else {
echo '<div class="ulf-error-text">'. esc_html__( 'No data available.', 'ulf' ) .'</div>';
}
$content = ob_get_clean();
wp_send_json_success( array( 'content' => $content ) );
}
add_action( 'wp_ajax_ulf-get-icons', 'ulf_get_icons' );
}
/**
*
* Export
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! function_exists( 'ulf_export' ) ) {
function ulf_export() {
$nonce = ( ! empty( $_GET[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_GET[ 'nonce' ] ) ) : '';
$unique = ( ! empty( $_GET[ 'unique' ] ) ) ? sanitize_text_field( wp_unslash( $_GET[ 'unique' ] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'ulf_backup_nonce' ) ) {
die( esc_html__( 'Error: Invalid nonce verification.', 'ulf' ) );
}
if ( empty( $unique ) ) {
die( esc_html__( 'Error: Invalid key.', 'ulf' ) );
}
// Export
header('Content-Type: application/json');
header('Content-disposition: attachment; filename=backup-'. gmdate( 'd-m-Y' ) .'.json');
header('Content-Transfer-Encoding: binary');
header('Pragma: no-cache');
header('Expires: 0');
echo json_encode( get_option( $unique ) );
die();
}
add_action( 'wp_ajax_ulf-export', 'ulf_export' );
}
/**
*
* Import Ajax
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! function_exists( 'ulf_import_ajax' ) ) {
function ulf_import_ajax() {
$nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
$unique = ( ! empty( $_POST[ 'unique' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'unique' ] ) ) : '';
$data = ( ! empty( $_POST[ 'data' ] ) ) ? wp_kses_post_deep( json_decode( wp_unslash( trim( $_POST[ 'data' ] ) ), true ) ) : array();
if ( ! wp_verify_nonce( $nonce, 'ulf_backup_nonce' ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'ulf' ) ) );
}
if ( empty( $unique ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid key.', 'ulf' ) ) );
}
if ( empty( $data ) || ! is_array( $data ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: The response is not a valid JSON response.', 'ulf' ) ) );
}
// Success
update_option( $unique, $data );
wp_send_json_success();
}
add_action( 'wp_ajax_ulf-import', 'ulf_import_ajax' );
}
/**
*
* Reset Ajax
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! function_exists( 'ulf_reset_ajax' ) ) {
function ulf_reset_ajax() {
$nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
$unique = ( ! empty( $_POST[ 'unique' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'unique' ] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'ulf_backup_nonce' ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'ulf' ) ) );
}
// Success
delete_option( $unique );
wp_send_json_success();
}
add_action( 'wp_ajax_ulf-reset', 'ulf_reset_ajax' );
}
/**
*
* Chosen Ajax
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! function_exists( 'ulf_chosen_ajax' ) ) {
function ulf_chosen_ajax() {
$nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
$type = ( ! empty( $_POST[ 'type' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'type' ] ) ) : '';
$term = ( ! empty( $_POST[ 'term' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'term' ] ) ) : '';
$query = ( ! empty( $_POST[ 'query_args' ] ) ) ? wp_kses_post_deep( $_POST[ 'query_args' ] ) : array();
if ( ! wp_verify_nonce( $nonce, 'ulf_chosen_ajax_nonce' ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'ulf' ) ) );
}
if ( empty( $type ) || empty( $term ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid term ID.', 'ulf' ) ) );
}
$capability = apply_filters( 'ulf_chosen_ajax_capability', 'manage_options' );
if ( ! current_user_can( $capability ) ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error: You do not have permission to do that.', 'ulf' ) ) );
}
// Success
$options = ULF_Fields::field_data( $type, $term, $query );
wp_send_json_success( $options );
}
add_action( 'wp_ajax_ulf-chosen', 'ulf_chosen_ajax' );
}