Making WordPress.org


Ignore:
Timestamp:
03/11/2021 11:48:52 PM (5 years ago)
Author:
coffee2code
Message:

Handbooks, Importer: Assign 'wordpressdotorg' as the default post author for all imported posts, if it exists.

Also adds handbooks_import_default_author_slug filter to customize or disable the default post author.

File:
1 edited

Legend:

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

    r10779 r10815  
    1919
    2020        /**
     21         * Memoized user ID of default import post author.
     22         *
     23         * @var int
     24         */
     25        protected static $import_user_id;
     26
     27        /**
    2128         * The slug base.
    2229         *
     
    118125                add_action( "handbook_{$post_type_base}_import_all_markdown", [ $this, 'import_all_markdown' ] );
    119126                add_filter( 'display_post_states',                            [ $this, 'display_post_states' ], 10, 2 );
     127                add_filter( 'wporg_markdown_post_data_pre_insert',            [ $this, 'assign_post_author' ] );
    120128
    121129                $editor = new Editor( $this );
     
    190198
    191199        /**
     200         * Adds user as the default post author for imported posts that don't have
     201         * a post author already assigned.
     202         *
     203         * By default, assigns all imported posts to the 'wordpressdotorg' user if
     204         * it exists. Use {@see 'handbooks_import_default_author_slug'} to filter
     205         * the default imported post author. Otherwise, no post author is assigned.
     206         *
     207         * @param array $post_data Post data.
     208         * @return array
     209         */
     210        public static function assign_post_author( $post_data ) {
     211                // Bail early if the post already has a post author specified.
     212                if ( isset( $post_data['post_author'] ) ) {
     213                        return $post_data;
     214                }
     215
     216                // Get the default import author if not already memoized.
     217                if ( ! self::$import_user_id ) {
     218                        /**
     219                         * Filters the slug of the user to be assigned as the post
     220                         * author for all imported posts.
     221                         *
     222                         * @param string $slug Slug of user to assign as imported post author.
     223                         *                     Use empty string or false to prevent a post author
     224                         *                     from being assigned. Default 'wordpressdotorg'.
     225                         * @return string
     226                         */
     227                        $default_user_slug = apply_filters( 'handbooks_import_default_author_slug', 'wordpressdotorg' );
     228
     229                        if ( $default_user_slug ) {
     230                                $user = get_user_by( 'slug', $default_user_slug );
     231                                if ( $user ) {
     232                                        self::$import_user_id = $user->ID;
     233                                }
     234                        }
     235                }
     236
     237                // Add the default import author if it was found.
     238                if ( self::$import_user_id ) {
     239                        $post_data['post_author'] = self::$import_user_id;
     240                }
     241
     242                return $post_data;
     243        }
     244
     245        /**
    192246         * Indicates if the specified handbook is imported.
    193247         *
Note: See TracChangeset for help on using the changeset viewer.