wp-init.php 67.5 KB
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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
<?php
/**
 * wp-init.php
 * 
 * Copyright (c) 2010, 2011 "kento" Karim Rahimpur www.itthinx.com
 * 
 * This code is released under the GNU General Public License.
 * See COPYRIGHT.txt and LICENSE.txt.
 * 
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * This header and all notices must be kept intact.
 * 
 * @author Karim Rahimpur
 * @package affiliates
 * @since affiliates 1.1.2
 */

if ( !defined( 'ABSPATH' ) ) {
	exit;
}

global $affiliates_options, $affiliates_version, $affiliates_admin_messages;

if ( !isset( $affiliates_admin_messages ) ) {
	$affiliates_admin_messages = array();
}

if ( !isset( $affiliates_version ) ) {
	$affiliates_version = AFFILIATES_CORE_VERSION;
}

// base class
require_once AFFILIATES_CORE_LIB . '/class-affiliates.php';

// options
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-options.php' );
if ( $affiliates_options == null ) {
	$affiliates_options = new Affiliates_Options();
}

// utilities
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-utility.php' );
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-ui-elements.php' );

// ajax
include_once AFFILIATES_CORE_LIB . '/class-affiliates-ajax.php';

// forms, shortcodes, widgets
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-contact.php' );
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-registration.php' );
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-registration-widget.php' );
include_once( AFFILIATES_CORE_LIB . '/class-affiliates-shortcodes.php' ); // don't make it conditional on is_admin(), get_total() is used in Manage Affiliates

// built-in user registration integration
if ( get_option( 'aff_user_registration_enabled', 'no' ) == 'yes' ) {
	require_once AFFILIATES_CORE_LIB . '/class-affiliates-user-registration.php';
}

// affiliates excluded
include_once AFFILIATES_CORE_LIB . '/class-affiliates-exclusion.php';

add_action( 'widgets_init', 'affiliates_widgets_init' );

/**
 * Register widgets
 */
function affiliates_widgets_init() {
	register_widget( 'Affiliates_Contact' );
	register_widget( 'Affiliates_Registration_Widget' );
}

add_action( 'admin_init', 'affiliates_admin_init' );

/**
 * Hook into admin_init. Used to get our styles at the right place.
 * @see affiliates_admin_menu()
 * @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages
 */
function affiliates_admin_init() {
	global $affiliates_version;
	wp_register_style( 'smoothness', AFFILIATES_PLUGIN_URL . 'css/smoothness/jquery-ui-1.8.16.custom.css', array(), $affiliates_version );
	wp_register_style( 'affiliates_admin', AFFILIATES_PLUGIN_URL . 'css/affiliates_admin.css', array(), $affiliates_version );
}

/**
 * Load styles.
 * @see affiliates_admin_menu()
 */
function affiliates_admin_print_styles() {
	wp_enqueue_style( 'smoothness' );
	wp_enqueue_style( 'affiliates_admin' );
}

/**
 * Load scripts.
 */
function affiliates_admin_print_scripts() {
	global $post_type, $affiliates_version;

	// load datepicker scripts for all
	wp_enqueue_script( 'datepicker', AFFILIATES_PLUGIN_URL . 'js/jquery.ui.datepicker.min.js', array( 'jquery', 'jquery-ui-core' ), $affiliates_version );
	wp_enqueue_script( 'datepickers', AFFILIATES_PLUGIN_URL . 'js/datepickers.js', array( 'jquery', 'jquery-ui-core', 'datepicker' ), $affiliates_version );
	// corners
	wp_enqueue_script( 'jquery-corner', AFFILIATES_PLUGIN_URL . 'js/jquery.corner.js', array( 'jquery', 'jquery-ui-core' ), $affiliates_version );
	// add more dates used for trips and events
	wp_enqueue_script( 'affiliates', AFFILIATES_PLUGIN_URL . 'js/affiliates.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-button', 'jquery-corner' ), $affiliates_version );
	// and thus are the translations of the buttons used

	wp_enqueue_script( 'excanvas', AFFILIATES_PLUGIN_URL . 'js/graph/flot/excanvas.min.js', array( 'jquery' ), $affiliates_version );
	wp_enqueue_script( 'flot', AFFILIATES_PLUGIN_URL . 'js/graph/flot/jquery.flot.min.js', array( 'jquery' ), $affiliates_version );
	wp_enqueue_script( 'flot-resize', AFFILIATES_PLUGIN_URL . 'js/graph/flot/jquery.flot.resize.min.js', array( 'jquery', 'flot' ), $affiliates_version );

//	echo '
//		<script type="text/javascript">
//			var fooText = "' . __( 'Foo', AFFILIATES_PLUGIN_DOMAIN ) . '";
//		</script>
//		';
}

add_action( 'wp_enqueue_scripts', 'affiliates_wp_enqueue_scripts' );
function affiliates_wp_enqueue_scripts() {
	global $affiliates_version;
	wp_register_style( 'affiliates', AFFILIATES_PLUGIN_URL . 'css/affiliates.css', array(), $affiliates_version );
}
// ---

register_activation_hook( AFFILIATES_FILE, 'affiliates_activate' );
add_action( 'wpmu_new_blog', 'affiliates_wpmu_new_blog', 10, 2 );
add_action( 'delete_blog', 'affiliates_delete_blog', 10, 2 );

add_action( 'init', 'affiliates_version_check' );
function affiliates_version_check() {
	global $affiliates_version, $affiliates_admin_messages;
	$previous_version = get_option( 'affiliates_plugin_version', null );
	$affiliates_version = AFFILIATES_CORE_VERSION;
	if ( version_compare( $previous_version, $affiliates_version ) < 0 ) {
		if ( affiliates_update( $previous_version ) ) {
			update_option( 'affiliates_plugin_version', $affiliates_version );
		} else {
			$affiliates_admin_messages[] = '<div class="error">Updating the Affiliates core FAILED.</div>';
		}
	}
}

function affiliates_admin_notices() {
	global $affiliates_admin_messages;
	if ( !empty( $affiliates_admin_messages ) ) {
		foreach ( $affiliates_admin_messages as $msg ) {
			echo $msg;
		}
	}
}

add_action( 'admin_notices', 'affiliates_admin_notices' );

/**
 * Tasks performed upon plugin activation.
 */
function affiliates_activate( $network_wide = false ) {
	if ( is_multisite() && $network_wide ) {
		$blog_ids = affiliates_get_blogs();
		foreach ( $blog_ids as $blog_id ) {
			switch_to_blog( $blog_id );
			wp_cache_reset();
			affiliates_setup();
			restore_current_blog();
		}
	} else {
		affiliates_setup();
	}
}

/**
 * Setup for a new creat blog.
 * @param int $blog_id
 */
function affiliates_wpmu_new_blog( $blog_id, $user_id ) {
	if ( is_multisite() ) {
		if ( affiliates_is_sitewide_plugin() ) {
			switch_to_blog( $blog_id );
			wp_cache_reset();
			affiliates_setup();
			restore_current_blog();
		}
	}
}

/**
 * Clean up for a given blog.
 * 
 * @param int $blog_id
 * @param boolean $drop
 */
function affiliates_delete_blog( $blog_id, $drop = false ) {
	if ( is_multisite() ) {
		if ( affiliates_is_sitewide_plugin() ) {
			switch_to_blog( $blog_id );
			wp_cache_reset();
			affiliates_cleanup( $drop );
			restore_current_blog();
		}
	}
}

/**
 * Returns true if the plugin is site-wide.
 * @return boolean true if site-wide plugin
 */
function affiliates_is_sitewide_plugin() {
	$result = false;
	if ( is_multisite() ) {
		$active_sitewide_plugins = get_site_option( 'active_sitewide_plugins', array() );
		$active_sitewide_plugins = array_keys( $active_sitewide_plugins );
		$components = explode( DIRECTORY_SEPARATOR, AFFILIATES_FILE );
		$plugin = '';
		$n = count( $components );
		if ( isset( $components[$n - 2] ) ) {
			$plugin .= $components[$n - 2] . DIRECTORY_SEPARATOR;
		}
		$plugin .= $components[$n - 1];
		$result = in_array( $plugin, $active_sitewide_plugins );
	}
	return $result;
}

/**
 * Retrieve current blogs' ids.
 * @return array blog ids 
 */
function affiliates_get_blogs() {
	global $wpdb;
	$result = array();
	if ( is_multisite() ) {
		$blogs = $wpdb->get_results( $wpdb->prepare(
			"SELECT blog_id FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND spam = '0' AND deleted = '0'",
			$wpdb->siteid
		) );
		if ( is_array( $blogs ) ) {
			foreach( $blogs as $blog ) {
				$result[] = $blog->blog_id;
			}
		}
	} else {
		$result[] = get_current_blog_id();
	}
	return $result;
}

/**
 * Create tables and prepare data.
 */
function affiliates_setup() {
	global $wpdb, $wp_roles;

	_affiliates_set_default_capabilities();

	$charset_collate = '';
	if ( ! empty( $wpdb->charset ) ) {
		$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
	}
	if ( ! empty( $wpdb->collate ) ) {
		$charset_collate .= " COLLATE $wpdb->collate";
	}

	$affiliates_table = _affiliates_get_tablename('affiliates');
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $affiliates_table . "'" ) != $affiliates_table ) {
		$queries[] = "CREATE TABLE " . $affiliates_table . "(
				affiliate_id bigint(20) unsigned NOT NULL auto_increment,
				name         varchar(100) NOT NULL,
				email        varchar(512) default NULL,
				from_date    date NOT NULL,
				thru_date    date default NULL,
				status       varchar(10) NOT NULL DEFAULT 'active',
				type         varchar(10) NULL,
				PRIMARY KEY  (affiliate_id),
				INDEX        affiliates_afts (affiliate_id, from_date, thru_date, status),
				INDEX        affiliates_sft (status, from_date, thru_date)
			) $charset_collate;";
	}

	// email @see http://tools.ietf.org/html/rfc5321
	// 2.3.11. Mailbox and Address
	// ... The standard mailbox naming convention is defined to
	// be "local-part@domain"; ...
	// 4.1.2. Command Argument Syntax
	// ...
	// Mailbox        = Local-part "@" ( Domain / address-literal )
	// ...
	// 4.5.3. Sizes and Timeouts
	// 4.5.3.1.1. Local-part
	// The maximum total length of a user name or other local-part is 64 octets.
	// 4.5.3.1.2. Domain
	// The maximum total length of a domain name or number is 255 octets.
	// 4.5.3.1.3. Path
	// The maximum total length of a reverse-path or forward-path is 256
	// octets (including the punctuation and element separators).
	// So the maximum size of an email address is ... ?
	// 64 + 1 + 255 = 320 octets
	// Then again, people change their minds ... we'll assume 512 as sufficient.
	// Note: WP's user.user_email is varchar(100) @see wp-admin/includes/schema.php
		
	// IPv6 addr
	// FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF = 340282366920938463463374607431768211455
	// Note that ipv6 is not part of the PK but can be handled using
	// the lower bits of the ipv6 address to fill in ip and using the
	// complete IPv6 address on ipv6.
	// Note also, that currently Affiliates does NOT use ipv6.

	$referrals_table = _affiliates_get_tablename( 'referrals' );
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $referrals_table . "'" ) != $referrals_table ) {
		$queries[] = "CREATE TABLE " . $referrals_table . "(
				referral_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				affiliate_id bigint(20) unsigned NOT NULL default '0',
				campaign_id  bigint(20) UNSIGNED DEFAULT NULL,
				post_id      bigint(20) unsigned NOT NULL default '0',
				datetime     datetime NOT NULL,
				description  varchar(5000),
				ip           int(10) unsigned default NULL,
				ipv6         decimal(39,0) unsigned default NULL,
				user_id      bigint(20) unsigned default NULL,
				amount       decimal(18,2) default NULL,
				currency_id  char(3) default NULL,
				data         longtext default NULL,
				status       varchar(10) NOT NULL DEFAULT '" . AFFILIATES_REFERRAL_STATUS_ACCEPTED . "',
				type         varchar(10) NULL,
				reference    VARCHAR(100) DEFAULT NULL,
				PRIMARY KEY  (referral_id),
				INDEX        aff_referrals_apd (affiliate_id, post_id, datetime),
				INDEX        aff_referrals_da (datetime, affiliate_id),
				INDEX        aff_referrals_sda (status, datetime, affiliate_id),
				INDEX        aff_referrals_tda (type, datetime, affiliate_id),
				INDEX        aff_referrals_ref (reference(20)),
				INDEX        aff_referrals_ac  (affiliate_id, campaign_id),
				INDEX        aff_referrals_c   (campaign_id)
			) $charset_collate;";
		// @see http://bugs.mysql.com/bug.php?id=27645 as of now (2011-03-19) NOW() can not be specified as the default value for a datetime column
	}
	// A note about whether or not to record information about
	// http_user_agent here: No, we don't do that!
	// Our business is to record hits on affiliate links, not
	// to gather statistical data about user agents. For our
	// purpose, it does not add value to record that and if
	// one wishes to do so, there are better solutions anyhow.
	// IMPORTANT:
	// datetime -- records the datetime with respect to the server's timezone
	// date and time are are also with respect to the server's timezone
	// date -- we do NOT use this to display information to the user
	// time -- same applies to this here
	// date and time (and currently only date really) are used for better
	// performance on certain queries.
	// We DO use the datetime (adjusted to the user's timezone using
	// DateHelper's s2u() function to display the date and time
	// in accordance to the user's date and time.
	$hits_table = _affiliates_get_tablename( 'hits' );
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $hits_table . "'" ) != $hits_table ) {
		$queries[] = "CREATE TABLE " . $hits_table . "(
				affiliate_id    BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
				campaign_id     BIGINT(20) UNSIGNED DEFAULT NULL,
				date            DATE NOT NULL,
				time            TIME NOT NULL,
				datetime        DATETIME NOT NULL,
				ip              INT(10) UNSIGNED DEFAULT NULL,
				ipv6            DECIMAL(39,0) UNSIGNED DEFAULT NULL,
				is_robot        TINYINT(1) DEFAULT 0,
				user_id         BIGINT(20) UNSIGNED DEFAULT NULL,
				count           INT DEFAULT 1,
				type            VARCHAR(10) DEFAULT NULL,
				PRIMARY KEY     (affiliate_id, date, time, ip),
				INDEX           aff_hits_ddt (date, datetime),
				INDEX           aff_hits_dtd (datetime, date),
				INDEX           aff_hits_acm (affiliate_id, campaign_id)
			) $charset_collate;";
	}
	$robots_table = _affiliates_get_tablename( 'robots' );
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $robots_table . "'" ) != $robots_table ) {
		$queries[] = "CREATE TABLE " . $robots_table . "(
				robot_id    bigint(20) unsigned NOT NULL auto_increment,
				name        varchar(100) NOT NULL,
				PRIMARY KEY (robot_id),
				INDEX       aff_robots_n (name)
			) $charset_collate;";
	}
	$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $affiliates_users_table . "'" ) != $affiliates_users_table ) {
		$queries[] = "CREATE TABLE " . $affiliates_users_table . "(
				affiliate_id bigint(20) unsigned NOT NULL,
				user_id      bigint(20) unsigned NOT NULL,
				PRIMARY KEY (affiliate_id, user_id)
			) $charset_collate;";
	}
	if ( !empty( $queries ) ) {
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $queries );
	}
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $affiliates_table . "'" ) == $affiliates_table ) {
		$today = date( 'Y-m-d', time() );
		$direct = intval( $wpdb->get_var( "SELECT COUNT(affiliate_id) FROM $affiliates_table WHERE type = '" . AFFILIATES_DIRECT_TYPE . "';" ) );
		if ( $direct <= 0 ) {
			$wpdb->query( "INSERT INTO $affiliates_table (name, from_date, type) VALUES ('" . AFFILIATES_DIRECT_NAME . "','$today','" . AFFILIATES_DIRECT_TYPE . "');" );
		}
	}

	affiliates_update();
	affiliates_update_rewrite_rules();
}

/**
 * Determines the default capabilities for the administrator role.
 * In lack of an administrator role, these capabilities are assigned
 * to any role that can manage_options.
 * This is also used to assure a minimum set of capabilities is
 * assigned to an appropriate role, so that it's not possible
 * to lock yourself out (although deactivating and then activating
 * the plugin would have the same effect but with the danger of
 * deleting all plugin data).
 * @param boolean $activate defaults to true, when this function is called upon plugin activation
 * @access private
 */
function _affiliates_set_default_capabilities() {
	global $wp_roles;
	// The administrator role should be there, if it's not, assign privileges to
	// any role that can manage_options:
	if ( $administrator_role = $wp_roles->get_role( 'administrator' ) ) {
		$administrator_role->add_cap( AFFILIATES_ACCESS_AFFILIATES );
		$administrator_role->add_cap( AFFILIATES_ADMINISTER_AFFILIATES );
		$administrator_role->add_cap( AFFILIATES_ADMINISTER_OPTIONS );
	} else {
		foreach ( $wp_roles->role_objects as $role ) {
			if ($role->has_cap( 'manage_options' ) ) {
				$role->add_cap( AFFILIATES_ACCESS_AFFILIATES );
				$role->add_cap( AFFILIATES_ADMINISTER_AFFILIATES );
				$role->add_cap( AFFILIATES_ADMINISTER_OPTIONS );
			}
		}
	}
}

/**
 * There must be at least one role with the minimum set of capabilities
 * to access and manage the Affiliates plugin's options.
 * If this condition is not met, the minimum set of capabilities is
 * reestablished.
 */
function _affiliates_assure_capabilities() {
	global $wp_roles;
	$complies = false;
	$roles = $wp_roles->role_objects;
	foreach( $roles as $role ) {
		if ( $role->has_cap( AFFILIATES_ACCESS_AFFILIATES ) && ( $role->has_cap( AFFILIATES_ADMINISTER_OPTIONS ) ) ) {
			$complies = true;
			break;
		}
	}
	if ( !$complies ) {
		_affiliates_set_default_capabilities();
	}
}

/**
 * Update from a previous version or repair on activation (within 2.x).
 * 
 * This is called from affiliates_setup() on plugin activation and affiliates_version_check()
 * when a previous version is detected.
 * 
 * @param string $previous_version
 * @return boolean
 */
function affiliates_update( $previous_version = null ) {

	global $wpdb;

	$result  = true;
	$queries = array();

	$hits_table = _affiliates_get_tablename( 'hits' );
	$column     = $wpdb->get_row( "SHOW COLUMNS FROM $hits_table LIKE 'campaign_id'" );
	if ( empty( $column ) ) {
		$queries[] = "ALTER TABLE " . $hits_table . "
		ADD COLUMN campaign_id BIGINT(20) UNSIGNED DEFAULT NULL,
		ADD INDEX aff_hits_acm (affiliate_id, campaign_id);";
	}

	$referrals_table = _affiliates_get_tablename( 'referrals' );
	$column          = $wpdb->get_row( "SHOW COLUMNS FROM $referrals_table LIKE 'campaign_id'" );
	if ( empty( $column ) ) {
		$queries[] = "ALTER TABLE " . $referrals_table . "
		ADD COLUMN campaign_id BIGINT(20) UNSIGNED DEFAULT NULL,
		ADD INDEX aff_referrals_ac (affiliate_id, campaign_id),
		ADD INDEX aff_referrals_c (campaign_id);";
	}

	foreach ( $queries as $query ) {
		// don't use dbDelta, it doesn't handle ALTER
		if ( $wpdb->query( $query ) === false ) {
			// fail but still try to go on
			$result = false;
		}
	}
	if ( !empty( $previous_version ) && version_compare( $previous_version, '2.1.5' ) < 0 ) {
		affiliates_update_rewrite_rules();
	}
	return $result;
}

register_deactivation_hook( AFFILIATES_FILE, 'affiliates_deactivate' );

/**
 * Drop tables and clear data if the plugin is deactivated.
 * This will happen only if the user chooses to delete data upon deactivation.
 */
function affiliates_deactivate( $network_wide = false ) {
	if ( is_multisite() && $network_wide ) {
		if ( get_option( 'aff_delete_network_data', false ) ) {
			$blog_ids = affiliates_get_blogs();
			foreach ( $blog_ids as $blog_id ) {
				switch_to_blog( $blog_id );
				wp_cache_reset();
				affiliates_cleanup( true );
				restore_current_blog();
			}
		}
	} else {
		affiliates_cleanup();
	}
}

/**
 * Cleans up tables, data, capabilities if the option is set.
 * 
 * @param boolean $delete force deletion
 */
function affiliates_cleanup( $delete = false ) {
	global $wpdb, $affiliates_options, $wp_roles;
	
	$delete_data = get_option( 'aff_delete_data', false ) || $delete;
	if ( $delete_data ) {
		foreach ( $wp_roles->role_objects as $role ) {
			$role->remove_cap( AFFILIATES_ACCESS_AFFILIATES );
			$role->remove_cap( AFFILIATES_ADMINISTER_AFFILIATES );
			$role->remove_cap( AFFILIATES_ADMINISTER_OPTIONS );
		}
		$wpdb->query('DROP TABLE IF EXISTS ' . _affiliates_get_tablename( 'referrals' ) );
		$wpdb->query('DROP TABLE IF EXISTS ' . _affiliates_get_tablename( 'hits' ) );
		$wpdb->query('DROP TABLE IF EXISTS ' . _affiliates_get_tablename( 'affiliates' ) );
		$wpdb->query('DROP TABLE IF EXISTS ' . _affiliates_get_tablename( 'robots' ) );
		$wpdb->query('DROP TABLE IF EXISTS ' . _affiliates_get_tablename( 'affiliates_users' ) );
		flush_rewrite_rules();
		$affiliates_options->flush_options();
		delete_option( 'affiliates_plugin_version' );
		delete_option( 'aff_allow_auto' );
		delete_option( 'aff_allow_auto_coupons' );
		delete_option( 'aff_cookie_timeout_days' );
		delete_option( 'aff_customer_registration_enabled' );
		delete_option( 'aff_default_referral_status' );
		delete_option( 'aff_delete_data' );
		delete_option( 'aff_delete_network_data' );
		delete_option( 'aff_duplicates' );
		delete_option( 'aff_id_encoding' );
		delete_option( 'aff_notify_admin' );
		delete_option( 'aff_pname' );
		delete_option( 'aff_redirect' );
		delete_option( 'aff_registration' );
		delete_option( 'aff_registration_fields' );
		delete_option( 'aff_setup_hide' );
		delete_option( 'aff_use_direct' );
		delete_option( 'aff_user_registration_amount' );
		delete_option( 'aff_user_registration_base_amount' );
		delete_option( 'aff_user_registration_currency' );
		delete_option( 'aff_user_registration_enabled' );
		delete_option( 'aff_user_registration_referral_status' );
	}
}

add_action( 'init', 'affiliates_init' );
	
/**
 * Initialize.
 * Loads the plugin's translations.
 */
function affiliates_init() {
	load_plugin_textdomain( AFFILIATES_PLUGIN_DOMAIN, null, AFFILIATES_PLUGIN_NAME . '/lib/core/languages' );
	if ( class_exists( 'Affiliates_Affiliate' ) && method_exists( 'Affiliates_Affiliate', 'register_attribute_filter' ) ) {
		Affiliates_Affiliate::register_attribute_filter( 'affiliates_attribute_filter' );
	}
}

add_filter( 'query_vars', 'affiliates_query_vars', 999 ); // filter acts late to avoid being messed with by others

/**
 * Register the affiliate query variable.
 * In addition to existing query variables, we'll use affiliates in the URL to track referrals.
 * @see affiliates_update_rewrite_rules() for the rewrite rule that matches the affiliates id
 */
function affiliates_query_vars( $query_vars ) {
	$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
	$query_vars[] = $pname;
	return $query_vars;
}

/**
 * Add a rewrite rule for pretty links.
 * @deprecated
 */
function affiliates_update_rewrite_rules() {
	$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
	add_rewrite_rule(
		str_replace( AFFILIATES_PNAME, $pname, AFFILIATES_REGEX_PATTERN ),
		'index.php?' . $pname . '=$matches[1]',
		'top'
	);
	flush_rewrite_rules();
}

add_action( 'parse_request', 'affiliates_parse_request' );
	
/**
 * Looks in the query variables and sets a cookie with the affiliate id.
 * Hook into parse_request.
 * @param WP $wp the WordPress environment
 * @link http://php.net/manual/en/function.setcookie.php
 */
function affiliates_parse_request( &$wp ) {

	global $wpdb, $affiliates_options, $affiliates_request_encoded_id;

	$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
	$affiliate_id = isset( $wp->query_vars[$pname] ) ? affiliates_check_affiliate_id_encoded( trim( $wp->query_vars[$pname] ) ) : null;
	if ( isset( $wp->query_vars[$pname] ) ) {
		// affiliates-by-username uses this hook
		$maybe_affiliate_id = apply_filters( 'affiliates_parse_request_affiliate_id', $wp->query_vars[$pname], $affiliate_id );
		if ( ( $maybe_affiliate_id !== null ) && $maybe_affiliate_id !== trim( $wp->query_vars[$pname] ) ) {
			$affiliate_id = $maybe_affiliate_id;
		}
	}

	if ( $affiliate_id ) {
		$encoded_id = affiliates_encode_affiliate_id( $affiliate_id );
		$days = apply_filters( 'affiliates_cookie_timeout_days', get_option( 'aff_cookie_timeout_days', AFFILIATES_COOKIE_TIMEOUT_DAYS ), $affiliate_id );
		if ( $days > 0 ) {
			$expire = time() + AFFILIATES_COOKIE_TIMEOUT_BASE * $days;
		} else {
			$expire = 0;
		}
		if ( class_exists( 'Affiliates_Campaign' ) && method_exists( 'Affiliates_Campaign', 'evaluate' ) ) {
			if ( !empty( $_REQUEST['cmid'] ) ) {
				if ( $cmid = Affiliates_Campaign::evaluate( $_REQUEST['cmid'], $affiliate_id ) ) {
					$encoded_id .= '.' . $cmid;
				}
			}
		}
		$affiliates_request_encoded_id = $encoded_id;
		setcookie(
			AFFILIATES_COOKIE_NAME,
			$encoded_id,
			$expire,
			SITECOOKIEPATH,
			COOKIE_DOMAIN
		);
		affiliates_record_hit( $affiliate_id );
		affiliates_pixel_request();
		unset( $wp->query_vars[$pname] ); // we use this to avoid ending up on the blog listing page
		if ( get_option( 'aff_redirect', false ) !== false ) {
			// use a redirect so that we end up on the desired url without the affiliate id dangling on the url
			$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
			$current_url = remove_query_arg( $pname, $current_url );
			$current_url = preg_replace( '#' . str_replace( AFFILIATES_PNAME, $pname, AFFILIATES_REGEX_PATTERN ) . '#', '', $current_url);
			// note that we must use delimiters other than / as these are used in AFFILIATES_REGEX_PATTERN
			$status = apply_filters( 'affiliates_redirect_status_code', 302 );
			$status = intval( $status );
			switch( $status ) {
				case 300 :
				case 301 :
				case 302 :
				case 303 :
				case 304 :
				case 305 :
				case 306 :
				case 307 :
					break;
				default :
					$status = 302;
			}
			wp_redirect( $current_url, $status );
			exit; // "wp_redirect() does not exit automatically and should almost always be followed by exit." @see http://codex.wordpress.org/Function_Reference/wp_redirect
		}
	} else {
		affiliates_pixel_request();
	}
}

/**
 * Requests pixel handling.
 */
function affiliates_pixel_request() {
	if (
		class_exists( 'Affiliates_Pixel' ) &&
		method_exists( 'Affiliates_Pixel', 'pixel' ) &&
		method_exists( 'Affiliates_Pixel', 'is_pixel_request' )
	) {
		$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
		$p = new Affiliates_Pixel( trailingslashit( home_url() ), $pname );
		$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		if ( $p->is_pixel_request( $current_url ) ) {
			$p->pixel();
		}
	}
}

/**
 * Record a hit on an affiliate link.
 *
 * @param int $affiliate_id the affiliate's id
 * @param int $now UNIX timestamp to use, if null the current time is used
 * @param string $type the type of hit to record
 */
function affiliates_record_hit( $affiliate_id, $now = null, $type = null ) {
	global $wpdb;
	// add a hit
	// @todo check/store IPv6 addresses
	//$http_user_agent = $_SERVER['HTTP_USER_AGENT'];

	$table    = _affiliates_get_tablename( 'hits' );
	if ( $now == null ) {
		$now = time();
	}
	$date     = date( 'Y-m-d' , $now );
	$time     = date( 'H:i:s' , $now );
	$datetime = date( 'Y-m-d H:i:s' , $now );

	$columns    = '(affiliate_id, date, time, datetime, type';
	$formats    = '(%d,%s,%s,%s,%s';
	$values     = array( $affiliate_id, $date, $time, $datetime, $type );

	$ip_address = $_SERVER['REMOTE_ADDR'];
	if ( PHP_INT_SIZE >= 8 ) {
		if ( $ip_int = ip2long( $ip_address ) ) {
			$columns .= ',ip';
			$formats .= ',%d';
			$values[] = $ip_int;
		}
	} else {
		if ( $ip_int = ip2long( $ip_address ) ) {
			$ip_int = sprintf( '%u', $ip_int );
			$columns .= ',ip';
			$formats .= ',%s';
			$values[] = $ip_int;
		}
	}
	if ( $user_id = get_current_user_id() ) {
		$columns .= ',user_id';
		$formats .= ',%d';
		$values[] = $user_id;
	}
	$is_robot = 0;
	if ( AFFILIATES_RECORD_ROBOT_HITS ) { // @todo review/fix logic
		$robots_table    = _affiliates_get_tablename( 'robots' );
		$robots_query    = $wpdb->prepare( "SELECT name FROM $robots_table WHERE %s LIKE concat('%%',name,'%%')", $http_user_agent );
		$got_robots      = $wpdb->get_results( $robots_query );
		if ( !empty( $got_robots ) ) {
			$columns .= ',is_robot';
			$formats .= ',%d';
			$values[] = '1';
		}
	}
	$campaign_id = null;
	if ( class_exists( 'Affiliates_Campaign' ) && method_exists(  'Affiliates_Campaign', 'evaluate' ) ) {
		if ( isset( $_REQUEST['cmid'] ) ) {
			$campaign_id = Affiliates_Campaign::evaluate( $_REQUEST['cmid'], $affiliate_id, $_REQUEST );
			if ( $campaign_id ) {
				$columns .= ',campaign_id';
				$formats .= ',%d';
				$values[] = intval( $campaign_id );
			}
		}
	}
	$columns .= ')';
	$formats .= ')';
	$query = $wpdb->prepare( "INSERT INTO $table $columns VALUES $formats ON DUPLICATE KEY UPDATE count = count + 1", $values );
	if ( $wpdb->query( $query ) ) {
		do_action(
			'affiliates_hit',
			array(
				'affiliate_id' => $affiliate_id,
				'campaign_id'  => $campaign_id,
				'date'         => $date,
				'time'         => $time,
				'datetime'     => $datetime,
				'ip'           => $ip_address,
				'ipv6'         => null,
				'is_robot'     => $is_robot,
				'user_id'      => $user_id,
				'type'         => $type
			)
		);
	}
}

/**
 * Suggest to record a referral. This function is used to actually store referrals and associated information.
 * If a valid affiliate is found, the referral is stored and attributed to the affiliate and the affiliate's id is returned.
 * Use the $description to describe the nature of the referral and the associated transaction.
 * Use $data to store transaction data or additional information as needed.
 *
 * $data must either be a string or an array. If given as an array, it is assumed that one provides
 * field data (for example: customer id, transaction id, customer name, ...) and for each field
 * one entry is added to the $data array obeying this format:
 *
 * $data['example'] = array(
 *   'title' => 'Field title',
 *   'domain' => 'Domain'
 *   'value' => 'Field value'
 * );
 *
 * This information is then used to display field data for each referral in the Referrals section.
 *
 * 'title' is the field's title, the title is translated using the 'domain'.
 * 'value' is displayed as the field's value.
 *
 * Example:
 *
 * A customer has submitted an order and a possible referral shall be recorded, including the customer's id
 * and the order id:
 *
 * $data = array(
 *   'customer-id' => array( 'title' => 'Customer Id', 'domain' => 'my_order_plugin', 'value' => $customer_id ),
 *   'order-id'    => array( 'title' => 'Order Id', 'domain' => 'my_order_plugin', 'value' => $order_id )
 * );
 * if ( $affiliate_id = affiliates_suggest_referral( $post_id, "Referral for order number $order_id", $data ) ) {
 *   $affiliate = affiliates_get_affiliate( $affiliate_id );
 *   $affiliate_email = $affiliate['email'];
 *   if ( !empty( $affiliate_email ) ) {
 *   	$message = "Dear Affiliate, an order has been made. You will be credited as soon as payment is received.";
 *   	my_order_plugin_send_an_email( $affiliate_email, $message );
 *   }
 * }
 *
 * @param int $post_id the referral post id; where the transaction or referral originates
 * @param string $description the referral description
 * @param string|array $data additional information that should be stored along with the referral
 * @param string $amount referral amount - if used, a $currency_id must be given
 * @þaram string $currency_id three letter currency code - if used, an $amount must be given
 * @return affiliate id if a valid referral is recorded, otherwise false
 */
function affiliates_suggest_referral( $post_id, $description = '', $data = null, $amount = null, $currency_id = null, $status = null, $type = null, $reference = null ) {
	global $wpdb, $affiliates_options;
	require_once( 'class-affiliates-service.php' );
	$affiliate_id = Affiliates_Service::get_referrer_id();
	if ( $affiliate_id ) {
		$affiliate_id = affiliates_add_referral($affiliate_id, $post_id, $description, $data, $amount, $currency_id, $status, $type, $reference );
	}
	return $affiliate_id;
}

/**
 * Store a referral.
 * @param int $affiliate_id
 * @param int  $post_id
 * @param string $description
 * @param array $data
 * @param string $amount
 * @param string $currency_id
 * @param string $status
 * @param string $type
 * @param string $reference
 * @return int
 */
function affiliates_add_referral( $affiliate_id, $post_id, $description = '', $data = null, $amount = null, $currency_id = null, $status = null, $type = null, $reference = null ) {
	global $wpdb;

	if ( $affiliate_id ) {

		$current_user = wp_get_current_user();
		$now = date('Y-m-d H:i:s', time() );
		$table = _affiliates_get_tablename( 'referrals' );

		$columns = "(affiliate_id, post_id, datetime, description";
		$formats = "(%d, %d, %s, %s";
		$values = array( $affiliate_id, $post_id, $now, $description );

		if ( !empty( $current_user ) ) {
			$columns .= ",user_id ";
			$formats .= ",%d ";
			$values[] = $current_user->ID;
		}

		// add ip
		$ip_address = $_SERVER['REMOTE_ADDR'];
		if ( PHP_INT_SIZE >= 8 ) {
			if ( $ip_int = ip2long( $ip_address ) ) {
				$columns .= ',ip ';
				$formats .= ',%d ';
				$values[] = $ip_int;
			}
		} else {
			if ( $ip_int = ip2long( $ip_address ) ) {
				$ip_int = sprintf( '%u', $ip_int );
				$columns .= ',ip';
				$formats .= ',%s';
				$values[] = $ip_int;
			}
		}

		if ( is_array( $data ) && !empty( $data ) ) {
			$columns .= ",data ";
			$formats .= ",%s ";
			$values[] = serialize( $data );
		}

		if ( !empty( $amount ) && !empty( $currency_id ) ) {
			if ( $amount = Affiliates_Utility::verify_referral_amount( $amount ) ) {
				if ( $currency_id =  Affiliates_Utility::verify_currency_id( $currency_id ) ) {
					$columns .= ",amount ";
					$formats .= ",%s ";
					$values[] = $amount;

					$columns .= ",currency_id ";
					$formats .= ",%s ";
					$values[] = $currency_id;
				}
			}
		}
		if ( !empty( $status ) && Affiliates_Utility::verify_referral_status_transition( $status, $status ) ) {
			$columns .= ',status ';
			$formats .= ',%s ';
			$values[] = $status;
		} else {
			$columns .= ',status ';
			$formats .= ',%s ';
			$values[] = get_option( 'aff_default_referral_status', AFFILIATES_REFERRAL_STATUS_ACCEPTED );
		}
			
		if ( !empty( $type ) ) {
			$columns  .= ',type ';
			$formats  .= ',%s';
			$values[] = $type;
		}
			
		if ( !empty( $reference ) ) {
			$columns  .= ',reference ';
			$formats  .= ',%s';
			$values[] = $reference;
		}

		$columns .= ")";
		$formats .= ")";

		// add the referral
		$keys = explode( ',', str_replace( ' ', '', substr( $columns, 1, strlen( $columns ) - 2 ) ) );
		$referral_data = array_combine( $keys, $values );
		$record_referral = apply_filters( 'affiliates_record_referral', true, $referral_data );
		if ( $record_referral ) {
			if ( !affiliates_is_duplicate_referral( compact( 'affiliate_id', 'amount', 'currency_id', 'type', 'reference', 'data' ) ) ) {
				$query = $wpdb->prepare( "INSERT INTO $table $columns VALUES $formats", $values );
				if ( $wpdb->query( $query ) !== false ) {
					if ( $referral_id = $wpdb->get_var( "SELECT LAST_INSERT_ID()" ) ) {
						do_action(
							'affiliates_referral',
							$referral_id,
							array(
								'affiliate_id' => $affiliate_id,
								'post_id' => $post_id,
								'description' => $description,
								'data' => $data,
								'amount' => $amount, 
								'currency_id' => $currency_id,
								'status' => $status,
								'type' => $type,
								'reference' => $reference
							)
						);
					}
				}
			}
		}
	}
	return $affiliate_id;
}

/**
 * Determines whether a referral is considered as a duplicate of an existing
 * one. Semantics: Based on the option aff_duplicates, if the option allows
 * duplicates, no referral will be considered as a duplicate. If the option
 * does not allow duplicates, a duplicate is identified as such based on the
 * amount of attribute available: affiliate_id, amount, currency_id, type,
 * referemce and data.
 * 
 * @param array $atts referral attributes: affiliate_id (required) and others
 */
function affiliates_is_duplicate_referral( $atts ) {

	global $wpdb;

	extract( $atts );

	$is_duplicate = false;
	if ( !get_option( 'aff_duplicates', false ) ) {
		if ( isset( $affiliate_id ) ) {
			$table = _affiliates_get_tablename( 'referrals' );
			$query = "SELECT * FROM $table WHERE affiliate_id = %d";
			$args = array( $affiliate_id );
			if ( !empty( $amount ) ) {
				$query .= " AND amount = %s";
				$args[] = $amount;
			} else {
				$query .= " AND ( amount IS NULL OR amount = '' ) ";
			}
			if ( !empty( $currency_id ) ) {
				$query .= " AND currency_id = %s";
				$args[] = $currency_id;
			} else {
				$query .= " AND ( currency_id IS NULL OR currency_id = '' ) ";
			}
			if ( !empty( $type ) ) {
				$query .= " AND type = %s";
				$args[] = $type;
			} else {
				$query .= " AND ( type IS NULL OR type = '' ) ";
			}
			if ( !empty( $reference ) ) {
				$query .= " AND reference = %s";
				$args[] = $reference;
			} else {
				$query .= " AND ( reference IS NULL OR reference = '' ) ";
			}
			if ( !empty( $data ) && is_array( $data ) ) {
				$query .= " AND data = %s";
				$args[] = serialize( $data );
			} else {
				$query .= " AND ( data IS NULL OR data = '' ) ";
			}
			if ( $wpdb->get_results( $wpdb->prepare( $query, $args ) ) ) {
				$is_duplicate = true;
			}
		}
	}
	return apply_filters( 'affiliates_is_duplicate_referral', $is_duplicate, $atts );
}

/**
 * Update the referral.
 * @param array $attributes to update, supports: affiliate_id, post_id, datetime, description, amount, currency_id, status, reference
 * @return array with keys, values and old_values or null if nothing was updated
 */
function affiliates_update_referral( $referral_id, $attributes ) {
	global $wpdb;

	$result = null;

	$referral = null;
	$referrals_table = _affiliates_get_tablename( 'referrals' );
	if ( $referrals = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $referrals_table WHERE referral_id = %d", intval( $referral_id) ) ) ) {
		if ( count( $referrals ) > 0 ) {
			$referral = $referrals[0];
		}
	}

	if ( $referral !== null ) {
		$set        = array();
		$keys       = array();
		$values     = array();
		$old_values = array();
		foreach( $attributes as $key => $value ) {
			$current_value = isset( $referral->$key ) ? $referral->$key : null;
			if ( $current_value !== $value ) {
				switch( $key ) {
					case 'affiliate_id' :
					case 'post_id' :
						$set[]        = " $key = %d ";
						$keys[]       = $key;
						$values[]     = intval( $value );
						$old_values[] = $current_value;
						break;
					case 'datetime' :
					case 'description' :
					case 'reference' :
						$set[]        = " $key = %s ";
						$keys[]       = $key;
						$values[]     = $value;
						$old_values[] = $current_value;
						break;
					case 'status' :
						// Just check that this is a valid status:
						if ( !empty( $value ) && Affiliates_Utility::verify_referral_status_transition( $value, $value ) ) {
							$set[]        = " $key = %s ";
							$keys[]       = $key;
							$values[]     = $value;
							$old_values[] = $current_value;
						}
						break;
					case 'amount' :
						if ( $value = Affiliates_Utility::verify_referral_amount( $value ) ) {
							$set[]        = " $key = %s ";
							$keys[]       = $key;
							$values[]     = $value;
							$old_values[] = $current_value;
						}
						break;
					case 'currency_id' :
						if ( $value =  Affiliates_Utility::verify_currency_id( $value ) ) {
							$set[]        = " $key = %s ";
							$keys[]       = $key;
							$values[]     = $value;
							$old_values[] = $current_value;
						}
						break;
				}
			}
		}
		if ( count( $set ) > 0 ) {
			$set = implode( ' , ', $set );
			if ( $wpdb->query( $wpdb->prepare( "UPDATE $referrals_table SET $set WHERE referral_id = %d", array_merge( $values, array( intval( $referral_id ) ) ) ) ) ) {
				$result = array(
					'keys'       => $keys,
					'values'     => $values,
					'old_values' => $old_values
				);
				do_action( 'affiliates_updated_referral', intval( $referral_id ), $keys, $values, $old_values );
			}
		}
	}
	return $result;
}



/**
 * Returns an array of possible id encodings.
 * @return array of possible id encodings, keys: encoding identifier, values: encoding name
 */
function affiliates_get_id_encodings() {
	return array(
		AFFILIATES_NO_ID_ENCODING => __( 'No encoding', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_MD5_ID_ENCODING => __( 'MD5', AFFILIATES_PLUGIN_DOMAIN )
	);
}

/**
 * Returns an encoded affiliate id.
 * If AFFILIATES_NO_ID_ENCODING is in effect, the $affiliate_id is returned as-is.
 * @param string|int $affiliate_id the affiliate id to encode
 * @return encoded affiliate id
 */
function affiliates_encode_affiliate_id( $affiliate_id ) {
	global $affiliates_options;
	$encoded_id = null;

	$id_encoding = get_option( 'aff_id_encoding', AFFILIATES_NO_ID_ENCODING );
	switch ( $id_encoding ) {
		case AFFILIATES_MD5_ID_ENCODING :
			$encoded_id = md5( $affiliate_id );
			break;
		default:
			$encoded_id = $affiliate_id;
	}
	return $encoded_id;
}

/**
 * Checks if an affiliate id is from a currently valid affiliate.
 * @param string $affiliate_id the affiliate id
 * @return returns the affiliate id if valid, otherwise FALSE
 */
function affiliates_check_affiliate_id_encoded( $affiliate_id ) {

	global $wpdb, $affiliates_options;

	$id_encoding = get_option( 'aff_id_encoding', AFFILIATES_NO_ID_ENCODING );
	switch( $id_encoding ) {
		case AFFILIATES_MD5_ID_ENCODING :
			$result = affiliates_check_affiliate_id_md5( $affiliate_id );
			break;
		default :
			$result = affiliates_check_affiliate_id( $affiliate_id );
	}
	return $result;
}

/**
 * Checks if an affiliate id is from a currently valid affiliate.
 * @param string $affiliate_id the affiliate id
 * @return returns the affiliate id if valid, otherwise FALSE
 */
function affiliates_check_affiliate_id( $affiliate_id ) {

	global $wpdb;
	$result = FALSE;
	$today = date( 'Y-m-d', time() );
	$table = _affiliates_get_tablename( 'affiliates' );
	$query = $wpdb->prepare( "SELECT * FROM $table WHERE affiliate_id = %d AND from_date <= %s AND ( thru_date IS NULL OR thru_date >= %s ) AND status = 'active'", intval( $affiliate_id ), $today, $today );
	$affiliate = $wpdb->get_row( $query, OBJECT );
	if ( !empty( $affiliate ) ) {
		$result = $affiliate->affiliate_id;
	}
	return $result;
}

/**
 * Checks if an md5-encoded affiliate id is from a currently valid affiliate.
 * @param string $affiliate_id_md5 the md5-encoded affiliate id
 * @return returns the (unencoded) affiliate id if valid, otherwise FALSE
 */
function affiliates_check_affiliate_id_md5( $affiliate_id_md5 ) {

	global $wpdb;
	$result = FALSE;
	$today = date( 'Y-m-d', time() );
	$table = _affiliates_get_tablename( 'affiliates' );
	$query = $wpdb->prepare( "SELECT * FROM (SELECT *, md5(affiliate_id) as affiliate_id_md5 FROM $table) md5d WHERE affiliate_id_md5 = %s AND from_date <= %s AND ( thru_date IS NULL OR thru_date >= %s ) AND status = 'active'", $affiliate_id_md5, $today, $today );
	$affiliate = $wpdb->get_row( $query, OBJECT );
	if ( !empty( $affiliate ) ) {
		$result = $affiliate->affiliate_id;
	}
	return $result;
}

/**
 * Returns the first id of an affiliate of type AFFILIATES_DIRECT_TYPE.
 * @return returns the affiliate id (if there is at least one of type AFFILIATES_DIRECT_TYPE), otherwise FALSE
 */
function affiliates_get_direct_id() {
	global $wpdb;
	$result = FALSE;
	$today = date( 'Y-m-d', time() );
	$table = _affiliates_get_tablename( 'affiliates' );
	$query = $wpdb->prepare( "SELECT * FROM $table WHERE type = %s AND from_date <= %s AND ( thru_date IS NULL OR thru_date >= %s ) AND status = 'active'", AFFILIATES_DIRECT_TYPE, $today, $today );
	$affiliate = $wpdb->get_row( $query, OBJECT );
	if ( !empty( $affiliate ) ) {
		$result = $affiliate->affiliate_id;
	}
	return $result;
}
 

// only needed when in admin
if ( is_admin() ) {
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin.php';
	include_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin-user-registration.php';
	if ( AFFILIATES_PLUGIN_NAME == 'affiliates' ) {
		include_once AFFILIATES_CORE_LIB . '/class-affiliates-totals.php';
	}
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin-add-ons.php';
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin-affiliates.php';
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin-hits.php';
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin-hits-affiliate.php';
	include_once AFFILIATES_CORE_LIB . '/affiliates-admin-referrals.php';
	include_once AFFILIATES_CORE_LIB . '/class-affiliates-dashboard-widget.php';
	include_once AFFILIATES_CORE_LIB . '/class-affiliates-admin-user-profile.php';
	add_action( 'admin_menu', 'affiliates_admin_menu' );
	add_action( 'network_admin_menu', 'affiliates_network_admin_menu' );
}

/**
 * Register our admin section.
 * Arrange to load styles and scripts required.
 */
function affiliates_admin_menu() {

	$pages = array();

	// main
	$page = add_menu_page(
		__( 'Affiliates Overview', AFFILIATES_PLUGIN_DOMAIN ),
		'Affiliates', // @todo translate after core bug 18857 has been fixed http://core.trac.wordpress.org/ticket/18857 translation affects $screen->id
		AFFILIATES_ACCESS_AFFILIATES,
		'affiliates-admin',
		'affiliates_admin',
		AFFILIATES_PLUGIN_URL . '/images/affiliates.png',
		'58.187'
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// overview on affiliates-admin
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'Manage Affiliates', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Manage Affiliates', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ADMINISTER_AFFILIATES,
		'affiliates-admin-affiliates',
		apply_filters( 'affiliates_add_submenu_page_function', 'affiliates_admin_affiliates' )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// hits by date
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'Visits & Referrals', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Visits & Referrals', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ACCESS_AFFILIATES,
		'affiliates-admin-hits',
		apply_filters( 'affiliates_add_submenu_page_function', 'affiliates_admin_hits' )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// hits by affiliate
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'Affiliates & Referrals', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Affiliates & Referrals', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ACCESS_AFFILIATES,
		'affiliates-admin-hits-affiliate',
		apply_filters( 'affiliates_add_submenu_page_function', 'affiliates_admin_hits_affiliate' )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// referrals
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'Referrals', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Referrals', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ACCESS_AFFILIATES,
		'affiliates-admin-referrals',
		apply_filters( 'affiliates_add_submenu_page_function', 'affiliates_admin_referrals' )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// totals
	if ( AFFILIATES_PLUGIN_NAME == 'affiliates' ) {
		$page = add_submenu_page(
			'affiliates-admin',
			__( 'Totals', AFFILIATES_PLUGIN_DOMAIN ),
			__( 'Totals', AFFILIATES_PLUGIN_DOMAIN ),
			AFFILIATES_ACCESS_AFFILIATES,
			'affiliates-admin-totals',
			apply_filters( 'affiliates_add_submenu_page_function', array( 'Affiliates_Totals', 'view' ) )
		);
		$pages[] = $page;
		add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
		add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );
	}

	// settings
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'Affiliates Settings', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Settings', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ADMINISTER_OPTIONS,
		'affiliates-admin-settings',
		apply_filters( 'affiliates_add_submenu_page_function', array( 'Affiliates_Settings', 'admin_settings' ) )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// user registration
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'User Registration', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'User Registration', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ADMINISTER_OPTIONS,
		'affiliates-admin-user-registration',
		apply_filters( 'affiliates_add_submenu_page_function', 'affiliates_admin_user_registration' )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	// add-ons
	$page = add_submenu_page(
		'affiliates-admin',
		__( 'Add-Ons', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Add-Ons', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ADMINISTER_OPTIONS,
		'affiliates-admin-add-ons',
		apply_filters( 'affiliates_add_submenu_page_function', 'affiliates_admin_add_ons' )
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );

	do_action( 'affiliates_admin_menu', $pages );
}

/**
 * Adds network admin menu.
 */
function affiliates_network_admin_menu() {
	include_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-network.php';
	$pages = array();
	$page = add_menu_page(
		__( 'Affiliates', AFFILIATES_PLUGIN_DOMAIN ),
		__( 'Affiliates', AFFILIATES_PLUGIN_DOMAIN ),
		AFFILIATES_ACCESS_AFFILIATES,
		'affiliates-network-admin',
		array( 'Affiliates_Settings_Network', 'network_admin_settings' ),
		AFFILIATES_PLUGIN_URL . '/images/affiliates.png'
	);
	$pages[] = $page;
	add_action( 'admin_print_styles-' . $page, 'affiliates_admin_print_styles' );
	add_action( 'admin_print_scripts-' . $page, 'affiliates_admin_print_scripts' );
	do_action( 'affiliates_network_admin_menu', $pages );
}

add_action( 'contextual_help', 'affiliates_contextual_help', 10, 3 );

function affiliates_contextual_help( $contextual_help, $screen_id, $screen ) {

	$pname = get_option( 'aff_pname', AFFILIATES_PNAME );

	$show_affiliates_help = false;

	$title = '<h3>';
	$title .= sprintf( '<a href="%s" target="_blank">%s</a>', esc_attr( 'http://www.itthinx.com/plugins/affiliates' ), __( 'Affiliates', AFFILIATES_PLUGIN_DOMAIN ) );
	$title .= '</h3>';

	$help = apply_filters( 'affiliates_help_tab_title', $title );

	switch ( $screen_id ) {
		case 'toplevel_page_affiliates-admin' :
			$show_affiliates_help = true;
			$help .= '<p>' . __( 'This screen offers an overview with basic statistical data.', AFFILIATES_PLUGIN_DOMAIN ) . '</p>';
			$help .= '<ul>';
			$help .= '<li>' . __( '<em>From operative affiliates:</em> includes affiliates that are currently active. This excludes affiliates whose dates are not currently valid as well as those that have been deleted.', AFFILIATES_PLUGIN_DOMAIN ) . '</li>';
			$help .= '<li>' . __( '<em>From operative and non-operative affiliates:</em> excludes deleted affiliates.', AFFILIATES_PLUGIN_DOMAIN ) . '</li>';
			$help .= '<li>' . __( '<em>All time</em> includes data from any affiliates, including deleted affiliates.', AFFILIATES_PLUGIN_DOMAIN ) . '</li>';
			$help .= '<ul>';
			$help .= '<ul>';
			$help .= '<li>' . __( '<em>Hits</em> are HTTP requests for affiliate links.', AFFILIATES_PLUGIN_DOMAIN ) . '</li>';
			$help .= '<li>' . __( '<em>Visits</em> are unique and daily requests for affiliate links.', AFFILIATES_PLUGIN_DOMAIN ) . '</li>';
			$help .= '<li>' . __( '<em>Referrals</em> are recording commissions and referral data.', AFFILIATES_PLUGIN_DOMAIN ) . '</li>';
			$help .= '</ul>';
			$help .= '<p>';
			$help .= __( 'The Affiliates plugin provides the <em>Affiliates Contact</em> widget that can be used to record lead referrals.', AFFILIATES_PLUGIN_DOMAIN );
			$help .= sprintf( __( 'To use it, place the widget in one of your <a href="%s">widget areas</a>.', AFFILIATES_PLUGIN_DOMAIN ), get_admin_url( null, 'widgets.php' ) );
			$help .= '</p>';

			$help .= '<p>';
			$help .= __( 'Note that <em>deleted</em> affiliates are not literally deleted but marked as such so that data that has been collected will still be accesible.', AFFILIATES_PLUGIN_DOMAIN );
			$help .= '</p>';
			break;
		case 'affiliates_page_affiliates-admin-affiliates':
			$show_affiliates_help = true;
			$help .=
				'<p>' .
				__( 'Here you can <strong>add</strong>, <strong>edit</strong> and <strong>remove</strong> affiliates.', AFFILIATES_PLUGIN_DOMAIN ) .
				'</p>';
			$help .=
				'<ul>' .
				'<li>' .
				'<p class="affiliate-link">' .
				__( 'Affiliate link', AFFILIATES_PLUGIN_DOMAIN ) .
				'<p/>' .
				'<p>' .
				__( 'This link uses a parameter in the URL to record visits you receive through your affiliates.', AFFILIATES_PLUGIN_DOMAIN ) . ' ' .
				'<br/>' .
				sprintf( __( 'You may also append the ?%s=... part to links to your posts.', AFFILIATES_PLUGIN_DOMAIN ), $pname ) .
				'</p>' .
				'</li>' .
				'</ul>' .
				'<p>' .
				__( 'Once a visitor has landed on your site through an affiliate link, referrals may be recorded and attributed to the affiliate.', AFFILIATES_PLUGIN_DOMAIN ) .
				'</p>';
			break;
		case 'affiliates_page_affiliates-admin-hits' :
			$show_affiliates_help = true;
			break;
		case 'affiliates_page_affiliates-admin-hits-affiliate' :
			$show_affiliates_help = true;
			break;
		case 'affiliates_page_affiliates-admin-totals' :
			$show_affiliates_help = true;
			break;
		case 'affiliates_page_affiliates-admin-referrals' :
			$show_affiliates_help = true;
			break;
		case 'affiliates_page_affiliates-admin-options' :
			$show_affiliates_help = true;
			break;
		default:
	}

	if ( !defined( 'AFFILIATES_PRO_PLUGIN_DOMAIN' ) && !defined( 'AFFILIATES_ENTERPRISE_PLUGIN_DOMAIN' ) ) {
		$help .= '<p>';
		$help .= affiliates_donate( false, true );
		$help .= '</p>';
	}

	$help .= affiliates_help_tab_footer( false );

	if ( $show_affiliates_help ) {
		return $help;
	} else {
		return $contextual_help;
	}
}

/**
 * Returns or renders the common footer for help tabs.
 * @param boolean $render
 * @return string or nothing
 */
function affiliates_help_tab_footer( $render = true ) {
	$footer =
		'<div class="affiliates-documentation">' .
		sprintf(
			'<a href="%s">%s</a>',
			esc_attr( 'http://docs.itthinx.com/document/affiliates/' ),
			esc_html( __( 'Online documentation', AFFILIATES_PLUGIN_DOMAIN ) )
		) .
		'</div>';
	$footer = apply_filters( 'affiliates_help_tab_footer', $footer );
	if ( $render ) {
		echo $footer;
	} else {
		return $footer;
	}
}

/**
 * Returns or renders the footer.
 * @param boolean $render
 * @return string or nothing
 */
function affiliates_footer( $render = true ) {
	$footer = '<div class="affiliates-footer">' .
		'<p>' .
		__( 'Thank you for using the <a style="text-decoration:none;" href="http://www.itthinx.com/plugins/affiliates" target="_blank">Affiliates</a> plugin by <a style="text-decoration:none;" href="http://www.itthinx.com" target="_blank">itthinx</a>.', AFFILIATES_PLUGIN_DOMAIN ) .
		' ' .
		sprintf(
			__( 'Please give it a <a style="text-decoration:none;" href="%s">&#9733;&#9733;&#9733;&#9733;&#9733;</a> rating!', AFFILIATES_PLUGIN_DOMAIN ),
			'http://wordpress.org/support/view/plugin-reviews/affiliates?filter=5#postform'
		) .
		'</p>' .
		'<p>' .
		affiliates_donate( false ) .
		'</p>' .
		'</div>';
	$footer = apply_filters( 'affiliates_footer', $footer );
	if ( $render ) {
		echo $footer;
	} else {
		return $footer;
	}
}

/**
 * Render or return a donation button.
 * Thanks for supporting me!
 * @param boolean $render
 */
function affiliates_donate( $render = true, $small = false ) {
	$donate = sprintf(
		'<a class="button" href="http://www.itthinx.com/shop/">%s</a>',
		__( 'Get Affiliates Pro', AFFILIATES_PLUGIN_DOMAIN )
	);
	if ( $render ) {
		echo $donate;
	} else {
		return $donate;
	}
}

/**
 * Retrieves an affiliate by id.
 * The array returned provides the following attributes:
 * <ul>
 * <li>affiliate_id - the integer id</li>
 * <li>name - the affiliate's name</li>
 * <li>email - email address</li>
 * <li>from_date - from when the affiliate is valid (*)</li>
 * <li>thru_date - [optional] until when the affiliate is valid (*)</li>
 * <li>status - either 'active' or 'deleted'</li>
 * </ul>
 * (*) Suggested referrals will succeed if the current date is within those dates. Both are inclusive.
 *
 * Usage example:
 * $affiliate = affiliates_get_affiliate( $affiliate_id );
 * $email = $affiliate['email'];
 *
 * @param int|string $affiliate_id the affiliate's id
 * @return array affiliate details or null
 */
function affiliates_get_affiliate( $affiliate_id ) {
	global $wpdb;
	$table = _affiliates_get_tablename( 'affiliates' );
	$affiliate = $wpdb->get_row(
		$wpdb->prepare( "SELECT * FROM $table WHERE affiliate_id = %d", intval( $affiliate_id ) ),
		ARRAY_A
	);
	return $affiliate;
}

/**
 * Return the user id related to an affiliate.
 * @param int $affiliate_id
 * @return user_id 
 */
function affiliates_get_affiliate_user( $affiliate_id ) {
	global $wpdb;
	$table = _affiliates_get_tablename( 'affiliates_users' );
	$user_id = $wpdb->get_var( $wpdb->prepare(
		"SELECT user_id FROM $table WHERE affiliate_id = %d", intval( $affiliate_id )
	) );
	return $user_id;
}

/**
 * Return the affiliate ids related to a user. 
 * @param int $user_id
 * @return array of int affiliate ids or null on failure
 */
function affiliates_get_user_affiliate( $user_id ) {
	global $wpdb;
	$result = null;
	$affiliates_table = _affiliates_get_tablename( 'affiliates' );
	$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
	if ( $affiliates = $wpdb->get_results( $wpdb->prepare(
		"SELECT $affiliates_table.affiliate_id FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status ='active'",
		intval( $user_id )
	) ) ) {
		$result = array();
		foreach( $affiliates as $affiliate ) {
			$result[] = $affiliate->affiliate_id;
		}
	}
	return $result;
}

/**
 * Returns true if the user is an affiliate.
 * @param int|object $user (optional) specify a user or use current if none given
 */
function affiliates_user_is_affiliate( $user_id = null ) {
	global $wpdb;
	$result = false;
	if ( is_user_logged_in() ) {
		if ( $user_id == null ) {
			$user = wp_get_current_user();
		} else {
			$user = get_user_by( 'id', $user_id );
		}
		if ( $user ) {
			$user_id = $user->ID;
			$affiliates_table = _affiliates_get_tablename( 'affiliates' );
			$affiliates_users_table = _affiliates_get_tablename( 'affiliates_users' );
			$affiliates = $wpdb->get_results(
				$wpdb->prepare( "SELECT * FROM $affiliates_users_table LEFT JOIN $affiliates_table ON $affiliates_users_table.affiliate_id = $affiliates_table.affiliate_id WHERE $affiliates_users_table.user_id = %d AND $affiliates_table.status ='active'", intval( $user_id ) )
			);
			$result = !empty( $affiliates );
		}
	}
	return $result;
}

/**
 * Returns an array of affiliates.
 * @param boolean $active returns only active affiliates (not deleted via the admin UI)
 * @return array of affiliates
 */
function affiliates_get_affiliates( $active = true, $valid = true ) {

	global $wpdb;
	$results = array();
	$today = date( 'Y-m-d', time() );
	$table = _affiliates_get_tablename( 'affiliates' );
	if ( $active ) {
		if ( $valid ) {
			$query = $wpdb->prepare( "SELECT * FROM $table WHERE from_date <= %s AND ( thru_date IS NULL OR thru_date >= %s ) AND status = 'active' ORDER BY NAME", $today, $today );
	 } else {
	 	$query = "SELECT * FROM $table WHERE status = 'active' ORDER BY NAME";
	 }
	} else {
		if ( $valid ) {
			$query = $wpdb->prepare( "SELECT * FROM $table WHERE from_date <= %s AND ( thru_date IS NULL OR thru_date >= %s ) ORDER BY NAME", $today, $today );
		} else {
			$query = "SELECT * FROM $table ORDER BY NAME";
		}
	}
	if ( $affiliates = $wpdb->get_results( $query, ARRAY_A ) ) {
		$results = $affiliates;
	}
	return $results;
}

/**
 * Returns the number of hits for a given affiliate.
 *
 * @param int $affiliate_id the affiliate's id
 * @param string $from_date optional from date
 * @param string $thru_date optional thru date
 * @param boolean $precise take time into account ($from_date and $thru_date include time)
 * @return int number of hits
 */
function affiliates_get_affiliate_hits( $affiliate_id, $from_date = null , $thru_date = null, $precise = false ) {
	global $wpdb;
	$hits_table = _affiliates_get_tablename('hits');
	$result = 0;
	$where = " WHERE affiliate_id = %d";
	$values = array( $affiliate_id );
	if ( $from_date ) {
		if ( $precise ) {
			$from_datetime = date( 'Y-m-d H:i:s', strtotime( $from_date ) );
		} else {
			$from_date = date( 'Y-m-d', strtotime( $from_date ) );
		}
	}
	if ( $thru_date ) {
		if ( $precise ) {
			$thru_datetime = date( 'Y-m-d H:i:s', strtotime( $thru_date ) );
		} else {
			$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
		}
	}
	if ( $from_date && $thru_date ) {
		if ( $precise ) {
			$where .= " AND datetime >= %s AND datetime < %s ";
			$values[] = $from_datetime;
			$values[] = $thru_datetime;
		} else {
			$where .= " AND date >= %s AND date < %s ";
			$values[] = $from_date;
			$values[] = $thru_date;
		}
	} else if ( $from_date ) {
		if ( $precise ) {
			$where .= " AND datetime >= %s ";
			$values[] = $from_datetime;
		} else {
			$where .= " AND date >= %s ";
			$values[] = $from_date;
		}
	} else if ( $thru_date ) {
		if ( $precise ) {
			$where .= " AND datetime < %s ";
			$values[] = $thru_datetime;
		} else {
			$where .= " AND date < %s ";
			$values[] = $thru_date;
		}
	}
	$query = $wpdb->prepare("
		SELECT sum(count) FROM $hits_table $where
		",
		$values
	);
	$result = intval( $wpdb->get_var( $query) );
	return $result;
}

/**
 * Returns the number of visits for a given affiliate.
 * One or more hits from the same IP on the same day count as one visit.
 *
 * @param int $affiliate_id the affiliate's id
 * @param string $from_date optional from date
 * @param string $thru_date optional thru date
 * @param boolean $precise take time into account ($from_date and $thru_date include time)
 * @return int number of visits
 */
function affiliates_get_affiliate_visits( $affiliate_id, $from_date = null , $thru_date = null, $precise = false ) {
	global $wpdb;
	$hits_table = _affiliates_get_tablename('hits');
	$result = 0;
	$where = " WHERE affiliate_id = %d";
	$values = array( $affiliate_id );
// 	if ( $from_date ) {
// 		$from_date = date( 'Y-m-d', strtotime( $from_date ) );
// 	}
// 	if ( $thru_date ) {
// 		$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
// 	}
// 	if ( $from_date && $thru_date ) {
// 		$where .= " AND date >= %s AND date < %s ";
// 		$values[] = $from_date;
// 		$values[] = $thru_date;
// 	} else if ( $from_date ) {
// 		$where .= " AND date >= %s ";
// 		$values[] = $from_date;
// 	} else if ( $thru_date ) {
// 		$where .= " AND date < %s ";
// 		$values[] = $thru_date;
// 	}
	if ( $from_date ) {
		if ( $precise ) {
			$from_datetime = date( 'Y-m-d H:i:s', strtotime( $from_date ) );
		} else {
			$from_date = date( 'Y-m-d', strtotime( $from_date ) );
		}
	}
	if ( $thru_date ) {
		if ( $precise ) {
			$thru_datetime = date( 'Y-m-d H:i:s', strtotime( $thru_date ) );
		} else {
			$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
		}
	}
	if ( $from_date && $thru_date ) {
		if ( $precise ) {
			$where .= " AND datetime >= %s AND datetime < %s ";
			$values[] = $from_datetime;
			$values[] = $thru_datetime;
		} else {
			$where .= " AND date >= %s AND date < %s ";
			$values[] = $from_date;
			$values[] = $thru_date;
		}
	} else if ( $from_date ) {
		if ( $precise ) {
			$where .= " AND datetime >= %s ";
			$values[] = $from_datetime;
		} else {
			$where .= " AND date >= %s ";
			$values[] = $from_date;
		}
	} else if ( $thru_date ) {
		if ( $precise ) {
			$where .= " AND datetime < %s ";
			$values[] = $thru_datetime;
		} else {
			$where .= " AND date < %s ";
			$values[] = $thru_date;
		}
	}
	$query = $wpdb->prepare("
		SELECT SUM(visits) FROM
		(SELECT count(DISTINCT IP) visits FROM $hits_table $where GROUP BY DATE) tmp
		",
		$values
	);
	$result = intval( $wpdb->get_var( $query) );
	return $result;
}

/**
 * Returns the number of referrals for a given affiliate.
 *
 * @param int $affiliate_id the affiliate's id
 * @param string $from_date optional from date
 * @param string $thru_date optional thru date
 * @return int number of hits
 */
function affiliates_get_affiliate_referrals( $affiliate_id, $from_date = null , $thru_date = null, $status = null, $precise = false ) {
	global $wpdb;
	$referrals_table = _affiliates_get_tablename( 'referrals' );
	$result = 0;
	$where = " WHERE affiliate_id = %d";
	$values = array( $affiliate_id );
	if ( $from_date ) {
		if ( $precise ) {
			$from_date = date( 'Y-m-d H:i:s', strtotime( $from_date ) );
		} else {
			$from_date = date( 'Y-m-d', strtotime( $from_date ) );
		}
	}
	if ( $thru_date ) {
		if ( $precise ) {
			$thru_date = date( 'Y-m-d H:i:s', strtotime( $thru_date ) );
		} else {
			$thru_date = date( 'Y-m-d', strtotime( $thru_date ) + 24*3600 );
		}
	}
	if ( $from_date && $thru_date ) {
		$where .= " AND datetime >= %s AND datetime < %s ";
		$values[] = $from_date;
		$values[] = $thru_date;
	} else if ( $from_date ) {
		$where .= " AND datetime >= %s ";
		$values[] = $from_date;
	} else if ( $thru_date ) {
		$where .= " AND datetime < %s ";
		$values[] = $thru_date;
	}
	if ( !empty( $status ) ) {
		$where .= " AND status = %s ";
		$values[] = $status;
	} else {
		$where .= " AND status IN ( %s, %s ) ";
		$values[] = AFFILIATES_REFERRAL_STATUS_ACCEPTED;
		$values[] = AFFILIATES_REFERRAL_STATUS_CLOSED;
	}
	$query = $wpdb->prepare("
		SELECT count(*) FROM $referrals_table $where
		",
		$values
	);
	$result = intval( $wpdb->get_var( $query) );
	return $result;
}

/**
 * Returns the prefixed DB table name.
 * @param string $name the name of the DB table
 * @return string prefixed DB table name
 */
function _affiliates_get_tablename( $name ) {
	global $wpdb;
	return $wpdb->prefix . AFFILIATES_TP . $name;
}

/**
 * Attribute filter for overrides.
 * 
 * @param mixed $value
 * @param int $affiliate_id
 * @param string $key
 * @return mixed
 */
function affiliates_attribute_filter( $value, $affiliate_id, $key ) {
	if ( $user_id = affiliates_get_affiliate_user( $affiliate_id ) ) {
		$maybe_value = get_user_meta( $user_id, $key , true );
		if ( !empty( $maybe_value ) ) {
			$value = $maybe_value;
		}
	}
	return $value;
}

/**
 * Compose a URL based on its components.
 * 
 * $components can provide values indexed by the keys scheme, host, port,
 * user, pass, path, query and fragment as produced by parse_url().
 * 
 * @param array $components
 * @return string URL
 */
function affiliates_compose_url( $components ) {
	$scheme   = isset( $components['scheme'] ) ? $components['scheme'] . '://' : '';
	$host     = isset( $components['host'] ) ? $components['host'] : '';
	$port     = isset( $components['port'] ) ? ':' . $components['port'] : '';
	$user     = isset( $components['user'] ) ? $components['user'] : '';
	$pass     = isset( $components['pass'] ) ? ':' . $components['pass']  : '';
	$pass     = ( !empty( $user ) || !empty( $pass ) ) ? "$pass@" : '';
	$path     = isset( $components['path'] ) ? $components['path'] : '';
	$query    = isset( $components['query'] ) ? '?' . $components['query'] : '';
	$fragment = isset( $components['fragment'] ) ? '#' . $components['fragment'] : '';
	return "$scheme$user$pass$host$port$path$query$fragment";
}

/**
 * Returns the URL converted to an affiliate URL for the given affiliate.
 * 
 * @param string $url
 * @param int $affiliate_id
 */
function affiliates_get_affiliate_url( $url, $affiliate_id ) {
	$pname = get_option( 'aff_pname', AFFILIATES_PNAME );
	$scheme = parse_url( $url, PHP_URL_SCHEME );
	if ( empty( $scheme ) ) {
		$prefix = '';
		// Although scheme is empty we could have a malformed scheme and we don't
		// want to make it worse so also check for http:// and https:// prefixes.
		if ( strpos( $url, 'http://' ) !== 0 && strpos( $url, 'https://' ) !== 0 ) {
			$prefix = !empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) != 'off' ? 'https:' : 'http:';
			if ( strpos( $url, '//' ) !== 0 ) {
				$prefix .= '//';
			}
		}
		$url = $prefix . $url;
	}
	$components = parse_url( $url );
	// If pname is already in query, don't modify.
	if ( strpos( isset( $components['query'] ) ? $components['query'] : '', "$pname=" ) === false ) {
		$query = '';
		if ( !empty( $components['query'] ) ) {
			$query = $components['query'] . '&';
		}
		$encoded_id = affiliates_encode_affiliate_id( $affiliate_id );
		$query .= sprintf( '%s=%s', $pname, $encoded_id );
		if ( empty( $components['path'] ) ) {
			$components['path'] = '/';
		}
		$components['query'] = $query;
	}
	return affiliates_compose_url( $components );
}