configs
1 year ago
customize
1 year ago
views
5 months ago
class-everest-forms-style-customizer.php
1 year ago
class-evf-style-customizer-ajax.php
1 year ago
class-evf-style-customizer-api.php
1 year ago
functions.php
1 year ago
class-evf-style-customizer-ajax.php
156 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Style Customizer Ajax |
| 4 | * |
| 5 | * @package EverestForms_Style_Customizer |
| 6 | * @since 1.0.5 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Main EverestForms Style Customizer Ajax Class. |
| 13 | * |
| 14 | * @class EVF_Style_Customizer_Ajax |
| 15 | */ |
| 16 | final class EVF_Style_Customizer_Ajax { |
| 17 | |
| 18 | /** |
| 19 | * Constructor. |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | add_action( 'wp_ajax_save_template', array( $this, 'save_template' ) ); |
| 23 | add_action( 'wp_ajax_delete_template', array( $this, 'delete_template' ) ); |
| 24 | add_action( 'wp_ajax_save_custom_color_palette', array( $this, 'evf_save_custom_color_palette' ) ); |
| 25 | |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Handle AJAX request to save custom color palette. |
| 30 | */ |
| 31 | public function evf_save_custom_color_palette() { |
| 32 | $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_nonce'] ) ) : ''; |
| 33 | |
| 34 | if ( ! wp_verify_nonce( $nonce, 'color_palette' ) ) { |
| 35 | wp_send_json_error( __( 'Nonce error. Please refresh the page.', 'everest-forms' ) ); |
| 36 | exit; |
| 37 | } |
| 38 | |
| 39 | $label = isset( $_POST['label'] ) ? sanitize_text_field( $_POST['label'] ) : ''; |
| 40 | $colors = isset( $_POST['colors'] ) ? $_POST['colors'] : array(); |
| 41 | |
| 42 | if ( empty( $colors ) ) { |
| 43 | wp_send_json_error( __( 'Colors array is empty.', 'everest-forms' ) ); |
| 44 | exit; |
| 45 | } |
| 46 | |
| 47 | delete_option( 'everest_forms_custom_color_palettes' ); |
| 48 | |
| 49 | $color_palettes = array(); |
| 50 | |
| 51 | $color_palettes[] = array( |
| 52 | 'label' => $label, |
| 53 | 'colors' => $colors, |
| 54 | 'is_pro' => true, |
| 55 | 'is_custom' => true, |
| 56 | ); |
| 57 | |
| 58 | update_option( 'everest_forms_custom_color_palettes', $color_palettes ); |
| 59 | |
| 60 | wp_send_json_success( esc_html__( 'Color palette saved successfully!', 'everest-forms' ) ); |
| 61 | |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | /** |
| 67 | * Save styles as a template. |
| 68 | * |
| 69 | * @retEVFn void |
| 70 | */ |
| 71 | public function save_template() { |
| 72 | $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_nonce'] ) ) : ''; |
| 73 | |
| 74 | if ( ! wp_verify_nonce( $nonce, 'save_template' ) ) { |
| 75 | wp_send_json_error( |
| 76 | array( |
| 77 | 'message' => __( 'Nonce error. Please refresh the page.', 'everest-forms' ), |
| 78 | ) |
| 79 | ); |
| 80 | exit; |
| 81 | } |
| 82 | |
| 83 | $form_id = isset( $_POST['form_id'] ) ? sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) : '0'; |
| 84 | |
| 85 | if ( ! empty( $form_id ) ) { |
| 86 | $template_name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 87 | |
| 88 | if ( empty( $template_name ) ) { |
| 89 | $template_name = strval( time() ); |
| 90 | } |
| 91 | |
| 92 | $template_slug = str_replace( ' ', '-', strtolower( $template_name ) ); |
| 93 | $templates = json_decode( get_option( 'evf_style_templates' ) ); |
| 94 | $styles = get_option( 'everest_forms_styles' ); |
| 95 | |
| 96 | if ( isset( $templates->$template_slug ) ) { |
| 97 | wp_send_json_error( |
| 98 | array( |
| 99 | 'message' => __( 'Template name exists. Please change the template name and try again.', 'everest-forms' ), |
| 100 | ) |
| 101 | ); |
| 102 | exit; |
| 103 | } |
| 104 | |
| 105 | $template = new stdClass(); |
| 106 | $template->name = $template_name; |
| 107 | $template->image = plugins_url( 'addons/StyleCustomizer/assets/images/templates/default.png', EVF_PLUGIN_FILE ); |
| 108 | $template->data = isset( $styles[ $form_id ] ) ? $styles[ $form_id ] : ''; |
| 109 | |
| 110 | if ( ! empty( $template->data ) ) { |
| 111 | unset( $template->data['template'] ); |
| 112 | } |
| 113 | |
| 114 | $templates->$template_slug = $template; |
| 115 | |
| 116 | update_option( 'evf_style_templates', wp_json_encode( $templates ) ); |
| 117 | |
| 118 | wp_send_json_success( |
| 119 | array( |
| 120 | 'template_id' => $template_slug, |
| 121 | 'message' => __( 'Template saved successfully. Please reload the page to view changes. Reload Now?', 'everest-forms' ), |
| 122 | ) |
| 123 | ); |
| 124 | exit; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * Delete template. |
| 131 | * |
| 132 | * @retEVFn void |
| 133 | */ |
| 134 | public function delete_template() { |
| 135 | $nonce = isset( $_POST['_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_nonce'] ) ) : ''; |
| 136 | |
| 137 | if ( ! wp_verify_nonce( $nonce, 'delete_template' ) ) { |
| 138 | wp_send_json_error( __( 'Nonce error. Please refresh the page.', 'everest-forms' ) ); |
| 139 | exit; |
| 140 | } |
| 141 | |
| 142 | $template_slug = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 143 | $templates = json_decode( get_option( 'evf_style_templates' ) ); |
| 144 | |
| 145 | if ( isset( $templates->$template_slug ) ) { |
| 146 | unset( $templates->$template_slug ); |
| 147 | } |
| 148 | |
| 149 | update_option( 'evf_style_templates', wp_json_encode( $templates ) ); |
| 150 | wp_send_json_success( __( 'Template deleted successfully.', 'everest-forms' ) ); |
| 151 | exit; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | new EVF_Style_Customizer_Ajax(); |
| 156 |