add_product_tab( ... ) ); add_action( 'woocommerce_product_data_panels', $this->render_product_panel( ... ) ); add_action( 'woocommerce_admin_process_product_object', $this->save_product( ... ) ); add_action( 'product_cat_add_form_fields', $this->render_term_add_fields( ... ) ); add_action( 'product_cat_edit_form_fields', $this->render_term_edit_fields( ... ) ); add_action( 'created_product_cat', $this->save_term( ... ) ); add_action( 'edited_product_cat', $this->save_term( ... ) ); } /** * Adds the "Button text" tab to the product data box. * * @param mixed $tabs Tabs. * @return array */ public function add_product_tab( mixed $tabs ): array { $tabs = is_array( $tabs ) ? $tabs : array(); $tabs[ self::TAB ] = array( 'label' => __( 'Button text', 'add-to-cart-text' ), 'target' => 'adtct_product_data', 'class' => array(), 'priority' => 75, ); return $tabs; } /** * Renders the product data panel. * * @return void */ public function render_product_panel(): void { global $product_object; $product = $product_object instanceof \WC_Product ? $product_object : null; ?> verify_nonce( self::PRODUCT_NONCE ) ) { return; } if ( ! current_user_can( 'edit_post', $product->get_id() ) ) { return; } foreach ( Context::cases() as $context ) { $value = $this->posted_text( $context ); if ( '' === $value ) { $product->delete_meta_data( $context->product_meta_key() ); } else { $product->update_meta_data( $context->product_meta_key(), $value ); } } } /** * Fields on the "Add new category" form. * * @return void */ public function render_term_add_fields(): void { wp_nonce_field( self::TERM_NONCE, self::TERM_NONCE ); foreach ( Context::cases() as $context ) { $id = $context->field_name(); ?>

term_help( $context ) ); ?>

field_name(); $value = get_term_meta( $term->term_id, $context->term_meta_key(), true ); ?>

term_help( $context ) ); ?>

verify_nonce( self::TERM_NONCE ) || ! current_user_can( 'edit_term', $term_id ) ) { return; } foreach ( Context::cases() as $context ) { if ( ! isset( $_POST[ $context->field_name() ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in verify_nonce(). continue; } $value = $this->posted_text( $context ); if ( '' === $value ) { delete_term_meta( $term_id, $context->term_meta_key() ); } else { update_term_meta( $term_id, $context->term_meta_key(), $value ); } } } /** * Checks the nonce of a form. * * @param string $action Nonce action (= field name). * @return bool */ private function verify_nonce( string $action ): bool { $nonce = isset( $_POST[ $action ] ) ? sanitize_text_field( wp_unslash( $_POST[ $action ] ) ) : ''; return '' !== $nonce && false !== wp_verify_nonce( $nonce, $action ); } /** * Sanitized posted value of a context field (call only after verify_nonce()). * * @param Context $context Button context. * @return string */ private function posted_text( Context $context ): string { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce verified by the caller, sanitized by Settings::clean_text(). return isset( $_POST[ $context->field_name() ] ) ? Settings::clean_text( wp_unslash( $_POST[ $context->field_name() ] ) ) : ''; } /** * Label of a category field. * * @param Context $context Button context. * @return string */ private function term_label( Context $context ): string { /* translators: %s: "Product page" or "Shop & archives". */ return sprintf( __( 'Button text – %s', 'add-to-cart-text' ), $context->label() ); } /** * Help text of a category field. * * @param Context $context Button context. * @return string */ private function term_help( Context $context ): string { return $context->help() . ' ' . __( 'Applies to all products in this category and its subcategories. Placeholders: {name}, {price}.', 'add-to-cart-text' ); } }