jarednova /
timber
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | class TimberMenuItem extends TimberCore implements TimberCoreInterface { |
||
| 4 | |||
| 5 | public $children; |
||
| 6 | public $has_child_class = false; |
||
| 7 | public $classes = array(); |
||
| 8 | public $class = ''; |
||
| 9 | public $level = 0; |
||
| 10 | public $post_name; |
||
| 11 | public $type; |
||
| 12 | public $url; |
||
| 13 | |||
| 14 | public $PostClass = 'TimberPost'; |
||
| 15 | |||
| 16 | protected $_name; |
||
| 17 | protected $_menu_item_object_id; |
||
| 18 | protected $_menu_item_url; |
||
| 19 | protected $menu_object; |
||
| 20 | protected $master_object; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * |
||
| 24 | * |
||
| 25 | * @param array|object $data |
||
| 26 | */ |
||
| 27 | public function __construct( $data ) { |
||
| 28 | $data = (object) $data; |
||
| 29 | $this->import( $data ); |
||
| 30 | $this->import_classes( $data ); |
||
| 31 | if ( isset( $this->name ) ) { |
||
| 32 | $this->_name = $this->name; |
||
|
0 ignored issues
–
show
|
|||
| 33 | } |
||
| 34 | $this->name = $this->name(); |
||
| 35 | $this->add_class( 'menu-item-' . $this->ID ); |
||
| 36 | $this->menu_object = $data; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return string the label for the menu item |
||
| 41 | */ |
||
| 42 | public function __toString() { |
||
| 43 | return $this->name(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * add a class the menu item should have |
||
| 48 | * @param string $class_name to be added |
||
| 49 | */ |
||
| 50 | public function add_class( $class_name ) { |
||
| 51 | $this->classes[] = $class_name; |
||
| 52 | $this->class .= ' ' . $class_name; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The label for the menu item |
||
| 57 | * @api |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function name() { |
||
| 61 | if ( $title = $this->title() ) { |
||
| 62 | return $title; |
||
| 63 | } |
||
| 64 | if ( isset( $this->_name ) ) { |
||
| 65 | return $this->_name; |
||
| 66 | } |
||
| 67 | return ''; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The slug for the menu item |
||
| 72 | * @api |
||
| 73 | * @example |
||
| 74 | * ```twig |
||
| 75 | * <ul> |
||
| 76 | * {% for item in menu.items %} |
||
| 77 | * <li class="{{item.slug}}"> |
||
| 78 | * <a href="{{item.link}}">{{item.name}}</a> |
||
| 79 | * </li> |
||
| 80 | * {% endfor %} |
||
| 81 | * </ul> |
||
| 82 | * @return string the slug of the menu item kinda-like-this |
||
| 83 | */ |
||
| 84 | public function slug() { |
||
| 85 | if ( !isset( $this->master_object ) ) { |
||
| 86 | $this->master_object = $this->get_master_object(); |
||
| 87 | } |
||
| 88 | if ( isset( $this->master_object->post_name ) && $this->master_object->post_name ) { |
||
| 89 | return $this->master_object->post_name; |
||
| 90 | } |
||
| 91 | return $this->post_name; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @internal |
||
| 96 | * @return mixed whatever object (Post, Term, etc.) the menu item represents |
||
| 97 | */ |
||
| 98 | protected function get_master_object() { |
||
| 99 | if ( isset( $this->_menu_item_object_id ) ) { |
||
| 100 | return new $this->PostClass( $this->_menu_item_object_id ); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @internal |
||
| 106 | * @see TimberMenuItem::link |
||
| 107 | * @return string an absolute URL http://example.org/my-page |
||
| 108 | */ |
||
| 109 | function get_link() { |
||
| 110 | if ( !isset( $this->url ) || !$this->url ) { |
||
| 111 | if ( isset( $this->_menu_item_type ) && $this->_menu_item_type == 'custom' ) { |
||
| 112 | $this->url = $this->_menu_item_url; |
||
| 113 | } else if ( isset( $this->menu_object ) && method_exists( $this->menu_object, 'get_link' ) ) { |
||
| 114 | $this->url = $this->menu_object->get_link(); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | return $this->url; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @internal |
||
| 122 | * @see TimberMenuItem::path() |
||
| 123 | * @return string a relative url /my-page |
||
| 124 | */ |
||
| 125 | function get_path() { |
||
| 126 | return TimberURLHelper::get_rel_url( $this->get_link() ); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * |
||
| 131 | * |
||
| 132 | * @param TimberMenuItem $item |
||
| 133 | */ |
||
| 134 | function add_child( $item ) { |
||
| 135 | if ( !$this->has_child_class ) { |
||
| 136 | $this->add_class( 'menu-item-has-children' ); |
||
| 137 | $this->has_child_class = true; |
||
| 138 | } |
||
| 139 | if ( !isset( $this->children ) ) { |
||
| 140 | $this->children = array(); |
||
| 141 | } |
||
| 142 | $this->children[] = $item; |
||
| 143 | $item->level = $this->level + 1; |
||
| 144 | if ($item->children) { |
||
| 145 | $this->update_child_levels(); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * |
||
| 151 | * @internal |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | function update_child_levels() { |
||
| 155 | if (is_array($this->children)) { |
||
| 156 | foreach( $this->children as $child ) { |
||
| 157 | $child->level = $this->level + 1; |
||
| 158 | $child->update_child_levels(); |
||
| 159 | } |
||
| 160 | return true; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Imports the classes to be used in CSS |
||
| 166 | * @internal |
||
| 167 | * @param array|object $data |
||
| 168 | */ |
||
| 169 | function import_classes( $data ) { |
||
| 170 | if ( is_array($data) ) { |
||
| 171 | $data = (object) $data; |
||
| 172 | } |
||
| 173 | $this->classes = array_merge( $this->classes, $data->classes ); |
||
| 174 | $this->classes = array_unique( $this->classes ); |
||
| 175 | $this->classes = apply_filters( 'nav_menu_css_class', $this->classes, $this ); |
||
| 176 | $this->class = trim( implode( ' ', $this->classes ) ); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * |
||
| 181 | * @internal |
||
| 182 | * @return array|bool |
||
| 183 | */ |
||
| 184 | function get_children() { |
||
| 185 | if ( isset( $this->children ) ) { |
||
| 186 | return $this->children; |
||
| 187 | } |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Checks to see if the menu item is an external link so if my site is `example.org`, `google.com/whatever` is an external link. Helpful when creating rules for the target of a link |
||
| 193 | * @api |
||
| 194 | * @example |
||
| 195 | * ```twig |
||
| 196 | * <a href="{{ item.link }}" target="{{ item.is_external ? '_blank' : '_self' }}"> |
||
| 197 | * ``` |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | function is_external() { |
||
| 201 | if ( $this->type != 'custom' ) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | return TimberURLHelper::is_external( $this->url ); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $key lookup key |
||
| 209 | * @return mixed whatever value is storied in the database |
||
| 210 | */ |
||
| 211 | public function meta( $key ) { |
||
| 212 | if ( is_object( $this->menu_object ) && method_exists( $this->menu_object, 'meta' ) ) { |
||
| 213 | return $this->menu_object->meta( $key ); |
||
| 214 | } |
||
| 215 | if ( isset( $this->$key ) ) { |
||
| 216 | return $this->$key; |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /* Aliases */ |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the child [TimberMenuItems](#TimberMenuItem)s of a [TimberMenuItem](#TimberMenuItem) |
||
| 224 | * @api |
||
| 225 | * @return array|bool |
||
| 226 | */ |
||
| 227 | public function children() { |
||
| 228 | return $this->get_children(); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Checks to see if a link is external, helpful when creating rules for the target of a link |
||
| 233 | * @see TimberMenuItem::is_external |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function external() { |
||
| 237 | return $this->is_external(); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the full link to a Menu Item |
||
| 242 | * @api |
||
| 243 | * @example |
||
| 244 | * ```twig |
||
| 245 | * {% for item in menu.items %} |
||
| 246 | * <li><a href="{{ item.link }}">{{ item.title }}</a></li> |
||
| 247 | * {% endfor %} |
||
| 248 | * ``` |
||
| 249 | * @return string a full URL like http://mysite.com/thing/ |
||
| 250 | */ |
||
| 251 | public function link() { |
||
| 252 | return $this->get_link(); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Return the relative path of a Menu Item's link |
||
| 257 | * @example |
||
| 258 | * ```twig |
||
| 259 | * {% for item in menu.items %} |
||
| 260 | * <li><a href="{{ item.path }}">{{ item.title }}</a></li> |
||
| 261 | * {% endfor %} |
||
| 262 | * ``` |
||
| 263 | * @see get_path() |
||
| 264 | * @return string the path of a URL like /foo |
||
| 265 | */ |
||
| 266 | public function path() { |
||
| 267 | return $this->get_path(); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Gets the link a menu item points at |
||
| 272 | * @internal |
||
| 273 | * @deprecated since 0.21.7 use link instead |
||
| 274 | * @see link() |
||
| 275 | * @return string a full URL like http://mysite.com/thing/ |
||
| 276 | */ |
||
| 277 | public function permalink() { |
||
| 278 | return $this->get_link(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @internal |
||
| 283 | * @deprecated since 0.21.7, use link instead |
||
| 284 | * @see link() |
||
| 285 | * @return string a full URL like http://mysite.com/thing/ |
||
| 286 | */ |
||
| 287 | public function get_permalink() { |
||
| 288 | return $this->get_link(); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Gets the public label for the menu item |
||
| 293 | * @example |
||
| 294 | * ```twig |
||
| 295 | * {% for item in menu.items %} |
||
| 296 | * <li><a href="{{ item.link }}">{{ item.title }}</a></li> |
||
| 297 | * {% endfor %} |
||
| 298 | * ``` |
||
| 299 | * @return string the public label like Foo |
||
| 300 | */ |
||
| 301 | public function title() { |
||
| 302 | if ( isset( $this->__title ) ) { |
||
| 303 | return $this->__title; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | } |
||
| 308 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: