Last active
August 29, 2015 13:56
-
-
Save gabrielmerovingi/8900291 to your computer and use it in GitHub Desktop.
This is a shortcode example for enforcing a limit on the number of times users can buy posts that has been set for sale via the myCRED Sell Content add-on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Custom Shortcode: Limited Content Purchases | |
* Example WordPress shortcode to impose a maximum number of times | |
* a user can purchase a post in WordPress using the myCRED Sell Content add-on. | |
* Requires myCRED 1.4 or higher! | |
* @version 1.1 | |
*/ | |
add_shortcode( 'mycred_sell_this_limited', 'mycred_render_sell_this_limited' ); | |
function mycred_render_sell_this_limited( $atts, $content = NULL ) { | |
// First make sure we do not get an error if myCRED gets deactivated | |
if ( ! function_exists( 'mycred' ) ) return; | |
// If we are logged in, check first if the current user has | |
// purchases left. | |
if ( is_user_logged_in() ) { | |
global $wpdb; | |
// Load myCRED | |
$mycred = mycred(); | |
// Lets query the database | |
$count = $wpdb->get_var( $wpdb->prepare( " | |
SELECT COUNT(*) | |
FROM {$mycred->log_table} | |
WHERE ref = %s | |
AND user_id = %d | |
AND ref_id = %d;", 'buy_content', get_current_user_id(), $GLOBALS['post']->ID ) ); | |
// If user reached the maximum allowed purchased, return something else | |
// instead of the shortcode. This will prevent them from being able to make further | |
// purchases. | |
// In this example, we return a simple message. You can change this to anything | |
// as long as it gets returned. | |
if ( $count >= 3 ) | |
return 'I am sorry but you can only buy this post max 3 times.'; | |
} | |
// Pass on any attributes we might be using | |
$attr = ''; | |
if ( is_array( $atts ) && ! empty( $atts ) ) { | |
foreach ( $atts as $key => $value ) | |
$attr .= $key . '="' . $value . '" '; | |
} | |
// Lets run the original myCRED shortcode | |
return do_shortcode( '[mycred_sell_this ' . $attr . ']' . $content . '[/mycred_sell_this]' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment