wp/wp-includes/bookmark-template.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    20  * used by themes.
    20  * used by themes.
    21  *
    21  *
    22  * @since 2.1.0
    22  * @since 2.1.0
    23  * @access private
    23  * @access private
    24  *
    24  *
    25  * @param array $bookmarks List of bookmarks to traverse.
    25  * @param array        $bookmarks List of bookmarks to traverse.
    26  * @param string|array $args {
    26  * @param string|array $args {
    27  *     Optional. Bookmarks arguments.
    27  *     Optional. Bookmarks arguments.
    28  *
    28  *
    29  *     @type int|bool $show_updated     Whether to show the time the bookmark was last updated.
    29  *     @type int|bool $show_updated     Whether to show the time the bookmark was last updated.
    30  *                                      Accepts 1|true or 0|false. Default 0|false.
    30  *                                      Accepts 1|true or 0|false. Default 0|false.
    60 		'show_rating'      => 0,
    60 		'show_rating'      => 0,
    61 		'link_before'      => '',
    61 		'link_before'      => '',
    62 		'link_after'       => '',
    62 		'link_after'       => '',
    63 	);
    63 	);
    64 
    64 
    65 	$r = wp_parse_args( $args, $defaults );
    65 	$parsed_args = wp_parse_args( $args, $defaults );
    66 
    66 
    67 	$output = ''; // Blank string to start with.
    67 	$output = ''; // Blank string to start with.
    68 
    68 
    69 	foreach ( (array) $bookmarks as $bookmark ) {
    69 	foreach ( (array) $bookmarks as $bookmark ) {
    70 		if ( ! isset( $bookmark->recently_updated ) ) {
    70 		if ( ! isset( $bookmark->recently_updated ) ) {
    71 			$bookmark->recently_updated = false;
    71 			$bookmark->recently_updated = false;
    72 		}
    72 		}
    73 		$output .= $r['before'];
    73 		$output .= $parsed_args['before'];
    74 		if ( $r['show_updated'] && $bookmark->recently_updated ) {
    74 		if ( $parsed_args['show_updated'] && $bookmark->recently_updated ) {
    75 			$output .= '<em>';
    75 			$output .= '<em>';
    76 		}
    76 		}
    77 		$the_link = '#';
    77 		$the_link = '#';
    78 		if ( ! empty( $bookmark->link_url ) ) {
    78 		if ( ! empty( $bookmark->link_url ) ) {
    79 			$the_link = esc_url( $bookmark->link_url );
    79 			$the_link = esc_url( $bookmark->link_url );
    80 		}
    80 		}
    81 		$desc  = esc_attr( sanitize_bookmark_field( 'link_description', $bookmark->link_description, $bookmark->link_id, 'display' ) );
    81 		$desc  = esc_attr( sanitize_bookmark_field( 'link_description', $bookmark->link_description, $bookmark->link_id, 'display' ) );
    82 		$name  = esc_attr( sanitize_bookmark_field( 'link_name', $bookmark->link_name, $bookmark->link_id, 'display' ) );
    82 		$name  = esc_attr( sanitize_bookmark_field( 'link_name', $bookmark->link_name, $bookmark->link_id, 'display' ) );
    83 		$title = $desc;
    83 		$title = $desc;
    84 
    84 
    85 		if ( $r['show_updated'] ) {
    85 		if ( $parsed_args['show_updated'] ) {
    86 			if ( '00' != substr( $bookmark->link_updated_f, 0, 2 ) ) {
    86 			if ( '00' !== substr( $bookmark->link_updated_f, 0, 2 ) ) {
    87 				$title .= ' (';
    87 				$title .= ' (';
    88 				$title .= sprintf(
    88 				$title .= sprintf(
       
    89 					/* translators: %s: Date and time of last update. */
    89 					__( 'Last updated: %s' ),
    90 					__( 'Last updated: %s' ),
    90 					date(
    91 					gmdate(
    91 						get_option( 'links_updated_date_format' ),
    92 						get_option( 'links_updated_date_format' ),
    92 						$bookmark->link_updated_f + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )
    93 						$bookmark->link_updated_f + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )
    93 					)
    94 					)
    94 				);
    95 				);
    95 				$title .= ')';
    96 				$title .= ')';
    96 			}
    97 			}
    97 		}
    98 		}
    98 		$alt = ' alt="' . $name . ( $r['show_description'] ? ' ' . $title : '' ) . '"';
    99 		$alt = ' alt="' . $name . ( $parsed_args['show_description'] ? ' ' . $title : '' ) . '"';
    99 
   100 
   100 		if ( '' != $title ) {
   101 		if ( '' !== $title ) {
   101 			$title = ' title="' . $title . '"';
   102 			$title = ' title="' . $title . '"';
   102 		}
   103 		}
   103 		$rel = $bookmark->link_rel;
   104 		$rel = $bookmark->link_rel;
   104 		if ( '' != $rel ) {
   105 		if ( '' !== $rel ) {
   105 			$rel = ' rel="' . esc_attr( $rel ) . '"';
   106 			$rel = ' rel="' . esc_attr( $rel ) . '"';
   106 		}
   107 		}
   107 		$target = $bookmark->link_target;
   108 		$target = $bookmark->link_target;
   108 		if ( '' != $target ) {
   109 		if ( '' !== $target ) {
   109 			$target = ' target="' . $target . '"';
   110 			$target = ' target="' . $target . '"';
   110 		}
   111 		}
   111 		$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
   112 		$output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
   112 
   113 
   113 		$output .= $r['link_before'];
   114 		$output .= $parsed_args['link_before'];
   114 
   115 
   115 		if ( $bookmark->link_image != null && $r['show_images'] ) {
   116 		if ( null != $bookmark->link_image && $parsed_args['show_images'] ) {
   116 			if ( strpos( $bookmark->link_image, 'http' ) === 0 ) {
   117 			if ( strpos( $bookmark->link_image, 'http' ) === 0 ) {
   117 				$output .= "<img src=\"$bookmark->link_image\" $alt $title />";
   118 				$output .= "<img src=\"$bookmark->link_image\" $alt $title />";
   118 			} else { // If it's a relative path
   119 			} else { // If it's a relative path.
   119 				$output .= '<img src="' . get_option( 'siteurl' ) . "$bookmark->link_image\" $alt $title />";
   120 				$output .= '<img src="' . get_option( 'siteurl' ) . "$bookmark->link_image\" $alt $title />";
   120 			}
   121 			}
   121 			if ( $r['show_name'] ) {
   122 			if ( $parsed_args['show_name'] ) {
   122 				$output .= " $name";
   123 				$output .= " $name";
   123 			}
   124 			}
   124 		} else {
   125 		} else {
   125 			$output .= $name;
   126 			$output .= $name;
   126 		}
   127 		}
   127 
   128 
   128 		$output .= $r['link_after'];
   129 		$output .= $parsed_args['link_after'];
   129 
   130 
   130 		$output .= '</a>';
   131 		$output .= '</a>';
   131 
   132 
   132 		if ( $r['show_updated'] && $bookmark->recently_updated ) {
   133 		if ( $parsed_args['show_updated'] && $bookmark->recently_updated ) {
   133 			$output .= '</em>';
   134 			$output .= '</em>';
   134 		}
   135 		}
   135 
   136 
   136 		if ( $r['show_description'] && '' != $desc ) {
   137 		if ( $parsed_args['show_description'] && '' !== $desc ) {
   137 			$output .= $r['between'] . $desc;
   138 			$output .= $parsed_args['between'] . $desc;
   138 		}
   139 		}
   139 
   140 
   140 		if ( $r['show_rating'] ) {
   141 		if ( $parsed_args['show_rating'] ) {
   141 			$output .= $r['between'] . sanitize_bookmark_field(
   142 			$output .= $parsed_args['between'] . sanitize_bookmark_field(
   142 				'link_rating',
   143 				'link_rating',
   143 				$bookmark->link_rating,
   144 				$bookmark->link_rating,
   144 				$bookmark->link_id,
   145 				$bookmark->link_id,
   145 				'display'
   146 				'display'
   146 			);
   147 			);
   147 		}
   148 		}
   148 		$output .= $r['after'] . "\n";
   149 		$output .= $parsed_args['after'] . "\n";
   149 	} // end while
   150 	} // End while.
   150 
   151 
   151 	return $output;
   152 	return $output;
   152 }
   153 }
   153 
   154 
   154 /**
   155 /**
   170  *     @type string   $orderby          How to order the links by. Accepts post fields. Default 'name'.
   171  *     @type string   $orderby          How to order the links by. Accepts post fields. Default 'name'.
   171  *     @type string   $order            Whether to order bookmarks in ascending or descending order.
   172  *     @type string   $order            Whether to order bookmarks in ascending or descending order.
   172  *                                      Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
   173  *                                      Accepts 'ASC' (ascending) or 'DESC' (descending). Default 'ASC'.
   173  *     @type int      $limit            Amount of bookmarks to display. Accepts 1+ or -1 for all.
   174  *     @type int      $limit            Amount of bookmarks to display. Accepts 1+ or -1 for all.
   174  *                                      Default -1.
   175  *                                      Default -1.
   175  *     @type string   $category         Comma-separated list of category ids to include links from.
   176  *     @type string   $category         Comma-separated list of category IDs to include links from.
   176  *                                      Default empty.
   177  *                                      Default empty.
   177  *     @type string   $category_name    Category to retrieve links for by name. Default empty.
   178  *     @type string   $category_name    Category to retrieve links for by name. Default empty.
   178  *     @type int|bool $hide_invisible   Whether to show or hide links marked as 'invisible'. Accepts
   179  *     @type int|bool $hide_invisible   Whether to show or hide links marked as 'invisible'. Accepts
   179  *                                      1|true or 0|false. Default 1|true.
   180  *                                      1|true or 0|false. Default 1|true.
   180  *     @type int|bool $show_updated     Whether to display the time the bookmark was last updated.
   181  *     @type int|bool $show_updated     Whether to display the time the bookmark was last updated.
   199  *                                      is true. Default 'name'.
   200  *                                      is true. Default 'name'.
   200  *     @type string   $category_order   Whether to order categories in ascending or descending order if
   201  *     @type string   $category_order   Whether to order categories in ascending or descending order if
   201  *                                      $categorize is true. Accepts 'ASC' (ascending) or 'DESC' (descending).
   202  *                                      $categorize is true. Accepts 'ASC' (ascending) or 'DESC' (descending).
   202  *                                      Default 'ASC'.
   203  *                                      Default 'ASC'.
   203  * }
   204  * }
   204  * @return string|void Will only return if echo option is set to not echo. Default is not return anything.
   205  * @return void|string Void if 'echo' argument is true, HTML list of bookmarks if 'echo' is false.
   205  */
   206  */
   206 function wp_list_bookmarks( $args = '' ) {
   207 function wp_list_bookmarks( $args = '' ) {
   207 	$defaults = array(
   208 	$defaults = array(
   208 		'orderby'          => 'name',
   209 		'orderby'          => 'name',
   209 		'order'            => 'ASC',
   210 		'order'            => 'ASC',
   223 		'class'            => 'linkcat',
   224 		'class'            => 'linkcat',
   224 		'category_before'  => '<li id="%id" class="%class">',
   225 		'category_before'  => '<li id="%id" class="%class">',
   225 		'category_after'   => '</li>',
   226 		'category_after'   => '</li>',
   226 	);
   227 	);
   227 
   228 
   228 	$r = wp_parse_args( $args, $defaults );
   229 	$parsed_args = wp_parse_args( $args, $defaults );
   229 
   230 
   230 	$output = '';
   231 	$output = '';
   231 
   232 
   232 	if ( ! is_array( $r['class'] ) ) {
   233 	if ( ! is_array( $parsed_args['class'] ) ) {
   233 		$r['class'] = explode( ' ', $r['class'] );
   234 		$parsed_args['class'] = explode( ' ', $parsed_args['class'] );
   234 	}
   235 	}
   235 	$r['class'] = array_map( 'sanitize_html_class', $r['class'] );
   236 	$parsed_args['class'] = array_map( 'sanitize_html_class', $parsed_args['class'] );
   236 	$r['class'] = trim( join( ' ', $r['class'] ) );
   237 	$parsed_args['class'] = trim( join( ' ', $parsed_args['class'] ) );
   237 
   238 
   238 	if ( $r['categorize'] ) {
   239 	if ( $parsed_args['categorize'] ) {
   239 		$cats = get_terms(
   240 		$cats = get_terms(
   240 			'link_category',
       
   241 			array(
   241 			array(
   242 				'name__like'   => $r['category_name'],
   242 				'taxonomy'     => 'link_category',
   243 				'include'      => $r['category'],
   243 				'name__like'   => $parsed_args['category_name'],
   244 				'exclude'      => $r['exclude_category'],
   244 				'include'      => $parsed_args['category'],
   245 				'orderby'      => $r['category_orderby'],
   245 				'exclude'      => $parsed_args['exclude_category'],
   246 				'order'        => $r['category_order'],
   246 				'orderby'      => $parsed_args['category_orderby'],
       
   247 				'order'        => $parsed_args['category_order'],
   247 				'hierarchical' => 0,
   248 				'hierarchical' => 0,
   248 			)
   249 			)
   249 		);
   250 		);
   250 		if ( empty( $cats ) ) {
   251 		if ( empty( $cats ) ) {
   251 			$r['categorize'] = false;
   252 			$parsed_args['categorize'] = false;
   252 		}
   253 		}
   253 	}
   254 	}
   254 
   255 
   255 	if ( $r['categorize'] ) {
   256 	if ( $parsed_args['categorize'] ) {
   256 		// Split the bookmarks into ul's for each category
   257 		// Split the bookmarks into ul's for each category.
   257 		foreach ( (array) $cats as $cat ) {
   258 		foreach ( (array) $cats as $cat ) {
   258 			$params    = array_merge( $r, array( 'category' => $cat->term_id ) );
   259 			$params    = array_merge( $parsed_args, array( 'category' => $cat->term_id ) );
   259 			$bookmarks = get_bookmarks( $params );
   260 			$bookmarks = get_bookmarks( $params );
   260 			if ( empty( $bookmarks ) ) {
   261 			if ( empty( $bookmarks ) ) {
   261 				continue;
   262 				continue;
   262 			}
   263 			}
   263 			$output .= str_replace(
   264 			$output .= str_replace(
   264 				array( '%id', '%class' ),
   265 				array( '%id', '%class' ),
   265 				array( "linkcat-$cat->term_id", $r['class'] ),
   266 				array( "linkcat-$cat->term_id", $parsed_args['class'] ),
   266 				$r['category_before']
   267 				$parsed_args['category_before']
   267 			);
   268 			);
   268 			/**
   269 			/**
   269 			 * Filters the category name.
   270 			 * Filters the category name.
   270 			 *
   271 			 *
   271 			 * @since 2.2.0
   272 			 * @since 2.2.0
   272 			 *
   273 			 *
   273 			 * @param string $cat_name The category name.
   274 			 * @param string $cat_name The category name.
   274 			 */
   275 			 */
   275 			$catname = apply_filters( 'link_category', $cat->name );
   276 			$catname = apply_filters( 'link_category', $cat->name );
   276 
   277 
   277 			$output .= $r['title_before'];
   278 			$output .= $parsed_args['title_before'];
   278 			$output .= $catname;
   279 			$output .= $catname;
   279 			$output .= $r['title_after'];
   280 			$output .= $parsed_args['title_after'];
   280 			$output .= "\n\t<ul class='xoxo blogroll'>\n";
   281 			$output .= "\n\t<ul class='xoxo blogroll'>\n";
   281 			$output .= _walk_bookmarks( $bookmarks, $r );
   282 			$output .= _walk_bookmarks( $bookmarks, $parsed_args );
   282 			$output .= "\n\t</ul>\n";
   283 			$output .= "\n\t</ul>\n";
   283 			$output .= $r['category_after'] . "\n";
   284 			$output .= $parsed_args['category_after'] . "\n";
   284 		}
   285 		}
   285 	} else {
   286 	} else {
   286 		//output one single list using title_li for the title
   287 		// Output one single list using title_li for the title.
   287 		$bookmarks = get_bookmarks( $r );
   288 		$bookmarks = get_bookmarks( $parsed_args );
   288 
   289 
   289 		if ( ! empty( $bookmarks ) ) {
   290 		if ( ! empty( $bookmarks ) ) {
   290 			if ( ! empty( $r['title_li'] ) ) {
   291 			if ( ! empty( $parsed_args['title_li'] ) ) {
   291 				$output .= str_replace(
   292 				$output .= str_replace(
   292 					array( '%id', '%class' ),
   293 					array( '%id', '%class' ),
   293 					array( 'linkcat-' . $r['category'], $r['class'] ),
   294 					array( 'linkcat-' . $parsed_args['category'], $parsed_args['class'] ),
   294 					$r['category_before']
   295 					$parsed_args['category_before']
   295 				);
   296 				);
   296 				$output .= $r['title_before'];
   297 				$output .= $parsed_args['title_before'];
   297 				$output .= $r['title_li'];
   298 				$output .= $parsed_args['title_li'];
   298 				$output .= $r['title_after'];
   299 				$output .= $parsed_args['title_after'];
   299 				$output .= "\n\t<ul class='xoxo blogroll'>\n";
   300 				$output .= "\n\t<ul class='xoxo blogroll'>\n";
   300 				$output .= _walk_bookmarks( $bookmarks, $r );
   301 				$output .= _walk_bookmarks( $bookmarks, $parsed_args );
   301 				$output .= "\n\t</ul>\n";
   302 				$output .= "\n\t</ul>\n";
   302 				$output .= $r['category_after'] . "\n";
   303 				$output .= $parsed_args['category_after'] . "\n";
   303 			} else {
   304 			} else {
   304 				$output .= _walk_bookmarks( $bookmarks, $r );
   305 				$output .= _walk_bookmarks( $bookmarks, $parsed_args );
   305 			}
   306 			}
   306 		}
   307 		}
   307 	}
   308 	}
   308 
   309 
   309 	/**
   310 	/**
   313 	 *
   314 	 *
   314 	 * @param string $html The HTML list of bookmarks.
   315 	 * @param string $html The HTML list of bookmarks.
   315 	 */
   316 	 */
   316 	$html = apply_filters( 'wp_list_bookmarks', $output );
   317 	$html = apply_filters( 'wp_list_bookmarks', $output );
   317 
   318 
   318 	if ( ! $r['echo'] ) {
   319 	if ( $parsed_args['echo'] ) {
       
   320 		echo $html;
       
   321 	} else {
   319 		return $html;
   322 		return $html;
   320 	}
   323 	}
   321 	echo $html;
       
   322 }
   324 }