Plugin Directory

Changeset 2052010

Timestamp:
03/17/2019 11:01:18 AM (5 years ago)
Author:
danieltj
Message:

Update Custom Archives to v3.0

Location:
custom-archives
Files:
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • custom-archives/trunk/README.md

    r2041692 r2052010  
    11# Custom Archives
    22
    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.
    44
    55## Thank You
     
    77Thanks 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.
    88
    9 ## Donate
    10 
    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 
    139## Useful Links
    1410
    1511- [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)
    1713- [GitHub Repository](https://github.com/danieltj27/Custom-Archives)
    18 - [Me](https://danieltj.uk/)
    19 
  • custom-archives/trunk/custom-archives.php

    r2050054 r2052010  
    55 * Plugin URI: https://wordpress.org/plugins/custom-archives/
    66 * Description: Select a page to be a custom archive for your post types.
     7
    78 * Author: Daniel James
    89 * Author URI: https://danieltj.uk/
    910 * Text Domain: custom-archives
    10  * Version: 2.1
    1111 */
    1212
     
    2929
    3030if ( ! defined( 'ABSPATH' ) ) {
     31
    3132    die();
     33
    3234}
    3335
    34 $Custom_Archives = new Custom_Archives;
     36new Custom_Archives;
    3537
    3638class Custom_Archives {
    3739
    3840    /**
    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.
    4742     *
    4843     * @return void
     
    5045    public function __construct() {
    5146
    52         add_action( 'plugins_loaded', array( __CLASS__, 'load_text_domain' ), 10, 0 );
    5347        add_action( 'admin_menu', array( __CLASS__, 'add_admin_page' ), 10, 0 );
    5448        add_action( 'admin_init', array( __CLASS__, 'add_settings' ), 10, 0 );
     
    5852        add_action( 'admin_bar_menu', array( __CLASS__, 'add_edit_link' ), 80, 1 );
    5953
    60         add_filter( 'plugin_action_links', array( __CLASS__, 'add_donate_link' ), 10, 2 );
    6154        add_filter( 'display_post_states', array( __CLASS__, 'add_post_states' ), 20, 2 );
    6255        add_filter( 'document_title_parts', array( __CLASS__, 'rewrite_page_title' ), 15, 1 );
     
    6861
    6962    /**
    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.
    8264     *
    8365     * @return array $post_types An array of post types.
    8466     */
    85     public static function get_post_types() {
    86 
    87         // Get all custom types
     67    public static function get_custom_post_types() {
     68
    8869        $get_types = get_post_types(
    8970            array(
     
    9980        foreach ( $get_types as $key => $value ) {
    10081
    101             // Add the post type
     82            // Add the post type
    10283            $post_types[ $value->name ] = $value;
    10384
     
    10687        /**
    10788         * Filter the array of post types.
     89
     90
    10891         *
    10992         * @param array $post_types The array of post types.
     
    122105     * @return array $pages
    123106     */
    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();
    128110
    129111        $pages = array();
     
    137119            }
    138120
    139             // Get the option for this post type
     121            // Get the
    140122            $page_id = get_option( 'archive_page_' . $type->name, false );
    141123
     
    146128            }
    147129
    148             // Add the page id to the array
    149130            $pages[ $type->name ] = $page_id;
    150131
     
    153134        /**
    154135         * Filter the array of custom archives.
     136
     137
    155138         *
    156139         * @param array $pages The array of custom archive ids.
     
    171154    public static function get_custom_archive_post_type( $page_id ) {
    172155
    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?
    177159        $post_type = array_search( $page_id, $ids );
    178160
     
    190172     * Get the archive page URL.
    191173     *
    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.
    195176     *
    196177     * @param int $page_id The page id to search.
     
    198179     * @return string|boolean
    199180     */
    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();
    205185
    206186        if ( in_array( $page_id, $ids ) ) {
    207187
    208             // Get the post type this page is the archive for
     188            //
    209189            $archive = array_search( $page_id, $ids );
    210190
    211191            if ( false !== $archive ) {
    212192
    213                 // Get the archive URL
     193                // Get the
    214194                $url = get_post_type_archive_link( $types[ $archive ]->name );
    215195
     
    252232
    253233    /**
    254      * Print the admin plugin page HTML.
     234     * Print the plugin page HTML.
    255235     *
    256236     * @return string
     
    258238    public static function show_plugin_page() {
    259239
    260         // Get the post types
    261         $types = self::get_post_types();
     240        $types = self::get_custom_post_types();
    262241
    263242        ?>
     
    289268     * Register page archive settings.
    290269     *
    291      * Adds the post type archive page settings
    292      * to the Reading settings page.
     270     * Adds the post type archive page settings
     271     * Reading settings page.
    293272     *
    294273     * @return void
     
    296275    public static function add_settings() {
    297276
    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
    302279        add_settings_section( 'custom_archives_group', false, false, 'custom_archives_section' );
    303280
    304         // Get the post loop fix value
    305         $value = get_option( 'archive_allow_empty_loops', false );
    306 
    307         // Add the post loop fix setting
    308         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 setting
    318         register_setting(
    319             'custom_archives_fields',
    320             'archive_allow_empty_loops',
    321             'esc_attr'
    322         );
    323 
    324281        foreach ( $types as $type ) {
    325282
     
    330287            }
    331288
    332             // Set the post type parameters
     289            // Set the
    333290            $name = 'archive_page_' . $type->name;
    334291            $value = get_option( $name, false );
     
    354311
    355312    /**
    356      * Print the checkbox setting UI.
     313     * Print the .
    357314     *
    358315     * @param array $args The settings field arguments.
     
    360317     * @return string
    361318     */
    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         <?php
    376 
    377     }
    378 
    379     /**
    380      * Print the select setting UI.
    381      *
    382      * @param array $args The settings field arguments.
    383      *
    384      * @return string
    385      */
    386319    public static function print_select_setting( $args ) {
    387320
    388         // Get the home and blog pages from Settings > Reading
     321        // Get the home
    389322        $home_page = get_option( 'page_on_front', false );
    390323        $blog_page = get_option( 'page_for_posts', false );
     
    392325        $pages = wp_dropdown_pages(
    393326            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[],
    397330                'exclude' => implode( ',', array( $home_page, $blog_page ) ),
    398331                'show_option_none' => esc_html__( 'Default', 'custom-archives' ),
     
    401334        );
    402335
    403         // Print the drop down select or error
     336        // Print
    404337        echo ( $pages ) ? $pages : '<p>' . esc_html__( 'No pages to select.', 'custom-archives' ) . '</p>';
    405338
     
    415348    public static function verify_setting( $new_value ) {
    416349
    417         // Force to int
    418350        $id = (int) $new_value;
    419351
    420         // Find the post
    421352        $post = get_post( $id );
    422353
    423         // Return default value if invalid
    424354        if ( ! $post || 'page' != $post->post_type ) {
    425355
     
    444374        global $post;
    445375
    446         // Get the custom archive ids
    447         $ids = self::get_archive_ids();
     376        $ids = self::get_custom_archive_ids();
    448377
    449378        if ( $post && isset( $post->ID ) && in_array( $post->ID, $ids ) ) {
    450379
    451             // Get the custom archive url
    452             $url = self::get_archive_url( $post->ID );
     380            $url = self::get_custom_archive_url( $post->ID );
    453381
    454382            if ( false !== $url ) {
     
    478406                $code = apply_filters( 'custom_archive_http_code', 301, $post->ID );
    479407
    480                 // Set a header response
     408                // Set
    481409                status_header( $code );
    482410
    483                 // Redirect to the archive page
    484411                wp_safe_redirect( $url, $code );
    485412
     
    495422     * Check custom archive page on update.
    496423     *
    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.
    501427     *
    502428     * @param string $new_status The new post status.
     
    510436        if ( 'page' == $post->post_type && 'publish' !== $new_status ) {
    511437
    512             // Get archive psot type
    513438            $post_type = self::get_custom_archive_post_type( $post->ID );
    514439
     
    526451     * Remove custom archive option on delete.
    527452     *
    528      * Delete the saved value for a custom archive page
    529      * if that page gets deleted from the site.
     453     * Delete the saved value for a custom archive page
     454     * page gets deleted from the site.
    530455     *
    531456     * @param int $post_id The deleted post id.
     
    535460    public static function post_deleted( $post_id ) {
    536461
    537         // Get archive psot type
    538462        $post_type = self::get_custom_archive_post_type( $post_id );
    539463
     
    549473     * Add an edit link to the Toolbar.
    550474     *
    551      * Adds an edit page link to the toolbar when viewing
    552      * an archive page 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.
    553477     *
    554478     * @param object $wp_admin_bar The toolbar links.
     
    560484        global $wp_query, $wp_the_query;
    561485
    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' ] : '';
    567490
    568491        if ( ! is_admin() && true === $wp_query->is_archive && array_key_exists( $post_type, $archive_ids ) ) {
    569492
    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
    574495            $post_type_object = get_post_type_object( $post->post_type );
    575496
    576             // Get the edit link
     497            // Get the edit
    577498            $edit_post_link = get_edit_post_link( $post->ID );
    578499
    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
    583502                $wp_admin_bar->add_menu(
    584503                    array(
     
    605524    public static function add_post_states( $states, $post ) {
    606525
    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();
    610528
    611529        if ( 'page' === $post->post_type ) {
     
    613531            if ( in_array( $post->ID, $ids ) ) {
    614532
    615                 // Get the post type for this ID
     533                //
    616534                $archive = array_search( $post->ID, $ids );
    617535
    618                 // Add the post state
     536                // Add the
    619537                $states[ 'archive_page_' . $post->post_type ] = sprintf( esc_html__( '%s Archive', 'custom-archives' ), $types[ $archive ]->labels->name );
    620538
     
    640558        if ( $wp_query->is_archive ) {
    641559
    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?
    649566            $post_id = ( isset( $ids[ $post_type ] ) ) ? $ids[ $post_type ] : 0;
    650567
    651568            if ( 0 !== $post_id ) {
    652569
    653                 // Get the post object
    654570                $post = get_post( $post_id );
    655571
    656                 // Set the page title
    657                 $title['title'] = $post->post_title;
     572                $title[ 'title' ] = $post->post_title;
    658573
    659574            }
     
    684599        if ( $wp_query->is_archive ) {
    685600
    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?
    693607            $post_id = ( isset( $ids[ $post_type ] ) ) ? $ids[ $post_type ] : 0;
    694608
    695609            if ( 0 !== $post_id ) {
    696610
    697                 // Get the post object
    698611                $post = get_post( $post_id );
    699612
    700                 // Update the $wp_query data
     613                // Update the
    701614                $wp_query->post = $post;
    702                 $wp_query->query_vars['archive_posts'] = $wp_query->posts;
     615                $wp_query->query_vars[] = $wp_query->posts;
    703616                $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
    715639                $directory = get_template_directory();
    716640
    717                 // Get the page template slug
     641                // Get the page template
    718642                $template = get_post_meta( $post->ID, '_wp_page_template', true );
    719643
    720                 // When no template name given
     644                //
    721645                if ( '' == $template || false === $template || 'default' == $template ) {
    722646
    723647                    $template = 'page.php';
    724648
    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                   
    731655
    732656                }
     
    743667                $template = apply_filters( 'pre_custom_archive_template', $template, $post_id, $post_type );
    744668
    745                 // Create the full template path
    746669                $template = $directory . '/' . $template;
    747670
     
    780703
    781704                    // 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' ) );
    783706
    784707                    /**
     
    818741        if ( 'page' == $post->post_type ) {
    819742
    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 );
    824746
    825747                if ( false !== $url ) {
     
    838760    }
    839761
    840     /**
    841      * Adds a donate link to the plugins table.
    842      *
    843      * @param array  $links A list of plugin links
    844      * @param string $file  The current plugin file.
    845      *
    846      * @return array $links
    847      */
    848     public static function add_donate_link( $links, $file ) {
    849 
    850         // Check if this is the current plugin
    851         if ( 'custom-archives/custom-archives.php' == $file ) {
    852 
    853             // Create the donate link
    854             $donate_link = '<a href="https://www.paypal.me/dtj27" target="_blank">' . esc_html__( 'Donate', 'custom-archives' ) . '</a>';
    855 
    856             // Add the link to the array
    857             array_unshift( $links, $donate_link );
    858 
    859         }
    860 
    861         return $links;
    862 
    863     }
    864 
    865762}
    866 
  • custom-archives/trunk/readme.txt

    r2050054 r2052010  
    11=== Custom Archives ===
    22Contributors: danieltj
    3 Tags: page, archive, custom, template, post type
    4 Requires at least: 4.0
     3Tags: , template, post type
     4Requires at least: 4.
    55Tested up to: 5.1
    6 Stable tag: 2.1
     6Stable tag:
    77License: GNU GPL v3
    88License URI: https://www.gnu.org/licenses/gpl-3.0.html
    9 Donate link: https://www.paypal.me/dtj27
    109
    11 Select a page to be a custom archive for your post types.
     10Select a page to be post types.
    1211
    1312== Description ==
     
    1716= Developers =
    1817
    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).
     18iki on the [GitHub repository](https://github.com/danieltj27/Custom-Archives/wiki).
    2019
    2120= Thank You =
     
    6160== Changelog ==
    6261
    63 Refer to the [GitHub repository](https://github.com/danieltj27/Custom-Archives) for more information on version history and updates.
     62Refer to the [GitHub repository](https://github.com/danieltj27/Custom-Archives) for .
Note: See TracChangeset for help on using the changeset viewer.