Completed
Pull Request — master (#460)
by Dustin
17:10
created

Give_Donators_Gravatars_Widget::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 26
ccs 0
cts 14
cp 0
crap 6
rs 8.8571
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Donators Gravatars
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Donators
7
 * @copyright   Copyright (c) 2015, WordImpress
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
class Give_Donators_Gravatars {
18
19
	/**
20
	 * Start your engines
21
	 *
22
	 * @since 1.0
23
	 *
24
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
25
	 */
26
	public function __construct() {
27
		$this->setup_actions();
0 ignored issues
show
Unused Code introduced by
The call to the method Give_Donators_Gravatars::setup_actions() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
28
	}
29
30
	/**
31
	 * Setup the default hooks and actions
32
	 *
33
	 * @since 1.0
34
	 *
35
	 * @return void
36
	 */
37
	private function setup_actions() {
38
		//		add_action( 'widgets_init', array( $this, 'register_widget' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
		//		add_shortcode( 'give_donators_gravatars', array( $this, 'shortcode' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
		//		add_filter( 'give_settings_display', array( $this, 'settings' ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
		//		do_action( 'give_donators_gravatars_setup_actions' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
	}
43
44
	/**
45
	 * Utility function to check if a gravatar exists for a given email or id
46
	 *
47
	 * @param int|string|object $id_or_email A user ID, email address, or comment object
48
	 *
49
	 * @return bool if the gravatar exists or not
50
	 */
51
52
	// https://gist.github.com/justinph/5197810
53
	function validate_gravatar( $id_or_email ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
		//id or email code borrowed from wp-includes/pluggable.php
55
		$email = '';
56
		if ( is_numeric( $id_or_email ) ) {
57
			$id   = (int) $id_or_email;
58
			$user = get_userdata( $id );
59
			if ( $user ) {
60
				$email = $user->user_email;
61
			}
62
		} elseif ( is_object( $id_or_email ) ) {
63
			// No avatar for pingbacks or trackbacks
64
			$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
65
			if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
66
				return false;
67
			}
68
69
			if ( ! empty( $id_or_email->user_id ) ) {
70
				$id   = (int) $id_or_email->user_id;
71
				$user = get_userdata( $id );
72
				if ( $user ) {
73
					$email = $user->user_email;
74
				}
75
			} elseif ( ! empty( $id_or_email->comment_author_email ) ) {
76
				$email = $id_or_email->comment_author_email;
77
			}
78
		} else {
79
			$email = $id_or_email;
80
		}
81
82
		$hashkey = md5( strtolower( trim( $email ) ) );
83
		$uri     = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404';
84
85
		$data = wp_cache_get( $hashkey );
86
		if ( false === $data ) {
87
			$response = wp_remote_head( $uri );
88
			if ( is_wp_error( $response ) ) {
89
				$data = 'not200';
90
			} else {
91
				$data = $response['response']['code'];
92
			}
93
			wp_cache_set( $hashkey, $data, $group = '', $expire = 60 * 5 );
94
95
		}
96
		if ( $data == '200' ) {
97
			return true;
98
		} else {
99
			return false;
100
		}
101
	}
102
103
	/**
104
	 * Get an array of all the log IDs using the Give Logging Class
105
	 *
106
	 * @since 1.0
107
	 * @return array if logs, false otherwise
108
	 *
109
	 * @param int $form_id
110
	 */
111
	function get_log_ids( $form_id = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
112
113
		// get Give_Logging class
114
		global $give_logs;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
115
116
		// get log for this form
117
		$logs = $give_logs->get_logs( $form_id );
118
119
		if ( $logs ) {
120
			// make an array with all the donor IDs
121
			foreach ( $logs as $log ) {
122
				$log_ids[] = $log->ID;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$log_ids was never initialized. Although not strictly required by PHP, it is generally a good practice to add $log_ids = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
123
			}
124
125
			return $log_ids;
0 ignored issues
show
Bug introduced by
The variable $log_ids does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
126
		}
127
128
		return null;
129
130
	}
131
132
133
	/**
134
	 * Get payment ID
135
	 *
136
	 * @since 1.0
137
	 */
138
	function get_payment_ids( $form_id = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
139
140
		global $give_options;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
141
142
		$log_ids = $this->get_log_ids( $form_id );
143
144
		if ( $log_ids ) {
145
146
			$payment_ids = array();
147
148
			foreach ( $log_ids as $id ) {
149
				// get the payment ID for each corresponding log ID
150
				$payment_ids[] = get_post_meta( $id, '_give_log_payment_id', true );
151
			}
152
153
			// remove donors who have purchased more than once so we can have unique avatars
154
			$unique_emails = array();
155
156
			foreach ( $payment_ids as $key => $id ) {
157
158
				$email = get_post_meta( $id, '_give_payment_user_email', true );
159
160
				if ( isset ( $give_options['give_donators_gravatars_has_gravatar_account'] ) ) {
161
					if ( ! $this->validate_gravatar( $email ) ) {
162
						continue;
163
					}
164
				}
165
166
				$unique_emails[ $id ] = get_post_meta( $id, '_give_payment_user_email', true );
167
168
			}
169
170
			// strip duplicate emails
171
			$unique_emails = array_unique( $unique_emails );
172
173
			// convert the unique IDs back into simple array
174
			foreach ( $unique_emails as $id => $email ) {
175
				$unique_ids[] = $id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$unique_ids was never initialized. Although not strictly required by PHP, it is generally a good practice to add $unique_ids = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
176
			}
177
178
			// randomize the payment IDs if enabled
179
			if ( isset( $give_options['give_donators_gravatars_random_gravatars'] ) ) {
180
				shuffle( $unique_ids );
181
			}
182
183
			// return our unique IDs
184
			return $unique_ids;
0 ignored issues
show
Bug introduced by
The variable $unique_ids does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
185
186
		}
187
188
	}
189
190
191
	/**
192
	 * Gravatars
193
	 *
194
	 * @since 1.0
195
	 */
196
	function gravatars( $form_id = false, $title = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
197
198
		// unique $payment_ids 
199
		$payment_ids = $this->get_payment_ids( $form_id );
200
201
		//			var_dump( $payment_ids );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
		//			 var_dump( $this->get_log_ids( get_the_ID() ) );
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203
204
		global $give_options;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
205
206
		// return if no ID
207
		if ( ! $form_id ) {
208
			return;
209
		}
210
211
		// minimum amount of purchases before showing gravatars
212
		// if the number of items in array is not greater or equal to the number specified, then exit
213
		if ( isset( $give_options['give_donators_gravatars_min_purchases_required'] ) && '' != $give_options['give_donators_gravatars_min_purchases_required'] ) {
214
			if ( ! ( count( $payment_ids ) >= $give_options['give_donators_gravatars_min_purchases_required'] ) ) {
215
				return;
216
			}
217
		}
218
219
		ob_start();
220
221
		$output = '';
222
		echo '<div id="give-purchase-gravatars">';
223
224
225
		if ( isset ( $title ) ) {
226
227
			if ( $title ) {
228
				echo apply_filters( 'give_donators_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $title ) . '</h3>' );
229
			} elseif ( isset( $give_options['give_donators_gravatars_heading'] ) ) {
230
				echo apply_filters( 'give_donators_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $give_options['give_donators_gravatars_heading'] ) . '</h2>' );
231
			}
232
233
		}
234
		echo '<ul class="give-purchase-gravatars-list">';
235
		$i = 0;
236
237
		if ( $payment_ids ) {
238
			foreach ( $payment_ids as $id ) {
239
240
				// Give saves a blank option even when the control is turned off, hence the extra check
241
				if ( isset( $give_options['give_donators_gravatars_maximum_number'] ) && '' != $give_options['give_donators_gravatars_maximum_number'] && $i == $give_options['give_donators_gravatars_maximum_number'] ) {
242
					continue;
243
				}
244
245
				// get the payment meta
246
				$payment_meta = get_post_meta( $id, '_give_payment_meta', true );
247
248
				// unserialize the payment meta
249
				$user_info = maybe_unserialize( $payment_meta['user_info'] );
250
251
				// get donor's first name
252
				$name = $user_info['first_name'];
253
254
				// get donor's email
255
				$email = get_post_meta( $id, '_give_payment_user_email', true );
256
257
				// set gravatar size and provide filter
258
				$size = isset( $give_options['give_donators_gravatars_gravatar_size'] ) ? apply_filters( 'give_donators_gravatars_gravatar_size', $give_options['give_donators_gravatars_gravatar_size'] ) : '';
259
260
				// default image
261
				$default_image = apply_filters( 'give_donators_gravatars_gravatar_default_image', false );
262
263
				// assemble output
264
				$output .= '<li>';
265
266
				$output .= get_avatar( $email, $size, $default_image, $name );
267
				$output .= '</li>';
268
269
				$i ++;
270
271
			} // end foreach
272
		}
273
274
		echo $output;
275
		echo '</ul>';
276
		echo '</div>';
277
278
		return apply_filters( 'give_donators_gravatars', ob_get_clean() );
279
	}
280
281
	/**
282
	 * Register widget
283
	 *
284
	 * @since 1.0
285
	 */
286
	function register_widget() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
287
		register_widget( 'Give_Donators_Gravatars_Widget' );
288
	}
289
290
	/**
291
	 * Shortcode
292
	 *
293
	 * @since 1.0
294
	 * @todo  set the ID to get_the_ID() if ID parameter is not passed through. Otherwise it will incorrectly get other gravatars
295
	 */
296
	function shortcode( $atts, $content = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
297
298
		$atts = shortcode_atts( array(
299
			'id'    => '',
300
			'title' => ''
301
		), $atts, 'give_donators_gravatars' );
302
303
		// if no ID is passed on single give_forms pages, get the correct ID
304
		if ( is_singular( 'give_forms' ) ) {
305
			$id = get_the_ID();
306
		}
307
308
		$content = $this->gravatars( $atts['id'], $atts['title'] );
309
310
		return $content;
311
312
	}
313
314
	/**
315
	 * Settings
316
	 *
317
	 * @since 1.0
318
	 */
319
	function settings( $settings ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
320
321
		$give_gravatar_settings = array(
322
			array(
323
				'name' => __( 'Donator Gravatars', 'give' ),
324
				'desc' => '<hr>',
325
				'id'   => 'give_title',
326
				'type' => 'give_title'
327
			),
328
			array(
329
				'name' => __( 'Heading', 'give' ),
330
				'desc' => __( 'The heading to display above the Gravatars', 'give' ),
331
				'type' => 'text',
332
				'id'   => 'give_donators_gravatars_heading'
333
			),
334
			array(
335
				'name'    => __( 'Gravatar Size', 'give' ),
336
				'desc'    => __( 'The size of each Gravatar in pixels (512px maximum)', 'give' ),
337
				'type'    => 'text_small',
338
				'id'      => 'give_donators_gravatars_gravatar_size',
339
				'default' => '64'
340
			),
341
			array(
342
				'name' => __( 'Minimum Unique Purchases Required', 'give' ),
343
				'desc' => sprintf( __( 'The minimum number of unique purchases a %s must have before the Gravatars are shown. Leave blank for no minimum.', 'give' ), strtolower( give_get_forms_label_singular() ) ),
344
				'type' => 'text_small',
345
				'id'   => 'give_donators_gravatars_min_purchases_required',
346
			),
347
			array(
348
				'name'    => __( 'Maximum Gravatars To Show', 'give' ),
349
				'desc'    => __( 'The maximum number of gravatars to show. Leave blank for no limit.', 'give' ),
350
				'type'    => 'text',
351
				'id'      => 'give_donators_gravatars_maximum_number',
352
				'default' => '20',
353
			),
354
			array(
355
				'name' => __( 'Gravatar Visibility', 'give' ),
356
				'desc' => __( 'Only show donators with a Gravatar account', 'give' ),
357
				'id'   => 'give_donators_gravatars_has_gravatar_account',
358
				'type' => 'checkbox',
359
			),
360
			array(
361
				'name' => __( 'Randomize Gravatars', 'give' ),
362
				'desc' => __( 'Randomize the Gravatars', 'give' ),
363
				'id'   => 'give_donators_gravatars_random_gravatars',
364
				'type' => 'checkbox',
365
			),
366
		);
367
368
		return array_merge( $settings, $give_gravatar_settings );
369
	}
370
371
}
372
373
374
/**
375
 * Widget
376
 *
377
 * @since 1.0
378
 */
379
class Give_Donators_Gravatars_Widget extends WP_Widget {
380
381
	/*
382
	 * widget constructor
383
	 */
384
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
385
386
		$give_label_singular = function_exists( 'give_get_forms_label_singular' ) ? strtolower( give_get_forms_label_singular() ) : null;
387
388
		// widget settings
389
		$widget_ops = array(
390
			'classname'   => 'give-donators-gravatars',
391
			'description' => sprintf( __( 'Displays gravatars of people who have donated using your your %s. Will only show on the single %s page.', 'give' ), $give_label_singular, $give_label_singular )
392
		);
393
394
		// widget control settings
395
		$control_ops = array(
396
			'width'   => 250,
397
			'height'  => 350,
398
			'id_base' => 'give_gravatars_widget'
399
		);
400
401
		// create the widget
402
		$this->WP_Widget(
403
			'give_donators_gravatars_widget',
404
			__( 'Give Donators Gravatars', 'give' ),
405
			$widget_ops,
406
			$control_ops
407
		);
408
409
	} // end constructor
410
411
412
	/*
413
	 * Outputs the content of the widget
414
	 */
415
	function widget( $args, $instance ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
416
		global $give_options;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
417
418
		extract( $args );
419
420
		if ( ! is_singular( 'give_forms' ) ) {
421
			return;
422
		}
423
424
		// Variables from widget settings
425
		$title = apply_filters( 'widget_title', $instance['title'] );
426
427
		// Used by themes. Opens the widget
428
		echo $before_widget;
429
430
		// Display the widget title
431
		if ( $title ) {
432
			echo $before_title . $title . $after_title;
433
		}
434
435
		$gravatars = new Give_Donators_Gravatars();
436
437
		echo $gravatars->gravatars( get_the_ID(), null ); // remove title
438
439
		// Used by themes. Closes the widget
440
		echo $after_widget;
441
442
	} // end WIDGET function
443
444
	/*
445
	 * Update function. Processes widget options to be saved
446
	 */
447
	function update( $new_instance, $old_instance ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
448
449
		$instance = $old_instance;
450
451
		$instance['title'] = strip_tags( $new_instance['title'] );
452
453
		return $instance;
454
455
	} // end UPDATE function
456
457
	/*
458
	 * Form function. Displays the actual form on the widget page
459
	 */
460
	function form( $instance ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
461
462
		// Set up some default widget settings.
463
		$defaults = array(
464
			'title' => '',
465
		);
466
467
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
468
469
		<!-- Title -->
470
		<p>
471
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'give' ) ?></label>
472
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" />
473
		</p>
474
475
476
		<?php
477
	} // end FORM function
478
479
}
480