* @copyright Copyright (c) 2018 Ekomi ltd (http://www.ekomi.de) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link http://www.ekomi.de */ /** * Returns the smart prc widget. * * @param string $product_id The product id. * * @return string */ function ekomi_prc_widget_link_shortcode( $product_id ) { $lang_code = EKOMI_INTEGRATION\ei_get_default_lang_code(); if ( isset( $_GET['lang'] ) ) { $lang_code = sanitize_key( $_GET['lang'] ); } else { $lang_code = ei_get_lang_code_from_url( $lang_code ); } $lang_code = explode( '_', $lang_code )[0]; if ( get_option( $lang_code . '_ee_active', '0' ) && get_option( $lang_code . '_ee_show_prc_widget', '0' ) && get_option( $lang_code . '_ee_prc_widget_token', '' ) ) { ob_start(); $product_identifier = ei_get_product_identfier( $product_id, $lang_code ); $customer_id = get_option( $lang_code . '_ee_shop_id', '' ); $widget_token = get_option( $lang_code . '_ee_prc_widget_token', '' ); include 'templates/prc-widget.php'; $widget = ob_get_contents(); ob_end_clean(); return $widget; } else { return ''; } } /** * Gets lang code from url. * * @param string $lang_code The Language iso code. * * @return mixed */ function ei_get_lang_code_from_url( $lang_code ) { $url = ''; if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { $url = sanitize_key( $_SERVER['HTTP_HOST'] ) . sanitize_key( $_SERVER['REQUEST_URI'] ); } foreach ( EKOMI_INTEGRATION\ei_prepare_lang_array( $lang_code ) as $code ) { if ( strpos( $url, $code . '.' ) !== false || strpos( $url, "/{$code}/" ) !== false ) { return $code; } } return $lang_code; } /** * Checks if product identifier is sku. * * @param mixed $product_id The product identifier. * @param string $lang_code The language code. * * @return mixed */ function ei_get_product_identfier( $product_id, $lang_code ) { if ( isset( $product_id['product_id'] ) ) { $product_id = $product_id['product_id']; } $product_identifier_option = get_option( $lang_code . '_ee_product_identifier', EKOMI_INTEGRATION\PRODUCT_IDENTIFIER_ID ); if ( ei_is_product_identfier_sku( $product_identifier_option ) ) { $product_id = ei_get_product_sku( $product_id ); } return $product_id; } /** * Checks the product identifier either Id or Sku. * * @param array $product_identifier_option Plugin configurations. * * @return boolean */ function ei_is_product_identfier_sku( $product_identifier_option ) { if ( EKOMI_INTEGRATION\PRODUCT_IDENTIFIER_SKU === $product_identifier_option ) { return true; } return false; } /** * Gets product sku. * * @param mixed $product_id The product identifier. * * @return mixed */ function ei_get_product_sku( $product_id ) { $_pf = new WC_Product_Factory(); $product = $_pf->get_product( $product_id ); return $product->get_sku(); } /** * Register short codes. */ add_shortcode( 'ekomi_prc_widget', __NAMESPACE__ . '\\ekomi_prc_widget_link_shortcode' );