Created
February 22, 2023 08:47
-
-
Save michaeluno/27f0dbd8aa4b472c36263f0434e6fde7 to your computer and use it in GitHub Desktop.
An Auto Amazon Links extension WordPress plugin that allows you to ser a custom product URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Auto Amazon Links - Custom Product URL | |
* Description: Sets a custom product URL. Edit the like of the plugin file saying "MODIFY THIS LINE". | |
* Version: 0.0.1 | |
*/ | |
namespace AutoAmazonLinks\CustomProductURL; | |
class App { | |
public function run() { | |
add_action( 'plugins_loaded', [ $this, 'load' ] ); | |
} | |
public function load () { | |
if ( ! class_exists( '\AmazonAutoLinks_Registry' ) ) { | |
return; | |
} | |
add_filter( 'aal_filter_product_link', [ $this, 'getCustomProductURL' ], 10, 1 ); | |
add_filter( 'aal_filter_unit_item_format_tag_replacements', [ $this, 'getProductElementReplacements' ], 10, 1 ); | |
} | |
public function getTheCustomURL() { | |
static $sCustomURL; | |
$sCustomURL = $sCustomURL | |
? $sCustomURL | |
: get_site_url( null, 'product1' ); // <-- MODIFY THIS LINE | |
return $sCustomURL; | |
} | |
public function getCustomProductURL( $sURL /* , $sURLRaw, $sASIN, $aUnitOptions, $sLanguageCode, $sCurrency */ ) { | |
return $this->getTheCustomURL(); | |
} | |
public function getProductElementReplacements( $aReplacements /* , $aProduct, $oUnitOutput */ ) { | |
$aReplacements[ '%rating%' ] = $this->___getLinkConverted( $aReplacements[ '%rating%' ] ); | |
return $aReplacements; | |
} | |
/** | |
* @param string $sHTML | |
* @return string | |
*/ | |
private function ___getLinkConverted( $sHTML ) { | |
return preg_replace_callback( $this->___getPattern(), [ $this, 'getLinkReplaced' ], $sHTML ); | |
} | |
public function getLinkReplaced( $aMatches ) { | |
return $aMatches[ 1 ] . $this->getTheCustomURL() . $aMatches[ 4 ]; | |
} | |
/** | |
* @return string A regex pattern. | |
*/ | |
private function ___getPattern() { | |
$_sPatternHref = "("; // first element open | |
$_sPatternHref .= "<"; // 1 start of the tag | |
$_sPatternHref .= "\s*"; // 2 zero or more whitespace | |
$_sPatternHref .= "a"; // 3 the a of the tag itself | |
$_sPatternHref .= "\s+"; // 4 one or more whitespace | |
$_sPatternHref .= "[^>]*"; // 5 zero or more of any character that is _not_ the end of the tag | |
$_sPatternHref .= "href"; // 6 the href bit of the tag | |
$_sPatternHref .= "\s*"; // 7 zero or more whitespace | |
$_sPatternHref .= "="; // 8 the = of the tag | |
$_sPatternHref .= "\s*"; // 9 zero or more whitespace | |
$_sPatternHref .= '["\']?'; // 10 none or one of " or ' opening quote | |
$_sPatternHref .= ')'; // first element close | |
$_sPatternHref .= "("; // second element open | |
$_sPatternHref .= 'https?:\/\/(www\.)?amazon\.[^"\' >]+'; // URL | |
$_sPatternHref .= ")"; // second element close | |
$_sPatternHref .= "("; // fourth element | |
$_sPatternHref .= '["\' >]'; // 14 closing characters of the bit we want to capture | |
$_sPatternHref .= ')'; // fourth element close | |
$_sNeedle = "/"; // regex start delimiter | |
$_sNeedle .= $_sPatternHref; | |
$_sNeedle .= "/"; // regex end delimiter | |
$_sNeedle .= "i"; // Pattern Modifier - makes regex case insensative | |
$_sNeedle .= "s"; // Pattern Modifier - makes a dot metacharater in the pattern | |
// match all characters, including newlines | |
$_sNeedle .= "U"; // Pattern Modifier - makes the regex ungready | |
return $_sNeedle; | |
} | |
} | |
$_oApp = new App; | |
$_oApp->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment