/**
  * Save widget options
  * 
  * @see WP_Widget::update()
  */
 function update($new_instance, $old_instance)
 {
     $settings = $old_instance;
     $settings['title'] = strip_tags($new_instance['title']);
     if (!empty($new_instance['amount'])) {
         $settings['amount'] = Affiliates_Utility::verify_referral_amount($new_instance['amount']);
     } else {
         unset($settings['amount']);
     }
     if (!empty($new_instance['currency_id'])) {
         $settings['currency_id'] = Affiliates_Utility::verify_currency_id($new_instance['currency_id']);
     } else {
         unset($settings['currency_id']);
     }
     return $settings;
 }
Exemplo n.º 2
0
/**
 * Update the referral.
 * @param array $attributes to update, supports: affiliate_id, post_id, datetime, description, amount, currency_id, status, reference
 * @return array with keys, values and old_values or null if nothing was updated
 */
function affiliates_update_referral($referral_id, $attributes)
{
    global $wpdb;
    $result = null;
    $referral = null;
    $referrals_table = _affiliates_get_tablename('referrals');
    if ($referrals = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$referrals_table} WHERE referral_id = %d", intval($referral_id)))) {
        if (count($referrals) > 0) {
            $referral = $referrals[0];
        }
    }
    if ($referral !== null) {
        $set = array();
        $keys = array();
        $values = array();
        $old_values = array();
        foreach ($attributes as $key => $value) {
            $current_value = isset($referral->{$key}) ? $referral->{$key} : null;
            if ($current_value !== $value) {
                switch ($key) {
                    case 'affiliate_id':
                    case 'post_id':
                        $set[] = " {$key} = %d ";
                        $keys[] = $key;
                        $values[] = intval($value);
                        $old_values[] = $current_value;
                        break;
                    case 'datetime':
                    case 'description':
                    case 'reference':
                        $set[] = " {$key} = %s ";
                        $keys[] = $key;
                        $values[] = $value;
                        $old_values[] = $current_value;
                        break;
                    case 'status':
                        // Just check that this is a valid status:
                        if (!empty($value) && Affiliates_Utility::verify_referral_status_transition($value, $value)) {
                            $set[] = " {$key} = %s ";
                            $keys[] = $key;
                            $values[] = $value;
                            $old_values[] = $current_value;
                        }
                        break;
                    case 'amount':
                        if ($value = Affiliates_Utility::verify_referral_amount($value)) {
                            $set[] = " {$key} = %s ";
                            $keys[] = $key;
                            $values[] = $value;
                            $old_values[] = $current_value;
                        }
                        break;
                    case 'currency_id':
                        if ($value = Affiliates_Utility::verify_currency_id($value)) {
                            $set[] = " {$key} = %s ";
                            $keys[] = $key;
                            $values[] = $value;
                            $old_values[] = $current_value;
                        }
                        break;
                }
            }
        }
        if (count($set) > 0) {
            $set = implode(' , ', $set);
            if ($wpdb->query($wpdb->prepare("UPDATE {$referrals_table} SET {$set} WHERE referral_id = %d", array_merge($values, array(intval($referral_id)))))) {
                $result = array('keys' => $keys, 'values' => $values, 'old_values' => $old_values);
                do_action('affiliates_updated_referral', intval($referral_id), $keys, $values, $old_values);
            }
        }
    }
    return $result;
}