Making WordPress.org


Ignore:
Timestamp:
10/03/2023 09:56:08 PM (8 months ago)
Author:
coffee2code
Message:

Handbooks, Admin Notices: Show a notice if an established handbook is missing its published landing page.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/admin-notices.php

    r10836 r12928  
    1515    public static function init() {
    1616        add_action( 'admin_notices', [ __CLASS__, 'show_new_handbook_message' ] );
     17        add_action( 'admin_notices', [ __CLASS__, 'show_missing_handbook_landing_page_message' ] );
    1718        add_action( 'admin_notices', [ __CLASS__, 'show_imported_handbook_notice' ] );
    1819        add_action( 'admin_notices', [ __CLASS__, 'show_imported_handbook_config_errors' ] );
     20    }
     21
     22    /**
     23     * Determines if the main admin listing of handbooks is being shown.
     24     *
     25     * "Main" in this context is the listing of all handbook posts for a handbook,
     26     * or at least the listing of its published posts.
     27     *
     28     * @param bool $must_be_empty Should the listing of handbook posts not actually
     29     *                            have any posts in the list? If true, then the
     30     *                            listing MUST be empty, else the listing MUST have
     31     *                            at least one post. Default true.
     32     * @return bool True if the main admin listing of handbooks is being shown with
     33     *              the required presence or absense of posts, else false.
     34     */
     35    protected static function is_main_handbook_listing( $must_be_empty = true ) {
     36        global $wp_query;
     37
     38        $current_screen = get_current_screen();
     39
     40        return (
     41            $current_screen
     42        &&
     43            'edit' === $current_screen->base
     44        &&
     45            in_array( $current_screen->post_type, wporg_get_handbook_post_types() )
     46        &&
     47            ( $must_be_empty ? ( 0 === $wp_query->post_count ) : ( $wp_query->post_count > 0 ) )
     48        &&
     49            ( empty( $wp_query->query_vars['post_status'] ) || 'publish' === $wp_query->query_vars['post_status'] )
     50        );
    1951    }
    2052
     
    6193            echo "</p></div>\n";
    6294        }
     95    }
     96
     97    /**
     98     * Outputs admin notice showing tips for newly created handbook.
     99     *
     100     * @todo Maybe instead of hiding the message once posts are present it should persist as long as no landing page has been created?
     101     *
     102     * @access public
     103     */
     104    public static function show_missing_handbook_landing_page_message() {
     105        if ( ! self::is_main_handbook_listing( false ) ) {
     106            return;
     107        }
     108
     109        $current_screen = get_current_screen();
     110        $handbook_post_type = $current_screen->post_type;
     111
     112        $handbook_obj = WPorg_Handbook_Init::get_handbook( $handbook_post_type );
     113        if ( ! $handbook_obj ) {
     114            return;
     115        }
     116
     117        // Get landing page and return if one already exists.
     118        $landing_page = $handbook_obj->get_landing_page();
     119        if ( $landing_page ) {
     120            return;
     121        }
     122
     123        $suggested_slugs = array_map(
     124            function( $x ) { return "<code>{$x}</code>"; },
     125            $handbook_obj->get_possible_landing_page_slugs()
     126        );
     127
     128        echo '<div class="notice notice-error"><p>';
     129        printf(
     130            /* translators: 1: example landing page title that includes post type name, 2: comma-separated list of acceptable post slugs */
     131            __( '<strong>Warning:</strong> A landing page for this handbook has not been created or is not published. You can title it anything you like (suggestions: <code>%1$s</code> or <code>Welcome</code>). However, you must ensure that it has one of the following slugs: %2$s. The slug will ultimately be omitted from the page&#8216;s permalink URL, but will still appear in the permalinks for its sub-pages. Without this page your handbook&#8216;s URL will show a seemingly random handbook page.', 'wporg' ),
     132            WPorg_Handbook::get_name( $handbook_post_type ),
     133            implode( ', ', $suggested_slugs )
     134        );
     135        echo "</p></div>\n";
    63136    }
    64137
Note: See TracChangeset for help on using the changeset viewer.