|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Retain recent bbPress replies, along with their topics and forums. |
| 4 | + * |
| 5 | + * @package pmc-wp-local-data-cli |
| 6 | + */ |
| 7 | + |
| 8 | +declare( strict_types = 1 ); |
| 9 | + |
| 10 | +namespace PMC\WP_Local_Data_CLI\Query_Args; |
| 11 | + |
| 12 | +use PMC\WP_Local_Data_CLI\Query_Args; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class bbPress. |
| 16 | + */ |
| 17 | +// phpcs:ignore PEAR.NamingConventions.ValidClassName.StartWithCapital, Squiz.Commenting.ClassComment.Missing |
| 18 | +final class bbPress extends Query_Args { |
| 19 | + /** |
| 20 | + * Backfill is not required as we query from replies and use their meta to |
| 21 | + * capture the topic and forum. |
| 22 | + * |
| 23 | + * @var bool |
| 24 | + */ |
| 25 | + public static bool $skip_backfill = true; |
| 26 | + |
| 27 | + /** |
| 28 | + * Build array of `WP_Query` arguments used to retrieve IDs to retain. |
| 29 | + * |
| 30 | + * @return array |
| 31 | + */ |
| 32 | + public static function get_query_args(): array { |
| 33 | + // Short-circuit this handler when reply post type is unknown. |
| 34 | + if ( ! function_exists( 'bbp_get_reply_post_type' ) ) { |
| 35 | + return [ |
| 36 | + 'post_type' => 'abcdef0123456789', |
| 37 | + 'date_query' => [ |
| 38 | + [ |
| 39 | + 'after' => '+500 years', |
| 40 | + ], |
| 41 | + ], |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + return [ |
| 46 | + 'post_type' => bbp_get_reply_post_type(), |
| 47 | + 'date_query' => [ |
| 48 | + [ |
| 49 | + 'after' => '-1 months', |
| 50 | + ], |
| 51 | + ], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Gather reply's parent objects. |
| 57 | + * |
| 58 | + * @param int $id Post ID. |
| 59 | + * @param string $post_type Post type of given ID. |
| 60 | + * @return array |
| 61 | + */ |
| 62 | + // Declaration must be compatible with overridden method. |
| 63 | + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed, Squiz.Commenting.FunctionComment.Missing, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 64 | + public static function get_linked_ids( int $id, string $post_type ): array { |
| 65 | + $ids = []; |
| 66 | + |
| 67 | + $ancestors = array_filter( |
| 68 | + [ |
| 69 | + (int) get_post_meta( $id, '_bbp_topic_id', true ), |
| 70 | + (int) get_post_meta( $id, '_bbp_forum_id', true ), |
| 71 | + ] |
| 72 | + ); |
| 73 | + |
| 74 | + foreach ( $ancestors as $ancestor ) { |
| 75 | + $ids[] = [ |
| 76 | + 'ID' => $ancestor, |
| 77 | + 'post_type' => get_post_type( $ancestor ), |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + return $ids; |
| 82 | + } |
| 83 | +} |
0 commit comments