Advertisement
zumixosan

WhatsApp Share

Oct 23rd, 2019
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.53 KB | None | 0 0
  1. <?php
  2.  
  3. class wpw_woocommerce {
  4.  
  5.     private static $plugin_url;
  6.  
  7.     private static $plugin_dir;     private static $plugin_basename;
  8.  
  9.    /* private static $plugin_title = "WhatsApp Share Setting";*/
  10.  
  11.     private static $plugin_slug = "wpw_woocommerce";
  12.  
  13.     private static $wpw_option_key = "wpw-settings";
  14.  
  15.     private $wpw_settings;
  16.  
  17.    
  18.  
  19.     public function __construct()
  20.  
  21.     {
  22.  
  23.         global $wpw_plugin_dir, $wpw_plugin_url,$wpw_plugin_basename;
  24.  
  25.         /* set plugin url and directory variables */
  26.  
  27.         self::$plugin_dir = $wpw_plugin_dir;
  28.  
  29.         self::$plugin_url = $wpw_plugin_url;        self::$plugin_basename=$wpw_plugin_basename;
  30.  
  31.  
  32.  
  33.         /* load all whhatsapp settings */
  34.  
  35.         $this->wpw_settings = get_option(self::$wpw_option_key);
  36.  
  37.        
  38.  
  39.         /* admin menu for whatsapp share setting page*/
  40.  
  41.         add_action("admin_menu", array($this, "wpw_show_menu"));
  42.  
  43.        
  44.  
  45.         add_action("wp_head", array($this, "wpw_head"));
  46.  
  47.         add_action("wp_footer", array($this, "wpw_footer"));
  48.  
  49.         add_action("admin_init", array($this, "wpw_head"));
  50.  
  51.         add_action('admin_enqueue_scripts', array($this, "admin_style_head"));
  52.  
  53.         /*add_filter("plugin_action_links_".self::$plugin_basename, array($this, "wpw_action_links"));*/
  54.  
  55.         add_action("admin_init", array($this, "wpw_display_options"));
  56.  
  57.        
  58.  
  59.         if ($this->get_setting("enable") == 1) {
  60.  
  61.             $this->load_share_button();
  62.  
  63.         }
  64.  
  65.     }       // 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;  }*/
  66.  
  67.     //Admin Error Message
  68.  
  69.     public function wpw_missing_woocommerce_notice() {
  70.  
  71.       ?>
  72.  
  73.       <div class="error notice">
  74.  
  75.           <p><?php _e( 'You need to install and activate WooCommerce in order to use Whatsapp Share for Woocommerce!', 'whatsapp-share-for-woocommerce' ); ?></p>
  76.  
  77.       </div>
  78.  
  79.       <?php
  80.  
  81.     }
  82.  
  83.    
  84.  
  85.     // Admin Settings Menu
  86.  
  87.     public function wpw_show_menu(){
  88.  
  89.         add_submenu_page( 'woocommerce', 'Whatsapp Share Settings',
  90.  
  91.             'Whatsapp Share Settings',
  92.  
  93.             'manage_options',
  94.  
  95.             self::$plugin_slug,
  96.  
  97.             array($this, "wpw_settings_page"),
  98.  
  99.             'dashicons-location',
  100.  
  101.             1000 );
  102.  
  103.     }
  104.  
  105.    
  106.  
  107.     /* admin setting page  */
  108.  
  109.     public function wpw_settings_page()
  110.  
  111.     {
  112.  
  113.        
  114.  
  115.         /* save whatsapp share button setting */
  116.  
  117.         if (isset($_POST[self::$plugin_slug])) {
  118.  
  119.             $this->setSetting();
  120.  
  121.         }
  122.  
  123.         /* include admin  whatsapp share button setting file */
  124.  
  125.         include_once self::$plugin_dir . "view/rkc-whatsapp-share-setting-page.php";
  126.  
  127.     }
  128.  
  129.     /* function for saving setting */
  130.  
  131.     public function setSetting()
  132.  
  133.     {
  134.  
  135.         $arrayRemove = array(self::$plugin_slug, "wpw-submit");
  136.  
  137.         $saveSetting = array();
  138.  
  139.         foreach ($_POST as $key => $value):
  140.  
  141.             if (in_array($key, $arrayRemove)) {
  142.  
  143.                 continue;
  144.  
  145.             }
  146.  
  147.             $saveSetting[$key] = $value;
  148.  
  149.         endforeach;
  150.  
  151.         $this->wpw_settings = $saveSetting;
  152.  
  153.         update_option(self::$wpw_option_key, $saveSetting);
  154.  
  155.     }
  156.  
  157.     /* function for get setting */
  158.  
  159.     public function get_setting($key)
  160.  
  161.     {
  162.  
  163.         if (!$key || $key == "") {
  164.  
  165.             return;
  166.  
  167.         }
  168.  
  169.         if (!isset($this->wpw_settings[$key])) {
  170.  
  171.             return;
  172.  
  173.         }
  174.  
  175.         return $this->wpw_settings[$key];
  176.  
  177.     }
  178.  
  179.     /* display options */  
  180.  
  181.     public function wpw_display_options()
  182.  
  183.     {
  184.  
  185.         if (!class_exists( 'WooCommerce' )) {
  186.  
  187.             add_action( 'admin_notices', array($this, "wpw_missing_woocommerce_notice"));
  188.  
  189.         }
  190.  
  191.         //here we display the sections and options in the settings page based on the active tab
  192.  
  193.         add_settings_section("whatsapp_section", "Whatsapp Setting Options", array($this, "wpw_display_whatsapp_options_content"), "whatsapp-options");    
  194.  
  195.         add_settings_section("general_section", "Setting Options", array($this, "wpw_display_general_options_content"), "general-options");
  196.  
  197.     }
  198.  
  199.    
  200.  
  201.     /* adding header meta required for sharing title,image,url */  
  202.  
  203.     public function wpw_head()
  204.  
  205.     {
  206.  
  207.         if (is_product() && $this->get_setting("enable") == 1){
  208.  
  209.             global $post;
  210.  
  211.             $product = wc_get_product($post->ID);
  212.  
  213.             $image = wp_get_attachment_image_src($product->get_image_id());
  214.  
  215.             ?>
  216.  
  217.             <meta property="og:type" content="website" />
  218.  
  219.             <meta property="og:url" content="<?php echo get_permalink($post->ID); ?>"/>
  220.  
  221.             <meta property="og:image" itemprop="image" content="<?php echo $image[0]; ?>"/>
  222.  
  223.             <meta property="og:title" content="<?php echo $product->get_title(); ?>"/>
  224.  
  225.             <?php          
  226.  
  227.                 wp_enqueue_style(self::$plugin_slug,self::$plugin_url . "assets/css/wpw-style.css", array(), null, 'all' );
  228.  
  229.         }
  230.  
  231.     }
  232.  
  233.  
  234.  
  235.     function admin_style_head() {
  236.  
  237.         wp_enqueue_style('admin-styles', self::$plugin_slug,self::$plugin_url."assets/css/wpw-style.css");
  238.  
  239.     }
  240.  
  241.     /* adding required javascript file */  
  242.  
  243.     public function wpw_footer()
  244.  
  245.     {
  246.  
  247.         wp_enqueue_script(self::$plugin_slug, self::$plugin_url . "assets/js/wpw-whatsapp.js");
  248.  
  249.     }
  250.  
  251.    
  252.  
  253.     public function display_whatsapp_button()
  254.     {
  255.         global $post;
  256.      
  257.         $product = wc_get_product($post->ID);
  258.         $price = $this->get_product_price($product);
  259.         $saleprice = $this->get_sale_product_price($product);
  260.    
  261.         $title = "*".$product->post->post_title."*";
  262.  
  263.         $description = strip_tags($product->post->post_excerpt);
  264.  
  265.         $description = ($this->get_setting("limit") > 0) ? substr($description, 0, $this->get_setting("limit")) : $description;
  266.  
  267.         $btn_class = "wa_btn_" . $this->get_setting("size");
  268.  
  269.         $alignClass = "text" . $this->get_setting("alignment");
  270.  
  271.         $buttonText = ($this->get_setting("btntext") != "") ? $this->get_setting("btntext") : "";
  272.  
  273.         $msg = str_replace(array("[product-title]", "[product-price]", "[product-description]","[product-saleprice]"), array($title, $price, $description, $saleprice), $this->get_setting("product_share_message"));
  274.  
  275.         $msg = $this->convert_html_for_mobile($msg);
  276.  
  277.  
  278.  
  279.         $sharebutton = "";
  280.  
  281.         $sharebutton.= '<div class="whatsapp_wrapper ' . $alignClass . '">';
  282.  
  283.         if ($this->get_setting("enable_sharing") == 1) {
  284.  
  285.             $sharebutton.= '<div class="whatsapp-button">';
  286.  
  287.             $sharebutton.='<a href="whatsapp://send" data-text="' . $msg . '" data-href="' . get_permalink($post->ID) . '" class="wa_btn ' . $btn_class . '" style="display:none;">' . $buttonText . '</a>';
  288.  
  289.             $sharebutton.='</div>';
  290.  
  291.         }      
  292.  
  293.         $sharebutton.='<input type="hidden" id="wpw_only_mobile" value="'.$this->get_setting("only_mobile").'"/>';
  294.  
  295.         $sharebutton.='<input type="hidden" id="wpw_iconcolor" value="'.$this->get_setting("iconcolor").'"/>';
  296.  
  297.         $sharebutton.='<input type="hidden" id="wpw_transparent" value="'.$this->get_setting("transparent").'"/>';
  298.  
  299.         $sharebutton.='</div>';
  300.  
  301.         echo $sharebutton;
  302.  
  303.     }
  304.  
  305.    
  306.  
  307.    
  308.  
  309.     public function convert_html_for_mobile($content)
  310.  
  311.     {
  312.  
  313.         $content = nl2br($content);
  314.  
  315.         $content = str_replace(array('<br>', '<br />', '<br/>'), array('%0a', '%0a', '%0a'), $content);
  316.  
  317.         $content = str_replace('%0a', '', $content);
  318.  
  319.         $content = strip_tags($content);
  320.  
  321.         return $content;
  322.  
  323.     }
  324.  
  325.  
  326.  
  327.    
  328.  
  329.     /* get product price */
  330.  
  331.     public function get_product_price($product)
  332.     {
  333.  
  334.         global $woocommerce;
  335.  
  336.         //return get_woocommerce_currency_symbol() . $product->get_price();
  337.  
  338.         if($product->is_on_sale()){
  339.             if ($product->is_type( 'simple' )) {
  340.                 $regular_price  =  $product->get_regular_price();
  341.             }
  342.             elseif($product->is_type('variable')){
  343.                 $regular_price  =  $product->get_variation_regular_price( 'max', true );
  344.             }
  345.             return "~".get_woocommerce_currency_symbol()." ".$regular_price."~";
  346.  
  347.         }else if($product->get_regular_price()!=0){
  348.             if ($product->is_type( 'simple' )) {
  349.                 $regular_price  =  $product->get_regular_price();
  350.             }
  351.             elseif($product->is_type('variable')){
  352.                 $regular_price  =  $product->get_variation_regular_price( 'max', true );
  353.             }
  354.             return "*".get_woocommerce_currency_symbol()." ".$regular_price."*";
  355.         }else{
  356.             return "*".get_woocommerce_currency_symbol()." ".$product->get_price()."*";
  357.         }
  358.     }
  359.  
  360.     public function get_sale_product_price($product){
  361.         global $woocommerce;
  362.         //return get_woocommerce_currency_symbol() . $product->get_price();
  363.         if($product->is_on_sale()){
  364.              if ($product->is_type( 'simple' )) {
  365.                 $sale_price     =  $product->get_sale_price();
  366.             }
  367.             elseif($product->is_type('variable')){
  368.                 $sale_price     =  $product->get_variation_sale_price( 'min', true );
  369.             }
  370.             return "*".get_woocommerce_currency_symbol()." ".$sale_price."*";
  371.         }else{
  372.             return $product->get_sale_price();
  373.         }
  374.  
  375.     }
  376.  
  377.     // public function get_var_product_price($product){
  378.  
  379.     //     global $woocommerce;
  380.  
  381.     //     $product_variations = $product->get_available_variations();
  382.     //     $variation_product_id = $product_variations [0]['variation_id'];
  383.     //     $variation_product = new WC_Product_Variation( $variation_product_id );
  384.     //     //return get_woocommerce_currency_symbol() . $product->get_price();
  385.     //     if($product->is_on_sale()){
  386.     //         return "*".get_woocommerce_currency_symbol()." ".$variation_product->regular_price."*";
  387.     //     }else{
  388.     //         return $product->get_sale_price();
  389.     //     }
  390.     // }
  391.  
  392.  
  393.  
  394.     /* display whatsapp sharing button for wordpress posts and pages */
  395.  
  396.     public function display_whatsapp_button_postpage($content)
  397.  
  398.     {
  399.  
  400.         if (!is_single() && !is_page()) {
  401.  
  402.             return $content;
  403.  
  404.         }
  405.  
  406.         global $post;
  407.  
  408.         if ($post->post_type == "product") {
  409.  
  410.             return $content;
  411.  
  412.         }
  413.  
  414.         if ($this->get_setting("enable_in_postpage") == 2 && !is_single()) {
  415.  
  416.             return $content;
  417.  
  418.         }
  419.  
  420.         if ($this->get_setting("enable_in_postpage") == 3 && !is_page()) {
  421.  
  422.             return $content;
  423.  
  424.         }
  425.  
  426.         $title = $post->post_title;
  427.  
  428.         $description = $post->post_excerpt;
  429.  
  430.         $description = ($this->get_setting("limit") > 0) ? substr($description, 0, $this->get_setting("limit")) : $description;
  431.  
  432.         $alignClass = "text" . $this->get_setting("alignment");
  433.  
  434.         $btn_class =  "wa_btn_" . $this->get_setting("size");
  435.  
  436.         $buttonText = ($this->get_setting("btntext") != "") ? $this->get_setting("btntext") : "Share";
  437.  
  438.         $msg = str_replace(array("[title]", "[description]"), array($title, $description), $this->get_setting("postpage_share_message"));
  439.  
  440.         $msg = $this->convert_html_for_mobile($msg);
  441.  
  442.         $sharebutton = '<div class="whatsapp_wrapper ' . $alignClass . '">';
  443.  
  444.         $sharebutton.='<a href="whatsapp://send" data-text="' . $msg . '" data-href="' . get_permalink($post->ID) . '" class="wa_btn ' . $btn_class . '" style="display:none;">' . $buttonText . '</a>';
  445.  
  446.         return $content;
  447.  
  448.     }
  449.  
  450.    
  451.  
  452.     /* load whatsapp sharebutton hooks  */
  453.  
  454.     public function load_share_button()
  455.  
  456.     {
  457.  
  458.         /* hook for display button on product page */
  459.  
  460.  
  461.  
  462.         if ($this->get_setting("button_pos") == 1) {
  463.  
  464.             add_action('woocommerce_single_product_summary', array(&$this, 'display_whatsapp_button'), 30);
  465.  
  466.         } elseif ($this->get_setting("button_pos") == 2) {
  467.  
  468.             add_action('woocommerce_single_product_summary', array(&$this, 'display_whatsapp_button'), 15);
  469.  
  470.         }
  471.  
  472.         elseif ($this->get_setting("button_pos") == 3) {
  473.  
  474.             add_action('woocommerce_share', array(&$this, 'display_whatsapp_button'), 6);
  475.  
  476.         }       else {
  477.  
  478.             add_action('woocommerce_single_product_summary', array(&$this, 'display_whatsapp_button'), 8);
  479.  
  480.         }
  481.  
  482.  
  483.  
  484.         /* hook for display button on posts and pages */
  485.  
  486.         if ($this->get_setting("enable_in_postpage") != 0 && $this->get_setting("enable_in_postpage") != "") {
  487.  
  488.             add_filter('the_content', array(&$this, 'display_whatsapp_button_postpage'));
  489.  
  490.         }
  491.  
  492.     }
  493.  
  494. }
  495.  
  496. new wpw_woocommerce();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement