Plugin Directory

Changeset 2846806


Ignore:
Timestamp:
01/11/2023 02:03:16 PM (3 years ago)
Author:
viaads
Message:

Performance Update

Location:
viaads
Files:
15 added
5 edited

Legend:

Unmodified
Added
Removed
  • viaads/trunk/hooks/addCart.php

    r2845940 r2846806  
    2828            return;
    2929        }
    30 
    3130        $cookieValue = sanitize_text_field($_COOKIE[ 'ViaAds' ]);
    3231
  • viaads/trunk/hooks/pageLook.php

    r2845940 r2846806  
    1414    try {
    1515        //Get product
    16         global $product;
    17         if ( !isset( $product ) ) {
     16        if ( is_product() ) {
     17            global $post;
     18            $product_id = $post->ID;
     19        } else {
    1820            return;
    1921        }
    20         $product = new WC_Product( get_page_by_path( $product, OBJECT, 'product' )->ID );
     22        $product = wc_get_product( $product_id );
     23
    2124        //guid//email//emailGuid
    2225        if(!isset($_COOKIE[ 'ViaAds' ])) {
     
    4649            $data->user_agent = $userAgent;
    4750        }
    48        
    4951
    5052        //ProductPageUrl
     
    8082        $date = new DateTime();
    8183        $data->Event_date = $date->format( 'Y-m-d\TH:i:s' );
    82 
    8384       
    8485        ViaAds_PostToUrlEvent( "https://integration.viaads.dk/woocommerce/event", $data );
  • viaads/trunk/readme.txt

    r2845940 r2846806  
    44Requires at least: 5.4
    55Tested up to: 6.1
    6 Stable tag: 1.0.6
     6Stable tag: 1.0.7
    77Requires PHP: 7.0
    88License: GPLv3
  • viaads/trunk/sync.php

    r2845940 r2846806  
    22
    33namespace ViaAds;
     4
    45use stdClass;
    56use DateTime;
    67use Exception;
    78
    8 defined( 'ABSPATH' ) || exit;
    9 
    10 add_action( 'template_redirect', 'ViaAds\\ViaAds_syncData' );
    11 
    12 function ViaAds_syncData() {
     9defined('ABSPATH') || exit;
     10
     11add_action('template_redirect', 'ViaAds\\ViaAds_syncData');
     12
     13function ViaAds_syncData()
     14{
    1315    try {
    1416        //Check if key isset
    15         if ( !isset( $_GET[ 'apiKeyViabillMarketing' ] ) ) {
     17        if (!isset($_GET['apiKeyViabillMarketing'])) {
    1618            return;
    1719        }
     
    1921        //Check if key match
    2022        $name = "viaads_api_key";
    21         if ( $_GET[ 'apiKeyViabillMarketing' ] != get_option( $name ) ) {
     23        if ($_GET['apiKeyViabillMarketing'] != get_option($name)) {
    2224            return;
    2325        }
    24         if ( $_GET[ 'event' ] == "webshop" ) {
     26        if ($_GET['event'] == "webshop") {
    2527            $webshop = new stdClass();
    2628            $webshop->Type = "WooCommerce";
    27             $webshop->ApiKey = get_option( $name );
     29            $webshop->ApiKey = get_option($name);
    2830            $webshop->Categories = ViaAds_getCategories();
    2931            $webshop->Products = ViaAds_getProducts();
    30             ViaAds_PostToUrl( "https://integration.viaads.dk/woocommerce/WebShopData", $webshop, true );
    31         } else if ( $_GET[ 'event' ] == "orders" ) {
    32             ViaAds_PostToUrl( "https://integration.viaads.dk/woocommerce/WebShopOrderData", ViaAds_getOrders(), true );
     32            ViaAds_PostToUrl("https://integration.viaads.dk/woocommerce/WebShopData", $webshop, true);
     33        } else if ($_GET['event'] == "orders") {
     34            ViaAds_PostToUrl("https://integration.viaads.dk/woocommerce/WebShopOrderData", ViaAds_getOrders(), true);
    3335        } else {
    3436            return;
    3537        }
    36     } catch ( Exception $e ) {
     38    } catch (Exception $e) {
    3739        $error_object = new stdClass();
    3840        $error_object->Error = $e->getMessage();
    39        
    40         $currentPageUrl = sanitize_url( home_url( $_SERVER['REQUEST_URI'] ) );
     41
     42        $currentPageUrl = sanitize_url(home_url($_SERVER['REQUEST_URI']));
    4143        $error_object->Url = wp_http_validate_url($currentPageUrl);
    4244
    43         ViaAds_PostToUrlEvent( "https://integration.viaads.dk/error", $error_object );
     45        ViaAds_PostToUrlEvent("https://integration.viaads.dk/error", $error_object);
    4446    }
    4547
     
    4951
    5052
    51 function ViaAds_getProducts() {
     53function ViaAds_getProducts()
     54{
    5255    $args = array(
    5356        'orderby' => 'name',
    5457        'limit' => -1,
    5558    );
    56     $productCollection = wc_get_products( $args );
     59    $productCollection = wc_get_products($args);
    5760    $products = [];
    5861
    59     foreach ( $productCollection as $product ) {
     62    foreach ($productCollection as $product) {
    6063        $product_object = new stdClass();
    6164        $product_object->WebshopProductId = $product->get_id();
     
    6366        $imageArray = array();
    6467        $image_object = new stdClass();
    65         $image_object->Src = get_the_post_thumbnail_url( $product->get_id() );
    66         $image_object->Id = get_post_thumbnail_id( $product->get_id() );
    67         if ( $image_object->Src == false ) {
     68        $image_object->Src = get_the_post_thumbnail_url($product->get_id());
     69        $image_object->Id = get_post_thumbnail_id($product->get_id());
     70        if ($image_object->Src == false) {
    6871            $image_object->Src = "";
    6972        }
    70         array_push( $imageArray, $image_object );
     73        array_push($imageArray, $image_object);
    7174        $attachment_ids = $product->get_gallery_image_ids();
    72         foreach ( $attachment_ids as $attachment_id ) {
     75        foreach ($attachment_ids as $attachment_id) {
    7376            $image_object = new stdClass();
    74             $image_object->Src = wp_get_attachment_url( $attachment_id );
     77            $image_object->Src = wp_get_attachment_url($attachment_id);
    7578            $image_object->Id = $attachment_id;
    76             if ( $image_object->Src == false ) {
     79            if ($image_object->Src == false) {
    7780                $image_object->Src = "";
    7881            }
    79             array_push( $imageArray, $image_object );
     82            array_push($imageArray, $image_object);
    8083        }
    8184        $product_object->ProductImages = $imageArray;
     
    8386        //Categories
    8487        $categories = array();
    85         foreach ( get_the_terms( $product->get_id(), 'product_cat' ) as $category ) {
     88        foreach (get_the_terms($product->get_id(), 'product_cat') as $category) {
    8689            $category_object = new stdClass();
    8790            $category_object->WebshopCategoryId = $category->term_id;
    8891            $category_object->Name = $category->name;
    8992            $category_object->Slug = $category->slug;;
    90             array_push( $categories, $category_object );
     93            array_push($categories, $category_object);
    9194        }
    9295        $product_object->Categories = $categories;
     
    9598        $product_object->AverageRating = $product->get_average_rating();
    9699        $product_object->Name = $product->get_name();
    97         $product_object->Permalink = get_permalink( $product->get_id() );
     100        $product_object->Permalink = get_permalink($product->get_id());
    98101        // SKU
    99102        $product_object->Sku = $product->get_sku();
    100103        // Updated At
    101         $lastModifiedGmt = new DateTime( $product->get_date_modified() );
    102         $product_object->LastModifiedGmt = $lastModifiedGmt->format( 'c' );
     104        $lastModifiedGmt = new DateTime($product->get_date_modified());
     105        $product_object->LastModifiedGmt = $lastModifiedGmt->format('c');
    103106        // Created At
    104         $createdAt = new DateTime( $product->get_date_created() );
    105         $product_object->CreatedGmt = $createdAt->format( 'c' );
     107        $createdAt = new DateTime($product->get_date_created());
     108        $product_object->CreatedGmt = $createdAt->format('c');
    106109        // Status
    107110        $product_object->Status = $product->get_status();
     
    120123        //Price
    121124        $product_object->price = $product->get_price();
     125        if ($product_object->price == "") {
     126            $product_object->price = null;
     127        }
    122128        //Regular Price
    123129        $product_object->regularPrice = $product->get_regular_price();
    124         //SalePrice
    125         $product_object->salePrice = $product->get_sale_price();
    126 
    127         if ( $product->get_sale_price() != 0 ) {
     130        if ($product_object->regularPrice == "") {
     131            $product_object->regularPrice = null;
     132        }
     133
     134        if ($product->get_sale_price() != 0) {
    128135            // On Sale
    129136            $product_object->OnSale = true;
     
    131138            $product_object->SalePrice = $product->get_sale_price();
    132139            // On Sale From Date
    133             $dateOnSaleFrom = new DateTime( $product->get_date_on_sale_from() );
    134             $product_object->DateOnSaleFrom = $dateOnSaleFrom->format( 'c' );
     140            $dateOnSaleFrom = new DateTime($product->get_date_on_sale_from());
     141            $product_object->DateOnSaleFrom = $dateOnSaleFrom->format('c');
    135142            // On Sale To Date
    136             $dateOnSaleTo = new DateTime( $product->get_date_on_sale_to() );
    137             $product_object->DateOnSaleTo = $dateOnSaleTo->format( 'c' );
     143            $dateOnSaleTo = new DateTime($product->get_date_on_sale_to());
     144            $product_object->DateOnSaleTo = $dateOnSaleTo->format('c');
    138145        } else {
    139146            // On Sale
     
    141148        }
    142149
    143         if ( $product_object->Type != 'simple' ) {
     150        if ($product_object->Type != 'simple') {
    144151            $children = $product->get_children();
    145152            $i = 1;
     
    147154            $highestPrice;
    148155            $childs = [];
    149             foreach ( $children as $childRaw ) {
     156            foreach ($children as $childRaw) {
    150157                $child_object = new stdClass();
    151                 $child = wc_get_product( $childRaw );
     158                $child = wc_get_product($childRaw);
    152159
    153160                // Product Id
     
    162169                $child_object->ShortDescription = $child->get_short_description();
    163170                // Permalink
    164                 $child_object->Permalink = get_permalink( $child->get_id() );
     171                $child_object->Permalink = get_permalink($child->get_id());
    165172                // Type
    166173                $child_object->Type = $child->get_type();
     
    168175                $child_object->Status = $child->get_status();
    169176                // Modified At
    170                 $dateModified = new DateTime( $child->get_date_modified() );
    171                 $child_object->DateModified = $dateModified->format( 'c' );
     177                $dateModified = new DateTime($child->get_date_modified());
     178                $child_object->DateModified = $dateModified->format('c');
    172179                // Created At
    173                 $createdAt = new DateTime( $child->get_date_created() );
    174                 $child_object->DateCreated = $createdAt->format( 'c' );
     180                $createdAt = new DateTime($child->get_date_created());
     181                $child_object->DateCreated = $createdAt->format('c');
    175182                // Gets the products Final Price
    176183                $childPrice = $child->get_price();
     
    178185                // Regular Price
    179186                $child_object->RegularPrice = $child->get_price();
     187                if ($child_object->RegularPrice == ""){
     188                    $child_object->RegularPrice = null;
     189                }
     190
    180191                // On Sale Data
    181                 if ( $product->get_sale_price() != 0 ) {
     192                if ($product->get_sale_price() != 0) {
    182193                    // On Sale
    183194                    $child_object->OnSale = true;
     
    185196                    $child_object->SalePrice = $child->get_sale_price();
    186197                    // On Sale From Date
    187                     $dateOnSaleFrom = new DateTime( $child->get_date_on_sale_from() );
    188                     $child_object->DateOnSaleFrom = $dateOnSaleFrom->format( 'c' );
     198                    $dateOnSaleFrom = new DateTime($child->get_date_on_sale_from());
     199                    $child_object->DateOnSaleFrom = $dateOnSaleFrom->format('c');
    189200                    // On Sale To Date
    190                     $dateOnSaleTo = new DateTime( $child->get_date_on_sale_to() );
    191                     $child_object->DateOnSaleTo = $dateOnSaleTo->format( 'c' );
     201                    $dateOnSaleTo = new DateTime($child->get_date_on_sale_to());
     202                    $child_object->DateOnSaleTo = $dateOnSaleTo->format('c');
    192203                } else {
    193204                    $child_object->OnSale = false;
     
    204215
    205216                // Getting the Lowest and the Highest price from Child (Variant) Products
    206                 if ( $i == 1 ) {
     217                if ($i == 1) {
    207218                    $lowestPrice = $childPrice;
    208219                    $highestPrice = $childPrice;
    209220                } else {
    210                     if ( $childPrice < $lowestPrice )
     221                    if ($childPrice < $lowestPrice)
    211222                        $lowestPrice = $childPrice;
    212                     if ( $childPrice > $highestPrice )
     223                    if ($childPrice > $highestPrice)
    213224                        $highestPrice = $childPrice;
    214225                }
    215226
    216                 array_push( $childs, $child_object );
     227                array_push($childs, $child_object);
    217228                $i++;
    218229            }
     
    226237        }
    227238
    228         array_push( $products, $product_object );
     239        array_push($products, $product_object);
    229240    }
    230241
     
    232243}
    233244
    234 function ViaAds_getCategories() {
     245function ViaAds_getCategories()
     246{
    235247    $categories = [];
    236248
     
    252264        'hide_empty' => $empty
    253265    );
    254     $all_categories = get_categories( $args );
    255     foreach ( $all_categories as $cat ) {
     266    $all_categories = get_categories($args);
     267    foreach ($all_categories as $cat) {
    256268        $category_id = $cat->term_id;
    257269        $category_object = new stdClass();
     
    265277        // Parent Id
    266278        $category_object->ParentId = $cat->parent;
    267         array_push( $categories, $category_object );
     279        array_push($categories, $category_object);
    268280    }
    269281    return $categories;
    270282}
    271283
    272 function ViaAds_getOrders() {
    273     $orderCollection = wc_get_orders( array(
    274         'date_modified' => date( 'Y-m-d', strtotime( date( "Y-m-d" ) . ' - 1 days' ) ) . '...'
    275         . date( 'Y-m-d', strtotime( date( "Y-m-d" ) . ' + 14 days' ) ),
    276     ) );
     284function ViaAds_getOrders()
     285{
     286    $orderCollection = wc_get_orders(array(
     287        'date_modified' => date('Y-m-d', strtotime(date("Y-m-d") . ' - 1 days')) . '...'
     288            . date('Y-m-d', strtotime(date("Y-m-d") . ' + 14 days')),
     289    ));
    277290    //Test
    278291    /*$orderCollection = wc_get_orders( array(
     
    282295
    283296    $orders = [];
    284     foreach ( $orderCollection as $order ) {
     297    foreach ($orderCollection as $order) {
    285298        $order_object = new stdClass();
    286299        // Order Number
     
    299312        $order_object->Currency = $order->get_currency();
    300313        // Last Modified
    301         $lastModifiedGmt = new DateTime( $order->get_date_modified() );
    302         $order_object->Last_modified = $lastModifiedGmt->format( 'c' );
     314        $lastModifiedGmt = new DateTime($order->get_date_modified());
     315        $order_object->Last_modified = $lastModifiedGmt->format('c');
    303316
    304317        // Billing Address
     
    330343
    331344        $orderItems = [];
    332         foreach ( $order->get_items() as $item_id => $item ) {
     345        foreach ($order->get_items() as $item_id => $item) {
    333346            $order_item_object = new stdClass();
    334347            // Product Id
     
    344357            $order_item_object->Price = $item->get_subtotal() / $item->get_quantity();
    345358            // Quantity
    346             $order_item_object->Quantity = round( $item->get_quantity() );
     359            $order_item_object->Quantity = round($item->get_quantity());
    347360            // Total
    348361            //$order_item_object->Total_price = number_format( $item->get_total(), 2, ".", "" );
     
    353366            $order_item_object->Total_price_tax_included = $item->get_total() + $item->get_total_tax();
    354367
    355             array_push( $orderItems, $order_item_object );
     368            array_push($orderItems, $order_item_object);
    356369        }
    357370
     
    361374        // Final infos
    362375        $orderFinal = new stdClass();
    363         $orderFinal->ApiKey = get_option( "viaads_api_key" );
     376        $orderFinal->ApiKey = get_option("viaads_api_key");
    364377        $orderFinal->Shop_order = $order_object;
    365378
     
    372385
    373386        // Order Date
    374         $createdAt = new DateTime( $order->get_date_created() );
    375         $orderFinal->Order_date = $createdAt->format( 'c' );
     387        $createdAt = new DateTime($order->get_date_created());
     388        $orderFinal->Order_date = $createdAt->format('c');
    376389        // Customer
    377390        $customer = new stdClass();
     
    379392        $orderFinal->customer = $customer;
    380393
    381         array_push( $orders, $orderFinal );
     394        array_push($orders, $orderFinal);
    382395    }
    383396    return $orders;
  • viaads/trunk/viaads.php

    r2845940 r2846806  
    33 * Plugin Name: ViaAds
    44 * Description: Plugin der muliggør forbindelsen til ViaAds / Plug-in enabling the connection to ViaAds.
    5  * Version: 1.0.6
     5 * Version: 1.0.7
    66 * Author: ViaAds
    77 * Author URI: https://www.viaads.dk/
Note: See TracChangeset for help on using the changeset viewer.