Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Set Content for Sale
- * If a post has tags that starts with a dollar sign, the post
- * will be automatically set for sale, allowing users to purchase
- * access to the post with myCRED Points.
- * Multiple tags can be used in which case their value is added up.
- * @requires myCRED 1.4 or higher
- * @version 1.1
- */
- add_action( 'save_post', 'mycred_pro_set_content_for_sale' );
- function mycred_pro_set_content_for_sale( $post_id ) {
- // If autosave
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
- // If myCRED is disabled
- if ( ! function_exists( 'mycred' ) ) return;
- // If draft posts
- if ( get_post_status( $post_id ) == 'draft' ) return;
- // Get post tags
- $tags = wp_get_post_tags( $post_id );
- if ( ! empty( $tags ) ) {
- // Check for price tags
- $price_tags = array();
- foreach ( $tags as $tag ) {
- if ( substr( $tag->name, 0, 1 ) == '$' )
- $price_tags[] = $tag->name;
- }
- // Load myCRED
- $mycred = mycred();
- // Prep cost
- $cost = 0;
- // If we found price tags
- if ( ! empty( $price_tags ) ) {
- // Add them up in case there is more then one
- foreach ( $price_tags as $tag ) {
- $price = ltrim( $tag, '$' );
- $cost = $mycred->number( $cost+$price );
- }
- }
- // If price is higher then zero
- if ( $cost > 0 ) {
- // Sell Content preference must be setup for myCRED to allow purchases
- if ( ! isset( $mycred->sell_content['defaults']['expire'] ) )
- $mycred->sell_content['defaults']['expire'] = 0;
- $sales_data = array(
- 'status' => 'enabled',
- 'price' => $cost,
- 'button_label' => $mycred->sell_content['defaults']['button_label'],
- 'expire' => $mycred->sell_content['defaults']['expire']
- );
- // Save
- update_post_meta( $post_id, 'myCRED_sell_content', $sales_data );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment