Sold!'),
stripslashes('_PRICE_ via PayPal, _SHIPPING_ shipping within US'),
NULL,
NULL,
NULL,
'',
'0',
'0',
stripslashes('Please contact me if you are interested in purchasing this piece.'),
'-1',
stripslashes('Sorry, this item is not currently available for sale. Please check back later.')
);
////////////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////////////
// Define our configuration pages
function ds_ap_add_pages () {
// Add our menu under "options"
add_options_page ( 'ArtPal', 'ArtPal', 'edit_plugins', __FILE__, 'ds_ap_options_page');
// Add our management menu under "Manage"
add_management_page ( 'ArtPal Items', 'ArtPal Items', 'edit_posts', __FILE__, 'ds_ap_manage_page');
}
function ds_ap_buynow_button () {
// Get post ID
global $id;
// Get price of item
$regularprice = get_post_meta ($id, ds_ap_CFPRICE, $single = true );
// If the price is not set, ...
if ($regularprice == null)
{
// Return pre-defined static text if dynamic info is not available.
$textToDisplay = htmlspecialchars_decode ( stripslashes ( get_option ( 'ds_ap_textifunknownmetadata' ) ) );
return $textToDisplay;
}
else {
// Apply discount to item if applicable.
$discount = get_option ( 'ds_ap_discountpercent' );
$price = $regularprice - ( $regularprice * ( $discount / 100 ) );
// Get shipping cost of item
$shipping = get_post_meta ($id, ds_ap_CFSHIPPING, $single = true );
// Get name of item
$name = get_the_title ( $id );
// Generate the button
$button_html = ds_ap_generatepaypalbutton (
stripslashes ( get_option ( 'ds_ap_paypalemail' ) ),
$name,
$id,
$price,
$shipping,
$regularprice
);
return $button_html; // Return the HTML code for the button.
}
}
// Replace the buy-now tag with a button or "SOLD" text.
function ds_ap_buynow_sold () {
return stripslashes ( htmlspecialchars_decode ( get_option ( 'ds_ap_soldcode' ) ) );
}
// Add category reference to post
function ds_ap_AddTaxonomyToObject ( $oid, $tid ) {
global $wpdb;
$sql = 'INSERT INTO ' . $wpdb -> term_relationships . ' ( object_id, term_taxonomy_id ) VALUES ( ' . $oid . ', ' . $tid . ' )';
$wpdb -> query ( $sql );
}
// Remove category reference from postq
function ds_ap_RemoveTaxonomyFromObject ( $oid, $tid ) {
global $wpdb;
$sql = 'DELETE FROM ' . $wpdb -> term_relationships . ' WHERE object_id = ' . $oid . ' AND term_taxonomy_id = ' . $tid;
$wpdb -> query ( $sql );
}
// Change the category of post from old to new
// DOES NOT CHECK TO VERIFY THAT CATEGORY WAS OLD
// This now changes the taxonomy rather than the category; variables need to be changed at some point accordingly.
function ds_ap_change_taxonomy_of_object ( $objid, $old_tid, $new_tid ) {
// apply new category
ds_ap_AddTaxonomyToObject ( $objid, $new_tid );
// remove old category
ds_ap_RemoveTaxonomyFromObject ( $objid, $old_tid );
}
// Figure out if this button has sold and act accordingly
function ds_ap_constructbuynow () {
// Find out if we've sold
// Get post id
global $id;
// Get post category
$cats = wp_get_post_cats ( 1, $id );
// Get category of for-sale artwork.
$cat_forsale = get_option ('ds_ap_unsoldcategory' );
$cat_forsold = get_option ('ds_ap_soldcategory' );
$cat_notsale = get_option ('ds_ap_saledisabledcategory'); // Category indicating that an item is temporarily not for sale.
// Sold unless it's in the category that's for sale.
$sold = false;
// Do we show anything at all (i.e. is it in the sold/unsold category, or a completely different one?)
$show = false;
// By default, an item is eligible to be on sale. This will be set to false if the item is found to belong to the category of items that are explicitly off sale (temporarily disabled).
$allowSale = true;
foreach ( $cats as $cat ) {
if ( $cat == $cat_forsale ) {
$show = true;
$sold = false;
}
if ( $cat == $cat_forsold ) {
$show = true;
$sold = true;
}
if ( $cat == $cat_notsale) {
$allowSale = false;
}
}
// If sold, tell user
if ( $sold && $show ) {
return ds_ap_buynow_sold ();
}
// If unsold and sale is allowed, draw button
else if ($show && $allowSale) {
return ds_ap_buynow_button ();
}
// If unsold and sale is not allowed, show not allowed text.
else if ($show && !$allowSale) {
return stripslashes ( htmlspecialchars_decode ( get_option('ds_ap_textifsaledisabled') ) );
}
else {
return NULL;
}
}
// Process IPN request
function ds_ap_doipn () {
// $myFile = "./ipnoutput.txt";
// $fh = fopen($myFile, 'w');
// fwrite ( $fh, "--------------------------------------------------\n" );
// fwrite ( $fh, "Begin Instant Payment Notification\n" );
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
// Get each element of IPN request
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
// fwrite ( $fh, "$key = $value \n" );
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
if (!$fp) {
// fwrite ( $fh, "HTTP ERROR\n" );
// HTTP ERROR
}
else {
// fwrite ( $fh, "NO HTTP ERROR\n" );
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
// fwrite ( $fh, "VERIFIED = 0\n" );
// check the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
if ( strtolower ( urldecode ( $receiver_email ) ) != strtolower ( get_option ( 'ds_ap_paypalemail' ) ) ) {
// fwrite ( $fh, "RECEIVER EMAILS DONT MATCH\n" );
exit;
}
// check that payment_amount/payment_currency are correct
// process payment
// Mark as sold!
$post_id = $item_number;
$old_cat = get_option ( 'ds_ap_unsoldcategory' );
$new_cat = get_option ( 'ds_ap_soldcategory' );
// fwrite ( $fh, "Changing Category of Post $post_id from $old_cat to $new_cat..." );
ds_ap_change_taxonomy_of_object ( $post_id, $old_cat, $new_cat );
// fwrite ( $fh, "done\n" );
// Flush the cache on the item in question.
if (defined('WP_CACHE') && WP_CACHE == true) {
wp_cache_no_postid($item_number);
}
}
else if (strcmp ($res, "INVALID") == 0) {
// fwrite ( $fh, "INVALID = 0\n" );
// log for manual investigation
echo stripslashes ( get_option ( 'ds_ap_suspiciousactivitymsg' ) );
echo '
';
echo 'Click to return to my site.
';
}
}
fclose ($fp);
}
// fwrite ( $fh, "End Instant Payment Notification\n" );
// fwrite ( $fh, "--------------------------------------------------\n" );
// fclose ( $fh );
$item_number = $_GET [ 'itempurchased' ];
return '';
}
// Generate a PayPal button to purchase a particular item
function ds_ap_generatepaypalbutton ( $selleremail, $itemname, $itemnumber, $price, $shipping, $regularprice = NULL) {
if ( $regularprice == $price )
$regularprice = NULL;
$pretext = htmlspecialchars_decode ( stripslashes ( get_option ( 'ds_ap_prebuttontext' ) ) );
$pricetext = '$' . $price;
// If regular price isn't the same as the current price, ...
if ( $regularprice != NULL ) {
// ... show the savings!
$pricetext = '$' . $regularprice . ' ' . $pricetext;
}
$pretext = str_replace ( '_PRICE_', $pricetext, $pretext );
if ( $shipping == 0 )
$shipping = 'free';
else
$shipping = '$' . $shipping;
$pretext = str_replace ( '_SHIPPING_', $shipping, $pretext );
if ( $shipping == 'free' )
$shipping = 0;
$button_html = $pretext . '
';
// Don't create the PayPal button if ecommerce is disabled.
if ( ! get_option ( 'ds_ap_disableecommerce' ) ) {
$button_html .= '