|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This is a PHP file containing the code for the acf_field_nav_menu class. |
| 4 | + * |
| 5 | + * @package wordpress/secure-custom-fields |
| 6 | + * @since 6.4.1 |
| 7 | + */ |
| 8 | + |
| 9 | +if ( ! class_exists( 'Acf_Field_Nav_Menu' ) ) : |
| 10 | + /** |
| 11 | + * Acf Nav menu field class |
| 12 | + */ |
| 13 | + class Acf_Field_Nav_Menu extends acf_field { |
| 14 | + |
| 15 | + /** |
| 16 | + * This function will setup the field type data |
| 17 | + * |
| 18 | + * @type function |
| 19 | + * @date 5/03/2014 |
| 20 | + * @since ACF 6.4.1 |
| 21 | + */ |
| 22 | + public function initialize() { |
| 23 | + |
| 24 | + // vars |
| 25 | + $this->name = 'nav_menu'; |
| 26 | + $this->label = _x( 'Nav Menu', 'noun', 'secure-custom-fields' ); |
| 27 | + $this->category = 'choice'; |
| 28 | + $this->description = __( 'A dropdown list with a selection of menu choices that you can chose.', 'secure-custom-fields' ); |
| 29 | + $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-select.png'; |
| 30 | + $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/nav_menu/'; |
| 31 | + $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/select/nav_menu-tutorial/'; |
| 32 | + $this->defaults = array( |
| 33 | + 'save_format' => 'id', |
| 34 | + 'allow_null' => 0, |
| 35 | + 'container' => 'div', |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Renders the Nav Menu Field options seen when editing a Nav Menu Field. |
| 41 | + * |
| 42 | + * @param array $field The array representation of the current Nav Menu Field. |
| 43 | + */ |
| 44 | + public function render_field_settings( $field ) { |
| 45 | + // Register the Return Value format setting |
| 46 | + acf_render_field_setting( |
| 47 | + $field, |
| 48 | + array( |
| 49 | + 'label' => __( 'Return Value', 'secure-custom-fields' ), |
| 50 | + 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ), |
| 51 | + 'type' => 'radio', |
| 52 | + 'name' => 'save_format', |
| 53 | + 'layout' => 'horizontal', |
| 54 | + 'choices' => array( |
| 55 | + 'object' => __( 'Nav Menu Object', 'secure-custom-fields' ), |
| 56 | + 'menu' => __( 'Nav Menu HTML', 'secure-custom-fields' ), |
| 57 | + 'id' => __( 'Nav Menu ID', 'secure-custom-fields' ), |
| 58 | + ), |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + // Register the Menu Container setting |
| 63 | + acf_render_field_setting( |
| 64 | + $field, |
| 65 | + array( |
| 66 | + 'label' => __( 'Menu Container', 'secure-custom-fields' ), |
| 67 | + 'instructions' => __( "What to wrap the Menu's ul with (when returning HTML only)", 'secure-custom-fields' ), |
| 68 | + 'type' => 'select', |
| 69 | + 'name' => 'container', |
| 70 | + 'choices' => $this->get_allowed_nav_container_tags(), |
| 71 | + ) |
| 72 | + ); |
| 73 | + |
| 74 | + // Register the Allow Null setting |
| 75 | + acf_render_field_setting( |
| 76 | + $field, |
| 77 | + array( |
| 78 | + 'label' => __( 'Allow Null?', 'secure-custom-fields' ), |
| 79 | + 'type' => 'radio', |
| 80 | + 'name' => 'allow_null', |
| 81 | + 'layout' => 'horizontal', |
| 82 | + 'choices' => array( |
| 83 | + 1 => __( 'Yes', 'secure-custom-fields' ), |
| 84 | + 0 => __( 'No', 'secure-custom-fields' ), |
| 85 | + ), |
| 86 | + ) |
| 87 | + ); |
| 88 | + } |
| 89 | + /** |
| 90 | + * Get the allowed wrapper tags for use with wp_nav_menu(). |
| 91 | + * |
| 92 | + * @return array An array of allowed wrapper tags. |
| 93 | + */ |
| 94 | + private function get_allowed_nav_container_tags() { |
| 95 | + $tags = apply_filters( 'wp_nav_menu_container_allowed_tags', array( 'div', 'nav' ) ); |
| 96 | + $formatted_tags = array( |
| 97 | + '0' => 'None', |
| 98 | + ); |
| 99 | + |
| 100 | + foreach ( $tags as $tag ) { |
| 101 | + $formatted_tags[ $tag ] = $tag; |
| 102 | + } |
| 103 | + |
| 104 | + return $formatted_tags; |
| 105 | + } |
| 106 | + /** |
| 107 | + * Renders the Nav Menu Field. |
| 108 | + * |
| 109 | + * @param array $field The array representation of the current Nav Menu Field. |
| 110 | + */ |
| 111 | + public function render_field( $field ) { |
| 112 | + $allow_null = $field['allow_null']; |
| 113 | + $nav_menus = wp_get_nav_menus( $allow_null ); |
| 114 | + if ( current_theme_supports( 'menus' ) ) { |
| 115 | + if ( empty( $nav_menus ) ) { |
| 116 | + ?> |
| 117 | + <div class="acf-notice" > |
| 118 | + <p> |
| 119 | + <?php esc_html_e( '⚠️ Warning: You don\'t have any menus created please visit', 'secure-custom-fields' ); ?> <a href="<?php echo esc_url( admin_url( 'nav-menus.php' ) ); ?>"><?php esc_html_e( 'this', 'secure-custom-fields' ); ?></a> <?php esc_html_e( 'link to create menus.', 'secure-custom-fields' ); ?> |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + |
| 123 | + <?php |
| 124 | + } else { |
| 125 | + ?> |
| 126 | + <select id="<?php esc_attr( $field['id'] ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>"> |
| 127 | + <?php |
| 128 | + if ( $allow_null ) { |
| 129 | + ?> |
| 130 | + <option value=""> |
| 131 | + <?php esc_html_e( '- Select -', 'secure-custom-fields' ); ?> |
| 132 | + </option> |
| 133 | + <?php |
| 134 | + } |
| 135 | + foreach ( $nav_menus as $nav_menu_name ) { |
| 136 | + ?> |
| 137 | + <option value="<?php echo esc_attr( $nav_menu_name->term_id ); ?>" <?php selected( $field['value'], $nav_menu_name->term_id ); ?>> |
| 138 | + <?php echo esc_html( $nav_menu_name->name ); ?> |
| 139 | + </option> |
| 140 | + <?php } ?> |
| 141 | + </select> |
| 142 | + <?php |
| 143 | + } |
| 144 | + } else { |
| 145 | + ?> |
| 146 | + <div class="acf-notice"> |
| 147 | + <p> |
| 148 | + <?php esc_html_e( '⚠️ Warning: The theme does not support navigation menus.', 'secure-custom-fields' ); ?> |
| 149 | + </p> |
| 150 | + </div> |
| 151 | + <?php |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Renders the Nav Menu Field. |
| 157 | + * |
| 158 | + * @param int $value The Nav Menu ID selected for this Nav Menu Field. |
| 159 | + * @param int $post_id The Post ID this $value is associated with. |
| 160 | + * @param array $field The array representation of the current Nav Menu Field. |
| 161 | + * |
| 162 | + * @return mixed The Nav Menu ID, or the Nav Menu HTML, or the Nav Menu Object, or false. |
| 163 | + */ |
| 164 | + public function format_value( $value, $post_id, $field ) { |
| 165 | + // bail early if no value |
| 166 | + if ( empty( $value ) ) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + // check format |
| 171 | + if ( 'object' === $field['save_format'] ) { |
| 172 | + $wp_menu_object = wp_get_nav_menu_object( $value ); |
| 173 | + |
| 174 | + if ( empty( $wp_menu_object ) ) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + $menu_object = new stdClass(); |
| 179 | + |
| 180 | + $menu_object->ID = $wp_menu_object->term_id; |
| 181 | + $menu_object->name = $wp_menu_object->name; |
| 182 | + $menu_object->slug = $wp_menu_object->slug; |
| 183 | + $menu_object->count = $wp_menu_object->count; |
| 184 | + |
| 185 | + return $menu_object; |
| 186 | + } elseif ( 'menu' === $field['save_format'] ) { |
| 187 | + ob_start(); |
| 188 | + |
| 189 | + wp_nav_menu( |
| 190 | + array( |
| 191 | + 'menu' => $value, |
| 192 | + 'container' => $field['container'], |
| 193 | + ) |
| 194 | + ); |
| 195 | + |
| 196 | + return ob_get_clean(); |
| 197 | + } |
| 198 | + |
| 199 | + // Just return the Nav Menu ID |
| 200 | + return $value; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + // initialize |
| 206 | + acf_register_field_type( 'Acf_Field_Nav_Menu' ); |
| 207 | +endif; // class_exists check |
0 commit comments