WordPoints_Points_Hook   F
last analyzed

Complexity

Total Complexity 63

Size/Duplication

Total Lines 760
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 760
rs 2.9268
c 0
b 0
f 0
wmc 63

27 Methods

Rating   Name   Duplication   Size   Complexity  
B points_type() 0 28 6
A save_instances() 0 9 2
B form_callback() 0 55 5
A get_name() 0 3 1
A next_hook_id_number() 0 9 2
A get_id_base() 0 3 1
A form() 0 21 3
A get_option() 0 8 2
A get_number_by_id() 0 3 1
A set_options() 0 2 1
A set_option() 0 3 1
A get_points() 0 15 3
A generate_description() 0 3 1
A get_number() 0 3 1
A get_field_name() 0 3 1
A get_options() 0 3 1
A get_description() 0 20 4
A get_field_id() 0 3 1
A update() 0 13 3
A set_number() 0 10 3
B update_callback() 0 38 4
A init() 0 22 2
C get_instances() 0 30 7
A the_field_id() 0 3 1
A delete_callback() 0 18 3
A get_id() 0 7 2
A the_field_name() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like WordPoints_Points_Hook often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WordPoints_Points_Hook, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * The points hook class.
5
 *
6
 * @package WordPoints\Points\Hooks
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Points hook class.
12
 *
13
 * This is an abstract class that is extended to create different typs of points
14
 * hooks. It has several methods which may be optionally extended. These methods
15
 * display the form for an instance of a hook [form()], update the instance's
16
 * settings when the form is submitted [update()], generate a description of the hook
17
 * [generate_description()], and retrieve the number of points an instance awards
18
 * [get_points()], respectively. These methods each have a default behaviour, so
19
 * extending each them is optional. (This changed in version 1.5.0, as previously the
20
 * form() and update() methods were abstract, and so had to be extended.)
21
 *
22
 * The rest of the methods are final, and therefore  cannot be extended. A few of
23
 * these are noteworthy, as you must use them for your hook to work. The first of
24
 * these, and the only method which it is absolutely required for every hook to call,
25
 * is the init() method, which your hook must call in it's constructor to initialize
26
 * the hook type. If you extend the form() method, you will also need to use the
27
 * methods to get the correct name [get_form_name() or the_form_name()] and id
28
 * [get_form_id() or the_form_id()] attribute values for your form elements. There is
29
 * one other method, get_instances(), which you will need to use to get a list of the
30
 * instances of the hook, when you are awarding points for example.
31
 *
32
 * The rest of the methods do other interesting acrobatics, most of which you don't
33
 * need to worry about.
34
 *
35
 * The entire Points Hooks API is based heavily on the Widgets API in WordPress Core.
36
 * The main difference of course, is that points hooks aren't displayed in the side-
37
 * bar of your site.
38
 *
39
 * @since 1.0.0
40
 */
41
abstract class WordPoints_Points_Hook {
42
43
	//
44
	// Private Vars.
45
	//
46
47
	/**
48
	 * Root id for all hooks of this type.
49
	 *
50
	 * @since 1.0.0
51
	 *
52
	 * @type string $id_base
53
	 */
54
	private $id_base;
55
56
	/**
57
	 * Name for this hook type.
58
	 *
59
	 * @since 1.0.0
60
	 *
61
	 * @type string $name
62
	 */
63
	private $name;
64
65
	/**
66
	 * The name for this hooks option field.
67
	 *
68
	 * @since 1.0.0
69
	 *
70
	 * @type string $option_name
71
	 */
72
	private $option_name;
73
74
	/**
75
	 * Option array.
76
	 *
77
	 * @since 1.0.0
78
	 *
79
	 * @type array $options
80
	 */
81
	private $options;
82
83
	/**
84
	 * Unique ID number of the current instance.
85
	 *
86
	 * The number will be false when none is set. When set, it will usually be an
87
	 * integer, or numeric string. For network hooks, it is prefixed with 'network_'.
88
	 *
89
	 * @since 1.0.0
90
	 *
91
	 * @type int|string|false $number
92
	 */
93
	private $number = false;
94
95
	//
96
	// Public Non-final Methods.
97
	//
98
99
	/**
100
	 * Get the number of points for a particular instance.
101
	 *
102
	 * @since 1.4.0
103
	 *
104
	 * @param int|string $number The hook number or ID to get the points for.
105
	 *
106
	 * @return int|false The number of points for this instance, or false.
107
	 */
108
	public function get_points( $number = null ) {
109
110
		if ( isset( $number ) ) {
111
			$number = $this->get_number_by_id( $number );
112
		} else {
113
			$number = $this->number;
114
		}
115
116
		$instances = $this->get_instances();
117
118
		if ( isset( $instances[ $number ]['points'] ) ) {
119
			return $instances[ $number ]['points'];
120
		}
121
122
		return false;
123
	}
124
125
	//
126
	// Protected Non-final Methods.
127
	//
128
129
	/**
130
	 * Update a particular instance.
131
	 *
132
	 * This function will verify that the points setting is an integer. If you have
133
	 * other settings that need to be sanitized, you should override this function.
134
	 * Your function should check that $new_instance is set correctly. The newly
135
	 * calculated value of $new_instance should be returned. If false is returned,
136
	 * the instance won't be saved/updated.
137
	 *
138
	 * If the instance does not have the 'points' setting set already, the default
139
	 * will be retrieved from the class's $defaults property, if available.
140
	 *
141
	 * @since 1.0.0
142
	 * @since 1.5.0 No longer abstract.
143
	 *
144
	 * @param array $new_instance New settings for this instance as input by the user
145
	 *                            via form().
146
	 * @param array $old_instance Old settings for this instance.
147
	 *
148
	 * @return array|false Settings to save, or false to cancel saving.
149
	 */
150
	protected function update( $new_instance, $old_instance ) {
151
152
		$new_instance = array_merge( array( 'points' => 0 ), $old_instance, $new_instance );
153
154
		if ( false === wordpoints_posint( $new_instance['points'] ) ) {
155
			if ( isset( $this->defaults['points'] ) ) {
156
				$new_instance['points'] = $this->defaults['points'];
157
			} else {
158
				return false;
159
			}
160
		}
161
162
		return $new_instance;
163
	}
164
165
	/**
166
	 * Display the settings update form.
167
	 *
168
	 * This function will output the test input for the points field. If you have
169
	 * other fields to output, you should override this function.
170
	 *
171
	 * If the instance does not have the 'points' setting set already, the default
172
	 * will be retrieved from the class's $defaults property, if available.
173
	 *
174
	 * @since 1.0.0
175
	 * @since 1.5.0 No longer abstract.
176
	 *
177
	 * @param array $instance Current settings.
178
	 *
179
	 * @return bool Whether the hook has a form.
180
	 */
181
	protected function form( $instance ) {
182
183
		if ( ! isset( $instance['points'] ) ) {
184
185
			if ( isset( $this->defaults['points'] ) ) {
186
				$instance['points'] = $this->defaults['points'];
187
			} else {
188
				$instance['points'] = 0;
189
			}
190
		}
191
192
		?>
193
194
		<p>
195
			<label for="<?php $this->the_field_id( 'points' ); ?>"><?php echo esc_html( $this->options['points_label'] ); ?></label>
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
			<label for="<?php $this->the_field_id( 'points' ); ?>"><?php echo /** @scrutinizer ignore-call */ esc_html( $this->options['points_label'] ); ?></label>
Loading history...
196
			<input class="widefat" name="<?php $this->the_field_name( 'points' ); ?>" id="<?php $this->the_field_id( 'points' ); ?>" type="number" min="0" value="<?php echo wordpoints_posint( $instance['points'] ); ?>" />
197
		</p>
198
199
		<?php
200
201
		return true;
202
	}
203
204
	/**
205
	 * Generate the description for an instance of a points hook.
206
	 *
207
	 * @since 1.4.0
208
	 *
209
	 * @param array $instance The settings for the hook instance, or an emtpy array.
210
	 *
211
	 * @return string The hook instance's description.
212
	 */
213
	protected function generate_description( $instance = array() ) {
214
215
		return $this->options['description'];
216
	}
217
218
	//
219
	// Public Final Methods.
220
	//
221
222
	/**
223
	 * Get the id_base.
224
	 *
225
	 * @since 1.0.0
226
	 *
227
	 * @return string
228
	 */
229
	final public function get_id_base() {
230
231
		return $this->id_base;
232
	}
233
234
	/**
235
	 * Get the ID for the current instance.
236
	 *
237
	 * This function must be used when the instance number is set up, unless the
238
	 * $number parameter is set.
239
	 *
240
	 * @since 1.0.0
241
	 *
242
	 * @param int $number The number of the instance to get the ID for.
243
	 *
244
	 * @return string The unique ID for the instance.
245
	 */
246
	final public function get_id( $number = null ) {
247
248
		if ( ! isset( $number ) ) {
249
			$number = $this->number;
250
		}
251
252
		return $this->id_base . '-' . (int) $number;
253
	}
254
255
	/**
256
	 * Get the number for the current hook instance.
257
	 *
258
	 * The current instance number is only set properly at certain times, such as
259
	 * just after saving the instance. So use with care.
260
	 *
261
	 * @since 1.4.0
262
	 *
263
	 * @return int|string|false The current hook number, the '__i__' placeholder, or
264
	 *                          false.
265
	 */
266
	final public function get_number() {
267
268
		return $this->number;
269
	}
270
271
	/**
272
	 * Set the current instance by number or ID.
273
	 *
274
	 * @since 1.4.0
275
	 *
276
	 * @param int|string $instance_id The number or ID of an instance.
277
	 */
278
	final public function set_number( $instance_id ) {
279
280
		$this->number = $this->get_number_by_id( $instance_id );
281
282
		if ( '__i__' === $this->number ) {
283
			return;
284
		} elseif ( '0' === $this->number ) {
285
			$this->number = 0;
286
		} else {
287
			wordpoints_posint( $this->number );
288
		}
289
	}
290
291
	/**
292
	 * Get the number for a hook by ID.
293
	 *
294
	 * @since 1.0.0
295
	 *
296
	 * @param string $id The id of a hook instance.
297
	 *
298
	 * @return string The number for the hook instance. Prefixed with 'network_' for
299
	 *                network hooks.
300
	 */
301
	final public function get_number_by_id( $id ) {
302
303
		return str_replace( $this->id_base . '-', '', $id );
304
	}
305
306
	/**
307
	 * Get the name of the hook.
308
	 *
309
	 * @since 1.0.0
310
	 *
311
	 * @return string The name of the hook.
312
	 */
313
	final public function get_name() {
314
315
		return $this->name;
316
	}
317
318
	/**
319
	 * Get the options.
320
	 *
321
	 * The options are the arguments for display of this instance.
322
	 *
323
	 * @since 1.0.0
324
	 *
325
	 * @return array
326
	 */
327
	final public function get_options() {
328
329
		return $this->options;
330
	}
331
332
	/**
333
	 * Get a particular option.
334
	 *
335
	 * @since 1.0.0
336
	 *
337
	 * @param string $option The index for the option.
338
	 *
339
	 * @return mixed The option, or null if it doesn't exist.
340
	 */
341
	final public function get_option( $option ) {
342
343
		if ( isset( $this->options[ $option ] ) ) {
344
345
			return $this->options[ $option ];
346
		}
347
348
		return null;
349
	}
350
351
	/**
352
	 * Set the hook options.
353
	 *
354
	 * @since 1.0.0
355
	 * @since 2.0.0 Now declares $options as required to be an array.
356
	 *
357
	 * @param array $options The options for the hook.
358
	 */
359
	final public function set_options( array $options ) {
360
		$this->options = $options;
361
	}
362
363
	/**
364
	 * Set a particular option.
365
	 *
366
	 * @since 1.9.0
367
	 * @since 2.0.0 Now declared final.
368
	 *
369
	 * @param string $option The index for the option.
370
	 * @param mixed  $value  The value to assign to this option.
371
	 */
372
	final public function set_option( $option, $value ) {
373
374
		$this->options[ $option ] = $value;
375
	}
376
377
	/**
378
	 * Calculate the ID number of the next instance of a hook.
379
	 *
380
	 * Each hook can have multiple instances, and to tell them apart each is assigned
381
	 * a consecutive ID number. This function calculates what the next ID number
382
	 * would be.
383
	 *
384
	 * @since 1.0.0
385
	 *
386
	 * @return int
387
	 */
388
	final public function next_hook_id_number() {
389
390
		if ( WordPoints_Points_Hooks::get_network_mode() ) {
391
			$type = 'network';
392
		} else {
393
			$type = 'standard';
394
		}
395
396
		return 1 + max( array_keys( $this->get_instances( $type ) ) + array( 0 ) );
397
	}
398
399
	/**
400
	 * Get all saved instances of this hook.
401
	 *
402
	 * Returns an array of hook instances indexed by ID number. You will need to use
403
	 * this to get the settings for each instance of your hook in your hook() method.
404
	 *
405
	 * By default it returns all instances of a hook, standard and, on multisite
406
	 * installs, network-wide. To get only network-wide hooks, set $type to
407
	 * 'network'. For only standard hooks, 'standard'. For both, 'all' (default).
408
	 * When all instances are being returned, the network instance's ID numbers (the
409
	 * keys) are prefixed with 'network_'.
410
	 *
411
	 * @since 1.0.0
412
	 * @since 1.2.0 The $type parameter was added.
413
	 *
414
	 * @param string $type The type of hooks to retrieve.
415
	 *
416
	 * @return array The saved instances of this hook.
417
	 */
418
	final public function get_instances( $type = 'all' ) {
419
420
		switch ( $type ) {
421
422
			case 'standard':
423
				$instances = wordpoints_get_array_option( $this->option_name );
424
			break;
425
426
			case 'network':
427
				if ( is_multisite() ) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

427
				if ( /** @scrutinizer ignore-call */ is_multisite() ) {
Loading history...
428
					$instances = wordpoints_get_array_option( $this->option_name, 'site' );
429
				} else {
430
					$instances = array();
431
				}
432
			break;
433
434
			case 'all':
435
				$instances = wordpoints_get_array_option( $this->option_name );
436
437
				if ( is_multisite() ) {
438
					foreach ( wordpoints_get_array_option( $this->option_name, 'site' ) as $number => $instance ) {
439
						$instances[ 'network_' . $number ] = $instance;
440
					}
441
				}
442
			break;
443
		}
444
445
		unset( $instances['__i__'] );
446
447
		return $instances;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $instances does not seem to be defined for all execution paths leading up to this point.
Loading history...
448
	}
449
450
	/**
451
	 * Update an instance's settings.
452
	 *
453
	 * Will also create a new instance if no old instance with the ID $number exists.
454
	 *
455
	 * @since 1.0.0
456
	 *
457
	 * @param array $new_instance The new instance of this hooks settings.
458
	 * @param int   $number       The ID number for this hook.
459
	 */
460
	final public function update_callback( $new_instance, $number ) {
461
462
		if ( WordPoints_Points_Hooks::get_network_mode() ) {
463
			$type = 'network';
464
		} else {
465
			$type = 'standard';
466
		}
467
468
		// Get all saved instances of this points hook.
469
		$all_instances = $this->get_instances( $type );
470
471
		$this->set_number( $number );
472
473
		$old_instance = isset( $all_instances[ $this->number ] ) ? $all_instances[ $this->number ] : array();
474
475
		$instance = $this->update( $new_instance, $old_instance );
476
477
		/**
478
		 * Filter a points hook's settings before saving.
479
		 *
480
		 * You can return false to cancel saving (keep the old settings if updating).
481
		 *
482
		 * @since 1.0.0
483
		 *
484
		 * @param array                  $instance     The updated instance of the
485
		 *        hook as returned by its update() method.
486
		 * @param array                  $new_instance The unfiltered instance of the
487
		 *        hook as input by the user.
488
		 * @param array                  $old_instance The old instance of the hook.
489
		 * @param WordPoints_Points_Hook $hook         The hook object.
490
		 */
491
		$instance = apply_filters( 'wordpoints_points_hook_update_callback', $instance, $new_instance, $old_instance, $this );
492
493
		if ( false !== $instance ) {
494
			$all_instances[ $this->number ] = $instance;
495
		}
496
497
		$this->save_instances( $all_instances );
498
	}
499
500
	/**
501
	 * Delete an instance of a hook.
502
	 *
503
	 * @since 1.0.0
504
	 *
505
	 * @param string $hook_id The ID of the instance to delete.
506
	 */
507
	final public function delete_callback( $hook_id ) {
508
509
		if ( WordPoints_Points_Hooks::get_network_mode() ) {
510
			$type = 'network';
511
		} else {
512
			$type = 'standard';
513
		}
514
515
		// Get all saved instances of this points hook.
516
		$all_instances = $this->get_instances( $type );
517
518
		$number = $this->get_number_by_id( $hook_id );
519
520
		if ( isset( $all_instances[ $number ] ) ) {
521
522
			unset( $all_instances[ $number ] );
523
524
			$this->save_instances( $all_instances );
525
		}
526
	}
527
528
	/**
529
	 * Generate the control form.
530
	 *
531
	 * @since 1.0.0
532
	 *
533
	 * @param int $number The number of the hook instance to display the form for.
534
	 *
535
	 * @return bool|null Whether the form was displayed.
536
	 */
537
	final public function form_callback( $number ) {
538
539
		$this->set_number( $number );
540
541
		if ( WordPoints_Points_Hooks::get_network_mode() ) {
542
			$type = 'network';
543
		} else {
544
			$type = 'standard';
545
		}
546
547
		$all_instances = $this->get_instances( $type );
548
549
		if ( 0 === $this->number || '0' === $this->number ) {
550
551
			// We echo out a form where 'number' can be set later.
552
			$this->set_number( '__i__' );
553
			$instance = array();
554
555
		} else {
556
557
			$instance = $all_instances[ $this->number ];
558
		}
559
560
		/**
561
		 * Filter the points hook instance before display by form().
562
		 *
563
		 * Returning false will cancel display of the form.
564
		 *
565
		 * @since 1.0.0
566
		 *
567
		 * @param array                  $instance The settings for this instance.
568
		 * @param WordPoints_Points_Hook $hook     The hook object.
569
		 */
570
		$instance = apply_filters( 'wordpoints_points_hook_form_callback', $instance, $this );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

570
		$instance = /** @scrutinizer ignore-call */ apply_filters( 'wordpoints_points_hook_form_callback', $instance, $this );
Loading history...
571
572
		if ( false !== $instance ) {
573
574
			$has_form = $this->form( $instance );
575
576
			/**
577
			 * Inside the points hook form.
578
			 *
579
			 * You can use this to add extra fields in the hook form. The hook fires
580
			 * after the form() method has been called.
581
			 *
582
			 * @param bool                   $has_form Whether the hook has a form.
583
			 * @param array                  $instance Settings for this instance.
584
			 * @param WordPoints_Points_Hook $hook     The hook object.
585
			 */
586
			do_action( 'wordpoints_in_points_hook_form', $has_form, $instance, $this );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

586
			/** @scrutinizer ignore-call */ 
587
   do_action( 'wordpoints_in_points_hook_form', $has_form, $instance, $this );
Loading history...
587
588
			return $has_form;
589
		}
590
591
		return null;
592
	}
593
594
	/**
595
	 * Constructs name attributes for use in form() fields.
596
	 *
597
	 * This function should be used in form() methods to create name attributes for
598
	 * fields to be saved by update(). Note that the returned value is escaped with
599
	 * esc_attr().
600
	 *
601
	 * @since 1.0.0
602
	 *
603
	 * @see WordPoints_Points_Hook::the_field_name()
604
	 *
605
	 * @param string $field_name Field name.
606
	 *
607
	 * @return string Name attribute for $field_name.
608
	 */
609
	final public function get_field_name( $field_name ) {
610
611
		return esc_attr( 'hook-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']' );
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

611
		return /** @scrutinizer ignore-call */ esc_attr( 'hook-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']' );
Loading history...
Bug introduced by
Are you sure $this->number of type integer|string|false can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

611
		return esc_attr( 'hook-' . $this->id_base . '[' . /** @scrutinizer ignore-type */ $this->number . '][' . $field_name . ']' );
Loading history...
612
	}
613
614
	/**
615
	 * Echo a name attribute for use in form() fields.
616
	 *
617
	 * @since 1.0.0
618
	 *
619
	 * @uses WordPoints_Points_Hook::get_field_name()
620
	 *
621
	 * @param string $field_name The field name.
622
	 */
623
	final public function the_field_name( $field_name ) {
624
625
		echo esc_attr( $this->get_field_name( $field_name ) );
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

625
		echo /** @scrutinizer ignore-call */ esc_attr( $this->get_field_name( $field_name ) );
Loading history...
626
	}
627
628
	/**
629
	 * Constructs id attributes for use in form() fields.
630
	 *
631
	 * This function should be used in form() methods to create id attributes for
632
	 * fields to be saved by update(). Note that the returned value is escaped with
633
	 * esc_attr().
634
	 *
635
	 * @since 1.0.0
636
	 *
637
	 * @seee WordPoints_Points_Hook::the_field_id()
638
	 *
639
	 * @param string $field_name Field name.
640
	 *
641
	 * @return string ID attribute for $field_name.
642
	 */
643
	final public function get_field_id( $field_name ) {
644
645
		return esc_attr( 'hook-' . $this->id_base . '-' . $this->number . '-' . $field_name );
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

645
		return /** @scrutinizer ignore-call */ esc_attr( 'hook-' . $this->id_base . '-' . $this->number . '-' . $field_name );
Loading history...
Bug introduced by
Are you sure $this->number of type integer|string|false can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

645
		return esc_attr( 'hook-' . $this->id_base . '-' . /** @scrutinizer ignore-type */ $this->number . '-' . $field_name );
Loading history...
646
	}
647
648
	/**
649
	 * Echo an id attribute for use in form() fields.
650
	 *
651
	 * @since 1.0.0
652
	 *
653
	 * @uses WordPoints_Points_Hook::get_field_id()
654
	 *
655
	 * @param string $field_name The field name.
656
	 */
657
	final public function the_field_id( $field_name ) {
658
659
		echo esc_attr( $this->get_field_id( $field_name ) );
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

659
		echo /** @scrutinizer ignore-call */ esc_attr( $this->get_field_id( $field_name ) );
Loading history...
660
	}
661
662
	/**
663
	 * Retrieve the description for the hook.
664
	 *
665
	 * @since 1.4.0
666
	 *
667
	 * @param string $type The type of description to return. If 'generated', a user-
668
	 *                     defined description will not be returned.
669
	 *
670
	 * @return string The hook's description.
671
	 */
672
	final public function get_description( $type = 'any' ) {
673
674
		$instances = $this->get_instances();
675
676
		if ( 'generated' !== $type && ! empty( $instances[ $this->number ]['_description'] ) ) {
677
			return $instances[ $this->number ]['_description'];
678
		}
679
680
		$instance = ( isset( $instances[ $this->number ] ) ) ? $instances[ $this->number ] : array();
681
682
		/**
683
		 * Filter the description for a points hook.
684
		 *
685
		 * @since 1.4.0
686
		 *
687
		 * @param string                 $description The description.
688
		 * @param WordPoints_Points_Hook $hook        The points hook object.
689
		 * @param array                  $instance    The settings for this instance.
690
		 */
691
		return apply_filters( 'wordpoints_points_hook_description', $this->generate_description( $instance ), $this, $instance );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

691
		return /** @scrutinizer ignore-call */ apply_filters( 'wordpoints_points_hook_description', $this->generate_description( $instance ), $this, $instance );
Loading history...
692
	}
693
694
	/**
695
	 * Get the points type for an instance.
696
	 *
697
	 * @since 1.0.0
698
	 *
699
	 * @param int|string $number The instance number. Prefixed by 'network_' for
700
	 *                           network-wide hooks.
701
	 *
702
	 * @return string|false The type of points, or false if none found.
703
	 */
704
	final public function points_type( $number = null ) {
705
706
		$network_mode = false;
707
708
		if ( ! isset( $number ) ) {
709
710
			$number = $this->number;
711
712
		} elseif ( is_string( $number ) && 'network_' === substr( $number, 0, 8 ) ) {
713
714
			$network_mode = true;
715
			$number       = (int) substr( $number, 8 );
716
		}
717
718
		$current_mode = WordPoints_Points_Hooks::get_network_mode();
719
720
		if ( $current_mode !== $network_mode ) {
721
			WordPoints_Points_Hooks::set_network_mode( $network_mode );
722
		}
723
724
		$points_type = WordPoints_Points_Hooks::get_points_type( $this->get_id( $number ) );
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string and false; however, parameter $number of WordPoints_Points_Hook::get_id() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

724
		$points_type = WordPoints_Points_Hooks::get_points_type( $this->get_id( /** @scrutinizer ignore-type */ $number ) );
Loading history...
725
726
		// Reset network mode if it was changed.
727
		if ( $current_mode !== $network_mode ) {
728
			WordPoints_Points_Hooks::set_network_mode( $current_mode );
729
		}
730
731
		return $points_type;
732
	}
733
734
	//
735
	// Protected Final Methods.
736
	//
737
738
	/**
739
	 * Initializer.
740
	 *
741
	 * You need to call this in your constructor.
742
	 *
743
	 * @since 1.0.0
744
	 *
745
	 * @param string $name    Name for the hook displayed on the configuration page.
746
	 * @param array  $options {
747
	 *        Optional arguments for the hooks' display
748
	 *
749
	 *        @type string $description  Shown on the configuration page.
750
	 *        @type int    $width        The width of your hook form. Required if
751
	 *              more than 250px, but you should stay within that if possible.
752
	 *        @type string $points_label The label for the points field. Used by the
753
	 *              default form function. The default is 'Points:'. If you are
754
	 *              overriding that function, you can ignore this option.
755
	 * }
756
	 */
757
	final protected function init( $name, array $options = array() ) {
758
759
		$this->id_base     = strtolower( get_class( $this ) );
760
		$this->name        = $name;
761
		$this->option_name = 'wordpoints_hook-' . $this->id_base;
762
763
		// Option names can only be 191 characters long.
764
		if ( isset( $this->option_name{191} ) ) {
765
			_doing_it_wrong( __METHOD__, sprintf( 'Points hook class names cannot be longer than 175 characters, %s is %s character(s) too long.', esc_html( $this->id_base ), (int) strlen( $this->id_base ) - 175 ), '1.5.0' );
0 ignored issues
show
Bug introduced by
The function _doing_it_wrong was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

765
			/** @scrutinizer ignore-call */ 
766
   _doing_it_wrong( __METHOD__, sprintf( 'Points hook class names cannot be longer than 175 characters, %s is %s character(s) too long.', esc_html( $this->id_base ), (int) strlen( $this->id_base ) - 175 ), '1.5.0' );
Loading history...
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

765
			_doing_it_wrong( __METHOD__, sprintf( 'Points hook class names cannot be longer than 175 characters, %s is %s character(s) too long.', /** @scrutinizer ignore-call */ esc_html( $this->id_base ), (int) strlen( $this->id_base ) - 175 ), '1.5.0' );
Loading history...
766
			return;
767
		}
768
769
		$this->options = array_merge(
770
			array(
771
				'width'        => 250,
772
				'description'  => '',
773
				'points_label' => __( 'Points:', 'wordpoints' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

773
				'points_label' => /** @scrutinizer ignore-call */ __( 'Points:', 'wordpoints' ),
Loading history...
774
			)
775
			, $options
776
		);
777
778
		$this->options['_classname'] = $this->option_name;
779
	}
780
781
	//
782
	// Private Final Methods.
783
	//
784
785
	/**
786
	 * Save the settings of all the hook's instances.
787
	 *
788
	 * @since 1.0.0
789
	 *
790
	 * @param array $instances All settings, indexed by instance number.
791
	 */
792
	final private function save_instances( $instances ) {
793
794
		// This needs to start at 1.
795
		unset( $instances[0] );
796
797
		if ( WordPoints_Points_Hooks::get_network_mode() ) {
798
			update_site_option( $this->option_name, $instances );
0 ignored issues
show
Bug introduced by
The function update_site_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

798
			/** @scrutinizer ignore-call */ 
799
   update_site_option( $this->option_name, $instances );
Loading history...
799
		} else {
800
			update_option( $this->option_name, $instances );
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

800
			/** @scrutinizer ignore-call */ 
801
   update_option( $this->option_name, $instances );
Loading history...
801
		}
802
	}
803
804
} // class WordPoints_Points_Hook
805
806
// EOF
807