Changeset 2052010
- Timestamp:
- 03/17/2019 11:01:18 AM (5 years ago)
- Location:
- custom-archives
- Files:
-
- 2 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
custom-archives/trunk/README.md
r2041692 r2052010 1 1 # Custom Archives 2 2 3 [Custom Archives](https://wordpress.org/plugins/custom-archives/) lets you select a page to be used as a custom archive page for your custom psot types.3 [Custom Archives](https://wordpress.org/plugins/custom-archives/) lets you select a page to be used as at types. 4 4 5 5 ## Thank You … … 7 7 Thanks go to Human Made who released [Page for post type](https://github.com/humanmade/page-for-post-type/) which was used as inspiration for building this plugin. 8 8 9 ## Donate10 11 In case you really like using this plugin and would like to support me, please consider [donating via PayPal](https://www.paypal.me/dtj27). Any amount is hugely appreciated and allows me to develop the plugin further. Thank you!12 13 9 ## Useful Links 14 10 15 11 - [Download Plugin](https://wordpress.org/plugins/custom-archives/) 16 - [ Help with Translations](https://translate.wordpress.org/projects/wp-plugins/custom-archives)12 - [](https://translate.wordpress.org/projects/wp-plugins/custom-archives) 17 13 - [GitHub Repository](https://github.com/danieltj27/Custom-Archives) 18 - [Me](https://danieltj.uk/)19 -
custom-archives/trunk/custom-archives.php
r2050054 r2052010 5 5 * Plugin URI: https://wordpress.org/plugins/custom-archives/ 6 6 * Description: Select a page to be a custom archive for your post types. 7 7 8 * Author: Daniel James 8 9 * Author URI: https://danieltj.uk/ 9 10 * Text Domain: custom-archives 10 * Version: 2.111 11 */ 12 12 … … 29 29 30 30 if ( ! defined( 'ABSPATH' ) ) { 31 31 32 die(); 33 32 34 } 33 35 34 $Custom_Archives =new Custom_Archives;36 new Custom_Archives; 35 37 36 38 class Custom_Archives { 37 39 38 40 /** 39 * The plugin version. 40 * 41 * @var string 42 */ 43 protected static $version = '2.1'; 44 45 /** 46 * Puts the archive into WordPress. 41 * Hook into WordPress. 47 42 * 48 43 * @return void … … 50 45 public function __construct() { 51 46 52 add_action( 'plugins_loaded', array( __CLASS__, 'load_text_domain' ), 10, 0 );53 47 add_action( 'admin_menu', array( __CLASS__, 'add_admin_page' ), 10, 0 ); 54 48 add_action( 'admin_init', array( __CLASS__, 'add_settings' ), 10, 0 ); … … 58 52 add_action( 'admin_bar_menu', array( __CLASS__, 'add_edit_link' ), 80, 1 ); 59 53 60 add_filter( 'plugin_action_links', array( __CLASS__, 'add_donate_link' ), 10, 2 );61 54 add_filter( 'display_post_states', array( __CLASS__, 'add_post_states' ), 20, 2 ); 62 55 add_filter( 'document_title_parts', array( __CLASS__, 'rewrite_page_title' ), 15, 1 ); … … 68 61 69 62 /** 70 * Load the plugin text domain. 71 * 72 * @return void 73 */ 74 public static function load_text_domain() { 75 76 load_plugin_textdomain( 'custom-archives', false, untrailingslashit( dirname( __FILE__ ) ) . '/languages' ); 77 78 } 79 80 /** 81 * Gets all available post types. 63 * Gets all custom post types. 82 64 * 83 65 * @return array $post_types An array of post types. 84 66 */ 85 public static function get_post_types() { 86 87 // Get all custom types 67 public static function get_custom_post_types() { 68 88 69 $get_types = get_post_types( 89 70 array( … … 99 80 foreach ( $get_types as $key => $value ) { 100 81 101 // Add the post type 82 // Add the post type 102 83 $post_types[ $value->name ] = $value; 103 84 … … 106 87 /** 107 88 * Filter the array of post types. 89 90 108 91 * 109 92 * @param array $post_types The array of post types. … … 122 105 * @return array $pages 123 106 */ 124 public static function get_archive_ids() { 125 126 // Get the post types 127 $types = self::get_post_types(); 107 public static function get_custom_archive_ids() { 108 109 $types = self::get_custom_post_types(); 128 110 129 111 $pages = array(); … … 137 119 } 138 120 139 // Get the option for this post type121 // Get the 140 122 $page_id = get_option( 'archive_page_' . $type->name, false ); 141 123 … … 146 128 } 147 129 148 // Add the page id to the array149 130 $pages[ $type->name ] = $page_id; 150 131 … … 153 134 /** 154 135 * Filter the array of custom archives. 136 137 155 138 * 156 139 * @param array $pages The array of custom archive ids. … … 171 154 public static function get_custom_archive_post_type( $page_id ) { 172 155 173 // Get the custom archive ids 174 $ids = self::get_archive_ids(); 175 176 // Search the array 156 $ids = self::get_custom_archive_ids(); 157 158 // Does the page ID exist? 177 159 $post_type = array_search( $page_id, $ids ); 178 160 … … 190 172 * Get the archive page URL. 191 173 * 192 * Returns the URL of the post archive page 193 * based on the custom archive page id that 194 * passed through the function. 174 * Returns the URL of the post archive page based on the custom 175 * archive page id that passed through the function. 195 176 * 196 177 * @param int $page_id The page id to search. … … 198 179 * @return string|boolean 199 180 */ 200 public static function get_archive_url( $page_id = 0 ) { 201 202 // Get the types and ids 203 $types = self::get_post_types(); 204 $ids = self::get_archive_ids(); 181 public static function get_custom_archive_url( $page_id = 0 ) { 182 183 $types = self::get_custom_post_types(); 184 $ids = self::get_custom_archive_ids(); 205 185 206 186 if ( in_array( $page_id, $ids ) ) { 207 187 208 // Get the post type this page is the archive for188 // 209 189 $archive = array_search( $page_id, $ids ); 210 190 211 191 if ( false !== $archive ) { 212 192 213 // Get the archive URL193 // Get the 214 194 $url = get_post_type_archive_link( $types[ $archive ]->name ); 215 195 … … 252 232 253 233 /** 254 * Print the adminplugin page HTML.234 * Print the plugin page HTML. 255 235 * 256 236 * @return string … … 258 238 public static function show_plugin_page() { 259 239 260 // Get the post types 261 $types = self::get_post_types(); 240 $types = self::get_custom_post_types(); 262 241 263 242 ?> … … 289 268 * Register page archive settings. 290 269 * 291 * Adds the post type archive page settings 292 * to theReading settings page.270 * Adds the post type archive page settings 271 * Reading settings page. 293 272 * 294 273 * @return void … … 296 275 public static function add_settings() { 297 276 298 // Get the post types 299 $types = self::get_post_types(); 300 301 // Add the setting section 277 $types = self::get_custom_post_types(); 278 302 279 add_settings_section( 'custom_archives_group', false, false, 'custom_archives_section' ); 303 280 304 // Get the post loop fix value305 $value = get_option( 'archive_allow_empty_loops', false );306 307 // Add the post loop fix setting308 add_settings_field(309 'archive_allow_empty_loops',310 esc_html__( 'Post Loops', 'custom-archives' ),311 array( __CLASS__, 'print_check_setting' ),312 'custom_archives_section',313 'custom_archives_group',314 array( 'name' => 'archive_allow_empty_loops', 'value' => $value, 'description' => esc_html__( 'Bypass empty post loops on custom archive pages.', 'custom-archives' ) )315 );316 317 // Register the post loop setting318 register_setting(319 'custom_archives_fields',320 'archive_allow_empty_loops',321 'esc_attr'322 );323 324 281 foreach ( $types as $type ) { 325 282 … … 330 287 } 331 288 332 // Set the post type parameters289 // Set the 333 290 $name = 'archive_page_' . $type->name; 334 291 $value = get_option( $name, false ); … … 354 311 355 312 /** 356 * Print the checkbox setting UI.313 * Print the . 357 314 * 358 315 * @param array $args The settings field arguments. … … 360 317 * @return string 361 318 */ 362 public static function print_check_setting( $args ) {363 364 ?>365 366 <fieldset>367 <label for="<?php echo esc_attr( $args['name'] ); ?>">368 <input type="checkbox" name="<?php echo esc_attr( $args['name'] ); ?>" id="<?php echo esc_attr( $args['name'] ); ?>"<?php if ( 'on' == $args['value'] ) : ?> checked="checked"<?php endif; ?> />369 <?php if ( isset( $args[ 'description' ] ) ) : ?>370 <?php echo $args[ 'description' ]; ?>371 <?php endif; ?>372 </label>373 </fieldset>374 375 <?php376 377 }378 379 /**380 * Print the select setting UI.381 *382 * @param array $args The settings field arguments.383 *384 * @return string385 */386 319 public static function print_select_setting( $args ) { 387 320 388 // Get the home and blog pages from Settings > Reading321 // Get the home 389 322 $home_page = get_option( 'page_on_front', false ); 390 323 $blog_page = get_option( 'page_for_posts', false ); … … 392 325 $pages = wp_dropdown_pages( 393 326 array( 394 'id' => esc_attr( 'select_' . $args[ 'name'] ),395 'name' => esc_attr( $args[ 'name'] ),396 'selected' => $args[ 'value'],327 'id' => esc_attr( 'select_' . $args[] ), 328 'name' => esc_attr( $args[] ), 329 'selected' => $args[], 397 330 'exclude' => implode( ',', array( $home_page, $blog_page ) ), 398 331 'show_option_none' => esc_html__( 'Default', 'custom-archives' ), … … 401 334 ); 402 335 403 // Print the drop down select or error336 // Print 404 337 echo ( $pages ) ? $pages : '<p>' . esc_html__( 'No pages to select.', 'custom-archives' ) . '</p>'; 405 338 … … 415 348 public static function verify_setting( $new_value ) { 416 349 417 // Force to int418 350 $id = (int) $new_value; 419 351 420 // Find the post421 352 $post = get_post( $id ); 422 353 423 // Return default value if invalid424 354 if ( ! $post || 'page' != $post->post_type ) { 425 355 … … 444 374 global $post; 445 375 446 // Get the custom archive ids 447 $ids = self::get_archive_ids(); 376 $ids = self::get_custom_archive_ids(); 448 377 449 378 if ( $post && isset( $post->ID ) && in_array( $post->ID, $ids ) ) { 450 379 451 // Get the custom archive url 452 $url = self::get_archive_url( $post->ID ); 380 $url = self::get_custom_archive_url( $post->ID ); 453 381 454 382 if ( false !== $url ) { … … 478 406 $code = apply_filters( 'custom_archive_http_code', 301, $post->ID ); 479 407 480 // Set a header response408 // Set 481 409 status_header( $code ); 482 410 483 // Redirect to the archive page484 411 wp_safe_redirect( $url, $code ); 485 412 … … 495 422 * Check custom archive page on update. 496 423 * 497 * This function ensures that in the event of 498 * a post having it's status changed to anything 499 * other than `published`, it'll remove it as 500 * a custom archive page. 424 * This function ensures that in the event of a post having it's 425 * status changed to anything other than `published`, it'll remove 426 * it as a custom archive page. 501 427 * 502 428 * @param string $new_status The new post status. … … 510 436 if ( 'page' == $post->post_type && 'publish' !== $new_status ) { 511 437 512 // Get archive psot type513 438 $post_type = self::get_custom_archive_post_type( $post->ID ); 514 439 … … 526 451 * Remove custom archive option on delete. 527 452 * 528 * Delete the saved value for a custom archive page 529 * if thatpage gets deleted from the site.453 * Delete the saved value for a custom archive page 454 * page gets deleted from the site. 530 455 * 531 456 * @param int $post_id The deleted post id. … … 535 460 public static function post_deleted( $post_id ) { 536 461 537 // Get archive psot type538 462 $post_type = self::get_custom_archive_post_type( $post_id ); 539 463 … … 549 473 * Add an edit link to the Toolbar. 550 474 * 551 * Adds an edit page link to the toolbar when viewing 552 * an archivepage that is using a custom archive.475 * Adds an edit page link to the toolbar when viewing 476 * page that is using a custom archive. 553 477 * 554 478 * @param object $wp_admin_bar The toolbar links. … … 560 484 global $wp_query, $wp_the_query; 561 485 562 // Get the archive ids 563 $archive_ids = self::get_archive_ids(); 564 565 // Get the archive post type 566 $post_type = ( isset( $wp_query->query['post_type'] ) ) ? $wp_query->query['post_type'] : ''; 486 $archive_ids = self::get_custom_archive_ids(); 487 488 // Get the archive page post type. 489 $post_type = ( isset( $wp_query->query[ 'post_type' ] ) ) ? $wp_query->query[ 'post_type' ] : ''; 567 490 568 491 if ( ! is_admin() && true === $wp_query->is_archive && array_key_exists( $post_type, $archive_ids ) ) { 569 492 570 // Get the post object 571 $post = get_post( $archive_ids[ $wp_query->query['post_type'] ] ); 572 573 // Get the post object 493 $post = get_post( $archive_ids[ $wp_query->query[ 'post_type' ] ] ); 494 574 495 $post_type_object = get_post_type_object( $post->post_type ); 575 496 576 // Get the edit link497 // Get the edit 577 498 $edit_post_link = get_edit_post_link( $post->ID ); 578 499 579 // Can we add an edit link for this user 580 if ( current_user_can( 'edit_post', $post->ID ) ) { 581 582 // Add the menu item 500 if ( current_user_can( $post_type_object->cap->edit_post, $post->ID ) ) { 501 583 502 $wp_admin_bar->add_menu( 584 503 array( … … 605 524 public static function add_post_states( $states, $post ) { 606 525 607 // Get the post types & ids 608 $types = self::get_post_types(); 609 $ids = self::get_archive_ids(); 526 $types = self::get_custom_post_types(); 527 $ids = self::get_custom_archive_ids(); 610 528 611 529 if ( 'page' === $post->post_type ) { … … 613 531 if ( in_array( $post->ID, $ids ) ) { 614 532 615 // Get the post type for this ID533 // 616 534 $archive = array_search( $post->ID, $ids ); 617 535 618 // Add the post state536 // Add the 619 537 $states[ 'archive_page_' . $post->post_type ] = sprintf( esc_html__( '%s Archive', 'custom-archives' ), $types[ $archive ]->labels->name ); 620 538 … … 640 558 if ( $wp_query->is_archive ) { 641 559 642 // Get the archive ids 643 $ids = self::get_archive_ids(); 644 645 // Get the archive post type 646 $post_type = ( isset( $wp_query->query['post_type'] ) ) ? $wp_query->query['post_type'] : ''; 647 648 // Get the page id for the archive page 560 $ids = self::get_custom_archive_ids(); 561 562 // Get the archive page post type. 563 $post_type = ( isset( $wp_query->query[ 'post_type' ] ) ) ? $wp_query->query[ 'post_type' ] : ''; 564 565 // Is the page a custom archive? 649 566 $post_id = ( isset( $ids[ $post_type ] ) ) ? $ids[ $post_type ] : 0; 650 567 651 568 if ( 0 !== $post_id ) { 652 569 653 // Get the post object654 570 $post = get_post( $post_id ); 655 571 656 // Set the page title 657 $title['title'] = $post->post_title; 572 $title[ 'title' ] = $post->post_title; 658 573 659 574 } … … 684 599 if ( $wp_query->is_archive ) { 685 600 686 // Get the archive ids 687 $ids = self::get_archive_ids(); 688 689 // Get the archive post type 690 $post_type = ( isset( $wp_query->query['post_type'] ) ) ? $wp_query->query['post_type'] : ''; 691 692 // Get the page id for the archive page 601 $ids = self::get_custom_archive_ids(); 602 603 // Get the archive page post type. 604 $post_type = ( isset( $wp_query->query[ 'post_type' ] ) ) ? $wp_query->query[ 'post_type' ] : ''; 605 606 // Is the page a custom archive? 693 607 $post_id = ( isset( $ids[ $post_type ] ) ) ? $ids[ $post_type ] : 0; 694 608 695 609 if ( 0 !== $post_id ) { 696 610 697 // Get the post object698 611 $post = get_post( $post_id ); 699 612 700 // Update the $wp_query data613 // Update the 701 614 $wp_query->post = $post; 702 $wp_query->query_vars[ 'archive_posts'] = $wp_query->posts;615 $wp_query->query_vars[] = $wp_query->posts; 703 616 $wp_query->posts = array( $post ); 704 $wp_query->query_vars['p'] = $post_id; 705 $wp_query->query_vars['page_id'] = $post_id; 706 707 // Check if the override post loops setting is on 708 if ( 'on' == get_option( 'archive_allow_empty_loops', false ) ) { 709 710 $wp_query->post_count = 1; 711 712 } 713 714 // Get the template directory 617 $wp_query->query_vars[ 'p' ] = $post_id; 618 $wp_query->query_vars[ 'page_id' ] = $post_id; 619 620 // Get a post count of at least 1. 621 $post_count = ( 1 <= $wp_query->post_count ) ? $wp_query->post_count : 1; 622 623 /** 624 * Filter the current query post count. 625 * 626 * The post count for the query must be 1 or more as when the 627 * `have_posts` function is run, if there are no published posts 628 * for this post type, (depending on the theme template used) 629 * nothing will be shown. 630 * 631 * @since 3.0 632 * 633 * @param int $post_count The default number of posts. 634 * @param string $post_type The archive page post type. 635 * @param object $query The current WP Query object. 636 */ 637 $wp_query->post_count = apply_filters( 'custom_archive_query_post_count', $post_count, $post_type, $query ); 638 715 639 $directory = get_template_directory(); 716 640 717 // Get the page template slug641 // Get the page template 718 642 $template = get_post_meta( $post->ID, '_wp_page_template', true ); 719 643 720 // When no template name given644 // 721 645 if ( '' == $template || false === $template || 'default' == $template ) { 722 646 723 647 $template = 'page.php'; 724 648 725 }726 727 // Fallback if page.php doesn't exist either 728 if ( ! file_exists( $directory . '/' . $template ) ) {729 730 $template = 'index.php';649 650 if ( ! file_exists( $directory . '/' . $template ) ) { 651 652 653 654 731 655 732 656 } … … 743 667 $template = apply_filters( 'pre_custom_archive_template', $template, $post_id, $post_type ); 744 668 745 // Create the full template path746 669 $template = $directory . '/' . $template; 747 670 … … 780 703 781 704 // Set the new document title. 782 $title = sprintf( _ _( '%s - %s', 'custom-archives' ), $page->post_title, get_bloginfo( 'name' ) );705 $title = sprintf( _', 'custom-archives' ), $page->post_title, get_bloginfo( 'name' ) ); 783 706 784 707 /** … … 818 741 if ( 'page' == $post->post_type ) { 819 742 820 if ( in_array( $post->ID, self::get_archive_ids() ) ) { 821 822 // Get the custom archive url 823 $url = self::get_archive_url( $post->ID ); 743 if ( in_array( $post->ID, self::get_custom_archive_ids() ) ) { 744 745 $url = self::get_custom_archive_url( $post->ID ); 824 746 825 747 if ( false !== $url ) { … … 838 760 } 839 761 840 /**841 * Adds a donate link to the plugins table.842 *843 * @param array $links A list of plugin links844 * @param string $file The current plugin file.845 *846 * @return array $links847 */848 public static function add_donate_link( $links, $file ) {849 850 // Check if this is the current plugin851 if ( 'custom-archives/custom-archives.php' == $file ) {852 853 // Create the donate link854 $donate_link = '<a href="https://www.paypal.me/dtj27" target="_blank">' . esc_html__( 'Donate', 'custom-archives' ) . '</a>';855 856 // Add the link to the array857 array_unshift( $links, $donate_link );858 859 }860 861 return $links;862 863 }864 865 762 } 866 -
custom-archives/trunk/readme.txt
r2050054 r2052010 1 1 === Custom Archives === 2 2 Contributors: danieltj 3 Tags: page, archive, custom, template, post type4 Requires at least: 4. 03 Tags: , template, post type 4 Requires at least: 4. 5 5 Tested up to: 5.1 6 Stable tag: 2.16 Stable tag: 7 7 License: GNU GPL v3 8 8 License URI: https://www.gnu.org/licenses/gpl-3.0.html 9 Donate link: https://www.paypal.me/dtj2710 9 11 Select a page to be a custom archive for yourpost types.10 Select a page to be post types. 12 11 13 12 == Description == … … 17 16 = Developers = 18 17 19 There are lots of action and filter hooks available to extend the functionality of this plugin. If you'd like more information on how to use them, take a look at the plugin Wiki on the [GitHub repository](https://github.com/danieltj27/Custom-Archives/wiki).18 iki on the [GitHub repository](https://github.com/danieltj27/Custom-Archives/wiki). 20 19 21 20 = Thank You = … … 61 60 == Changelog == 62 61 63 Refer to the [GitHub repository](https://github.com/danieltj27/Custom-Archives) for more information on version history and updates.62 Refer to the [GitHub repository](https://github.com/danieltj27/Custom-Archives) for .
Note: See TracChangeset
for help on using the changeset viewer.