<?php

/*
 Plugin Name: Simple checkout page donations/tips for WooCommerce
 Plugin URI: https://profiles.wordpress.org/theorcawp
 Description: This plugin lets you add some doantion options to the checkout page.
 Author: theorcawp
 Author URI: https://orcawp.com
 text Domain: simple-checkout-page-donationstips-for-woocommerce
 Version: 1.4
 License: GPLv2 or later
 License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

if ( ! function_exists( 'scpdtfw_fs' ) ) {
    // Create a helper function for easy SDK access.
    function scpdtfw_fs() {
        global $scpdtfw_fs;

        if ( ! isset( $scpdtfw_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/freemius/start.php';
            $scpdtfw_fs = fs_dynamic_init( array(
                'id'                  => '20468',
                'slug'                => 'simple-checkout-page-donations-tips-for-woocommerce',
                'type'                => 'plugin',
                'public_key'          => 'pk_253c2360f48216089e2a35f03eca7',
                'is_premium'          => false,
                'has_addons'          => false,
                'has_paid_plans'      => false,
                'menu'                => array(
                    'first-path'     => 'plugins.php',
                    'account'        => false,
                    'contact'        => false,
                    'support'        => false,
                ),
            ) );
        }

        return $scpdtfw_fs;
    }

    // Init Freemius.
    scpdtfw_fs();
    // Signal that SDK was initiated.
    do_action( 'scpdtfw_fs_loaded' );
}

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

function wc_tipme_section( $sections ) {
    $sections['wc_tipme_section'] = esc_html__( 'Simple donations/tips for WooCommerce', 'simple-checkout-page-donationstips-for-woocommerce' );
    return $sections;
}

add_filter( 'woocommerce_get_sections_products', 'wc_tipme_section' );


function wc_tipme_settings( $settings, $current_section ) {

    if ( 'wc_tipme_section' === $current_section ) {

        $wc_tipme_settings[] = array( 'title' => esc_html__( 'Tips', 'simple-checkout-page-donationstips-for-woocommerce' ), 'type' => 'title', 'id' => 'wc_tipme' );

        $wc_tipme_settings[] = array(
                'name'     => esc_html__( 'Enable ', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'type' => 'checkbox',
                'desc'     => esc_html__( 'Enable the plugin', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'id'       => 'wc_tipme_enable',
                'desc_tip' => true
            );

        $wc_tipme_settings[] = array(
                'name'     => esc_html__( 'Label ', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'type' => 'text',
                'desc'     => esc_html__( 'Add your own label, such as "Want to give me a tip?"', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'id'       => 'wc_tipme_label',
            );

        $wc_tipme_settings[] = array(
                'title'    => esc_html__( 'Tip option 1', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'desc' => esc_html__( 'Add your preferred tip amount here', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'id'       => 'wc_tipme_option_1',
                'type'     => 'number',
                'placeholder' => '2',
                'css'      => 'min-width:250px;',
                'custom_attributes' => array(
                    'min'  => '0',  // no negatives allowed
                    'step' => '1',  // only integers, change to 0.01 if decimals allowed
                ),
                'default' => 2,
            );

        $wc_tipme_settings[] = array(
                'title'    => esc_html__( 'Tip option 2', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'desc' => esc_html__( 'Add your preferred tip amount here', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'id'       => 'wc_tipme_option_2',
                'type'     => 'number',
                'placeholder' => '5',
                'css'      => 'min-width:350px;',
                'custom_attributes' => array(
                    'min'  => '0',  // no negatives allowed
                    'step' => '1',  // only integers, change to 0.01 if decimals allowed
                ),
                'default' => 5,
            );

        $wc_tipme_settings[] = array(
                'title'    => esc_html__( 'Tip option 3', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'desc' => esc_html__( 'Add your preferred tip amount here', 'simple-checkout-page-donationstips-for-woocommerce' ),
                'id'       => 'wc_tipme_option_3',
                'type'     => 'number',
                'placeholder' => '10',
                'css'      => 'min-width:350px;',
                'custom_attributes' => array(
                    'min'  => '0',  // no negatives allowed
                    'step' => '1',  // only integers, change to 0.01 if decimals allowed
                ),
                'default' => 10,
            );

        $wc_tipme_settings[] = array( 'type' => 'sectionend', 'id' => 'wc_tipme' );
        return $wc_tipme_settings;
} else {
        return $settings;
    }

}

if( get_option('wc_tipme_enable', true )=='yes') {
  
add_action( 'woocommerce_review_order_before_payment', 'wc_tipme_radio_choice' );
  
function wc_tipme_radio_choice() {
     
   $chosen = WC()->session->get( 'radio_chosen' );
   $chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
   $chosen = empty( $chosen ) ? '0' : $chosen;
   $chosen = '0';
        
   $args = array(
   'type' => 'radio',
   'class' => array( 'form-row-wide', 'update_totals_on_change' ),
   'options' => array( get_option('wc_tipme_option_1') => ('$') . get_option('wc_tipme_option_1'), get_option('wc_tipme_option_2') => ('$') . get_option('wc_tipme_option_2'), get_option('wc_tipme_option_3') => ('$') . get_option('wc_tipme_option_3'), '0' => 'No thanks',),
   'default' => $chosen
   );
     
   echo '<div id="checkout-radio">';
   echo '<h4>' . esc_html( get_option('wc_tipme_label') ) . '</h4>';
   woocommerce_form_field( 'radio_choice', $args, $chosen );
   echo '</div>';
     
}

add_action( 'woocommerce_cart_calculate_fees', 'wc_tipme_checkout_radio_choice_fee', 20, 1 );
  
function wc_tipme_checkout_radio_choice_fee( $cart ) {
   
   if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
   $radio = WC()->session->get( 'radio_chosen' );
     
   if ( $radio ) {
      $cart->add_fee( 'Tip', $radio );
   }
   
}

add_action( 'woocommerce_checkout_update_order_review', 'wc_tipme_checkout_radio_choice_set_session' );
  
function wc_tipme_checkout_radio_choice_set_session( $posted_data ) {
    parse_str( $posted_data, $output );
    if ( isset( $output['radio_choice'] ) ){
        WC()->session->set( 'radio_chosen', $output['radio_choice'] );
        }
    }
}

function wc_tipme_adding_styles() {
wp_register_style('tipme', plugins_url('/assets/css/tipme.css', __FILE__), array(), '1.0', 'all');
wp_enqueue_style('tipme');
}

add_action( 'wp_enqueue_scripts', 'wc_tipme_adding_styles' );  
add_filter( 'woocommerce_get_settings_products','wc_tipme_settings', 10, 2 );
}
