plugin_name = $plugin_name; $this->version = $version; } /** * Registers the product metabox. * * @param string $post_type Current post type. * @return void */ public function dweb_ps_add_boxes( $post_type ) { if ( in_array( $post_type, array( 'product' ), true ) ) { add_meta_box( '3dweb-ps-woo-product-sku', __( 'Product ID in Configurator', '3dweb-print-studio' ), array( self::class, 'dweb_ps_product_360_view_callback', ), $post_type, 'normal', 'core' ); } } /** * Saves the metabox values. * * @param int $post_id Current post ID. * @return void */ public function dweb_ps_save_boxes( $post_id ) { if ( ! $this->dweb_ps_is_valid_save_request( $post_id ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification happens in dweb_ps_is_valid_save_request(). if ( isset( $_POST[ self::FIELD_PRODUCT_SKU ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification happens in dweb_ps_is_valid_save_request(). update_post_meta( $post_id, self::FIELD_PRODUCT_SKU, sanitize_text_field( wp_unslash( $_POST[ self::FIELD_PRODUCT_SKU ] ) ) ); delete_post_meta( $post_id, self::LEGACY_FIELD_PRODUCT_SKU ); } else { delete_post_meta( $post_id, self::FIELD_PRODUCT_SKU ); delete_post_meta( $post_id, self::LEGACY_FIELD_PRODUCT_SKU ); } } /** * Searches configurator products via AJAX. * * @return void */ public function dweb_ps_search_products() { check_ajax_referer( DWeb_PS_Admin::AJAX_NONCE_ACTION, '_ajax_nonce' ); // phpcs:ignore WordPress.WP.Capabilities.Unknown -- WooCommerce registers product editing capabilities. if ( ! current_user_can( 'edit_products' ) ) { wp_send_json_error( array( 'error' => 'Permission denied' ), 403 ); } $raw_search = filter_input( INPUT_GET, 'search', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $search = is_string( $raw_search ) ? sanitize_text_field( wp_unslash( $raw_search ) ) : ''; $api = new DWeb_PS_API(); $result = $api->dweb_ps_search_products( $search ); if ( isset( $result['error'] ) ) { wp_send_json_error( $result ); } wp_send_json_success( $result ); } /** * Enqueues metabox scripts on product edit screens. * * @param string $hook Current admin hook suffix. * @return void */ public function dweb_ps_enqueue_scripts( $hook ) { if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'product' !== $screen->post_type ) { return; } wp_enqueue_script( '3dweb-ps-admin' ); wp_register_script( '3dweb-ps-woo-metabox', plugin_dir_url( dirname( __DIR__ ) ) . 'admin/js/woo-metabox.js', array( 'jquery', '3dweb-ps-admin' ), $this->version, true ); wp_localize_script( '3dweb-ps-woo-metabox', 'dwebPsWooMetabox', array( 'noProductsFound' => __( 'No products found', '3dweb-print-studio' ), 'searchError' => __( 'Error searching products', '3dweb-print-studio' ), ) ); wp_enqueue_script( '3dweb-ps-woo-metabox' ); } /** * Validates the product save request. * * @param int $post_id Current post ID. * @return bool */ private function dweb_ps_is_valid_save_request( $post_id ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return false; } if ( wp_is_post_revision( $post_id ) ) { return false; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return false; } if ( ! isset( $_POST[ self::FIELD_NONCE ] ) ) { return false; } $nonce = sanitize_text_field( wp_unslash( $_POST[ self::FIELD_NONCE ] ) ); if ( '' === $nonce ) { return false; } return (bool) wp_verify_nonce( $nonce, self::FIELD_NONCE_ACTION ); } /** * Renders the product mapping metabox. * * @param WP_Post $post Current product post object. * @return void */ public static function dweb_ps_product_360_view_callback( $post ) { wp_nonce_field( self::FIELD_NONCE_ACTION, self::FIELD_NONCE ); $product_id = self::dweb_ps_get_product_sku( $post->ID ); $token = get_option( DWeb_PS_ADMIN_API::TOKEN, '' ); $host = get_option( DWeb_PS_ADMIN_API::CONFIGURATOR_HOST, DWeb_PS_API::DEFAULT_CONFIGURATOR_HOST ); if ( empty( $host ) ) { $host = DWeb_PS_API::DEFAULT_CONFIGURATOR_HOST; } $has_credentials = ! empty( $token ) && ! empty( $host ); ?>

▾