add_action( 'save_post_acc_collaboration', $this, 'on_collaboration_saved', 20, 2 ); $loader->add_action( 'save_post_page', $this, 'on_linked_page_saved', 20, 2 ); $loader->add_action( 'trashed_post', $this, 'on_cpt_trashed' ); $loader->add_action( 'untrashed_post', $this, 'on_cpt_untrashed' ); $loader->add_action( 'before_delete_post', $this, 'on_cpt_before_delete', 10, 2 ); $loader->add_action( 'before_delete_post', $this, 'on_page_before_delete', 10, 2 ); } // --------------------------------------------------------------- // Save hooks // --------------------------------------------------------------- public function on_collaboration_saved( int $post_id, \WP_Post $post ): void { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( wp_is_post_revision( $post_id ) ) return; if ( 'publish' !== $post->post_status ) return; if ( self::$syncing ) return; $existing_page_id = (int) get_post_meta( $post_id, '_academiccrew_linked_page_id', true ); if ( $existing_page_id && get_post( $existing_page_id ) ) { $this->sync_to_page( $post->post_content, $existing_page_id ); return; } $this->generate_page( $post_id ); } public function on_linked_page_saved( int $page_id, \WP_Post $page ): void { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( wp_is_post_revision( $page_id ) ) return; if ( self::$syncing ) return; if ( 'publish' !== $page->post_status && 'private' !== $page->post_status ) return; $collab_id = (int) get_post_meta( $page_id, '_academiccrew_collaboration_id', true ); if ( ! $collab_id ) return; self::$syncing = true; wp_update_post( [ 'ID' => $collab_id, 'post_content' => $page->post_content ] ); self::$syncing = false; } private function sync_to_page( string $content, int $page_id ): void { if ( self::$syncing ) return; self::$syncing = true; wp_update_post( [ 'ID' => $page_id, 'post_content' => $content ] ); self::$syncing = false; } // --------------------------------------------------------------- // Lifecycle hooks — trash / delete // --------------------------------------------------------------- public function on_cpt_trashed( int $post_id ): void { if ( 'acc_collaboration' !== get_post_type( $post_id ) ) return; $page_id = (int) get_post_meta( $post_id, '_academiccrew_linked_page_id', true ); if ( ! $page_id || 'publish' !== get_post_status( $page_id ) ) return; self::$syncing = true; wp_trash_post( $page_id ); self::$syncing = false; } public function on_cpt_untrashed( int $post_id ): void { if ( 'acc_collaboration' !== get_post_type( $post_id ) ) return; $page_id = (int) get_post_meta( $post_id, '_academiccrew_linked_page_id', true ); if ( ! $page_id || 'trash' !== get_post_status( $page_id ) ) return; self::$syncing = true; wp_untrash_post( $page_id ); if ( 'publish' !== get_post_status( $page_id ) ) { wp_update_post( [ 'ID' => $page_id, 'post_status' => 'publish' ] ); } self::$syncing = false; } public function on_cpt_before_delete( int $post_id, \WP_Post $post ): void { if ( 'acc_collaboration' !== $post->post_type ) return; $page_id = (int) get_post_meta( $post_id, '_academiccrew_linked_page_id', true ); if ( ! $page_id || ! get_post( $page_id ) ) return; self::$syncing = true; $this->remove_nav_menu_items_for_page( $page_id ); wp_delete_post( $page_id, true ); self::$syncing = false; } public function on_page_before_delete( int $post_id, \WP_Post $post ): void { if ( 'page' !== $post->post_type ) return; if ( self::$syncing ) return; $collab_id = (int) get_post_meta( $post_id, '_academiccrew_collaboration_id', true ); if ( ! $collab_id ) return; delete_post_meta( $collab_id, '_academiccrew_linked_page_id' ); } // --------------------------------------------------------------- // Page generation // --------------------------------------------------------------- public function generate_page( int $collab_id ): int { $collab = get_post( $collab_id ); if ( ! $collab ) { return 0; } // Seed the editable page body from the project (e.g. its short description); // an empty page would otherwise sync back and wipe it. $content = (string) $collab->post_content; $archive_page_id = (int) get_option( 'academiccrew_collaborations_archive_page_id', 0 ); if ( ! $archive_page_id ) { CollaborationsArchivePageGenerator::generate_if_needed(); $archive_page_id = (int) get_option( 'academiccrew_collaborations_archive_page_id', 0 ); } $page_id = $this->create_page( [ 'post_title' => $collab->post_title, 'post_content' => $content, 'post_status' => 'publish', 'post_type' => 'page', 'post_name' => $collab->post_name, 'post_parent' => $archive_page_id ?: 0, 'meta_input' => [ '_academiccrew_is_collaboration_page' => 1, '_academiccrew_collaboration_id' => $collab_id, 'pyre_page_title' => 'no', 'pyre_sidebar_position' => 'none', ], ] ); if ( ! $page_id ) { return 0; } update_post_meta( $collab_id, '_academiccrew_linked_page_id', $page_id ); return $page_id; } }