diff --git a/inc/class-insert-post-handler.php b/inc/class-insert-post-handler.php new file mode 100644 index 0000000..0399660 --- /dev/null +++ b/inc/class-insert-post-handler.php @@ -0,0 +1,91 @@ + + */ + private $postarr = []; + + /** + * Filters slashed post data just before it is inserted into the database. + * + * @param mixed[] $data An array of slashed, sanitized, and processed post data. + * @param mixed[] $postarr An array of sanitized (and slashed) but otherwise unmodified post data. + * @param mixed $unsanitized_postarr An array (or object that implements array access, like a WP_Post) of + * slashed yet _unsanitized_ and unprocessed post data as originally passed + * to wp_insert_post(). + * @return mixed[] An array of slashed, sanitized, and processed post data. + */ + function filter_wp_insert_post_data( array $data, array $postarr, $unsanitized_postarr ) : array { + // Make sure the unsanitized post array is actually an array. Core sometimes passes it as a WP_Post object. + $this->postarr = (array) $unsanitized_postarr; + + return $data; + } + + /** + * Fires once a post has been saved. + * + * @param int $post_ID Post ID. + * @param WP_Post $post Post object. + * @param bool $update Whether this is an existing post being updated. + */ + function action_wp_insert_post( int $post_ID, WP_Post $post, bool $update ) : void { + $unsanitized_postarr = $this->postarr; + + $this->postarr = []; + + if ( isset( $unsanitized_postarr['tax_input'] ) && ! empty( $unsanitized_postarr['tax_input'][ TAXONOMY ] ) ) { + return; + } + + $existing_authors = get_authors( $post ); + + if ( $update && ! isset( $unsanitized_postarr[ POSTS_PARAM ] ) && $existing_authors ) { + return; + } + + if ( isset( $unsanitized_postarr[ POSTS_PARAM ] ) ) { + $authors = $unsanitized_postarr[ POSTS_PARAM ]; + } else { + /** + * Set the default authorship author. Defaults to the orignal post author. + * + * @param array $authors Authors to add to a post on insert if none have been passed. Default to post author. + * @param WP_Post $post Post object. + */ + $authors = array_filter( apply_filters( + 'authorship_default_author', + [ isset( $unsanitized_postarr['post_author'] ) ? $unsanitized_postarr['post_author'] : null ], + $post + ) ); + } + + if ( empty( $authors ) ) { + return; + } + + try { + set_authors( $post, wp_parse_id_list( $authors ) ); + } catch ( Exception $e ) { + // Nothing at the moment. + } + } +} diff --git a/inc/namespace.php b/inc/namespace.php index 0a840f6..0d360d3 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -333,79 +333,6 @@ function filter_rest_request_after_callbacks( $result, array $handler, WP_REST_R return $result; } -class InsertPostHandler { - /** - * @var array - */ - private $postarr = []; - - /** - * Filters slashed post data just before it is inserted into the database. - * - * @param mixed[] $data An array of slashed, sanitized, and processed post data. - * @param mixed[] $postarr An array of sanitized (and slashed) but otherwise unmodified post data. - * @param mixed $unsanitized_postarr An array (or object that implements array access, like a WP_Post) of - * slashed yet _unsanitized_ and unprocessed post data as originally passed - * to wp_insert_post(). - * @return mixed[] An array of slashed, sanitized, and processed post data. - */ - function filter_wp_insert_post_data( array $data, array $postarr, $unsanitized_postarr ) : array { - // Make sure the unsanitized post array is actually an array. Core sometimes passes it as a WP_Post object. - $this->postarr = (array) $unsanitized_postarr; - - return $data; - } - - /** - * Fires once a post has been saved. - * - * @param int $post_ID Post ID. - * @param WP_Post $post Post object. - * @param bool $update Whether this is an existing post being updated. - */ - function action_wp_insert_post( int $post_ID, WP_Post $post, bool $update ) : void { - $unsanitized_postarr = $this->postarr; - - $this->postarr = []; - - if ( isset( $unsanitized_postarr['tax_input'] ) && ! empty( $unsanitized_postarr['tax_input'][ TAXONOMY ] ) ) { - return; - } - - $existing_authors = get_authors( $post ); - - if ( $update && ! isset( $unsanitized_postarr[ POSTS_PARAM ] ) && $existing_authors ) { - return; - } - - if ( isset( $unsanitized_postarr[ POSTS_PARAM ] ) ) { - $authors = $unsanitized_postarr[ POSTS_PARAM ]; - } else { - /** - * Set the default authorship author. Defaults to the orignal post author. - * - * @param array $authors Authors to add to a post on insert if none have been passed. Default to post author. - * @param WP_Post $post Post object. - */ - $authors = array_filter( apply_filters( - 'authorship_default_author', - [ isset( $unsanitized_postarr['post_author'] ) ? $unsanitized_postarr['post_author'] : null ], - $post - ) ); - } - - if ( empty( $authors ) ) { - return; - } - - try { - set_authors( $post, wp_parse_id_list( $authors ) ); - } catch ( Exception $e ) { - // Nothing at the moment. - } - } -} - /** * Adds the authorship field to the REST API for post objects. * diff --git a/plugin.php b/plugin.php index 4369d15..80837d6 100644 --- a/plugin.php +++ b/plugin.php @@ -43,6 +43,7 @@ require_once __DIR__ . '/inc/namespace.php'; require_once __DIR__ . '/inc/taxonomy.php'; require_once __DIR__ . '/inc/class-users-controller.php'; +require_once __DIR__ . '/inc/class-insert-post-handler.php'; require_once __DIR__ . '/inc/template.php'; if ( is_admin() ) {