/**
  * Show Balance in Header
  * @since 0.1
  * @version 1.4.1
  */
 public function show_balance($dump = NULL, $context = 'header')
 {
     // Prep
     $output = '';
     $user_id = bp_displayed_user_id();
     // Check for exclusion
     if ($this->core->exclude_user($user_id)) {
         return;
     }
     // Check visibility settings
     if (!$this->buddypress['visibility']['balance'] && !bp_is_my_profile() && !mycred_is_admin()) {
         return;
     }
     // Parse template
     $template = $this->buddypress['balance_template'];
     // Loop though all post types
     $mycred_types = mycred_get_types();
     if (!empty($mycred_types)) {
         $_template = $template;
         foreach ($mycred_types as $type => $label) {
             $template = $_template;
             // Load myCRED with this points type
             $mycred = mycred($type);
             // Check if user is excluded from this type
             if ($mycred->exclude_user($user_id)) {
                 continue;
             }
             // Get users balance
             $balance = $mycred->get_users_cred($user_id, $type);
             // Output
             $template = str_replace('%label%', $label, $template);
             $template = $mycred->template_tags_general($template);
             $output .= '<div class="mycred-balance mycred-' . $type . '">' . $template . ' ' . $mycred->format_creds($balance) . '</div>';
         }
     }
     echo apply_filters('mycred_bp_profile_header', $output, $this->buddypress['balance_template'], $this);
 }
        /**
         * Render Shortcode Form
         * Returns an advanced version allowing for further customizations.
         * @since 0.1
         * @version 1.5
         */
        public function render_shortcode_form($atts, $content = '')
        {
            // Make sure the add-on has been setup
            if (!isset($this->core->buy_creds)) {
                if (mycred_is_admin()) {
                    return '<p style="color:red;"><a href="' . $this->get_settings_url('buycred_module') . '">' . __('This Add-on needs to setup before you can use this shortcode.', 'mycred') . '</a></p>';
                } else {
                    return '';
                }
            }
            extract(shortcode_atts(array('button' => '', 'gateway' => '', 'ctype' => 'mycred_default', 'amount' => '', 'gift_to' => '', 'login' => $this->core->template_tags_general($this->core->buy_creds['login'])), $atts));
            // If we are not logged in
            if (!is_user_logged_in()) {
                return '<p class="mycred-buy login">' . $login . '</p>';
            }
            // Catch errors
            $installed = $this->get();
            if (empty($installed)) {
                return __('No gateways installed.', 'mycred');
            }
            if (!empty($gateway) && !array_key_exists($gateway, $installed)) {
                return __('Gateway does not exist.', 'mycred');
            }
            if (empty($this->active)) {
                return __('No active gateways found.', 'mycred');
            }
            if (!empty($gateway) && !$this->is_active($gateway)) {
                return __('The selected gateway is not active.', 'mycred');
            }
            // Prep
            $buy_author = false;
            $buy_member = false;
            $buy_others = false;
            $buy_self = false;
            $classes = array('myCRED-buy-form');
            // Gifting is enabled
            if ($this->core->buy_creds['gifting']['authors']) {
                // This is a gift to the post author
                if ($gift_to == 'author') {
                    global $post;
                    $user_id = $post->post_author;
                    $buy_author = true;
                } elseif ($gift_to == 'select') {
                    $user_id = '';
                    $buy_others = true;
                } elseif (is_numeric($gift_to)) {
                    $user_id = absint($gift_to);
                    $buy_member = true;
                } else {
                    $user_id = get_current_user_id();
                    $buy_self = true;
                }
            } else {
                $user_id = get_current_user_id();
                $buy_self = true;
            }
            // Button
            if (!empty($gateway) && isset($installed[$gateway]['title']) && empty($button)) {
                $button_label = __('Buy with %gateway%', 'mycred');
            } elseif (!empty($button)) {
                $button_label = $button;
            } else {
                $button_label = __('Buy Now', 'mycred');
            }
            $button_label = $this->core->template_tags_general($button_label);
            if (!empty($gateway)) {
                $gateway_name = explode(' ', $installed[$gateway]['title']);
                $button_label = str_replace('%gateway%', $gateway_name[0], $button_label);
                $classes[] = $gateway_name[0];
            }
            // Start constructing form with title and submit button
            $form = '
<style type="text/css">
form.myCRED-buy-form { display: block; min-height: 80px; }
form.myCRED-buy-form > div { float: left; margin-right: 24px; }
form.myCRED-buy-form > div label { display: block; }
</style>
<form method="post" action="" class="' . implode(' ', $classes) . '">';
            // Gifting a specific user or post author
            if ($buy_author) {
                $form .= '
	<input type="hidden" name="post_id" value="' . $post_id . '" />';
            } elseif ($buy_member) {
                $form .= '
	<input type="hidden" name="gift_to" value="' . $user_id . '" />';
            } elseif ($buy_others) {
                // Select gift recipient from a drop-down
                if ($gift_to == 'select') {
                    $select = '<select name="gift_to">';
                    $blog_users = get_users();
                    if (!empty($blog_users)) {
                        foreach ($blog_users as $blog_user) {
                            if ($this->core->exclude_user($blog_user->ID) || $blog_user->ID === get_current_user_id()) {
                                continue;
                            }
                            $select .= '<option value="' . $blog_user->ID . '">' . $blog_user->display_name . '</option>';
                        }
                        unset($blog_users);
                    } else {
                        $select .= '<option value="">' . __('No users found', 'mycred') . '</option>';
                    }
                    $select .= '</select>';
                } else {
                    $select = '<input type="text" name="gift_to" value="" class="pick-user" size="20" />';
                }
                $form .= '
	<div class="select-to">
		<label>' . __('To', 'mycred') . ':</label>
		' . $select . '
	</div>';
            }
            // Amount
            $no_of_amounts = 0;
            $minimum = $this->core->number($this->core->buy_creds['minimum']);
            if (!empty($amount)) {
                $no_of_amounts = sizeof(array_filter(explode(',', $amount), create_function('$a', 'return !empty($a);')));
            }
            // Multiple amounts set
            if ($no_of_amounts > 1) {
                // Let user select from this list of amounts
                $amount = explode(',', $amount);
                $form .= '
	<div class="select-amount">
		<label>' . __('Select Amount', 'mycred') . ':</label>
		<select name="amount">';
                foreach ($amount as $number) {
                    $form .= '<option value="' . $number . '">' . $number . '</option>';
                }
                $form .= '
		</select>
	</div>';
            } elseif ((int) $no_of_amounts == 1) {
                $form .= '
	<input type="hidden" name="amount" value="' . $this->core->number($amount) . '" />';
            } else {
                $form .= '
	<div class="select-amount">
		<label>' . __('Amount', 'mycred') . ':</label>
		<input type="text" name="amount" value="' . $minimum . '" size="5" /><br />
		<em>' . __('min.', 'mycred') . ' ' . $minimum . '</em>
	</div>';
            }
            // No gateway nominated - User needs to select
            if ($gateway == '') {
                $form .= '
	<div class="select-gateway">
		<label>' . __('Select Gateway', 'mycred') . ':</label>
		<select name="mycred_buy">';
                foreach ($installed as $gateway_id => $data) {
                    if (!$this->is_active($gateway_id)) {
                        continue;
                    }
                    $form .= '<option value="' . $gateway_id . '">' . $data['title'] . '</option>';
                }
                $form .= '
		</select>
	</div>';
            } else {
                $form .= '
	<input type="hidden" name="mycred_buy" value="' . $gateway . '" />';
            }
            $form .= '
	<input type="hidden" name="token" value="' . wp_create_nonce('mycred-buy-creds') . '" />
	<input type="hidden" name="transaction_id" value="' . strtoupper(wp_generate_password(6, false, false)) . '" />
	<input type="hidden" name="ctype" value="' . $ctype . '" />
	<input type="submit" name="submit" style="margin-top:18px;" value="' . $button_label . '" class="mycred-buy button button-primary button-large btn btn-primary btn-lg" />
	<div class="clear clearfix"></div>
</form>';
            return $form;
        }
 /**
  * BuddyPress Profile
  * @since 1.4
  * @version 1.1
  */
 public function buddypress_profile()
 {
     // Prep
     $output = '';
     $user_id = bp_displayed_user_id();
     // Check for exclusion
     if ($this->core->exclude_user($user_id)) {
         return;
     }
     // If it is my profile or other members allowed to view eachothers profiles or if we are admins
     if (bp_is_my_profile() || mycred_is_admin()) {
         $users_ref_link = $this->get_ref_link($user_id, home_url('/'));
         $output .= '<div class="bp-widget mycred">';
         // Title if set
         if ($this->prefs['buddypress']['title'] != '') {
             $output .= '<h4>' . $this->prefs['buddypress']['title'] . '</h4>';
         }
         // Table
         $output .= '<table class="profile-fields">';
         $output .= sprintf('<tr class="field_1 field_ref_link"><td class="label">%s</td><td>%s</td></tr>', __('Link', 'mycred'), $users_ref_link);
         // Show Visitor referral count
         if ($this->prefs['visit']['creds'] != 0) {
             $output .= sprintf('<tr class="field_2 field_ref_count_visit"><td class="label">%s</td><td>%s</td></tr>', __('Visitors Referred', 'mycred'), mycred_count_ref_instances('visitor_referral', $user_id, $this->mycred_type));
         }
         // Show Signup referral count
         if ($this->prefs['signup']['creds'] != 0) {
             $output .= sprintf('<tr class="field_3 field_ref_count_signup"><td class="label">%s</td><td>%s</td></tr>', __('Signups Referred', 'mycred'), mycred_count_ref_instances('signup_referral', $user_id, $this->mycred_type));
         }
         $output .= '</table>';
         // Description if set
         if (!empty($this->prefs['buddypress']['desc'])) {
             $output .= wpautop(wptexturize($this->prefs['buddypress']['desc']));
         }
         $output .= '</div>';
     }
     $output = do_shortcode($output);
     echo apply_filters('mycred_affiliate_bp_profile', $output, $user_id, $users_ref_link, $this);
 }
 /**
  * Ajax: Inline Edit Users Balance
  * @since 1.2
  * @version 1.1
  */
 public function inline_edit_user_balance()
 {
     // Security
     check_ajax_referer('mycred-update-users-balance', 'token');
     // Check current user
     $current_user = get_current_user_id();
     if (!mycred_is_admin($current_user)) {
         wp_send_json_error('ERROR_1');
     }
     // Type
     $type = sanitize_text_field($_POST['type']);
     $mycred = mycred($type);
     // User
     $user_id = abs($_POST['user']);
     if ($mycred->exclude_user($user_id)) {
         wp_send_json_error(array('error' => 'ERROR_2', 'message' => __('User is excluded', 'mycred')));
     }
     // Log entry
     $entry = trim($_POST['entry']);
     if ($mycred->can_edit_creds() && !$mycred->can_edit_plugin() && empty($entry)) {
         wp_send_json_error(array('error' => 'ERROR_3', 'message' => __('Log Entry can not be empty', 'mycred')));
     }
     // Amount
     if ($_POST['amount'] == 0 || empty($_POST['amount'])) {
         wp_send_json_error(array('error' => 'ERROR_4', 'message' => __('Amount can not be zero', 'mycred')));
     } else {
         $amount = $mycred->number($_POST['amount']);
     }
     // Data
     $data = apply_filters('mycred_manual_change', array('ref_type' => 'user'), $this);
     // Execute
     $result = $mycred->add_creds('manual', $user_id, $amount, $entry, $current_user, $data, $type);
     if ($result !== false) {
         wp_send_json_success($mycred->get_users_cred($user_id, $type));
     } else {
         wp_send_json_error(array('error' => 'ERROR_5', 'message' => __('Failed to update this uses balance.', 'mycred')));
     }
 }
 /**
  * Render Shortcode AJAX
  * Just as protecting the entire content, the mycred_sell_this_ajax shortcode protects
  * parts of the content and uses AJAX to make the purchase
  *
  * @returns (string) content
  * @since 0.1
  * @version 1.0.1
  */
 public function render_ajax_shortcode($atts, $content)
 {
     global $mycred_buy_content;
     // The Post
     $post_id = $this->get_post_ID();
     $the_post = get_post($post_id);
     $user_id = get_current_user_id();
     $sell_content = $this->sell_content;
     $prefs = shortcode_atts(array('price' => $sell_content['defaults']['price'], 'button_label' => $sell_content['defaults']['button_label'], 'expire' => $sell_content['defaults']['expire']), $atts);
     $sales_prefs = $this->get_sale_prefs($post_id);
     // If we are not using defaults save these settings.
     if ($sales_prefs['price'] != $prefs['price'] || $sales_prefs['button_label'] != $prefs['button_label'] || $sales_prefs['expire'] != $prefs['expire']) {
         update_post_meta($post_id, 'myCRED_sell_content', array('price' => $prefs['price'], 'status' => $sales_prefs['status'], 'button_label' => $prefs['button_label'], 'expire' => $prefs['expire']));
     }
     // Not logged in
     if (!is_user_logged_in()) {
         $template = $sell_content['templates']['visitors'];
         $template = str_replace('%price%', $this->core->format_creds($prefs['price']), $template);
         $template = $this->core->template_tags_general($template);
         $template = $this->core->template_tags_post($template, $the_post);
         unset($content);
         return '<div class="mycred-content-forsale">' . $template . '</div>';
     } elseif (is_user_logged_in() && !$this->user_paid($user_id, $post_id) && $this->user_can_buy($user_id, $prefs['price'])) {
         $template = $sell_content['templates']['members'];
         $template = str_replace('%price%', $this->core->format_creds($prefs['price']), $template);
         $template = $this->core->template_tags_general($template);
         $template = $this->core->template_tags_post($template, $the_post);
         if (isset($prefs['button_label'])) {
             $button_text = $prefs['button_label'];
         } else {
             $button_text = $sell_content['defaults']['button_label'];
         }
         $button = '<input type="button" data-id="' . $the_post->ID . '" name="mycred-buy-button" value="' . $this->core->template_tags_post($button_text, $the_post) . '" class="mycred-sell-this-button button large" />';
         $template = str_replace('%buy_button%', $button, $template);
         unset($content);
         $mycred_buy_content = true;
         return '<div class="mycred-content-forsale">' . $template . '</div>';
     } elseif (is_user_logged_in() && !$this->user_paid($user_id, $post_id) && !$this->user_can_buy($user_id, $prefs['price'])) {
         $template = $sell_content['templates']['cantafford'];
         $template = str_replace('%price%', $this->core->format_creds($prefs['price']), $template);
         $template = $this->core->template_tags_general($template);
         $template = $this->core->template_tags_post($template, $the_post);
         unset($content);
         return '<div class="mycred-content-forsale">' . $template . '</div>';
     }
     // Admin and Author Wrapper for highlight of content set for sale
     if (mycred_is_admin() || $the_post->post_author == $user_id) {
         $content = '<div class="mycred-mark-title">' . __('The following content is set for sale:', 'mycred') . '</div><div class="mycred-mark-content">' . $content . '</div>';
     }
     return do_shortcode($content);
 }
 /**
  * Save Rank Details
  * @since 1.1
  * @version 1.1
  */
 public function save_rank_settings($post_id)
 {
     // Make sure this is the correct post type
     if (get_post_type($post_id) != 'mycred_rank') {
         return;
     } elseif (!mycred_is_admin(get_current_user_id())) {
         return;
     } elseif (!isset($_POST['mycred_rank']) || !is_array($_POST['mycred_rank'])) {
         return;
     } elseif (!wp_verify_nonce($_POST['mycred_rank']['token'], 'mycred-edit-rank')) {
         return;
     }
     // Minimum can not be empty
     if (empty($_POST['mycred_rank']['min'])) {
         $min = 0;
     } else {
         $min = trim($_POST['mycred_rank']['min']);
     }
     // Maximum can not be empty
     if (empty($_POST['mycred_rank']['max'])) {
         $max = 999;
     } else {
         $max = trim($_POST['mycred_rank']['max']);
     }
     update_post_meta($post_id, 'mycred_rank_min', $min);
     update_post_meta($post_id, 'mycred_rank_max', $max);
     if (get_post_status($post_id) == 'publish') {
         mycred_assign_ranks();
     }
 }
 /**
  * Save Email Notice Details
  * @since 1.1
  * @version 1.1
  */
 public function save_email_notice($post_id)
 {
     // Make sure this is the correct post type
     if (get_post_type($post_id) != 'mycred_email_notice') {
         return;
     } elseif (!mycred_is_admin(get_current_user_id())) {
         return;
     } elseif (!isset($_POST['mycred_email']) || !is_array($_POST['mycred_email'])) {
         return;
     } elseif (!wp_verify_nonce($_POST['mycred_email']['token'], 'mycred-edit-email')) {
         return;
     }
     // Update Instance
     if (!empty($_POST['mycred_email']['instance'])) {
         // Lets make sure the value is properly formatted otherwise things could go uggly later
         $instance_key = trim($_POST['mycred_email']['instance']);
         $keys = explode('|', $instance_key);
         if ($keys !== false && !empty($keys)) {
         }
         update_post_meta($post_id, 'mycred_email_instance', $instance_key);
     }
     // Construct new settings
     $settings = array();
     // If recipient is set but differs from the default, use the posted one else use default
     if (!empty($_POST['mycred_email']['recipient'])) {
         $settings['recipient'] = $_POST['mycred_email']['recipient'];
     } else {
         $settings['recipient'] = 'user';
     }
     // If senders name is set but differs from the default, use the posted one else use default
     if (!empty($_POST['mycred_email']['senders_name'])) {
         $settings['senders_name'] = $_POST['mycred_email']['senders_name'];
     } else {
         $settings['senders_name'] = $this->emailnotices['from']['name'];
     }
     // If senders email is set but differs from the default, use the posted one else use default
     if (!empty($_POST['mycred_email']['senders_email'])) {
         $settings['senders_email'] = $_POST['mycred_email']['senders_email'];
     } else {
         $settings['senders_email'] = $this->emailnotices['from']['email'];
     }
     // If senders email is set but differs from the default, use the posted one else use default
     if (!empty($_POST['mycred_email']['reply_to'])) {
         $settings['reply_to'] = $_POST['mycred_email']['reply_to'];
     } else {
         $settings['reply_to'] = $this->emailnotices['from']['reply_to'];
     }
     $settings['label'] = sanitize_text_field($_POST['mycred_email']['label']);
     // Save settings
     update_post_meta($post_id, 'mycred_email_settings', $settings);
     // If rich editing is disabled bail now
     if ($this->emailnotices['use_html'] === false) {
         return;
     }
     // Save styling
     if (!empty($_POST['mycred_email']['styling'])) {
         update_post_meta($post_id, 'mycred_email_styling', trim($_POST['mycred_email']['styling']));
     }
 }
Exemple #8
0
 /**
  * BuddyPress Profile
  * @since 1.4
  * @version 1.0
  */
 public function buddypress_profile()
 {
     // Prep
     $output = '';
     $user_id = bp_displayed_user_id();
     // Check for exclusion
     if ($this->core->exclude_user($user_id)) {
         return;
     }
     // If it is my profile or other members allowed to view eachothers profiles or if we are admins
     if (bp_is_my_profile() || mycred_is_admin()) {
         $users_ref_link = $this->get_ref_link($user_id, home_url('/'));
         $output .= '<div class="bp-widget mycred">';
         // Title if set
         if (!empty($this->prefs['buddypress']['title'])) {
             $output .= '<h4>' . $this->prefs['buddypress']['title'] . '</h4>';
         }
         // Table
         $output .= sprintf('<table class="profile-fields"><tr class="field_1 field_ref_link"><td class="label">%s</td><td>%s</td></tr></table>', __('Link', 'mycred'), $users_ref_link);
         // Description if set
         if (!empty($this->prefs['buddypress']['desc'])) {
             $output .= $this->prefs['buddypress']['desc'];
         }
         $output .= '</div>';
     }
     echo apply_filters('mycred_affiliate_bp_profile', $output, $user_id, $users_ref_link, $this);
 }