1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
<?php
include_once dirname(__FILE__) . '/functions_options.php';
include_once dirname(__FILE__) . '/functions_service.php';
include_once dirname(__FILE__) . '/functions_i18n.php';
if (!function_exists('asa2_pm')) {
/**
* Shortcut to retrieve ASA2's plugin manager
* @since 1.8.0
* @ignore
* @return Ifw_Wp_Plugin_Manager
*/
function asa2_pm() {
if (class_exists('Ifw_Wp_Plugin_Manager')) {
return Ifw_Wp_Plugin_Manager::getInstance('Asa2');
}
}
}
if (!function_exists('asa2_config')) {
/**
* Shortcut to retrieve ASA2's config object
* @since 1.8.0
* @ignore
* @return Ifw_Wp_Plugin_Config
*/
function asa2_config() {
if (class_exists('Ifw_Wp_Plugin_Manager')) {
return Ifw_Wp_Plugin_Manager::getInstance('Asa2')->getConfig();
}
}
}
if (!function_exists('asa2_env')) {
/**
* Shortcut to retrieve ASA2's environment object
*
* @since 1.8.0
* @ignore
* @return Ifw_Wp_Env_Plugin
*/
function asa2_env() {
if (class_exists('Ifw_Wp_Plugin_Manager')) {
return Ifw_Wp_Plugin_Manager::getInstance('Asa2')->getEnv();
}
}
}
if (!function_exists('asa2_default_error')) {
/**
* @package Log
* @ignore Can be deleted in future versions
* @deprecated Use function asa2_log_default_error
*
* @param string $title Error title
* @param string $msg Error message
*/
function asa2_default_error($title, $msg) {
apply_filters('asa2_default_error', $title, $msg);
}
}
if (!function_exists('asa2_log_default_error')) {
/**
* Creates default error fetched by ASA2's logger.
*
* Example:
* ```php
* asa2_log_default_error(__('Unknown Associate ID set', 'asa2'), __('No Associate ID set could be found for the value: ', 'asa2') . $setId);
* ```
*
* @since 1.8.1
* @package Log
* @ignore Activate with version 1.8.1
*
* @param string $title Error title
* @param string $msg Error message
*/
function asa2_log_default_error($title, $msg) {
apply_filters('asa2_default_error', $title, $msg);
}
}
if (!function_exists('asa2_get_associate_id_for_url')) {
/**
* Retrieves the Associate ID that best matches an Amazon URL.
*
* @since 1.8.0
* @package Helper
*
* @param string $url Amazon URL.
* @param integer $associate_set_id Optional. ID of associate ID set to use.
* @return null|string
*/
function asa2_get_associate_id_for_url($url, $associate_set_id = null) {
if (class_exists('Asa2_AssociateIdSet')) {
return Asa2_AssociateIdSet::getInstance()->getAssociateIdForUrl($url, $associate_set_id);
}
return null;
}
}
if (!function_exists('asa2_replace_tracking_id')) {
/**
* Replaces the tracking ID in an Amazon URL with the given one.
*
* Example:
* ```php
* $url = asa2_replace_tracking_id('https://www.amazon.com/dp/B008JAAT5Y/tag=evil-tag-21', 'my-own-id-21');
* // $url will contain "https://www.amazon.com/dp/B008JAAT5Y/tag=my-own-id-21"
* ```
*
* @since 1.8.0
* @package Helper
*
* @param string $url Amazon URL.
* @param string $newTrackingId Optional. The new tracking ID which should be used in the given URL. If unused, the default tracking ID is used.
* @param bool $addIfNoneWasFound Optional. Add the given tracking ID even when no tracking ID was found in the URL. Default: true.
* @return string The URL that may have been modified.
*/
function asa2_replace_tracking_id($url, $newTrackingId = null, $addIfNoneWasFound = true) {
if (class_exists('Asa2_Service_Amazon_UrlParser')) {
return Asa2_Service_Amazon_UrlParser::replaceTrackingId($url, $newTrackingId, $addIfNoneWasFound);
}
return $url;
}
}
if (!function_exists('asa2_filter_asin')) {
/**
* Filters an Amazon ASIN. It removes invalid characters.
*
* Example:
* ```php
* $asin = asa2_filter_asin('B01M2AYHBV/');
* // $asin will contain 'B01M2AYHBV'
* ```
*
* @since 1.8.0
* @package Helper
*
* @param string $asin Amazon ASIN.
* @return string The filtered ASIN.
*/
function asa2_filter_asin($asin) {
if (!asa2_option_is_disable_asin_filter()) {
if (strpos($asin, ',') !== false) {
// comma found, might be a list of ASINs
$filteredAsinList = array();
foreach (explode(',', $asin) as $asinFromList) {
array_push($filteredAsinList, asa2_filter_asin($asinFromList));
}
return implode(',', $filteredAsinList);
} else {
// single ASIN
$unfiltered = $asin;
$asin = preg_replace("/[^a-zA-Z0-9]+/", '', $asin);
if ((strlen($asin) != 10 || strlen($unfiltered) != 10) && get_transient('asa2_invalid_asin_log_'. $asin) == false) {
$msg = __('ASA2\'s ASIN filter detected a probably invalid ASIN.', 'asa2') . '[br][br]';
$msg .= __('Reason', 'asa2') . ': ' . __('An ASIN must have ten characters.', 'asa2') . '[br][br]';
$msg .= __('Unfiltered (before)', 'asa2') . ': ' . $unfiltered . '[br]';
$msg .= __('Filtered (after)', 'asa2') . ': ' . $asin . '[br]';
asa2_default_error(__('Probably invalid ASIN detected', 'asa2') . ': ' . $asin, $msg);
set_transient('asa2_invalid_asin_log_'. $asin, true, 3600);
}
}
}
return $asin;
}
}
if (!function_exists('asa2_is_asin')) {
/**
* Checks if a string is a valid ASIN.
*
* @since 1.8.0
* @package Helper
*
* @param string $asin Amazon ASIN
* @return bool
*/
function asa2_is_asin($asin) {
return strlen($asin) == 10;
}
}
if (!function_exists('asa2_render_asin')) {
/**
* Renders a single product based on an ASIN and optional country code like shortcode [[asa2]](http://docs.getasa2.com/shortcodes_asa2.html) would do.
*
* Example:
* ```php
* // with default template:
* echo asa2_render_asin('B01A9EN4YU', array('country_code' => 'DE'));
*
* // with custom tempalte:
* echo asa2_render_asin('B01A9EN4YU', array(
* 'country_code' => 'DE',
* 'tpl' => 'my_template'
* ));
* ```
*
* @since 1.8.0
* @package Renderer
* @link http://docs.getasa2.com/php_functions_shortcode_rendering.html#asa2-renderer-asin-render
*
* @param string $asin Amazon ASIN
* @param array $options Optional. An array of options. Supports the same values as the [asa2] shortcode.
* @param array $isCacheOptions Optional. Array of options that should be used for generating a cache token. If empty, ASIN an options are used.
* @param array $debug Optional. Additional information for cache debug logger.
* @return string The generated output
*/
function asa2_render_asin($asin, $options = array(), $isCacheOptions = array(), $debug = array()) {
if (class_exists('Asa2_Renderer_Asin')) {
return Asa2_Renderer_Asin::render($asin, $options, $isCacheOptions, $debug);
}
}
}
if (!function_exists('asa2_collection_exists')) {
/**
* Checks if a collection exists.
*
* Example:
* ```php
* if (asa2_collection_exists('my_collection_name')) {
* // collections exists, do something here
* }
* ```
*
* @since 1.8.0
* @package Helper
*
* @param string $name Collection name
* @param int $excludeId Optional. ID of a collection that should be excluded from search.
* @return bool
*/
function asa2_collection_exists($name, $excludeId = null) {
if (class_exists('Asa2_Model_Mapper_Collection')) {
return Asa2_Model_Mapper_Collection::exists($name, $excludeId);
}
}
}
if (!function_exists('asa2_render_collection')) {
/**
* Renders a collection like shortcode [[asa2_collection]](http://docs.getasa2.com/shortcodes_asa2_collection.html) would do.
*
* Example:
* ```php
* // with default template:
* echo asa2_render_collection('my_collection_name');
*
* // with custom template and some options:
* echo asa2_render_collection('my_collection_name', array(
* 'tpl' => 'my_collection_tpl',
* 'orderby' => 'rand',
* 'limit' => 3
* ));
* ```
*
* @since 1.8.0
* @package Renderer
*
* @param string $collection Collection name or ID.
* @param array $options Optional.
* @return string The generated output.
* @throws Asa2_Renderer_Exception If $collection is empty.
*/
function asa2_render_collection($collection, $options = array()) {
if (class_exists('Asa2_Renderer_Collection')) {
return Asa2_Renderer_Collection::render($collection, $options);
}
}
}
if (!function_exists('asa2_render_smart_collection')) {
/**
* Renders a smart collection like shortcode [[asa2_smart_collection]](http://docs.getasa2.com/shortcodes_asa2_smart_collection.html) would do.
*
* Example:
* ```php
* echo asa2_render_smart_collection(array(
* 'cat_slug' => 'games',
* 'limit' => 10,
* 'is_available_main' => true,
* 'is_prime' => true,
* 'rating_gt' => 3,
* 'orderby' => 'rand',
* 'tplid' => 46
* ));
* ```
* @param array $options Smart collection options.
* @since 1.8.0
* @package Renderer
* @return string The generated output.
*/
function asa2_render_smart_collection(array $options) {
if (class_exists('Asa2_Module_Repo_Renderer_SmartCollection')) {
return Asa2_Module_Repo_Renderer_SmartCollection::render($options);
}
}
}
if (!function_exists('asa2_render_image')) {
/**
* Renders a single image like shortcode [[asa2_img]](http://docs.getasa2.com/shortcodes_asa2_img.html) would do.
*
* Example:
* ```php
* echo asa2_img('B01M2AYHBV', array(
* 'country_code' => 'UK',
* 'img' => 4,
* 'size' => 'LargeImage',
* 'width' => 350,
* 'height' => 180,
* 'tpl' => 'custom_img_tpl'
* ));
* ```
*
* @since 1.8.0
* @package Renderer The generated output.
*
* @param string $asin Amazon ASIN.
* @param array $options Options defining the image to be displayed.
* @return string The generated output.
*/
function asa2_render_image($asin, array $options = array()) {
if (class_exists('Asa2_Renderer_Asin')) {
$item = asa2_item_lookup($asin, $options);
if (asa2_is_item_object($item)) {
return Asa2_Renderer_Asin::renderImage($item, $options);
}
}
}
}
if (!function_exists('asa2_is_supported_country_code')) {
/**
* Checks if a country code is valid and supported by the Amazon API.
*
* @since 1.8.0
* @package Helper
*
* @param $countryCode
* @return bool
*/
function asa2_is_supported_country_code($countryCode) {
if (class_exists('Asa2_Service_Stores')) {
return Asa2_Service_Stores::isSupportedCountryCode($countryCode);
}
}
}
if (!function_exists('asa2_get_default_country_code')) {
/**
* Retrieve the default country code configured in the ASA2 setup.
*
* @since 1.8.0
* @package Helper
*
* @return string The default country code.
*/
function asa2_get_default_country_code() {
if (class_exists('Asa2_Service_Amazon_Credentials')) {
return Asa2_Service_Amazon_Credentials::getInstance()->getDefaultCountryStore();
}
}
}
if (!function_exists('asa2_is_item_object')) {
/**
* Checks if $item is a valid ASA2 item object which is an object of type Asa2_Service_Amazon_Item_Wrapper_Abstract.
*
* Example:
* ```php
* if (asa2_is_item_object($item)) {
* echo $item->getASIN();
* }
* ```
* @since 1.8.0
* @package Helper
*
* @param object $item An item object, e.g. a result from asa2_item_lookup function.
* @return bool
*/
function asa2_is_item_object($item) {
if (class_exists('Asa2_Service')) {
return Asa2_Service::isItem($item);
}
}
}
if (!function_exists('asa2_save_repo_item'))
{
/**
* @param $asin
* @param $countryCode
* @param array $options
* @ignore
* @deprecated use asa2_repo_save_item instead
* @return bool|int|null|WP_Error
*/
function asa2_save_repo_item($asin, $countryCode, $options = array()) {
return asa2_repo_save_item($asin, $countryCode, $options);
}
}
if (!function_exists('asa2_repo_save_item')) {
/**
* Stores a product in the Repo, based on ASIN and country code.
*
* Example:
* ```php
* $result = asa2_save_repo_item('B01M2AYHBV', 'UK');
* ```
*
* @since 1.8.1
* @package Repo
*
* @param string $asin Amazon ASIN
* @param string $countryCode A supported country code
* @param array $options Optional. Additional item parameters. (Not supported yet)
* @return bool|int|null|WP_Error Returns true if the article was successfully queued for saving.
*/
function asa2_repo_save_item($asin, $countryCode, $options = array()) {
if (class_exists('Asa2_Module_Repo_Model_Repo')) {
if (asa2_is_asin($asin) && asa2_is_supported_country_code($countryCode)) {
$repoItem = new Asa2_Module_Repo_Model_Repo();
$repoItem->setASIN($asin, $countryCode);
return $repoItem->save();
}
}
return null;
}
}