Plugin Directory

Changeset 2897663


Ignore:
Timestamp:
04/12/2023 08:17:13 AM (3 years ago)
Author:
rider7991
Message:

BASE64 and JSON are now used, readme.txt updated

Location:
share-that-cart/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • share-that-cart/trunk/readme.txt

    r2846570 r2897663  
    44Donate link: https://www.paypal.me/Rider7991
    55Requires at least: 4.0
    6 Tested up to: 6.1
     6Tested up to: 6.2
    77Requires PHP: 7.0
    88Stable tag: trunk
  • share-that-cart/trunk/share-that-cart.php

    r2846574 r2897663  
    33Plugin Name:    Share That Cart
    44Description:    This plugin allows you to share your cart via link with others.
    5 Version:        1.3.1
     5Version:        1.4.0
    66Author:         Mateusz Styrna
    77Author URI:     https://mateusz-styrna.pl/
     
    1111
    1212if ( !defined( 'SC_VERSION' ) ) {
    13     define( 'SC_VERSION', '1.0.1' );
     13    define( 'SC_VERSION', '1.2.0' );
    1414}
    1515
    16 add_action('woocommerce_after_cart_contents', 'sc_button');
     16add_action( 'woocommerce_after_cart_contents', 'sc_button' );
    1717add_action( 'wp_enqueue_scripts', 'sc_load_scripts' );
    18 add_action('wp_loaded', 'sc_add_products');
    19 register_activation_hook(__FILE__, 'sc_active');
     18add_action( 'wp_loaded', 'sc_add_products' );
     19register_activation_hook( __FILE__, 'sc_active' );
    2020
    2121function sc_active() {
    2222    if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    2323        deactivate_plugins( basename( __FILE__ ) );
    24         die(_e('Plugin NOT activated: Share That Cart requires the WooCommerce plugin to be installed and active.', 'share-that-cart'));
     24        die( _e( 'Plugin NOT activated: Share That Cart requires the WooCommerce plugin to be installed and active.', 'share-that-cart' ) );
    2525        return false;
    2626    } else {
     
    3131function sc_load_scripts() {
    3232    if ( is_cart() ) {
    33         wp_enqueue_style( 'sc-css', plugins_url('/style.css', __FILE__), '', SC_VERSION, false );
    34         wp_enqueue_script( 'sc-js', plugins_url('/copy.js', __FILE__), '', SC_VERSION, true );
     33        wp_enqueue_style( 'sc-css', plugins_url( '/style.css', __FILE__), '', SC_VERSION, false );
     34        wp_enqueue_script( 'sc-js', plugins_url( '/copy.js', __FILE__), '', SC_VERSION, true );
    3535    }
    3636}
    3737
    3838function sc_button() {
    39     $items = WC()->cart->get_cart();
     39    $cart_items = WC()->cart->get_cart();
    4040    $link = wc_get_cart_url();
    41     $ids = "";
    42     $quantities = "";
    43     $attr = "";
     41    $items = [];
    4442
    45     foreach($items as $item => $values) {
    46         $ids .= $values['product_id'] . ",";
    47         $quantities .= $values['quantity'] . ",";
     43    foreach( $cart_items as $cart_item => $values ) {
    4844
    49         if ($values['variation_id']) {
    50             $attr .= $values['product_id'] . "," . $values['variation_id'] . ",";
    51         }
     45        if ( $values[ 'variation_id' ] )
     46            $attr = $values[ 'variation_id' ];
     47        else
     48            $attr = false;
     49
     50        $item = ( object ) array( 'ID' => $values[ 'product_id' ], 'QTY' => $values[ 'quantity' ], 'ATTR' => $attr );
     51
     52        array_push( $items, $item );
    5253    }
    5354
    54     $link .= "?scp=" .substr($ids, 0, -1). "&scq=".substr($quantities, 0, -1) . "&sca=".substr($attr, 0, -1);
     55    $link .= "?scp=" . base64_encode( json_encode( $items ) );
    5556    ?>
    5657    <tr>
     
    6061                <button id="sc__button" class="sc__button">
    6162                    <?php
    62                     echo _e('Share That Cart', 'share-that-cart');
     63                    echo _e( 'Share That Cart', 'share-that-cart' );
    6364                    ?>
    6465                </button>
     
    6869    <div class="sc__popup" id="sc__popup">
    6970        <?php
    70         echo _e('Done! You can now share link to this cart by simply pasting it. It is in your clipboard!', 'share-that-cart');
     71        echo _e( 'Done! You can now share link to this cart by simply pasting it. It is in your clipboard!', 'share-that-cart' );
    7172        ?>
    7273    </div>
     
    7475}
    7576
    76 function sc_secure($arr) {
    77     $arrFinal = array();
    78     array_walk($arr, function($value,$key) use (&$arr, &$arrFinal){
    79         $value = intval($value);
    80         if ($value != 0) {
    81             array_push($arrFinal, $value);
    82         }
    83     });
    84     $arr = $arrFinal;
    85     return $arr;
     77function sc_add_products() {
     78    if ( !isset( $_GET[ 'scp' ] ) )
     79        return;
     80
     81    $items = json_decode( base64_decode( $_GET[ 'scp' ] ) );
     82
     83    foreach ( $items as $item ) {
     84        if ( $item->ATTR )
     85            WC()->cart->add_to_cart( intval( $item->ID ), intval( $item->QTY ), intval( $item->ATTR ) );
     86        else
     87            WC()->cart->add_to_cart(  intval( $item->ID ), intval( $item->QTY ) );
     88    }
     89
     90    wp_redirect( wc_get_cart_url() );
     91    exit;
    8692}
    8793
    88 function sc_add_products() {
    89     if (isset($_GET['scp']) && isset($_GET['scq'])) {
    90         $sc_products = sc_secure(explode(",", $_GET['scp']));
    91         var_dump($sc_products);
    92         $sc_qty = sc_secure(explode(",", $_GET['scq']));
    93         var_dump($sc_qty);
    94         $sc_attrs = sc_secure(explode(",", $_GET['sca']));
    95         var_dump($sc_attrs);
    96 
    97         $i = 0;
    98         while ($sc_products[$i]) {
    99 
    100             if (array_search($sc_products[$i],$sc_attrs) !== false) {
    101                 WC()->cart->add_to_cart( $sc_products[$i], $sc_qty[$i], $sc_attrs[array_search( $sc_products[$i], $sc_attrs ) + 1] );
    102                 \array_splice($sc_attrs, array_search($sc_products[$i],$sc_attrs), array_search($sc_products[$i],$sc_attrs)+1);
    103             }
    104             else {
    105                 WC()->cart->add_to_cart( $sc_products[$i], $sc_qty[$i] );
    106             }
    107             $i++;
    108         }
    109         wp_redirect(wc_get_cart_url());
    110         exit;
    111     }
    112 }
    113 
    114 load_plugin_textdomain('share-that-cart', false, basename( dirname( __FILE__ ) ) . '/languages' );
     94load_plugin_textdomain( 'share-that-cart', false, basename( dirname( __FILE__ ) ) . '/languages' );
    11595?>
Note: See TracChangeset for help on using the changeset viewer.