designbymerovingi

Auto for sale content

Feb 3rd, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. /**
  2.  * Set Content for Sale
  3.  * If a post has tags that starts with a dollar sign, the post
  4.  * will be automatically set for sale, allowing users to purchase
  5.  * access to the post with myCRED Points.
  6.  * Multiple tags can be used in which case their value is added up.
  7.  * @requires myCRED 1.4 or higher
  8.  * @version 1.1
  9.  */
  10. add_action( 'save_post', 'mycred_pro_set_content_for_sale' );
  11. function mycred_pro_set_content_for_sale( $post_id ) {
  12.     // If autosave
  13.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  14.     // If myCRED is disabled
  15.     if ( ! function_exists( 'mycred' ) ) return;
  16.     // If draft posts
  17.     if ( get_post_status( $post_id ) == 'draft' ) return;
  18.  
  19.     // Get post tags
  20.     $tags = wp_get_post_tags( $post_id );
  21.     if ( ! empty( $tags ) ) {
  22.  
  23.         // Check for price tags
  24.         $price_tags = array();
  25.         foreach ( $tags as $tag ) {
  26.             if ( substr( $tag->name, 0, 1 ) == '$' )
  27.                 $price_tags[] = $tag->name;
  28.         }
  29.  
  30.         // Load myCRED
  31.         $mycred = mycred();
  32.  
  33.         // Prep cost
  34.         $cost = 0;
  35.  
  36.         // If we found price tags
  37.         if ( ! empty( $price_tags ) ) {
  38.  
  39.             // Add them up in case there is more then one
  40.             foreach ( $price_tags as $tag ) {
  41.                 $price = ltrim( $tag, '$' );
  42.                 $cost = $mycred->number( $cost+$price );
  43.             }
  44.  
  45.         }
  46.  
  47.         // If price is higher then zero
  48.         if ( $cost > 0 ) {
  49.  
  50.             // Sell Content preference must be setup for myCRED to allow purchases
  51.             if ( ! isset( $mycred->sell_content['defaults']['expire'] ) )
  52.                 $mycred->sell_content['defaults']['expire'] = 0;
  53.  
  54.             $sales_data = array(
  55.                 'status'       => 'enabled',
  56.                 'price'        => $cost,
  57.                 'button_label' => $mycred->sell_content['defaults']['button_label'],
  58.                 'expire'       => $mycred->sell_content['defaults']['expire']
  59.             );
  60.  
  61.             // Save
  62.             update_post_meta( $post_id, 'myCRED_sell_content', $sales_data );
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment