From 615c9d854ab885d9a472a14e7c3ebdecdb68892a Mon Sep 17 00:00:00 2001 From: vortis Date: Sat, 27 Sep 2014 01:53:27 -0300 Subject: [PATCH] Ported for ACF v5 Made some tweaks on the main acf-code_area.php ( based on http://bit.ly/1t3HXV5 ) and used pieces of code from acf_code_area-v4.php to create the acf_code_area-v5.php. --- acf-code_area.php | 67 ++++--------- acf_code_area-v5.php | 222 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+), 49 deletions(-) create mode 100644 acf_code_area-v5.php diff --git a/acf-code_area.php b/acf-code_area.php index 0fab48d..c58c7b9 100644 --- a/acf-code_area.php +++ b/acf-code_area.php @@ -10,60 +10,29 @@ License URI: http://www.gnu.org/licenses/gpl-2.0.html */ +class acf_field_code_area_plugin { -class acf_field_code_area_plugin -{ - /* - * Construct - * - * @description: - * @since: 3.6 - * @created: 1/04/13 - */ - - function __construct() - { - - // version 4+ - add_action('acf/register_fields', array($this, 'register_fields')); + function __construct() { + // version 5 + add_action('acf/include_field_types', array($this, 'include_field_types')); + + // version 4 + add_action('acf/register_fields', array($this, 'register_fields')); - - // version 3- - add_action( 'init', array( $this, 'init' ), 5); - } - - - /* - * Init - * - * @description: - * @since: 3.6 - * @created: 1/04/13 - */ - - function init() - { - if(function_exists('register_field')) - { - register_field('acf_field_code_area', dirname(__File__) . '/acf_code_area-v3.php'); - } } - - /* - * register_fields - * - * @description: - * @since: 3.6 - * @created: 1/04/13 - */ - - function register_fields() - { + + // version 4 + function register_fields() { include_once('acf_code_area-v4.php'); } - + + // version 5 + function include_field_types( $version ) { + include_once('acf_code_area-v5.php'); + } + } new acf_field_code_area_plugin(); - -?> + +?> \ No newline at end of file diff --git a/acf_code_area-v5.php b/acf_code_area-v5.php new file mode 100644 index 0000000..b29f80b --- /dev/null +++ b/acf_code_area-v5.php @@ -0,0 +1,222 @@ +name = 'code_area'; + $this->label = __('Code Area'); + $this->defaults = array( + 'new_lines' => '', + 'maxlength' => '', + 'placeholder' => '', + 'readonly' => 0, + 'disabled' => 0, + 'rows' => '' + ); + + $this->settings = array( + 'path' => plugin_dir_path( __FILE__ ), + 'dir' => plugin_dir_url( __FILE__ ), + 'version' => '1.0.0' + ); + + + // do not delete! + parent::__construct(); + } + + + + function input_admin_enqueue_scripts() + { + + // register acf scripts + wp_register_script( 'acf-input-code_area-code_mirror_js', $this->settings['dir'] . 'js/codemirror.js', array('acf-input'), $this->settings['version'] ); + wp_register_script( 'acf-input-code_area-code_mirror_mode_js', $this->settings['dir'] . 'js/mode/javascript.js', array('acf-input'), $this->settings['version'] ); + wp_register_script( 'acf-input-code_area-code_mirror_mode_css', $this->settings['dir'] . 'js/mode/css.js', array('acf-input'), $this->settings['version'] ); + wp_register_style( 'acf-input-code_area-code_mirror_css', $this->settings['dir'] . 'css/codemirror.css', array('acf-input'), $this->settings['version'] ); + wp_register_script( 'acf-input-code_area-code_mirror_mode_html', $this->settings['dir'] . 'js/mode/htmlmixed.js', array('acf-input'), $this->settings['version'] ); + wp_register_script( 'acf-input-code_area-code_mirror_mode_xml', $this->settings['dir'] . 'js/mode/xml.js', array('acf-input'), $this->settings['version'] ); + wp_register_script( 'acf-input-code_area-code_mirror_mode_php', $this->settings['dir'] . 'js/mode/php.js', array('acf-input'), $this->settings['version'] ); + wp_register_script( 'acf-input-code_area-code_mirror_mode_clike', $this->settings['dir'] . 'js/mode/clike.js', array('acf-input'), $this->settings['version'] ); + + + // scripts + wp_enqueue_script(array( + 'acf-input-code_area-code_mirror_js', + 'acf-input-code_area-code_mirror_mode_js', + 'acf-input-code_area-code_mirror_mode_css', + 'acf-input-code_area-code_mirror_mode_html', + 'acf-input-code_area-code_mirror_mode_xml', + 'acf-input-code_area-code_mirror_mode_php', + 'acf-input-code_area-code_mirror_mode_clike', + )); + + // styles + wp_enqueue_style(array( + 'acf-input-code_area-code_mirror_css', + )); + + } + + function render_field( $field ) { + + // vars + $o = array( 'id', 'class', 'name', 'rows' ); + $e = ''; + + + // maxlength + if( $field['maxlength'] !== '' ) { $o[] = 'maxlength'; } + + // rows + if( empty($field['rows']) ) { $field['rows'] = 8; } + + + // populate atts + $atts = array(); + foreach( $o as $k ) { + $atts[ $k ] = $field[ $k ]; + } + + + $e .= ''; + $e .= ''; + $e .= ''; + + + // return + echo $e; + + } + + + function render_field_settings( $field ) { + + // ACF4 migration + if( empty($field['ID']) ) { + + $field['new_lines'] = 'wpautop'; + + } + + + // default_value + acf_render_field_setting( $field, array( + 'label' => __('Default Value','acf'), + 'instructions' => __('Appears when creating a new post','acf'), + 'type' => 'textarea', + 'name' => 'default_value', + )); + + + // maxlength + acf_render_field_setting( $field, array( + 'label' => __('Character Limit','acf'), + 'instructions' => __('Leave blank for no limit','acf'), + 'type' => 'number', + 'name' => 'maxlength', + )); + + + // rows + acf_render_field_setting( $field, array( + 'label' => __('Rows','acf'), + 'instructions' => __('Sets the textarea height','acf'), + 'type' => 'number', + 'name' => 'rows', + 'placeholder' => 8 + )); + + + // language + acf_render_field_setting( $field, array( + 'label' => __('Language','acf'), + 'instructions' => __('Controls how new lines are rendered','acf'), + 'type' => 'radio', + 'name' => 'code_language', + 'choices' => array( + 'css' => __("CSS",'acf'), + 'javascript' => __("Javascript",'acf'), + 'htmlmixed' => __("HTML",'acf'), + 'php' => __("PHP",'acf'), + ) + )); + + // theme + acf_render_field_setting( $field, array( + 'label' => __('Themes','acf'), + 'instructions' => __("Set a theme for the editor (Preview Here) ",'acf'), + 'type' => 'select', + 'name' => 'code_theme', + 'choices' => array( + 'default' => __("Default",'acf'), + 'ambiance' => __("Ambiance",'acf'), + 'blackboard' => __("Blackboard",'acf'), + 'cobalt' => __("Cobalt",'acf'), + 'eclipse' => __("Eclipse",'acf'), + 'elegant' => __("Elegant",'acf'), + 'erlang-dark' => __("Erlang Dark",'acf'), + 'lesser-dark' => __("Lesser Dark",'acf'), + 'midnight' => __("Midnight",'acf'), + 'monokai' => __("Monokai",'acf'), + 'neat' => __("Neat",'acf'), + 'night' => __("Night",'acf'), + 'rubyblue' => __("Rubyblue",'acf'), + 'solarized' => __("Solarized",'acf'), + 'twilight' => __("Twilight",'acf'), + 'vibrant-ink' => __("Vibrant Ink",'acf'), + 'xq-dark' => __("XQ Dark",'acf'), + 'xq-light' => __("XQ Light",'acf'), + ) + )); + + } + + function format_value( $value, $post_id, $field ) { + + switch($field["code_language"]){ + case 'css': + return ''; + break; + case 'javascript': + return ''; + break; + case 'htmlmixed': + return nl2br($value); + break; + case 'php': + return eval($value); + break; + default: + return $value; + } + + return $value; + + } + +} + +new acf_field_code_area(); + +endif; + +?>