Changeset 10202 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2020/functions.php
- Timestamp:
- 08/25/2020 12:15:55 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2020/functions.php
r10201 r10202 201 201 202 202 /** 203 * Returns whether all post for workshop 204 * 205 * @return array 206 */ 207 function wporg_get_workshops( $options = null ) { 208 $args = array( 209 'post_type' => 'wporg_workshop', 210 ); 211 212 if ( ! is_null( $options ) ) { 213 $args = array_merge( $args, $options ); 214 215 } 216 217 $query = new \WP_Query( $args ); 218 return $query; 203 * Change the query for workshops in some circumstances. 204 * 205 * @param WP_Query $query 206 * 207 * @return void 208 */ 209 function wporg_workshop_modify_query( WP_Query $query ) { 210 if ( is_admin() ) { 211 return; 212 } 213 214 if ( $query->is_main_query() && $query->is_post_type_archive( 'wporg_workshop' ) ) { 215 $featured = wporg_get_featured_workshops(); 216 217 if ( ! empty( $featured ) ) { 218 $featured = reset( $featured ); 219 $query->set( 'post__not_in', array( $featured->ID ) ); 220 } 221 } 222 223 if ( $query->is_main_query() && $query->is_tax( 'wporg_workshop_series' ) ) { 224 $query->set( 'order', 'asc' ); 225 } 226 } 227 add_action( 'pre_get_posts', 'wporg_workshop_modify_query' ); 228 229 /** 230 * Get a query object for displaying workshop posts. 231 * 232 * @return WP_Query 233 */ 234 function wporg_get_workshops_query( array $args = array() ) { 235 $args = wp_parse_args( $args, array( 236 'post_type' => 'wporg_workshop', 237 'post_status' => 'publish', 238 ) ); 239 240 return new WP_Query( $args ); 241 } 242 243 /** 244 * Get a number of workshop posts that are marked as "featured". 245 * 246 * Currently there is no taxonomy or postmeta value to mark a workshop as "featured", 247 * so we're just grabbing the most recent workshops. This may change. 248 * 249 * @param int $number 250 * 251 * @return WP_Post[] 252 */ 253 function wporg_get_featured_workshops( $number = 1 ) { 254 $query = wporg_get_workshops_query( array( 255 'posts_per_page' => $number, 256 ) ); 257 258 return $query->get_posts(); 219 259 } 220 260 … … 265 305 } 266 306 307 /** 308 * Conditionally change or remove the prefix from archive titles. 309 * 310 * @param string $prefix 311 * 312 * @return string 313 */ 314 function wporg_modify_archive_title_prefix( $prefix ) { 315 if ( is_post_type_archive() ) { 316 return ''; 317 } 318 319 return sprintf( 320 '<span class="archive-title-prefix">%s</span>', 321 $prefix 322 ); 323 } 324 add_filter( 'get_the_archive_title_prefix', 'wporg_modify_archive_title_prefix' ); 325 326 /** 327 * Get the series taxonomy term object for a workshop post. 328 * 329 * @param int|WP_Post|null $workshop 330 * 331 * @return WP_Term|bool 332 */ 333 function wporg_workshop_series_get_term( $workshop = null ) { 334 if ( ! $workshop instanceof WP_Post ) { 335 $workshop = get_post( $workshop ); 336 } 337 338 $terms = wp_get_post_terms( $workshop->ID, 'wporg_workshop_series' ); 339 340 if ( empty( $terms ) ) { 341 return false; 342 } 343 344 return $terms[0]; 345 } 346 347 /** 348 * Given a workshop post in a series, get all the workshop posts in the series. 349 * 350 * @param int|WP_Post|null $workshop 351 * 352 * @return WP_Post[] 353 */ 354 function wporg_workshop_series_get_siblings( $workshop = null ) { 355 $term = wporg_workshop_series_get_term( $workshop ); 356 357 if ( ! $term ) { 358 return array(); 359 } 360 361 $args = array( 362 'post_type' => 'wporg_workshop', 363 'post_status' => 'publish', 364 'posts_per_page' => 999, 365 'order' => 'asc', 366 'tax_query' => array( 367 array( 368 'taxonomy' => 'wporg_workshop_series', 369 'terms' => $term->term_id, 370 ), 371 ), 372 ); 373 374 return get_posts( $args ); 375 } 376 377 /** 378 * Given a workshop post in a series, get an adjacent workshop post in the series. 379 * 380 * @param string $which Which adjacent post to retrieve. 'previous' or 'next'. 381 * @param int|WP_Post|null $workshop 382 * 383 * @return WP_Post|bool 384 */ 385 function wporg_workshop_series_get_adjacent( $which, $workshop = null ) { 386 if ( ! $workshop instanceof WP_Post ) { 387 $workshop = get_post( $workshop ); 388 } 389 390 $siblings = wporg_workshop_series_get_siblings( $workshop ); 391 $sibling_ids = wp_list_pluck( $siblings, 'ID' ); 392 $index = array_search( $workshop->ID, $sibling_ids, true ); 393 394 if ( false === $index ) { 395 return false; 396 } 397 398 switch ( $which ) { 399 case 'previous': 400 $index --; 401 break; 402 case 'next': 403 $index ++; 404 break; 405 } 406 407 return $siblings[ $index ] ?? false; 408 }
Note: See TracChangeset
for help on using the changeset viewer.