Skip to content

Commit

Permalink
initial commit - v 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuat committed Nov 29, 2017
0 parents commit 3b75d72
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Tracking Link Changelog

## 1.0.0 -- 2017-11-26

* Initial release
8 changes: 8 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2017 Joshua Turner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Tracking Link plugin for Craft CMS

Convert tracking numbers from USPS, UPS, FedEx, DHL, and OnTrac to the appropriate URL format

## Installation

To install Tracking Link, follow these steps:

1. Download & unzip the file. Rename directory to `trackinglink` and place into your `craft/plugins` directory
2. Install plugin in the Craft Control Panel under Settings > Plugins
3. The plugin folder should be named `trackinglink` for Craft to see it.

Tracking Link works on Craft 2.4.x and Craft 2.5.x.

## About

Effectively a port of darkain's [PHP Package Tracking URL Library](https://github.com/darkain/php-tracking-urls).
89 changes: 89 additions & 0 deletions TrackingLinkPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Tracking Link plugin for Craft CMS
*
* Convert tracking numbers from USPS, UPS, FedEx, DHL, and OnTrac to the appropriate URL format
*
* @author Joshua Turner
* @copyright Copyright (c) 2017 Joshua Turner
* @link joshuaturner.co
* @package TrackingLink
* @since 1.0.0
*/

namespace Craft;

class TrackingLinkPlugin extends BasePlugin
{
/**
* @return mixed
*/
public function init()
{
parent::init();
}

/**
* @return mixed
*/
public function getName()
{
return Craft::t('Tracking Link');
}

/**
* @return mixed
*/
public function getDescription()
{
return Craft::t('Convert tracking numbers from USPS, UPS, FedEx, DHL, and OnTrac to the appropriate URL format');
}

/**
* @return string
*/
public function getDocumentationUrl()
{
return 'https://github.com/joshua-turner/craft-trackinglink/blob/master/README.md';
}

/**
* @return string
*/
public function getVersion()
{
return '1.0.0';
}

/**
* @return string
*/
public function getSchemaVersion()
{
return '1.0.0';
}

/**
* @return string
*/
public function getDeveloper()
{
return 'Joshua Turner';
}

/**
* @return string
*/
public function getDeveloperUrl()
{
return 'joshuaturner.co';
}

/**
* @return bool
*/
public function hasCpSection()
{
return false;
}
}
75 changes: 75 additions & 0 deletions variables/TrackingLinkVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Tracking Link plugin for Craft CMS
*
* Tracking Link Variable
*
* @author Joshua Turner
* @copyright Copyright (c) 2017 Joshua Turner
* @link joshuaturner.co
* @package TrackingLink
* @since 1.0.0
*/

namespace Craft;

class TrackingLinkVariable
{
private static $providers = [
[
'name' => 'UPS',
'url' => 'http://wwwapps.ups.com/WebTracking/processInputRequest?TypeOfInquiryNumber=T&InquiryNumber1=',
'pattern' => '/\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|T\d{3} ?\d{4} ?\d{3})\b/i'
],
[
'name' => 'USPS',
'url' => 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=',
'pattern' => '/\b((420 ?\d{5} ?)?(91|92|93|94|01|03|04|70|23|13)\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4}( ?\d{2})?)\b/i'
],
[
'name' => 'USPS',
'url' => 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=',
'pattern' => '/\b((M|P[A-Z]?|D[C-Z]|LK|E[A-C]|V[A-Z]|R[A-Z]|CP|CJ|LC|LJ) ?\d{3} ?\d{3} ?\d{3} ?[A-Z]?[A-Z]?)\b/i'
],
[
'name' => 'USPS',
'url' => 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=',
'pattern' => '/\b(82 ?\d{3} ?\d{3} ?\d{2})\b/i'
],
[
'name' => 'FedEx',
'url' => 'http://www.fedex.com/Tracking?language=english&cntry_code=us&tracknumbers=',
'pattern' => '/\b(((96\d\d|6\d)\d{3} ?\d{4}|96\d{2}|\d{4}) ?\d{4} ?\d{4}( ?\d{3})?)\b/i'
],
[
'name' => 'OnTrac',
'url' => 'http://www.ontrac.com/trackres.asp?tracking_number=',
'pattern' => '/\b(C\d{14})\b/i'
],
[
'name' => 'DHL',
'url' => 'http://www.dhl.com/content/g0/en/express/tracking.shtml?brand=DHL&AWB=',
'pattern' => '/\b(\d{4}[- ]?\d{4}[- ]?\d{2}|\d{3}[- ]?\d{8}|[A-Z]{3}\d{7})\b/i'
],
];

public function getUrl($trackingNumber)
{
foreach (self::$providers as $provider) {
$match = [];
preg_match($provider['pattern'], $trackingNumber, $match);
if (count($match)) {
return [
'name' => $provider['name'],
'url' => $provider['url'] . preg_replace('/\s/', '', strtoupper($match[0]))
];
}
}

if (substr($trackingNumber, 0, 1) === '0') {
return $this->getUrl(ltrim($trackingNumber, '0'));
}

return false;
}
}

0 comments on commit 3b75d72

Please sign in to comment.