Plugin Directory

Changeset 3030498


Ignore:
Timestamp:
02/02/2024 10:36:57 AM (2 years ago)
Author:
eberwp
Message:

update version 3.4 with API settings enable

Location:
eber
Files:
21 added
5 edited

Legend:

Unmodified
Added
Removed
  • eber/trunk/index.php

    r3002641 r3030498  
    55Description: Eber is a smart member system comes with comprehensive loyalty & rewards system, marketing and analytic tool.
    66Author: Eber
    7 Version: 3.3.1
     7Version: 3.4
    88Author URI: https://eber.co
    99License: GPLv2 or later
     
    2828add_action( 'wp_enqueue_scripts', array('Eber_FrontEnd','get_js') );
    2929add_action( 'wp_ajax_eber_init', array('Eber_FrontEnd','ajax_call'));
     30add_action( 'wp_ajax_eber_api', array('Eber_FrontEnd','api_call'));
    3031// none login
    3132add_action( 'wp_ajax_nopriv_eber_init', array('Eber_FrontEnd','ajax_call'));
     33add_action( 'wp_ajax_nopriv_eber_api', array('Eber_FrontEnd','api_call'));
    3234
    3335// end load js
  • eber/trunk/init/frontend.php

    r2916744 r3030498  
    11<?php
    2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    3 
    4     class Eber_FrontEnd {
    5         public static function get_js(){
    6             $slug = get_option('eber_business_slug','');
    7             $id = get_option('eber_business_id','');
    8             $status = get_option('eber_status',false);
    9             $widget_new = get_option('widget_new',false);
    10             $eber_custom_link = get_option('eber_custom_link',false);
    11             if($eber_custom_link)
     2if (!defined('ABSPATH')) exit; // Exit if accessed directly
     3
     4class Eber_FrontEnd
     5{
     6    public static function get_js()
     7    {
     8        $slug = get_option('eber_business_slug', '');
     9        $id = get_option('eber_business_id', '');
     10        $status = get_option('eber_status', false);
     11        $widget_new = get_option('widget_new', false);
     12        $eber_custom_link = get_option('eber_custom_link', false);
     13        if ($eber_custom_link) {
     14            $login = '&login=' . urlencode(get_option('eber_custom_login_url', ''));
     15            $login_url = '&login_url=' . urlencode(get_option('eber_custom_login_url', ''));
     16            $signup = '&signup=' . urlencode(get_option('eber_custom_signup_url', ''));
     17        } else {
     18            $login = '';
     19            $login_url = '';
     20            $signup = '';
     21        }
     22        if ($slug != '') {
     23            if (!$widget_new)
     24                wp_enqueue_script('my-js', '//widget.eber.co/widget/' . $slug . '?type=wordpress&hid=' . ((!$status) ? '1' : '0') . '&shop=' . get_bloginfo('url') . $login . $signup . '&init=' . get_bloginfo('url') . '/wp-admin/admin-ajax.php?action=eber_init', array('jquery'));
     25            else
     26                wp_enqueue_script('my-js', '//widget.eber.co/new-widget/initialize/' . $id . '?type=wordpress&hid=' . ((!$status) ? '1' : '0') . '&shop=' . get_bloginfo('url') . $login_url . $signup . '&init=' . get_bloginfo('url') . '/wp-admin/admin-ajax.php?action=eber_init', array('jquery'), null, true);
     27        }
     28    }
     29
     30    public static function api_call()
     31    {
     32        $api_key = get_option('eber_api_key', '');
     33        $store_id = get_option('eber_store_id', 0);
     34        if ($api_key == '' || $store_id == 0) {
     35            $return = array();
     36            $return['success'] = false;
     37            $return['message'] = 'Plugin not Active yet';
     38            header('Content-Type: application/json');
     39            echo json_encode($return);
     40            die;
     41        }
     42        $api_enable = get_option('eber_api_enable', true);
     43        if (!$api_enable) {
     44            $return = array();
     45            $return['success'] = false;
     46            $return['message'] = 'API setting is turn off';
     47            header('Content-Type: application/json');
     48            echo json_encode($return);
     49            die;
     50        }
     51        $api_user = sanitize_text_field($_SERVER['PHP_AUTH_USER']);
     52        $api_pass  = sanitize_text_field($_SERVER['PHP_AUTH_PW']);
     53        if ($api_user !== $api_key || $api_pass !== $store_id) {
     54            $return = array();
     55            $return['success'] = false;
     56            $return['message'] = 'API not Valid';
     57            header('Content-Type: application/json');
     58            echo json_encode($return);
     59            die;
     60
     61        }
     62        // get data
     63        $post_data = array();
     64        foreach($_POST as $key=>$value)
     65        {
     66            $post_data[$key] = sanitize_text_field($value);
     67        }
     68        // end get data
     69        $validate_array = array(
     70            'user_login',
     71            'user_email',
     72            'first_name',
     73            'last_name'
     74        );
     75        $message = '';
     76        // check validate
     77        foreach($validate_array as $value)
     78        {
     79            if(empty($post_data[$value]))
     80                $message .= ' Field '.ucfirst(str_replace('_',' ',$value). ' is required,');
     81        }
     82        // end check
     83   
     84        if($message != '')
     85        {
     86            $return = array();
     87            $return['success'] = false;
     88            $return['message'] = $message;
     89            header('Content-Type: application/json');
     90            echo json_encode($return);
     91            die;
     92        }
     93        // check email
     94        $user_check = email_exists($post_data['user_email']);
     95        if(!$user_check)
     96            $user_check = username_exists($post_data['user_login']);
     97        $is_skip = false;
     98        if($user_check)
     99        {
     100            $user_meta = get_userdata($user_check);
     101            $user_roles = $user_meta->roles;
     102            foreach($user_roles as $role)
    12103            {
    13                 $login = '&login='.urlencode(get_option('eber_custom_login_url',''));
    14                 $login_url = '&login_url='.urlencode(get_option('eber_custom_login_url',''));
    15                 $signup = '&signup='.urlencode(get_option('eber_custom_signup_url',''));
    16             }
    17             else
     104                if($role != 'subscriber')
     105                    $is_skip = true;
     106            }
     107            if(!$is_skip)
    18108            {
    19                 $login = '';
    20                 $login_url = '';
    21                 $signup = '';
    22             }
    23             if($slug != '')
    24             {
    25                 if(!$widget_new)
    26                     wp_enqueue_script('my-js', '//widget.eber.co/widget/'. $slug .'?type=wordpress&hid='.((!$status)?'1':'0').'&shop=' . get_bloginfo('url') .$login.$signup. '&init=' . get_bloginfo('url').'/wp-admin/admin-ajax.php?action=eber_init', array('jquery'));
    27                 else
    28                     wp_enqueue_script('my-js', '//widget.eber.co/new-widget/initialize/'. $id .'?type=wordpress&hid='.((!$status)?'1':'0').'&shop=' . get_bloginfo('url') .$login_url.$signup. '&init=' . get_bloginfo('url').'/wp-admin/admin-ajax.php?action=eber_init', array('jquery'),null,true);
    29             }
    30 
    31 
    32 
    33         }
    34        
    35 
    36 
    37         public static function ajax_call(){
    38             $sync = get_option('eber_sync',false);
    39             $eber_welcome_email = get_option('eber_welcome_email',false);
    40             $notify = 0;
    41             if($eber_welcome_email)
    42                 $notify = 1;
    43             if ( is_user_logged_in()  && $sync)
    44             {
    45                 $current_user = wp_get_current_user();
    46                 $eber_hash_key = get_option('eber_hash_key','');
    47                 $user_id = $current_user->data->ID;
    48                 $user_email = $current_user->data->user_email;
    49                 $user_name = $current_user->data->display_name;
    50                 if($current_user->first_name != '' || $current_user->last_name != '')
    51                     $user_name = $current_user->first_name . ' ' . $current_user->last_name;
    52                 $digest = md5($user_id.$eber_hash_key);
    53                 // Call API
    54                 require_once(dirname(__FILE__).'/../init/api/API.php');
    55                 require_once(dirname(__FILE__).'/../init/api/APIResponse.php');
    56                 $api_key = get_option('eber_api_key','');
    57                 $api = new API;
    58                 $result = $api->setData(['provider' => 'wordpress', 'provider_identifier' => $eber_hash_key,'hash'=>$digest,'email'=>$user_email,'display_name'=>$user_name,'provider_user_id'=>$user_id,'notify'=>$notify])
    59                     ->setQueue()
    60                     ->requireAuth($api_key)
    61                     ->post('/v3/user/ecom');
    62                 $return_api = ['data' => []];
    63                 if (!$result->isFailure()) {
    64                     $return_api = $result->data;
    65                     $return = array();
    66                     $return['success'] = true;
    67                     $return['message'] = $return_api;
    68                     header('Content-Type: application/json');
    69                     echo json_encode($return);
    70                 }
    71                 else {
    72                     header('Content-Type: application/json');
    73                     echo json_encode(array('success'=>FALSE,'message'=>'API not valid'));
    74                 }
    75 
    76                 // End API
    77 
    78 
    79 
    80             } else {
     109                $post_data['ID'] = $user_check;
     110            }
     111        }
     112        // end_check
     113        // create user
     114        if(empty($post_data['user_pass']) && !$user_check)
     115            $post_data['user_pass'] = md5($api_key.md5(strtotime('now')));
     116        if(empty($post_data['display_name']))
     117            $post_data['display_name'] = $post_data['first_name'].' '.$post_data['last_name'];
     118        $post_data['role'] = 'subscriber';
     119        $user = (wp_insert_user($post_data));
     120        if ( is_wp_error( $user ) ) {
     121            $return = array();
     122            $return['success'] = false;
     123            $return['message'] = $user->get_error_message();;
     124            header('Content-Type: application/json');
     125            echo json_encode($return);
     126            die;
     127        }
     128        // end create
     129        header('Content-Type: application/json');
     130        echo json_encode(array( 'success' => TRUE, 'message' => 'User update succesfully !','user_id'=>$user ));
     131         die;
     132    }
     133
     134    public static function ajax_call(){
     135        $sync = get_option('eber_sync',false);
     136        $eber_welcome_email = get_option('eber_welcome_email',false);
     137        $notify = 0;
     138        if($eber_welcome_email)
     139            $notify = 1;
     140        if ( is_user_logged_in()  && $sync)
     141        {
     142            $current_user = wp_get_current_user();
     143            $eber_hash_key = get_option('eber_hash_key','');
     144            $user_id = $current_user->data->ID;
     145            $user_email = $current_user->data->user_email;
     146            $user_name = $current_user->data->display_name;
     147            if($current_user->first_name != '' || $current_user->last_name != '')
     148                $user_name = $current_user->first_name . ' ' . $current_user->last_name;
     149            $digest = md5($user_id.$eber_hash_key);
     150            // Call API
     151            require_once(dirname(__FILE__).'/../init/api/API.php');
     152            require_once(dirname(__FILE__).'/../init/api/APIResponse.php');
     153            $api_key = get_option('eber_api_key','');
     154            $api = new API;
     155            $result = $api->setData(['provider' => 'wordpress', 'provider_identifier' => $eber_hash_key,'hash'=>$digest,'email'=>$user_email,'display_name'=>$user_name,'provider_user_id'=>$user_id,'notify'=>$notify])
     156                ->setQueue()
     157                ->requireAuth($api_key)
     158                ->post('/v3/user/ecom');
     159            $return_api = ['data' => []];
     160            if (!$result->isFailure()) {
     161                $return_api = $result->data;
     162                $return = array();
     163                $return['success'] = true;
     164                $return['message'] = $return_api;
    81165                header('Content-Type: application/json');
    82                 echo json_encode(array('success'=>FALSE,'message'=>'User not login'));
    83             }
    84             die;
    85         }
    86 
    87         public static function sync_user($user_id)
    88         {
    89             $user = get_user_by('id',$user_id);
    90             $sync = get_option('eber_sync',false);
    91             $eber_welcome_email = get_option('eber_welcome_email',false);
    92             $notify = 0;
    93             if($eber_welcome_email)
    94                 $notify = 1;
    95             if($sync)
    96             {
    97                 $eber_hash_key = get_option('eber_hash_key','');
    98                 $user_id = $user->data->ID;
    99                 $user_email = $user->data->user_email;
    100                 $user_name = $user->data->display_name;
    101                 if(!empty($user->data->first_name) != '' || !empty($user->data->last_name) != '')
    102                     $user_name = $user->data->first_name . ' ' . $user->data->last_name;
    103                 $digest = md5($user_id.$eber_hash_key);
    104                 // Call API
    105                 require_once(dirname(__FILE__).'/../init/api/API.php');
    106                 require_once(dirname(__FILE__).'/../init/api/APIResponse.php');
    107                 $api_key = get_option('eber_api_key','');
    108                 $api = new API;
    109                 $result = $api->setData(['provider' => 'wordpress', 'provider_identifier' => $eber_hash_key,'hash'=>$digest,'email'=>$user_email,'display_name'=>$user_name,'provider_user_id'=>$user_id,'notify'=>$notify])
    110                     ->setQueue()
    111                     ->requireAuth($api_key)
    112                     ->post('/v3/user/ecom');
    113                 $return_api = ['data' => []];
    114                 // if (!$result->isFailure()) {
    115                 //     $return_api = $result->data;
    116                 //     $return = array();
    117                 //     $return['success'] = true;
    118                 //     $return['message'] = $return_api;
    119                 //     header('Content-Type: application/json');
    120                 //     echo json_encode($return);
    121                 // }
    122                 // else {
    123                 //     header('Content-Type: application/json');
    124                 //     echo json_encode(array('success'=>FALSE,'message'=>'API not valid'));
    125                 // }
    126             }
    127         }
    128        
    129        
    130     }
     166                echo json_encode($return);
     167            }
     168            else {
     169                header('Content-Type: application/json');
     170                echo json_encode(array('success'=>FALSE,'message'=>'API not valid'));
     171            }
     172
     173            // End API
     174
     175
     176
     177        } else {
     178            header('Content-Type: application/json');
     179            echo json_encode(array('success'=>FALSE,'message'=>'User not login'));
     180        }
     181        die;
     182    }
     183
     184    public static function sync_user($user_id)
     185    {
     186        $user = get_user_by('id',$user_id);
     187        $sync = get_option('eber_sync',false);
     188        $eber_welcome_email = get_option('eber_welcome_email',false);
     189        $notify = 0;
     190        if($eber_welcome_email)
     191            $notify = 1;
     192        if($sync)
     193        {
     194            $eber_hash_key = get_option('eber_hash_key','');
     195            $user_id = $user->data->ID;
     196            $user_email = $user->data->user_email;
     197            $user_name = $user->data->display_name;
     198            if(!empty($user->data->first_name) != '' || !empty($user->data->last_name) != '')
     199                $user_name = $user->data->first_name . ' ' . $user->data->last_name;
     200            $digest = md5($user_id.$eber_hash_key);
     201            // Call API
     202            require_once(dirname(__FILE__).'/../init/api/API.php');
     203            require_once(dirname(__FILE__).'/../init/api/APIResponse.php');
     204            $api_key = get_option('eber_api_key','');
     205            $api = new API;
     206            $result = $api->setData(['provider' => 'wordpress', 'provider_identifier' => $eber_hash_key,'hash'=>$digest,'email'=>$user_email,'display_name'=>$user_name,'provider_user_id'=>$user_id,'notify'=>$notify])
     207                ->setQueue()
     208                ->requireAuth($api_key)
     209                ->post('/v3/user/ecom');
     210            $return_api = ['data' => []];
     211            // if (!$result->isFailure()) {
     212            //     $return_api = $result->data;
     213            //     $return = array();
     214            //     $return['success'] = true;
     215            //     $return['message'] = $return_api;
     216            //     header('Content-Type: application/json');
     217            //     echo json_encode($return);
     218            // }
     219            // else {
     220            //     header('Content-Type: application/json');
     221            //     echo json_encode(array('success'=>FALSE,'message'=>'API not valid'));
     222            // }
     223        }
     224    }
     225}
  • eber/trunk/init/menu.php

    r3002249 r3030498  
    3131            $exclude_coupon = get_option('exclude_coupon',true);
    3232            $widget_new = get_option('widget_new',false);
     33            $eber_api_enable = get_option('eber_api_enable',false);
    3334            $sync = get_option('eber_sync',false);
    3435            if($api_key != ''){
     
    4748                $exclude_coupon = (isset($_POST['exclude_coupon']));
    4849                $widget_new = (isset($_POST['widget_new']));
     50                $eber_api_enable = (isset($_POST['eber_api_enable']));
    4951                if(isset($_POST['eber_custom_login_url']))
    5052                {
     
    180182                                    update_option('issue_point_when',sanitize_text_field($_POST['issue_point_when']));
    181183                                    $issue_point_when = sanitize_text_field($_POST['issue_point_when']);
     184                                }
     185                                if(isset($_POST['eber_api_enable']))
     186                                {
     187                                    update_option('eber_api_enable',true);
     188                                }
     189                                else
     190                                {
     191                                    update_option('eber_api_enable',false);
    182192                                }
    183193
  • eber/trunk/layout/index.php

    r3002249 r3030498  
    112112    </td>
    113113</tr>
    114 
     114<tr>
     115    <th scope="row">&nbsp;</th>
     116    <td>
     117        <fieldset>
     118        <legend class="screen-reader-text"><span>API</span></legend><label for="eber_api_enable">
     119                <input name="eber_api_enable" type="checkbox" id="eber_api_enable" value="1" <?php echo ($eber_api_enable)?'checked':'';?>>
     120                Enable API ?</label>
     121       
     122               </fieldset>
     123    </td>
    115124<tr>
    116125    <th scope="row">&nbsp;</th>
  • eber/trunk/readme.txt

    r3002641 r3030498  
    9494= 3.3.1 =
    9595hot fix for manual sync
     96= 3.4 =
     97API enable settings
    9698
    9799
Note: See TracChangeset for help on using the changeset viewer.