0 ) { update_term_meta( $kb_id, self::META, self::get( $kb_id ) + 1 ); } } /** * Refuses a save based on a stale version. * * @param int $kb_id KB. * @param int|null $expected Version the client loaded (null = not sent, skip check). * @return true|\WP_Error */ public static function check( $kb_id, $expected ) { if ( null === $expected || self::get( $kb_id ) === (int) $expected ) { return true; } return new \WP_Error( 'kdocs_tree_conflict', __( 'Someone else changed this knowledge base while you were editing. Reload to see their changes.', 'karma-docs' ), array( 'status' => 409, 'version' => self::get( $kb_id ), ) ); } /** * Article KB / category assignments (new articles, moves between categories and KBs). * * @param int $object_id Object. * @param array $terms Terms. * @param array $tt_ids New term taxonomy ids. * @param string $taxonomy Taxonomy. * @param bool $append Append. * @param array $old_tt_ids Previous term taxonomy ids. */ public function on_set_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { if ( ! in_array( $taxonomy, array( Taxonomies::KB, Taxonomies::CATEGORY ), true ) ) { return; } sort( $tt_ids ); sort( $old_tt_ids ); if ( array_map( 'intval', $tt_ids ) === array_map( 'intval', $old_tt_ids ) ) { return; } if ( Taxonomies::KB === $taxonomy ) { foreach ( array_unique( array_merge( $tt_ids, $old_tt_ids ) ) as $tt_id ) { $term = get_term_by( 'term_taxonomy_id', (int) $tt_id, Taxonomies::KB ); if ( $term ) { self::bump( $term->term_id ); } } return; } $kb = Taxonomies::article_kb( $object_id ); if ( $kb ) { self::bump( $kb->term_id ); } } /** * Parent or order changed in the editor. * * @param int $post_id Post. * @param \WP_Post $after After. * @param \WP_Post $before Before. */ public function on_post_updated( $post_id, $after, $before ) { if ( PostTypes::ARTICLE !== $after->post_type ) { return; } if ( (int) $after->post_parent !== (int) $before->post_parent || (int) $after->menu_order !== (int) $before->menu_order ) { $this->on_post( $post_id ); } } /** * Article trashed, restored or deleted. * * @param int $post_id Post. */ public function on_post( $post_id ) { if ( PostTypes::ARTICLE !== get_post_type( $post_id ) ) { return; } $kb = Taxonomies::article_kb( $post_id ); if ( $kb ) { self::bump( $kb->term_id ); } } /** * Category created or edited (name, parent). * * @param int $term_id Term. */ public function on_category( $term_id ) { self::bump( Taxonomies::category_kb( $term_id ) ); } /** * Category assigned to a knowledge base (new categories get it right after creation). * * @param int $meta_id Meta id. * @param int $term_id Term. * @param string $meta_key Key. * @param mixed $meta_value Value. */ public function on_term_meta( $meta_id, $term_id, $meta_key, $meta_value ) { if ( 'kdocs_kb' === $meta_key ) { self::bump( (int) $meta_value ); } } /** * Category about to be deleted (its KB meta is still readable). * * @param int $term_id Term. * @param string $taxonomy Taxonomy. */ public function on_delete_term( $term_id, $taxonomy ) { if ( Taxonomies::CATEGORY === $taxonomy ) { self::bump( Taxonomies::category_kb( $term_id ) ); } } }