field-advanced-link.php
2 years ago
field-button.php
3 years ago
field-checkbox.php
3 years ago
field-clone.php
2 years ago
field-code-editor.php
3 years ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
3 years ago
field-flexible-content-actions.php
2 years ago
field-flexible-content-async.php
3 years ago
field-flexible-content-controls.php
3 years ago
field-flexible-content-edit.php
3 years ago
field-flexible-content-hide.php
3 years ago
field-flexible-content-preview.php
2 years ago
field-flexible-content-select.php
3 years ago
field-flexible-content-settings.php
3 years ago
field-flexible-content-state.php
3 years ago
field-flexible-content-thumbnail.php
3 years ago
field-flexible-content.php
2 years ago
field-forms.php
2 years ago
field-group.php
3 years ago
field-hidden.php
3 years ago
field-image.php
2 years ago
field-post-object.php
3 years ago
field-post-statuses.php
3 years ago
field-post-types.php
3 years ago
field-recaptcha.php
2 years ago
field-repeater.php
3 years ago
field-select.php
3 years ago
field-slug.php
3 years ago
field-taxonomies.php
3 years ago
field-taxonomy-terms.php
2 years ago
field-taxonomy.php
2 years ago
field-textarea.php
3 years ago
field-user-roles.php
3 years ago
field-wysiwyg.php
3 years ago
field-code-editor.php
257 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(acf_version_compare($GLOBALS['wp_version'], '<', '4.9')){ |
| 8 | return; |
| 9 | } |
| 10 | |
| 11 | if(!class_exists('acfe_field_code_editor')): |
| 12 | |
| 13 | class acfe_field_code_editor extends acf_field{ |
| 14 | |
| 15 | // vars |
| 16 | var $textarea = ''; |
| 17 | |
| 18 | /** |
| 19 | * initialize |
| 20 | */ |
| 21 | function initialize(){ |
| 22 | |
| 23 | $this->name = 'acfe_code_editor'; |
| 24 | $this->label = __('Code Editor', 'acfe'); |
| 25 | $this->category = 'content'; |
| 26 | $this->defaults = array( |
| 27 | 'default_value' => '', |
| 28 | 'placeholder' => '', |
| 29 | 'mode' => 'text/html', |
| 30 | 'lines' => true, |
| 31 | 'indent_unit' => 4, |
| 32 | 'maxlength' => '', |
| 33 | 'rows' => 4, |
| 34 | 'max_rows' => '', |
| 35 | 'return_format' => array(), |
| 36 | ); |
| 37 | |
| 38 | $this->textarea = acf_get_field_type('textarea'); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * render_field_settings |
| 45 | * |
| 46 | * @param $field |
| 47 | */ |
| 48 | function render_field_settings($field){ |
| 49 | |
| 50 | // default_value |
| 51 | acf_render_field_setting($field, array( |
| 52 | 'label' => __('Default Value','acf'), |
| 53 | 'instructions' => __('Appears when creating a new post','acf'), |
| 54 | 'type' => 'acfe_code_editor', |
| 55 | 'name' => 'default_value', |
| 56 | 'rows' => 4 |
| 57 | )); |
| 58 | |
| 59 | // placeholder |
| 60 | acf_render_field_setting($field, array( |
| 61 | 'label' => __('Placeholder','acf'), |
| 62 | 'instructions' => __('Appears within the input','acf'), |
| 63 | 'type' => 'acfe_code_editor', |
| 64 | 'name' => 'placeholder', |
| 65 | 'rows' => 4 |
| 66 | )); |
| 67 | |
| 68 | // Mode |
| 69 | acf_render_field_setting($field, array( |
| 70 | 'label' => __('Editor mode','acf'), |
| 71 | 'instructions' => __('Choose the syntax highlight','acf'), |
| 72 | 'type' => 'select', |
| 73 | 'name' => 'mode', |
| 74 | 'choices' => array( |
| 75 | 'text/html' => __('Text/HTML', 'acf'), |
| 76 | 'javascript' => __('JavaScript', 'acf'), |
| 77 | 'application/x-json' => __('Json', 'acf'), |
| 78 | 'css' => __('CSS', 'acf'), |
| 79 | 'application/x-httpd-php' => __('PHP (mixed)', 'acf'), |
| 80 | 'text/x-php' => __('PHP (plain)', 'acf'), |
| 81 | ) |
| 82 | )); |
| 83 | |
| 84 | // Lines |
| 85 | acf_render_field_setting($field, array( |
| 86 | 'label' => __('Show Lines', 'acf'), |
| 87 | 'instructions' => 'Whether to show line numbers to the left of the editor', |
| 88 | 'type' => 'true_false', |
| 89 | 'name' => 'lines', |
| 90 | 'ui' => true, |
| 91 | )); |
| 92 | |
| 93 | // Indent Unit |
| 94 | acf_render_field_setting($field, array( |
| 95 | 'label' => __('Indent Unit', 'acf'), |
| 96 | 'instructions' => 'How many spaces a block (whatever that means in the edited language) should be indented', |
| 97 | 'type' => 'number', |
| 98 | 'min' => 0, |
| 99 | 'name' => 'indent_unit', |
| 100 | )); |
| 101 | |
| 102 | // maxlength |
| 103 | acf_render_field_setting($field, array( |
| 104 | 'label' => __('Character Limit','acf'), |
| 105 | 'instructions' => __('Leave blank for no limit','acf'), |
| 106 | 'type' => 'number', |
| 107 | 'name' => 'maxlength', |
| 108 | )); |
| 109 | |
| 110 | // rows |
| 111 | acf_render_field_setting($field, array( |
| 112 | 'label' => __('Rows','acf'), |
| 113 | 'instructions' => __('Sets the textarea height','acf'), |
| 114 | 'type' => 'number', |
| 115 | 'name' => 'rows', |
| 116 | 'placeholder' => '' |
| 117 | )); |
| 118 | |
| 119 | // max rows |
| 120 | acf_render_field_setting($field, array( |
| 121 | 'label' => __('Max rows','acf'), |
| 122 | 'instructions' => __('Sets the textarea max height','acf'), |
| 123 | 'type' => 'number', |
| 124 | 'name' => 'max_rows', |
| 125 | 'placeholder' => '' |
| 126 | )); |
| 127 | |
| 128 | // return format |
| 129 | acf_render_field_setting($field, array( |
| 130 | 'label' => __('Return Value', 'acf'), |
| 131 | 'instructions' => '', |
| 132 | 'type' => 'checkbox', |
| 133 | 'name' => 'return_format', |
| 134 | 'layout' => 'horizontal', |
| 135 | 'choices' => array( |
| 136 | 'htmlentities' => __("HTML Entities", 'acfe'), |
| 137 | 'nl2br' => __("New Lines to <br>", 'acfe'), |
| 138 | ), |
| 139 | )); |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * update_field |
| 146 | * |
| 147 | * @param $field |
| 148 | * |
| 149 | * @return mixed |
| 150 | */ |
| 151 | function update_field($field){ |
| 152 | |
| 153 | $field['return_format'] = acf_get_array($field['return_format']); |
| 154 | |
| 155 | return $field; |
| 156 | |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * input_admin_enqueue_scripts |
| 162 | */ |
| 163 | function input_admin_enqueue_scripts(){ |
| 164 | |
| 165 | if(acfe_is_block_editor()){ |
| 166 | |
| 167 | wp_enqueue_script('code-editor'); |
| 168 | wp_enqueue_style('code-editor'); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /** |
| 176 | * render_field |
| 177 | * |
| 178 | * @param $field |
| 179 | */ |
| 180 | function render_field($field){ |
| 181 | |
| 182 | // enqueue |
| 183 | wp_enqueue_script('code-editor'); |
| 184 | wp_enqueue_style('code-editor'); |
| 185 | |
| 186 | // field type |
| 187 | $field['type'] = 'textarea'; |
| 188 | |
| 189 | // wrapper |
| 190 | $wrapper = array( |
| 191 | 'class' => 'acf-input-wrap acfe-field-code-editor', |
| 192 | 'data-mode' => $field['mode'], |
| 193 | 'data-lines' => $field['lines'], |
| 194 | 'data-indent-unit' => $field['indent_unit'], |
| 195 | 'data-rows' => $field['rows'], |
| 196 | 'data-max-rows' => $field['max_rows'], |
| 197 | ); |
| 198 | |
| 199 | ?> |
| 200 | <div <?php echo acf_esc_atts($wrapper); ?>> |
| 201 | <?php $this->textarea->render_field($field); ?> |
| 202 | </div> |
| 203 | <?php |
| 204 | |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * validate_value |
| 210 | * |
| 211 | * @param $valid |
| 212 | * @param $value |
| 213 | * @param $field |
| 214 | * @param $input |
| 215 | * |
| 216 | * @return mixed |
| 217 | */ |
| 218 | function validate_value($valid, $value, $field, $input){ |
| 219 | return $this->textarea->validate_value($valid, $value, $field, $input); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** |
| 224 | * format_value |
| 225 | * |
| 226 | * @param $value |
| 227 | * @param $post_id |
| 228 | * @param $field |
| 229 | * |
| 230 | * @return string |
| 231 | */ |
| 232 | function format_value($value, $post_id, $field){ |
| 233 | |
| 234 | // force array |
| 235 | $field['return_format'] = acf_get_array($field['return_format']); |
| 236 | |
| 237 | // htmlentities |
| 238 | if(in_array('htmlentities', $field['return_format'])){ |
| 239 | $value = htmlentities($value); |
| 240 | } |
| 241 | |
| 242 | // nl2br |
| 243 | if(in_array('nl2br', $field['return_format'])){ |
| 244 | $value = nl2br($value); |
| 245 | } |
| 246 | |
| 247 | // return |
| 248 | return $value; |
| 249 | |
| 250 | } |
| 251 | |
| 252 | } |
| 253 | |
| 254 | // initialize |
| 255 | acf_register_field_type('acfe_field_code_editor'); |
| 256 | |
| 257 | endif; |