CURDATE()
AND up.expiration_unit = 'D'
)
OR
(
DATE_ADD(up.purchase_date, INTERVAL up.expire WEEK) > CURDATE()
AND up.expiration_unit = 'W'
)
OR
(
DATE_ADD(up.purchase_date, INTERVAL up.expire MONTH) > CURDATE()
AND up.expiration_unit = 'M'
)
OR
(
DATE_ADD(up.purchase_date, INTERVAL up.expire YEAR) > CURDATE()
AND up.expiration_unit = 'Y'
)
OR up.expire is null
OR up.expire = 0
";
var $users_library_page;
var $purchased_posts_list_placeholder;
var $smarty;
var $msg_writable_folders;
function Are_PayPal() {
global $wpdb;
$configuration = new Are_PayPal_Configuration ( );
$this->check_writable_folders ();
//Initialize properties
$this->start_delimiter = $configuration->start_delimiter;
$this->end_delimiter = $configuration->end_delimiter;
$this->delimiter_tag_name = $configuration->delimiter_tag_name;
$this->paied_users_table = $configuration->paied_users_table;
$this->paied_items_table = $configuration->paied_items_table;
$this->bonus_posts_table = $configuration->bonus_posts_table;
$this->paypal_requests_table = $configuration->paypal_requests_table;
$this->paypal_field_types_table = $configuration->paypal_field_types_table;
$this->paypal_fields_table = $configuration->paypal_fields_table;
$this->users_library_page = $configuration->users_library_page;
$this->purchased_posts_list_placeholder = $configuration->purchased_posts_list_placeholder;
$this->paypal_url = $configuration->paypal_url;
$this->paypal_email = $configuration->paypal_email;
//Hook into wordpress
add_action ( 'admin_menu', array (&$this, 'Are_PayPal_Configuration' ) );
if (!$this->msg_writable_folders) {
add_filter ( 'the_content', array (&$this, 'post_filter' ) );
add_filter ( 'the_content', array (&$this, 'purchased_posts' ) );
add_action ( 'wp_head', array (&$this, 'add_html_headers' ) );
add_action ( 'wp_footer', array (&$this, 'put_my_url_to_footer' ) );
add_action ( 'admin_head', array (&$this, 'admin_head' ));
}
$install = new Are_PayPal_Install ( );
register_activation_hook ( __FILE__, array (&$install, 'install' ) );
$this->set_templates ();
$this->smarty =& new Smarty;
$this->smarty->template_dir = dirname ( __FILE__ ) . "/templates/";
$this->smarty->compile_dir = dirname ( __FILE__ ) . "/cache/";
$this->smarty->cache_dir = dirname ( __FILE__ ) . "/cache/";
$this->smarty->config_dir = dirname ( __FILE__ ) . "/configs/";
$this->smarty->assign("Prefix",$this->prefix);
$this->smarty->assign ("HomeUrl", get_option ( "home" ) );
}
function admin_head() {
echo '';
}
function check_writable_folders() {
$this->msg_writable_folders = $this->is_folder_writable ( dirname ( __FILE__ ) . "/cache/" );
}
function is_folder_writable($folder) {
if (! is_writable ( $folder )) {
return ($folder . " " . __ ( "must be writable" ));
}
return "";
}
function put_my_url_to_footer() {
$this->smarty->assign ( 'my_url_text', __ ( "This blog is monetized using Are-PayPal WP Plugin" ) );
$this->smarty->display ( 'my_url_in_footer.tpl' );
}
function set_templates() {
$templates = new Are_PayPal_Templates ( );
$templates->set_templates ();
}
function show_login_button($url, $urltext) {
$templateName = $this->prefix . "_LoginButtonTemplate";
$result = stripslashes ( get_option ( $templateName ) );
$explanation = get_option ( $this->prefix . '_TextToShowIfNotLogedIn' );
$result = str_replace ( '%EXPLANATION%', $explanation, $result );
$result = str_replace ( '%LOGINURL%', $url, $result );
$result = str_replace ( '%LOGINURLTEXT%', $urltext, $result );
return $result;
}
function purchased_posts($content) {
global $wpdb;
if (strpos ( $content, $this->purchased_posts_list_placeholder ) === false) {
if (is_page ( $this->users_library_page )) {
$content .= $this->purchased_posts_list_placeholder;
}
}
if (! (strpos ( $content, $this->purchased_posts_list_placeholder ) === false)) {
global $current_user, $user_ID;
$userID = $user_ID;
if ($userID == 0) {
$userID = $current_user->id;
}
$sql = "SELECT DISTINCT posts.post_title,posts.guid, items.post_id FROM $wpdb->posts posts INNER JOIN $this->paied_users_table items ON items.post_id=posts.id WHERE user_id ='$userID' AND $this->post_type_clause";
$purchased_posts = $wpdb->get_results ( $sql, OBJECT );
if ($purchased_posts) {
$purchasedPostsList = "";
foreach ( $purchased_posts as $post ) {
if ($this->IsPostPurchased ( $post->post_id, $userID )) {
$purchasedPostsList .= "
$post->post_title
";
}
}
}
$content = str_replace ( $this->purchased_posts_list_placeholder, $purchasedPostsList, $content );
}
return $content;
}
function post_filter($content) {
$start_delimiter = $this->start_delimiter;
$end_delimiter = $this->end_delimiter;
global $current_user, $user_ID, $post_ID, $post, $id;
$postID = $post_ID;
$userID = $user_ID;
if ($userID == 0) {
$userID = $current_user->id;
}
if ($postID == 0) {
$postID = $post->id;
}
if ($postID == 0) {
$postID = $id;
}
$isPostPurchased = $this->IsPostPurchased ( $postID, $userID );
$combination = ((! $this->IsGooglebot ()) && (($userID == 0) || (! $isPostPurchased)));
if ((! $this->IsGooglebot ()) && (($userID == 0) || (! $isPostPurchased))) {
$start = strpos ( $content, $start_delimiter );
$end = strpos ( $content, $end_delimiter );
$pre = substr ( $content, 0, $start );
$suf = substr ( $content, $end, strlen ( $content ) );
$delimiterRegex = "/\\[$this->delimiter_tag_name\\].*?\\[\/$this->delimiter_tag_name\\]/is";
if (! (($start === FALSE) && ($end === FALSE))) {
if ($userID == 0) {
$domain = $_SERVER ['HTTP_HOST'];
$url = "http://" . $domain . $_SERVER ['REQUEST_URI'];
$LoginButton = $this->show_login_button ( get_option ( 'siteurl' ) . "/wp-login.php?redirect_to=$url", __ ( "Log in" ) );
$content = preg_replace ( $delimiterRegex, $LoginButton, $content );
} else {
global $wpdb;
$sql = "SELECT posts.ID,posts.post_title, items.* FROM $wpdb->posts posts INNER JOIN $this->paied_items_table items ON items.post_id=posts.id WHERE posts.id='$postID'";
$items = $wpdb->get_results ( $sql );
if ($items) {
$item = $items [0];
$item_id = $item->ID;
$item_title = $item->post_title;
$item_amount = $item->amount;
$item_currency = $item->currency;
$item_name = $item->name;
$item_number = $item->number;
$item_expiration_unit = $item->expiration_unit;
$item_expire = $this->native_expiration_message ( $item->expire, $item_expiration_unit );
$postButton = $this->paypal_buy_now_form ( $item_name . ":" . $item_title . " " . $item_expire, $item_number, $item_amount, $item_currency, $postID, $userID, $item->expire, $item_expiration_unit );
}
$blog_item_amount = get_option ( $this->prefix . '_BlogAmount' );
if ($blog_item_amount) {
$blog_item_amount = get_option ( $this->prefix . '_BlogAmount' );
$blog_item_currency = get_option ( $this->prefix . '_BlogCurrency' );
$blog_item_name = get_option ( $this->prefix . '_BlogName' );
$blog_item_number = get_option ( $this->prefix . '_BlogNumber' );
$blog_item_expire = get_option ( $this->prefix . '_BlogExpire' );
$blog_item_expiration_unit = get_option ( $this->prefix . '_BlogExpirationUnits' );
$blog_item_expire = $this->native_expiration_message ( $blog_item_expire, $blog_item_expiration_unit );
$blogButton = $this->paypal_buy_now_form ( $blog_item_name . ":" . $blog_item_title . " " . $blog_item_expire, $blog_item_number, $blog_item_amount, $blog_item_currency, - 1, $userID, get_option ( $this->prefix . '_BlogExpire' ), $blog_item_expiration_unit ); //-1 post id for whole site
}
if ($blogButton || $postButton) {
$content = preg_replace ( $delimiterRegex, $postButton . $blogButton, $content );
}
}
}
}
$content = str_replace ( $start_delimiter, '', $content );
$content = str_replace ( $end_delimiter, '', $content );
return $content;
}
function translate_expiration_unit($item_expiration_unit) {
switch ($item_expiration_unit) {
case "D" :
return "days";
break;
case "W" :
return "weeks";
break;
case "M" :
return "months";
break;
case "Y" :
return "years";
break;
default :
return "days";
break;
}
}
function native_expiration_message($item_expire, $item_expiration_unit = "D") {
if ($item_expire) {
$item_expire = __ ( " Expires in " ) . $item_expire . " " . __ ( $this->translate_expiration_unit ( $item_expiration_unit ) );
} else {
$item_expire = "";
}
return $item_expire;
}
function Are_PayPal_Configuration() {
global $wpdb;
if (!$this->msg_writable_folders){
if (function_exists ( 'add_submenu_page' )) {
add_menu_page ( __ ( $this->prefix ), __ ( $this->prefix ), 10, __FILE__, array (&$this, 'Main_Configuration_Page' ), '/wp-content/plugins/are-paypal/images/icon_paypal_2Ps_16x14.gif' );
add_submenu_page ( __FILE__, __ ( $this->prefix . ' Configuration 2' ), __ ( 'Post Prices' ), 10, $this->prefix . '_PostSetup', array (&$this, 'Configure_Prices_For_Posts' ) );
add_submenu_page ( __FILE__, __ ( $this->prefix . ' Configuration 3' ), __ ( 'Paypal data' ), 10, $this->prefix . '_PaypalData', array (&$this, 'View_Payments' ) );
add_submenu_page ( __FILE__, __ ( $this->prefix . ' Configuration 4' ), __ ( 'Blog Price' ), 10, $this->prefix . '_BlogPrice', array (&$this, 'Blog_Price' ) );
add_submenu_page ( __FILE__, __ ( $this->prefix . ' Configuration 5' ), __ ( 'How to use' ), 10, $this->prefix . '_HowToUse', array (&$this, 'How_To_Use' ) );
add_submenu_page ( __FILE__, __ ( $this->prefix . ' Configuration 6' ), __ ( 'Donate' ), 10, $this->prefix . '_Donate', array (&$this, 'Donate' ) );
}
}else{
add_menu_page ( __ ( $this->prefix ), __ ( $this->prefix ), 10, __FILE__, array (&$this, 'Protected_Folders_Detected' ), '/wp-content/plugins/are-paypal/images/icon_paypal_2Ps_16x14.gif' );
}
}
function Protected_Folders_Detected(){
?>
Protected Folders Detected
msg_writable_folders);?>
delete_options("_InstantPaymentTemplate,_RecurentPaymentTemplate,_LoginButtonTemplate");
$this->set_templates ();
}
if (isset ( $_REQUEST ['submit'] )) {
$this->set_option("_TextToShowIfNotLogedIn","TextToShowIfNotLogedIn");
$this->set_option("_PayPal_Email","PayPal_Email");
$this->set_option("_test","test");
$this->set_option("_TextToShowIfNotPurchased","TextToShowIfNotPurchased");
$this->set_option("_InstantPaymentTemplate","InstantPaymentTemplate");
$this->set_option("_RecurentPaymentTemplate","RecurentPaymentTemplate");
$this->set_option("_LoginButtonTemplate","LoginButtonTemplate");
$this->set_option("_Suppress_Notification_Emails","Suppress_Notification_Emails");
$this->set_option("_Users_Library_Page","users_library_page");
$LastAction = __ ( "Updated successfully ..." );
}
$this->smarty->assign ( 'LastAction', $LastAction );
$this->smarty->assign ( 'PageHeader', __ ( $this->prefix . ' Configuration' ) );
$this->smarty->assign ( 'TextToShowIfNotLogedInLabel', __ ( 'Text to show if visitor is not logged in' ) );
$this->smarty->assign ( 'TextToShowIfNotLogedIn', stripslashes ( get_option ( $this->prefix . '_TextToShowIfNotLogedIn' ) ) );
$this->smarty->assign ( 'TextToShowIfNotPurchasedLabel', __ ( 'Text to show if content is not purchased by a visitor' ) );
$this->smarty->assign ( 'TextToShowIfNotPurchased', stripslashes ( get_option ( $this->prefix . '_TextToShowIfNotPurchased' ) ) );
$this->smarty->assign ( 'PayPalSandBoxMode', get_option ( $this->prefix . '_test' ) );
$this->smarty->assign ( 'UsePayPalSandBoxLinkText', __ ( 'Use PayPal Sandbox' ) );
$this->smarty->assign ( 'UsePayPalSandBoxNextToLinkText', __ ( '(testing only)' ) );
$this->smarty->assign ( 'Suppress_Notification_EmailsMode', get_option ( $this->prefix . '_Suppress_Notification_Emails' ) );
$this->smarty->assign ( 'Suppress_Notification_EmailsLabel', __ ( 'Suppress notification emails' ) );
$this->smarty->assign ( 'PayPal_EmailLabel', __ ( 'PayPal email' ) );
$this->smarty->assign ( 'PayPal_Email', stripslashes ( get_option ( $this->prefix . '_PayPal_Email' ) ) );
$this->smarty->assign ( 'InstantPaymentTemplateLabel', __ ( 'Instant Payment Button Template' ) );
$this->smarty->assign ( 'InstantPaymentTemplate', stripslashes ( get_option ( $this->prefix . '_InstantPaymentTemplate' ) ) );
$this->smarty->assign ( 'RecurentPaymentTemplateLabel', __ ( 'Recurent Payment Button Template' ) );
$this->smarty->assign ( 'RecurentPaymentTemplate', stripslashes ( get_option ( $this->prefix . '_RecurentPaymentTemplate' ) ) );
$this->smarty->assign ( 'LoginButtonTemplateLabel', __ ( 'Login Button Template' ) );
$this->smarty->assign ( 'LoginButtonTemplate', stripslashes ( get_option ( $this->prefix . '_LoginButtonTemplate' ) ) );
$this->smarty->assign ( 'users_library_pageLabel', __ ( 'Page or Post to show users purchased posts' ) );
$this->smarty->assign ( 'Users_Library_Page', get_option ( $this->prefix . '_Users_Library_Page', $_REQUEST ['users_library_page'] ) );
$this->smarty->assign ( 'UpdateOptions', __ ( 'Update Options»' ) );
$this->smarty->assign ( 'RestoreDefaults', __ ( 'Restore Template Defaults»' ) );
$all_posts = $wpdb->get_results ( "SELECT posts.* FROM $wpdb->posts posts WHERE 1=1 AND $this->post_type_clause AND posts.post_type='page'" );
$this->smarty->assign ( 'Posts', $all_posts );
$TemplateExamples = array ("InstantPayment" => $this->paypal_buy_now_form ( "Item Name", "Item Number", "1", "EUR", 0, 0, 0, "D" ), "RecurentPayment" => $this->paypal_buy_now_form ( "Item Name", "Item Number", "1", "EUR", 0, 0, 30, "D" ), "LoginButton" => $this->show_login_button ( "URL", "URLTEXT" ) );
$this->smarty->assign ( 'TemplateExamples', $TemplateExamples );
$this->smarty->display ( 'main_configuration_page.tpl' );
}
function Donate() {
check_admin_referer ();
$this->smarty->assign ( 'PageHeader', __ ( 'Donate' ) );
$this->smarty->assign ( 'DonateInfo', __ ( 'Click the button below to donate. Any amount is highly appreciated.' ) );
$this->smarty->display ( 'donate.tpl' );
}
function How_To_Use() {
check_admin_referer ();
$this->smarty->assign ( 'PageHeader', __ ( 'How to use' ) );
$this->smarty->assign ( 'StartDelimiter', $this->start_delimiter );
$this->smarty->assign ( 'EndDelimiter', $this->end_delimiter );
$this->smarty->assign ( 'PurchasedPostsListPlaceholder', $this->purchased_posts_list_placeholder );
$this->smarty->assign ( 'PayPalUrl', $this->paypal_url );
$this->smarty->display ( 'how_to_use.tpl' );
}
function delete_options($options){
$options=explode(",",$options);
foreach($options as $option){
delete_option ( $this->prefix . $option );
}
}
function set_option($option,$rqname){
$Value = $_REQUEST [$rqname];
update_option ( $this->prefix . $option, $Value );
}
function Blog_Price() {
check_admin_referer ();
$action = mysql_escape_string ( $_REQUEST ["action"] );
$post_id = mysql_escape_string ( $_REQUEST ["post_id"] );
$this->ManagePurchasers ( $action, $post_id );
if (isset ( $_REQUEST ['submit'] )) {
$this->set_option("_BlogAmount","amount");
$this->set_option("_BlogCurrency","currency");
$this->set_option("_BlogName","name");
$this->set_option("_BlogNumber","number");
$this->set_option("_BlogExpire","expire");
$this->set_option("_BlogExpirationUnits","expiration_units");
$LastAction = __ ( " Updated successfully ..." );
}
if (isset ( $_REQUEST ['clear'] )) {
$this->delete_options("_BlogAmount,_BlogCurrency,_BlogName,_BlogNumber,_BlogExpire,_BlogExpirationUnits");
$LastAction = __ ( " Deleted successfully ..." );
}
if (! $action || $action == "edit") {
$blog_price_data = array (
array (
"fieldname" => "Name",
"formfieldname" => "name",
"formfieldvalue" => get_option ( $this->prefix . '_BlogName' )
),
array (
"fieldname" => "Number",
"formfieldname" =>"number",
"formfieldvalue" => get_option ( $this->prefix . '_BlogNumber' )
),
array (
"fieldname" => "Price",
"formfieldname" =>"amount",
"formfieldvalue" => get_option ( $this->prefix . '_BlogAmount' )
),
array (
"fieldname" => "Currency",
"formfieldname" =>"currency",
"formfieldvalue" => get_option ( $this->prefix . '_BlogCurrency' )
),
array (
"fieldname" => "Expire",
"formfieldname" =>"expire",
"formfieldvalue" => get_option ( $this->prefix . '_BlogExpire' )
),
);
//SMARTY
$expiration_units_data = array (
"label" => __('Expiration units'),
"value" => get_option ( $this->prefix . '_BlogExpirationUnits' ),
"units" => explode(",","D,W,M,Y"),
"translated_units" => explode(",","Days,Weeks,Months,Years")
);
$this->smarty->assign ( 'LastAction', $LastAction );
$this->smarty->assign ( 'PageHeader', __ ('Edit Blog Price') );
$this->smarty->assign ( 'Page', $_REQUEST ["page"] );
$this->smarty->assign ( 'BlogPriceData', $blog_price_data );
$this->smarty->assign ( 'ExpirationUnitsData', $expiration_units_data);
$this->smarty->assign ( 'EditPurchasersLabel',__( 'Edit Purchasers' ));
$this->smarty->display ( 'blog_price.tpl' );
}
}
function View_Payments() {
global $wpdb;
check_admin_referer ();
$sql="
SELECT r.cnt,f.* FROM (select count(*) as cnt from $this->paypal_requests_table %WHERE%) r
CROSS JOIN $this->paypal_fields_table f %WHERE%";
if ($_REQUEST ["action"] == "details") {
$RequestID = $_REQUEST ["RequestID"];
$sql = str_ireplace("%WHERE%","WHERE RequestID='$RequestID'",$sql);
}else{
$sql = str_ireplace("%WHERE%","",$sql);
}
$requests = $wpdb->get_results ( $sql );
$data=array();
foreach($requests as $field){
$data[$field->RequestID][$field->Name] = array(
"value" => $field->Value,
"name" => $field->Name,
);
if ($field->Name == "custom"){
list ( $post_id, $user_id ) = explode ( "|", $field->Value );
$user_id = mysql_escape_string ( $user_id );
$post_id = mysql_escape_string ( $post_id );
$login = $wpdb->get_results ( "SELECT user_login FROM $wpdb->users where ID='$user_id'" );
$post = $wpdb->get_results ( "SELECT post_title FROM $wpdb->posts where ID='$post_id'" );
$data[$field->RequestID]["login"] = array(
"value" => $login[0]->user_login,
"name" => "login",
);
$data[$field->RequestID]["post_title"] = array(
"value" => $post[0]->post_title,
"name" => "post_title",
);
}
}
SmartyPaginate::connect();
SmartyPaginate::setLimit(25);
SmartyPaginate::setUrl('admin.php?page=Are_PayPal_PaypalData');
SmartyPaginate::setTotal(count($data));
$chunked_data = array_chunk($data,SmartyPaginate::getLimit(),true);
$pageNumber=SmartyPaginate::getCurrentIndex()/SmartyPaginate::getLimit();
$pageNumber=floor($pageNumber);
echo SmartyPaginate::getCurrentItem();
echo " / ";
echo SmartyPaginate::getLimit();
echo " = ";
echo $pageNumber;
$data=$chunked_data[$pageNumber];
$this->smarty->assign ( 'PageHeader', __ ('View Payments') );
$this->smarty->assign ( 'Data', $data);
SmartyPaginate::assign($this->smarty);
$this->smarty->display ( 'view_payments.tpl' );
}
function ManagePurchasers($action, $post_id) {
check_admin_referer ();
global $wpdb;
if ($_REQUEST ["PurchasersSubmit"]) {
if ($_REQUEST ["PurchasersSubmit"] == ">>") {
$item_purchaser = $_REQUEST ["available_users"];
$wpdb->query ( "INSERT INTO $this->paied_users_table(post_id,user_id) VALUES($post_id,$item_purchaser)" );
$LastAction = __ ( "Added successfully ..." );
} else {
$item_purchaser = $_REQUEST ["paied_users"];
$wpdb->query ( "DELETE FROM $this->paied_users_table WHERE post_id=$post_id AND user_id=$item_purchaser" );
$LastAction = __ ( "Removed successfully ..." );
}
}
if (($action == "purchasers") && ($post_id)) {
$items = $wpdb->get_results ( "SELECT posts.ID,posts.post_title FROM $wpdb->posts posts WHERE posts.id='$post_id'" );
if ($items || $post_id == - 1) {
if ($post_id == - 1) {
$item_id = - 1;
$item_title = "Entire blog ...";
} else {
$item = $items [0];
$item_id = $item->ID;
$item_title = $item->post_title;
}
$available_users_sql = "
SELECT DISTINCT
u.id,
user_login
FROM $wpdb->users u
WHERE u.id not in
(
SELECT user_id FROM $this->paied_users_table up
WHERE
post_id = $item_id
AND ($this->paid_user_id_sql_criteria)
)";
$paid_users_sql = "
SELECT DISTINCT
u.id,
user_login
FROM $wpdb->users u
INNER JOIN
$this->paied_users_table up ON up.user_id = u.id and up.post_id=$item_id
AND ($this->paid_user_id_sql_criteria)
";
$available_users = $wpdb->get_results ($available_users_sql);
echo("");
$paid_users = $wpdb->get_results ($paid_users_sql);
$this->smarty->assign ( 'LastAction', $LastAction );
$this->smarty->assign ( 'PageHeader', __ ('Post purchasers') );
$this->smarty->assign ( 'Page', $_REQUEST ["page"] );
$this->smarty->assign ( 'PostID', $post_id );
$this->smarty->assign ( 'PostLabel', __ ('Post') );
$this->smarty->assign ( 'PostTitle', $item_title );
$this->smarty->assign ( 'AvailableUsersLabel', __ ('Available users') );
$this->smarty->assign ( 'PaiedUsersLabel', __ ('Paied users') );
$this->smarty->assign ( 'AvailableUsers',$available_users);
$this->smarty->assign ( 'PaidUsers',$paid_users);
$this->smarty->display ( 'manage_purchasers.tpl' );
}
}
}
function Configure_Prices_For_Posts() {
check_admin_referer ();
global $wpdb;
### Get The Posts
$action = mysql_escape_string ( $_REQUEST ["action"] );
$post_id = mysql_escape_string ( $_REQUEST ["post_id"] );
if ($_REQUEST ["BonusSubmit"]) {
if ($_REQUEST ["BonusSubmit"] == ">>") {
$post2_id = $_REQUEST ["post_to_package"];
$wpdb->query ( "INSERT INTO $this->bonus_posts_table VALUES($post_id,$post2_id)" );
$wpdb->query ( "INSERT INTO $this->bonus_posts_table VALUES($post2_id,$post_id)" );
$LastAction = __ ( "Added successfully ..." );
} else {
$post2_id = $_REQUEST ["post_in_package"];
$wpdb->query ( "DELETE FROM $this->bonus_posts_table WHERE post1_id=$post_id AND post2_id=$post2_id" );
$wpdb->query ( "DELETE FROM $this->bonus_posts_table WHERE post1_id=$post2_id AND post2_id=$post_id" );
$LastAction = __ ( "Removed successfully ..." );
}
}
if (($action == "delete") && ($post_id)) {
$wpdb->query ( "DELETE FROM $this->paied_items_table WHERE post_id=$post_id" );
$wpdb->query ( "DELETE FROM $this->paied_users_table WHERE post_id=$post_id" );
$LastAction = __ ( "Deleted successfully ..." );
}
if (($action == "write") && ($post_id)) {
$items = $wpdb->get_results ( "SELECT posts.ID,posts.post_title, items.* FROM $wpdb->posts posts LEFT OUTER JOIN $this->paied_items_table items ON items.post_id=posts.id WHERE posts.id='$post_id'" );
$item = $items [0];
$item_id = $item->ID;
$item_amount = mysql_escape_string ( $_REQUEST ["amount"] );
$item_currency = mysql_escape_string ( $_REQUEST ["currency"] );
$item_name = mysql_escape_string ( $_REQUEST ["name"] );
$item_number = mysql_escape_string ( $_REQUEST ["number"] );
$item_expire = $_REQUEST ["expire"];
$item_expiration_units = $_REQUEST ["expiration_units"];
if ($item->post_id) {
//UPDATE
$sql = "UPDATE $this->paied_items_table SET name='$item_name',number='$item_number',amount='$item_amount',currency='$item_currency',expire='$item_expire', expiration_unit='$item_expiration_units' WHERE post_id=$item_id";
$LastAction = __ ( "Updated successfully ..." );
} else {
//INSERT
$sql = "INSERT INTO $this->paied_items_table (name,number,amount,currency, post_id,expire,expiration_unit) VALUES('$item_name','$item_number','$item_amount','$item_currency','$item_id','$item_expire','$item_expiration_units')";
$LastAction = __ ( "Inserted successfully ..." );
}
$wpdb->query ( $sql );
}
if (($action == "purchasers") && ($post_id)) {
$this->ManagePurchasers ( $action, $post_id );
} elseif (($action == "bonus") && ($post_id)) {
$items = $wpdb->get_results ( "SELECT posts.ID,posts.post_title, items.* FROM $wpdb->posts posts LEFT OUTER JOIN $this->paied_items_table items ON items.post_id=posts.id WHERE posts.id='$post_id' AND $this->post_type_clause" );
if ($items) {
$item = $items [0];
$item_id = $item->ID;
$item_title = $item->post_title;
$available_posts = $wpdb->get_results ( "SELECT posts.* FROM $wpdb->posts posts WHERE posts.id!=$item_id AND posts.post_content LIKE '%$this->start_delimiter%'AND $this->post_type_clause AND posts.id NOT IN(select post2_id from $this->bonus_posts_table WHERE post1_id='$item_id')" );
$bonus_posts = $wpdb->get_results ( "SELECT posts.* FROM $wpdb->posts posts WHERE posts.id!=$item_id AND posts.post_content LIKE '%$this->start_delimiter%' AND $this->post_type_clause AND posts.id IN(select post2_id from $this->bonus_posts_table WHERE post1_id='$item_id')" );
$this->smarty->assign ( 'LastAction', $LastAction );
$this->smarty->assign ( 'PageHeader', __ ('Post purchasers') );
$this->smarty->assign ( 'Page', $_REQUEST ["page"] );
$this->smarty->assign ( 'PostID', $item_id );
$this->smarty->assign ( 'PostLabel', __ ('Post') );
$this->smarty->assign ( 'PostTitle', $item_title );
$this->smarty->assign ( 'AvailablePostsLabel', __ ('Available posts') );
$this->smarty->assign ( 'BonusPostsLabel', __ ('Bonus posts') );
$this->smarty->assign ( 'AvailablePosts',$available_posts);
$this->smarty->assign ( 'BonusPosts',$bonus_posts);
$this->smarty->display ( 'bonus_posts.tpl' );
}
} else if (($action == "edit") && ($post_id)) {
$items = $wpdb->get_results ( "SELECT posts.ID,posts.post_title, items.* FROM $wpdb->posts posts LEFT OUTER JOIN $this->paied_items_table items ON items.post_id=posts.id WHERE posts.id='$post_id'" );
if ($items) {
$item = $items [0];
$item_id = $item->ID;
$post_price_data = array (
array (
"fieldname" => "ID",
"formfieldvalue" => $item->ID
),
array (
"fieldname" => "Post Title",
"formfieldvalue" => $item->post_title
),
array (
"fieldname" => "Name",
"formfieldname" => "name",
"formfieldvalue" => $item->name
),
array (
"fieldname" => "Number",
"formfieldname" =>"number",
"formfieldvalue" => $item->number
),
array (
"fieldname" => "Price",
"formfieldname" =>"amount",
"formfieldvalue" => $item->amount
),
array (
"fieldname" => "Currency",
"formfieldname" =>"currency",
"formfieldvalue" => $item->currency
),
array (
"fieldname" => "Expire",
"formfieldname" =>"expire",
"formfieldvalue" => $item->expire
),
);
$expiration_units_data = array (
"label" => __('Expiration units'),
"value" => $item->expiration_unit,
"units" => explode(",","D,W,M,Y"),
"translated_units" => explode(",","Days,Weeks,Months,Years")
);
$post_purchasers_sql = "
SELECT
user_login
FROM $wpdb->users u
INNER JOIN $this->paied_users_table up ON up.user_id=u.id and up.post_id=$item_id
AND ($this->paid_user_id_sql_criteria)
";
$post_purchasers = $wpdb->get_results ($post_purchasers_sql);
$this->smarty->assign ( 'Mode', "post" );
$this->smarty->assign ( 'LastAction', $LastAction );
$this->smarty->assign ( 'PageHeader', __ ('Edit Post Price') );
$this->smarty->assign ( 'Page', $_REQUEST ["page"] );
$this->smarty->assign ( 'PostID', $item_id );
$this->smarty->assign ( 'PostPriceData', $post_price_data );
$this->smarty->assign ( 'ExpirationUnitsData', $expiration_units_data);
$this->smarty->assign ( 'PurchasersLabel', __ ( 'Purchasers' ));
$this->smarty->assign ( 'Purchasers',$post_purchasers);
$this->smarty->assign ( 'EditPurchasersLabel', __ ( 'Edit purchasers' ));
$this->smarty->display ( 'post_price.tpl' );
}
} else {
$sql = "SELECT posts.ID,posts.post_title, items.* FROM $wpdb->posts posts LEFT OUTER JOIN $this->paied_items_table items ON items.post_id=posts.id WHERE posts.post_content LIKE '%$this->start_delimiter%' AND $this->post_type_clause";
$Data = $wpdb->get_results ( $sql );
$Purchasers = array();
foreach($Data as $item){
$purchasers_sql = "
SELECT user_login FROM $wpdb->users u INNER JOIN $this->paied_users_table up ON up.user_id=u.id and up.post_id=$item->ID
AND ($this->paid_user_id_sql_criteria)
";
$Purchasers[]=$wpdb->get_results ( $purchasers_sql );
}
$field_names = explode(",","ID,Post Title,Purchasers,Name,Number,Price,Currency,Expire,Expiration units");
$this->smarty->assign ( 'LastAction', $LastAction );
$this->smarty->assign ( 'PageHeader', __ ('Post Prices') );
$this->smarty->assign ( 'FieldNames', $field_names );
$this->smarty->assign ( 'Data', $Data );
$this->smarty->assign ( 'Purchasers', $Purchasers );
$this->smarty->display ( 'view_prices.tpl' );
}
}
function IsGooglebot() {
// check if user agent contains googlebot
if (eregi ( "Googlebot", $_SERVER ['HTTP_USER_AGENT'] )) {
$ip = $_SERVER ['REMOTE_ADDR'];
//server name e.g. crawl-66-249-66-1.googlebot.com
$name = gethostbyaddr ( $ip );
//check if name ciontains googlebot
if (eregi ( "Googlebot", $name )) {
//list of IP's
$hosts = gethostbynamel ( $name );
foreach ( $hosts as $host ) {
if ($host == $ip) {
return true;
}
}
return false; // Pretender, take some action if needed
} else {
return false; // Pretender, take some action if needed
}
} else {
// Not googlebot, take some action if needed
}
return false;
}
function IsPostPurchased($post_id, $user_id) {
$user_id = mysql_escape_string ( $user_id );
$post_id = mysql_escape_string ( $post_id );
if ($this->IsPostPurchasedSql ( "-1", $user_id )) {
return true;
}
if ($this->IsPostPurchasedSql ( $post_id, $user_id )) {
return true;
}
return false;
}
function IsPostPurchasedSql($post_id, $user_id) {
global $wpdb;
$sql = "
SELECT up.* FROM $this->paied_users_table up WHERE (up.post_id='$post_id') AND up.user_id='$user_id'
AND ($this->paid_user_id_sql_criteria)";
$users = $wpdb->get_results ( $sql );
if ($users) {
return true;
}
return false;
}
function paypal_buy_now_form($item_name, $item_number, $item_price, $item_currency, $post_id, $user_id, $item_expiration, $item_expiration_unit) {
$paypal_url = $this->paypal_url;
$paypal_email = $this->paypal_email;
$item_custom = $post_id . "|" . $user_id;
$domain = $_SERVER ['HTTP_HOST'];
$item_return = "http://" . $domain . $_SERVER ['REQUEST_URI'];
$textExplanation = stripslashes ( get_option ( $this->prefix . '_TextToShowIfNotPurchased' ) );
$templateName = $this->prefix . "_RecurentPaymentTemplate";
if (! $item_expiration) {
$templateName = $this->prefix . "_InstantPaymentTemplate";
}
$result = stripslashes ( get_option ( $templateName ) );
$result = str_replace ( '%EXPLANATION%', $textExplanation, $result );
$result = str_replace ( '%PAYPALURL%', $paypal_url, $result );
$result = str_replace ( '%ITEMNAME%', $item_name, $result );
$result = str_replace ( '%ITEMNUMBER%', $item_number, $result );
$result = str_replace ( '%ITEMPRICE%', $item_price, $result );
$result = str_replace ( '%ITEMCURRENCY%', $item_currency, $result );
$result = str_replace ( '%BONUSLIST%', $this->BonusPostsList ( $post_id ), $result );
$result = str_replace ( '%PAYPALEMAIL%', $paypal_email, $result );
$result = str_replace ( '%ITEMRETURN%', $item_return, $result );
$result = str_replace ( '%ITEMCUSTOM%', $item_custom, $result );
$result = str_replace ( '%EXPIRATION%', $item_expiration, $result );
$result = str_replace ( '%EXPIRATIONUNITS%', $item_expiration_unit, $result );
$result = str_replace ( '%BUTTONALT%', '', $result );
return $result;
}
function BonusPostsList($post_id) {
global $wpdb;
$post_id = mysql_escape_string ( $post_id );
$sql = "select posts.post_title,posts.id from $this->bonus_posts_table bonuses INNER JOIN $wpdb->posts posts ON posts.id=bonuses.post2_id WHERE bonuses.post1_id='$post_id'";
$bonuses = $wpdb->get_results ( $sql );
$result = "";
if ($bonuses) {
$result .= "" . __ ( "Purchasing current content you will also get access for posts below" ) . "
";
}
return $result;
}
function add_html_headers() {
echo ('');
echo ('');
}
}
}
//instantiate the class
if (class_exists ( 'Are_PayPal' )) {
$Are_PayPal = new Are_PayPal ( );
}
?>