Plugin Directory

Changeset 2115208


Ignore:
Timestamp:
07/01/2019 05:57:04 AM (7 years ago)
Author:
dstitan
Message:

Improve logging

Location:
ds-titan
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • ds-titan/tags/0.2/src/DSTitan.php

    r2112597 r2115208  
    191191    public function pushWebhook($req)
    192192    {
     193        // Log everything
     194        $appLogs = get_option(Products::APP_LOG_KEY, []);
     195        $logs = [];
    193196        try {
    194197            $rawBody = $req->get_body();
     
    197200            // Get the body id
    198201            $productIds = $body['productIds'];
     202
     203            $logs[] = 'Products pushed from DSTitan:' . implode($productIds);
    199204        } catch(\Exception $e) {
    200205            return new \WP_Error('invalid_post_body', 'Sorry the post body is invalid', [
     
    207212        $productMapping = get_option(Products::PRODUCT_MAPPING_KEY, []);
    208213
     214        // Go through each productIds
     215        $newProducts = [];
     216
     217        // Get new instance of api
     218        $accessToken = get_option(self::PLUGIN_ID . '_access_token');
     219        $Api = new API($accessToken);
     220
     221        foreach ($productIds as $productId) {
     222            try {
     223                $newProducts[$productId] = $Api->getProduct($productId);
     224            } catch (\Exception $e) {
     225                $logs[] = "Failed to fetch product detail from DSTitan for: $productId";
     226            }
     227        }
     228
     229        // Save the log
     230        $appLogs[time()] = $logs;
     231        update_option(Product::APP_LOG_KEY, $appLogs);
     232
     233        // Try to save the products
    209234        try {
    210             // Get new instance of api
    211             $accessToken = get_option(self::PLUGIN_ID . '_access_token');
    212             $Api = new API($accessToken);
    213 
    214             // Go through each productIds
    215             $newProducts = [];
    216             foreach ($productIds as $productId) {
    217             $newProducts[$productId] = $Api->getProduct($productId);
    218             }
    219 
    220235            // Save the products
    221236            $Products = new Products();
  • ds-titan/tags/0.2/src/Products.php

    r2112597 r2115208  
    6464                }
    6565                $productMapping[$rawProduct['id']][] = $productId;
     66            } else {
     67                $this->logs[] = "Failed create product for " . $rawProduct['id'];
    6668            }
    6769
     
    497499
    498500            // Log it
     501            /*
    499502            if ($newVariation) {
    500503                $this->logs[] = "Created variation: $variationId for parent product: $parentId";
     
    502505                $this->logs[] = "Updated variation: $variationId for parent product: $parentId";
    503506            }
     507             */
    504508        }
    505509    }
  • ds-titan/tags/0.2/views/admin.php

    r2112592 r2115208  
    4848
    4949        <br />
     50        <?php /*
    5051        <?php if ($accessToken) { ?>
    5152            <form action="<?= esc_url(admin_url('admin-post.php')) ?>">
     
    5556            </form>
    5657        <?php } ?>
     58         */ ?>
    5759
    5860        <br />
  • ds-titan/trunk/src/DSTitan.php

    r2112597 r2115208  
    191191    public function pushWebhook($req)
    192192    {
     193        // Log everything
     194        $appLogs = get_option(Products::APP_LOG_KEY, []);
     195        $logs = [];
    193196        try {
    194197            $rawBody = $req->get_body();
     
    197200            // Get the body id
    198201            $productIds = $body['productIds'];
     202
     203            $logs[] = 'Products pushed from DSTitan:' . implode($productIds);
    199204        } catch(\Exception $e) {
    200205            return new \WP_Error('invalid_post_body', 'Sorry the post body is invalid', [
     
    207212        $productMapping = get_option(Products::PRODUCT_MAPPING_KEY, []);
    208213
     214        // Go through each productIds
     215        $newProducts = [];
     216
     217        // Get new instance of api
     218        $accessToken = get_option(self::PLUGIN_ID . '_access_token');
     219        $Api = new API($accessToken);
     220
     221        foreach ($productIds as $productId) {
     222            try {
     223                $newProducts[$productId] = $Api->getProduct($productId);
     224            } catch (\Exception $e) {
     225                $logs[] = "Failed to fetch product detail from DSTitan for: $productId";
     226            }
     227        }
     228
     229        // Save the log
     230        $appLogs[time()] = $logs;
     231        update_option(Product::APP_LOG_KEY, $appLogs);
     232
     233        // Try to save the products
    209234        try {
    210             // Get new instance of api
    211             $accessToken = get_option(self::PLUGIN_ID . '_access_token');
    212             $Api = new API($accessToken);
    213 
    214             // Go through each productIds
    215             $newProducts = [];
    216             foreach ($productIds as $productId) {
    217             $newProducts[$productId] = $Api->getProduct($productId);
    218             }
    219 
    220235            // Save the products
    221236            $Products = new Products();
  • ds-titan/trunk/src/Products.php

    r2112597 r2115208  
    6464                }
    6565                $productMapping[$rawProduct['id']][] = $productId;
     66            } else {
     67                $this->logs[] = "Failed create product for " . $rawProduct['id'];
    6668            }
    6769
     
    497499
    498500            // Log it
     501            /*
    499502            if ($newVariation) {
    500503                $this->logs[] = "Created variation: $variationId for parent product: $parentId";
     
    502505                $this->logs[] = "Updated variation: $variationId for parent product: $parentId";
    503506            }
     507             */
    504508        }
    505509    }
  • ds-titan/trunk/views/admin.php

    r2112580 r2115208  
    4848
    4949        <br />
     50        <?php /*
    5051        <?php if ($accessToken) { ?>
    5152            <form action="<?= esc_url(admin_url('admin-post.php')) ?>">
     
    5556            </form>
    5657        <?php } ?>
     58         */ ?>
    5759
    5860        <br />
Note: See TracChangeset for help on using the changeset viewer.