Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.13% covered (success)
94.13%
417 / 443
73.68% covered (warning)
73.68%
14 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
snap_marketing_admin_side
94.34% covered (success)
94.34%
417 / 442
73.68% covered (warning)
73.68%
14 / 19
87.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
94 / 94
100.00% covered (success)
100.00%
1 / 1
3
 snap_marketing_reset_token
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 treatment_columns_head
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 treatment_columns_content
81.25% covered (warning)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
7.32
 get_treatment_type
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 register_marketing_settings
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
4
 save_create_treatments_post_type
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
7
 create_treatments_post_type_extra_options
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 treatment_logo_urls
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
12
 treatment_extra_options
98.48% covered (success)
98.48%
65 / 66
0.00% covered (danger)
0.00%
0 / 1
10
 create_configuration_page
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 snap_marketing_configuration_settings
84.85% covered (warning)
84.85%
28 / 33
0.00% covered (danger)
0.00%
0 / 1
7.17
 configuration_input_fields
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 configuration_textarea_fields
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 configuration_checkbox_fields
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
2.00
 configuration_select_fields
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 create_treatments_post_type
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
1
 snap_marketing_scripts
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
8
 validate_token
72.22% covered (warning)
72.22%
39 / 54
0.00% covered (danger)
0.00%
0 / 1
16.62
1<?php
2
3/*******  admin side view *******/
4class snap_marketing_admin_side {
5
6
7    public $Treatment_Types = array(
8        'PRE_APPROVAL' => 'Get Approved',
9        'AS_LOW_AS'    => 'Get Approved – As low as',
10        'BANNER'       => 'Banner',
11        /*
12        'PRE_QUALIFICATION' => 'Prequalification',
13        'PRE_QUALIFICATION_AS_LOW_AS' => 'Prequalification - As low as',*/
14    );
15
16    public $Treatment_Estimate = array(
17        'BW' => 'Bi Weekly',
18        'W'  => 'Weekly',
19        'M'  => 'Monthly',
20        'MW' => 'Monthly Week',
21        'SM' => 'Semi Monthly',
22    );
23
24    public $Treatment_Alignment = array(
25        'Left',
26        'Center',
27        'Right',
28    );
29
30    public $Treatment_Logo = array();
31
32    public $Treatment_Active = array( 'Enable', 'Disable' );
33
34    public $Configuration_Options = array();
35
36    public $Treatment_Settings = array();
37
38    /**
39     * Class constructor, more about it in Step 3
40     */
41    public function __construct() {
42
43        $this->Treatment_Logo        = $this->treatment_logo_urls();
44        $this->Configuration_Options = array(
45            'snap_marketing_active' => array(
46                'type'    => 'checkbox',
47                'label'   => __( 'Enable/Disable', 'snap-marketing' ),
48                'details' => __( 'Enable Snap Marketing', 'snap-marketing' ),
49                'value'   => 0,
50            ),
51            'snap_marketing_title'              => array(
52                'type'     => 'text',
53                'value'    => 'Snap Marketing',
54                'label'    => __( 'Title', 'snap-marketing' ),
55                'readonly' => 'readonly',
56            ),
57            'snap_marketing_description'        => array(
58                'type'     => 'textarea',
59                'value'    => 'The Snap Marketing plugin enables preapproval functionality where your credit-challenged shoppers can get potentially approved during key moments of their shopping journey on your webstore; thereby, giving you greater ability to close more sales. There are multiple ways to drive e-commerce preapproval customers, with our “Get Approved? and “Get Approved – As low as? treatments. We encourage you to use both on key places on your site to drive visibility for customers who may need financing to transact on your store.',
60                'label'    => __( 'Description', 'snap-marketing' ),
61                'readonly' => 'readonly',
62            ),
63            'snap_marketing_environment'        => array(
64                'type'     => 'select',
65                'value'    => '',
66                'options'  => ( defined( 'SNAP_ENV' ) && SNAP_ENV === 'snap_qa' )
67                ? array(
68                    'Dev'    => 'Dev',
69                    'Qa'    => 'QA',
70                    'Sandbox'    => 'Sandbox',
71                    'Production' => 'Production',
72                )
73                : array(
74                    'Sandbox'    => 'Sandbox',
75                    'Production' => 'Production',
76                ),
77                'label'    => __( 'Environment', 'snap-marketing' ),
78                'readonly' => '',
79            ),
80            
81            /*
82            'snap_marketing_estimate'        => array(
83                'type'    => 'select',
84                'value'   => '',
85                'options' => $this->Treatment_Estimate,
86                'label'   => __( 'As Low As Frequency', 'snap-marketing' ),
87                'readonly' => ''
88            ),*/
89            'snap_marketing_dev_id'         => array(
90                'type'     => 'text',
91                'value'    => '',
92                'label'    => __( 'Dev Client ID', 'snap-marketing' ),
93                'readonly' => '',
94            ),
95            'snap_marketing_dev_secret_key' => array(
96                'type'     => 'password',
97                'value'    => '',
98                'label'    => __( 'Dev Secret Key', 'snap-marketing' ),
99                'readonly' => '',
100            ),
101            'snap_marketing_qa_id'         => array(
102                'type'     => 'text',
103                'value'    => '',
104                'label'    => __( 'QA Client ID', 'snap-marketing' ),
105                'readonly' => '',
106            ),
107            'snap_marketing_qa_secret_key' => array(
108                'type'     => 'password',
109                'value'    => '',
110                'label'    => __( 'QA Secret Key', 'snap-marketing' ),
111                'readonly' => '',
112            ),
113            'snap_marketing_sandbox_id'         => array(
114                'type'     => 'text',
115                'value'    => '',
116                'label'    => __( 'Sandbox Client ID', 'snap-marketing' ),
117                'readonly' => '',
118            ),
119            'snap_marketing_sandbox_secret_key' => array(
120                'type'     => 'password',
121                'value'    => '',
122                'label'    => __( 'Sandbox Secret Key', 'snap-marketing' ),
123                'readonly' => '',
124            ),
125            'snap_marketing_live_id'            => array(
126                'type'     => 'text',
127                'value'    => '',
128                'label'    => __( 'Production Client ID', 'snap-marketing' ),
129                'readonly' => '',
130            ),
131            'snap_marketing_live_secret_key'    => array(
132                'type'     => 'password',
133                'value'    => '',
134                'label'    => __( 'Production Secret Key', 'snap-marketing' ),
135                'readonly' => '',
136            ),
137        );
138
139        $this->Treatment_Settings = array(
140            'Snap_TreatmentType'   => 'Treatment Type',
141            'Snap_TreatmentLogo'   => 'Logo URL',
142            'Snap_TreatmentActive' => 'Active?',
143            'Shortcode'            => 'Shortcode',
144        );
145
146        add_filter( 'snap_marketing_treatment_types', array( $this, 'Treatment_Types' ) );
147        add_filter( 'snap_marketing_treatment_Logo', array( $this, 'Treatment_Logo' ) );
148        add_filter( 'snap_marketing_treatment_Active', array( $this, 'Treatment_Active' ) );
149
150
151        // We need custom JavaScript to obtain a token
152        add_action( 'admin_enqueue_scripts', array( $this, 'snap_marketing_scripts' ) );
153        add_action( 'admin_menu', array( $this, 'create_configuration_page' ) );
154        add_action( 'init', array( $this, 'create_treatments_post_type' ) );
155        add_action( 'add_meta_boxes', array( $this, 'create_treatments_post_type_extra_options' ) );
156        add_action( 'save_post', array( $this, 'save_create_treatments_post_type' ) );
157        add_action( 'admin_init', array( $this, 'register_marketing_settings' ) );
158        add_action( 'wp_ajax_reset_marketing_token', array( $this, 'snap_marketing_reset_token' ) );
159        add_filter( 'manage_snap_treatments_posts_columns', array( $this, 'treatment_columns_head' ) );
160        add_action( 'manage_snap_treatments_posts_custom_column', array( $this, 'treatment_columns_content' ), 10, 2 );
161
162        
163    }
164
165    /*
166     * Reset Snap marketing token on site
167     */
168    public function snap_marketing_reset_token() {
169        delete_transient( 'snap_marketing_token' );
170    }
171
172    /*
173     * Add treatment setting columns on treatment post list page
174     */
175    public function treatment_columns_head( $defaults ) {
176        $date_column = $defaults['date'];
177        unset( $defaults['date'] );
178        foreach ( $this->Treatment_Settings as $Treatment_Setting_Key => $Treatment_Setting_Heading ) {
179            $defaults[ $Treatment_Setting_Key ] = $Treatment_Setting_Heading;
180        }
181        $defaults['date'] = $date_column;
182
183        return $defaults;
184    }
185
186    /*
187     * Add treatment setting columns value on treatment post list page
188     */
189    public function treatment_columns_content( $column_name, $treatment_ID ) {
190        foreach ( $this->Treatment_Settings as $Treatment_Setting_Key => $Treatment_Setting_Heading ) {
191            if ( $column_name == $Treatment_Setting_Key ) {
192                switch ( $Treatment_Setting_Key ) {
193                    case 'Shortcode':
194                        $treatment_Type = $this->get_treatment_type( get_post_meta( $treatment_ID, 'Snap_TreatmentType', true ) );
195                        $treatment_Type = strtolower( str_replace( array( ' - ', ' – ', ' ' ), array( '_', '_', '_' ), $this->Treatment_Types[ $treatment_Type ] ) );
196                        echo sprintf( __( '[snap_treatment_add_%1$s treat_id="%2$s" ]', 'snap-marketing' ), $treatment_Type, $treatment_ID );
197                        break;
198                    case 'Snap_TreatmentLogo':
199                        echo sprintf( __( '<img class="img_%1$s" src="%2$s" />', 'snap-marketing' ), $Treatment_Setting_Key, get_post_meta( $treatment_ID, $Treatment_Setting_Key, true ) );
200                        break;
201                    case 'Snap_TreatmentType':
202                        $Snap_TreatmentType = $this->get_treatment_type( get_post_meta( $treatment_ID, $Treatment_Setting_Key, true ) );
203                        echo esc_html( $this->Treatment_Types[ $Snap_TreatmentType ] );
204                        break;
205                    default:
206                        echo esc_html( get_post_meta( $treatment_ID, $Treatment_Setting_Key, true ) );
207                        break;
208                }
209            }
210        }
211
212    }
213
214    /*
215     * Set Default treatment type
216     */
217    public function get_treatment_type( $type ) {
218        if ( empty( $type ) ) {
219            $type = 'PRE_APPROVAL';
220        }
221        return $type;
222    }
223
224    /*
225     * Register configuration options
226     */
227    public function register_marketing_settings() {
228
229        $args = array(
230            'type'              => 'string',
231            'sanitize_callback' => 'sanitize_text_field',
232            'default'           => null,
233        );
234
235        
236        register_setting( 'snap-marketing-configuration', 'active', $args );
237        register_setting( 'snap-marketing-configuration', 'title', $args );
238        register_setting( 'snap-marketing-configuration', 'description', $args );
239        register_setting( 'snap-marketing-configuration', 'environment', $args );
240        /*register_setting( 'snap-marketing-configuration', 'estimate' , $args );*/
241        register_setting( 'snap-marketing-configuration', 'sandbox_id', $args );
242        register_setting( 'snap-marketing-configuration', 'sandbox_secret_key', $args );
243        register_setting( 'snap-marketing-configuration', 'dev_id', $args );
244        register_setting( 'snap-marketing-configuration', 'dev_secret_key', $args );
245        register_setting( 'snap-marketing-configuration', 'qa_id', $args );
246        register_setting( 'snap-marketing-configuration', 'qa_secret_key', $args );
247        register_setting( 'snap-marketing-configuration', 'live_id', $args );
248        register_setting( 'snap-marketing-configuration', 'live_secret_key', $args );
249
250
251        
252        $this->validate_token();
253
254        
255        if ( isset( $_POST['option_page'] ) ) {
256            if ( $_POST['option_page'] == 'snap-marketing-configuration' ) {
257                foreach ( $this->Configuration_Options as $configuration_option_key => $Configuration_Options_Value ) {
258                    update_option( $configuration_option_key, sanitize_text_field( $_POST[ $configuration_option_key ] ) );
259                }
260            }
261        }
262    }
263
264    /*
265     * Save treatment extra options
266     */
267    public function save_create_treatments_post_type( $post_id ) {
268
269        if ( ! current_user_can( 'edit_post', $post_id ) ) {
270            return;
271        }
272
273        if ( isset( $_POST['post_type'] ) && 'snap_treatments' === $_POST['post_type'] && wp_verify_nonce( $_POST['nonce'], 'snap_treatment_details' ) ) {
274            update_post_meta( $post_id, 'Snap_TreatmentType', sanitize_text_field( $_POST['TreatmentType'] ) );
275            update_post_meta( $post_id, 'Snap_TreatmentLogo', sanitize_text_field( $_POST['TreatmentLogo'] ) );
276            update_post_meta( $post_id, 'Snap_TreatmentActive', sanitize_text_field( $_POST['TreatmentActive'] ) );
277            update_post_meta( $post_id, 'Snap_TreatmentAlignment', sanitize_text_field( $_POST['TreatmentAlignment'] ) );
278            if ( isset( $_POST['Snap_Product_Active'] ) ) {
279                update_option( 'Snap_Product_Active', sanitize_text_field( $_POST['Snap_Product_Active'] ) );
280            } else {
281                $Snap_Product_Active = get_option( 'Snap_Product_Active' );
282                if ( $Snap_Product_Active == $post_id ) {
283                    update_option( 'Snap_Product_Active', '' );
284                }
285            }
286        }
287
288    }
289
290    /*
291     * Create custom extra option meta box for treatment post type
292     */
293    public function create_treatments_post_type_extra_options() {
294        add_meta_box(
295            'snap-treatments-extra-options',
296            __( 'Treatments Settings', 'snap-marketing' ),
297            array( $this, 'treatment_extra_options' ),
298            'snap_treatments'
299        );
300    }
301
302    /*
303     * Get Logo URls from snap finace server side
304     */
305    public function treatment_logo_urls() {
306        $Treatment_Logo = array();
307        // Get all logos URL from snapfinance server by URL
308        $sand_box_urls     = constant( 'Snap_Marketing_Logo_URL' );
309        $response_xml_data = wp_remote_get( $sand_box_urls );
310        if ( is_array( $response_xml_data ) && ! is_wp_error( $response_xml_data ) ) {
311            $response_xml_data = (array) $response_xml_data;
312            $response_xml_data = simplexml_load_string( $response_xml_data['body'] );
313            if ( $response_xml_data ) {
314
315                foreach ( $response_xml_data->Contents as $Contents ) {
316                    $value_text = (string) $Contents->Key;
317                    $value_name = basename( $value_text );
318                    $value_name = explode( '.', $value_name );
319                    if ( count( $value_name ) > 1 ) {
320                        $value_name = str_replace( array( '-', '_' ), ' ', $value_name[0] );
321                        if ( strpos( $Contents->Key, 'aslowas' ) !== false && $value_text ) {
322                            $Treatment_Logo[ $sand_box_urls . $value_text ]            = array(
323                                'name' => $value_name,
324                                'type' => 'get_approved_as_low_as',
325                            );
326                            $Treatment_Logo[ $sand_box_urls . $value_text . '?ver=1' ] = array(
327                                'name' => $value_name,
328                                'type' => 'prequalification_as_low_as',
329                            );
330                        }
331                        if ( strpos( $Contents->Key, 'ecomm-plugin-banners' ) !== false && $value_text ) {
332                            $Treatment_Logo[ $sand_box_urls . $value_text ] = array(
333                                'name' => $value_name,
334                                'type' => 'banner',
335                            );
336                        }
337                        if ( strpos( $Contents->Key, 'getapproved' ) !== false && $value_text ) {
338                            $Treatment_Logo[ $sand_box_urls . $value_text ]            = array(
339                                'name' => $value_name,
340                                'type' => 'get_approved',
341                            );
342                            $Treatment_Logo[ $sand_box_urls . $value_text . '?ver=1' ] = array(
343                                'name' => $value_name,
344                                'type' => 'prequalification',
345                            );
346                        }
347                    }
348                }
349            }
350        }
351        return $Treatment_Logo;
352    }
353
354    /*
355     * Treatment extra option seting fields
356     */
357    public function treatment_extra_options( $meta_id ) {
358        $outline             = '<table>';
359        $outline            .= '<tr><th><label>' . esc_html__( 'Treatment Type', 'snap-marketing' ) . '</label></th>';
360        $TreatmentType_value = get_post_meta( $meta_id->ID, 'Snap_TreatmentType', true );
361        $outline            .= '<td><select type="text" name="TreatmentType" id="TreatmentType" class="TreatmentType" />';
362        foreach ( $this->Treatment_Types as $TreatmentType_Key => $TreatmentType ) {
363            $selected = '';
364            if ( $TreatmentType_Key == $TreatmentType_value ) {
365                $selected = 'selected';
366            }
367            $outline .= '<option value="' . esc_attr($TreatmentType_Key) . '" ' . esc_attr($selected) . ' >' . esc_html($TreatmentType) . '</option>';
368        }
369        $outline .= '</select></td></tr>';
370        $outline .= '<tr><th><label>' . esc_html__( 'Description', 'snap-marketing' ) . '</label></th>';
371        $outline .= '<td>';
372        $outline .= '<div class="get_approved_as_low_as_desc desciption_box">';
373        $outline .= __( '<b>Offer contextual financing experience with “Get Approved – As low as? treatment and banners</b><br/>Enable the “Get Approved – As low as? promotional treatment and get enhanced benefits of the “Get Approved? but customized for your product pages. Place the treatment in your product pages to give customers a glimpse of what their payments for may look like with Snap. Our research has shown that customers are more likely to apply and transact if they have an idea of how much their payments will look like with Snap.', 'snap-marketing' );
374        $outline .= '</div>';
375        $outline .= '<div class="prequalification_as_low_as_desc desciption_box">';
376        $outline .= __( '<b>Offer contextual financing experience with "Prequalification - As low as" treatment and banners</b><br/>Enable the "Prequalification - As low as" promotional treatment and get enhanced benefits of the "Get Approved" but customized for your product pages. Place the treatment in your product pages to give customers a glimpse of what their payments for may look like with Snap. Our research has shown that customers are more likely to apply and transact if they have an idea of how much their payments will look like with Snap.', 'snap-marketing' );
377        $outline .= '</div>';
378        $outline .= '<div class="banner_desc desciption_box">';
379
380        $outline               .= '</div>';
381        $outline               .= '<div class="prequalification_desc desciption_box">';
382        $outline               .= __( '<b>Capture customers at top of funnel with "Prequalification" treatment</b><br/>Enable the "Prequalification" promotional treatment on any pages of your webstore. Let customers know they have a financing option, from the start, to turn browsers into actual customers. With the preapproval application flow, if approved, customers will know exactly how much they have been approved for, giving them the motivation to transact for a higher shopping cart value on your store. As research shows, when customers know they have an approval and the amount, not only are they likely to purchase but purchase more items or a higher price point item ', 'snap-marketing' );
383        $outline               .= '</div>';
384        $outline               .= '<div class="get_approved_desc desciption_box">';
385        $outline               .= __( '<b>Capture customers at top of funnel with “Get Approved? treatment and banners</b><br/>Enable the “Get Approved? promotional treatment on any pages of your webstore. Let customers know they have a financing option, from the start, to turn browsers into actual customers. With the preapproval application flow, if approved, customers will know exactly how much they have been approved for, giving them the motivation to transact for a higher shopping cart value on your store. As research shows, when customers know they have an approval and the amount, not only are they likely to purchase but purchase more items or a higher price point item. ', 'snap-marketing' );
386        $outline               .= '</div>';
387        $outline               .= '</td></tr>';
388        $outline               .= '<tr><th><label>' . esc_html__( 'Active?', 'snap-marketing' ) . '</label></th>';
389        $Treatment_Active_value = get_post_meta( $meta_id->ID, 'Snap_TreatmentActive', true );
390        $outline               .= '<td><select type="text" name="TreatmentActive" id="TreatmentActive" class="TreatmentActive" />';
391        foreach ( $this->Treatment_Active as $Treatment_Active ) {
392            $selected = '';
393            if ( $Treatment_Active == $Treatment_Active_value ) {
394                $selected = 'selected';
395            }
396            $outline .= '<option value="' . esc_attr($Treatment_Active) . '" ' . esc_attr($selected) . ' >' . esc_html($Treatment_Active) . '</option>';
397        }
398        $outline             .= '</select></td></tr>';
399        $outline             .= '<tr class="hide_active" ><th><label>' . esc_html__( 'Logo URL', 'snap-marketing' ) . '</label></th>';
400        $Marketing_Logo_value = get_post_meta( $meta_id->ID, 'Snap_TreatmentLogo', true );
401        $outline             .= '<td><select type="text" name="TreatmentLogo" id="TreatmentLogo" class="TreatmentLogo"/>';
402        foreach ( $this->Treatment_Logo as $Treatment_Logo_URL => $Treatment_Logo_Name ) {
403            $selected = '';
404            if ( $Treatment_Logo_URL == $Marketing_Logo_value ) {
405                $selected = 'selected';
406            }
407
408            $outline .= '<option value="' . esc_attr(esc_url($Treatment_Logo_URL)) . '" ' . esc_attr($selected) . ' data-type="' . esc_attr($Treatment_Logo_Name['type']) . '" >' . esc_html($Treatment_Logo_Name['name']) . '</option>';
409        }
410        $outline            .= '</select><div class="snap_marketing_banner"><img id="snap_marketing_banner" attr="Snap Marketing Banner" /></div></td></tr>';
411        $outline            .= '<tr class="enable_in_all_product" ><th><label>' . esc_html__( 'Enable in all product description pages?', 'snap-marketing' ) . '</label></th>';
412        $Snap_Product_Active = get_option( 'Snap_Product_Active' );
413        $outline            .= '<td><input type="checkbox" name="Snap_Product_Active" value="' . esc_attr($meta_id->ID) . '" ';
414        if ( $meta_id->ID == $Snap_Product_Active ) {
415            $outline .= 'checked';
416        }
417        $outline                  .= ' /></td></tr>';
418        $outline                  .= '<tr class="hide_active" ><th><label>' . esc_html__( 'Alignment', 'snap-marketing' ) . '</label></th>';
419        $Treatment_Alignment_value = get_post_meta( $meta_id->ID, 'Snap_TreatmentAlignment', true );
420        $outline                  .= '<td><select type="text" name="TreatmentAlignment" id="TreatmentAlignment" class="TreatmentAlignment" />';
421        foreach ( $this->Treatment_Alignment as $Treatment_Alignment ) {
422            $selected = '';
423            if ( $Treatment_Alignment == $Treatment_Alignment_value ) {
424                $selected = 'selected';
425            }
426            $outline .= '<option value="' . esc_attr($Treatment_Alignment) . '" ' . esc_attr($selected) . ' >' . esc_html($Treatment_Alignment) . '</option>';
427        }
428        $outline .= '</select></td></tr>';
429
430        $outline               .= '<tr class="hide_active" > <th><label>' . esc_html__( 'Shortcode', 'snap-marketing' ) . '</label></th>';
431        $Treatment_Active_value = get_post_meta( $meta_id->ID, 'Snap_TreatmentActive', true );
432        $outline               .= '<td class="shortcode-text" >[snap_treatment_add treat_id="' . $meta_id->ID . '" ]</td></tr>';
433
434        $outline .= '</table>';
435        $outline .= '<input type="hidden" name="nonce" value="' . esc_attr(wp_create_nonce( 'snap_treatment_details' )) . '" >';
436        echo __( $outline );
437    }
438
439    /**
440     * Adds a submenu page snap marketing a Treatment post type parent.
441     */
442    public function create_configuration_page() {
443        add_submenu_page(
444            'edit.php?post_type=snap_treatments',
445            __( 'Configuration', 'snap-marketing' ),
446            __( 'Configuration', 'snap-marketing' ),
447            'manage_options',
448            'snap-marketing-configuration',
449            array( $this, 'snap_marketing_configuration_settings' )
450        );
451    }
452
453    /*
454     * Create snap marketing configuration settings page
455     */
456    public function snap_marketing_configuration_settings() {
457        register_setting( 'snap-marketing-configuration', 'active' );
458        register_setting( 'snap-marketing-configuration', 'title' );
459        register_setting( 'snap-marketing-configuration', 'description' );
460        register_setting( 'snap-marketing-configuration', 'environment' );
461        register_setting( 'snap-marketing-configuration', 'sandbox_id' );
462        register_setting( 'snap-marketing-configuration', 'sandbox_secret_key' );
463        register_setting( 'snap-marketing-configuration', 'live_id' );
464        register_setting( 'snap-marketing-configuration', 'live_secret_key' );
465
466        ?>
467        <div class="wrap">
468            <h1><?php _e( 'Snap Marketing Configuration', 'snap-marketing' ); ?></h1>
469            <form method="post" class="snap-marketing" action="options.php">
470                <?php settings_fields( 'snap-marketing-configuration' ); ?>
471                <?php do_settings_sections( 'snap-marketing-configuration' ); ?>
472                <table class="form-table">
473                    <?php
474                    foreach ( $this->Configuration_Options as $snap_marketing_configuration_id => $snap_marketing_configuration_value ) {
475                        switch ( $snap_marketing_configuration_value['type'] ) {
476                            case 'text':
477                                $this->configuration_input_fields( $snap_marketing_configuration_value, $snap_marketing_configuration_id );
478                                break;
479                            case 'password':
480                                $this->configuration_input_fields( $snap_marketing_configuration_value, $snap_marketing_configuration_id );
481                                break;
482                            case 'textarea':
483                                $this->configuration_textarea_fields( $snap_marketing_configuration_value, $snap_marketing_configuration_id );
484                                break;
485                            case 'select':
486                                $this->configuration_select_fields( $snap_marketing_configuration_value, $snap_marketing_configuration_id );
487                                break;
488                            case 'checkbox':
489                                $this->configuration_checkbox_fields( $snap_marketing_configuration_value, $snap_marketing_configuration_id );
490                                break;
491                        }
492                    }
493                    ?>
494                </table>
495
496                <?php submit_button(); ?>
497
498            </form>
499        </div>
500        <?php
501    }
502
503    /*
504     * Create text filed for snap marketing configuration option
505     */
506    public function configuration_input_fields( $option_values, $option_id ) {
507        ?>
508        <tr valign="top" id="<?php echo esc_attr( $option_id ); ?>_tr">
509            <th scope="row"><?php echo esc_html( $option_values['label'] ); ?></th>
510            <td><input type="<?php echo esc_attr( $option_values['type'] ); ?><?php echo esc_attr( $option_values['readonly'] ); ?> name="<?php echo esc_attr( $option_id ); ?>"
511                value="<?php echo esc_attr( get_option( $option_id, $option_values['value'] ) ); ?>"/></td>
512            </tr>
513            <?php
514    }
515    /*
516     * Create text filed for snap marketing configuration option
517     */
518    public function configuration_textarea_fields( $option_values, $option_id ) {
519        ?>
520        <tr valign="top" id="<?php echo $option_id; ?>_tr">
521            <th scope="row">
522                <?php echo esc_html( $option_values['label'] ); ?>
523            </th>
524            <td>
525                <textarea rows="10" <?php echo esc_attr( $option_values['readonly'] ); ?>
526                name="<?php echo esc_attr( $option_id ); ?>"><?php echo esc_attr( $option_values['value'] ); ?></textarea>
527            </td>
528        </tr>
529        <?php
530    }
531
532    /*
533     * Create text filed for snap marketing configuration option
534     */
535    public function configuration_checkbox_fields( $option_values, $option_id ) {
536        ?>
537        <tr valign="top" id="<?php echo esc_attr( $option_id ); ?>_tr">
538            <th scope="row"><?php echo esc_html( $option_values['label'] ); ?></th>
539            <td>
540                <label for="<?php echo esc_attr( $option_id ); ?>">
541                    <input class="" type="checkbox" name="<?php echo esc_attr( $option_id ); ?>"
542                    id="<?php echo esc_attr( $option_id ); ?>"
543                    value="on" 
544                    <?php
545                    if ( get_option( $option_id, $option_values['value'] ) == 'on' ) {
546                        echo esc_attr( 'checked' );
547                    }
548                    ?>
549                    >
550                    <?php echo esc_html( $option_values['details'] ); ?>
551                </label>
552            </tr>
553            <?php
554    }
555
556    /*
557     * Create text filed for snap marketing configuration option
558     */
559    public function configuration_select_fields( $option_values, $option_id ) {
560        $select_value = get_option( $option_id, $option_values['value'] );
561
562        ?>
563        <tr valign="top" id="<?php echo esc_attr( $option_id ); ?>_tr">
564            <th scope="row"><?php echo esc_attr( $option_values['label'] ); ?></th>
565            <td>
566                <select id="<?php echo esc_attr( $option_id ); ?><?php echo esc_attr( $option_values['readonly'] ); ?> name="<?php echo esc_attr( $option_id ); ?>">
567                    <?php
568                    foreach ( $option_values['options'] as $option_key => $option_value ) {
569                        $select_data = '';
570                        if ( $select_value == $option_key ) {
571                            $select_data = 'selected';
572                        }
573                        echo '<option value="' . esc_attr( $option_key ) . '" ' . esc_attr( $select_data ) . ' > ' . esc_html( $option_value ) . ' </option>';
574                    }
575                    ?>
576                </select>
577            </td>
578        </tr>
579        <?php
580    }
581
582    /*
583    * Register treatments post type on admin side
584    */
585    public function create_treatments_post_type() {
586
587        // Set UI labels for Treatments Post Type
588        $labels = array(
589            'name'               => _x( 'Treatments', 'Post Type General Name', 'snap-marketing' ),
590            'singular_name'      => _x( 'Treatment', 'Post Type Singular Name', 'snap-marketing' ),
591            'menu_name'          => __( 'Snap Marketing', 'snap-marketing' ),
592            'parent_item_colon'  => __( 'Parent Treatment', 'snap-marketing' ),
593            'all_items'          => __( 'All Treatments', 'snap-marketing' ),
594            'view_item'          => __( 'View Treatment', 'snap-marketing' ),
595            'add_new_item'       => __( 'Add New Treatment', 'snap-marketing' ),
596            'add_new'            => __( 'Add New', 'snap-marketing' ),
597            'edit_item'          => __( 'Edit Treatment', 'snap-marketing' ),
598            'update_item'        => __( 'Update Treatment', 'snap-marketing' ),
599            'search_items'       => __( 'Search Treatment', 'snap-marketing' ),
600            'not_found'          => __( 'Not Found', 'snap-marketing' ),
601            'not_found_in_trash' => __( 'Not found in Trash', 'snap-marketing' ),
602        );
603
604        // Set other options for Treatments Post Type
605
606        $args = array(
607            'label'               => __( 'Treatments', 'snap-marketing' ),
608            'description'         => __( 'Snap Treatments', 'snap-marketing' ),
609            'labels'              => $labels,
610            // Features this Treatments supports in Post Editor
611            'supports'            => array( 'title' ),
612            /*
613             A hierarchical Treatments is like Pages and can have
614            * Parent and child items. A non-hierarchical Treatments
615            * is like Posts.
616            */
617            'hierarchical'        => false,
618            'public'              => true,
619            'show_ui'             => true,
620            'show_in_menu'        => true,
621            'show_in_nav_menus'   => true,
622            'show_in_admin_bar'   => true,
623            'menu_position'       => 5,
624            'can_export'          => true,
625            'has_archive'         => true,
626            'exclude_from_search' => false,
627            'publicly_queryable'  => true,
628            'capability_type'     => 'post',
629            'show_in_rest'        => true,
630
631        );
632
633        // Registering treatments Post Type
634        register_post_type( 'snap_treatments', $args );
635    }
636
637    /*
638     * Custom CSS and JS, in most cases required only when you decided to go with a custom credit card form
639     */
640    public function snap_marketing_scripts() {
641
642        global $post;
643        $load_style = false;
644        if ( isset( $_GET['page'] ) ) {
645            if ( $_GET['page'] == 'snap-marketing-configuration' ) {
646                $load_style = true;
647            }
648        }
649        if ( isset( $_GET['post_type'] ) ) {
650            if ( $_GET['post_type'] == 'snap_treatments' ) {
651                $load_style = true;
652            }
653        }
654        if ( $post ) {
655            if ( $post->post_type == 'snap_treatments' ) {
656                $load_style = true;
657            }
658        }
659        if ( $load_style ) {
660            wp_enqueue_script( 'snap-marketing-admin-script', plugins_url( 'assets/js/snap-marketing-admin.js', __FILE__ ), array( 'jquery' ), 1.1, true );
661            wp_localize_script( 'snap-marketing-admin-script', 'snap_marketing', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
662            wp_enqueue_style( 'snap-marketing-admin-style', plugins_url( 'assets/css/snap-marketing-admin.css', __FILE__ ), array(), '', false );
663        }
664    }
665
666public function validate_token() {
667
668    if ( ! isset($_POST['submit']) || $_POST['submit'] !== 'Save Changes' ) {
669        return false; // STOP if not submitting form
670    }
671
672    delete_transient('snap_admin_error');
673    delete_transient('snap_admin_success');
674
675    // Sanitize mode
676    $mode = isset($_POST['snap_marketing_environment'])
677        ? sanitize_text_field($_POST['snap_marketing_environment'])
678        : 'Sandbox';
679
680    $allowed_modes = ['Sandbox', 'DEV', 'QA', 'Production'];
681    if (!in_array($mode, $allowed_modes, true)) {
682        $mode = 'Sandbox';
683    }
684
685    // Set client_id / client_secret based on mode
686    switch ($mode) {
687
688        case 'Sandbox':
689            $api_url      = constant('SANDBOX_API_URL');
690            $audience_url = constant('TRAINING_AUDIENCE_URL');
691            $client_id     = sanitize_text_field($_POST['snap_marketing_sandbox_id'] ?? '');
692            $client_secret = sanitize_text_field($_POST['snap_marketing_sandbox_secret_key'] ?? '');
693            break;
694
695        case 'dev':
696            $api_url      = constant('DEV_API_URL');
697            $audience_url = constant('DEV_AUDIENCE_URL');
698            $client_id     = sanitize_text_field($_POST['snap_marketing_dev_id'] ?? '');
699            $client_secret = sanitize_text_field($_POST['snap_marketing_dev_secret_key'] ?? '');
700            break;
701
702        case 'qa':
703            $api_url      = constant('QA_API_URL');
704            $audience_url = constant('QA_AUDIENCE_URL');
705            $client_id     = sanitize_text_field($_POST['snap_marketing_qa_id'] ?? '');
706            $client_secret = sanitize_text_field($_POST['snap_marketing_qa_secret_key'] ?? '');
707            break;
708
709        case 'live':
710        default:
711            $api_url      = constant('LIVE_API_URL');
712            $audience_url = constant('LIVE_AUDIENCE_URL');
713            $client_id     = sanitize_text_field($_POST['snap_marketing_live_id'] ?? '');
714            $client_secret = sanitize_text_field($_POST['snap_marketing_live_secret_key'] ?? '');
715            break;
716    }
717
718
719    // Build API request
720    $response = wp_remote_post($api_url, [
721        'headers' => ['Content-Type' => 'application/json'],
722        'body'    => json_encode([
723            'client_id'     => $client_id,
724            'client_secret' => $client_secret,
725            'audience'      => $audience_url,
726            'grant_type'    => 'client_credentials',
727        ]),
728        'timeout' => 15,
729    ]);
730
731    // Connection error
732    if (is_wp_error($response)) {
733        set_transient('snap_admin_error', 'Unable to connect to the API. Please check your credentials or try again.', 20);
734        return false; // ← STOP
735    }
736
737    $code = wp_remote_retrieve_response_code($response);
738    $body = json_decode(wp_remote_retrieve_body($response), true);
739
740    // Invalid response
741    if ($code !== 200 || empty($body['access_token'])) {
742        set_transient('snap_admin_error', 'Invalid credentials. Please verify your Client ID and Client Secret.', 20);
743        return false; // ← STOP
744    }
745
746    // SUCCESS
747    set_transient('snap_admin_success', 'Settings saved and credentials verified successfully.', 20);
748
749    return true;
750}
751
752
753
754}
755
756$snap_marketing_admin_side = new snap_marketing_admin_side();