Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. /**
  2.  * Shortcode: Take Points
  3.  */
  4. add_shortcode( 'mycred_take', 'mycred_pro_render_take_shortcode' );
  5. function mycred_pro_render_take_shortcode( $atts, $label = 'Download Charge' ) {
  6.  
  7.     extract( shortcode_atts( array(
  8.         'user_id' => '',
  9.         'confirm' => '',
  10.         'amount'  => '',
  11.         'unique'  => 0,
  12.         'ref'     => 'mycred_take',
  13.         'entry'   => '%plural% lose',
  14.         'done'    => 'You have lost points',
  15.         'ctype'   => 'mycred_default'
  16.     ), $atts ) );
  17.  
  18.     if ( ! is_user_logged_in() || ! function_exists( 'mycred' ) ) {
  19.         return '';
  20.     }
  21.  
  22.     if ( $user_id == '' ) {
  23.         $user_id = get_current_user_id();
  24.     }
  25.  
  26.     // Load essentials
  27.     $user_id = absint( $user_id );
  28.     $mycred = mycred( $ctype );
  29.  
  30.     // User is excluded = has no balance
  31.     if ( $mycred->exclude_user( $user_id ) ) {
  32.         return '';
  33.     }
  34.  
  35.     // Unique check
  36.     if ( $unique == 1 && $mycred->has_entry( $ref, 0, $user_id, '', $ctype ) ){
  37.         return '';
  38.     }
  39.  
  40.     $balance = $mycred->get_users_balance( $user_id, $ctype );
  41.  
  42.     $output = '';
  43.  
  44.     // If button was pushed
  45.     if ( isset( $_POST['mycred-take-points-token'] ) && wp_verify_nonce( $_POST['mycred-take-points-token'], 'mycred-deduct-points' . $ref . $ctype ) ) {
  46.  
  47.         // Deduct
  48.         $mycred->add_creds(
  49.             $ref,
  50.             $user_id,
  51.             0 - $amount,
  52.             $entry
  53.         );
  54.  
  55.         // Update balance
  56.         $balance = $balance - $amount;
  57.  
  58.         if ( $done != '' ) {
  59.             $output .= '<p>' . $done . '</p>';
  60.         }
  61.  
  62.         ob_start();
  63.         $url ="https://google.com";
  64.         ?>
  65.         <script type="text/javascript">
  66.             window.location="<?php echo esc_url( $url );?>";
  67.         </script>
  68.         <?php
  69.         $script = ob_get_clean();
  70.         $output .=$script;
  71.     }
  72.  
  73.     // Too low balance
  74.     if ( $balance < $amount ) {
  75.         return '';
  76.     }
  77.  
  78.     $onclick = '';
  79.     if ( $confirm != '' ) {
  80.         $onclick = '
  81. <script type="text/javascript">
  82. ( function( $ ) {
  83.     $( "form#mycred-take-shortcode' . $ref . $ctype . '" ).on( "submit", function(){
  84.         if ( ! confirm( \'' . $confirm . '\' ) ) return false;
  85.     });
  86. } )( jQuery );
  87. </script>';
  88.     }
  89.  
  90.     return $output . '<form action="" method="post" id="mycred-take-shortcode' . $ref . $ctype . '"><input type="hidden" name="mycred-take-points-token" value="' . wp_create_nonce( 'mycred-deduct-points' . $ref . $ctype ) . '" /><input type="submit" class="button" value="' . $label . '" /></form>' . $onclick;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement