Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class wpw_woocommerce {
- private static $plugin_url;
- private static $plugin_dir; private static $plugin_basename;
- /* private static $plugin_title = "WhatsApp Share Setting";*/
- private static $plugin_slug = "wpw_woocommerce";
- private static $wpw_option_key = "wpw-settings";
- private $wpw_settings;
- public function __construct()
- {
- global $wpw_plugin_dir, $wpw_plugin_url,$wpw_plugin_basename;
- /* set plugin url and directory variables */
- self::$plugin_dir = $wpw_plugin_dir;
- self::$plugin_url = $wpw_plugin_url; self::$plugin_basename=$wpw_plugin_basename;
- /* load all whhatsapp settings */
- $this->wpw_settings = get_option(self::$wpw_option_key);
- /* admin menu for whatsapp share setting page*/
- add_action("admin_menu", array($this, "wpw_show_menu"));
- add_action("wp_head", array($this, "wpw_head"));
- add_action("wp_footer", array($this, "wpw_footer"));
- add_action("admin_init", array($this, "wpw_head"));
- add_action('admin_enqueue_scripts', array($this, "admin_style_head"));
- /*add_filter("plugin_action_links_".self::$plugin_basename, array($this, "wpw_action_links"));*/
- add_action("admin_init", array($this, "wpw_display_options"));
- if ($this->get_setting("enable") == 1) {
- $this->load_share_button();
- }
- } // Add settings link on plugin page /*public function wpw_action_links( $links ) { $links = array_merge( array( '<a href="' . esc_url( admin_url( '/admin.php?page=wpw_woocommerce' ) ) . '">' . __( 'Settings', 'textdomain' ) . '</a>' ), $links ); return $links; }*/
- //Admin Error Message
- public function wpw_missing_woocommerce_notice() {
- ?>
- <div class="error notice">
- <p><?php _e( 'You need to install and activate WooCommerce in order to use Whatsapp Share for Woocommerce!', 'whatsapp-share-for-woocommerce' ); ?></p>
- </div>
- <?php
- }
- // Admin Settings Menu
- public function wpw_show_menu(){
- add_submenu_page( 'woocommerce', 'Whatsapp Share Settings',
- 'Whatsapp Share Settings',
- 'manage_options',
- self::$plugin_slug,
- array($this, "wpw_settings_page"),
- 'dashicons-location',
- 1000 );
- }
- /* admin setting page */
- public function wpw_settings_page()
- {
- /* save whatsapp share button setting */
- if (isset($_POST[self::$plugin_slug])) {
- $this->setSetting();
- }
- /* include admin whatsapp share button setting file */
- include_once self::$plugin_dir . "view/rkc-whatsapp-share-setting-page.php";
- }
- /* function for saving setting */
- public function setSetting()
- {
- $arrayRemove = array(self::$plugin_slug, "wpw-submit");
- $saveSetting = array();
- foreach ($_POST as $key => $value):
- if (in_array($key, $arrayRemove)) {
- continue;
- }
- $saveSetting[$key] = $value;
- endforeach;
- $this->wpw_settings = $saveSetting;
- update_option(self::$wpw_option_key, $saveSetting);
- }
- /* function for get setting */
- public function get_setting($key)
- {
- if (!$key || $key == "") {
- return;
- }
- if (!isset($this->wpw_settings[$key])) {
- return;
- }
- return $this->wpw_settings[$key];
- }
- /* display options */
- public function wpw_display_options()
- {
- if (!class_exists( 'WooCommerce' )) {
- add_action( 'admin_notices', array($this, "wpw_missing_woocommerce_notice"));
- }
- //here we display the sections and options in the settings page based on the active tab
- add_settings_section("whatsapp_section", "Whatsapp Setting Options", array($this, "wpw_display_whatsapp_options_content"), "whatsapp-options");
- add_settings_section("general_section", "Setting Options", array($this, "wpw_display_general_options_content"), "general-options");
- }
- /* adding header meta required for sharing title,image,url */
- public function wpw_head()
- {
- if (is_product() && $this->get_setting("enable") == 1){
- global $post;
- $product = wc_get_product($post->ID);
- $image = wp_get_attachment_image_src($product->get_image_id());
- ?>
- <meta property="og:type" content="website" />
- <meta property="og:url" content="<?php echo get_permalink($post->ID); ?>"/>
- <meta property="og:image" itemprop="image" content="<?php echo $image[0]; ?>"/>
- <meta property="og:title" content="<?php echo $product->get_title(); ?>"/>
- <?php
- wp_enqueue_style(self::$plugin_slug,self::$plugin_url . "assets/css/wpw-style.css", array(), null, 'all' );
- }
- }
- function admin_style_head() {
- wp_enqueue_style('admin-styles', self::$plugin_slug,self::$plugin_url."assets/css/wpw-style.css");
- }
- /* adding required javascript file */
- public function wpw_footer()
- {
- wp_enqueue_script(self::$plugin_slug, self::$plugin_url . "assets/js/wpw-whatsapp.js");
- }
- public function display_whatsapp_button()
- {
- global $post;
- $product = wc_get_product($post->ID);
- $price = $this->get_product_price($product);
- $saleprice = $this->get_sale_product_price($product);
- $title = "*".$product->post->post_title."*";
- $description = strip_tags($product->post->post_excerpt);
- $description = ($this->get_setting("limit") > 0) ? substr($description, 0, $this->get_setting("limit")) : $description;
- $btn_class = "wa_btn_" . $this->get_setting("size");
- $alignClass = "text" . $this->get_setting("alignment");
- $buttonText = ($this->get_setting("btntext") != "") ? $this->get_setting("btntext") : "";
- $msg = str_replace(array("[product-title]", "[product-price]", "[product-description]","[product-saleprice]"), array($title, $price, $description, $saleprice), $this->get_setting("product_share_message"));
- $msg = $this->convert_html_for_mobile($msg);
- $sharebutton = "";
- $sharebutton.= '<div class="whatsapp_wrapper ' . $alignClass . '">';
- if ($this->get_setting("enable_sharing") == 1) {
- $sharebutton.= '<div class="whatsapp-button">';
- $sharebutton.='<a href="whatsapp://send" data-text="' . $msg . '" data-href="' . get_permalink($post->ID) . '" class="wa_btn ' . $btn_class . '" style="display:none;">' . $buttonText . '</a>';
- $sharebutton.='</div>';
- }
- $sharebutton.='<input type="hidden" id="wpw_only_mobile" value="'.$this->get_setting("only_mobile").'"/>';
- $sharebutton.='<input type="hidden" id="wpw_iconcolor" value="'.$this->get_setting("iconcolor").'"/>';
- $sharebutton.='<input type="hidden" id="wpw_transparent" value="'.$this->get_setting("transparent").'"/>';
- $sharebutton.='</div>';
- echo $sharebutton;
- }
- public function convert_html_for_mobile($content)
- {
- $content = nl2br($content);
- $content = str_replace(array('<br>', '<br />', '<br/>'), array('%0a', '%0a', '%0a'), $content);
- $content = str_replace('%0a', '', $content);
- $content = strip_tags($content);
- return $content;
- }
- /* get product price */
- public function get_product_price($product)
- {
- global $woocommerce;
- //return get_woocommerce_currency_symbol() . $product->get_price();
- if($product->is_on_sale()){
- if ($product->is_type( 'simple' )) {
- $regular_price = $product->get_regular_price();
- }
- elseif($product->is_type('variable')){
- $regular_price = $product->get_variation_regular_price( 'max', true );
- }
- return "~".get_woocommerce_currency_symbol()." ".$regular_price."~";
- }else if($product->get_regular_price()!=0){
- if ($product->is_type( 'simple' )) {
- $regular_price = $product->get_regular_price();
- }
- elseif($product->is_type('variable')){
- $regular_price = $product->get_variation_regular_price( 'max', true );
- }
- return "*".get_woocommerce_currency_symbol()." ".$regular_price."*";
- }else{
- return "*".get_woocommerce_currency_symbol()." ".$product->get_price()."*";
- }
- }
- public function get_sale_product_price($product){
- global $woocommerce;
- //return get_woocommerce_currency_symbol() . $product->get_price();
- if($product->is_on_sale()){
- if ($product->is_type( 'simple' )) {
- $sale_price = $product->get_sale_price();
- }
- elseif($product->is_type('variable')){
- $sale_price = $product->get_variation_sale_price( 'min', true );
- }
- return "*".get_woocommerce_currency_symbol()." ".$sale_price."*";
- }else{
- return $product->get_sale_price();
- }
- }
- // public function get_var_product_price($product){
- // global $woocommerce;
- // $product_variations = $product->get_available_variations();
- // $variation_product_id = $product_variations [0]['variation_id'];
- // $variation_product = new WC_Product_Variation( $variation_product_id );
- // //return get_woocommerce_currency_symbol() . $product->get_price();
- // if($product->is_on_sale()){
- // return "*".get_woocommerce_currency_symbol()." ".$variation_product->regular_price."*";
- // }else{
- // return $product->get_sale_price();
- // }
- // }
- /* display whatsapp sharing button for wordpress posts and pages */
- public function display_whatsapp_button_postpage($content)
- {
- if (!is_single() && !is_page()) {
- return $content;
- }
- global $post;
- if ($post->post_type == "product") {
- return $content;
- }
- if ($this->get_setting("enable_in_postpage") == 2 && !is_single()) {
- return $content;
- }
- if ($this->get_setting("enable_in_postpage") == 3 && !is_page()) {
- return $content;
- }
- $title = $post->post_title;
- $description = $post->post_excerpt;
- $description = ($this->get_setting("limit") > 0) ? substr($description, 0, $this->get_setting("limit")) : $description;
- $alignClass = "text" . $this->get_setting("alignment");
- $btn_class = "wa_btn_" . $this->get_setting("size");
- $buttonText = ($this->get_setting("btntext") != "") ? $this->get_setting("btntext") : "Share";
- $msg = str_replace(array("[title]", "[description]"), array($title, $description), $this->get_setting("postpage_share_message"));
- $msg = $this->convert_html_for_mobile($msg);
- $sharebutton = '<div class="whatsapp_wrapper ' . $alignClass . '">';
- $sharebutton.='<a href="whatsapp://send" data-text="' . $msg . '" data-href="' . get_permalink($post->ID) . '" class="wa_btn ' . $btn_class . '" style="display:none;">' . $buttonText . '</a>';
- return $content;
- }
- /* load whatsapp sharebutton hooks */
- public function load_share_button()
- {
- /* hook for display button on product page */
- if ($this->get_setting("button_pos") == 1) {
- add_action('woocommerce_single_product_summary', array(&$this, 'display_whatsapp_button'), 30);
- } elseif ($this->get_setting("button_pos") == 2) {
- add_action('woocommerce_single_product_summary', array(&$this, 'display_whatsapp_button'), 15);
- }
- elseif ($this->get_setting("button_pos") == 3) {
- add_action('woocommerce_share', array(&$this, 'display_whatsapp_button'), 6);
- } else {
- add_action('woocommerce_single_product_summary', array(&$this, 'display_whatsapp_button'), 8);
- }
- /* hook for display button on posts and pages */
- if ($this->get_setting("enable_in_postpage") != 0 && $this->get_setting("enable_in_postpage") != "") {
- add_filter('the_content', array(&$this, 'display_whatsapp_button_postpage'));
- }
- }
- }
- new wpw_woocommerce();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement