Completed
Pull Request — master (#664)
by Devin
19:01
created

Give_Donators_Gravatars::gravatars()   C

Complexity

Conditions 14
Paths 18

Size

Total Lines 81
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 14
eloc 36
c 2
b 0
f 0
nc 18
nop 2
dl 0
loc 81
rs 5.0042

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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) 2016, 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
		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...
202
203
		// return if no ID
204
		if ( ! $form_id ) {
205
			return;
206
		}
207
208
		// minimum amount of purchases before showing gravatars
209
		// if the number of items in array is not greater or equal to the number specified, then exit
210
		if ( isset( $give_options['give_donators_gravatars_min_purchases_required'] ) && '' != $give_options['give_donators_gravatars_min_purchases_required'] ) {
211
			if ( ! ( count( $payment_ids ) >= $give_options['give_donators_gravatars_min_purchases_required'] ) ) {
212
				return;
213
			}
214
		}
215
216
		ob_start();
217
218
		$output = '';
219
		echo '<div id="give-purchase-gravatars">';
220
221
222
		if ( isset ( $title ) ) {
223
224
			if ( $title ) {
225
				echo apply_filters( 'give_donators_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $title ) . '</h3>' );
226
			} elseif ( isset( $give_options['give_donators_gravatars_heading'] ) ) {
227
				echo apply_filters( 'give_donators_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $give_options['give_donators_gravatars_heading'] ) . '</h2>' );
228
			}
229
230
		}
231
		echo '<ul class="give-purchase-gravatars-list">';
232
		$i = 0;
233
234
		if ( $payment_ids ) {
235
			foreach ( $payment_ids as $id ) {
236
237
				// Give saves a blank option even when the control is turned off, hence the extra check
238
				if ( isset( $give_options['give_donators_gravatars_maximum_number'] ) && '' != $give_options['give_donators_gravatars_maximum_number'] && $i == $give_options['give_donators_gravatars_maximum_number'] ) {
239
					continue;
240
				}
241
242
				// get the payment meta
243
				$payment_meta = get_post_meta( $id, '_give_payment_meta', true );
244
245
				// unserialize the payment meta
246
				$user_info = maybe_unserialize( $payment_meta['user_info'] );
247
248
				// get donor's first name
249
				$name = $user_info['first_name'];
250
251
				// get donor's email
252
				$email = get_post_meta( $id, '_give_payment_user_email', true );
253
254
				// set gravatar size and provide filter
255
				$size = isset( $give_options['give_donators_gravatars_gravatar_size'] ) ? apply_filters( 'give_donators_gravatars_gravatar_size', $give_options['give_donators_gravatars_gravatar_size'] ) : '';
256
257
				// default image
258
				$default_image = apply_filters( 'give_donators_gravatars_gravatar_default_image', false );
259
260
				// assemble output
261
				$output .= '<li>';
262
263
				$output .= get_avatar( $email, $size, $default_image, $name );
264
				$output .= '</li>';
265
266
				$i ++;
267
268
			} // end foreach
269
		}
270
271
		echo $output;
272
		echo '</ul>';
273
		echo '</div>';
274
275
		return apply_filters( 'give_donators_gravatars', ob_get_clean() );
276
	}
277
278
	/**
279
	 * Register widget
280
	 *
281
	 * @since 1.0
282
	 */
283
	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...
284
		register_widget( 'Give_Donators_Gravatars_Widget' );
285
	}
286
287
	/**
288
	 * Shortcode
289
	 *
290
	 * @since 1.0
291
	 * @todo  set the ID to get_the_ID() if ID parameter is not passed through. Otherwise it will incorrectly get other gravatars
292
	 */
293
	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...
294
295
		$atts = shortcode_atts( array(
296
			'id'    => '',
297
			'title' => ''
298
		), $atts, 'give_donators_gravatars' );
299
300
		// if no ID is passed on single give_forms pages, get the correct ID
301
		if ( is_singular( 'give_forms' ) ) {
302
			$id = get_the_ID();
303
		}
304
305
		$content = $this->gravatars( $atts['id'], $atts['title'] );
306
307
		return $content;
308
309
	}
310
311
	/**
312
	 * Settings
313
	 *
314
	 * @since 1.0
315
	 */
316
	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...
317
318
		$give_gravatar_settings = array(
319
			array(
320
				'name' => __( 'Donator Gravatars', 'give' ),
321
				'desc' => '<hr>',
322
				'id'   => 'give_title',
323
				'type' => 'give_title'
324
			),
325
			array(
326
				'name' => __( 'Heading', 'give' ),
327
				'desc' => __( 'The heading to display above the Gravatars', 'give' ),
328
				'type' => 'text',
329
				'id'   => 'give_donators_gravatars_heading'
330
			),
331
			array(
332
				'name'    => __( 'Gravatar Size', 'give' ),
333
				'desc'    => __( 'The size of each Gravatar in pixels (512px maximum)', 'give' ),
334
				'type'    => 'text_small',
335
				'id'      => 'give_donators_gravatars_gravatar_size',
336
				'default' => '64'
337
			),
338
			array(
339
				'name' => __( 'Minimum Unique Purchases Required', 'give' ),
340
				'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() ) ),
341
				'type' => 'text_small',
342
				'id'   => 'give_donators_gravatars_min_purchases_required',
343
			),
344
			array(
345
				'name'    => __( 'Maximum Gravatars To Show', 'give' ),
346
				'desc'    => __( 'The maximum number of gravatars to show. Leave blank for no limit.', 'give' ),
347
				'type'    => 'text',
348
				'id'      => 'give_donators_gravatars_maximum_number',
349
				'default' => '20',
350
			),
351
			array(
352
				'name' => __( 'Gravatar Visibility', 'give' ),
353
				'desc' => __( 'Only show donators with a Gravatar account', 'give' ),
354
				'id'   => 'give_donators_gravatars_has_gravatar_account',
355
				'type' => 'checkbox',
356
			),
357
			array(
358
				'name' => __( 'Randomize Gravatars', 'give' ),
359
				'desc' => __( 'Randomize the Gravatars', 'give' ),
360
				'id'   => 'give_donators_gravatars_random_gravatars',
361
				'type' => 'checkbox',
362
			),
363
		);
364
365
		return array_merge( $settings, $give_gravatar_settings );
366
	}
367
368
}
369
370
371
/**
372
 * Widget
373
 *
374
 * @since 1.0
375
 */
376
class Give_Donators_Gravatars_Widget extends WP_Widget {
377
378
	/*
379
	 * widget constructor
380
	 */
381
	public function __construct() {
382
383
		$give_label_singular = function_exists( 'give_get_forms_label_singular' ) ? strtolower( give_get_forms_label_singular() ) : null;
384
385
		// widget settings
386
		$widget_ops = array(
387
			'classname'   => 'give-donators-gravatars',
388
			'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 )
389
		);
390
391
		// widget control settings
392
		$control_ops = array(
393
			'width'   => 250,
394
			'height'  => 350,
395
			'id_base' => 'give_gravatars_widget'
396
		);
397
398
		// create the widget
399
		parent::__construct(
400
			'give_donators_gravatars_widget',
401
			__( 'Give Donators Gravatars', 'give' ),
402
			$widget_ops,
403
			$control_ops
404
		);
405
406
	} // end constructor
407
408
409
	/*
410
	 * Outputs the content of the widget
411
	 */
412
	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...
413
		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...
414
415
		//@TODO: Don't extract it!!!
416
		extract( $args );
417
418
		if ( ! is_singular( 'give_forms' ) ) {
419
			return;
420
		}
421
422
		// Variables from widget settings
423
		$title = apply_filters( 'widget_title', $instance['title'] );
424
425
		// Used by themes. Opens the widget
426
		echo $before_widget;
427
428
		// Display the widget title
429
		if ( $title ) {
430
			echo $before_title . $title . $after_title;
431
		}
432
433
		$gravatars = new Give_Donators_Gravatars();
434
435
		echo $gravatars->gravatars( get_the_ID(), null ); // remove title
436
437
		// Used by themes. Closes the widget
438
		echo $after_widget;
439
440
	} // end WIDGET function
441
442
	/*
443
	 * Update function. Processes widget options to be saved
444
	 */
445
	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...
446
447
		$instance = $old_instance;
448
449
		$instance['title'] = strip_tags( $new_instance['title'] );
450
451
		return $instance;
452
453
	} // end UPDATE function
454
455
	/*
456
	 * Form function. Displays the actual form on the widget page
457
	 */
458
	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...
459
460
		// Set up some default widget settings.
461
		$defaults = array(
462
			'title' => '',
463
		);
464
465
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
466
467
		<!-- Title -->
468
		<p>
469
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'give' ) ?></label>
470
			<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']; ?>" />
471
		</p>
472
473
474
		<?php
475
	} // end FORM function
476
477
}
478