Plugin Directory

Changeset 1007668


Ignore:
Timestamp:
10/15/2014 05:24:24 AM (11 years ago)
Author:
Faison
Message:

Uploading Version 2.0.0

Location:
advanced-custom-fields-nav-menu-field
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • advanced-custom-fields-nav-menu-field/trunk/fz-acf-nav-menu.php

    r862555 r1007668  
    11<?php
    22/*
    3 Plugin Name: Advanced Custom Fields: Nav Menu Field
    4 Plugin URI: http://faisonz.com/wordpress-plugins/advanced-custom-fields-nav-menu-field/
    5 Description: Add-On plugin for Advanced Custom Fields (ACF) that adds a 'Nav Menu' Field type.
    6 Version: 1.1.2
    7 Author: Faison Zutavern
    8 Author URI: http://faisonz.com
    9 License: GPL2 or later
    10 */
     3 * Plugin Name: Advanced Custom Fields: Nav Menu Field
     4 * Plugin URI: http://faisonz.com/wordpress-plugins/advanced-custom-fields-nav-menu-field/
     5 * Description: Add-On plugin for Advanced Custom Fields (ACF) that adds a 'Nav Menu' Field type.
     6 * Version: 2.0.0
     7 * Author: Faison Zutavern
     8 * Author URI: http://faisonz.com
     9 * License: GPL2 or later
     10 */
    1111
     12// Exit if accessed directly
     13if ( ! defined( 'ABSPATH' ) ) {
     14    exit;
     15}
    1216
    13 class acf_field_nav_menu_plugin
    14 {
    15     /*
    16     *  Construct
    17     *
    18     *  @description:
    19     *  @since: 3.6
    20     *  @created: 1/04/13
    21     */
    22    
    23     function __construct()
    24     {
    25         // set text domain
    26         /*
    27         $domain = 'acf-nav_menu';
    28         $mofile = trailingslashit(dirname(__File__)) . 'lang/' . $domain . '-' . get_locale() . '.mo';
    29         load_textdomain( $domain, $mofile );
    30         */
    31        
    32        
    33         // version 4+
    34         add_action('acf/register_fields', array($this, 'register_fields'));
     17class ACF_Nav_Menu_Field_Plugin {
    3518
     19    /**
     20     * Adds register hooks for the Nav Menu Field.
     21     */
     22    public function __construct() {
     23        // version 4
     24        add_action( 'acf/register_fields', array( $this, 'register_field_v4' ) );   
     25
     26        // version 5
     27        add_action( 'acf/include_field_types', array( $this, 'register_field_v5' ) );
    3628    }
    37    
    38     /*
    39     *  register_fields
    40     *
    41     *  @description:
    42     *  @since: 3.6
    43     *  @created: 1/04/13
    44     */
    45    
    46     function register_fields()
    47     {
    48         include_once('nav-menu-v4.php');
     29
     30    /**
     31     * Loads up the Nav Menu Field for ACF v4
     32     */
     33    public function register_field_v4() {
     34        include_once 'nav-menu-v4.php';
     35    }
     36
     37    /**
     38     * Loads up the Nav Menu Field for ACF v5
     39     */
     40    public function register_field_v5() {
     41        include_once 'nav-menu-v5.php';
    4942    }
    5043   
    5144}
    5245
    53 new acf_field_nav_menu_plugin();
    54        
    55 ?>
     46new ACF_Nav_Menu_Field_Plugin();
  • advanced-custom-fields-nav-menu-field/trunk/nav-menu-v4.php

    r760421 r1007668  
    11<?php
    2 
    3 class acf_field_nav_menu extends acf_field
    4 {
    5     // vars
    6     var $settings, // will hold info such as dir / path
    7         $defaults; // will hold default field options
    8        
    9        
    10     /*
    11     *  __construct
    12     *
    13     *  Set name / label needed for actions / filters
    14     *
    15     *  @since   3.6
    16     *  @date    23/01/13
    17     */
    18    
    19     function __construct()
    20     {
    21         // vars
    22         $this->name = 'nav_menu';
    23         $this->label = __('Nav Menu');
    24         $this->category = __("Relational",'acf'); // Basic, Content, Choice, etc
     2/**
     3* Nav Menu Field v4
     4*
     5* @package ACF Nav Menu Field
     6*/
     7
     8// Exit if accessed directly
     9if ( ! defined( 'ABSPATH' ) ) {
     10    exit;
     11}
     12
     13/**
     14 * ACF_Field_Nav_Menu_V4 Class
     15 *
     16 * This class contains all the custom workings for the Nav Menu Field for ACF v4
     17 */
     18class ACF_Field_Nav_Menu_V4 extends acf_field {
     19
     20    /**
     21     * Sets up some default values and delegats work to the parent constructor.
     22     */
     23    public function __construct() {
     24        $this->name     = 'nav_menu';
     25        $this->label    = __( 'Nav Menu' );
     26        $this->category = __( 'Relational' ); // Basic, Content, Choice, etc
    2527        $this->defaults = array(
    2628            'save_format' => 'id',
    27             'allow_null' => 0,
    28             'container' => 'div'
     29            'allow_null'  => 0,
     30            'container'   => 'div',
    2931        );
    30        
    31        
    32         // do not delete!
    33         parent::__construct();
    34        
    35        
    36         // settings
    37         $this->settings = array(
    38             'path' => apply_filters('acf/helpers/get_path', __FILE__),
    39             'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
    40             'version' => '1.1.2'
     32
     33        parent::__construct();
     34    }
     35
     36    /**
     37     * Renders the Nav Menu Field options seen when editing a Nav Menu Field.
     38     *
     39     * @param array $field The array representation of the current Nav Menu Field.
     40     */
     41    public function create_options( $field ) {
     42        $field = array_merge( $this->defaults, $field );
     43        $key   = $field['name'];
     44
     45        // Create Field Options HTML
     46        ?>
     47        <tr class="field_option field_option_<?php echo esc_attr( $this->name ); ?>">
     48            <td class="label">
     49                <label><?php _e( 'Return Value' ); ?></label>
     50            </td>
     51            <td>
     52            <?php
     53                do_action('acf/create_field', array(
     54                    'type'    => 'radio',
     55                    'name'    => 'fields['.$key.'][save_format]',
     56                    'value'   => $field['save_format'],
     57                    'layout'  => 'horizontal',
     58                    'choices' => array(
     59                        'object' => __( 'Nav Menu Object' ),
     60                        'menu'   => __( 'Nav Menu HTML' ),
     61                        'id'     => __( 'Nav Menu ID' ),
     62                    ),
     63                ) );
     64            ?>
     65            </td>
     66        </tr>
     67        <tr class="field_option field_option_<?php echo esc_attr( $this->name ); ?>">
     68            <td class="label">
     69                <label><?php _e( 'Menu Container' ); ?></label>
     70                <p class="description"><?php _e( "What to wrap the Menu's ul with (when returning HTML only)" ) ?></p>
     71            </td>
     72            <td>
     73            <?php
     74                do_action('acf/create_field', array(
     75                    'type'    => 'select',
     76                    'name'    => 'fields['.$key.'][container]',
     77                    'value'   => $field['container'],
     78                    'choices' => $this->get_allowed_nav_container_tags(),
     79                ) );
     80            ?>
     81            </td>
     82        </tr>
     83        <tr class="field_option field_option_<?php echo esc_attr( $this->name ); ?>">
     84            <td class="label">
     85                <label><?php _e( 'Allow Null?' ); ?></label>
     86            </td>
     87            <td>
     88            <?php
     89                do_action('acf/create_field', array(
     90                    'type'    => 'radio',
     91                    'name'    => 'fields['.$key.'][allow_null]',
     92                    'value'   => $field['allow_null'],
     93                    'layout'  => 'horizontal',
     94                    'choices' => array(
     95                        1 => __( 'Yes' ),
     96                        0 => __( 'No' ),
     97                    ),
     98                ) );
     99            ?>
     100            </td>
     101        </tr>
     102        <?php
     103    }
     104
     105    /**
     106     * Renders the Nav Menu Field.
     107     *
     108     * @param array $field The array representation of the current Nav Menu Field.
     109     */
     110    public function create_field( $field ) {
     111        $allow_null = $field['allow_null'];
     112        $nav_menus  = $this->get_nav_menus( $allow_null );
     113
     114        if ( empty( $nav_menus ) ) {
     115            return;
     116        }
     117        ?>
     118        <select id="<?php esc_attr( $field['id'] ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>">
     119        <?php foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) : ?>
     120            <option value="<?php echo esc_attr( $nav_menu_id ); ?>" <?php selected( $field['value'], $nav_menu_id ); ?>>
     121                <?php echo esc_html( $nav_menu_name ); ?>
     122            </option>
     123        <?php endforeach; ?>
     124        </select>
     125        <?php
     126    }
     127
     128    /**
     129     * Gets a list of Nav Menus indexed by their Nav Menu IDs.
     130     *
     131     * @param bool $allow_null If true, prepends the null option.
     132     *
     133     * @return array An array of Nav Menus indexed by their Nav Menu IDs.
     134     */
     135    private function get_nav_menus( $allow_null = false ) {
     136        $navs = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
     137
     138        $nav_menus = array();
     139
     140        if ( $allow_null ) {
     141            $nav_menus[''] = ' - Select - ';
     142        }
     143
     144        foreach ( $navs as $nav ) {
     145            $nav_menus[ $nav->term_id ] = $nav->name;
     146        }
     147
     148        return $nav_menus;
     149    }
     150
     151    /**
     152     * Get the allowed wrapper tags for use with wp_nav_menu().
     153     *
     154     * @return array An array of allowed wrapper tags.
     155     */
     156    private function get_allowed_nav_container_tags() {
     157        $tags           = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
     158        $formatted_tags = array(
     159            '0' => 'None',
    41160        );
    42161
    43     }
    44    
    45    
    46     /*
    47     *  create_options()
    48     *
    49     *  Create extra options for your field. This is rendered when editing a field.
    50     *  The value of $field['name'] can be used (like bellow) to save extra data to the $field
    51     *
    52     *  @type    action
    53     *  @since   3.6
    54     *  @date    23/01/13
    55     *
    56     *  @param   $field  - an array holding all the field's data
    57     */
    58    
    59     function create_options( $field )
    60     {
    61         // defaults?
     162        foreach ( $tags as $tag ) {
     163            $formatted_tags[$tag] = ucfirst( $tag );
     164        }
     165
     166        return $formatted_tags;
     167    }
     168
     169    /**
     170     * Renders the Nav Menu Field.
     171     *
     172     * @param int   $value   The Nav Menu ID selected for this Nav Menu Field.
     173     * @param int   $post_id The Post ID this $value is associated with.
     174     * @param array $field   The array representation of the current Nav Menu Field.
     175     *
     176     * @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false.
     177     */
     178    public function format_value_for_api( $value, $post_id, $field ) {
    62179        $field = array_merge($this->defaults, $field);
    63180       
    64         // key is needed in the field names to correctly save the data
    65         $key = $field['name'];
    66        
    67        
    68         // Create Field Options HTML
    69         ?>
    70 <tr class="field_option field_option_<?php echo $this->name; ?>">
    71     <td class="label">
    72         <label><?php _e("Return Value",'acf'); ?></label>
    73     </td>
    74     <td>
    75         <?php
    76        
    77         do_action('acf/create_field', array(
    78             'type'      =>  'radio',
    79             'name'      =>  'fields['.$key.'][save_format]',
    80             'value'     =>  $field['save_format'],
    81             'layout'    =>  'horizontal',
    82             'choices'   =>  array(
    83                 'object'    =>  __("Nav Menu Object",'acf'),
    84                 'menu'      =>  __("Nav Menu HTML",'acf'),
    85                 'id'        =>  __("Nav Menu ID",'acf')
    86             )
    87         ));
    88        
    89         ?>
    90     </td>
    91 </tr>
    92 <tr class="field_option field_option_<?php echo $this->name; ?>">
    93     <td class="label">
    94         <label><?php _e("Menu Container",'acf'); ?></label>
    95         <p class="description">What to wrap the Menu's ul with.<br />Only used when returning HTML.</p>
    96     </td>
    97     <td>
    98         <?php
    99 
    100         $choices = $this->get_allowed_nav_container_tags();
    101        
    102         do_action('acf/create_field', array(
    103             'type'      =>  'select',
    104             'name'      =>  'fields['.$key.'][container]',
    105             'value'     =>  $field['container'],
    106             'choices'   =>  $choices
    107         ));
    108        
    109         ?>
    110     </td>
    111 </tr>
    112 <tr class="field_option field_option_<?php echo $this->name; ?>">
    113     <td class="label">
    114         <label><?php _e("Allow Null?",'acf'); ?></label>
    115     </td>
    116     <td>
    117         <?php
    118         do_action('acf/create_field', array(
    119             'type'  =>  'radio',
    120             'name'  =>  'fields['.$key.'][allow_null]',
    121             'value' =>  $field['allow_null'],
    122             'choices'   =>  array(
    123                 1   =>  __("Yes",'acf'),
    124                 0   =>  __("No",'acf'),
    125             ),
    126             'layout'    =>  'horizontal',
    127         ));
    128         ?>
    129     </td>
    130 </tr>
    131         <?php
    132        
    133     }
    134    
    135    
    136     /*
    137     *  create_field()
    138     *
    139     *  Create the HTML interface for your field
    140     *
    141     *  @param   $field - an array holding all the field's data
    142     *
    143     *  @type    action
    144     *  @since   3.6
    145     *  @date    23/01/13
    146     */
    147    
    148     function create_field( $field )
    149     {
    150         // defaults?
    151         /*
    152         $field = array_merge($this->defaults, $field);
    153         */
    154                
    155         // create Field HTML
    156         echo sprintf( '<select id="%d" class="%s" name="%s">', $field['id'], $field['class'], $field['name']  );
    157 
    158         // null
    159         if( $field['allow_null'] )
    160         {
    161             echo '<option value=""> - Select - </option>';
    162         }
    163 
    164         // Nav Menus
    165         $nav_menus = $this->get_nav_menus();
    166 
    167         foreach( $nav_menus as $nav_menu_id => $nav_menu_name ) {
    168             $selected = selected( $field['value'], $nav_menu_id );
    169             echo sprintf( '<option value="%1$d" %3$s>%2$s</option>', $nav_menu_id, $nav_menu_name, $selected );
    170         }
    171 
    172         echo '</select>';
    173     }
    174 
    175     function get_nav_menus() {
    176         $navs = get_terms('nav_menu', array( 'hide_empty' => false ) );
    177        
    178         $nav_menus = array();
    179         foreach( $navs as $nav ) {
    180             $nav_menus[ $nav->term_id ] = $nav->name;
    181         }
    182 
    183         return $nav_menus;
    184     }
    185 
    186     function get_allowed_nav_container_tags() {
    187         $tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
    188         $formatted_tags = array(
    189             array( '0' => 'None' )
    190         );
    191         foreach( $tags as $tag ) {
    192             $formatted_tags[0][$tag] = ucfirst( $tag );
    193         }
    194         return $formatted_tags;
    195     }
    196    
    197     function format_value_for_api( $value, $post_id, $field )
    198     {
    199         // defaults
    200         $field = array_merge($this->defaults, $field);
    201        
    202         if( !$value ) {
     181        if( empty( $value ) ) {
    203182            return false;
    204183        }
    205184
    206185        // check format
    207         if( $field['save_format'] == 'object' ) {
     186        if( 'object' == $field['save_format'] ) {
    208187            $wp_menu_object = wp_get_nav_menu_object( $value );
    209188
    210             if( !$wp_menu_object ) {
     189            if( empty( $wp_menu_object ) ) {
    211190                return false;
    212191            }
     
    214193            $menu_object = new stdClass;
    215194
    216             $menu_object->ID = $wp_menu_object->term_id;
    217             $menu_object->name = $wp_menu_object->name;
    218             $menu_object->slug = $wp_menu_object->slug;
     195            $menu_object->ID    = $wp_menu_object->term_id;
     196            $menu_object->name  = $wp_menu_object->name;
     197            $menu_object->slug  = $wp_menu_object->slug;
    219198            $menu_object->count = $wp_menu_object->count;
    220199
    221200            return $menu_object;
    222201
    223         } elseif( $field['save_format'] == 'menu' ) {
    224            
     202        } elseif( 'menu' == $field['save_format'] ) {
    225203            ob_start();
    226204
     
    229207                'container' => $field['container']
    230208            ) );
    231            
     209
    232210            return ob_get_clean();
    233 
    234         }
    235        
     211        }
     212
     213        // Just return the Nav Menu ID
    236214        return $value;
    237215    }
    238 
    239    
    240216}
    241217
    242 
    243 // create field
    244 new acf_field_nav_menu();
    245 
    246 ?>
     218new ACF_Field_Nav_Menu_V4();
  • advanced-custom-fields-nav-menu-field/trunk/readme.txt

    r894901 r1007668  
    55Author URI: http://faisonz.com
    66Requires at least: 3.4
    7 Tested up to: 3.9
    8 Stable tag: 1.1.2
     7Tested up to: 4.0
     8Stable tag: 2.0.0
    99License: GPL2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1414== Description ==
    1515
    16 Add [Navigation Menus](http://codex.wordpress.org/Navigation_Menus) to [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) (ACF) with the Nav Menu Field plugin! This plugin adds the Nav Menu Field type to ACF (version 4 and up), allowing you to select from the menus you create in the WordPress Admin backend to use on your website's frontend.
     16Add [Navigation Menus](http://codex.wordpress.org/Navigation_Menus) to [Advanced Custom Fields](http://wordpress.org/extend/plugins/advanced-custom-fields/) (ACF) with the Nav Menu Field plugin! This plugin adds the Nav Menu Field type to ACF (version 5 & 4), allowing you to select from the menus you create in the WordPress Admin backend to use on your website's frontend.
    1717
    1818Using ACF, you can set the Nav Menu Field to return the selected menu's:
     
    3030This add-on will work with:
    3131
    32 * version 4 and up
     32* version 5
     33* version 4
    3334
    3435== Installation ==
    3536
    36 This add-on can be treated as both a WP plugin and a theme include.
    37 
    38 = Plugin =
    39 1. Copy the 'advanced-custom-fields-nav-menu-field' folder into your plugins folder
    40 2. Activate the plugin via the Plugins admin page
    41 
    42 = Include =
    43 1.  Copy the 'advanced-custom-fields-nav-menu-field' folder into your theme folder (can use sub folders). You can place the folder anywhere inside the 'wp-content' directory
    44 2.  Edit your functions.php file and add the code below (Make sure the path is correct to include the nav-menu-v4.php file)
    45 
    46 `
    47 add_action('acf/register_fields', 'my_register_fields');
    48 
    49 function my_register_fields()
    50 {
    51     include_once('advanced-custom-fields-nav-menu-field/nav-menu-v4.php');
    52 }
    53 `
     37Follow the following instructions: https://codex.wordpress.org/Managing_Plugins#Installing_Plugins
    5438
    5539== Frequently Asked Questions ==
     
    7660= Will you make this plugin compatible with Advanced Custom Fields v3? =
    7761
    78 I will do that soon, but you really should think about upgrading Advanced Custom Fields. ACF has seen a lot of great changes from v3 to the most current version.
     62No.
    7963
    8064= Why does the Nav Menu returned by your plugin look like an unstyled list of links? =
    8165
    82 I decided to return the Nav Menus without any added style, because I don't know what your website looks like. Frankly, I find it annoying when I have to compete with a plugin's styles, especially when a lot of them use !important. Now I can use this plugin on many sites with only a little extra styling work ahead of me.
     66So that you can style it yourself. I don't want to step on your toes :)
    8367
    8468= I added the Nav Menu Field to Pages, selected my menu when creating a new page, but the menu doesn't show. What gives? =
     
    9882== Changelog ==
    9983
     84= 2.0.0 =
     85* Added ACF v5 class.
     86* Updated code to follow coding standards
     87* Updated the ACF v4 class to use the updated code found in the ACF v5 Class
     88
    10089= 1.1.2 =
    10190* Fixed a silly mistake related to allowing Null for a Nav Menu Field. Basically, it was storing the string "null" when you don't select a menu, that's taken care of now.
     
    112101== Upgrade Notice ==
    113102
     103= 2.0.0 =
     104You now have support for ACF v5, and when the code is read, it makes fewer people cry!
     105
    114106= 1.1.1 =
    115107I forgot to add a default value for the Menu Container field. So to eliminate WP_DEBUG warnings, I added 'div' as the default value. Please upgrade to avoid the warnings.
Note: See TracChangeset for help on using the changeset viewer.