field-advanced-link.php
1 month ago
field-button.php
5 months ago
field-checkbox.php
1 month ago
field-clone.php
2 years ago
field-code-editor.php
1 month ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
1 month ago
field-flexible-content-actions-title.php
1 month ago
field-flexible-content-actions-toggle.php
1 month ago
field-flexible-content-actions.php
1 month ago
field-flexible-content-async.php
2 months ago
field-flexible-content-compatibility.php
1 month ago
field-flexible-content-controls.php
7 months ago
field-flexible-content-hide.php
1 month ago
field-flexible-content-hooks.php
7 months ago
field-flexible-content-modal-edit.php
7 months ago
field-flexible-content-modal-select.php
1 month ago
field-flexible-content-modal-settings.php
1 month ago
field-flexible-content-popup.php
1 month ago
field-flexible-content-preview.php
1 month ago
field-flexible-content-state.php
7 months ago
field-flexible-content-thumbnail.php
7 months ago
field-flexible-content-wysiwyg.php
7 months ago
field-flexible-content.php
1 month ago
field-forms.php
1 month ago
field-gallery.php
1 month ago
field-google-map.php
1 month ago
field-group.php
1 month ago
field-hidden.php
3 years ago
field-image.php
1 month ago
field-post-object.php
1 month ago
field-post-statuses.php
1 month ago
field-post-types.php
1 month ago
field-radio.php
1 month ago
field-recaptcha.php
1 month ago
field-relationship.php
1 month ago
field-repeater.php
1 month ago
field-select.php
1 month ago
field-slug.php
10 months ago
field-taxonomies.php
1 month ago
field-taxonomy-terms.php
1 month ago
field-taxonomy.php
1 month ago
field-textarea.php
1 month ago
field-user-roles.php
1 month ago
field-user.php
1 month ago
field-wysiwyg.php
1 month ago
field-button.php
223 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_button')): |
| 8 | |
| 9 | class acfe_field_button extends acf_field{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'acfe_button'; |
| 17 | $this->label = __('Button', 'acfe'); |
| 18 | $this->category = 'basic'; |
| 19 | $this->defaults = array( |
| 20 | 'button_value' => __('Submit', 'acfe'), |
| 21 | 'button_type' => 'button', |
| 22 | 'button_before' => '', |
| 23 | 'button_after' => '', |
| 24 | 'button_class' => 'button button-secondary', |
| 25 | 'button_id' => '', |
| 26 | 'button_ajax' => 0, |
| 27 | ); |
| 28 | |
| 29 | $this->add_action('wp_ajax_acfe/fields/button', array($this, 'ajax_request'), 99); |
| 30 | $this->add_action('wp_ajax_nopriv_acfe/fields/button', array($this, 'ajax_request'), 99); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * ajax_request |
| 37 | */ |
| 38 | function ajax_request(){ |
| 39 | |
| 40 | // bail early |
| 41 | if(!acf_verify_ajax()){ |
| 42 | die; |
| 43 | } |
| 44 | |
| 45 | // vars |
| 46 | $field_key = acf_maybe_get_POST('field_key', ''); |
| 47 | $post_id = acf_maybe_get_POST('post_id', 0); |
| 48 | $acf = acf_maybe_get_POST('acf', array()); |
| 49 | |
| 50 | // get field |
| 51 | $field = acf_get_field($field_key); |
| 52 | |
| 53 | // field not found |
| 54 | if(!$field){ |
| 55 | die; |
| 56 | } |
| 57 | |
| 58 | // setup meta |
| 59 | acfe_setup_meta($acf, 'acfe/button', true); |
| 60 | |
| 61 | // actions |
| 62 | do_action("acfe/fields/button", $field, $post_id); |
| 63 | do_action("acfe/fields/button/name={$field['name']}", $field, $post_id); |
| 64 | do_action("acfe/fields/button/key={$field_key}", $field, $post_id); |
| 65 | |
| 66 | // reset |
| 67 | acfe_reset_meta(); |
| 68 | |
| 69 | die; |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * render_field_settings |
| 76 | * |
| 77 | * @param $field |
| 78 | */ |
| 79 | function render_field_settings($field){ |
| 80 | |
| 81 | // Value |
| 82 | acf_render_field_setting($field, array( |
| 83 | 'label' => __('Button value', 'acfe'), |
| 84 | 'instructions' => __('Set a default button value', 'acfe'), |
| 85 | 'type' => 'text', |
| 86 | 'name' => 'button_value', |
| 87 | )); |
| 88 | |
| 89 | // Type |
| 90 | acf_render_field_setting($field, array( |
| 91 | 'label' => __('Button type', 'acfe'), |
| 92 | 'instructions' => __('Choose the button type', 'acfe'), |
| 93 | 'type' => 'radio', |
| 94 | 'name' => 'button_type', |
| 95 | 'layout' => 'horizontal', |
| 96 | 'choices' => array( |
| 97 | 'button' => __('Button', 'acfe'), |
| 98 | 'submit' => __('Input', 'acfe'), |
| 99 | ), |
| 100 | )); |
| 101 | |
| 102 | // class |
| 103 | acf_render_field_setting($field, array( |
| 104 | 'label' => __('Button attributes', 'acfe'), |
| 105 | 'instructions' => '', |
| 106 | 'type' => 'text', |
| 107 | 'name' => 'button_class', |
| 108 | 'prepend' => __('class', 'acf'), |
| 109 | )); |
| 110 | |
| 111 | // id |
| 112 | acf_render_field_setting($field, array( |
| 113 | 'label' => '', |
| 114 | 'instructions' => '', |
| 115 | 'type' => 'text', |
| 116 | 'name' => 'button_id', |
| 117 | 'prepend' => __('id', 'acf'), |
| 118 | '_append' => 'button_class' |
| 119 | )); |
| 120 | |
| 121 | // Before HTML |
| 122 | acf_render_field_setting($field, array( |
| 123 | 'label' => __('Before HTML', 'acfe'), |
| 124 | 'instructions' => __('Custom HTML before the button', 'acfe'), |
| 125 | 'type' => 'acfe_code_editor', |
| 126 | 'name' => 'button_before', |
| 127 | 'rows' => 4, |
| 128 | )); |
| 129 | |
| 130 | // After HTML |
| 131 | acf_render_field_setting($field, array( |
| 132 | 'label' => __('After HTML', 'acfe'), |
| 133 | 'instructions' => __('Custom HTML after the button', 'acfe'), |
| 134 | 'type' => 'acfe_code_editor', |
| 135 | 'name' => 'button_after', |
| 136 | 'rows' => 4, |
| 137 | )); |
| 138 | |
| 139 | // Ajax |
| 140 | acf_render_field_setting($field, array( |
| 141 | 'label' => __('Ajax Request', 'acfe'), |
| 142 | 'instructions' => __('Trigger ajax event on click', 'acfe') . '. <a href="https://www.acf-extended.com/features/fields/button" target="_blank">' . __('See documentation', 'acfe') . '</a>', |
| 143 | 'name' => 'button_ajax', |
| 144 | 'type' => 'true_false', |
| 145 | 'ui' => 1, |
| 146 | )); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * render_field |
| 153 | * |
| 154 | * @param $field |
| 155 | */ |
| 156 | function render_field($field){ |
| 157 | |
| 158 | // Before |
| 159 | if($field['button_before']){ |
| 160 | echo $field['button_before']; |
| 161 | } |
| 162 | |
| 163 | $ajax = false; |
| 164 | |
| 165 | if($field['button_ajax']){ |
| 166 | $ajax = 'data-ajax="1"'; |
| 167 | } |
| 168 | |
| 169 | // Button |
| 170 | if($field['button_type'] === 'button'){ |
| 171 | |
| 172 | echo '<button |
| 173 | type="submit" |
| 174 | id="' . esc_attr($field['button_id']) . '" |
| 175 | class="' . esc_attr($field['button_class']) . '" |
| 176 | name="' . esc_attr($field['name']) . '" |
| 177 | value="' . esc_attr($field['button_value']) . '" |
| 178 | ' . $ajax . ' |
| 179 | >' . esc_attr($field['button_value']) . '</button>'; |
| 180 | |
| 181 | // Submit |
| 182 | }elseif($field['button_type'] === 'submit'){ |
| 183 | |
| 184 | echo '<input |
| 185 | type="submit" |
| 186 | id="' . esc_attr($field['button_id']) . '" |
| 187 | class="' . esc_attr($field['button_class']) . '" |
| 188 | name="' . esc_attr($field['name']) . '" |
| 189 | value="' . esc_attr($field['button_value']) . '" |
| 190 | ' . $ajax . ' |
| 191 | />'; |
| 192 | |
| 193 | } |
| 194 | |
| 195 | // After |
| 196 | if($field['button_after']){ |
| 197 | echo $field['button_after']; |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /** |
| 204 | * translate_field |
| 205 | * |
| 206 | * @param $field |
| 207 | * |
| 208 | * @return mixed |
| 209 | */ |
| 210 | function translate_field($field){ |
| 211 | |
| 212 | $field['button_value'] = acf_translate($field['button_value']); |
| 213 | |
| 214 | return $field; |
| 215 | |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | // initialize |
| 221 | acf_register_field_type('acfe_field_button'); |
| 222 | |
| 223 | endif; |