Advertisement
designbymerovingi

mycred_sell_content_payment_buttons version 1.1

Sep 19th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. /**
  2.  * Get Payment Buttons
  3.  * Returns all payment buttons a user can use to pay for a given post.
  4.  * @since 1.7
  5.  * @version 1.1
  6.  */
  7. if ( ! function_exists( 'mycred_sell_content_payment_buttons' ) ) :
  8.     function mycred_sell_content_payment_buttons( $user_id = NULL, $post_id = NULL ) {
  9.  
  10.         if ( $user_id === NULL || $post_id === NULL ) return false;
  11.  
  12.         $settings    = mycred_sell_content_settings();
  13.         $post        = get_post( $post_id );
  14.         $result      = false;
  15.  
  16.         if ( ! empty( $settings['type'] ) ) {
  17.  
  18.             $buttons = array();
  19.  
  20.             foreach ( $settings['type'] as $point_type ) {
  21.  
  22.                 // Load point type
  23.                 $mycred       = mycred( $point_type );
  24.                 $setup        = mycred_get_option( 'mycred_sell_this_' . $point_type );
  25.                 $price        = mycred_get_content_price( $post_id, $point_type, $user_id );
  26.                 $status       = $setup['status'];
  27.  
  28.                 // Manual mode
  29.                 if ( $settings['filters'][ $post->post_type ]['by'] == 'manual' ) {
  30.  
  31.                     $suffix       = ( $point_type != MYCRED_DEFAULT_TYPE_KEY ) ? '_' . $point_type : '';
  32.                     $manual_setup = (array) get_post_meta( $post_id, 'myCRED_sell_content' . $suffix, true );
  33.                     if ( ! empty( $manual_setup ) && array_key_exists( 'status', $manual_setup ) )
  34.                         $status = $manual_setup['status'];
  35.  
  36.                 }
  37.  
  38.                 // Point type not enabled
  39.                 if ( $status == 'disabled' ) continue;
  40.  
  41.                 // Make sure we are not excluded from this type
  42.                 if ( $mycred->exclude_user( $user_id ) ) continue;
  43.  
  44.                 // Make sure we can afford to pay
  45.                 if ( $mycred->get_users_balance( $user_id, $point_type ) < $price ) continue;
  46.  
  47.                 $button_label = str_replace( '%price%', $mycred->format_creds( $price ), $setup['button_label'] );
  48.  
  49.                 $button       = '<button type="button" class="mycred-buy-this-content-button ' . $setup['button_classes'] . '" data-pid="' . $post_id . '" data-type="' . $point_type . '">' . $button_label . '</button>';
  50.                 $buttons[]    = apply_filters( 'mycred_sell_this_button', $button, $post, $setup, $mycred );
  51.  
  52.             }
  53.  
  54.             if ( ! empty( $buttons ) )
  55.                 $result = implode( ' ', $buttons );
  56.  
  57.         }
  58.  
  59.         // Return a string of buttons or false if user can not afford
  60.         return apply_filters( 'mycred_sellcontent_buttons', $result, $user_id, $post_id );
  61.  
  62.     }
  63. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement