do_add_generate_button = ( 'yes' === get_option( 'alg_wc_ean_backend_add_generate_button', 'no' ) ); if ( $this->do_add_generate_button ) { add_action( 'admin_footer', array( $this, 'add_generate_button' ) ); add_action( 'wp_ajax_alg_wc_ean_generate_ajax', array( $this, 'generate_button_ajax' ) ); } } } /** * add_generate_button. * * @version 4.0.0 * @since 4.0.0 */ function add_generate_button() { if ( is_admin() && function_exists( 'get_current_screen' ) && ( $screen = get_current_screen() ) && 'product' === $screen->post_type ) { $this->generate_button_js(); } } /** * generate_button_js. * * @version 4.0.0 * @since 4.0.0 * * @todo (dev) static? (3x) * @todo (dev) recheck `return false;` * @todo (dev) recheck `ajaxurl` * @todo (dev) use `admin_enqueue_scripts`? */ static function generate_button_js() { ?>core->product_tools->generate_ean( intval( $_POST['product'] ), alg_wc_ean()->core->product_tools->get_generate_data() ); die(); } /** * get_generate_button. * * @version 4.0.0 * @since 4.0.0 * * @todo (dev) spinner: styling? * @todo (dev) spinner: `float: none;`? * @todo (dev) spinner: wcfm */ static function get_generate_button( $product_id, $input_html_id ) { return '' . sprintf( esc_html__( 'Generate %s', 'ean-for-woocommerce' ), get_option( 'alg_wc_ean_title', esc_html__( 'EAN', 'ean-for-woocommerce' ) ) ) . '' . ''; } /** * add_bulk_and_quick_edit_fields. * * @version 2.2.7 * @since 1.5.0 * * @todo (dev) reposition this (e.g., right after the "SKU" field)? * @todo (dev) actual value (instead of "No change" placeholder)? (probably need to add value to `woocommerce_inline_`) (quick edit only?) */ function add_bulk_and_quick_edit_fields() { echo ( 'woocommerce_product_quick_edit_end' === current_filter() ? '
' : '' ) . ''; } /** * save_bulk_and_quick_edit_fields. * * @version 4.5.0 * @since 1.5.0 */ function save_bulk_and_quick_edit_fields( $post_id, $post ) { // If this is an autosave, our form has not been submitted, so we don't want to do anything if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } // Don't save revisions and autosaves if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || 'product' !== $post->post_type || ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; } // Check nonce if ( ! isset( $_REQUEST['woocommerce_quick_edit_nonce'] ) || ! wp_verify_nonce( $_REQUEST['woocommerce_quick_edit_nonce'], 'woocommerce_quick_edit_nonce' ) ) { // WPCS: input var ok, sanitization ok. return $post_id; } // Save if ( isset( $_REQUEST['_alg_ean_qb'] ) && '' !== $_REQUEST['_alg_ean_qb'] ) { alg_wc_ean()->core->set_ean( $post_id, wc_clean( $_REQUEST['_alg_ean_qb'] ) ); } return $post_id; } /** * get_ean_input_desc. * * @version 3.6.0 * @since 1.0.1 * * @todo (dev) replace `style` with `class` */ function get_ean_input_desc( $ean, $product_id = false ) { $desc = array(); if ( 'yes' === get_option( 'alg_wc_ean_backend_is_valid', 'yes' ) ) { $desc[] = ( alg_wc_ean()->core->is_valid_ean( $ean, $product_id ) ? '' . esc_html__( 'Valid EAN', 'ean-for-woocommerce' ) . '' : '' . esc_html__( 'Invalid EAN', 'ean-for-woocommerce' ) . '' ); } if ( 'yes' === get_option( 'alg_wc_ean_backend_is_unique', 'no' ) ) { $desc[] = ( ! alg_wc_ean()->core->do_ean_exist( $ean, $product_id ) ? '' . esc_html__( 'Unique EAN', 'ean-for-woocommerce' ) . '' : '' . esc_html__( 'Duplicated EAN', 'ean-for-woocommerce' ) . '' ); } return implode( ' | ', $desc ); } /** * get_ean_input_custom_atts. * * @version 4.8.0 * @since 1.0.1 * * @todo (dev) `AUTO`: better maxlength (13); add minlength (8) * @todo (dev) `ean-13`: `array( 'pattern' => '.{0}|[0-9]{13}', 'maxlength' => '13' ) )` * @todo (dev) `ean-13`: `array( 'pattern' => '.{0}|[0-9]+', 'minlength' => '13', 'maxlength' => '13' )` */ function get_ean_input_custom_atts( $product_id = false, $atts = array() ) { $result = $atts; // Required if ( 'yes' === get_option( 'alg_wc_ean_required', 'no' ) ) { $result = array_merge( $result, array( 'required' => 'required' ) ); } // Pattern and max length $type = false; if ( 'yes' === get_option( 'alg_wc_ean_add_pattern', 'no' ) ) { $type = alg_wc_ean()->core->get_type( false, false, $product_id ); switch ( $type ) { case 'EAN8': case 'UPCA': case 'EAN13': case 'ISBN13': case 'JAN': case 'EAN14': case 'AUTO': $result = array_merge( $result, array( 'pattern' => '[0-9]+', 'maxlength' => ( 'AUTO' === $type ? 13 : alg_wc_ean()->core->get_ean_type_length( $type ) ), ) ); break; } } // Deprecated filter $result = apply_filters( 'alg_wc_ean_input_pattern', $result, $atts, $type ); return apply_filters( 'alg_wc_ean_input_custom_atts', $result, $product_id, $atts ); } /** * add_ean_input_variation. * * @version 4.4.0 * @since 1.0.0 * * @todo (dev) `variable{$key}` to `variable_{$key}`? */ function add_ean_input_variation( $loop, $variation_data, $variation ) { $key = alg_wc_ean()->core->ean_key; woocommerce_wp_text_input( array( 'id' => "variable{$key}_{$loop}", 'name' => "variable{$key}[{$loop}]", 'value' => ( isset( $variation_data[ $key ][0] ) ? $variation_data[ $key ][0] : '' ), 'label' => esc_html( get_option( 'alg_wc_ean_title', __( 'EAN', 'ean-for-woocommerce' ) ) ), 'wrapper_class' => 'form-row form-row-full', 'placeholder' => alg_wc_ean()->core->get_ean( $variation->post_parent ), 'description' => ( ! empty( $variation_data[ $key ][0] ) ? $this->get_ean_input_desc( $variation_data[ $key ][0], $variation->ID ) : ( $this->do_add_generate_button ? '

' . $this->get_generate_button( $variation->ID, "variable{$key}_{$loop}" ) . '

' : '' ) ), 'custom_attributes' => $this->get_ean_input_custom_atts( $variation->ID ), ) ); } /** * save_ean_input_variation. * * @version 4.5.0 * @since 1.0.0 */ function save_ean_input_variation( $variation_id, $i ) { $key = alg_wc_ean()->core->ean_key; if ( isset( $_POST[ 'variable' . $key ][ $i ] ) ) { alg_wc_ean()->core->set_ean( $variation_id, wc_clean( $_POST[ 'variable' . $key ][ $i ] ) ); } } /** * add_ean_input. * * @version 4.4.0 * @since 1.0.0 */ function add_ean_input() { $product_id = get_the_ID(); $value = alg_wc_ean()->core->get_ean( $product_id ); woocommerce_wp_text_input( array( 'id' => alg_wc_ean()->core->ean_key, 'value' => $value, 'label' => esc_html( get_option( 'alg_wc_ean_title', __( 'EAN', 'ean-for-woocommerce' ) ) ), 'description' => ( ! empty( $value ) ? $this->get_ean_input_desc( $value, $product_id ) : ( $this->do_add_generate_button ? $this->get_generate_button( $product_id, alg_wc_ean()->core->ean_key ) : '' ) ), 'custom_attributes' => $this->get_ean_input_custom_atts( $product_id ), ) ); } /** * save_ean_input. * * @version 4.5.0 * @since 1.0.0 * * @todo (dev) save `$key . '_is_valid'` (same in `save_ean_input_variation()`) */ function save_ean_input( $post_id, $__post ) { if ( isset( $_POST[ alg_wc_ean()->core->ean_key ] ) && empty( $_REQUEST['woocommerce_quick_edit'] ) && empty( $_REQUEST['woocommerce_bulk_edit'] ) ) { alg_wc_ean()->core->set_ean( $post_id, wc_clean( $_POST[ alg_wc_ean()->core->ean_key ] ) ); } } } endif; return new Alg_WC_EAN_Edit();