current_pack_slug = sanitize_key( wp_unslash( $_GET['pack'] ) ); } // phpcs:enable } /** * Emit the content for the Packs tab — browse + detail + modal template. * * @return void */ public function render_tab_content() { if ( '' !== $this->current_pack_slug ) { $this->render_detail(); } else { $this->render_browse(); } $this->render_install_modal_template(); } /** * Whether the Packs tab is the active one based on the request. * * @return bool */ public function is_active_tab() { // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( '' !== $this->current_pack_slug ) { return true; } if ( isset( $_GET['tab'] ) && 'packs' === sanitize_key( wp_unslash( $_GET['tab'] ) ) ) { return true; } // phpcs:enable return false; } /** * Browse view — uses the stock wpcode-items-metabox pattern so the * sidebar + search + category filter are wired by the existing * items-list.js (no custom JS needed for those). * * @return void */ protected function render_browse() { $packs = WPCode_Packs::get_instance()->get_packs(); $counts = array(); foreach ( $packs as $r ) { $cat_slug = $this->group_slug( $r ); if ( ! isset( $counts[ $cat_slug ] ) ) { $counts[ $cat_slug ] = array( 'count' => 0, 'name' => $this->group_name( $r ), ); } $counts[ $cat_slug ]['count']++; } $installed_count = 0; foreach ( $packs as $r ) { if ( $r['installed'] ) { $installed_count++; } } ?>
' . esc_html__( 'Pack not found.', 'insert-headers-and-footers' ) . '
'; return; } $group_name = $this->group_name( $pack ); $icon = $this->pack_icon( $pack ); $packs_helper = WPCode_Packs::get_instance(); // Resolve every option's local snippet in a single query instead of one // query per row. $library_ids = wp_list_pluck( $pack['snippets'], 'library_id' ); $installed = $packs_helper->find_local_snippets_by_library_ids( $library_ids ); // Map every library snippet to the packs that include it, so we can tell // the user when an option is shared across packs. The same underlying // snippet is shared — so the note appears on *every* pack that lists it, // not just the ones installed after the first. $shared_pack_names = array(); foreach ( $packs_helper->get_packs() as $other_pack ) { if ( $other_pack['slug'] === $pack['slug'] ) { continue; } foreach ( $other_pack['snippets'] as $other_option ) { $shared_pack_names[ (int) $other_option['library_id'] ][ $other_pack['slug'] ] = $other_pack['name']; } } $rows = array(); foreach ( $pack['snippets'] as $option ) { $lib_id = (int) $option['library_id']; $local = isset( $installed[ $lib_id ] ) ? $installed[ $lib_id ] : null; $is_installed = (bool) $local; // WPCode encodes active state in post_status (publish = active, draft = inactive). $active = $is_installed && 'publish' === $local->post_status; $edit_url = $is_installed ? admin_url( 'admin.php?page=wpcode-snippet-manager&snippet_id=' . $local->ID ) : ''; // Other packs that also include this snippet (deduped by slug). $shared_with = isset( $shared_pack_names[ $lib_id ] ) ? array_values( $shared_pack_names[ $lib_id ] ) : array(); $rows[] = array( 'option' => $option, 'is_installed' => $is_installed, 'active' => $active, 'edit_url' => $edit_url, 'shared_with' => $shared_with, ); } $active_count = 0; foreach ( $rows as $row ) { if ( $row['active'] ) { $active_count++; } } $back_url = add_query_arg( array( 'page' => 'wpcode-snippet-manager', 'tab' => 'packs', ), admin_url( 'admin.php' ) ); ?> get_packs(); $payload = array(); foreach ( $packs as $pack ) { $payload[ $pack['slug'] ] = array( 'name' => $pack['name'], 'icon' => $this->pack_icon( $pack ), 'category' => $this->group_name( $pack ), 'snippets' => wp_list_pluck( $pack['snippets'], 'title' ), ); } ?>