/**
 * the meta box for post/page purchase
 *
 */
function mgm_post_setup_meta_box_form($post)
{
    // get object
    $post_obj = mgm_get_post($post->ID);
    $datepickerformat = mgm_get_datepicker_format();
    // set default price
    if ($post_obj->purchase_cost == 0) {
        if (mgm_get_module('mgm_paypal', 'payment')->setting['purchase_price']) {
            $post_obj->purchase_cost = mgm_get_module('mgm_paypal', 'payment')->setting['purchase_price'];
        } else {
            $post_obj->purchase_cost = mgm_get_class('system')->setting['post_purchase_price'];
        }
    }
    // protect
    $protect_content = mgm_protect_content();
    //issue#: 414(changed id submitpost => submitpost_member for the below div )
    ?>

	<div class="submitbox" id="submitpost_member">	
		<div class="misc-pub-section">
			<p id="howto">
				<?php 
    _e('Select which membership types will have access to read this post/page.', 'mgm');
    ?>

				<?php 
    _e('Note: The private parts of the post should be inside the following tags: <strong>[private]</strong> <em>your text</em> <strong>[/private]</strong>', 'mgm');
    ?>

			</p>
			<p>
				<div class="mgm_post_setup_meta_box_div">
					<input type="checkbox" name="check_all" value="mgm_post[access_membership_types][]" /> <span><?php 
    _e('Select all', 'mgm');
    ?>
</span>
				</div>
			</p>
			<p>
				<?php 
    echo mgm_make_checkbox_group('mgm_post[access_membership_types][]', mgm_get_class('membership_types')->get_membership_types(), $post_obj->access_membership_types, MGM_KEY_VALUE);
    ?>
				
			</p>
			<?php 
    if ($protect_content == false) {
        ?>

			<div class="information mgm_width_230px"><?php 
        echo sprintf(__('<a href="%s">Content Protection</a> is <b>%s</b>. Make sure its enabled to Protect Post/Page.', 'mgm'), 'admin.php?page=mgm/admin', $protect_content ? 'enabled' : 'disabled');
        ?>
</div>			
			<?php 
    }
    ?>

		</div>	
		
		<div class="misc-pub-section">
			<b><?php 
    _e('Pay Per Post', 'mgm');
    ?>
:</b>
			<a href="#payperpost" class="mgm-toggle"><?php 
    _e('Edit');
    ?>
</a>
			<div id="payperpostdiv" class="hide-if-js">
				<div class="mgm_padding_5px">				
					<p class="postpurhase-heading"><?php 
    _e('Purchasable Settings', 'mgm');
    ?>
:</p>
					<ul class="mgm_post_setup_meta_box_ul">
						<li>
							<label><?php 
    _e('If the user doesn\'t have access, is this post/page available to buy?', 'mgm');
    ?>
</label><br/>	
							<input type="radio" class="radio" name="mgm_post[purchasable]" value='N' <?php 
    mgm_check_if_match('N', $post_obj->purchasable);
    ?>
/>
							<label><?php 
    _e('No', 'mgm');
    ?>
</label>
							<input type="radio" class="radio" name="mgm_post[purchasable]" value='Y' <?php 
    mgm_check_if_match('Y', $post_obj->purchasable);
    ?>
/> 
							<label><?php 
    _e('Yes', 'mgm');
    ?>
</label>
						</li>
						<li>
							<label><?php 
    _e('Cost of Post?', 'mgm');
    ?>
 </label><br>
							<input type="text" name="mgm_post[purchase_cost]" class="mgm_width_55px" value="<?php 
    echo $post_obj->purchase_cost;
    ?>
"/> <?php 
    echo mgm_get_setting('currency');
    ?>
							
						</li>
						<li>
							<label><?php 
    _e('The date that the ability to buy this page/post expires (Leave blank for indefinate).', 'mgm');
    ?>
</label><br />
							<input type="text" name="mgm_post[purchase_expiry]" class="date_input mgm_width_100px" value="<?php 
    echo intval($post_obj->purchase_expiry) > 0 ? date(MGM_DATE_FORMAT_INPUT, strtotime($post_obj->purchase_expiry)) : '';
    ?>
"/>
							<span class="mgm_font_size_8px">(<?php 
    echo $datepickerformat;
    ?>
)</span>							
						</li>
						<li>
							<label><?php 
    _e('The number of days that the buyer will have access for (0 for indefinate).', 'mgm');
    ?>
</label><br />
							<input type="text" name="mgm_post[access_duration]" class="mgm_width_50px" value="<?php 
    echo $post_obj->get_access_duration();
    ?>
"/>
						</li>
						<li>
							<label><?php 
    _e('The number of times that the buyer will have access for, "PAY PER VIEW" (0 for unlimited views).', 'mgm');
    ?>
</label><br />
							<input type="text" name="mgm_post[access_view_limit]" class="mgm_width_50px" value="<?php 
    echo $post_obj->get_access_view_limit();
    ?>
"/>
						</li>	
					</ul>
					
					<?php 
    if ($addons = mgm_get_all_addon_combo()) {
        ?>

					<p class="postpurhase-heading"><?php 
        _e('Addon Settings', 'mgm');
        ?>
:</p>
					<ul class="mgm_post_setup_meta_box_ul">		
						<li>
							<label><?php 
        _e('Allow Addons?', 'mgm');
        ?>
</label>	<br />
							<select name="mgm_post[addons][]" class="mgm_width_50px">
								<option value="">-</option>
								<?php 
        echo mgm_make_combo_options($addons, $post_obj->get_addons(), MGM_KEY_VALUE);
        ?>

							</select>							
						</li>					
					</ul>	
					<?php 
    }
    ?>


					<p class="postpurhase-heading"><?php 
    _e('Payment Settings', 'mgm');
    ?>
:</p>	
					<p class="fontweightbold"><?php 
    _e('Allow Modules', 'mgm');
    ?>
:</p>
					<?php 
    if ($payment_modules = mgm_get_class('system')->get_active_modules('payment')) {
        $modue_i = 0;
        foreach ($payment_modules as $payment_module) {
            if (!in_array($payment_module, array('mgm_trial'))) {
                ?>

					<input type="checkbox" name="mgm_post[allowed_modules][<?php 
                echo $modue_i;
                ?>
]" value="<?php 
                echo $payment_module;
                ?>
" <?php 
                echo in_array($payment_module, $post_obj->get_allowed_modules()) ? 'checked' : '';
                ?>
/> 
					<label><?php 
                echo mgm_get_module($payment_module)->name;
                ?>
</label><br/>
					<?php 
                $modue_i++;
            }
        }
    } else {
        ?>
				
					<b class="mgm_color_red"><?php 
        _e('No payment module is active.', 'mgm');
        ?>
</b>		
					<?php 
    }
    ?>

					<?php 
    // init
    $payment_settings = '';
    // product id mapping
    if ($payment_modules) {
        foreach ($payment_modules as $payment_module) {
            if ($module = mgm_is_valid_module($payment_module, 'payment', 'object')) {
                if ($module->has_product_map()) {
                    $payment_settings .= $module->settings_post_purchase($post_obj);
                }
            }
        }
    }
    // print
    if (!empty($payment_settings)) {
        echo $payment_settings;
    }
    ?>

					
					<?php 
    do_action('mgm_widget_payperpost_options', $post->ID);
    ?>
												
				</div>	
			</div>
		</div>			
		<div class="misc-pub-section misc-pub-section-last">
			<b><?php 
    _e('Post Delay (sequential posts)', 'mgm');
    ?>
:</b>
			<a href="#postdelay" class="mgm-toggle"><?php 
    _e('Edit');
    ?>
</a>
			<div id="postdelaydiv" class="hide-if-js">
				<div class="mgm_padding_5px">				
					<p id="howto"><?php 
    _e('How long should the user have been a member to see this content?', 'mgm');
    ?>
</p>
					<div class="div_table mgm_width_100pr">
					<?php 
    foreach (mgm_get_class('membership_types')->membership_types as $type_code => $type_name) {
        $val = isset($post_obj->access_delay[$type_code]) ? (int) $post_obj->access_delay[$type_code] : 0;
        ?>

						<div class="row">
							<div class="cell mgm_width_100px mgm_font_size_11px"><?php 
        echo $type_name;
        ?>
</div>
							<div class="cell mgm_font_size_11px">
								<input type="text" name="mgm_post[access_delay][<?php 
        echo $type_code;
        ?>
]" value="<?php 
        echo $val;
        ?>
" class="mgm_width_50px"/> Day(s)
							</div>
						</div>
					<?php 
    }
    ?>

					</div>		
				</div>	
			</div>
		</div>				
	</div>	
	
	<script language="javascript">
		jQuery(document).ready(function(){			
			jQuery('.mgm-toggle').bind('click', function(){
				if(jQuery(this).html() == '<?php 
    _e('Edit', 'mgm');
    ?>
'){
					jQuery(jQuery(this).attr('href')+'div').slideDown();
					jQuery(this).html('<?php 
    _e('Close', 'mgm');
    ?>
')
				}else{
					jQuery(jQuery(this).attr('href')+'div').slideUp();
					jQuery(this).html('<?php 
    _e('Edit', 'mgm');
    ?>
')
				}
			});
			// check bind
			jQuery("#submitpost_member :checkbox[name='check_all']").bind('click',function(){
				// check
				jQuery("#submitpost_member :checkbox[name='"+jQuery(this).val()+"']").attr('checked', (jQuery(this).attr('checked')=='checked') );
				// label
				if(jQuery(this).attr('checked')){
					jQuery(this).next().html('<?php 
    _e('Deselect all', 'mgm');
    ?>
');
				}else{
					jQuery(this).next().html('<?php 
    _e('Select all', 'mgm');
    ?>
');
				}
			});	

			// bind module allow
			jQuery(":checkbox[name^='mgm_post[allowed_modules]']").bind('click',function() {		
				var _m = jQuery(this).val().replace('mgm_', '');
				
				if(jQuery(this).attr('checked')){				
					jQuery('#settings_postpurchase_package_' + _m).slideDown('slow');
				}else{				
					jQuery('#settings_postpurchase_package_' + _m).slideUp('slow');
				}
			});
			// date		
			try{	
				mgm_date_picker('.date_input', false, {yearRange:"<?php 
    echo mgm_get_calendar_year_range();
    ?>
", dateFormat: "<?php 
    echo $datepickerformat;
    ?>
"});
			}catch(ex){}	
		});
	</script>
	<?php 
}
/**
 * parse shortcodes
 *
 * @param array @args
 * @param string @content
 * @param string @tag
 * @return string $content
 */
function mgm_shortcode_parse($args, $content, $tag)
{
    // current_user
    $current_user = wp_get_current_user();
    // system
    $system_obj = mgm_get_class('system');
    // issue#: 859
    // add <p> to the beggining and </p> to the end of content
    // as WP pass $content with incomplete p tags
    $content = '<p>' . $content . '</p>';
    // remove any '<p></p> found
    $content = str_replace(array('<p></p>'), '', $content);
    // @todo test with force_balance_tags();
    // tag block
    switch ($tag) {
        case 'private':
            // [private] protected content [/private]
            if (mgm_protect_content() || mgm_post_is_purchasable()) {
                //issue #1687
                if (mgm_content_post_access_delay($args)) {
                    $content = mgm_replace_postdealy_content($content);
                } else {
                    $content = mgm_replace_content_tags($tag, $content, $args);
                }
            }
            break;
        case 'private_or':
            // [private_or#member] protected content [/private_or]
            // [private_or membership_type="member"] protected content [/private_or]
            $membership_type = isset($args['membership_type']) ? $args['membership_type'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($membership_type) {
                $content = mgm_replace_content_tags($tag, $content, $membership_type);
            }
            break;
        case 'private_and':
            // [private_and#member] protected content [/private_and]
            // [private_and membership_type="member"] protected content [/private_and]
            $membership_type = isset($args['membership_type']) ? $args['membership_type'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($membership_type) {
                $content = mgm_replace_content_tags($tag, $content, $membership_type);
            }
            break;
        case 'payperpost_pack':
            // [payperpost_pack#1] : 1 = pack_id, packs to be created in MGM -> PayPerPost -> Post Packs, use the id here
            // [payperpost_pack id=1] : 1 = pack_id
            $pack_id = isset($args['id']) ? $args['id'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($pack_id) {
                $content = mgm_replace_content_tags($tag, $content, $pack_id);
            }
            break;
        case 'payperpost':
            // [payperpost#1] : 1 = post_id
            // [payperpost id=1] : 1 = post_id
            $pack_id = isset($args['id']) ? $args['id'] : str_replace('#', '', mgm_array_shift($args));
            // match
            if ($pack_id) {
                $content = mgm_replace_content_tags($tag, $content, $pack_id);
            }
            break;
        case 'subscription_packs':
            // subscription packs / payment gateways
            $content = mgm_sidebar_register_links($current_user->user_login, true, 'page');
            // @todo test
            break;
        case 'user_unsubscribe':
            // user unsubscribe
            $content = mgm_user_unsubscribe_info(null, $args);
            // view current user
            break;
        case 'user_other_subscriptions':
            // other subscriptions
            $content = mgm_user_other_subscriptions_info();
            break;
        case 'membership_details':
            // user subscription
            $content = mgm_membership_details();
            // view current user
            break;
        case 'user_upgrade':
            // user upgrade membership
            $content = mgm_get_upgrade_buttons($args);
            break;
        case 'user_purchase_another_membership':
            // purchase another subscription
            $content = mgm_get_purchase_another_subscription_button($args);
            break;
        case 'user_subscribe':
        case 'user_register':
            // named
            if ($method = mgm_get_var('method', '', true)) {
                // method
                switch ($method) {
                    case 'login':
                        $content = mgm_user_login_form(false);
                        break;
                    case 'lostpassword':
                        $content = mgm_user_lostpassword_form(false);
                        break;
                    default:
                        if (preg_match('/^payment/', $method)) {
                            $content = mgm_transactions_page($args);
                        }
                        break;
                }
            } else {
                $content = mgm_user_register_form($args);
            }
            break;
        case 'user_profile':
            // user profile
            $content = mgm_user_profile_form(NULL, false, $args);
            // view
            break;
        case 'user_public_profile':
            // user profile
            $content = mgm_user_public_profile($args);
            // view
            break;
        case 'transactions':
            // user payments/transactions
            $content = mgm_transactions_page($args);
            break;
        case 'user_contents_by_membership':
            // user contents by membership level
            $content = mgm_membership_content_page();
            break;
        case 'user_lostpassword':
            // user lost password form
            $content = mgm_user_lostpassword_form(false);
            break;
        case 'user_login':
            // user login form
            $content = mgm_user_login_form(false);
            break;
        case 'user_field':
            // user field
            $content = __('Experimental', 'mgm');
            break;
        case 'membership_contents':
            // membership contents
            $content = mgm_membership_contents();
            // view current user
            break;
        case 'logout_link':
            // custom logout link
            // [logout_link#Logout]
            // [logout_link label="Logout"]
            $label = isset($args['label']) ? $args['label'] : str_replace('#', '', mgm_array_shift($args));
            // match
            $content = mgm_logout_link($label);
            break;
        case 'membership_extend_link':
            //INCOMPLETE
            // membership extend link
            // [membership_extend_link#Extend]
            // [membership_extend_link label="Extend"]
            $label = isset($args['label']) ? $args['label'] : str_replace('#', '', mgm_array_shift($args));
            // match
            $content = mgm_membership_extend_link($label);
            break;
        case 'download_error':
            // content
            $content = isset($_GET['error_code']) ? mgm_download_error($_GET['error_code']) : '';
            break;
        case 'user_payment_history':
            // content
            $content = mgm_user_payment_history();
            // view current user
            break;
        case 'user_list':
            // content
            $content = mgm_generate_member_list($args);
            break;
        case 'user_facebook_login':
            // content
            $content = mgm_generate_facebook_login();
            break;
        case 'user_facebook_registration':
            // content
            $content = mgm_generate_facebook_registration();
            break;
        case 'user_purchased_contents':
            // content
            $content = mgm_generate_purchased_contents();
            break;
        case 'user_purchasable_contents':
            // content
            $content = mgm_generate_purchasable_contents();
            break;
            /*case 'addon':
            			// content
             			$content = mgm_purchase_addons($args);
            		break;*/
        /*case 'addon':
        			// content
         			$content = mgm_purchase_addons($args);
        		break;*/
        default:
            // default, which are not shortcode but content tags
            $args = str_replace('#', '', mgm_array_shift($args));
            // match
            $content = mgm_replace_content_tags($tag, $content, $args);
            break;
    }
    // return
    return $content;
}
		<div class="row">
			<div class="cell height10px">		
				<div class="tips"><?php 
_e('<b>Available Wildcards:</b> All Sub pages - [URL]<b>:any</b> OR [URL]<b>*</b>', 'mgm');
?>
</div>
			</div>
		</div>
		<?php 
if (mgm_protect_content() == false) {
    ?>

		<div class="row">
			<div class="cell height10px">
				<div class="information"><?php 
    echo sprintf(__('<a href="%s">Content Protection</a> is <b>%s</b>. Make sure its enabled to Protect Post/Page(s).', 'mgm'), 'javascript:mgm_set_tab_url(2,0)', mgm_protect_content() ? 'enabled' : 'disabled');
    ?>
</div>	
			</div>
		</div>			
		<?php 
}
?>

		<div class="row">&nbsp;</div>
		<div class="row">
			<div class="cell height10px">
				<input type="checkbox" name="add_private_tags" 	value="Y" <?php 
if (mgm_get_class('system')->setting['add_private_tags'] == "Y") {
    echo "checked='checked'";
}
 function posts()
 {
     global $wpdb;
     // local
     extract($_POST);
     // update
     if (isset($post_setup_save) && !empty($post_setup_save)) {
         // init updatd
         $updated = 0;
         // get system object
         $system_obj = mgm_get_class('system');
         //$setting['enable_facebook']= $system_obj->setting['enable_facebook'];
         // content protection
         $content_protection = $system_obj->setting['content_protection'];
         //Issue #720
         if (isset($add_private_tags)) {
             if ($add_private_tags == 'Y') {
                 $system_obj->setting['add_private_tags'] = 'Y';
                 $system_obj->save();
             }
         } else {
             $system_obj->setting['add_private_tags'] = 'N';
             $system_obj->save();
         }
         // membership types
         if (is_array($access_membership_types)) {
             $membership_types = json_encode($access_membership_types);
         } else {
             $membership_types = json_encode(array());
         }
         // init posts
         $wp_posts = array();
         // posts
         if (isset($posts)) {
             $wp_posts = array_merge($wp_posts, $posts);
         }
         // pages
         if (isset($pages)) {
             $wp_posts = array_merge($wp_posts, $pages);
         }
         // custom post types
         if (isset($custom_post_types)) {
             $wp_posts = array_merge($wp_posts, $custom_post_types);
         }
         // add direct urls
         if ($direct_urls) {
             // loop
             foreach ($direct_urls as $direct_url_id => $direct_url) {
                 // affected
                 $affected = false;
                 // insert
                 if (!empty($direct_url)) {
                     // check duplicate
                     if (!mgm_is_duplicate(TBL_MGM_POST_PROTECTED_URL, array('url'), '', array('url' => $direct_url))) {
                         // add
                         $affected = $wpdb->insert(TBL_MGM_POST_PROTECTED_URL, array('url' => $direct_url, 'membership_types' => $membership_types));
                     }
                 }
                 // update counter
                 if ($affected) {
                     $updated++;
                 }
             }
         }
         // check
         if ($wp_posts) {
             // loop
             foreach ($wp_posts as $post_id) {
                 // get object
                 $post_obj = mgm_get_post($post_id);
                 //Issue #838
                 if (isset($purchasable) == 'Y') {
                     // set
                     $post_obj->purchasable = $purchasable;
                 }
                 // check
                 if (isset($purchase_cost) && isset($purchasable) == 'Y') {
                     // check
                     if (!empty($purchase_cost) && $purchase_cost > 0) {
                         // set
                         $post_obj->purchase_cost = $purchase_cost;
                     }
                 }
                 // apply filter
                 $post_obj = apply_filters('mgm_post_update', $post_obj, $post_id);
                 // save meta
                 $post_obj->save();
                 // if access set
                 if (is_array($access_membership_types)) {
                     // set
                     $post_obj->access_membership_types = $access_membership_types;
                     // apply filter
                     $post_obj = apply_filters('mgm_post_update', $post_obj, $post_id);
                     // save meta
                     $post_obj->save();
                     // unset
                     unset($post_obj);
                     // check duplicate
                     if (!mgm_is_duplicate(TBL_MGM_POST_PROTECTED_URL, array('post_id'), '', array('post_id' => $post_id))) {
                         // add
                         $affected = $wpdb->insert(TBL_MGM_POST_PROTECTED_URL, array('url' => get_permalink($post_id), 'post_id' => $post_id, 'membership_types' => $membership_types));
                     } else {
                         $affected = $wpdb->update(TBL_MGM_POST_PROTECTED_URL, array('membership_types' => $membership_types), array('post_id' => $post_id));
                     }
                 }
                 // make private, add [private] tag
                 if (mgm_protect_content($content_protection)) {
                     // get post
                     $wp_post = wp_get_single_post($post_id);
                     // Check private tag on/off Issue #720
                     if (bool_from_yn($system_obj->setting['add_private_tags'])) {
                         // double check, not already added
                         if (preg_match('/\\[private\\](.*)\\[\\/private\\]/', $wp_post->post_content) == FALSE) {
                             // make content private
                             $post_content = sprintf('[private]%s[/private]', $wp_post->post_content);
                             // update
                             wp_update_post(array('post_content' => $post_content, 'ID' => $wp_post->ID));
                         }
                     }
                 }
                 // update counter
                 $updated++;
             }
         }
         // response
         if ($updated) {
             $response = array('status' => 'success', 'message' => sprintf(__('Post protection successfully updated. %d Post/Page(s) updated.', 'mgm'), $updated));
         } else {
             $response = array('status' => 'error', 'message' => sprintf(__('Post protection failed. %d Post/Page(s) selected.', 'mgm'), $updated));
         }
         // print
         echo json_encode($response);
         // return
         return;
     }
     // data
     $data = array();
     // member types
     $arr_membershiptypes = array();
     // loop
     foreach (mgm_get_class('membership_types')->membership_types as $code => $name) {
         $arr_membershiptypes[$code] = mgm_stripslashes_deep($name);
     }
     // set
     $data['membership_types'] = $arr_membershiptypes;
     // posts
     $data['posts'] = mgm_field_values($wpdb->posts, 'ID', 'post_title', "AND (post_content NOT LIKE '%[private]%' OR post_content LIKE '[private]%') AND post_type = 'post' AND post_status = 'publish'");
     // pages
     $data['pages'] = mgm_field_values($wpdb->posts, 'ID', 'post_title', "AND (post_content NOT LIKE '%[private]%' OR post_content LIKE '[private]%') AND post_type = 'page' AND post_status = 'publish'");
     // custom post types
     if ($post_types = mgm_get_post_types(true, array('page', 'post'))) {
         $data['custom_post_types'] = mgm_field_values($wpdb->posts, 'ID', "CONCAT(post_title, ' ( ', post_type, ' )') AS post_title", "AND (post_content NOT LIKE '%[private]%' OR post_content LIKE '[private]%') AND post_type IN ({$post_types}) AND post_status = 'publish'", 'post_title');
     } else {
         $data['custom_post_types'] = array();
     }
     // posts access
     $data['posts_access'] = $wpdb->get_results(sprintf("SELECT * FROM %s WHERE `post_id` IS NOT NULL ORDER BY id ASC", TBL_MGM_POST_PROTECTED_URL));
     // direct urls access
     $data['direct_urls_access'] = $wpdb->get_results(sprintf("SELECT * FROM %s WHERE `post_id` IS NULL ORDER BY id ASC", TBL_MGM_POST_PROTECTED_URL));
     // load template view
     $this->load->template('settings/posts', array('data' => $data));
 }