| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post scheduler class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Scheduler; |
| 9 |
|
| 10 |
use Activitypub\Collection\Outbox; |
| 11 |
|
| 12 |
use function Activitypub\add_to_outbox; |
| 13 |
use function Activitypub\is_post_disabled; |
| 14 |
use function Activitypub\get_wp_object_state; |
| 15 |
|
| 16 |
/** |
| 17 |
* Post scheduler class. |
| 18 |
*/ |
| 19 |
class Post { |
| 20 |
/** |
| 21 |
* Initialize the class, registering WordPress hooks. |
| 22 |
*/ |
| 23 |
public static function init() { |
| 24 |
// Post transitions. |
| 25 |
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 ); |
| 26 |
|
| 27 |
// Attachment transitions. |
| 28 |
\add_action( 'add_attachment', array( self::class, 'transition_attachment_status' ) ); |
| 29 |
\add_action( 'edit_attachment', array( self::class, 'transition_attachment_status' ) ); |
| 30 |
\add_action( 'delete_attachment', array( self::class, 'transition_attachment_status' ) ); |
| 31 |
|
| 32 |
// Get all post types that support ActivityPub. |
| 33 |
$post_types = \get_post_types_by_support( 'activitypub' ); |
| 34 |
|
| 35 |
foreach ( $post_types as $post_type ) { |
| 36 |
\add_filter( "rest_pre_insert_{$post_type}", array( self::class, 'rest_insert' ), 10, 2 ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Schedules Activities for attachment transitions. |
| 42 |
* |
| 43 |
* @param int $post_id Attachment ID. |
| 44 |
*/ |
| 45 |
public static function transition_attachment_status( $post_id ) { |
| 46 |
if ( ! \post_type_supports( 'attachment', 'activitypub' ) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
switch ( current_action() ) { |
| 51 |
case 'add_attachment': |
| 52 |
self::schedule_post_activity( 'publish', '', get_post( $post_id ) ); |
| 53 |
break; |
| 54 |
case 'edit_attachment': |
| 55 |
self::schedule_post_activity( 'publish', 'publish', get_post( $post_id ) ); |
| 56 |
break; |
| 57 |
case 'delete_attachment': |
| 58 |
self::schedule_post_activity( 'trash', '', get_post( $post_id ) ); |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Schedule Activities. |
| 65 |
* |
| 66 |
* @param string $new_status New post status. |
| 67 |
* @param string $old_status Old post status. |
| 68 |
* @param \WP_Post $post Post object. |
| 69 |
*/ |
| 70 |
public static function schedule_post_activity( $new_status, $old_status, $post ) { |
| 71 |
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
if ( is_post_disabled( $post ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Bail on bulk edits, unless post author or post status changed. |
| 80 |
if ( isset( $_REQUEST['bulk_edit'] ) && -1 === (int) $_REQUEST['post_author'] && -1 === (int) $_REQUEST['_status'] ) { // phpcs:ignore WordPress |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
switch ( $new_status ) { |
| 85 |
case 'publish': |
| 86 |
$type = ( 'publish' === $old_status ) ? 'Update' : 'Create'; |
| 87 |
break; |
| 88 |
|
| 89 |
case 'draft': |
| 90 |
$type = ( 'publish' === $old_status ) ? 'Update' : false; |
| 91 |
break; |
| 92 |
|
| 93 |
case 'trash': |
| 94 |
$type = 'Delete'; |
| 95 |
break; |
| 96 |
|
| 97 |
default: |
| 98 |
$type = false; |
| 99 |
} |
| 100 |
|
| 101 |
// Do not send Activities if `$type` is not set or unknown. |
| 102 |
if ( empty( $type ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
// Add the post to the outbox. |
| 107 |
add_to_outbox( $post, $type, $post->post_author ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Filter the post data before it is inserted via the REST API. |
| 112 |
* |
| 113 |
* Posts being inserted via the REST API have a different order of operations than in wp_insert_post(). |
| 114 |
* This filter updates post meta before the post is inserted into the database, so that the |
| 115 |
* information is available by the time @see Outbox::add() runs. |
| 116 |
* |
| 117 |
* @param \stdClass $post An object representing a single post prepared for inserting or updating the database. |
| 118 |
* @param \WP_REST_Request $request The request object. |
| 119 |
* |
| 120 |
* @return \stdClass The prepared post. |
| 121 |
*/ |
| 122 |
public static function rest_insert( $post, $request ) { |
| 123 |
$metas = $request->get_param( 'meta' ); |
| 124 |
|
| 125 |
if ( empty( $post->ID ) || ! $metas || ! is_array( $metas ) ) { |
| 126 |
return $post; |
| 127 |
} |
| 128 |
|
| 129 |
foreach ( $metas as $meta_key => $meta_value ) { |
| 130 |
if ( |
| 131 |
\str_starts_with( $meta_key, 'activitypub_' ) || |
| 132 |
\str_starts_with( $meta_key, '_activitypub_' ) |
| 133 |
) { |
| 134 |
if ( $meta_value ) { |
| 135 |
\update_post_meta( $post->ID, $meta_key, $meta_value ); |
| 136 |
} else { |
| 137 |
\delete_post_meta( $post->ID, $meta_key ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $post; |
| 143 |
} |
| 144 |
} |
| 145 |
|