= 3.0 and PHP >= 5.1.2
*/
// Define constants
define("AMAZONAUTOLINKSKEY", "amazonautolinks");
define("AMAZONAUTOLINKSPLUGINNAME", "Amazon Auto Links");
// Embed Plugin Settings Link
add_filter("plugin_action_links_" . plugin_basename(__FILE__), 'AmazonAutoLinks_SettingsLink' );
// Load actions to hook events for Cron jobs
add_action('init', 'AmazonAutoLinks_LoadActions');
// Plugin Requirements
add_action('admin_init', 'AmazonAutoLinks_Requirements');
// Register Classes
add_action('plugins_loaded', 'AmazonAutoLinks_RegisterClasses');
// Admin Page
add_action( 'plugins_loaded', create_function( '', '$oAALAdmin = new AmazonAutoLinks_Admin;' ) );
// Custom Admin CSS
add_action('admin_head', 'AmazonAutoLinks_CustomCSS');
// Widgets
// add_action('widgets_init', 'AmazonAutoLinks_Widgets'); // this is disabled until the blank page issue gets resolved.
// uncomment the following function to clear all options and initialize to the default.
// AmazonAutoLinks_CleanOptions();
function AmazonAutoLinks_CleanOptions($key='') {
delete_option( AMAZONAUTOLINKSKEY );
delete_option('amazonautolinks_catcache_events');
$arr = array();
if ($key != '') {
$arr = get_option(AMAZONAUTOLINKSKEY);
$arr[$key] = array();
}
update_option(AMAZONAUTOLINKSKEY, $arr);
global $wpdb;
$wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%_aal_%')" );
$wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient_timeout%_aal_%')" );
// $wpdb->query( "DELETE FROM `wp_options` WHERE `option_name` LIKE ('_transient%_feed_%')" ); // this is for feed cache
}
// Caches
function AmazonAutoLinks_LoadActions() {
// Since this function has to be called prior to other hooks including the class registration process, retrieve the options manually.
// The event option uses a separate option key since cron jobs runs and updates options asyncronomously,
// It should not affect or get affected by other processes.
$arrCatCacheEvents = get_option('amazonautolinks_catcache_events');
if (!is_array($arrCatCacheEvents)) {
update_option('amazonautolinks_catcache_events', array());
return;
}
// register actions
$i = 0;
foreach($arrCatCacheEvents as $strActionName => $strURL) {
$i++;
add_action($strActionName,'AmazonAutoLinks_CacheCategory'); // the first parameter is the action name to be registered
}
// this is mostly for debugging.
AmazonAutoLinks_Log( $i . ' action(s) is(are) hooked.', __FUNCTION__);
}
function AmazonAutoLinks_Log($strMsg, $strFunc='', $strFileName='log.html') {
return; // if you like to see the plugin workings, comment out this line and the below two lines and you'll find a log file in the plugin directory.
if (!in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) // if the access is not from localhost, do not process.
return;
// for debugging
if ($strFunc=='') $strFunc = __FUNCTION__;
$strPath = __DIR__ . '/' . $strFileName;
if (!file_exists($strPath)) file_put_contents($strPath, ''); // create a file if not exist
$strLog = date('Y m d h:i:s A') . ': ' . $strFunc . ': ' . $strMsg . '
' . PHP_EOL;
$arrLines = file($strPath);
$arrLines = array_reverse($arrLines);
array_push($arrLines, $strLog);
$arrLines = array_reverse($arrLines);
$arrLines = array_splice($arrLines, 0, 100); // extract the first 100 elements
file_put_contents($strPath, implode('', $arrLines));
}
function AmazonAutoLinks_CacheCategory() {
// This function is triggered by the run-off shcedule event.
// It builds caches for only one url per call since this function is assigned by all pre-fetch events.
AmazonAutoLinks_Log(' called.', __FUNCTION__);
// Instantiate class objects
$oAALCatCache = new AmazonAutoLinks_CategoryCache(AMAZONAUTOLINKSKEY);
$arrCatCacheEvents = get_option('amazonautolinks_catcache_events');
// extract the first entry; the oldest jobs from the begginning
$arrEvent = array_splice($arrCatCacheEvents, 0, 10); // take out 5 elements from the beggining of the array
if (count($arrEvent) == 0) {
// echo '';
AmazonAutoLinks_Log('No events are scheduled. Returning.', __FUNCTION__);
return; // if nothing extracted, return
}
// build cache for this url; this array, $arrEvent only holds one element with a key of the action name and the value of url
$i = 0;
foreach($arrEvent as $strURL)
if ($oAALCatCache->cache_category($strURL)) $i++;
echo '';
AmazonAutoLinks_Log($i . ' number of page(s) are cached', __FUNCTION__);
// Do not update the option! Since the process can be stopped by any reason and do not complete caching.
// So let cache_html() update the option when the passed url is cached.
// update_option('amazonautolinks_catcache_events', $arrCatCacheEvents);
// if there are remaining tasks, continue executing in the background
if ( count($arrCatCacheEvents) > 0 )
$oAALCatCache->run_in_background('Background Process: There are '. count($arrCatCacheEvents) .' remaining events. Keep fetching!');
else
AmazonAutoLinks_Log('All done!', __FUNCTION__);
}
// for the plugin admin panel theming
function AmazonAutoLinks_CustomCSS() {
global $wp_version;
if ($_GET['page'] != AMAZONAUTOLINKSKEY)
return;
// if the option page of this plugin is loaded
if (IsSet($_POST[AMAZONAUTOLINKSKEY]['tab202']['proceedbutton']) || IsSet($_POST[AMAZONAUTOLINKSKEY]['tab100']['proceedbutton'])) {
$numTab = isset($_POST[AMAZONAUTOLINKSKEY]['tab202']['proceedbutton']) ? 202 : 100;
$numImageSize = $_POST[AMAZONAUTOLINKSKEY]['tab' . $numTab]['imagesize'];
$numIframeWidth = $numImageSize * 2 + 480; // $strFieldName = $this->pluginkey . '[tab' . $numTabnum . '][imagesize]'
if ( version_compare($wp_version, '3.1.9', "<" ) ) // if the WordPress version is below 3.2
$strIframeWidth = $numIframeWidth < 1180 ? 'width:100%;' : 'width:' . $numIframeWidth . 'px;'; // set the minimum width
else // if the WordPress version is above 3.2
$strIframeWidth = $numIframeWidth < 1180 ? 'width:1180px;' : 'width:' . $numIframeWidth . 'px;'; // set the minimum width
echo '';
} else if ($_GET['tab'] == 400) // for the upgrading to pro tab; the table needs additional styles
echo '';
}
// the function used to embed the Amazon products unit in a theme
function AmazonAutoLinks($unitlabel) {
$options = get_option(AMAZONAUTOLINKSKEY);
if (!IsSet($options['units'][$unitlabel])) {
echo AMAZONAUTOLINKSPLUGINNAME . ' ';
_e('Error: No such unit label exists.', 'amazonautolinks');
return;
}
$oAAL = new AmazonAutoLinks_Core($options['units'][$unitlabel], $options['general']);
echo $oAAL->fetch( $oAAL->UrlsFromUnitLabel($unitlabel, $options));
}
function AmazonAutoLinks_RegisterClasses() {
/*
This function reads class files in wp-include/plugins/[this-plugin-path]/classes
and registers them to be auto-loaded so that require() or include() in each class file is no longer necessary.
After that, it defines new clesses based on the regstered class names.
The class files must have a class definition with the file name without file extension.
This function should be trigered with the plugins_loaded() function; otherwise, the "header already sent" error may occur during
the plugin activation.
*/
global $strAALDirPath, $arrAALPHPfiles; // needs to be global since the following create_function() needs the values.
// Register classes
$strAALDirPath = dirname(__FILE__) . '/classes/';
$arrAALPHPfiles = array_map(create_function( '$a', 'return basename($a, ".php");' ), glob($strAALDirPath . '*.php'));
spl_autoload_register(
create_function('$class_name', '
global $arrAALPHPfiles, $strAALDirPath;
if (in_array($class_name, $arrAALPHPfiles))
include($strAALDirPath . $class_name . ".php");' )
);
// Define classes
$strClassNamePrefix = 'AmazonAutoLinks_'; // define a prefix of file name to avoid executing harmful code in file names.
foreach ($arrAALPHPfiles as $strFileName) {
// apply security filters
if (substr($strFileName, 0, strlen($strClassNamePrefix)) != $strClassNamePrefix)
continue; // filter out files whhch don't start with the prefix
if (preg_match("/[#;\(\){}]/", $strFileName))
continue; // if the file name contains characters looking like PHP code, skip it
// $strFileName: either ending with _ or Pro e.g. AmazonAutoLinks_Admin_ / AmazonAutoLinks_Admin_Pro
if (substr($strFileName, -4) == '_Pro') { // case, Pro
$strClassNamePro = $strFileName; // leave it as it is, e.g. AmazonAutoLinks_Admin_Pro -> AmazonAutoLinks_Admin_Pro
$strClassName = substr($strFileName, 0, -4); // removes the last four caracters. e.g. AmazonAutoLinks_Admin_Pro -> AmazonAutoLinks_Admin
} else { // case, Standard
$strClassNamePro = $strFileName . 'Pro'; // adds Pro, e.g. amazonautolinks_admin_ -> amazonautolinks_admin_pro
$strClassName = substr($strFileName, 0, -1); // removes the last one character. e.g. amazonautolinks_admin_ -> amazonautolinks_admin
}
// delare classes
if (class_exists($strClassNamePro) && !class_exists($strClassName))
eval("class $strClassName extends $strClassNamePro {};");
else if (class_exists($strFileName) && !class_exists($strClassName))
eval("class $strClassName extends $strFileName {};");
}
}
// requirements for this plugin to work in PHP version >= 5.1.2
function AmazonAutoLinks_Requirements() {
global $wp_version, $wpdb;
$plugin = plugin_basename( __FILE__ );
$plugin_data = get_plugin_data( __FILE__, false );
$numPHPver='5.1.2'; // required php version
$numWPver='3.0'; // required WordPress version
$bSufficient = True;
$strMsg = '';
if ( version_compare(phpversion(), $numPHPver, "<" ) ) {
$bSufficient = False;
$strMsg .= $plugin_data['Name'] . ': ' . __('The plugin requires the following PHP version or higher:', 'amazonautolinks')
. ' ' . $numPHPver . ' ' . __('Your PHP version is:', 'amazonautolinks') . phpversion()
. ' ' . __('Deactivating the plugin.', 'amazonautolinks') . '
';
}
if ( version_compare($wp_version, $numWPver, "<" ) ) {
$bSufficient = False;
$strMsg .= $plugin_data['Name'] . ': ' . __('The plugin requires the following WordPress version or higher:', 'amazonautolinks')
. ' ' . $numWPver . ' ' . __('Your WordPress version is:', 'amazonautolinks') . $wp_version . ' '
. __('Deactivating the plugin.', 'amazonautolinks') ;
}
if (!$bSufficient && is_plugin_active($plugin)) {
echo '
' . $strMsg . '