'wc_coupon_message', 'textarea_rows' => 10, 'editor_class' => 'wp_editor_coupon_message', 'media_buttons' => true, 'tinymce' => true, ); echo '
'; } /** * Function to save coupon plus data in coupon's meta * * @since 1.0 * * @param integer $post_id Coupon's id. * @param WC_Coupon $coupon Current coupon object. */ public function wc_process_coupon_message_meta( $post_id = 0, $coupon = null ) { if ( empty( $post_id ) ) { return; } $coupon = new WC_Coupon( $coupon ); $allowed_html = wp_kses_allowed_html( 'post' ); $additional_allowed_html = array( 'style' => array(), ); $additional_allowed_html = apply_filters( 'wc_sc_kses_allowed_html_for_coupon_message', $additional_allowed_html, array( 'source' => $this ) ); if ( ! empty( $additional_allowed_html ) ) { foreach ( $additional_allowed_html as $tag => $attributes ) { if ( ! empty( $attributes ) && array_key_exists( $tag, $allowed_html ) ) { $allowed_html[ $tag ] = array_merge( $allowed_html[ $tag ], $attributes ); } else { $allowed_html[ $tag ] = $attributes; } } } if ( $this->is_callable( $coupon, 'update_meta_data' ) && $this->is_callable( $coupon, 'save' ) ) { if ( isset( $_POST['wc_coupon_message'] ) ) { // phpcs:ignore $coupon->update_meta_data( 'wc_coupon_message', wp_kses( wp_unslash( $_POST['wc_coupon_message'] ), $allowed_html ) ); // phpcs:ignore } if ( isset( $_POST['wc_email_message'] ) ) { // phpcs:ignore $coupon->update_meta_data( 'wc_email_message', wc_clean( wp_unslash( $_POST['wc_email_message'] ) ) ); // phpcs:ignore } else { $coupon->update_meta_data( 'wc_email_message', 'no' ); } $coupon->save(); } else { if ( isset( $_POST['wc_coupon_message'] ) ) { // phpcs:ignore update_post_meta( $post_id, 'wc_coupon_message', wp_kses( wp_unslash( $_POST['wc_coupon_message'] ), $allowed_html ) ); // phpcs:ignore } if ( isset( $_POST['wc_email_message'] ) ) { // phpcs:ignore update_post_meta( $post_id, 'wc_email_message', wc_clean( wp_unslash( $_POST['wc_email_message'] ) ) ); // phpcs:ignore } else { update_post_meta( $post_id, 'wc_email_message', 'no' ); } } } /** * Function to print coupon message * * @param array $applied_coupons Applied coupons. */ public function print_coupon_message( $applied_coupons = array() ) { if ( empty( $applied_coupons ) ) { echo ''; return; } foreach ( $applied_coupons as $coupon_code ) { $coupon = new WC_Coupon( $coupon_code ); if ( ! $this->is_valid( $coupon ) ) { continue; } if ( $this->is_wc_gte_30() ) { $coupon_id = ( ! empty( $coupon ) && is_callable( array( $coupon, 'get_id' ) ) ) ? $coupon->get_id() : 0; } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; } $wc_coupon_message = ( $this->is_callable( $coupon, 'get_meta' ) ) ? $coupon->get_meta( 'wc_coupon_message' ) : get_post_meta( $coupon_id, 'wc_coupon_message', true ); if ( empty( $wc_coupon_message ) ) { continue; } ?> cart ) || WC()->cart->is_empty() ) { return; } $applied_coupons = WC()->cart->get_applied_coupons(); ?> cart->get_applied_coupons(); $this->print_coupon_message( $applied_coupons ); die(); } /** * Function to add coupon's message in email * * @since 1.0 * * @param WC_Order $order Order's object. * @param boolean $bool Not used in this function. * @param boolean $plain_text Not used in this function. */ public function wc_add_coupons_message_in_email( $order = null, $bool = false, $plain_text = false ) { $used_coupons = $this->get_coupon_codes( $order ); if ( count( $used_coupons ) <= 0 ) { return; } $show_coupon_message_title = false; $coupon_messages = ''; foreach ( $used_coupons as $coupon_code ) { $coupon = new WC_Coupon( $coupon_code ); if ( $this->is_wc_gte_30() ) { $coupon_id = ( ! empty( $coupon ) && is_callable( array( $coupon, 'get_id' ) ) ) ? $coupon->get_id() : 0; } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; } $is_callable_coupon_get_meta = $this->is_callable( $coupon, 'get_meta' ); if ( true === $is_callable_coupon_get_meta ) { $coupon_message = $coupon->get_meta( 'wc_coupon_message' ); $include_in_email = $coupon->get_meta( 'wc_email_message' ); } else { $coupon_message = get_post_meta( $coupon_id, 'wc_coupon_message', true ); $include_in_email = get_post_meta( $coupon_id, 'wc_email_message', true ); } if ( ! empty( $coupon_message ) && 'yes' === $include_in_email ) { $is_filter_content = apply_filters( 'wc_sc_is_filter_content_coupon_message', true, array( 'source' => $this, 'called_by' => current_filter(), 'coupon_object' => $coupon, 'order_object' => $order, ) ); if ( true === $is_filter_content ) { $coupon_messages .= apply_filters( 'the_content', $coupon_message ); } else { $coupon_messages .= $coupon_message; } $show_coupon_message_title = true; } } if ( $show_coupon_message_title ) { ?> '; echo wp_kses_post( $coupon_messages ); // phpcs:ignore echo ''; } } /** * Add meta in export headers * * @param array $headers Existing headers. * @return array */ public function export_headers( $headers = array() ) { $cm_headers = array( 'wc_coupon_message' => __( 'Coupon Message', 'woocommerce-smart-coupons' ), 'wc_email_message' => __( 'Is Email Coupon Message', 'woocommerce-smart-coupons' ), ); return array_merge( $headers, $cm_headers ); } /** * Post meta defaults for CM's meta * * @param array $defaults Existing postmeta defaults. * @return array */ public function postmeta_defaults( $defaults = array() ) { $cm_defaults = array( 'wc_coupon_message' => '', 'wc_email_message' => '', ); return array_merge( $defaults, $cm_defaults ); } /** * Add CM's meta with value in coupon meta * * @param array $data The row data. * @param array $post The POST values. * @return array Modified data */ public function generate_coupon_meta( $data = array(), $post = array() ) { $data['wc_coupon_message'] = ( ! empty( $post['wc_coupon_message'] ) ) ? wp_kses_post( $post['wc_coupon_message'] ) : ''; $data['wc_email_message'] = ( ! empty( $post['wc_email_message'] ) ) ? wc_clean( wp_unslash( $post['wc_email_message'] ) ) : 'no'; return $data; } /** * Make meta data of SC CM, protected * * @param bool $protected Is protected. * @param string $meta_key The meta key. * @param string $meta_type The meta type. * @return bool $protected */ public function make_action_meta_protected( $protected, $meta_key, $meta_type ) { $sc_meta = array( 'wc_coupon_message' => '', 'wc_email_message' => '', ); if ( in_array( $meta_key, $sc_meta, true ) ) { return true; } return $protected; } /** * Function to copy CM meta in newly generated coupon * * @param array $args The arguments. */ public function copy_coupon_action_meta( $args = array() ) { $new_coupon_id = ( ! empty( $args['new_coupon_id'] ) ) ? absint( $args['new_coupon_id'] ) : 0; $coupon = ( ! empty( $args['ref_coupon'] ) ) ? $args['ref_coupon'] : false; if ( empty( $new_coupon_id ) || empty( $coupon ) ) { return; } $new_coupon = new WC_Coupon( $new_coupon_id ); if ( $this->is_callable( $new_coupon, 'get_meta' ) && $this->is_callable( $new_coupon, 'update_meta_data' ) && $this->is_callable( $new_coupon, 'save' ) ) { $coupon_message = $coupon->get_meta( 'wc_coupon_message' ); $email_message = $coupon->get_meta( 'wc_email_message' ); $new_coupon->update_meta_data( 'wc_coupon_message', wp_filter_post_kses( $coupon_message ) ); $new_coupon->update_meta_data( 'wc_email_message', $email_message ); } else { $old_coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; $coupon_message = get_post_meta( $old_coupon_id, 'wc_coupon_message', true ); $email_message = get_post_meta( $old_coupon_id, 'wc_email_message', true ); update_post_meta( $new_coupon_id, 'wc_coupon_message', wp_filter_post_kses( $coupon_message ) ); update_post_meta( $new_coupon_id, 'wc_email_message', $email_message ); } } } } WC_SC_Coupon_Message::get_instance();