Skip to content

Commit

Permalink
Move this class into its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Aug 29, 2023
1 parent 3cf23bf commit 2d965cd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 73 deletions.
91 changes: 91 additions & 0 deletions inc/class-insert-post-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Handler for encapsulating hook callbacks called when inserting posts.
*
* @package authorship
*/

declare( strict_types=1 );

namespace Authorship;

use Exception;
use WP_Post;

/**
* Core class for encapsulating hook callbacks called when inserting posts.
*
* This is needed because state is passed between the callbacks.
*/
class InsertPostHandler {
/**
* @var array<mixed>
*/
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.
}
}
}
73 changes: 0 additions & 73 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,79 +333,6 @@ function filter_rest_request_after_callbacks( $result, array $handler, WP_REST_R
return $result;
}

class InsertPostHandler {
/**
* @var array<mixed>
*/
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.
*
Expand Down
1 change: 1 addition & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand Down

0 comments on commit 2d965cd

Please sign in to comment.