Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.67% covered (warning)
88.67%
180 / 203
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
snap_marketing_front_side
89.11% covered (warning)
89.11%
180 / 202
57.14% covered (warning)
57.14%
4 / 7
60.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
3
 snap_marketing_front_scripts
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
5
 snap_marketing_front
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 set_snap_marketing_environment
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 snap_treatment_add
73.13% covered (warning)
73.13%
49 / 67
0.00% covered (danger)
0.00%
0 / 1
49.63
 get_snap_finance_token
98.51% covered (success)
98.51%
66 / 67
0.00% covered (danger)
0.00%
0 / 1
12
 add_log_message
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/*******  admin side view *******/
4class snap_marketing_front_side {
5
6    public $snap_marketin_configuration = array( 'snap_marketing_estimate' => 'W' );
7
8    public $Treatment_Estimate = array(
9        'BW' => 'Bi Weekly',
10        'W'  => 'week',
11        'M'  => 'Monthly',
12        'MW' => 'Monthly Week',
13        'SM' => 'Semi Monthly',
14    );
15
16    public $set_snap_marketing_environment = '';
17
18    /**
19     * Class constructor, more about it in Step 3
20     */
21    public function __construct() {
22
23        $treatment_types = array(
24            'get_approved_as_low_as',
25            'get_approved',
26            'banner',
27            /*
28            'prequalification',
29            'prequalification_as_low_as'*/
30        );
31
32        foreach ( $treatment_types as $treatment_type ) {
33            add_shortcode( 'snap_treatment_add_' . $treatment_type, array( $this, 'snap_treatment_add' ) );
34        }
35
36        // This action use for show list of quality range price details on product detail page.
37        add_action( 'woocommerce_single_product_summary', array( $this, 'snap_treatment_add' ), 15 );
38        $Configuration_Options = array(
39            'snap_marketing_active',
40            'snap_marketing_title',
41            'snap_marketing_description',
42            'snap_marketing_environment',
43            /*'snap_marketing_estimate',*/
44            'snap_marketing_dev_id',
45            'snap_marketing_dev_secret_key',
46            'snap_marketing_qa_id',
47            'snap_marketing_qa_secret_key',
48            'snap_marketing_sandbox_id',
49            'snap_marketing_sandbox_secret_key',
50            'snap_marketing_live_id',
51            'snap_marketing_live_secret_key',
52        );
53        foreach ( $Configuration_Options as $Configuration_Option ) {
54            $this->snap_marketin_configuration[ $Configuration_Option ] = get_option( $Configuration_Option );
55        }
56        $this->set_snap_marketing_environment();
57        add_filter( 'snap_marketing_configuration_details', array($this, 'snap_marketing_configuration') );
58        add_filter( 'wp_enqueue_scripts', array( $this, 'snap_marketing_front_scripts' ) );
59        add_action( 'wp_enqueue_scripts', array( $this, 'snap_marketing_front_scripts' ) );
60        add_action( 'wp_ajax_snap_marketing_front', array( $this, 'snap_marketing_front' ) );
61        add_action( 'wp_ajax_nopriv_snap_marketing_front', array( $this, 'snap_marketing_front' ) );
62
63    }
64
65    /*
66     * Add style and script file on front side of site.
67     */
68
69    public function snap_marketing_front_scripts() {
70        global $post;
71
72        $snap_marketing_script = constant( 'Sandbox_Snap_Marketing_SDK' );
73        if ( $this->snap_marketin_configuration['snap_marketing_environment'] == 'Dev' ) {
74            $snap_marketing_script = constant( 'Dev_Snap_Marketing_SDK' );
75        } else if ( $this->snap_marketin_configuration['snap_marketing_environment'] == 'Qa' ) {
76            $snap_marketing_script = constant( 'Qa_Snap_Marketing_SDK' );
77        } else if ( $this->snap_marketin_configuration['snap_marketing_environment'] == 'Production' ) {
78            $snap_marketing_script = constant( 'Live_Snap_Marketing_SDK' );
79        } else {
80            $snap_marketing_script = constant( 'Sandbox_Snap_Marketing_SDK' );
81        }
82        //$payment_method = filter_input( INPUT_GET, 'payment_method', FILTER_SANITIZE_STRING );
83        $payment_method = wp_strip_all_tags(filter_input( INPUT_GET, 'payment_method', FILTER_UNSAFE_RAW ));
84        if ( empty( $payment_method ) ) {
85            wp_enqueue_script( 'snap-finance-sdk', $snap_marketing_script, array( 'jquery' ), time(), true );
86        }
87        wp_localize_script(
88            'snap-finance-sdk',
89            'ajax_object',
90            array(
91                'ajax_url' => admin_url( 'admin-ajax.php' ),
92                'we_value' => 1234,
93            )
94        );
95
96        wp_enqueue_script( 'snap-finance-snap-marketing-front', plugins_url( 'assets/js/snap-marketing-front.js', __FILE__ ), array( 'jquery' ), time(), true );
97        // wp_enqueue_script( 'snap-finance-sdk-front', 'https://js-qa-dev.snapfinance.com/dev/v2/snap-sdk.js', array( 'jquery' ), time(), true );
98
99        wp_enqueue_style( 'snap-marketing-front-style', plugins_url( 'assets/css/snap-marketing-front.css', __FILE__ ), array(), time(), false );
100
101    }
102
103    public function snap_marketing_front() {
104
105        $variation_id = sanitize_text_field($_REQUEST['variation_id']);
106
107        $this->snap_treatment_add( $variation_id );
108
109        die;
110    }
111
112    public function set_snap_marketing_environment() {
113        $snap_finance_setting = $this->snap_marketin_configuration;
114        if ( ! isset( $snap_finance_setting['snap_marketing_environment'] ) ) {
115            $snap_finance_setting['snap_marketing_environment'] = 'Sandbox';
116        }
117        if ( empty( $snap_finance_setting['snap_marketing_environment'] ) ) {
118            $snap_finance_setting['snap_marketing_environment'] = 'Sandbox';
119        }
120
121        $this->set_snap_marketing_environment = $snap_finance_setting['snap_marketing_environment'];
122
123    }
124
125    /*
126     * Create shortcode treatment
127     */
128    public function snap_treatment_add( $atts = '' ) {
129
130        if ( empty( $atts ) ) {
131            $atts = array( 'variation_id' => 0 );
132        }
133        if ( ! $this->snap_marketin_configuration['snap_marketing_active'] ) {
134            return '';
135        }
136
137        global $post, $product;
138        $product_price = 0;
139        if ( ! isset( $atts['treat_id'] ) ) {
140            $Snap_Product_Active = get_option( 'Snap_Product_Active' );
141
142            if ( $Snap_Product_Active ) {
143                $atts = array(
144                    'treat_id'     => $Snap_Product_Active,
145                    'echo'         => true,
146                    'variation_id' => $atts,
147                );
148            } else {
149                return '';
150            }
151        } else {
152            if ( is_singular( 'product' ) ) {
153                $Snap_Product_Active = get_option( 'Snap_Product_Active' );
154                if ( $Snap_Product_Active && ( get_post_meta( $Snap_Product_Active, 'Snap_TreatmentType', true ) == get_post_meta( $atts['treat_id'], 'Snap_TreatmentType', true ) ) ) {
155                    return '';
156                }
157            }
158        }
159
160        $Treatment_data = get_post( $atts['treat_id'] );
161
162        $Snap_TreatmentType   = get_post_meta( $atts['treat_id'], 'Snap_TreatmentType', true );
163        $Snap_TreatmentLogo   = get_post_meta( $atts['treat_id'], 'Snap_TreatmentLogo', true );
164        $Snap_TreatmentActive = get_post_meta( $atts['treat_id'], 'Snap_TreatmentActive', true );
165
166        if ( $Snap_TreatmentActive == 'Disable' ) {
167            return '';
168        }
169
170        if ( ( is_singular( 'product' ) && $Snap_TreatmentType == 'AS_LOW_AS' ) || isset( $atts['variation_id'] ) ) {
171
172            if ( $atts['variation_id'] != 0 && ! is_array( $atts['variation_id'] ) ) {
173                $sales_price = get_post_meta( $atts['variation_id'], '_sale_price', true );
174                if ( $sales_price ) {
175                    $product_price = $sales_price;
176                } else {
177                    $product_price = get_post_meta( $atts['variation_id'], '_regular_price', true );
178                }
179
180                $post = get_post_parent( $atts['variation_id'] );
181
182                if ( $post ) {
183                    $product = wc_get_product( $post->ID );
184                }
185            } else {
186                if ( $product->is_type( 'simple' ) ) {
187                    $product_price = $product->get_price();
188                } elseif ( $product->is_type( 'variable' ) ) {
189                    $product_price = $product->get_variation_price();
190                }
191            }
192            
193            if ( $product_price < 150 || $product->get_stock_status() === 'outofstock' || empty( $product_price ) ) {
194                return '';
195           }
196        }
197
198        $Snap_TreatmentAlignment = get_post_meta( $atts['treat_id'], 'Snap_TreatmentAlignment', true );
199
200        if ( ( ( $post->post_type != 'product' && $Snap_TreatmentType == 'AS_LOW_AS' ) || ( $Treatment_data->post_status != 'publish' ) ) && ! isset( $attr['variation_id'] ) ) {
201            return '';
202        }
203        $Snap_Token = $this->get_snap_finance_token();
204
205        ob_start();
206        ?>
207        <section class="snap-marketing-treatment alignment-<?php echo esc_attr( strtolower( $Snap_TreatmentAlignment ) ); ?> <?php echo esc_attr( strtolower( $Snap_TreatmentType ) ); ?>">
208            <div class="treatment-logo">
209                <img src="<?php echo esc_url( $Snap_TreatmentLogo ); ?>"/>
210            </div>
211            <div class="treatment-desc">
212                <?php
213                switch ( $Snap_TreatmentType ) {
214                    case 'PRE_APPROVAL':
215                        include 'inc/snap-pre-approval.php';
216                        break;
217                    case 'BANNER':
218                        include 'inc/snap-banner.php';
219                        break;
220                    case 'PRE_QUALIFICATION':
221                        include 'inc/snap-pre-qualification.php';
222                        break;
223                    case 'PRE_QUALIFICATION_AS_LOW_AS':
224                        include 'inc/snap-pre-qualification-as-low-as.php';
225                        break;
226                    default:
227                        include 'inc/snap-get-approved-as-low-as.php';
228                }
229                ?>
230            </div>
231        </section>
232        <?php
233        $output = ob_get_contents();
234        ob_end_clean();
235
236        if ( isset( $atts['echo'] ) ) {
237            echo __( $output );
238        } else {
239            return $output;
240        }
241    }
242
243    public function get_snap_finance_token() {
244        $api_url              = $client_id = $client_secret = $audience_url = '';
245        $snap_finance_setting = $this->snap_marketin_configuration;
246        if ( $this->set_snap_marketing_environment == 'Dev' ) {
247            $client_id     = $snap_finance_setting['snap_marketing_dev_id'];
248            $client_secret = $snap_finance_setting['snap_marketing_dev_secret_key'];
249            $api_url       = constant( 'Dev_API_URL' );
250            $audience_url  = constant( 'Dev_Audience_URL' );
251        } else if ( $this->set_snap_marketing_environment == 'Qa' ) {
252            $client_id     = $snap_finance_setting['snap_marketing_qa_id'];
253            $client_secret = $snap_finance_setting['snap_marketing_qa_secret_key'];
254            $api_url       = constant( 'Qa_API_URL' );
255            $audience_url  = constant( 'Qa_Audience_URL' );
256        }  else if ( $this->set_snap_marketing_environment == 'Sandbox' ) {
257            $client_id     = $snap_finance_setting['snap_marketing_sandbox_id'];
258            $client_secret = $snap_finance_setting['snap_marketing_sandbox_secret_key'];
259            $api_url       = constant( 'Sandbox_API_URL' );
260            $audience_url  = constant( 'Sandbox_Audience_URL' );
261        } else {
262            $client_id     = $snap_finance_setting['snap_marketing_live_id'];
263            $client_secret = $snap_finance_setting['snap_marketing_live_secret_key'];
264            $api_url       = constant( 'Live_API_URL' );
265            $audience_url  = constant( 'Live_Audience_URL' );
266        }
267        $snap_finance_token = false;
268        // Checking last updated token data in database.
269        if ( 1 == 1 or WP_DEBUG or false === ( $snap_finance_token = get_transient( 'snap_marketing_token' ) ) ) {
270
271            $args = array(
272                'returntransfer' => true,
273                'maxredirs'      => 10,
274                'httpversion'    => CURL_HTTP_VERSION_1_1,
275                'headers'        => array(
276                    'content-type: application/json',
277                ),
278                'body'           => array(
279                    'client_id'     => $client_id,
280                    'client_secret' => $client_secret,
281                    'audience'      => $audience_url,
282                    'grant_type'    => 'client_credentials',
283                ),
284            );
285
286            $response = wp_remote_post( $api_url, $args );
287            if ( ! is_wp_error( $response ) ) {
288                $response = $response['body'];
289                $response = json_decode( $response );
290                if ( isset( $response->access_token ) ) {
291                    $snap_finance_token = $response->access_token;
292                } else {
293                    $train_audience_url  = constant( 'Training_Audience_URL' );
294                    $args = array(
295                        'returntransfer' => true,
296                        'maxredirs'      => 10,
297                        'httpversion'    => CURL_HTTP_VERSION_1_1,
298                        'headers'        => array(
299                            'content-type: application/json',
300                        ),
301                        'body'           => array(
302                            'client_id'     => $client_id,
303                            'client_secret' => $client_secret,
304                            'audience'      => $train_audience_url,
305                            'grant_type'    => 'client_credentials',
306                        ),
307                    );
308                    $response_train = wp_remote_post( $api_url, $args );
309                    $response_train = $response_train['body'];
310                    $response_train = json_decode( $response_train );
311                    if ( isset( $response_train->access_token ) ) {
312                        $snap_finance_token = $response_train->access_token;
313                    } else {
314                        $snap_finance_token = false;
315                    }
316                }
317                // Add new updated token in database.
318                // $this->add_log_message( 'Create new snap finance token' );
319                if ( $snap_finance_token && empty( $version ) ) {
320                    set_transient( 'snap_marketing_token', $snap_finance_token, 600 );
321                }
322            }
323        }
324
325        return $snap_finance_token;
326    }
327
328    public function add_log_message( $message = '' ) {
329        $log_url      = plugin_dir_url( __FILE__ ) . 'log.txt';
330        $old_message  = '';
331        $old_message  = wp_remote_get( $log_url )['body'];
332        $old_message .= '\n\n\r' . $message . ' [ ' . date( 'm/d/Y h:i:s a', time() ) . ' ]';
333        file_put_contents( $path, $old_message );
334    }
335
336}
337
338$snap_marketing_front_side = new snap_marketing_front_side();