actions.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
  2. /**
  3. *
  4. * Get icons from admin ajax
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. if ( ! function_exists( 'csf_get_icons' ) ) {
  11. function csf_get_icons() {
  12. $nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
  13. if ( ! wp_verify_nonce( $nonce, 'csf_icon_nonce' ) ) {
  14. wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'sakurairo_csf' ) ) );
  15. }
  16. ob_start();
  17. $icon_library = ( apply_filters( 'csf_fa4', false ) ) ? 'fa4' : 'fa5';
  18. Sakurairo_CSF::include_plugin_file( 'fields/icon/'. $icon_library .'-icons.php' );
  19. $icon_lists = apply_filters( 'csf_field_icon_add_icons', csf_get_default_icons() );
  20. if ( ! empty( $icon_lists ) ) {
  21. foreach ( $icon_lists as $list ) {
  22. echo ( count( $icon_lists ) >= 2 ) ? '<div class="csf-icon-title">'. esc_attr( $list['title'] ) .'</div>' : '';
  23. foreach ( $list['icons'] as $icon ) {
  24. echo '<i title="'. esc_attr( $icon ) .'" class="'. esc_attr( $icon ) .'"></i>';
  25. }
  26. }
  27. } else {
  28. echo '<div class="csf-error-text">'. esc_html__( 'No data available.', 'sakurairo_csf' ) .'</div>';
  29. }
  30. $content = ob_get_clean();
  31. wp_send_json_success( array( 'content' => $content ) );
  32. }
  33. add_action( 'wp_ajax_csf-get-icons', 'csf_get_icons' );
  34. }
  35. /**
  36. *
  37. * Export
  38. *
  39. * @since 1.0.0
  40. * @version 1.0.0
  41. *
  42. */
  43. if ( ! function_exists( 'csf_export' ) ) {
  44. function csf_export() {
  45. $nonce = ( ! empty( $_GET[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_GET[ 'nonce' ] ) ) : '';
  46. $unique = ( ! empty( $_GET[ 'unique' ] ) ) ? sanitize_text_field( wp_unslash( $_GET[ 'unique' ] ) ) : '';
  47. if ( ! wp_verify_nonce( $nonce, 'csf_backup_nonce' ) ) {
  48. die( esc_html__( 'Error: Invalid nonce verification.', 'sakurairo_csf' ) );
  49. }
  50. if ( empty( $unique ) ) {
  51. die( esc_html__( 'Error: Invalid key.', 'sakurairo_csf' ) );
  52. }
  53. // Export
  54. header('Content-Type: application/json');
  55. header('Content-disposition: attachment; filename=backup-'. gmdate( 'd-m-Y' ) .'.json');
  56. header('Content-Transfer-Encoding: binary');
  57. header('Pragma: no-cache');
  58. header('Expires: 0');
  59. echo json_encode( get_option( $unique ) );
  60. die();
  61. }
  62. add_action( 'wp_ajax_csf-export', 'csf_export' );
  63. }
  64. /**
  65. *
  66. * Import Ajax
  67. *
  68. * @since 1.0.0
  69. * @version 1.0.0
  70. *
  71. */
  72. if ( ! function_exists( 'csf_import_ajax' ) ) {
  73. function csf_import_ajax() {
  74. $nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
  75. $unique = ( ! empty( $_POST[ 'unique' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'unique' ] ) ) : '';
  76. $data = ( ! empty( $_POST[ 'data' ] ) ) ? wp_kses_post_deep( json_decode( wp_unslash( trim( $_POST[ 'data' ] ) ), true ) ) : array();
  77. if ( ! wp_verify_nonce( $nonce, 'csf_backup_nonce' ) ) {
  78. wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'sakurairo_csf' ) ) );
  79. }
  80. if ( empty( $unique ) ) {
  81. wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid key.', 'sakurairo_csf' ) ) );
  82. }
  83. if ( empty( $data ) || ! is_array( $data ) ) {
  84. wp_send_json_error( array( 'error' => esc_html__( 'Error: The response is not a valid JSON response.', 'sakurairo_csf' ) ) );
  85. }
  86. // Success
  87. update_option( $unique, $data );
  88. wp_send_json_success();
  89. }
  90. add_action( 'wp_ajax_csf-import', 'csf_import_ajax' );
  91. }
  92. /**
  93. *
  94. * Reset Ajax
  95. *
  96. * @since 1.0.0
  97. * @version 1.0.0
  98. *
  99. */
  100. if ( ! function_exists( 'csf_reset_ajax' ) ) {
  101. function csf_reset_ajax() {
  102. $nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
  103. $unique = ( ! empty( $_POST[ 'unique' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'unique' ] ) ) : '';
  104. if ( ! wp_verify_nonce( $nonce, 'csf_backup_nonce' ) ) {
  105. wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'sakurairo_csf' ) ) );
  106. }
  107. // Success
  108. delete_option( $unique );
  109. wp_send_json_success();
  110. }
  111. add_action( 'wp_ajax_csf-reset', 'csf_reset_ajax' );
  112. }
  113. /**
  114. *
  115. * Chosen Ajax
  116. *
  117. * @since 1.0.0
  118. * @version 1.0.0
  119. *
  120. */
  121. if ( ! function_exists( 'csf_chosen_ajax' ) ) {
  122. function csf_chosen_ajax() {
  123. $nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
  124. $type = ( ! empty( $_POST[ 'type' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'type' ] ) ) : '';
  125. $term = ( ! empty( $_POST[ 'term' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'term' ] ) ) : '';
  126. $query = ( ! empty( $_POST[ 'query_args' ] ) ) ? wp_kses_post_deep( $_POST[ 'query_args' ] ) : array();
  127. if ( ! wp_verify_nonce( $nonce, 'csf_chosen_ajax_nonce' ) ) {
  128. wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid nonce verification.', 'sakurairo_csf' ) ) );
  129. }
  130. if ( empty( $type ) || empty( $term ) ) {
  131. wp_send_json_error( array( 'error' => esc_html__( 'Error: Invalid term ID.', 'sakurairo_csf' ) ) );
  132. }
  133. $capability = apply_filters( 'csf_chosen_ajax_capability', 'manage_options' );
  134. if ( ! current_user_can( $capability ) ) {
  135. wp_send_json_error( array( 'error' => esc_html__( 'Error: You do not have permission to do that.', 'sakurairo_csf' ) ) );
  136. }
  137. // Success
  138. $options = CSF_Fields::field_data( $type, $term, $query );
  139. wp_send_json_success( $options );
  140. }
  141. add_action( 'wp_ajax_csf-chosen', 'csf_chosen_ajax' );
  142. }