ID ); } /** * Category assignments. * * @param int $object_id Object. * @param array $terms Terms. * @param array $tt_ids TT ids. * @param string $taxonomy Taxonomy. */ public function on_set_terms( $object_id, $terms, $tt_ids, $taxonomy ) { if ( Taxonomies::CATEGORY === $taxonomy && PostTypes::ARTICLE === get_post_type( $object_id ) ) { self::article( $object_id ); } } /** * Applies the rules to a single article (and cascades category to descendants). * * @param int $post_id Article id. */ public static function article( $post_id ) { if ( self::$running || self::$suspended ) { return; } self::$running = true; $post = get_post( $post_id ); if ( $post && PostTypes::ARTICLE === $post->post_type ) { if ( $post->post_parent && PostTypes::ARTICLE === get_post_type( $post->post_parent ) ) { $parent_cat = Taxonomies::article_category( $post->post_parent ); $own_cat = Taxonomies::article_category( $post ); if ( $parent_cat && ( ! $own_cat || $own_cat->term_id !== $parent_cat->term_id ) ) { wp_set_object_terms( $post->ID, array( (int) $parent_cat->term_id ), Taxonomies::CATEGORY ); } if ( ! $parent_cat ) { $parent_kb = Taxonomies::article_kb( $post->post_parent ); if ( $parent_kb ) { wp_set_object_terms( $post->ID, array( (int) $parent_kb->term_id ), Taxonomies::KB ); } } } $cat = Taxonomies::article_category( $post ); if ( $cat ) { $kb_id = Taxonomies::category_kb( $cat->term_id ); if ( $kb_id ) { wp_set_object_terms( $post->ID, array( $kb_id ), Taxonomies::KB ); } } self::cascade( $post->ID ); } self::$running = false; } /** * Copies an article's category + KB onto all of its descendants. * * @param int $post_id Parent article. */ public static function cascade( $post_id ) { $cat = Taxonomies::article_category( $post_id ); $kb = Taxonomies::article_kb( $post_id ); $children = get_posts( array( 'post_type' => PostTypes::ARTICLE, 'post_parent' => $post_id, 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', ) ); foreach ( $children as $child_id ) { wp_set_object_terms( $child_id, $cat ? array( (int) $cat->term_id ) : array(), Taxonomies::CATEGORY ); if ( $kb ) { wp_set_object_terms( $child_id, array( (int) $kb->term_id ), Taxonomies::KB ); } self::cascade( $child_id ); } } }