Guest User

Untitled

a guest
Mar 24th, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.83 KB | None | 0 0
  1. <?php
  2.  
  3. class acf_field_font_awesome extends acf_field {
  4.    
  5.     var $stylesheet, // will hold fontawesome stylesheet url
  6.         $version; // will hold fontawesome version number
  7.  
  8.     /*
  9.     *  __construct
  10.     *
  11.     *  This function will setup the field type data
  12.     *
  13.     *  @type    function
  14.     *  @date    5/03/2014
  15.     *  @since   5.0.0
  16.     *
  17.     *  @param   n/a
  18.     *  @return  n/a
  19.     */
  20.    
  21.     function __construct() {   
  22.  
  23.         $this->name = 'font-awesome';
  24.         $this->label = __('Font Awesome Icon');
  25.         $this->category = __("Content",'acf'); // Basic, Content, Choice, etc
  26.         $this->defaults = array(
  27.             'enqueue_fa'    =>  0,
  28.             'allow_null'    =>  0,
  29.             'save_format'   =>  'element',
  30.             'default_value' =>  '',
  31.             'fa_live_preview'   =>  '',
  32.             'choices'       =>  $this->get_icons()
  33.         );
  34.         $this->l10n = array();
  35.  
  36.         $this->settings = array(
  37.             'path' => dirname(__FILE__),
  38.             'dir' => $this->helpers_get_dir( __FILE__ ),
  39.             'version' => '1.5'
  40.         );
  41.  
  42.         add_filter('acf/load_field', array( $this, 'maybe_enqueue_font_awesome' ) );
  43.  
  44.         file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/log1.txt', print_r($this->settings, true), FILE_APPEND);
  45.  
  46.         parent::__construct();
  47.     }
  48.  
  49.     function get_icons()
  50.     {
  51.         require_once ( dirname( __FILE__ ) . '/better-font-awesome-library/better-font-awesome-library.php' );
  52.  
  53.         $args = array(
  54.             'version'               => 'latest',
  55.             'minified'              => true,
  56.             'remove_existing_fa'    => false,
  57.             'load_styles'           => false,
  58.             'load_admin_styles'     => false,
  59.             'load_shortcode'        => false,
  60.             'load_tinymce_plugin'   => false
  61.         );
  62.  
  63.         $bfa        = Better_Font_Awesome_Library::get_instance( $args );
  64.         $bfa_icons  = $bfa->get_icons();
  65.         $bfa_prefix = $bfa->get_prefix() . '-';
  66.         $new_icons  = array();
  67.  
  68.         $this->stylesheet   = $bfa->get_stylesheet_url();
  69.         $this->version      = $bfa->get_version();
  70.  
  71.         foreach ( $bfa_icons as $hex => $class ) {
  72.             $unicode = '&#x' . ltrim( $hex, '\\') . ';';
  73.             $new_icons[ $bfa_prefix . $class ] = $unicode . ' ' . $bfa_prefix . $class;
  74.         }
  75.  
  76.         return $new_icons;
  77.     }
  78.  
  79.     /*
  80.     *  maybe_enqueue_font_awesome()
  81.     *
  82.     *  If Enqueue FA is set to true, enqueue it in the footer. We cannot enqueue in the header because wp_head has already been called
  83.     *  
  84.     */
  85.  
  86.     function maybe_enqueue_font_awesome( $field )
  87.     {
  88.         if( 'font-awesome' == $field['type'] && $field['enqueue_fa'] ) {
  89.             add_action( 'wp_footer', array( $this, 'frontend_enqueue_scripts' ) );
  90.         }
  91.  
  92.         return $field;
  93.     }
  94.  
  95.     /*
  96.     *  frontend_enqueue_scripts()
  97.     *
  98.     *  This action is called in the wp_enqueue_scripts action on the front end.
  99.     *  
  100.     *  $info    http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
  101.     *  @type    action
  102.     */
  103.  
  104.     function frontend_enqueue_scripts()
  105.     {
  106.         wp_register_style('font-awesome', $this->stylesheet, array(), $this->version);
  107.  
  108.         wp_enqueue_style( array( 'font-awesome' ) );
  109.     }
  110.  
  111.     /*
  112.     *  render_field_settings()
  113.     *
  114.     *  Create extra settings for your field. These are visible when editing a field
  115.     *
  116.     *  @type    action
  117.     *  @since   3.6
  118.     *  @date    23/01/13
  119.     *
  120.     *  @param   $field (array) the $field being edited
  121.     *  @return  n/a
  122.     */
  123.    
  124.     function render_field_settings( $field ) {
  125.        
  126.         /*
  127.         *  acf_render_field_setting
  128.         *
  129.         *  This function will create a setting for your field. Simply pass the $field parameter and an array of field settings.
  130.         *  The array of settings does not require a `value` or `prefix`; These settings are found from the $field array.
  131.         *
  132.         *  More than one setting can be added by copy/paste the above code.
  133.         *  Please note that you must also have a matching $defaults value for the field name (font_size)
  134.         */
  135.        
  136.         acf_render_field_setting( $field, array(
  137.             'label'         => __('Live Preview','acf-font-awesome'),
  138.             'instructions'  => '',
  139.             'type'          => 'message',
  140.             'name'          => 'fa_live_preview',
  141.             'class'         => 'live-preview'
  142.         ));
  143.  
  144.         acf_render_field_setting( $field, array(
  145.             'label'         => __('Default Icon','acf-font-awesome'),
  146.             'instructions'  => '',
  147.             'type'          => 'select',
  148.             'name'          => 'default_value',
  149.             'class'         =>  'fontawesome',
  150.             'choices'       =>  $field['choices']
  151.         ));
  152.  
  153.         acf_render_field_setting( $field, array(
  154.             'label'         => __('Return Value','acf-font-awesome'),
  155.             'instructions'  => __('Specify the returned value on front end','acf-font-awesome'),
  156.             'type'          => 'radio',
  157.             'name'          => 'save_format',
  158.             'choices'   =>  array(
  159.                 'element'   =>  __('Icon Element','acf-font-awesome'),
  160.                 'class'     =>  __('Icon Class','acf-font-awesome'),
  161.                 'unicode'   =>  __('Icon Unicode','acf-font-awesome'),
  162.                 'object'    =>  __('Icon Object','acf-font-awesome'),
  163.             )
  164.         ));
  165.  
  166.         acf_render_field_setting( $field, array(
  167.             'label'         => __('Allow Null?','acf-font-awesome'),
  168.             'instructions'  => '',
  169.             'type'          => 'radio',
  170.             'name'          => 'allow_null',
  171.             'choices'   =>  array(
  172.                 1   =>  __('Yes','acf-font-awesome'),
  173.                 0   =>  __('No','acf-font-awesome')
  174.             )
  175.         ));
  176.  
  177.         acf_render_field_setting( $field, array(
  178.             'label'         => __('Enqueue FontAwesome?','acf-font-awesome'),
  179.             'instructions'  => __('Set to \'Yes\' to enqueue FA in the footer on any pages using this field.','acf-font-awesome'),
  180.             'type'          => 'radio',
  181.             'name'          => 'enqueue_fa',
  182.             'choices'   =>  array(
  183.                 1   =>  __('Yes','acf-font-awesome'),
  184.                 0   =>  __('No','acf-font-awesome')
  185.             )
  186.         ));
  187.     }
  188.    
  189.    
  190.     /*
  191.     *  render_field()
  192.     *
  193.     *  Create the HTML interface for your field
  194.     *
  195.     *  @param   $field (array) the $field being rendered
  196.     *
  197.     *  @type    action
  198.     *  @since   3.6
  199.     *  @date    23/01/13
  200.     *
  201.     *  @param   $field (array) the $field being edited
  202.     *  @return  n/a
  203.     */
  204.    
  205.     function render_field( $field ) {
  206.        
  207.         if( 'object' == $field['save_format'] )
  208.             $field['value'] = array( $field['value']->class );
  209.  
  210.         // value must be array
  211.         if( !is_array($field['value']) )
  212.         {
  213.             // perhaps this is a default value with new lines in it?
  214.             if( strpos($field['value'], "\n") !== false )
  215.             {
  216.                 // found multiple lines, explode it
  217.                 $field['value'] = explode("\n", $field['value']);
  218.             }
  219.             else
  220.             {
  221.                 $field['value'] = array( $field['value'] );
  222.             }
  223.         }
  224.        
  225.         // trim value
  226.         $field['value'] = array_map('trim', $field['value']);
  227.        
  228.         // html
  229.         echo '<div class="fa-field-wrapper">';
  230.         echo '<div class="fa-live-preview"></div>';
  231.         echo '<select id="' . $field['id'] . '" class="' . $field['class'] . ' fa-select2-field" name="' . $field['name'] . '" >'; 
  232.        
  233.         // null
  234.         if( $field['allow_null'] )
  235.         {
  236.             echo '<option value="null">- ' . __("Select",'acf') . ' -</option>';
  237.         }
  238.        
  239.         // loop through values and add them as options
  240.         if( is_array($field['choices']) )
  241.         {
  242.             foreach( $field['choices'] as $key => $value )
  243.             {
  244.                 $selected = $this->find_selected( $key, $field['value'], $field['save_format'], $field['choices'] );
  245.                 echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
  246.             }
  247.         }
  248.  
  249.         echo '</select>';
  250.         echo '</div>';
  251.     }
  252.  
  253.     function find_selected( $needle, $haystack, $type, $choices )
  254.     {
  255.         switch( $type )
  256.         {
  257.             case 'object':
  258.             case 'element':
  259.                 $search = array( '<i class="fa ', '"></i>' );
  260.                 $string = str_replace( $search, '', $haystack[0] );
  261.                 break;
  262.  
  263.             case 'class':
  264.                 $string = $haystack[0];
  265.                 break;
  266.         }
  267.  
  268.         if( $string == $needle )
  269.             return 'selected="selected"';
  270.  
  271.         return '';
  272.     }
  273.        
  274.     /*
  275.     *  input_admin_enqueue_scripts()
  276.     *
  277.     *  This action is called in the admin_enqueue_scripts action on the edit screen where your field is created.
  278.     *  Use this action to add CSS + JavaScript to assist your render_field() action.
  279.     *
  280.     *  @type    action (admin_enqueue_scripts)
  281.     *  @since   3.6
  282.     *  @date    23/01/13
  283.     *
  284.     *  @param   n/a
  285.     *  @return  n/a
  286.     */
  287.  
  288.     function input_admin_enqueue_scripts() {
  289.  
  290.         // register acf scripts
  291.         wp_enqueue_script('acf-input-font-awesome-edit-input', $this->settings['dir'] . 'js/edit_input.js', array(), $this->settings['version']);
  292.         wp_enqueue_style('acf-input-font-awesome-input', $this->settings['dir'] . 'css/input.css', array(), $this->settings['version']);
  293.         wp_enqueue_style('acf-input-font-awesome-fa', $this->stylesheet, array(), $this->version);
  294.     }
  295.    
  296.     /*
  297.     *  field_group_admin_enqueue_scripts()
  298.     *
  299.     *  This action is called in the admin_enqueue_scripts action on the edit screen where your field is edited.
  300.     *  Use this action to add CSS + JavaScript to assist your render_field_options() action.
  301.     *
  302.     *  @type    action (admin_enqueue_scripts)
  303.     *  @since   3.6
  304.     *  @date    23/01/13
  305.     *
  306.     *  @param   n/a
  307.     *  @return  n/a
  308.     */
  309.    
  310.     function field_group_admin_enqueue_scripts() {
  311.  
  312.         // register acf scripts
  313.         wp_enqueue_script('font-awesome-create-input', $this->settings['dir'] . 'js/create_input.js', array(), $this->settings['version']);
  314.         wp_enqueue_style('acf-input-font-awesome-input', $this->settings['dir'] . 'css/input.css', array(), $this->settings['version']);
  315.         wp_enqueue_style('acf-input-font-awesome-fa', $this->stylesheet, array(), $this->version);
  316.     }
  317.    
  318.     /*
  319.     *  load_value()
  320.     *
  321.     *  This filter is applied to the $value after it is loaded from the db
  322.     *
  323.     *  @type    filter
  324.     *  @since   3.6
  325.     *  @date    23/01/13
  326.     *
  327.     *  @param   $value (mixed) the value found in the database
  328.     *  @param   $post_id (mixed) the $post_id from which the value was loaded
  329.     *  @param   $field (array) the field array holding all the field options
  330.     *  @return  $value
  331.     */
  332.    
  333.     function load_value( $value, $post_id, $field ) {
  334.  
  335.         switch( $field['save_format'] )
  336.         {
  337.             case 'object':
  338.                 $icon_unicode_string = $this->defaults['choices'][ $value ];
  339.                 $icon_unicode_arr = explode( ' ', $icon_unicode_string );
  340.                 $icon_unicode = $icon_unicode_arr[0];
  341.                 $value = (object) array(
  342.                         'unicode' => $icon_unicode,
  343.                         'class'   => $value,
  344.                         'element' => '<i class="fa ' . $value . '"></i>'
  345.                     );
  346.                 break;
  347.  
  348.             case 'unicode':
  349.                 $icon_unicode_string = $this->defaults['choices'][ $value ];
  350.                 $icon_unicode_arr = explode( ' ', $icon_unicode_string );
  351.                 $value = $icon_unicode_arr[0];
  352.                 break;
  353.  
  354.             case 'element':
  355.                 $value = '<i class="fa ' . $value . '"></i>';
  356.                 break;
  357.         }
  358.  
  359.         return $value;
  360.     }
  361.  
  362.     /*
  363.     *  helpers_get_dir()
  364.     *
  365.     *  Helper function taken from ACF 4.x to allow finding of asset paths when plugin is included from outside the plugins directory
  366.     *
  367.     */
  368.  
  369.     function helpers_get_dir( $file ) {
  370.         $dir = trailingslashit( dirname( $file ) );
  371.         $count = 0;
  372.  
  373.         // sanitize for Win32 installs
  374.         $dir = str_replace('\\' ,'/', $dir);
  375.        
  376.         // if file is in plugins folder
  377.         $wp_plugin_dir = str_replace( '\\' ,'/', WP_PLUGIN_DIR );
  378.         $dir = str_replace( $wp_plugin_dir, plugins_url(), $dir, $count );
  379.  
  380.         if ( $count < 1 ) {
  381.             // if file is in wp-content folder
  382.             $wp_content_dir = str_replace( '\\' ,'/', WP_CONTENT_DIR );
  383.             $dir = str_replace( $wp_content_dir, content_url(), $dir, $count );
  384.         }
  385.  
  386.         if ( $count < 1 ) {
  387.             // if file is in ??? folder
  388.             $wp_dir = str_replace( '\\' ,'/', ABSPATH );
  389.             $dir = str_replace( $wp_dir, site_url( '/' ), $dir );
  390.         }
  391.        
  392.         return $dir;
  393.     }
  394.  
  395. }
  396.  
  397.  
  398. // create field
  399. new acf_field_font_awesome();
  400.  
  401. ?>
Advertisement
Add Comment
Please, Sign In to add comment