Plugin Directory

Changeset 2700245


Ignore:
Timestamp:
03/27/2022 03:30:23 PM (4 years ago)
Author:
kitcartcommerce
Message:

Massive Updates in Hooks and Activation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kitcart/trunk/kitcart.php

    r2634464 r2700245  
    3131Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    3232
    33 Copyright 2020-2021 Kitcart Technology, Inc.
     33Copyright 2020-2022 Kitcart Technology, Inc.
    3434 */
    3535
     
    4646define('KITCART__PLUGIN_DIR', plugin_dir_path(__FILE__));
    4747define('KITCART_DELETE_LIMIT', 100000);
    48 define('KITCART_API_URI', 'https://kitcart.net/api/create-order');
     48define('KITCART_API_URI', 'https://kitcart.net/api/wordpress/');
    4949
    5050// add settings link
     
    9292
    9393    if ($kitcart_wc_active == 'no') {
    94     ?>
     94?>
    9595
    9696        <div class="notice notice-error is-dismissible">
    9797            <p>WooCommerce is not activated, please activate it to use <b>Kitcart Plugin</b></p>
    9898        </div>
    99 <?php
     99    <?php
    100100
    101101    }
     
    106106add_action('activated_plugin', 'kitcart_activated_action');
    107107register_uninstall_hook(__FILE__, 'uninstall_kitcart');
     108// register deactivation hook
     109register_deactivation_hook(__FILE__, 'deactivate_kitcart');
     110
     111// kitcart plugin action function to report plugin activation
     112function kitcart_plugin_action($action)
     113{
     114    $route = 'action';
     115    $body = array(
     116
     117        'public_key' => get_option('kitcart_public_key'),
     118        'secret_key' => get_option('kitcart_secret_key'),
     119        'action' => $action,
     120    );
     121
     122    $req_args = array(
     123        'body'        => $body,
     124        'timeout'     => '60',
     125        'redirection' => '5',
     126        'httpversion' => '1.0',
     127        'blocking'    => true,
     128        'headers'     => array(
     129            'accept' => "application/json",
     130        ),
     131        'cookies'     => array(
     132            'XSRF-TOKEN' => 'woocommerce-api-request',
     133            'kitcart_session' => 'woocommerce'
     134        ),
     135    );
     136    try {
     137        $response = wp_remote_post(KITCART_API_URI . $route, $req_args);
     138        $body = wp_remote_retrieve_body($response);
     139        $body = json_decode($body);
     140        if ($body->status == 'success') {
     141            return true;
     142        } else {
     143            return false;
     144        }
     145    } catch (\Throwable $th) {
     146        return false;
     147    }
     148}
     149
     150function submit_all_successful_orders()
     151{
     152    // empty array to store all successful orders
     153    $all_order_data = array();
     154    //route
     155    $route = 'submit-all-successful-orders';
     156    // get all successful orders
     157    $args = array(
     158        'post_type' => 'shop_order',
     159        'post_status' => 'wc-processing',
     160        'posts_per_page' => -1,
     161    );
     162    $orders = get_posts($args);
     163    // loop through orders
     164    foreach ($orders as $order) {
     165        $order_id = $order->ID;
     166        $order = wc_get_order($order_id);
     167        $items = $order->get_items();
     168        $items_data = [];
     169        foreach ($items as $item_key => $item) {
     170            $items_data[] = $item->get_data();
     171        }
     172
     173        $order_data = array(
     174            'order_id' => $order_id,
     175            'product_data' => $items_data,
     176            'order_data' => $order->get_data(),
     177        );
     178        $all_order_data[] = $order_data;
     179    }
     180
     181    $body = array(
     182        'public_key' => get_option('kitcart_public_key'),
     183        'secret_key' => get_option('kitcart_secret_key'),
     184        'orders' => $all_order_data,
     185    );
     186    $req_args = array(
     187        'body'        => $body,
     188        'timeout'     => '300',
     189        'redirection' => '5',
     190        'httpversion' => '1.0',
     191        'blocking'    => true,
     192        'headers'     => array(
     193            'accept' => "application/json",
     194        ),
     195        'cookies'  => array(
     196            'XSRF-TOKEN' => 'woocommerce-api-request',
     197            'kitcart_session' => 'woocommerce'
     198        ),
     199    );
     200    $response = wp_remote_post(KITCART_API_URI . $route, $req_args);
     201}
    108202
    109203// action functions and datas
     
    111205{
    112206    //value to be change on first update
    113    add_option('kitcart_redirect_to_settings', 'yes');
     207    if (get_option('kitcart_secret_key') && get_option('kitcart_public_key')) {
     208        kitcart_plugin_action('activate');
     209        submit_all_successful_orders();
     210    } else {
     211
     212        add_option('kitcart_redirect_to_settings', 'yes');
     213    }
    114214}
    115215
    116216//redirect after activation to settings page
    117 function kitcart_activated_action( $plugin ) {
     217function kitcart_activated_action($plugin)
     218{
    118219    //if the user has updated secret keys before, the value will be NO
    119     if( get_option('kitcart_redirect_to_settings') === 'yes') {
    120         if( $plugin == plugin_basename( __FILE__ ) ) {
    121             exit( wp_redirect( admin_url( '/admin.php?page=wc-settings&tab=kitcart' ) ) );
    122         }
    123     }
    124    
     220    if (get_option('kitcart_redirect_to_settings') === 'yes') {
     221        if ($plugin == plugin_basename(__FILE__)) {
     222            exit(wp_redirect(admin_url('/admin.php?page=wc-settings&tab=kitcart')));
     223        }
     224    }
     225}
     226
     227function deactivate_kitcart()
     228{
     229    // let kitcart know that the plugin is deactivated
     230    kitcart_plugin_action('deactivate');
    125231}
    126232
     
    128234function uninstall_kitcart()
    129235{
     236
    130237    if (get_option('kitcart_secret_key')) {
    131         delete_option('kitcart_redirect_to_settings', 'yes');
    132         delete_option('kitcart_secret_key');
    133         delete_option('kitcart_public_key');
     238        // delete all the values
     239        if (kitcart_plugin_action('uninstall')) {
     240            delete_option('kitcart_public_key');
     241            delete_option('kitcart_secret_key');
     242            delete_option('kitcart_redirect_to_settings');
     243        } else {
     244            return;
     245        }
    134246    }
    135247}
     
    157269            update_option('kitcart_public_key', sanitize_text_field($_POST['kitcart_public_key']));
    158270            update_option('kitcart_secret_key', sanitize_text_field($_POST['kitcart_secret_key']));
     271            //make kitcart api call to update the keys
     272            if (kitcart_plugin_action('activate')) {
     273                $message = '<div class="notice notice-success is-dismissible"><p>' . __('Your keys have been updated successfully', 'woocommerce') . '</p></div>';
     274            } else {
     275                $message = '<div class="notice notice-error is-dismissible"><p>' . __('Your keys could not be updated', 'woocommerce') . '</p></div>';
     276            }
    159277        } else {
    160278            update_option('kitcart_redirect_to_settings', 'no');
    161279            add_option('kitcart_public_key', sanitize_text_field($_POST['kitcart_public_key']));
    162280            add_option('kitcart_secret_key', sanitize_text_field($_POST['kitcart_secret_key']));
    163         }
    164     }
    165     $public_key = get_option('kitcart_public_key') ? get_option('kitcart_public_key') : "";
    166     $secret_key = get_option('kitcart_secret_key') ? get_option('kitcart_secret_key') : '';
    167 
    168     // Styling the table a bit
    169 ?>
     281            //make kitcart api call to create record of all successful orders
     282            submit_all_successful_orders();
     283            // make kitcart aware of the activation
     284            if (kitcart_plugin_action('activate')) {
     285                $message = '<div class="notice notice-success is-dismissible"><p>' . __('Your keys have been updated successfully', 'woocommerce') . '</p></div>';
     286            } else {
     287                $message = '<div class="notice notice-error is-dismissible"><p>' . __('Your keys could not be updated', 'woocommerce') . '</p></div>';
     288            }
     289        }
     290    } else {
     291        $message = '<div class="notice notice-error is-dismissible"><p>' . __('Please enter valid keys', 'woocommerce') . '</p></div>';
     292    }
     293    $public_key = get_option('kitcart_public_key') ?? "";
     294    $secret_key = get_option('kitcart_secret_key') ?? '';
     295    echo $message
     296
     297    ?>
     298
     299
     300
    170301    <h2>Kitcart API Keys</h2>
    171302    <table class="form-table">
     
    189320    </table>
    190321
    191     <?php
     322<?php
    192323}
    193324
    194325// The Final Order Sending part
    195 add_action('woocommerce_thankyou', 'create_new_order_on_kitcart', 10);
     326add_action('woocommerce_payment_complete', 'create_new_order_on_kitcart', 10);
     327add_action('woocommerce_order_status_completed', 'create_new_order_on_kitcart', 10);
     328add_action('woocommerce_order_status_processing', 'create_new_order_on_kitcart', 10);
     329add_action('woocommerce_order_status_on-hold', 'create_new_order_on_kitcart', 10);
     330add_action('woocommerce_order_status_pending', 'create_new_order_on_kitcart', 10);
     331add_action('woocommerce_order_status_failed', 'create_new_order_on_kitcart', 10);
     332add_action('woocommerce_order_status_cancelled', 'create_new_order_on_kitcart', 10);
     333add_action('woocommerce_order_status_refunded', 'create_new_order_on_kitcart', 10);
     334add_action('woocommerce_order_status_processing_to_completed', 'create_new_order_on_kitcart', 10);
     335add_action('woocommerce_order_status_completed_to_processing', 'create_new_order_on_kitcart', 10);
     336add_action('woocommerce_order_status_completed_to_on-hold', 'create_new_order_on_kitcart', 10);
     337add_action('woocommerce_order_status_completed_to_pending', 'create_new_order_on_kitcart', 10);
    196338function create_new_order_on_kitcart($order_id)
    197339{
     340    $route = "create-order";
    198341
    199342    if (class_exists('woocommerce')) {
     
    228371        );
    229372
    230         $response = wp_remote_post(KITCART_API_URI, $req_args);
    231         $body = wp_remote_retrieve_body($response);
    232     }
    233 }
     373        $response = wp_remote_post(KITCART_API_URI . $route, $req_args);
     374    }
     375}
Note: See TracChangeset for help on using the changeset viewer.