Plugin Directory

Changeset 2743271


Ignore:
Timestamp:
06/16/2022 07:29:09 AM (4 years ago)
Author:
codup
Message:

V 1.1.2.1

Location:
codup-woocommerce-referral-system/trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • codup-woocommerce-referral-system/trunk/codup-woocommerce-referral-system.php

    r2700997 r2743271  
    33Plugin Name: Codup WooCommerce Referral System
    44Description: Build up your own referral system
    5 Version:     1.1.1.12
     5Version:     1.1.2.1
    66Author:      codup.io
    77Author URI:  http://codup.io/
     
    1010Text Domain: codup_woocommerce_referral_system
    1111Domain Path: /languages
    12 Tested up to: 5.9.2
     12
    1313*/
    1414
     
    2323 */
    2424if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){
    25  
     25  
    2626    define('CWRF_SLUG', 'cwrf');
    2727    define('CWRF_PLUGIN_NAME','codup-woocommerce-referral-system');
    28     define('CWRF_PLUGIN_VER','1.1.1.12');
     28    define('CWRF_PLUGIN_VER','1.1.2.0');
    2929    define('CWRF_PLUGIN_BASENAME', plugin_basename(__FILE__));
    3030    define('CWRF_PLUGIN_DIR', __DIR__);
     
    5050else{
    5151    global $pagenow;
    52     
     52   
    5353    // Throw an error in the wordpress admin console
    5454    add_action('admin_notices', 'cwrf_plugin_notice');       
     
    5757function cwrf_plugin_notice(){
    5858    $error_message = __('Codup WooCommerce Referral System requires <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> to be active!', 'woocommerce');
    59     echo '<div class="notice notice-warning"><p>'.$error_message.'</p></div>';
     59    echo esc_html('<div class="notice notice-warning"><p>'.$error_message.'</p></div>');
    6060
    6161}
     
    7373            update_option(CWRF_SLUG.'_referral_page_id', $page_id);
    7474        }
    75        
    76 
    7775    }   
    7876}
  • codup-woocommerce-referral-system/trunk/includes/class.codup-wcrf-email.php

    r2700997 r2743271  
    11<?php
    22
    3 if ( ! defined( 'ABSPATH' ) ) {
    4     exit; // Exit if accessed directly
    5 }
     3if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    64
    75if ( ! class_exists( 'CodupWC_ReferralSystem_Email' ) ) :
    86
     7/**
     8 * A custom WooCommerce Email class
     9 *
     10 * @since 0.1
     11 * @extends \WC_Email
     12 */
     13class CodupWC_ReferralSystem_Email extends WC_Email{   
     14
    915    /**
    10      * A custom WooCommerce Email class
    11      *
    12      * @since 0.1
    13      * @extends \WC_Email
     16     * Constructor
    1417     */
    15     class CodupWC_ReferralSystem_Email extends WC_Email {
     18    function __construct() {
    1619
    17         /**
    18          * Constructor
    19          */
    20         function __construct() {
     20        $this->id                   = 'wcrf_referral_email';
     21        $this->title                = __( 'Codup Referral Emails', 'codup_woocommerce_referral_system' );
     22        $this->description          = __( 'Referral emails are sent when a customer is rewarded by referral system', 'codup_woocommerce_referral_system' );
    2123
    22             $this->id          = 'wcrf_referral_email';
    23             $this->title       = __( 'Codup Referral Emails', 'codup_woocommerce_referral_system' );
    24             $this->description = __( 'Referral emails are sent when a customer is rewarded by referral system', 'codup_woocommerce_referral_system' );
     24        $this->template_html        = 'emails/wcrf-email.php';
     25        $this->template_plain       = 'emails/plain/wcrf-email-plain.php';
    2526
    26             $this->template_html  = 'emails/wcrf-email.php';
    27             $this->template_plain = 'emails/plain/wcrf-email-plain.php';
     27        $this->subject              = __( 'Referrer Reward!', 'codup_woocommerce_referral_system');
     28        $this->heading              = __( 'Congratulations! You have been awarded for being a referrer', 'codup_woocommerce_referral_system');     
    2829
    29             $this->subject = __( 'Referrer Reward!', 'codup_woocommerce_referral_system' );
    30             $this->heading = __( 'Congratulations! You have been awarded for being a referrer', 'codup_woocommerce_referral_system' );
    31 
    32             add_action( 'codup_wcrf_send_email', array( $this, 'trigger' ), 10, 4 );
    33             // Call parent constructor
    34             parent::__construct();
    35         }
    36 
    37         /**
    38          * trigger function.
    39          *
    40          * @access public
    41          * @return void
    42          */
    43         function trigger( $order, $referrer, $new, $old ) {
    44             if ( $order ) {
    45                 $this->object        = $order;
    46                 $this->recipient     = $referrer->user_email;
    47                 $this->referrer_name = $referrer->display_name;
    48                 $this->statusnew     = $new;
    49                 $this->statusold     = $old;
    50 
    51             }
    52 
    53             if ( ! $this->get_recipient() ) {
    54                 return;
    55             }
    56 
    57             $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    58         }
    59 
    60         /**
    61          * get_content_html function.
    62          *
    63          * @access public
    64          * @return string
    65          */
    66         function get_content_html() {
    67             ob_start();
    68             wc_get_template(
    69                 $this->template_html,
    70                 array(
    71                     'order'         => $this->object,
    72                     'new'           => $this->statusnew,
    73                     'old'           => $this->statusold,
    74                     'email_heading' => $this->get_heading(),
    75                     'referrer_name' => $this->referrer_name,
    76                     'coupon'        => get_option( CWRF_SLUG . '_coupon_list', true ),
    77                     'sent_to_admin' => false,
    78                     'plain_text'    => false,
    79                 ),
    80                 '',
    81                 trailingslashit( dirname( __FILE__ ) . DS . 'templates' )
    82             );
    83             return ob_get_clean();
    84         }
    85 
    86         /**
    87          * get_content_plain function.
    88          *
    89          * @access public
    90          * @return string
    91          */
    92         function get_content_plain() {
    93             ob_start();
    94             wc_get_template(
    95                 $this->template_plain,
    96                 array(
    97                     'order'         => $this->object,
    98                     'email_heading' => $this->get_heading(),
    99                     'referrer_name' => $this->referrer_name,
    100                     'coupon'        => get_option( CWRF_SLUG . '_coupon_list', true ),
    101                     'sent_to_admin' => false,
    102                     'plain_text'    => true,
    103                 ),
    104                 '',
    105                 trailingslashit( dirname( __FILE__ ) . DS . 'templates' )
    106             );
    107             return ob_get_clean();
    108         }
    109 
    110         /**
    111          * Initialise Settings Form Fields
    112          *
    113          * @access public
    114          * @return void
    115          */
    116         function init_form_fields() {
    117             $this->form_fields = array(
    118                 'enabled'    => array(
    119                     'title'   => 'Enable/Disable',
    120                     'type'    => 'checkbox',
    121                     'label'   => 'Enable this email notification',
    122                     'default' => 'yes',
    123                 ),
    124                 'subject'    => array(
    125                     'title'       => __( 'Email subject', 'woocommerce' ),
    126                     'type'        => 'text',
    127                     'description' => sprintf( __( 'Subject of the email defaults to <code>%s</code>', 'woocommerce' ), $this->subject ),
    128                     'placeholder' => $this->subject,
    129                     'default'     => '',
    130                 ),
    131                 'heading'    => array(
    132                     'title'       => __( 'Email heading', 'woocommerce' ),
    133                     'type'        => 'text',
    134                     'description' => sprintf( __( 'Heading of the email defaults to <code>%s</code>', 'woocommerce' ), $this->heading ),
    135                     'placeholder' => $this->heading,
    136                     'default'     => '',
    137                 ),
    138                 'email_type' => array(
    139                     'title'       => __( 'Email type', 'woocommerce' ),
    140                     'type'        => 'select',
    141                     'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
    142                     'default'     => 'html',
    143                     'class'       => 'email_type',
    144                     'options'     => array(
    145                         'plain' => __( 'Plain text', 'woocommerce' ),
    146                         'html'  => __( 'HTML', 'woocommerce' ),
    147                     ),
    148                 ),
    149             );
    150         }
     30        add_action('codup_wcrf_send_email', array($this, 'trigger'), 10, 4 );
     31        // Call parent constructor
     32        parent::__construct(); 
    15133    }
    15234
     35    /**
     36     * trigger function.
     37     *
     38     * @access public
     39     * @return void
     40     */
     41    function trigger( $order, $referrer, $new, $old ){     
     42        if ( $order ) {
     43            $this->object           = $order;
     44            $this->recipient        = $referrer->user_email;
     45            $this->referrer_name    = $referrer->display_name;
     46            $this->statusnew        = $new;
     47            $this->statusold        = $old;
     48
     49        }       
     50       
     51        if ( ! $this->get_recipient() )
     52            return;
     53
     54        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
     55    }
     56
     57    /**
     58     * get_content_html function.
     59     *
     60     * @access public
     61     * @return string
     62     */
     63    function get_content_html(){
     64        ob_start();
     65        wc_get_template( $this->template_html, array(
     66            'order'         => $this->object,
     67            'new'               => $this->statusnew,
     68            'old'               => $this->statusold,
     69            'email_heading' => $this->get_heading(),
     70            'referrer_name' => $this->referrer_name,           
     71            'coupon'        => get_option(CWRF_SLUG.'_coupon_list', true),
     72            'sent_to_admin' => false,
     73            'plain_text'    => false
     74        ),'', trailingslashit(dirname(__FILE__).DS. 'templates'));
     75        return ob_get_clean();
     76    }
     77
     78    /**
     79     * get_content_plain function.
     80     *
     81     * @access public
     82     * @return string
     83     */
     84    function get_content_plain(){
     85        ob_start();
     86        wc_get_template( $this->template_plain, array(
     87            'order'         => $this->object,
     88            'email_heading' => $this->get_heading(),
     89            'referrer_name' => $this->referrer_name,           
     90            'coupon'        => get_option(CWRF_SLUG.'_coupon_list', true),
     91            'sent_to_admin' => false,
     92            'plain_text'    => true
     93        ),'', trailingslashit(dirname(__FILE__).DS. 'templates'));
     94        return ob_get_clean();
     95    }
     96
     97    /**
     98     * Initialise Settings Form Fields
     99     *
     100     * @access public
     101     * @return void
     102     */
     103    function init_form_fields(){
     104        $this->form_fields = array(
     105            'enabled' => array(
     106                'title' => 'Enable/Disable',
     107                'type' => 'checkbox',
     108                'label' => 'Enable this email notification',
     109                'default' => 'yes'
     110            ),
     111            'subject' => array(
     112                'title'         => __( 'Email subject', 'woocommerce' ),
     113                'type'          => 'text',
     114                'description'   => sprintf( __( 'Subject of the email defaults to <code>%s</code>', 'woocommerce' ), $this->subject ),
     115                'placeholder'   => $this->subject ,
     116                'default'       => ''
     117            ),
     118            'heading' => array(
     119                'title'         => __( 'Email heading', 'woocommerce' ),
     120                'type'          => 'text',
     121                'description'   => sprintf( __( 'Heading of the email defaults to <code>%s</code>', 'woocommerce' ), $this->heading ),
     122                'placeholder'   => $this->heading ,
     123                'default'       => ''
     124            ),
     125            'email_type' => array(
     126                'title'         => __( 'Email type', 'woocommerce' ),
     127                'type'          => 'select',
     128                'description'   => __( 'Choose which format of email to send.', 'woocommerce' ),
     129                'default'       => 'html',
     130                'class'         => 'email_type',
     131                'options'       => array(
     132                    'plain'         => __( 'Plain text', 'woocommerce' ),
     133                    'html'          => __( 'HTML', 'woocommerce' ),
     134                )
     135            )
     136        );
     137    }
     138}
     139
    153140endif;
  • codup-woocommerce-referral-system/trunk/includes/class.codup-wcrf.php

    r2700997 r2743271  
    11<?php
    22
    3 class CodupWCReferralSystem {
    4 
    5     const OPT_REDIRECT_LINK   = CWRF_SLUG . '_redirect_link';
    6     const OPT_REFERRAL_SLUG   = CWRF_SLUG . '_referral_slug';
    7     const OPT_REFERRAL_EXPIRY = CWRF_SLUG . '_referral_expiry';
    8     const OPT_REFEREE_AWARD   = CWRF_SLUG . '_referee_award';
    9     const OPT_COUPON_LIST     = CWRF_SLUG . '_coupon_list';
    10 
    11     private static $optDefaults = array(
    12         self::OPT_REDIRECT_LINK   => '',
    13         self::OPT_REFERRAL_SLUG   => 'cwrf_ref',
    14         self::OPT_REFERRAL_EXPIRY => 1,
    15         self::OPT_REFEREE_AWARD   => 1,
    16 
    17     );
    18 
    19     function __construct() {
    20 
    21         add_filter( 'plugin_action_links_' . CWRF_PLUGIN_BASENAME, array( $this, 'addActionLinks' ) );
    22 
    23         /** Add settings tabs to woocommerce setting tabs */
    24         add_filter( 'woocommerce_settings_tabs_array', array( $this, 'addSettingsTab' ), 50 );
    25         add_action( 'woocommerce_settings_tabs_' . CWRF_SLUG, array( $this, 'addSettingsTabContents' ) );
    26         add_action( 'woocommerce_update_options_' . CWRF_SLUG, array( $this, 'saveSettings' ) );
    27         add_action( 'init', array( $this, 'addSettingDefaults' ) );
    28 
    29         /** Add custom link and page to woocommerce account menu for referral system */
    30         add_filter( 'woocommerce_account_menu_items', array( $this, 'add_referral_link' ) );
    31         add_filter( 'woocommerce_get_endpoint_url', array( $this, 'hook_referral_endpoint' ), 10, 4 );
    32         add_shortcode( 'cwrf-referral-system', array( $this, 'add_referral_system_page' ) );
    33         add_action( 'template_redirect', array( $this, 'save_referral_parameter' ) );
    34 
    35         /** Award referrer and referee */
    36         add_filter( 'woocommerce_order_status_changed', array( $this, 'reward_referrer' ), 10, 3 );
    37         add_action( 'woocommerce_applied_coupon', array( $this, 'apply_coupon_validation' ) );
    38         add_action( 'woocommerce_before_cart', array( $this, 'apply_coupon_for_referral' ) );
    39         add_action( 'woocommerce_removed_coupon', array( $this, 'remove_reward_coupon' ) );
    40 
    41         /** Add custom email to woocommerce emails */
    42         add_filter( 'woocommerce_email_classes', array( $this, 'add_custom_wcrf_email' ) );
    43         add_filter( 'woocommerce_locate_core_template', array( $this, 'locate_core_template' ), 10, 3 );
    44 
    45     }
    46 
    47     /**
    48      * Inserts default values into WordPress options.
    49      */
    50     function addSettingDefaults() {
    51         foreach ( self::$optDefaults as $key => $defaultValue ) {
    52             if ( ! get_option( $key ) ) {
    53                 add_option( $key, $defaultValue );
    54             }
    55         }
    56     }
    57 
    58     /**
    59      * Adds the CWL tab to WooCommerce settings page.
    60      *
    61      * @param string[] $tabs
    62      * @return string[]
    63      */
    64     function addSettingsTab( $tabs ) {
    65         $tabs[ CWRF_SLUG ] = __( 'Referral Sytem', CWRF_SLUG );
    66         return $tabs;
    67     }
    68 
    69     /**
    70      * Adds the content of the CWL settings tab in WC settings page.
    71      */
    72     function addSettingsTabContents() {
    73         do_shortcode( '[codup_ads_top]' );
    74         woocommerce_admin_fields( $this->generateSettingsFields() );
    75     }
    76 
    77 
    78     /**
    79      * Saves the settings entered into the form.
    80      */
    81     function saveSettings() {
    82         woocommerce_update_options( $this->generateSettingsFields() );
    83     }
    84 
    85     /**
    86      * Generates the WC settings fields configuration for this plugin tab.
    87      *
    88      * @return mixed[]
    89      */
    90     private function generateSettingsFields() {
    91 
    92         $settings = array(
    93             'general_title'       => array(
    94                 'title' => __( 'Referral System Settings', CWRF_SLUG ),
    95                 'type'  => 'title',
    96                 'id'    => CWRF_SLUG . '_general_title',
    97             ),
    98             'table_content'       => array(
    99                 'type' => 'title',
    100                 'desc' => __( '<hr><h4>Copy this short code <span style="font-weight:800;">[cwrf-refferal-system]</span> and paste it into your page to generate referral link.<h4/>', CWRF_SLUG ),
    101                 'id'   => 'short_code_label',
    102             ),
    103             'redirect_link'       => array(
    104                 'title'    => __( 'Redirect Page', CWRF_SLUG ),
    105                 'type'     => 'select',
    106                 'options'  => $this->get_pages(),
    107                 'desc_tip' => __( 'Select the page where you want the user with referral link to redirect.', CWRF_SLUG ),
    108                 'id'       => self::OPT_REDIRECT_LINK,
    109                 'class'    => 'cwrf-redirect-link',
    110             ),
    111             'referral_slug'       => array(
    112                 'title'    => __( 'Referral URL Variable', CWRF_SLUG ),
    113                 'type'     => 'text',
    114                 'desc'     => '',
    115                 'desc_tip' => __( 'Create your custom slug, a part of referral url identifying the referrer by ID', CWRF_SLUG ),
    116                 'id'       => self::OPT_REFERRAL_SLUG,
    117                 'class'    => 'cwrf-referral-slug',
    118             ),
    119             'referral_expiry'     => array(
    120                 'title'    => __( 'Referral Expiry', CWRF_SLUG ),
    121                 'type'     => 'number',
    122                 'desc'     => __( 'days', CWRF_SLUG ),
    123                 'desc_tip' => __( 'Set referral link expiry in days.', CWRF_SLUG ),
    124                 'id'       => self::OPT_REFERRAL_EXPIRY,
    125                 'class'    => 'cwrf-referral-expiry',
    126             ),
    127             'referee_award'       => array(
    128                 'title' => __( 'Award Referee?', CWRF_SLUG ),
    129                 'type'  => 'checkbox',
    130                 'desc'  => __( '<span class="description">Select whether to reward the referee along with referrer</span>', CWRF_SLUG ),
    131                 'id'    => self::OPT_REFEREE_AWARD,
    132                 'class' => 'cwrf-referee-award',
    133             ),
    134             'coupons_list'        => array(
    135                 'title'   => __( 'Select Coupon to Reward', CWRF_SLUG ),
    136                 'type'    => 'select',
    137                 'options' => $this->get_coupons(),
    138                 'desc'    => '',
    139                 'id'      => self::OPT_COUPON_LIST,
    140                 'class'   => 'cwrf-coupun-list',
    141             ),
    142             'general_section_end' => array(
    143                 'type' => 'sectionend',
    144                 'id'   => CWRF_SLUG . '_general_title',
    145             ),
    146         );
    147 
    148         return apply_filters( CWRF_SLUG . '_settings', $settings );
    149     }
    150 
    151     function get_pages() {
    152         $pages  = array();
    153         $args   = array(
    154             'post_status' => 'publish',
    155         );
    156         $result = get_pages( $args, ARRAY_A, 'page' );
    157         foreach ( $result as $page ) {
    158             $pages[ $page->post_title ] = $page->post_title;
    159         }
    160 
    161         return $pages;
    162     }
    163 
    164     function get_coupons() {
    165         $args = array(
    166             'posts_per_page' => -1,
    167             'orderby'        => 'title',
    168             'order'          => 'asc',
    169             'post_type'      => 'shop_coupon',
    170             'post_status'    => 'publish',
    171         );
    172 
    173         $posts   = get_posts( $args );
    174         $coupons = array();
    175         foreach ( $posts as $key => $post ) {
    176             $coupons[ $post->post_title ] = $post->post_title;
    177         }
    178 
    179         return $coupons;
    180     }
    181 
    182     function add_referral_link( $menu_links ) {
    183 
    184         $new        = array( 'referrallink' => 'Referral System' );
    185         $menu_links = array_slice( $menu_links, 0, 1, true )
    186         + $new
    187         + array_slice( $menu_links, 1, null, true );
    188         return $menu_links;
    189     }
    190 
    191     function hook_referral_endpoint( $url, $endpoint, $value, $permalink ) {
    192 
    193         if ( $endpoint === 'referrallink' ) {
    194             $pages = get_pages( array( 'sort_column' => 'ID' ) );
    195 
    196             foreach ( $pages as $page ) {
    197                 if ( preg_match( '[cwrf-referral-system]', $page->post_content ) ) {
    198 
    199                     $url = site_url() . '/' . $page->post_name . '/';
    200                     break;
    201                 }
    202             }
    203         }
    204         return $url;
    205     }
    206 
    207     function add_referral_system_page() {
    208 
    209         if ( ! is_admin() ) {
    210             $referral_slug = get_option( CWRF_SLUG . '_referral_slug' );
    211             ob_start();
    212             include dirname( __FILE__ ) . '/templates' . '/referral_system.php';
    213             $referral_system_page_content = ob_get_contents();
    214             ob_get_clean();
    215             return $referral_system_page_content;
    216         }
    217     }
    218 
    219     function save_referral_parameter() {
    220         global $wp;
    221 
    222         $referral_slug = get_option( CWRF_SLUG . '_referral_slug' );
    223 
    224         if ( isset( $_GET[ $referral_slug ] ) ) {
    225             $referrer      = $_GET[ $referral_slug ];
    226             $redirect_page = get_option( CWRF_SLUG . '_redirect_link', true );
    227 
    228             if ( $referrer != get_current_user_id() && get_userdata( $referrer ) && ! $_COOKIE['STYXKEY_cwrref'] ) {
    229                 $referral_expiry = get_option( CWRF_SLUG . '_referral_expiry', true );
    230                 setcookie( 'STYXKEY_cwrref', $referrer, time() + ( 24 * 3600 * $referral_expiry ), '/' );
    231 
    232             }
    233 
    234             $page = get_page_by_title( $redirect_page, ARRAY_A, 'page' );
    235             wp_redirect( $page['guid'] );
    236             exit;
    237 
    238         }
    239     }
    240 
    241     function reward_referrer( $order, $old_status, $new_status ) {
    242         global $wpdb;
    243 
    244         if ( is_numeric( $order ) ) {
    245             $order = wc_get_order( $order );
    246         }
    247 
    248         $table = $wpdb->prefix . CWRF_SLUG . '_coupon_rewards';
    249 
    250         if ( $_COOKIE['STYXKEY_cwrref'] && $new_status == 'processing' ) {
    251 
    252             $referrer_id = $_COOKIE['STYXKEY_cwrref'];
    253             $coupon_code = get_option( CWRF_SLUG . '_coupon_list', true );
    254             $query       = 'SELECT * FROM  ' . $table . " WHERE user_id = '" . $referrer_id . "' AND coupon_code = '" . $coupon_code . "'";
    255             $result      = $wpdb->get_results( $query, ARRAY_A );
    256 
    257             if ( sizeof( $result ) == 0 ) {
    258                 $data = array(
    259                     'user_id'      => $referrer_id,
    260                     'coupon_code'  => $coupon_code,
    261                     'coupon_count' => 1,
    262 
    263                 );
    264 
    265                 $wpdb->insert( $table, $data );
    266             } else {
    267 
    268                 $data = array(
    269                     'user_id'      => $referrer_id,
    270                     'coupon_code'  => $coupon_code,
    271                     'coupon_count' => $result[0]['coupon_count'] + 1,
    272 
    273                 );
    274                 $where = array(
    275                     'id' => $result[0]['id'],
    276                 );
    277 
    278                 $wpdb->update( $table, $data, $where );
    279 
    280             }
    281             $user = get_userdata( $referrer_id );
    282 
    283             setcookie( 'STYXKEY_cwrref', '', time() - 3600, '/' );
    284             setcookie( 'STYXKEY_remove_coupon', '', time() - 3600, '/' );
    285             do_action( 'codup_wcrf_send_email', $order, $user, $new_status, $old_status );
    286 
    287         }
    288 
    289         $current_user_id = get_current_user_id();
    290         $coupons         = $order->get_used_coupons();
    291         $query           = 'SELECT id, coupon_code, coupon_count FROM  ' . $table . " WHERE user_id = '" . $current_user_id . "'";
    292         $users_coupons   = $wpdb->get_results( $query, ARRAY_A );
    293 
    294         foreach ( $users_coupons as $coupon ) {
    295             if ( in_array( $coupon['coupon_code'], $coupons ) ) {
    296                 $data = array(
    297                     'user_id'      => $current_user_id,
    298                     'coupon_code'  => $coupon['coupon_code'],
    299                     'coupon_count' => $coupon['coupon_count'] - 1,
    300 
    301                 );
    302                 $where = array(
    303                     'id' => $coupon['id'],
    304                 );
    305 
    306                 $wpdb->update( $table, $data, $where );
    307             }
    308         }
    309     }
    310 
    311     function apply_coupon_validation() {
    312         global $woocommerce;
    313 
    314         $awardReferee    = get_option( CWRF_SLUG . '_referee_award', true );
    315         $coupon_code     = get_option( CWRF_SLUG . '_coupon_list', true );
    316         $applied_coupons = $woocommerce->cart->get_applied_coupons();
    317         $coupons         = $this->get_reward_coupons();
    318 
    319         foreach ( $coupons as $coupon ) {
    320 
    321             if ( $coupon['coupon_code'] == $coupon_code && $_COOKIE['STYXKEY_cwrref'] && $awardReferee == 'yes' ) {
    322                 break;
    323             }
    324 
    325             if ( in_array( $coupon['coupon_code'], $applied_coupons ) ) {
    326                 $coupon_data = new WC_Coupon( $coupon['coupon_code'] );
    327                 if ( $coupon_data->is_valid() ) {
    328                     if ( $coupon['coupon_count'] <= 0 ) {
    329                         $woocommerce->cart->remove_coupon( sanitize_text_field( $coupon['coupon_code'] ) );
    330                         $woocommerce->cart->calculate_totals();
    331 
    332                         wc_clear_notices();
    333                         wc_print_notice( 'You have reached the limit to use this discount!', 'error' );
    334 
    335                     }
    336                 }
    337             }
    338         }
    339     }
    340 
    341     function apply_coupon_for_referral() {
    342         global $woocommerce;
    343 
    344         $awardReferee = get_option( CWRF_SLUG . '_referee_award', true );
    345         $coupon_code  = get_option( CWRF_SLUG . '_coupon_list', true );
    346 
    347         if ( $_COOKIE['STYXKEY_cwrref'] && $awardReferee == 'yes' ) {
    348 
    349             wc_clear_notices();
    350             $isCouponAlreadyApplied = in_array( $coupon_code, $woocommerce->cart->get_applied_coupons() );
    351 
    352             global $woocommerce;
    353             $coupon_data     = new WC_Coupon( $coupon_code );
    354             $checkUsageLimit = $coupon_data->usage_limit - $coupon_data->usage_count; // check if Usage Limit Exceeeds
    355 
    356             if ( ! $isCouponAlreadyApplied && $_COOKIE['STYXKEY_remove_coupon'] != 'yes' && $checkUsageLimit != 0 ) {
    357                 $woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ) );
    358                 wc_clear_notices();
    359                 wc_print_notice( 'A discount coupon has been applied for being a referee!', 'success' );
    360             }
    361 
    362             if ( ! $isCouponAlreadyApplied && $_COOKIE['STYXKEY_remove_coupon'] == 'yes' ) {
    363                 wc_print_notice( 'Get your referee discount by using coupon <strong>' . $coupon_code . '</strong>', 'notice' );
    364             }
    365         }
    366     }
    367 
    368     function remove_reward_coupon( $coupon_code ) {
    369 
    370         $reward_coupon_code = get_option( CWRF_SLUG . '_coupon_list', true );
    371 
    372         if ( $reward_coupon_code == $coupon_code && ! $_COOKIE['STYXKEY_remove_coupon'] ) {
    373 
    374             $referral_expiry = get_option( CWRF_SLUG . '_referral_expiry', true );
    375 
    376             setcookie( 'STYXKEY_remove_coupon', 'yes', time() + ( 24 * 3600 * $referral_expiry ), '/' );
    377         }
    378     }
    379 
    380     function get_reward_coupons( $user_id = '' ) {
    381         if ( ! is_user_logged_in() ) {
    382             return array();
    383         }
    384 
    385         global $wpdb;
    386 
    387         if ( $user_id == '' ) {
    388             $user_id = get_current_user_id();
    389         }
    390 
    391         $table   = $wpdb->prefix . CWRF_SLUG . '_coupon_rewards';
    392         $query   = 'SELECT * FROM  ' . $table . " WHERE user_id = '" . $user_id . "'";
    393         $coupons = $wpdb->get_results( $query, ARRAY_A );
    394 
    395         return $coupons;
    396     }
    397 
    398     /**
    399      *  Add a custom email to the list of emails WooCommerce should load on settings page
    400      *
    401      * @since 0.1
    402      * @param array $email_classes available email classes
    403      * @return array filtered available email classes
    404      */
    405     function add_custom_wcrf_email( $email_classes ) {
    406         // include our custom email class
    407         require_once dirname( __FILE__ ) . DS . 'class.codup-wcrf-email.php';
    408 
    409         // add the email class to the list of email classes that WooCommerce loads
    410         $email_classes['CodupWC_ReferralSystem_Email'] = new CodupWC_ReferralSystem_Email();
    411 
    412         return $email_classes;
    413     }
    414 
    415     /**
    416      * Locate the plugin email templates
    417      *
    418      * @param $core_file
    419      * @param $template
    420      * @param $template_base
    421      *
    422      * @return string
    423      */
    424     function locate_core_template( $core_file, $template, $template_base ) {
    425         $custom_template = array(
    426             'emails/wcrf-email.php',
    427             'emails/plain/wcrf-email-plain.php',
    428         );
    429 
    430         if ( in_array( $template, $custom_template ) ) {
    431             $core_file = trailingslashit( dirname( __FILE__ ) . DS . 'templates' ) . $template;
    432         }
    433 
    434         return $core_file;
    435     }
    436 
    437      /**
    438       * Add links to the bottom of the plugin name in the WP plugins list
    439       *
    440       * @param array $links
    441       */
    442     function addActionLinks( $links ) {
    443         $cwlLinks = array(
    444             '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=cwrf' ) . '">Settings</a>',
    445         );
    446 
    447         return array_merge( $cwlLinks, $links );
    448     }
     3class CodupWCReferralSystem{
     4
     5    const OPT_REDIRECT_LINK = CWRF_SLUG.'_redirect_link';
     6    const OPT_REFERRAL_SLUG = CWRF_SLUG.'_referral_slug';
     7    const OPT_REFERRAL_EXPIRY = CWRF_SLUG.'_referral_expiry';
     8    const OPT_REFEREE_AWARD = CWRF_SLUG.'_referee_award';
     9    const OPT_COUPON_LIST = CWRF_SLUG.'_coupon_list';
     10
     11    private static $optDefaults = array(
     12        CodupWCReferralSystem::OPT_REDIRECT_LINK => '',
     13        CodupWCReferralSystem::OPT_REFERRAL_SLUG => 'cwrf_ref',
     14        CodupWCReferralSystem::OPT_REFERRAL_EXPIRY => 1,
     15        CodupWCReferralSystem::OPT_REFEREE_AWARD => 1
     16       
     17    );   
     18
     19    function __construct(){     
     20       
     21        add_filter('plugin_action_links_'.CWRF_PLUGIN_BASENAME, array($this, 'addActionLinks'));       
     22       
     23        /** Add settings tabs to woocommerce setting tabs **/
     24        add_filter('woocommerce_settings_tabs_array', array($this, 'addSettingsTab'), 50);
     25        add_action('woocommerce_settings_tabs_'.CWRF_SLUG, array($this, 'addSettingsTabContents'));
     26        add_action('woocommerce_update_options_'.CWRF_SLUG, array($this, 'saveSettings'));
     27        add_action('init', array($this, 'addSettingDefaults'));
     28
     29        /** Add custom link and page to woocommerce account menu for referral system **/
     30        add_filter ( 'woocommerce_account_menu_items',  array($this, 'add_referral_link') );
     31        add_filter( 'woocommerce_get_endpoint_url', array($this, 'hook_referral_endpoint'), 10, 4 );
     32        add_shortcode('cwrf-referral-system', array($this, 'add_referral_system_page'));
     33        add_action('template_redirect', array($this, 'save_referral_parameter'));
     34
     35        /** Award referrer and referee **/
     36        add_filter( 'woocommerce_order_status_changed', array($this, 'reward_referrer'), 10, 3 );
     37        add_action('woocommerce_applied_coupon', array($this, 'apply_coupon_validation'));
     38        add_action('woocommerce_before_cart', array($this, 'apply_coupon_for_referral'));
     39        add_action('woocommerce_removed_coupon', array($this, 'remove_reward_coupon'));
     40
     41        /** Add custom email to woocommerce emails */
     42        add_filter( 'woocommerce_email_classes', array($this, 'add_custom_wcrf_email') );
     43        add_filter('woocommerce_locate_core_template', array($this, 'locate_core_template'), 10, 3);
     44       
     45    }     
     46
     47    /**
     48     * Inserts default values into WordPress options.
     49    */
     50    function addSettingDefaults(){
     51        //echo "<pre>";print_r(CodupWCReferralSystem::$optDefaults);
     52        foreach(CodupWCReferralSystem::$optDefaults as $key => $defaultValue){
     53            if(!get_option($key)){
     54                add_option($key, $defaultValue);   
     55            }
     56        }
     57    }
     58   
     59    /**
     60     * Adds the CWL tab to WooCommerce settings page.
     61     * @param string[] $tabs
     62     * @return string[]
     63     */
     64    function addSettingsTab( $tabs ){
     65        $tabs[CWRF_SLUG] = __('Referral Sytem', CWRF_SLUG);
     66        return $tabs;
     67    }
     68   
     69    /**
     70     * Adds the content of the CWL settings tab in WC settings page.
     71     */
     72    function addSettingsTabContents(){
     73        do_shortcode('[codup_ads_top]');
     74        woocommerce_admin_fields( $this->generateSettingsFields() );
     75    }
     76
     77
     78    /**
     79     * Saves the settings entered into the form.
     80     */
     81    function saveSettings(){
     82        woocommerce_update_options( $this->generateSettingsFields() );
     83    }
     84
     85    /**
     86     * Generates the WC settings fields configuration for this plugin tab.
     87     * @return mixed[]
     88     */
     89    private function generateSettingsFields(){
     90       
     91        $settings = array(           
     92            'general_title' => array(
     93                'title' => __('Referral System Settings', CWRF_SLUG),
     94                'type' => 'title',
     95                'id' => CWRF_SLUG.'_general_title'
     96            ),
     97            'table_content' => array(
     98                'type' => 'title',
     99                'desc' => __('<hr><h4>Copy this short code <span style="font-weight:800;">[cwrf-refferal-system]</span> and paste it into your page to generate referral link.<h4/>', CWRF_SLUG),
     100                'id' => 'short_code_label'
     101            ),
     102            'redirect_link' => array(
     103                'title' => __('Redirect Page', CWRF_SLUG),
     104                'type' => 'select',
     105                'options' => $this->get_pages(),             
     106                'desc_tip' => __('Select the page where you want the user with referral link to redirect.', CWRF_SLUG),
     107                'id' => CodupWCReferralSystem::OPT_REDIRECT_LINK,
     108                'class' => 'cwrf-redirect-link'
     109            ),
     110            'referral_slug' => array(
     111                'title' => __('Referral URL Variable', CWRF_SLUG),
     112                'type' => 'text',
     113                'desc' => '',
     114                'desc_tip' => __('Create your custom slug, a part of referral url identifying the referrer by ID', CWRF_SLUG),
     115                'id' => CodupWCReferralSystem::OPT_REFERRAL_SLUG,
     116                'class' => 'cwrf-referral-slug'
     117            ),           
     118            'referral_expiry' => array(
     119                'title' => __('Referral Expiry', CWRF_SLUG),
     120                'type' => 'number',
     121                'desc' => __('days', CWRF_SLUG),
     122                'desc_tip' => __('Set referral link expiry in days.', CWRF_SLUG),
     123                'id' => CodupWCReferralSystem::OPT_REFERRAL_EXPIRY,
     124                'class' => 'cwrf-referral-expiry'
     125            ),
     126            'referee_award' => array(
     127                'title' => __('Award Referee?', CWRF_SLUG),
     128                'type' => 'checkbox',
     129                'desc' => __('<span class="description">Select whether to reward the referee along with referrer</span>', CWRF_SLUG),
     130                'id' => CodupWCReferralSystem::OPT_REFEREE_AWARD,
     131                'class' => 'cwrf-referee-award'
     132            ),
     133            'coupons_list' => array(
     134                'title' => __('Select Coupon to Reward', CWRF_SLUG),
     135                'type' => 'select',
     136                'options' => $this->get_coupons(),
     137                'desc' => '',
     138                'id' => CodupWCReferralSystem::OPT_COUPON_LIST,
     139                'class' => 'cwrf-coupun-list'
     140            ),
     141            'general_section_end' => array(
     142                'type' => 'sectionend',
     143                'id' => CWRF_SLUG.'_general_title'
     144            )
     145        );     
     146       
     147        return apply_filters(CWRF_SLUG.'_settings', $settings);
     148    }
     149
     150    function get_pages(){
     151        $pages = array();
     152        $args = array(
     153            'post_status' => 'publish'
     154        );
     155        $result = get_pages($args, ARRAY_A, 'page');
     156        foreach($result as $page){
     157            $pages[$page->post_title] = $page->post_title;
     158        }
     159
     160        return $pages;
     161    }
     162
     163    function get_coupons(){
     164        $args = array(
     165            'posts_per_page'   => -1,
     166            'orderby'          => 'title',
     167            'order'            => 'asc',
     168            'post_type'        => 'shop_coupon',
     169            'post_status'      => 'publish',
     170        );
     171           
     172        $posts = get_posts( $args );
     173        $coupons = array();
     174        foreach ($posts as $key => $post) {
     175            $coupons[$post->post_title] =  $post->post_title;
     176        }
     177
     178        return $coupons;
     179    }
     180
     181    function add_referral_link( $menu_links ){       
     182       
     183        $new = array( 'referrallink' => 'Referral System' );     
     184        $menu_links = array_slice( $menu_links, 0, 1, true )
     185        + $new
     186        + array_slice( $menu_links, 1, NULL, true );
     187        return $menu_links;   
     188    }     
     189
     190    function hook_referral_endpoint( $url, $endpoint, $value, $permalink ){     
     191       
     192        if( $endpoint === 'referrallink' ) {
     193            $pages = get_pages( ['sort_column' => 'ID'] );
     194
     195            foreach($pages as $page){
     196                if( preg_match( '[cwrf-referral-system]', $page->post_content  ) ){
     197
     198                    $url = site_url().'/'. $page->post_name .'/';
     199                    break;
     200                }
     201            }
     202        }
     203        return $url;     
     204    }
     205
     206    function add_referral_system_page(){
     207
     208        if( !is_admin() ){
     209            $referral_slug = get_option(CWRF_SLUG.'_referral_slug');
     210            ob_start();
     211            include(dirname(__FILE__). '/templates'.'/referral_system.php');
     212            $referral_system_page_content = ob_get_contents();
     213            ob_get_clean();
     214            return $referral_system_page_content;
     215        }
     216    }
     217
     218    function save_referral_parameter(){       
     219        global $wp;
     220       
     221        $referral_slug = get_option(CWRF_SLUG.'_referral_slug');
     222
     223        if( isset( $_GET[$referral_slug] ) ){
     224            $referrer = sanitize_text_field($_GET[$referral_slug]);           
     225            $redirect_page = get_option(CWRF_SLUG.'_redirect_link', true);
     226
     227            if($referrer != get_current_user_id() && get_userdata($referrer) && !$_COOKIE["STYXKEY_cwrref"]){                       
     228                $referral_expiry = get_option(CWRF_SLUG.'_referral_expiry', true);
     229                setcookie("STYXKEY_cwrref", $referrer, time() + (24 * 3600 * $referral_expiry), "/");
     230                       
     231            }   
     232
     233            $page = get_page_by_title( $redirect_page, ARRAY_A, 'page' );
     234            wp_redirect($page['guid']);
     235            exit;       
     236           
     237        }
     238    }
     239
     240    function reward_referrer( $order, $old_status, $new_status ){       
     241        global $wpdb;
     242
     243        if (is_numeric($order)) {
     244            $order = wc_get_order($order);
     245        }
     246       
     247        $table = $wpdb->prefix . CWRF_SLUG . "_coupon_rewards";
     248       
     249       
     250        if($_COOKIE["STYXKEY_cwrref"] && $new_status == 'processing'){
     251
     252            $referrer_id = sanitize_text_field($_COOKIE["STYXKEY_cwrref"]);           
     253            $coupon_code = get_option(CWRF_SLUG.'_coupon_list', true);
     254
     255            $query = $wpdb->prepare(
     256                    "SELECT * FROM $table WHERE user_id = %d AND coupon_code = %d",
     257                    $referrer_id,
     258                    $coupon_code,
     259                );
     260
     261            $result = $wpdb->get_results($query, ARRAY_A);
     262
     263            if(sizeof($result) == 0){
     264                $data = array(                   
     265                    'user_id' => $referrer_id,
     266                    'coupon_code' => $coupon_code,
     267                    'coupon_count' => 1
     268
     269                );
     270               
     271                $wpdb->insert( $table, $data);
     272            }
     273            else{
     274               
     275                $data = array(                   
     276                    'user_id' => $referrer_id,
     277                    'coupon_code' => $coupon_code,
     278                    'coupon_count' => $result[0]['coupon_count'] + 1
     279
     280                );
     281                $where = array(
     282                    'id' => $result[0]['id']
     283                );
     284
     285                $wpdb->update( $table, $data, $where);
     286
     287            }
     288            $user = get_userdata($referrer_id);
     289
     290            setcookie("STYXKEY_cwrref", "", time() - 3600, "/");
     291            setcookie("STYXKEY_remove_coupon", "", time() - 3600, "/");
     292            do_action('codup_wcrf_send_email', $order, $user, $new_status, $old_status);   
     293           
     294        }
     295       
     296        $current_user_id = get_current_user_id();
     297        $coupons = $order->get_used_coupons();
     298
     299        $query = $wpdb->prepare(
     300                    "SELECT id, coupon_code, coupon_count FROM $table WHERE user_id = %d",
     301                    $current_user_id,
     302                );
     303
     304        $users_coupons = $wpdb->get_results($query, ARRAY_A);
     305
     306        foreach($users_coupons as $coupon){
     307            if(in_array($coupon['coupon_code'], $coupons)){
     308                $data = array(                   
     309                    'user_id' => $current_user_id,
     310                    'coupon_code' => $coupon['coupon_code'],
     311                    'coupon_count' => $coupon['coupon_count'] - 1
     312
     313                );
     314                $where = array(
     315                    'id' => $coupon['id']
     316                );
     317
     318                $wpdb->update( $table, $data, $where);
     319            }
     320        }
     321    }
     322
     323    function apply_coupon_validation(){
     324        global $woocommerce;
     325
     326        $awardReferee = get_option(CWRF_SLUG.'_referee_award', true);
     327        $coupon_code = get_option(CWRF_SLUG.'_coupon_list', true);
     328        $applied_coupons = $woocommerce->cart->get_applied_coupons();
     329        $coupons = $this->get_reward_coupons();   
     330
     331        foreach($coupons as $coupon){
     332
     333            if($coupon['coupon_code'] == $coupon_code && $_COOKIE["STYXKEY_cwrref"] && $awardReferee == "yes"){             
     334                break;
     335            }
     336
     337            if(in_array($coupon['coupon_code'], $applied_coupons) ){
     338                $coupon_data = new WC_Coupon($coupon['coupon_code']);
     339                if($coupon_data->is_valid()){
     340                    if($coupon['coupon_count'] <= 0){
     341                        $woocommerce->cart->remove_coupon( sanitize_text_field( $coupon['coupon_code']  ));
     342                        $woocommerce->cart->calculate_totals();
     343                       
     344                        wc_clear_notices();
     345                        wc_print_notice( 'You have reached the limit to use this discount!', 'error' );
     346                       
     347                       
     348                    }
     349                }
     350            }
     351        }               
     352    }
     353
     354    function apply_coupon_for_referral(){
     355        global $woocommerce;
     356   
     357        $awardReferee = get_option(CWRF_SLUG.'_referee_award', true);
     358        $coupon_code = get_option(CWRF_SLUG.'_coupon_list', true);
     359       
     360       
     361   
     362        if($_COOKIE["STYXKEY_cwrref"] && $awardReferee == "yes" ){
     363           
     364            wc_clear_notices();
     365            $isCouponAlreadyApplied = in_array($coupon_code, $woocommerce->cart->get_applied_coupons());
     366           
     367            $coupon_data = new WC_Coupon($coupon_code);
     368           
     369            $checkUsageLimit = '';
     370            if($coupon_data){
     371                if( $coupon_data->usage_limit > 0){
     372                    $checkUsageLimit = $coupon_data->usage_limit - $coupon_data->usage_count; //check if Usage Limit Exceeeds
     373                }
     374                else{
     375                    $checkUsageLimit = 1;
     376                }
     377            }
     378            else{
     379                $checkUsageLimit = 1;
     380            }
     381           
     382            if (isset($_COOKIE['STYXKEY_remove_coupon'])) {
     383                 if(!$isCouponAlreadyApplied && $_COOKIE["STYXKEY_remove_coupon"] != 'yes' && $checkUsageLimit){
     384                $woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ));
     385                wc_clear_notices();
     386                wc_print_notice( 'A discount coupon has been applied for being a referee!', 'success' );
     387            }
     388        }
     389           
     390
     391            if(!$isCouponAlreadyApplied && $_COOKIE["STYXKEY_remove_coupon"] == 'yes'){                     
     392                wc_print_notice( 'Get your referee discount by using coupon <strong>'.$coupon_code . '</strong>', 'notice' );       
     393            }
     394        }
     395    }
     396
     397    function remove_reward_coupon( $coupon_code ){
     398       
     399        $reward_coupon_code = get_option(CWRF_SLUG.'_coupon_list', true);
     400       
     401        if($reward_coupon_code == $coupon_code && !$_COOKIE["STYXKEY_remove_coupon"]){
     402           
     403            $referral_expiry = get_option(CWRF_SLUG.'_referral_expiry', true);           
     404           
     405            setcookie("STYXKEY_remove_coupon", 'yes', time() + (24 * 3600 * $referral_expiry), "/");
     406        }
     407    }
     408
     409    function get_reward_coupons( $user_id='' ){       
     410        if(!is_user_logged_in()){
     411            return array();
     412        }
     413
     414        global $wpdb;
     415
     416        if($user_id == ''){
     417            $user_id = get_current_user_id();
     418        }
     419
     420        $table = $wpdb->prefix . CWRF_SLUG . "_coupon_rewards";
     421         $query = $wpdb->prepare(
     422                    "SELECT * FROM $table WHERE user_id = %d",
     423                    $user_id,
     424                );       
     425        $coupons = $wpdb->get_results($query, ARRAY_A);
     426
     427        return $coupons;
     428    }
     429
     430    /**
     431     *  Add a custom email to the list of emails WooCommerce should load on settings page
     432     *
     433     * @since 0.1
     434     * @param array $email_classes available email classes
     435     * @return array filtered available email classes
     436     */
     437    function add_custom_wcrf_email( $email_classes ) {
     438        // include our custom email class
     439        require_once( dirname(__FILE__).DS. 'class.codup-wcrf-email.php' );
     440
     441        // add the email class to the list of email classes that WooCommerce loads
     442        $email_classes['CodupWC_ReferralSystem_Email'] = new CodupWC_ReferralSystem_Email();
     443
     444        return $email_classes;
     445    }
     446
     447    /**
     448     * Locate the plugin email templates
     449     *
     450     * @param $core_file
     451     * @param $template
     452     * @param $template_base
     453     *
     454     * @return string
     455     */
     456    function locate_core_template( $core_file, $template, $template_base ) {
     457        $custom_template = array(
     458            'emails/wcrf-email.php',
     459            'emails/plain/wcrf-email-plain.php',
     460        );
     461
     462        if (in_array($template, $custom_template)) {
     463            $core_file = trailingslashit( dirname(__FILE__).DS. 'templates') . $template;
     464        }     
     465
     466        return $core_file;
     467    }
     468
     469     /**
     470     * Add links to the bottom of the plugin name in the WP plugins list
     471     * @param array $links
     472     */
     473    function addActionLinks( $links ){
     474        $cwlLinks = array(
     475            '<a href="'.admin_url('admin.php?page=wc-settings&tab=cwrf').'">Settings</a>'
     476        );
     477       
     478        return array_merge($cwlLinks, $links);
     479    }   
    449480}
  • codup-woocommerce-referral-system/trunk/includes/lib/codupads/codupads.php

    r2700997 r2743271  
    11<?php
    22
    3 class CodupAds {
     3class CodupAds{     
    44
    5     public function __construct() {
    6         add_shortcode( 'codup_ads_top', array( $this, 'codup_render_top_ads' ) );
    7         add_shortcode( 'codup_ads_right', array( $this, 'codup_render_right_ads' ) );
    8         add_action( 'admin_init', array( $this, 'add_style_scripts' ) );
    9         add_action( 'init', array( $this, 'add_style_scripts' ) );
     5    public function __construct(){
     6        add_shortcode('codup_ads_top', array($this, 'codup_render_top_ads'));
     7        add_shortcode('codup_ads_right', array($this, 'codup_render_right_ads'));
     8        add_action('admin_init', array($this, 'add_style_scripts'));
     9        add_action('init', array($this, 'add_style_scripts'));
     10       
    1011
    1112    }
    1213
    13     function cwrf_get_woo_version_number() {
    14         if ( ! function_exists( 'get_plugins' ) ) {
    15             require_once ABSPATH . 'wp-admin/includes/plugin.php';
    16         }
     14    function cwrf_get_woo_version_number(){
     15        if ( ! function_exists( 'get_plugins' ) )
     16            require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
     17       
     18        $plugin_folder = get_plugins( '/' . 'woocommerce' );
     19        $plugin_file = 'woocommerce.php';
     20       
     21        if(isset( $plugin_folder[$plugin_file]['Version'])) {
     22            return $plugin_folder[$plugin_file]['Version'];
    1723
    18         $plugin_folder = get_plugins( '/' . 'woocommerce' );
    19         $plugin_file   = 'woocommerce.php';
     24        }
     25        else{
    2026
    21         if ( isset( $plugin_folder[ $plugin_file ]['Version'] ) ) {
    22             return $plugin_folder[ $plugin_file ]['Version'];
    23 
    24         } else {
    25 
    26             return null;
     27            return NULL;
    2728        }
    2829    }
    2930
    30     function add_style_scripts() {
     31    function add_style_scripts(){
    3132        $pluginConfig = array(
     33       
     34            "pluginName" => CWRF_PLUGIN_NAME,
     35            "pluginVersion" => CWRF_PLUGIN_VER,
     36            "pageSlug" => CWRF_PAGE_SLUG,
     37            "wpVersion" => get_bloginfo('version'),
     38            "wcVersion" => $this->cwrf_get_woo_version_number()
     39        );
     40        wp_enqueue_script(CWRF_PLUGIN_NAME, plugin_dir_url( __FILE__ ) . 'scripts/adscript.js', array( 'jquery' ), CWRF_PLUGIN_VER, false );
     41        wp_enqueue_style(CWRF_PLUGIN_NAME.'- codupads-styles', plugin_dir_url( __FILE__ ) . 'styles/style.css', array(), CWRF_PLUGIN_VER, 'all' );
     42        wp_localize_script(CWRF_PLUGIN_NAME,'PluginConfig',$pluginConfig);
     43    }   
    3244
    33             'pluginName'    => CWRF_PLUGIN_NAME,
    34             'pluginVersion' => CWRF_PLUGIN_VER,
    35             'pageSlug'      => CWRF_PAGE_SLUG,
    36             'wpVersion'     => get_bloginfo( 'version' ),
    37             'wcVersion'     => $this->cwrf_get_woo_version_number(),
    38         );
    39         wp_enqueue_script( CWRF_PLUGIN_NAME, plugin_dir_url( __FILE__ ) . 'scripts/adscript.js', array( 'jquery' ), CWRF_PLUGIN_VER, false );
    40         wp_enqueue_style( CWRF_PLUGIN_NAME . '- codupads-styles', plugin_dir_url( __FILE__ ) . 'styles/style.css', array(), CWRF_PLUGIN_VER, 'all' );
    41         wp_localize_script( CWRF_PLUGIN_NAME, 'PluginConfig', $pluginConfig );
     45    function codup_render_top_ads(){
     46        echo '<div id="codup-topad" class="wrap"></div>';
    4247    }
    4348
    44     function codup_render_top_ads() {
    45         echo '<div id="codup-topad" class="wrap"></div>';
    46     }
    47 
    48     function codup_render_right_ads() {
    49         echo '<div id="codup-rightad" class="stick-to-right"></div>';
    50     }
     49    function codup_render_right_ads(){     
     50        echo '<div id="codup-rightad" class="stick-to-right"></div>';
     51    }
    5152
    5253
    5354}
     55       
     56           
    5457
    5558
    56 
    57 
  • codup-woocommerce-referral-system/trunk/includes/lib/codupads/scripts/adscript.js

    r2700997 r2743271  
    11(function($) {
     2   
     3   
     4    $.ajax({
     5        url: "http://wpads.coduplabs.com/",
     6       
     7        data: {
     8            pluginConfig: PluginConfig
     9        },
     10        type: 'GET',
     11        headers: {
     12            'X-Codup-Ads' : 'ae237uiew4222ghq'
     13        },
     14        success: appendAds
     15    });
     16   
     17    function appendAds(data) {
    218
    3     $.ajax(
    4         {
    5             url: "http://wpads.coduplabs.com/",
     19        data = JSON.parse(data);
     20        $('#codup-topad').html(data.topad);
     21        $('#codup-rightad').html(data.rightad);
    622
    7             data: {
    8                 pluginConfig: PluginConfig
    9             },
    10             type: 'GET',
    11             headers: {
    12                 'X-Codup-Ads' : 'ae237uiew4222ghq'
    13             },
    14             success: appendAds
    15         }
    16     );
    17 
    18     function appendAds(data) {
    19 
    20         data = JSON.parse( data );
    21         $( '#codup-topad' ).html( data.topad );
    22         $( '#codup-rightad' ).html( data.rightad );
    23 
    24     }
    25 
    26 })( jQuery );
     23    }
     24   
     25})(jQuery);
  • codup-woocommerce-referral-system/trunk/includes/lib/codupads/styles/style.css

    r2700997 r2743271  
    11.codup-settings-page {
    2     width : 68%;
    3     display: inline-block;
    4     float: left;
     2    width : 68%;
     3    display: inline-block;
     4    float: left;
    55}
    66
    77.stick-to-right {
    8     margin-top: 60px;
    9     display: inline-block;
     8    margin-top: 60px;
     9    display: inline-block;
    1010}
    1111
    1212#codup-topad{
    1313    box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
    14     background-color: white;
    15     margin-right: 0;
     14    background-color: white;
     15    margin-right: 0;
    1616}
    1717
    1818#codup-topad img{
    1919    vertical-align: middle;
    20     padding: 10px;
    21     height: 16px;
     20    padding: 10px;
     21    height: 16px;
    2222}
    2323#codup-topad a{
  • codup-woocommerce-referral-system/trunk/includes/lib/tables.php

    r2700997 r2743271  
    44global $wpdb;
    55
    6 $wpdb->query(
    7     '
    8     CREATE TABLE IF NOT EXISTS `' . $wpdb->prefix . CWRF_SLUG . '_coupon_rewards` (
     6$wpdb->query("
     7    CREATE TABLE IF NOT EXISTS `" . $wpdb->prefix . CWRF_SLUG . "_coupon_rewards` (
    98      `id` int(11) NOT NULL AUTO_INCREMENT,
    109      `user_id` int(11) NOT NULL, 
     
    1514      PRIMARY KEY (`id`)
    1615    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0
    17 '
    18 );
     16");
  • codup-woocommerce-referral-system/trunk/includes/templates/emails/plain/wcrf-email-plain.php

    r2700997 r2743271  
    22/**
    33 * Codup woocommerce referral system email template
    4  *
     4 * 
    55 * This template can be overridden by copying it to yourtheme/woocommerce/emails/wcrf-email.php .
    6  *
     6 * 
    77 * @param  array  $coupon           The coupon which is rewarded
    88 *
    9  * @param  string $referrer_name    First name and last name of referrer who is being rewarded.
     9 * @param  string $referrer_name    First name and last name of referrer who is being rewarded. 
    1010 */
    11 if ( ! defined( 'ABSPATH' ) ) {
    12     exit;
     11if (!defined('ABSPATH')) {
     12    exit;
    1313}
    1414
     
    1717 */
    1818
    19 echo sprintf( __( 'Hi %s,', 'codup_woocommerce_referral_system' ), $referrer_name );
     19echo esc_html__('Hi '.$referrer_name.',', 'codup_woocommerce_referral_system');
    2020
    21 echo "\n\n";
     21echo esc_html("\n\n");
    2222
    23 echo sprintf( __( 'Thank you for referring a new customer. Here\'s a little treat from us. Enjoy the discount by using coupon %s on your next order.' ), $coupon );
     23echo esc_html__('Thank you for referring a new customer. Here\'s a little treat from us. Enjoy the discount by using coupon '.$coupon.' on your next order.', 'codup_woocommerce_referral_system');
    2424
    25 echo "\n";
     25echo esc_html("\n\n");
    2626
    27 echo sprintf( __( 'Shop now at %s', 'codup_woocommerce_referral_system' ), site_url() . '/shop' );
     27echo esc_html__('Shop now at '. site_url().'/shop', 'codup_woocommerce_referral_system');
    2828
    29 echo "\n\n";
     29echo esc_html("\n\n");
    3030
    3131/**
     
    3434
    3535
    36 echo apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' ) );
     36echo esc_html(apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' ) ));
  • codup-woocommerce-referral-system/trunk/includes/templates/emails/wcrf-email.php

    r2700997 r2743271  
    22/**
    33 * Codup woocommerce referral system email template
     4 *
     5 * This template can be overridden by copying it to yourtheme/woocommerce/emails/wcrf-email.php .
     6 *
     7 * @param  array  $coupon           The coupon which is rewarded
    48 *
    5  * This template can be overridden by copying it to yourtheme/woocommerce/emails/wcrf-email.php .
    6  *
    7  * @param  array  $coupon           The coupon which is rewarded
    8  *
    9  * @param  string $referrer_name    First name and last name of referrer who is being rewarded.
     9 * @param  string $referrer_name    First name and last name of referrer who is being rewarded.   
    1010 */
    11 if ( ! defined( 'ABSPATH' ) ) {
    12     exit;
     11if (!defined('ABSPATH')) {
     12    exit;
    1313}
    1414
     
    1616 * @hooked WC_Emails::email_header() Output the email header
    1717 */
    18 do_action( 'woocommerce_email_header', $email_heading, $email );
     18do_action('woocommerce_email_header', $email_heading, $email);
    1919?>
    2020
    2121<div>       
    22     <?php printf( __( 'Hi <strong>%s</strong>,', 'codup_woocommerce_referral_system' ), $referrer_name ); ?> 
     22    <?php printf(__('Hi <strong>%s</strong>,', 'codup_woocommerce_referral_system'), $referrer_name); ?> 
    2323</div>
    2424
    2525<div>   
    26     <?php printf( __( 'Thank you for referring a new customer. Here\'s a little treat from us. Enjoy the discount by using coupon <strong>%s</strong> on your next order.' ), $coupon ); ?>
     26    <?php printf(__('Thank you for referring a new customer. Here\'s a little treat from us. Enjoy the discount by using coupon <strong>%s</strong> on your next order.' ), $coupon);?>
    2727</div>
    2828<div>
    29     <?php printf( __( '<a href="%s">Shop now! </a>', 'codup_woocommerce_referral_system' ), site_url() . '/shop' ); ?>
     29    <?php printf(__('<a href="%s">Shop now! </a>', 'codup_woocommerce_referral_system'), site_url().'/shop');?>
    3030</div>
    3131
     
    3434 * @hooked WC_Emails::email_footer() Output the email footer
    3535 */
    36 do_action( 'woocommerce_email_footer', $email );
     36do_action('woocommerce_email_footer', $email);
  • codup-woocommerce-referral-system/trunk/includes/templates/referral_system.php

    r2700997 r2743271  
    1 <?php echo do_shortcode( '[codup_ads_top]' ); ?>
     1<?php echo do_shortcode("[codup_ads_top]"); ?>
    22<div class="cwrf_referral_system">
    33    <div class="cwrf_referral_link">
    4         <p>Your referral url: <span><strong><?php echo site_url() . '/?' . $referral_slug . '=' . get_current_user_id(); ?></strong></span></p>
     4        <p>Your referral url: <span><strong><?php echo esc_html(site_url(). '/?' . $referral_slug . '=' . get_current_user_id()); ?></strong></span></p>
    55       
    66    </div>
  • codup-woocommerce-referral-system/trunk/readme.txt

    r2700997 r2743271  
    22Contributors: Codup
    33Tags: WooCommerce, Referral System, Affiliate, Referral
    4 Tested up to: 5.9.2
     4Tested up to: 5.3.2
     5Stable tag: 1.1.2.0
    56License: GPLv2 or later
    67
     
    5556
    5657
     581.1.1.12
     59discount limit and send email to refree has been fixed
     60
     611.1.1.13
     62discount inifinty limit has been fixed
Note: See TracChangeset for help on using the changeset viewer.