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