Advertisement
rejuancse

dsfd

Sep 21st, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.11 KB | None | 0 0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3.     exit; // Exit if accessed directly
  4. }
  5.  
  6. if (! class_exists('Wpneo_Crowdfunding')) {
  7.  
  8.     class Wpneo_Crowdfunding{
  9.  
  10.         protected static $_instance = null;
  11.         public static function instance() {
  12.             if ( is_null( self::$_instance ) ) {
  13.                 self::$_instance = new self();
  14.             }
  15.             return self::$_instance;
  16.         }
  17.  
  18.         public function __construct(){
  19.             include_once plugin_dir_path(__FILE__).'class-wpneo-wc-reward.php';
  20.             add_action( 'plugins_loaded',                                   array($this, 'includes')); //Include all of resource to the plugin
  21.             add_filter( 'product_type_selector',                            array($this, 'wpneo_product_type_selector')); //Added one more product type in woocommerce product
  22.             add_action( 'init',                                             array($this, 'wpneo_register_product_type') ); //Initialized the product type class
  23.             add_action( 'woocommerce_product_options_general_product_data', array($this,'wpneo_add_meta_info')); //Additional Meta form for croudfunding campaign
  24.             add_action( 'add_meta_boxes',                                   array( $this, 'add_campaign_update' ), 30 );
  25.             add_action( 'woocommerce_process_product_meta',                 array($this, 'wpneo_update_status_save')  ); //Save update status for this campaign with product
  26.             add_action( 'woocommerce_process_product_meta',                 array($this, 'wpneo_funding_custom_field_save')); //Additional meta action, save right this way
  27.             add_filter( 'woocommerce_add_cart_item',                        array($this, 'wpneo_save_user_donation_to_cookie'), 10, 3 ); //Filter cart item and save donation amount into cookir if product type crowdfunding
  28.             add_action( 'woocommerce_before_calculate_totals',              array($this, 'wpneo_add_user_donation')); //Save user input as there preferable amount with cart
  29.  
  30.             add_filter( 'woocommerce_add_to_cart_redirect',                 array($this, 'wpneo_redirect_to_checkout')); //Skip cart page after click Donate button, going directly on checkout page
  31.             add_filter( 'woocommerce_coupons_enabled',                      array($this, 'wpneo_wc_coupon_disable')); //Hide coupon form on checkout page
  32.             add_filter( 'woocommerce_get_price_html',                       array($this, 'wpneo_wc_price_remove'), 10, 2 ); //Hide default price details
  33.             add_filter( 'woocommerce_is_purchasable',                       array($this, 'return_true_woocommerce_is_purchasable'), 10, 2 ); // Return true is purchasable
  34.             add_filter( 'woocommerce_paypal_args',                          array($this, 'wpneo_custom_override_paypal_email'), 100, 1); // Override paypal reciever email address with campaign creator email
  35.             add_action( 'woocommerce_add_to_cart_validation',               array($this, 'wpneo_remove_crowdfunding_item_from_cart'), 10, 5); // Remove crowdfunding item from cart
  36.             add_action( 'woocommerce_new_order',                            array($this, 'wpneo_crowdfunding_order_type')); // Track is this product crowdfunding.
  37.             add_filter( 'woocommerce_checkout_fields' ,                     array($this, 'wpneo_override_checkout_fields') ); // Remove billing address from the checkout page
  38.  
  39.             add_action('woocommerce_review_order_before_payment', array($this, 'check_anonymous_backer'));
  40.             add_action('woocommerce_checkout_order_processed', array($this, 'check_anonymous_backer_post'));
  41.  
  42.             //Adding this hook for test
  43.             add_action('woocommerce_new_order_item', array($this, 'crowdfunding_new_order_item'), 10, 3);
  44.  
  45.             add_filter('wc_tax_enabled', array($this, 'is_tax_enable_for_crowdfunding_product'));
  46.  
  47.             add_action('product_cat_edit_form_fields', array($this, 'edit_product_taxonomy_field'), 10, 1);
  48.             add_action('product_cat_add_form_fields', array($this, 'add_checked_crowdfunding_categories'), 10, 1);
  49.  
  50.             add_action('create_product_cat', array($this, 'mark_category_as_crowdfunding'), 10, 2);
  51.             add_action('edit_product_cat', array($this, 'edit_mark_category_as_crowdfunding'), 10, 2);
  52.             add_filter("manage_product_cat_custom_column", array($this, 'filter_description_col_product_taxomony'), 10, 3);
  53.             add_filter('manage_edit-product_cat_columns' , array($this, 'product_taxonomy_is_crowdfunding_columns'), 10, 1);
  54.  
  55.         }
  56.  
  57.         /**
  58.          * @include()
  59.          *
  60.          * Include if necessary resources
  61.          */
  62.         public function includes(){
  63.             //include_once plugin_dir_path(__FILE__).'/class-wp-neo-crowdfunding.php';
  64.         }
  65.  
  66.         /**
  67.          * Registering Crowdfunding product type in product post woocommerce
  68.          */
  69.         public function wpneo_register_product_type(){
  70.             include_once plugin_dir_path(__FILE__).'class-wp-neo-wc-product-type-crowdfunding.php';
  71.         }
  72.  
  73.  
  74.         /**
  75.          * Remove billing address from the checkout page
  76.          */
  77.         function wpneo_override_checkout_fields( $fields ) {
  78.  
  79.             global $woocommerce;
  80.             $crowdfunding_found = '';
  81.             $items = $woocommerce->cart->get_cart();
  82.             if( $items ){
  83.                 foreach($items as $item => $values) {
  84.                     $product = wc_get_product( $values['product_id'] );
  85.                     if( $product->get_type() == 'crowdfunding' ){
  86.                         if( 'true' == get_option('hide_cf_address_from_checkout','') ) {
  87.                             unset($fields['billing']['billing_first_name']);
  88.                             unset($fields['billing']['billing_last_name']);
  89.                             unset($fields['billing']['billing_company']);
  90.                             unset($fields['billing']['billing_address_1']);
  91.                             unset($fields['billing']['billing_address_2']);
  92.                             unset($fields['billing']['billing_city']);
  93.                             unset($fields['billing']['billing_postcode']);
  94.                             unset($fields['billing']['billing_country']);
  95.                             unset($fields['billing']['billing_state']);
  96.                             unset($fields['billing']['billing_phone']);
  97.                             unset($fields['order']['order_comments']);
  98.                             unset($fields['billing']['billing_address_2']);
  99.                             unset($fields['billing']['billing_postcode']);
  100.                             unset($fields['billing']['billing_company']);
  101.                             unset($fields['billing']['billing_last_name']);
  102.                             unset($fields['billing']['billing_email']);
  103.                             unset($fields['billing']['billing_city']);
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.             return $fields;
  109.         }
  110.  
  111.         /**
  112.          * @param $product_type
  113.          * @return mixed
  114.          *
  115.          * Added a product type in woocommerce
  116.          */
  117.         function wpneo_product_type_selector($product_type){
  118.             $product_type['crowdfunding'] = __( 'Crowdfunding', 'wp-crowdfunding' );
  119.             return $product_type;
  120.         }
  121.  
  122.         /**
  123.          * Additional Meta form for Crowdfunding plugin
  124.          */
  125.  
  126.         public static function wpneo_check_settings($arg){
  127.             $var = get_option($arg,true);
  128.             if( $var == '' || $var == 'false' ){
  129.                 return false;
  130.             }else{
  131.                 return true;
  132.             }
  133.         }
  134.  
  135.  
  136.         function wpneo_add_meta_info(){
  137.  
  138.             global $woocommerce;
  139.  
  140.             echo '<div class="options_group show_if_neo_crowdfunding_options">';
  141.  
  142.             // Expirey
  143.             woocommerce_wp_text_input(
  144.                 array(
  145.                     'id'            => 'wpneo_funding_video',
  146.                     'label'         => __( 'Video Url', 'wp-crowdfunding' ),
  147.                     'placeholder'   => __( 'Video url', 'wp-crowdfunding' ),
  148.                     'description'   => __( 'Enter a video url to show your video in campaign details page', 'wp-crowdfunding' )
  149.                     )
  150.             );
  151.  
  152.             // Expirey
  153.             woocommerce_wp_text_input(
  154.                 array(
  155.                     'id'            => '_nf_duration_start',
  156.                     'label'         => __( 'Start date', 'wp-crowdfunding' ),
  157.                     'placeholder'   => __( 'Start time of this campaign', 'wp-crowdfunding' ),
  158.                     'description'   => __( 'Enter start of this campaign', 'wp-crowdfunding' )
  159.                     )
  160.             );
  161.  
  162.             woocommerce_wp_text_input(
  163.                 array(
  164.                     'id'            => '_nf_duration_end',
  165.                     'label'         => __( 'End date', 'wp-crowdfunding' ),
  166.                     'placeholder'   => __( 'End time of this campaign', 'wp-crowdfunding' ),
  167.                     'description'   => __( 'Enter end time of this campaign', 'wp-crowdfunding' )
  168.                     )
  169.             );
  170.  
  171.             echo '<div class="options_group"></div>';
  172.  
  173.             if (get_option('wpneo_show_min_price')) {
  174.                 woocommerce_wp_text_input(
  175.                     array(
  176.                         'id'            => 'wpneo_funding_minimum_price',
  177.                         'label'         => __('Minimum Price ('. get_woocommerce_currency_symbol().')', 'wp-crowdfunding'),
  178.                         'placeholder'   => __('Minimum Price','wp-crowdfunding'),
  179.                         'description'   => __('Enter the minimum price', 'wp-crowdfunding'),
  180.                         'class'         => 'wc_input_price'
  181.                         )
  182.                 );
  183.             }
  184.  
  185.             if (get_option('wpneo_show_max_price')) {
  186.                 woocommerce_wp_text_input(
  187.                     array(
  188.                         'id'            => 'wpneo_funding_maximum_price',
  189.                         'label'         => __('Maximum Price ('. get_woocommerce_currency_symbol() . ')', 'wp-crowdfunding'),
  190.                         'placeholder'   => __('Maximum Price','wp-crowdfunding'),
  191.                         'description'   => __('Enter the maximum price', 'wp-crowdfunding'),
  192.                         'class'         =>'wc_input_price'
  193.                         )
  194.                 );
  195.             }
  196.  
  197.             if (get_option('wpneo_show_recommended_price')) {
  198.                 woocommerce_wp_text_input(
  199.                     array(
  200.                         'id'            => 'wpneo_funding_recommended_price',
  201.                         'label'         => __('Recommended Price (' . get_woocommerce_currency_symbol() . ')', 'wp-crowdfunding'),
  202.                         'placeholder'   => __('Recommended Price', 'wp-crowdfunding'),
  203.                         'description'   => __('Enter the recommended price', 'wp-crowdfunding'),
  204.                         'class'         => 'wc_input_price'
  205.                         )
  206.                 );
  207.             }
  208.             echo '<div class="options_group"></div>';
  209.  
  210.             woocommerce_wp_text_input(
  211.                 array(
  212.                     'id'            => 'wpcf_predefined_pledge_amount',
  213.                     'label'         => __( 'Predefined Pledge Amount', 'wp-crowdfunding' ),
  214.                     'placeholder'   => __( '10,20,30,40', 'wp-crowdfunding' ),
  215.                     'description'   => __( 'Predefined amount allow you to place the amount in donate box by click, example: <code>10,20,30,40</code>', 'wp-crowdfunding' )
  216.                 )
  217.             );
  218.  
  219.             echo '<div class="options_group"></div>';
  220.  
  221.             // Funding goal/ target
  222.             woocommerce_wp_text_input(
  223.                 array(
  224.                     'id'            => '_nf_funding_goal',
  225.                     'label'         => __( 'Funding Goal ('.get_woocommerce_currency_symbol().')', 'wp-crowdfunding' ),
  226.                     'placeholder'   => __( 'Funding goal','wp-crowdfunding' ),
  227.                     'description'   => __('Enter the funding goal', 'wp-crowdfunding' ),
  228.                     'class'         => 'wc_input_price'
  229.                     )
  230.             );
  231.  
  232.    
  233.             $options = array();
  234.             if (get_option('wpneo_show_target_goal') == 'true'){
  235.                 $options['target_goal'] = 'Target Goal';
  236.             }
  237.             if (get_option('wpneo_show_target_date') == 'true'){
  238.                 $options['target_date'] = 'Target Date';
  239.             }
  240.             if (get_option('wpneo_show_target_goal_and_date') == 'true'){
  241.                 $options['target_goal_and_date'] = 'Target Goal & Date';
  242.             }
  243.             if (get_option('wpneo_show_campaign_never_end') == 'true'){
  244.                 $options['never_end'] = 'Campaign Never Ends';
  245.             }
  246.  
  247.             //Campaign end method
  248.             woocommerce_wp_select(
  249.                 array(
  250.                     'id'            => 'wpneo_campaign_end_method',
  251.                     'label'         => __('Campaign End Method', 'wp-crowdfunding'),
  252.                     'placeholder'   => __('Country', 'wp-crowdfunding'),
  253.                     'class'         => 'select2 wpneo_campaign_end_method',
  254.                     'options'       => $options
  255.                 )
  256.             );
  257.        
  258.  
  259.             //Show contributor table
  260.             woocommerce_wp_checkbox(
  261.                 array(
  262.                     'id'            => 'wpneo_show_contributor_table',
  263.                     'label'         => __( 'Show Contributor Table', 'wp-crowdfunding' ),
  264.                     'cbvalue'       => 1,
  265.                     'description'   => __( 'Enable this option to display the contributors for this Campaign', 'wp-crowdfunding' ),
  266.                 )
  267.             );
  268.  
  269.             //Mark contributors as anonymous
  270.             woocommerce_wp_checkbox(
  271.                 array(
  272.                     'id'            => 'wpneo_mark_contributors_as_anonymous',
  273.                     'label'         => __( 'Mark Contributors as Anonymous', 'wp-crowdfunding' ),
  274.                     'cbvalue'       => 1,
  275.                     'description'   => __( 'Enable this option to display the contributors Name as Anonymous for this Campaign' ),
  276.                 )
  277.             );
  278.             echo '<div class="options_group"></div>';
  279.  
  280.  
  281.             //Get country select
  282.             $countries_obj      = new WC_Countries();
  283.             $countries          = $countries_obj->__get('countries');
  284.             array_unshift($countries, 'Select a country');
  285.  
  286.             //Country list
  287.             woocommerce_wp_select(
  288.                 array(
  289.                     'id'            => 'wpneo_country',
  290.                     'label'         => __( 'Country', 'wp-crowdfunding' ),
  291.                     'placeholder'   => __( 'Country', 'wp-crowdfunding' ),
  292.                     'class'         => 'select2 wpneo_country',
  293.                     'options'       => $countries
  294.                 )
  295.             );
  296.  
  297.             // Location of this campaign
  298.             woocommerce_wp_text_input(
  299.                 array(
  300.                     'id'            => '_nf_location',
  301.                     'label'         => __( 'Location', 'wp-crowdfunding' ),
  302.                     'placeholder'   => __( 'Location', 'wp-crowdfunding' ),
  303.                     'description'   => __( 'Location of this campaign','wp-crowdfunding' ),
  304.                     'type'          => 'text'
  305.                     )
  306.             );
  307.             do_action( 'new_crowd_funding_campaign_option' );
  308.             echo '</div>';
  309.         }
  310.  
  311.  
  312.         public function add_campaign_update(){
  313.             add_meta_box( 'campaign-update-status-meta', __( 'Campaign Update Status', 'wpneo-crowdfunding' ), array($this, 'wpneo_campaign_status_metabox'), 'product', 'normal' );
  314.         }
  315.  
  316.  
  317.         public function wpneo_campaign_status_metabox() {
  318.             global $post;
  319.             $saved_campaign_update = get_post_meta($post->ID, 'wpneo_campaign_updates', true);
  320.             $saved_campaign_update_a = json_decode($saved_campaign_update, true);
  321.  
  322.             $display ='block;';
  323.             if (is_array($saved_campaign_update_a) && count($saved_campaign_update_a) > 0) {
  324.                 $display ='none;';
  325.             }
  326.  
  327.             echo "<div id='campaign_status' class='panel woocommerce_options_panel'>";
  328.  
  329.             echo "<div id='campaign_update_field' style='display: $display'>";
  330.                 echo "<div class='campaign_update_field_copy'>";
  331.  
  332.                 woocommerce_wp_text_input(
  333.                     array(
  334.                         'id'            => 'wpneo_prject_update_date_field[]',
  335.                         'label'         => __( 'Date', 'wp-crowdfunding' ),
  336.                         'desc_tip'      => 'true',
  337.                         'type'          => 'text',
  338.                         'class'         => 'datepicker',
  339.                         'placeholder'   => __( date('d-m-Y'), 'wp-crowdfunding' ),
  340.                         'value'         => ''
  341.                     )
  342.                 );
  343.                 woocommerce_wp_text_input(
  344.                     array(
  345.                         'id'            => 'wpneo_prject_update_title_field[]',
  346.                         'label'         => __( 'Update Title', 'wp-crowdfunding' ),
  347.                         'desc_tip'      => 'true',
  348.                         'type'          => 'text',
  349.                         'placeholder'   => __( 'Update title', 'wp-crowdfunding' ),
  350.                         'value'         => ''
  351.                     )
  352.                 );
  353.                 woocommerce_wp_textarea_input(
  354.                     array(
  355.                         'id'            => 'wpneo_prject_update_details_field[]',
  356.                         'label'         => __( 'Update Details', 'wp-crowdfunding' ),
  357.                         'desc_tip'      => 'true',
  358.                         'type'          => 'text',
  359.                         'placeholder'   => __( 'Update details', 'wp-crowdfunding' ),
  360.                         'value'         => ''
  361.                     )
  362.                 );
  363.             echo '<input name="remove_udpate" type="button" class="button tagadd removecampaignupdate" value="'.__('Remove', 'wp-crowdfunding').'" />';
  364.             echo '<div style="border-bottom: 1px solid #eee"></div>';
  365.             echo "</div>";
  366.             echo "</div>";
  367.  
  368.             echo "<div id='campaign_update_addon_field'>";
  369.                 if (is_array($saved_campaign_update_a) && count($saved_campaign_update_a) > 0){
  370.                     foreach($saved_campaign_update_a as $key => $value) {
  371.                         echo "<div class='campaign_update_field_copy'>";
  372.                         woocommerce_wp_text_input(
  373.                             array(
  374.                                 'id'            => 'wpneo_prject_update_date_field[]',
  375.                                 'label'         => __( 'Date', 'wp-crowdfunding' ),
  376.                                 'desc_tip'      => 'true',
  377.                                 'type'          => 'text',
  378.                                 'class'         => 'datepicker',
  379.                                 'placeholder'   => __( date('d-m-Y'), 'wp-crowdfunding' ),
  380.                                 'value'         => stripslashes($value['date'])
  381.                             )
  382.                         );
  383.                         woocommerce_wp_text_input(
  384.                             array(
  385.                                 'id'        => 'wpneo_prject_update_title_field[]',
  386.                                 'label'     => __('Update Title', 'wp-crowdfunding'),
  387.                                 'desc_tip'  => 'true',
  388.                                 'type'      => 'text',
  389.                                 'placeholder' => __('Update title', 'wp-crowdfunding'),
  390.                                 'value'     => stripslashes($value['title'])
  391.                             )
  392.                         );
  393.                        /* woocommerce_wp_textarea_input(
  394.                             array(
  395.                                 'id'        => 'wpneo_prject_update_details_field[]',
  396.                                 'label'     => __('Update Details', 'wp-crowdfunding'),
  397.                                 'desc_tip'  => 'true',
  398.                                 'type'      => 'text',
  399.                                 'placeholder' => __('Update details', 'wp-crowdfunding'),
  400.                                 'value'     => stripslashes($value['details'])
  401.                             )
  402.                         );*/
  403.  
  404.                         wp_editor(stripslashes($value['details']), 'wpneo_prject_update_details_field'.$key, array('textarea_name' => 'wpneo_prject_update_details_field[]'));
  405.  
  406.                         echo '<div class="wpcf-campaign-update-btn-wrap"><input name="remove_udpate" type="button"
  407. class="button tagadd removecampaignupdate"
  408. value="'.__('Remove', 'wp-crowdfunding').'" /></div>';
  409.  
  410.  
  411.                         echo '<div style="border-bottom: 1px solid #eee"></div>';
  412.  
  413.                         echo "</div>";
  414.                     }
  415.                 }
  416.             echo "</div>";
  417.  
  418.  
  419.             echo '<input name="save_update" type="button" class="button tagadd" id="addcampaignupdate" value="'.__('+ Add Update', 'wp-crowdfunding').'" />';
  420.             echo '<div style="clear: both;"></div>';
  421.             echo "</div>";
  422.         }
  423.  
  424.         /**
  425.          * @param $post_id
  426.          *
  427.          * Save Update at Meta Data
  428.          */
  429.         public function wpneo_update_status_save($post_id){
  430.             if ( ! empty($_POST['wpneo_prject_update_title_field'])){
  431.                
  432.                 $wpneo_prject_update_title_field = $_POST['wpneo_prject_update_title_field'];
  433.                 $wpneo_prject_update_date_field = $_POST['wpneo_prject_update_date_field'];
  434.                 $wpneo_prject_update_details_field = $_POST['wpneo_prject_update_details_field'];
  435.                 $total_update_field = count( $wpneo_prject_update_title_field );
  436.  
  437.                 $data = array();
  438.                 for ($i=0; $i<$total_update_field; $i++){
  439.                     if (! empty($wpneo_prject_update_title_field[$i])) {
  440.                         $data[] = array(
  441.                             'date'      => sanitize_text_field($wpneo_prject_update_date_field[$i]),
  442.                             'title'     => sanitize_text_field($wpneo_prject_update_title_field[$i]),
  443.                             'details'   => $wpneo_prject_update_details_field[$i]
  444.                         );
  445.                     }
  446.                 }
  447.                 $data_json = json_encode($data,JSON_UNESCAPED_UNICODE);
  448.  
  449.                 update_post_meta($post_id, 'wpneo_campaign_updates', wp_slash($data_json));
  450.             }
  451.         }
  452.  
  453.  
  454.         /**
  455.          * @param $post_id
  456.          * Saving meta information over this method
  457.          */
  458.         function wpneo_funding_custom_field_save($post_id ){
  459.  
  460.             // _neo_crowdfunding_product_type
  461.             $_neo_crowdfunding_product_type = sanitize_text_field(wpneo_post('_neo_crowdfunding_product_type'));
  462.             if( !empty( $_neo_crowdfunding_product_type) ) {
  463.                 update_post_meta($post_id, '_neo_crowdfunding_product_type', 'yes');
  464.             } else {
  465.                 update_post_meta($post_id, '_neo_crowdfunding_product_type', 'no');
  466.             }
  467.  
  468.             // _nf_location
  469.             $_nf_location = sanitize_text_field( $_POST['_nf_location'] );
  470.             wpneo_crowdfunding_update_post_meta_text($post_id, '_nf_location', $_nf_location);
  471.  
  472.             // wpneo_funding_video
  473.             $wpneo_funding_video = sanitize_text_field( $_POST['wpneo_funding_video'] );
  474.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_funding_video', $wpneo_funding_video);
  475.  
  476.             // _nf_duration_start
  477.             $_nf_duration_start = sanitize_text_field( $_POST['_nf_duration_start'] );
  478.             wpneo_crowdfunding_update_post_meta_text($post_id, '_nf_duration_start', $_nf_duration_start);
  479.  
  480.             // _nf_duration_end
  481.             $_nf_duration_end = sanitize_text_field( $_POST['_nf_duration_end'] );
  482.             wpneo_crowdfunding_update_post_meta_text($post_id, '_nf_duration_end', $_nf_duration_end);
  483.  
  484.             // _nf_funding_goal
  485.             $_nf_funding_goal = sanitize_text_field($_POST['_nf_funding_goal']);
  486.             wpneo_crowdfunding_update_post_meta_text($post_id, '_nf_funding_goal', $_nf_funding_goal);
  487.  
  488.             // wpneo_funding_minimum_price
  489.             $wpneo_funding_minimum_price = sanitize_text_field($_POST['wpneo_funding_minimum_price']);
  490.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_funding_minimum_price', $wpneo_funding_minimum_price);
  491.  
  492.             // wpneo_funding_maximum_price
  493.             $wpneo_funding_maximum_price = sanitize_text_field($_POST['wpneo_funding_maximum_price']);
  494.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_funding_maximum_price', $wpneo_funding_maximum_price);
  495.  
  496.             // wpneo_funding_recommended_price
  497.             $wpneo_funding_recommended_price = sanitize_text_field($_POST['wpneo_funding_recommended_price']);
  498.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_funding_recommended_price', $wpneo_funding_recommended_price);
  499.  
  500.             //wpcf_predefined_pledge_amount
  501.             $wpcf_predefined_pledge_amount = sanitize_text_field($_POST['wpcf_predefined_pledge_amount']);
  502.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpcf_predefined_pledge_amount', $wpcf_predefined_pledge_amount);
  503.  
  504.             // wpneo_campaign_end_method
  505.             $wpneo_campaign_end_method = sanitize_text_field( $_POST['wpneo_campaign_end_method'] );
  506.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_campaign_end_method', $wpneo_campaign_end_method);
  507.  
  508.             // wpneo_show_contributor_table
  509.             $wpneo_show_contributor_table = sanitize_text_field( $_POST['wpneo_show_contributor_table'] );
  510.             wpneo_crowdfunding_update_post_meta_checkbox($post_id, 'wpneo_show_contributor_table', $wpneo_show_contributor_table);
  511.  
  512.             // wpneo_mark_contributors_as_anonymous
  513.             $wpneo_mark_contributors_as_anonymous = sanitize_text_field( $_POST['wpneo_mark_contributors_as_anonymous'] );
  514.             wpneo_crowdfunding_update_post_meta_checkbox($post_id, 'wpneo_mark_contributors_as_anonymous', $wpneo_mark_contributors_as_anonymous);
  515.  
  516.             // wpneo_campaigner_paypal_id
  517.             $wpneo_campaigner_paypal_id = sanitize_text_field( $_POST['wpneo_campaigner_paypal_id'] );
  518.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_campaigner_paypal_id', $wpneo_campaigner_paypal_id);
  519.  
  520.             // wpneo_country
  521.             $wpneo_country = sanitize_text_field( $_POST['wpneo_country'] );
  522.             wpneo_crowdfunding_update_post_meta_text($post_id, 'wpneo_country', $wpneo_country);
  523.         }
  524.  
  525.  
  526.         /**
  527.          * wpneo_donate_input_field();
  528.          */
  529.         function wpneo_donate_input_field()
  530.         {
  531.             global $post, $woocommerce;
  532.             $product = wc_get_product( $post->ID );
  533.  
  534.             //wp_die(var_dump($product));
  535.  
  536.             $html = '';
  537.             if ($product->get_type() == 'crowdfunding') {
  538.                 $html .= '<div class="donate_field wp_neo">';
  539.  
  540.                 if (WPNEOCF()->campaignValid()) {
  541.  
  542.                     $html .= '<form class="cart" method="post" enctype="multipart/form-data">';
  543.                     $html .= do_action('before_wpneo_donate_field');
  544.                     $recomanded_price = get_post_meta($post->ID, 'wpneo_funding_recommended_price', true);
  545.                     $html .= get_woocommerce_currency_symbol();
  546.                     $html .= apply_filters('neo_donate_field', '<input type ="number" step="any" class="input-text amount wpneo_donation_input text" name="wpneo_donate_amount_field" min="0" value="'.esc_attr($recomanded_price).'" />');
  547.                     $html .= do_action('after_wpneo_donate_field');
  548.                     $html .= '<input type="hidden" name="add-to-cart" value="' . esc_attr($product->get_id()) . '" />';
  549.                     $btn_text = get_option('wpneo_donation_btn_text');
  550.                     $html .= '<button type="submit" class="'.apply_filters('add_to_donate_button_class', 'single_add_to_cart_button button alt').'">' . __(apply_filters('add_to_donate_button_text', esc_html($btn_text) ? esc_html($btn_text) : 'Donate now'), 'woocommerce').'</button>';
  551.                     $html .= '</form>';
  552.                 } else {
  553.                     $html .= apply_filters('end_campaign_message', __('This campaign has been end', 'wp-crowdfunding'));
  554.                 }
  555.                 $html .= '</div>';
  556.             }
  557.             echo $html;
  558.         }
  559.  
  560.  
  561.         /**
  562.          * Remove Crowdfunding item form cart
  563.          */
  564.         public function wpneo_remove_crowdfunding_item_from_cart($passed, $product_id, $quantity, $variation_id = '', $variations= '') {
  565.             global $woocommerce;
  566.             $product = wc_get_product($product_id);
  567.  
  568.             if($product->get_type() == 'crowdfunding') {
  569.                 foreach (WC()->cart->cart_contents as $item_cart_key => $prod_in_cart) {
  570.                     WC()->cart->remove_cart_item( $item_cart_key );
  571.                 }
  572.             }
  573.             foreach (WC()->cart->cart_contents as $item_cart_key => $prod_in_cart) {
  574.                 if ($prod_in_cart['data']->get_type() == 'crowdfunding') {
  575.                     WC()->cart->remove_cart_item( $item_cart_key );
  576.                 }
  577.             }
  578.             return $passed;
  579.         }
  580.  
  581.         /**
  582.          * @param $array
  583.          * @param $int
  584.          * @return mixed
  585.          *
  586.          * Save user input donation into cookie
  587.          */
  588.         function wpneo_save_user_donation_to_cookie( $array, $int ) {
  589.             if ($array['data']->get_type() == 'crowdfunding'){
  590.                 if ( ! empty($_POST['wpneo_donate_amount_field'])){
  591.                     if (is_user_logged_in()){
  592.                         $user_id = get_current_user_id();
  593.                         delete_user_meta($user_id,'wpneo_wallet_info');
  594.                     }
  595.  
  596.                     //setcookie("wpneo_user_donation", esc_attr($_POST['wpneo_donate_amount_field']), 0, "/");
  597.                     $donate_amount = sanitize_text_field(wpneo_post('wpneo_donate_amount_field'));
  598.                     WC()->session->set('wpneo_donate_amount', $donate_amount);
  599.  
  600.                     if ( isset($_POST['wpneo_rewards_index'])){
  601.                         if ( ! $_POST['wpneo_rewards_index']){
  602.                             return;
  603.                         }
  604.  
  605.                         $selected_reward = stripslashes_deep($_POST['wpneo_selected_rewards_checkout']);
  606.                         $selected_reward = unserialize($selected_reward);
  607.  
  608.                         $reward_index = (int) $_POST['wpneo_rewards_index'];
  609.  
  610.                         $wpneo_rewards_index = (int) sanitize_text_field(wpneo_post('wpneo_rewards_index')) -1;
  611.                         $_cf_product_author_id = sanitize_text_field(wpneo_post('_cf_product_author_id'));
  612.                         $product_id = sanitize_text_field(wpneo_post('add-to-cart'));
  613.                         WC()->session->set('wpneo_rewards_data',
  614.                             array(
  615.                                 'wpneo_selected_rewards_checkout' => $selected_reward,
  616.                                 'rewards_index' => $wpneo_rewards_index,
  617.                                 'product_id' => $product_id,
  618.                                 '_cf_product_author_id' => $_cf_product_author_id
  619.                             )
  620.                         );
  621.                     }else{
  622.                         WC()->session->__unset('wpneo_rewards_data');
  623.                     }
  624.                 }
  625.             }
  626.             return $array;
  627.         }
  628.  
  629.         /**
  630.          * Get donation amount from cookie. Add user input base donation amount to cart
  631.          */
  632.  
  633.         function wpneo_add_user_donation(){
  634.             global $woocommerce;
  635.             foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
  636.                 if ($cart_item['data']->get_type() == 'crowdfunding') {
  637.                     /*if ( ! empty($_COOKIE['wpneo_user_donation'])){
  638.                         $cart_item['data']->set_price($_COOKIE['wpneo_user_donation']);
  639.                     }*/
  640.                     $donate_cart_amount = WC()->session->get('wpneo_donate_amount');
  641.                     if ( ! empty($donate_cart_amount)){
  642.                         $cart_item['data']->set_price($donate_cart_amount);
  643.                     }
  644.                 }
  645.             }
  646.         }
  647.  
  648.         /**
  649.          * Redirect to checkout after cart
  650.          */
  651.         function wpneo_redirect_to_checkout($url) {
  652.             global $woocommerce, $product;
  653.  
  654.             if (! empty($_REQUEST['add-to-cart'])){
  655.                 $product_id = absint( $_REQUEST['add-to-cart'] );
  656.                 $product = wc_get_product( $product_id );
  657.  
  658.                 if($product && $product->is_type( 'crowdfunding' ) ){
  659.  
  660.                     $checkout_url   = wc_get_checkout_url();
  661.                     $preferance     = get_option('wpneo_crowdfunding_add_to_cart_redirect');
  662.  
  663.                     if ($preferance == 'checkout_page'){
  664.                         $checkout_url = wc_get_checkout_url();
  665.                     }elseif ($preferance == 'cart_page'){
  666.                         $checkout_url = $woocommerce->cart->get_cart_url();
  667.                     }else{
  668.                         $checkout_url = get_permalink();
  669.                     }
  670.  
  671.                     wc_clear_notices();
  672.                     return $checkout_url;
  673.                 }
  674.             }
  675.             return $url;
  676.         }
  677.  
  678.         /**
  679.          * Disabled coupon system from system
  680.          */
  681.         function wpneo_wc_coupon_disable( $coupons_enabled ) {
  682.             global $woocommerce;
  683.             return false;
  684.         }
  685.  
  686.         /**
  687.          * @param $price
  688.          * @param $product
  689.          * @return string
  690.          *
  691.          * reove price html for crowdfunding campaign
  692.          */
  693.  
  694.         function wpneo_wc_price_remove( $price, $product ) {
  695.             $target_product_types = array( 'crowdfunding' );
  696.             if ( in_array ( $product->get_type(), $target_product_types ) ) {
  697.                 // if variable product return and empty string
  698.                 return '';
  699.             }
  700.             // return normal price
  701.             return $price;
  702.         }
  703.  
  704.  
  705.         /**
  706.          * @param $purchasable
  707.          * @param $product
  708.          * @return bool
  709.          *
  710.          * Return true is purchasable if not found price
  711.          */
  712.  
  713.         function return_true_woocommerce_is_purchasable( $purchasable, $product ){
  714.             if( $product->get_price() == 0 ||  $product->get_price() == ''){
  715.                 $purchasable = true;
  716.             }
  717.             return $purchasable;
  718.         }
  719.  
  720.  
  721.         /**
  722.          * @return mixed
  723.          *
  724.          * get PayPal email address from campaign
  725.          */
  726.         public function wpneo_get_paypal_reciever_email_address() {
  727.             global $woocommerce;
  728.             foreach ($woocommerce->cart->cart_contents as $item) {
  729.                 $emailid = get_post_meta($item['product_id'], 'wpneo_campaigner_paypal_id', true);
  730.                 $enable_paypal_per_campaign = get_option('wpneo_enable_paypal_per_campaign_email');
  731.  
  732.                 if ($enable_paypal_per_campaign == 'true') {
  733.                     if (!empty($emailid)) {
  734.                         return $emailid;
  735.                     } else {
  736.                         $paypalsettings = get_option('woocommerce_paypal_settings');
  737.                         return $paypalsettings['email'];
  738.                     }
  739.                 } else {
  740.                     $paypalsettings = get_option('woocommerce_paypal_settings');
  741.                     return $paypalsettings['email'];
  742.                 }
  743.             }
  744.         }
  745.  
  746.         public function wpneo_custom_override_paypal_email($paypal_args) {
  747.             global $woocommerce;
  748.             $paypal_args['business'] = $this->wpneo_get_paypal_reciever_email_address();
  749.             return $paypal_args;
  750.         }
  751.  
  752.         /**
  753.          * @param $order_id
  754.          *
  755.          * Save order reward if any with order meta
  756.          */
  757.         public function wpneo_crowdfunding_order_type($order_id){
  758.             global $woocommerce;
  759.  
  760.             $wpneo_rewards_data = WC()->session->get('wpneo_rewards_data');
  761.             if ( ! empty($wpneo_rewards_data)){
  762.                 //$campaign_rewards   = get_post_meta($wpneo_rewards_data['product_id'], 'wpneo_reward', true);
  763.                 //$campaign_rewards   = stripslashes($campaign_rewards);
  764.                 //$campaign_rewards_a = json_decode($campaign_rewards, true);
  765.                 //$reward = $campaign_rewards_a[$wpneo_rewards_data['rewards_index']];
  766.                 $reward = $wpneo_rewards_data['wpneo_selected_rewards_checkout'];
  767.                 update_post_meta($order_id, 'wpneo_selected_reward', $reward);
  768.                 update_post_meta($order_id, '_cf_product_author_id', $wpneo_rewards_data['_cf_product_author_id'] );
  769.                 WC()->session->__unset('wpneo_rewards_data');
  770.             }
  771.         }
  772.  
  773.         public function crowdfunding_new_order_item( $item_id, $item, $order_id){
  774.             $product_id = wc_get_order_item_meta($item_id, '_product_id', true);
  775.             if( ! $product_id ){
  776.                 return;
  777.             }
  778.             $get_product = wc_get_product($product_id);
  779.             $product_type = $get_product->get_type();
  780.             if ($product_type === 'crowdfunding'){
  781.                 update_post_meta($order_id, 'is_crowdfunding_order','1');
  782.             }
  783.         }
  784.  
  785.         public function check_anonymous_backer(){
  786.             global $woocommerce;
  787.             $items = $woocommerce->cart->get_cart();
  788.             if( $items ){
  789.                 foreach($items as $item => $values) {
  790.                     $product = wc_get_product( $values['product_id'] );
  791.                     if( $product->get_type() == 'crowdfunding' ){
  792.                         // if( get_post_meta( $values['product_id'],'wpneo_mark_contributors_as_anonymous',true ) == '1' ){
  793.                         echo '<div id="mark_name_anonymous" class="mark_name_anonymous_wrap">';
  794.                         echo '<label><input type="checkbox" value="true" name="mark_name_anonymous" /> '.__('Make me anonymous', 'wp-crowdfunding').' </label>';
  795.                         echo '</div>';
  796.                         // }
  797.                     }
  798.                 }
  799.             }
  800.         }
  801.  
  802.         /**
  803.          * @param $order_id
  804.          */
  805.         public function check_anonymous_backer_post($order_id){
  806.             if (! empty($_POST['mark_name_anonymous'])){
  807.                 if ($_POST['mark_name_anonymous'] === 'true'){
  808.                     update_post_meta($order_id, 'mark_name_anonymous', 'true');
  809.                 }
  810.             }
  811.         }
  812.  
  813.  
  814.         public function is_tax_enable_for_crowdfunding_product($bool){
  815.             if( ! $bool){
  816.                 return false;
  817.             }
  818.  
  819.             $is_enabled = get_option('wpcf_enable_tax') === 'true';
  820.  
  821.             if ($bool && $is_enabled){
  822.                 return true;
  823.             }
  824.  
  825.  
  826.             $is_crowdfunding_in_cart = false;
  827.             if ( ! empty(wc()->cart->cart_contents)){
  828.                 $cart_content = wc()->cart->cart_contents;
  829.                 foreach ($cart_content as $content){
  830.                     if ( ! empty($content['data']->product_type) && $content['data']->product_type === 'crowdfunding'){
  831.                         $is_crowdfunding_in_cart = true;
  832.                     }
  833.                 }
  834.             }
  835.  
  836.             if ($is_crowdfunding_in_cart && ! $is_enabled){
  837.                 return false;
  838.             }
  839.  
  840.             return $bool;
  841.         }
  842.  
  843.  
  844.  
  845.         public function add_checked_crowdfunding_categories( $taxonomy){
  846.             ?>
  847.  
  848.             <div class="form-field term-check-crowdfunding-category-wrap">
  849.                 <label for="tag-check-crowdfunding-category">
  850.                     <input type="checkbox" name="tag_check_crowdfunding_category" id="tag-check-crowdfunding-category" value="1">
  851.                     <?php _e( 'Mark as Crowdfunding Category' ); ?>
  852.                 </label>
  853.  
  854.                 <p><?php _e('This check mark allow you to detect crowdfunding specific category,'); ?></p>
  855.             </div>
  856.  
  857.             <?php
  858.         }
  859.  
  860.         public function edit_product_taxonomy_field($term){
  861.             ?>
  862.  
  863.             <tr class="form-field">
  864.                 <th scope="row" valign="top"><label><?php _e( 'Is Crowdfunding Category', 'woocommerce' );
  865.                 ?></label></th>
  866.                 <td>
  867.  
  868.                     <label for="tag-check-crowdfunding-category">
  869.                         <?php
  870.                         $is_checked_crowdfunding = get_term_meta($term->term_id, '_marked_as_crowdfunding', true);
  871.  
  872.                         ?>
  873.                         <input type="checkbox" name="tag_check_crowdfunding_category"
  874.                                id="tag-check-crowdfunding-category" value="1" <?php checked($is_checked_crowdfunding, '1' ) ?>>
  875.                         <?php _e( 'Mark as Crowdfunding Category' ); ?>
  876.                     </label>
  877.  
  878.                     <p><?php _e('This check mark allow you to detect crowdfunding specific category,'); ?></p>
  879.  
  880.                 </td>
  881.             </tr>
  882.  
  883.             <?php
  884.  
  885.         }
  886.  
  887.         /**
  888.          * @param $term_id (int) Term ID.
  889.          * @param $tt_id (int) Term taxonomy ID.
  890.          *
  891.          *
  892.          */
  893.         public function mark_category_as_crowdfunding( $term_id, $tt_id){
  894.             if (isset($_POST['tag_check_crowdfunding_category']) && $_POST['tag_check_crowdfunding_category'] == '1'){
  895.                 $term_meta = update_term_meta($term_id, '_marked_as_crowdfunding', $_POST['tag_check_crowdfunding_category']);
  896.             }
  897.         }
  898.  
  899.         public function edit_mark_category_as_crowdfunding( $term_id, $tt_id){
  900.             if (isset($_POST['tag_check_crowdfunding_category']) && $_POST['tag_check_crowdfunding_category'] == '1'){
  901.                 $term_meta = update_term_meta($term_id, '_marked_as_crowdfunding', $_POST['tag_check_crowdfunding_category']);
  902.             }else{
  903.                 delete_term_meta($term_id, '_marked_as_crowdfunding');
  904.             }
  905.         }
  906.  
  907.         public function product_taxonomy_is_crowdfunding_columns($columns){
  908.             $columns['crowdfunding_col'] = __('Crowdfunding', 'wp-crowdfunding');
  909.             return $columns;
  910.         }
  911.  
  912.  
  913.         function filter_description_col_product_taxomony($content, $column_name, $term_id ) {
  914.             switch ($column_name) {
  915.                 case 'crowdfunding_col':
  916.                     $is_crowdfunding_col = get_term_meta($term_id, '_marked_as_crowdfunding', true);
  917.                     if ($is_crowdfunding_col == '1'){
  918.                         $content = __('Yes', 'wp-crowdfunding');
  919.                     }
  920.                     break;
  921.                 default:
  922.                     break;
  923.             }
  924.             return $content;
  925.         }
  926.  
  927.  
  928.     } //End class bracket
  929. } //End if class exists
  930.  
  931. /**
  932.  * @return null|Wpneo_Crowdfunding
  933.  * @Wpneo_Crowdfunding() for initialize Main class
  934.  */
  935. function Wpneo_Crowdfunding(){
  936.     require_once WPNEO_CROWDFUNDING_DIR_PATH.'includes/woocommerce/class-wpneo-wc-admin-dashboard.php';
  937.     require_once WPNEO_CROWDFUNDING_DIR_PATH.'includes/woocommerce/class-wpneo-frontend-hook.php';
  938.     require_once WPNEO_CROWDFUNDING_DIR_PATH.'includes/woocommerce/class-wpneo-frontend-campaign-submit-form.php';
  939.     require_once WPNEO_CROWDFUNDING_DIR_PATH.'includes/woocommerce/class-wpneo-wc-account-dashboard.php';
  940.     return Wpneo_Crowdfunding::instance();
  941. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement