ADimeEveryTime", 1, 'a-dime-every-time/admin.php', 'bachbill_general_options', plugins_url('a-dime-every-time/img/dime_logo_15x15.png'));
add_submenu_page('a-dime-every-time/admin.php', __('Settings', 'bachbill'), __('Settings', 'bachbill'), 1, 'a-dime-every-time/admin.php','bachbill_general_options');
add_submenu_page('a-dime-every-time/admin.php', __('Reporting', 'bachbill'), __('Reporting', 'bachbill'), 1, __('reporting', 'bachbill'), 'bachbill_reporting');
$accountSetup=get_option('bachbill_account_setup');
if ($accountSetup && $accountSetup=='ready'){
add_submenu_page('a-dime-every-time/admin.php', __('Payments', 'bachbill'), __('Payments', 'bachbill'), 1, __('Payments', 'bachbill'), 'bachbill_payments');
}
}
function rewriteLinks($post, $content){
$links=get_post_meta($post->ID, 'bachbill_protected_links', '');
if (!is_array($links)){
return;
}
$links=$links[0];
if ($links){
$root=getRootUrl();
foreach ($links as $link){
$index=strrpos($link, '.');
$name=md5($link);
$ext='';
if ($index>0){
$ext=substr($link, $index);
}
$fName=$name.$ext;
$fullName=$root.'/wp-content/plugins/a-dime-every-time/media.php?file='.$fName.'&postId='.$post->ID;
update_option('bachbill_protected_'.$post->ID.'_'.$fName, $link);
$content=str_replace($link, $fullName, $content);
}
}
return $content;
}
function intercept($content){
global $post;
$isChargeable=get_post_meta($post->ID, 'bachbill_chargeable', true);
if (!$isChargeable){
// see if any category is chargeable
$categories=wp_get_post_categories($post->ID);
foreach ($categories as $cat){
$isChargeable=get_option('bachbill_cat_'.$cat.'_chargeable');
if ($isChargeable){
break;
}
}
}
if (!$isChargeable){
return $content;
}
$content=rewriteLinks($post, $content);
$serviceId='s_'.$post->ID;
$user=wp_get_current_user();
$role=$user->roles[0];
if (is_user_logged_in() && $role!='subscriber'){
return $content;
}
if ( !is_user_logged_in() ){
return $post->post_excerpt.' '.__('You are not logged in to see this content', 'bachbill').'. '.__('log in', 'bachbill').'';
}else {
if ($serviceId){
@session_start();
$userServices=$_SESSION['bachbill_userServices'];
if (!isset($userServices)){
$userServices=array();
$_SESSION['bachbill_userServices']=$userServices;
}
$service=$userServices[$serviceId];
if ($service){
return $content;
}else {
// try to authorize the user
// concurrence control. Some themes ask for the_content more than once and we don't want that
@session_start();
//!!! disable $concurrenceTime=$_SESSION['bachbill_concurrence_thecontent'];
//!!! disable $_SESSION['bachbill_concurrence_thecontent']=microtime(true);
//!!! disable if ($concurrenceTime>0 && microtime(true)-$concurrenceTime<0.7){
//!!! disable return '';//.(microtime(true)-$concurrenceTime);
//!!! disable }
$nUsages=get_post_meta($post->ID, 'bachbill_n_usages', '1');
$nUsages=$nUsages?$nUsages:1;
if (is_single() || is_page()){
$api=getBachbillApi();
$priceplanId=get_option('bachbill_priceplanId');
$endUserAreaId=get_option('bachbill_endUserAreaId');
$api->setMethod("get");
$res=$api->authorize($priceplanId, $endUserAreaId, getUserId(), $serviceId, $nUsages);
if ($api->hasErrors()){
return $api->getErrorMessage();
}
if (!$res){
return __('There was an error while trying to authorize the user to use the content', 'bachbill');
}
if ($res['error']){
echo $res['error']['message'];
}else {
$res=$res['AuthorizeResponse'];
$transaction=$res['transaction'];
if ($res['code']==0){
if ($transaction){
if ($transaction['captureDate']){
// transaction is captured so user is authorized permanently. Otherwise it wouldn't be reported and only the subscription object would be there
$service=new BachbillService($serviceId);
$userServices[$serviceId]=$service;
$_SESSION['bachbill_userServices']=$userServices;
}else {
// transaction is ongoing and not captured yet
}
return $content;
}
// if the subscription is credit based, show splash page
$pendingAccesses=$res['subscriptions']['Subscription']['pendingAccesses'];
if (!$_GET['confirmUsageSplash']=='true' && $pendingAccesses>0){
$finalUrl=get_permalink( $post->ID );
$position=strpos($finalUrl, '?');
if ($position){
$finalUrl.='&confirmUsageSplash=true';
}else{
$finalUrl.='?confirmUsageSplash=true';
}
// $finalUrl=strpos($finalUrl, '?')>=0?($finalUrl+'&confirmUsageSplash'):($finalUrl+'?confirmUsageSplash');
return
'
'.
__('You have', 'bachbill').
' '.$pendingAccesses.' '.
__('credits left in your subscription.', 'bachbill').
'
'.
__('Please hit', 'bachbill').
' '.
__('Continue', 'bachbill').
''.
' '.
__('to access the content and discount the credit from your balance.', 'bachbill');
}else {
$subscriptionId=$res['subscriptions']['Subscription']['id'];
$res=$api->usage($priceplanId, $endUserAreaId, getUserId(), $subscriptionId, $serviceId, $nUsages);
$service=new BachbillService($serviceId);
$userServices[$serviceId]=$service;
$_SESSION['bachbill_userServices']=$userServices;
return $content;
}
}else {
if (!$res){
return __('There was an error while trying to authorize the user to use the content', 'bachbill');
}
@session_start();
$_SESSION['bachbill_redirect_after_purchase']=get_permalink( $post->ID );
return $post->post_excerpt.' '.renderPurchaseExperience($res['bundles']);
}
return 'Lets see...';
}
}else {
$glue='';
if ($post->post_excerpt){
$glue='