*/ public function add_tab( mixed $tabs ): array { $tabs = is_array( $tabs ) ? $tabs : array(); $tabs['pnscripts_product_tabs'] = array( 'label' => __( 'Custom tabs', 'pnscripts-tabcrest' ), 'target' => 'pnscripts_product_tabs_panel', 'class' => array(), 'priority' => 75, ); return $tabs; } /** * Scripts on the product edit screen only. * * @param string $hook_suffix Admin page. */ public function enqueue( string $hook_suffix ): void { if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = get_current_screen(); if ( null === $screen || 'product' !== $screen->post_type ) { return; } wp_enqueue_editor(); wp_enqueue_style( 'pnscripts-product-tabs-admin', PNSCRIPTS_PRODUCT_TABS_URL . 'assets/css/admin.css', array(), PNSCRIPTS_PRODUCT_TABS_VERSION ); wp_enqueue_script( 'pnscripts-product-tabs-product', PNSCRIPTS_PRODUCT_TABS_URL . 'assets/js/admin-product.js', array( 'jquery', 'jquery-ui-sortable', 'editor' ), PNSCRIPTS_PRODUCT_TABS_VERSION, true ); wp_localize_script( 'pnscripts-product-tabs-product', 'pnscriptsProductTabs', array( 'confirmRemove' => __( 'Remove this tab? It is deleted when you update the product.', 'pnscripts-tabcrest' ), 'untitled' => __( '(no title)', 'pnscripts-tabcrest' ), ) ); } /** * Panel markup. */ public function render_panel(): void { global $post; $product_id = $post instanceof \WP_Post ? (int) $post->ID : 0; $tabs = $product_id > 0 ? $this->product_tabs->tabs( $product_id ) : array(); $globals = $this->all_globals(); $hidden_g = $product_id > 0 ? $this->product_tabs->hidden_globals( $product_id ) : array(); $hidden_d = $product_id > 0 ? $this->product_tabs->hidden_defaults( $product_id ) : array(); $linked = array(); foreach ( $tabs as $tab ) { if ( TabSanitizer::TYPE_GLOBAL === $tab['type'] ) { $linked[ $tab['global_id'] ] = true; } } $context = array( 'product_id' => $product_id, 'categories' => $product_id > 0 ? TabRenderer::with_ancestors( array_map( 'intval', wc_get_product_term_ids( $product_id, 'product_cat' ) ), 'product_cat' ) : array(), 'tags' => $product_id > 0 ? array_values( array_map( 'intval', wc_get_product_term_ids( $product_id, 'product_tag' ) ) ) : array(), ); ?> $globals Global tabs. */ private function render_row( string $index, array $tab, array $globals ): void { $name = self::FIELD . '[tabs][' . $index . ']'; $is_new = '__INDEX__' === $index; $editor = 'pnscripts-pt-content-' . $index; $global = TabSanitizer::TYPE_GLOBAL === $tab['type'] ? ( $globals[ $tab['global_id'] ] ?? null ) : null; $title = TabSanitizer::TYPE_GLOBAL === $tab['type'] ? ( null !== $global ? $global['title'] : __( '(deleted global tab)', 'pnscripts-tabcrest' ) ) : $tab['title']; $type_txt = array( TabSanitizer::TYPE_CONTENT => __( 'Rich text', 'pnscripts-tabcrest' ), TabSanitizer::TYPE_FAQ => __( 'FAQ', 'pnscripts-tabcrest' ), TabSanitizer::TYPE_GLOBAL => __( 'Global', 'pnscripts-tabcrest' ), )[ $tab['type'] ] ?? ''; ?>
  • $item ) { self::render_faq_item( $name . '[faq][' . $i . ']', $item ); } ?>

  • get_id(); if ( $product_id <= 0 || ! current_user_can( 'edit_post', $product_id ) ) { return; } // Sanitised field by field in TabSanitizer / ProductTabs::default_keys(). $raw = isset( $_POST[ self::FIELD ] ) && is_array( $_POST[ self::FIELD ] ) ? wp_unslash( $_POST[ self::FIELD ] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( ! is_array( $raw ) || ! isset( $raw['submitted'] ) ) { return; } $tabs = TabSanitizer::product_tabs( isset( $raw['tabs'] ) && is_array( $raw['tabs'] ) ? array_values( $raw['tabs'] ) : array(), current_user_can( 'unfiltered_html' ) ); $this->product_tabs->save( $product_id, $tabs, TabSanitizer::id_list( $raw['hidden_globals'] ?? array() ), ProductTabs::default_keys( $raw['hidden_defaults'] ?? array() ) ); } /** * All global tabs (published and drafts) for the admin selector. * * @return array */ private function all_globals(): array { $out = $this->globals->all(); return $out; } /** * Empty tab for templates. * * @param string $type Type. * @return ProductTab */ private static function blank( string $type ): array { return array( 'id' => '', 'type' => $type, 'title' => '', 'content' => '', 'faq' => TabSanitizer::TYPE_FAQ === $type ? array( array( 'q' => '', 'a' => '', ), ) : array(), 'schema' => true, 'global_id' => 0, 'enabled' => true, 'origin' => '', ); } /** * Translated names of the default tabs. * * @return array */ public static function default_labels(): array { $labels = array( 'description' => __( 'Description', 'pnscripts-tabcrest' ), 'additional_information' => __( 'Additional information', 'pnscripts-tabcrest' ), 'reviews' => __( 'Reviews', 'pnscripts-tabcrest' ), ); return array_intersect_key( $labels, array_flip( DefaultTabs::KEYS ) ); } }