Completed
Push — master ( 873ec4...144c08 )
by J.D.
03:34
created

functions.php ➔ wordpoints_entity_user_can_view()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
eloc 13
nc 3
nop 3
dl 0
loc 34
rs 8.8571
1
<?php
2
3
/**
4
 * Main functions.
5
 *
6
 * @package wordpoints-hooks-api
7
 * @since 1.0.0
8
 */
9
10
/**
11
 * Initialize the hooks API.
12
 *
13
 * @since 1.0.0
14
 *
15
 * @WordPress\action wordpoints_modules_loaded
16
 */
17
function wordpoints_init_hooks() {
18
19
	$hooks = wordpoints_hooks();
20
21
	// Just accessing this causes it to be initialized. We need to do that so
22
	// the actions will be registered and hooked up. The rest of the API can be
23
	// lazy-loaded as it is needed.
24
	$hooks->actions;
25
}
26
27
/**
28
 * Register hook reactors when the reactors registry is initialized.
29
 *
30
 * @since 1.0.0
31
 *
32
 * @WordPress\action wordpoints_init_app_registry-hooks-reactors
33
 *
34
 * @param WordPoints_Class_Registry_Persistent $reactors The reactors registry.
35
 */
36
function wordpoints_hook_reactors_init( $reactors ) {
37
38
	$reactors->register( 'points', 'WordPoints_Hook_Reactor_Points' );
39
40
	$router = wordpoints_hooks()->router;
41
42
	$router->add_action_type_to_reactor( 'fire', 'points', 'award' );
43
	$router->add_action_type_to_reactor( 'toggle_on', 'points', 'award' );
44
	$router->add_action_type_to_reactor( 'toggle_off', 'points', 'reverse' );
45
}
46
47
/**
48
 * Register hook reaction stores when the reaction store registry is initialized.
49
 *
50
 * @since 1.0.0
51
 *
52
 * @WordPress\action wordpoints_init_app_registry-hooks-reaction_stores
53
 *
54
 * @param WordPoints_Class_Registry_Children $reaction_stores The store registry.
55
 */
56
function wordpoints_hook_reaction_stores_init( $reaction_stores ) {
57
58
	$reaction_stores->register(
59
		'standard'
60
		, 'points'
61
		, 'WordPoints_Hook_Reaction_Store_Options'
62
	);
63
64
	if ( is_wordpoints_network_active() ) {
65
		$reaction_stores->register(
66
			'network'
67
			, 'points'
68
			, 'WordPoints_Hook_Reaction_Store_Options_Network'
69
		);
70
	}
71
}
72
73
/**
74
 * Register hook extension when the extension registry is initialized.
75
 *
76
 * @since 1.0.0
77
 *
78
 * @WordPress\action wordpoints_init_app_registry-hooks-extensions
79
 *
80
 * @param WordPoints_Class_Registry_Persistent $extensions The extension registry.
81
 */
82
function wordpoints_hook_extension_init( $extensions ) {
83
84
	$extensions->register( 'blocker', 'WordPoints_Hook_Extension_Blocker' );
85
	$extensions->register( 'conditions', 'WordPoints_Hook_Extension_Conditions' );
86
	$extensions->register( 'periods', 'WordPoints_Hook_Extension_Periods' );
87
}
88
89
/**
90
 * Register hook conditions when the conditions registry is initialized.
91
 *
92
 * @since 1.0.0
93
 *
94
 * @WordPress\action wordpoints_init_app_registry-hooks-conditions
95
 *
96
 * @param WordPoints_Class_Registry_Children $conditions The conditions registry.
97
 */
98
function wordpoints_hook_conditions_init( $conditions ) {
99
100
	$conditions->register(
101
		'text'
102
		, 'contains'
103
		, 'WordPoints_Hook_Condition_String_Contains'
104
	);
105
106
	$conditions->register(
107
		'text'
108
		, 'equals'
109
		, 'WordPoints_Hook_Condition_Equals'
110
	);
111
112
	$conditions->register(
113
		'entity'
114
		, 'equals'
115
		, 'WordPoints_Hook_Condition_Equals'
116
	);
117
118
	$conditions->register(
119
		'entity_array'
120
		, 'contains'
121
		, 'WordPoints_Hook_Condition_Entity_Array_Contains'
122
	);
123
}
124
125
/**
126
 * Register hook actions when the action registry is initialized.
127
 *
128
 * @since 1.0.0
129
 *
130
 * @WordPress\action wordpoints_init_app_registry-hooks-actions
131
 *
132
 * @param WordPoints_Hook_Actions $actions The action registry.
133
 */
134
function wordpoints_hook_actions_init( $actions ) {
135
136
	$actions->register(
137
		'comment_approve'
138
		, 'WordPoints_Hook_Action'
139
		, array(
140
			'action' => 'transition_comment_status',
141
			'data'   => array(
142
				'arg_index'    => array( 'comment' => 2 ),
143
				'requirements' => array( 0 => 'approved' ),
144
			),
145
		)
146
	);
147
148
	$actions->register(
149
		'comment_new'
150
		, 'WordPoints_Hook_Action_Comment_New'
151
		, array(
152
			'action' => 'wp_insert_comment',
153
			'data'   => array(
154
				'arg_index' => array( 'comment' => 1 ),
155
			),
156
		)
157
	);
158
159
	$actions->register(
160
		'comment_deapprove'
161
		, 'WordPoints_Hook_Action'
162
		, array(
163
			'action' => 'transition_comment_status',
164
			'data'   => array(
165
				'arg_index' => array( 'comment' => 2 ),
166
				'requirements' => array( 1 => 'approved' ),
167
			),
168
		)
169
	);
170
171
	// This works for all post types except attachments.
172
	$actions->register(
173
		'post_publish'
174
		, 'WordPoints_Hook_Action_Post_Publish'
175
		, array(
176
			'action' => 'transition_post_status',
177
			'data'   => array(
178
				'arg_index' => array( 'post' => 2 ),
179
				'requirements' => array( 0 => 'publish' ),
180
			),
181
		)
182
	);
183
184
	$actions->register(
185
		'post_depublish'
186
		, 'WordPoints_Hook_Action_Post_Publish'
187
		, array(
188
			'action' => 'transition_post_status',
189
			'data'   => array(
190
				'arg_index' => array( 'post' => 2 ),
191
				'requirements' => array( 1 => 'publish' ),
192
			),
193
		)
194
	);
195
196
	$actions->register(
197
		'add_attachment'
198
		, 'WordPoints_Hook_Action'
199
		, array(
200
			'action' => 'add_attachment',
201
			'data'   => array(
202
				'arg_index' => array( 'post\attachment' => 0 ),
203
			),
204
		)
205
	);
206
207
	$actions->register(
208
		'post_delete'
209
		, 'WordPoints_Hook_Action'
210
		, array(
211
			'action' => 'delete_post',
212
			'data'   => array(
213
				'arg_index' => array( 'post' => 0 ),
214
			),
215
		)
216
	);
217
218
	$actions->register(
219
		'user_register'
220
		, 'WordPoints_Hook_Action'
221
		, array(
222
			'action' => 'user_register',
223
			'data'   => array(
224
				'arg_index' => array( 'user' => 0 ),
225
			),
226
		)
227
	);
228
229
	$actions->register(
230
		'user_delete'
231
		, 'WordPoints_Hook_Action'
232
		, array(
233
			'action' => is_multisite() ? 'wpmu_delete_user' : 'delete_user',
234
			'data'   => array(
235
				'arg_index' => array( 'user' => 0 ),
236
			),
237
		)
238
	);
239
240
	$actions->register(
241
		'user_visit'
242
		, 'WordPoints_Hook_Action'
243
		, array(
244
			'action' => 'wp',
245
		)
246
	);
247
}
248
249
/**
250
 * Register hook events when the event registry is initialized.
251
 *
252
 * @since 1.0.0
253
 *
254
 * @WordPress\action wordpoints_init_app_registry-hooks-events
255
 *
256
 * @param WordPoints_Hook_Events $events The event registry.
257
 */
258
function wordpoints_hook_events_init( $events ) {
259
260
	$events->register(
261
		'user_register'
262
		, 'WordPoints_Hook_Event_User_Register'
263
		, array(
264
			'actions' => array(
265
				'toggle_on'  => 'user_register',
266
				'toggle_off' => 'user_delete',
267
			),
268
			'args' => array(
269
				'user' => 'WordPoints_Hook_Arg',
270
			),
271
		)
272
	);
273
274
	$events->register(
275
		'user_visit'
276
		, 'WordPoints_Hook_Event_User_Visit'
277
		, array(
278
			'actions' => array(
279
				'fire' => 'user_visit',
280
			),
281
			'args' => array(
282
				'current:user' => 'WordPoints_Hook_Arg_Current_User',
283
			),
284
		)
285
	);
286
287
	// Register events for all of the public post types.
288
	$post_types = get_post_types( array( 'public' => true ) );
289
290
	/**
291
	 * Filter which post types to register hook events for.
292
	 *
293
	 * @since 1.0.0
294
	 *
295
	 * @param string[] The post type slugs ("names").
296
	 */
297
	$post_types = apply_filters( 'wordpoints_register_hook_events_for_post_types', $post_types );
298
299
	foreach ( $post_types as $slug ) {
300
		wordpoints_register_post_type_hook_events( $slug );
301
	}
302
303
	if ( is_multisite() ) {
304
305
		$event_slugs = array(
306
			'user_visit',
307
			'user_register',
308
		);
309
310
		foreach ( $event_slugs as $event_slug ) {
311
			// TODO network hooks
312
			$events->args->register(
313
				$event_slug
314
				, 'current:site'
315
				, 'WordPoints_Hook_Arg_Current_Site'
316
			);
317
		}
318
	}
319
}
320
321
/**
322
 * Register sub-apps when the Entities app is initialized.
323
 *
324
 * @since 1.0.0
325
 *
326
 * @WordPress\action wordpoints_init_app-entities
327
 *
328
 * @param WordPoints_App_Registry $entities The entities app.
329
 */
330
function wordpoints_entities_app_init( $entities ) {
331
332
	$entities->sub_apps->register( 'children', 'WordPoints_Class_Registry_Children' );
0 ignored issues
show
Documentation introduced by
The property $sub_apps is declared protected in WordPoints_App. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
333
	$entities->sub_apps->register( 'contexts', 'WordPoints_Class_Registry' );
0 ignored issues
show
Documentation introduced by
The property $sub_apps is declared protected in WordPoints_App. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
334
}
335
336
/**
337
 * Register entity contexts when the registry is initialized.
338
 *
339
 * @since 1.0.0
340
 *
341
 * @WordPress\action wordpoints_init_app_registry-entities-contexts
342
 *
343
 * @param WordPoints_Class_Registry $contexts The entity context registry.
344
 */
345
function wordpoints_entity_contexts_init( $contexts ) {
346
347
	$contexts->register( 'network', 'WordPoints_Entity_Context_Network' );
348
	$contexts->register( 'site', 'WordPoints_Entity_Context_Site' );
349
}
350
351
/**
352
 * Register entities when the entities app is initialized.
353
 *
354
 * @since 1.0.0
355
 *
356
 * @WordPress\action wordpoints_init_app_registry-apps-entities
357
 *
358
 * @param WordPoints_App_Registry $entities The entities app.
359
 */
360
function wordpoints_entities_init( $entities ) {
361
362
	$children = $entities->children;
0 ignored issues
show
Documentation introduced by
The property children does not exist on object<WordPoints_App_Registry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
363
364
	$entities->register( 'user', 'WordPoints_Entity_User' );
365
	$children->register( 'user', 'roles', 'WordPoints_Entity_User_Roles' );
366
367
	$entities->register( 'user_role', 'WordPoints_Entity_User_Role' );
368
	$children->register( 'user_role', 'name', 'WordPoints_Entity_User_Role_Name' );
369
370
	// Register entities for all of the public post types.
371
	$post_types = get_post_types( array( 'public' => true ) );
372
373
	/**
374
	 * Filter which post types to register entities for.
375
	 *
376
	 * @since 1.0.0
377
	 *
378
	 * @param string[] The post type slugs ("names").
379
	 */
380
	$post_types = apply_filters( 'wordpoints_register_entities_for_post_types', $post_types );
381
382
	foreach ( $post_types as $slug ) {
383
		wordpoints_register_post_type_entities( $slug );
384
	}
385
386
	// Register entities for all of the public taxonomies.
387
	$taxonomies = get_taxonomies( array( 'public' => true ) );
388
389
	/**
390
	 * Filter which taxonomies to register entities for.
391
	 *
392
	 * @since 1.0.0
393
	 *
394
	 * @param string[] The taxonomy slugs.
395
	 */
396
	$taxonomies = apply_filters( 'wordpoints_register_entities_for_taxonomies', $taxonomies );
397
398
	foreach ( $taxonomies as $slug ) {
399
		wordpoints_register_taxonomy_entities( $slug );
400
	}
401
}
402
403
/**
404
 * Register the entities for a post type.
405
 *
406
 * @since 1.0.0
407
 *
408
 * @param string $slug The slug of the post type.
409
 */
410
function wordpoints_register_post_type_entities( $slug ) {
411
412
	$entities = wordpoints_entities();
413
	$children = $entities->children;
0 ignored issues
show
Documentation introduced by
The property children does not exist on object<WordPoints_App_Registry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
414
415
	$entities->register( "post\\{$slug}", 'WordPoints_Entity_Post' );
416
	$children->register( "post\\{$slug}", 'author', 'WordPoints_Entity_Post_Author' );
417
418
	$supports = get_all_post_type_supports( $slug );
419
420
	if ( isset( $supports['editor'] ) ) {
421
		$children->register( "post\\{$slug}", 'content', 'WordPoints_Entity_Post_Content' );
422
	}
423
424
	if ( isset( $supports['comments'] ) ) {
425
		$entities->register( "comment\\{$slug}", 'WordPoints_Entity_Comment' );
426
		$children->register( "comment\\{$slug}", "post\\{$slug}", 'WordPoints_Entity_Comment_Post' );
427
		$children->register( "comment\\{$slug}", 'author', 'WordPoints_Entity_Comment_Author' );
428
	}
429
430
	foreach ( get_object_taxonomies( $slug ) as $taxonomy_slug ) {
431
		$children->register( "post\\{$slug}", "terms\\{$taxonomy_slug}", 'WordPoints_Entity_Post_Terms' );
432
	}
433
434
	/**
435
	 * Fired when registering the entities for a post type.
436
	 *
437
	 * @since 1.0.0
438
	 *
439
	 * @param string $slug The slug ("name") of the post type.
440
	 */
441
	do_action( 'wordpoints_register_post_type_entities', $slug );
442
}
443
444
/**
445
 * Register the hook events for a post type.
446
 *
447
 * @since 1.0.0
448
 *
449
 * @param string $slug The slug of the post type.
450
 */
451
function wordpoints_register_post_type_hook_events( $slug ) {
452
453
	$event_slugs = array();
454
455
	$events = wordpoints_hooks()->events;
456
457
	if ( 'attachment' === $slug ) {
458
459
		$event_slugs[] = 'media_upload';
460
461
		$events->register(
462
			'media_upload'
463
			, 'WordPoints_Hook_Event_Media_Upload'
464
			, array(
465
				'actions' => array(
466
					'toggle_on'  => 'add_attachment',
467
					'toggle_off' => 'post_delete',
468
				),
469
				'args'    => array(
470
					"post\\{$slug}" => 'WordPoints_Hook_Arg_Dynamic',
471
				),
472
			)
473
		);
474
475 View Code Duplication
	} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
476
477
		$event_slugs[] = "post_publish\\{$slug}";
478
479
		$events->register(
480
			"post_publish\\{$slug}"
481
			, 'WordPoints_Hook_Event_Post_Publish'
482
			, array(
483
				'actions' => array(
484
					'toggle_on'  => 'post_publish',
485
					'toggle_off' => array( 'post_depublish', 'post_delete' ),
486
				),
487
				'args'    => array(
488
					"post\\{$slug}" => 'WordPoints_Hook_Arg_Dynamic',
489
				),
490
			)
491
		);
492
	}
493
494 View Code Duplication
	if ( post_type_supports( $slug, 'comments' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
495
496
		$event_slugs[] = "comment_leave\\{$slug}";
497
498
		$events->register(
499
			"comment_leave\\{$slug}"
500
			, 'WordPoints_Hook_Event_Comment_Leave'
501
			, array(
502
				'actions' => array(
503
					'toggle_on'  => array( 'comment_approve', 'comment_new' ),
504
					'toggle_off' => 'comment_deapprove',
505
				),
506
				'args' => array(
507
					"comment\\{$slug}" => 'WordPoints_Hook_Arg_Dynamic',
508
				),
509
			)
510
		);
511
	}
512
513
	if ( is_multisite() ) {
514
		foreach ( $event_slugs as $event_slug ) {
515
			$events->args->register(
516
				$event_slug
517
				, 'current:site'
518
				, 'WordPoints_Hook_Arg_Current_Site'
519
			);
520
		}
521
	}
522
523
	/**
524
	 * Fires when registering the hook events for a post type.
525
	 *
526
	 * @since 1.0.0
527
	 *
528
	 * @param string $slug The slug ("name") of the post type.
529
	 */
530
	do_action( 'wordpoints_register_post_type_hook_events', $slug );
531
}
532
533
/**
534
 * Register the entities for a taxonomy.
535
 *
536
 * @since 1.0.0
537
 *
538
 * @param string $slug The slug of the taxonomy.
539
 */
540
function wordpoints_register_taxonomy_entities( $slug ) {
541
542
	$entities = wordpoints_entities();
543
	$children = $entities->children;
0 ignored issues
show
Documentation introduced by
The property children does not exist on object<WordPoints_App_Registry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
544
545
	$entities->register( "term\\{$slug}", 'WordPoints_Entity_Term' );
546
	$children->register( "term\\{$slug}", 'id', 'WordPoints_Entity_Term_Id' );
547
548
	/**
549
	 * Fired when registering the entities for a taxonomy.
550
	 *
551
	 * @since 1.0.0
552
	 *
553
	 * @param string $slug The taxonomy's slug.
554
	 */
555
	do_action( 'wordpoints_register_taxonomy_entities', $slug );
556
}
557
558
/**
559
 * Register the data types with the data types registry is initialized.
560
 *
561
 * @since 1.0.0
562
 *
563
 * @WordPress\action wordpoints_init_app_registry-apps-data_types
564
 *
565
 * @param WordPoints_Class_RegistryI $data_types The data types registry.
566
 */
567
function wordpoints_data_types_init( $data_types ) {
568
569
	$data_types->register( 'integer', 'WordPoints_Data_Type_Integer' );
570
	$data_types->register( 'text', 'WordPoints_Data_Type_Text' );
571
}
572
573
/**
574
 * Check whether a user can view a points log.
575
 *
576
 * @since 1.0.0
577
 *
578
 * @WordPress\filter wordpoints_user_can_view_points_log
579
 *
580
 * @param bool   $can_view Whether the user can view the points log.
581
 * @param object $log      The points log's data.
582
 *
583
 * @return bool Whether the user can view the points log.
584
 */
585
function wordpoints_hooks_user_can_view_points_log( $can_view, $log ) {
586
587
	if ( ! $can_view ) {
588
		return $can_view;
589
	}
590
591
	$user_id = get_current_user_id();
592
593
	$event_slug = $log->log_type;
594
595
	/** @var WordPoints_Hook_Arg $arg */
596
	foreach ( wordpoints_hooks()->events->args->get_children( $event_slug ) as $slug => $arg ) {
597
598
		$value = wordpoints_get_points_log_meta( $log->id, $slug, true );
599
600
		if ( ! $value ) {
601
			continue;
602
		}
603
604
		$can_view = wordpoints_entity_user_can_view(
605
			$user_id
606
			, $arg->get_entity_slug()
607
			, $value
608
		);
609
610
		if ( ! $can_view ) {
611
			break;
612
		}
613
	}
614
615
	return $can_view;
616
}
617
618
/**
619
 * Check whether a user can view an entity.
620
 *
621
 * @since 1.0.0
622
 *
623
 * @param int    $user_id     The user ID.
624
 * @param string $entity_slug The slug of the entity type.
625
 * @param mixed  $entity_id   The entity ID.
626
 *
627
 * @return bool Whether the user can view this entity.
628
 */
629
function wordpoints_entity_user_can_view( $user_id, $entity_slug, $entity_id ) {
630
631
	$entity = wordpoints_entities()->get( $entity_slug );
632
633
	// If this entity type is not found, we have no way of determining whether it is
634
	// safe for the user to view it.
635
	if ( ! ( $entity instanceof WordPoints_Entity ) ) {
636
		return false;
637
	}
638
639
	$can_view = true;
640
641
	if ( $entity instanceof WordPoints_Entity_Restricted_VisibilityI ) {
642
		$can_view = $entity->user_can_view( $user_id, $entity_id );
643
	}
644
645
	/**
646
	 * Filter whether a user can view an entity.
647
	 *
648
	 * @since 1.0.0
649
	 *
650
	 * @param bool              $can_view  Whether the user can view the entity.
651
	 * @param int               $user_id   The user ID.
652
	 * @param int               $entity_id The entity ID.
653
	 * @param WordPoints_Entity $entity    The entity object.
654
	 */
655
	return apply_filters(
656
		'wordpoints_entity_user_can_view'
657
		, $can_view
658
		, $user_id
659
		, $entity_id
660
		, $entity
661
	);
662
}
663
664
/**
665
 * Get the main WordPoints app.
666
 *
667
 * @since 1.0.0
668
 *
669
 * @return WordPoints_App The main WordPoints app.
670
 */
671
function wordpoints_apps() {
672
673
	if ( ! isset( WordPoints_App::$main ) ) {
674
		WordPoints_App::$main = new WordPoints_App( 'apps' );
675
	}
676
677
	return WordPoints_App::$main;
678
}
679
680
/**
681
 * Get the hooks app.
682
 *
683
 * @since 1.0.0
684
 *
685
 * @return WordPoints_Hooks The hooks app.
686
 */
687
function wordpoints_hooks() {
688
689
	if ( ! isset( WordPoints_App::$main ) ) {
690
		wordpoints_apps();
691
	}
692
693
	return WordPoints_App::$main->hooks;
694
}
695
696
/**
697
 * Get the entities app.
698
 *
699
 * @since 1.0.0
700
 *
701
 * @return WordPoints_App_Registry The hooks app.
702
 */
703
function wordpoints_entities() {
704
705
	if ( ! isset( WordPoints_App::$main ) ) {
706
		wordpoints_apps();
707
	}
708
709
	return WordPoints_App::$main->entities;
710
}
711
712
/**
713
 * Register sub apps when the apps app is initialized.
714
 *
715
 * @since 1.0.0
716
 *
717
 * @WordPress\action wordpoints_init_app-apps
718
 *
719
 * @param WordPoints_App $app The main apps app.
720
 */
721
function wordpoints_apps_init( $app ) {
722
723
	$apps = $app->sub_apps;
724
725
	$apps->register( 'hooks', 'WordPoints_Hooks' );
726
	$apps->register( 'entities', 'WordPoints_App_Registry' );
727
	$apps->register( 'data_types', 'WordPoints_Class_Registry' );
728
}
729
730
/**
731
 * Construct a class with a variable number of args.
732
 *
733
 * @since 1.0.0
734
 *
735
 * @param string $class_name The name of the class to construct.
736
 * @param array  $args       Up to 4 args to pass to the constructor.
737
 *
738
 * @return object|false The constructed object, or false if to many args were passed.
739
 */
740
function wordpoints_construct_class_with_args( $class_name, array $args ) {
741
742
	switch ( count( $args ) ) {
743
		case 0:
744
			return new $class_name();
745
		case 1:
746
			return new $class_name( $args[0] );
747
		case 2:
748
			return new $class_name( $args[0], $args[1] );
749
		case 3:
750
			return new $class_name( $args[0], $args[1], $args[2] );
751
		case 4:
752
			return new $class_name( $args[0], $args[1], $args[2], $args[3] );
753
		default:
754
			return false;
755
	}
756
}
757
758
/**
759
 * Parse a dynamic slug into the dynamic and generic components.
760
 *
761
 * In the hooks and entities APIs, we have a convention of using dynamic slugs when
762
 * certain elements are registered dynamically. Such slugs are of the following
763
 * format: <generic part>\<dynamic part>. In other words, the generic and dynamic
764
 * parts are separated by a backslash. This function provides a canonical method of
765
 * parsing a slug into its constituent parts.
766
 *
767
 * @since 1.0.0
768
 *
769
 * @param string $slug A slug (for an entity or hook event, for example).
770
 *
771
 * @return array The slug parsed into the 'generic' and 'dynamic' portions. If the
772
 *               slug is not dynamic, the value of each of those keys will be false.
773
 */
774
function wordpoints_parse_dynamic_slug( $slug ) {
775
776
	$parsed = array( 'dynamic' => false, 'generic' => false );
777
778
	$parts = explode( '\\', $slug, 2 );
779
780
	if ( isset( $parts[1] ) ) {
781
		$parsed['dynamic'] = $parts[1];
782
		$parsed['generic'] = $parts[0];
783
	}
784
785
	return $parsed;
786
}
787
788
/**
789
 * Get the GUID of the current entity context.
790
 *
791
 * Most entities exist only in the context of a specific site on the network (in
792
 * multisite—when not on multisite they are just global to the install). An
793
 * example of this would be a Post: a post on one site with the ID 5 is different
794
 * than a post with that same ID on another site. To get the ID of such an entity's
795
 * context, you would pass 'site' as the value of the `$slug` arg, and the IDs for
796
 * both the 'site' and 'network' contexts would be returned.
797
 *
798
 * Some entities exist in the context of the network itself, not any particular
799
 * site. You can get the ID for the context of such an entity by passing 'network'
800
 * as the value of `$slug`.
801
 *
802
 * Still other entities are global to the install, existing across all networks even
803
 * on a multi-network installation. An example of this would be a User: the user with
804
 * the ID 3 is the same on every site on the network, and every network in the
805
 * install.
806
 *
807
 * Some entities might exist in other contexts entirely.
808
 *
809
 * The context IDs are returned in ascending hierarchical order.
810
 *
811
 * @since 1.0.0
812
 *
813
 * @param string $slug The slug of the context you want to get the current GUID of.
814
 *
815
 * @return array|false The ID of the context you passed in and the IDs of its parent
816
 *                     contexts, indexed by context slug, or false if any of the
817
 *                     contexts isn't current.
818
 */
819
function wordpoints_entities_get_current_context_id( $slug ) {
820
821
	$current_context = array();
822
823
	/** @var WordPoints_Class_Registry $contexts */
824
	$contexts = wordpoints_entities()->contexts;
0 ignored issues
show
Documentation introduced by
The property contexts does not exist on object<WordPoints_App_Registry>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
825
826
	while ( $slug ) {
827
828
		$context = $contexts->get( $slug );
829
830
		if ( ! $context instanceof WordPoints_Entity_Context ) {
831
			return false;
832
		}
833
834
		$id = $context->get_current_id();
835
836
		if ( false === $id ) {
837
			return false;
838
		}
839
840
		$current_context[ $slug ] = $id;
841
842
		$slug = $context->get_parent_slug();
843
	}
844
845
	return $current_context;
846
}
847
848
/**
849
 * Checks if we are in network context.
850
 *
851
 * There are times on multisite when we are in the context of the network as a whole,
852
 * and not in the context of any particular site. This includes the network admin
853
 * screens, and Ajax requests that originate from them.
854
 *
855
 * @since 1.0.0
856
 *
857
 * @return bool Whether we are in network context.
858
 */
859
function wordpoints_is_network_context() {
860
861
	if ( ! is_multisite() ) {
862
		return false;
863
	}
864
865
	if ( is_network_admin() ) {
866
		return true;
867
	}
868
869
	// See https://core.trac.wordpress.org/ticket/22589
870
	if (
871
		defined( 'DOING_AJAX' )
872
		&& DOING_AJAX
873
		&& isset( $_SERVER['HTTP_REFERER'] )
874
		&& preg_match(
875
			'#^' . preg_quote( network_admin_url(), '#' ) . '#i'
876
			, esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) )
877
		)
878
	) {
879
		return true;
880
	}
881
882
	/**
883
	 * Filter whether we are currently in network context.
884
	 *
885
	 * @since 1.0.0
886
	 *
887
	 * @param bool $in_network_context Whether we are in network context.
888
	 */
889
	return apply_filters( 'wordpoints_is_network_context', false );
890
}
891
892
/**
893
 * Get the GUID of the primary arg of an event, serialized as JSON.
894
 *
895
 * If the event does not have a primary arg, an empty string will be returned.
896
 *
897
 * @since 1.0.0
898
 *
899
 * @param WordPoints_Hook_Event_Args $event_args The event args.
900
 *
901
 * @return string The primary arg's GUID, JSON encoded.
902
 */
903
function wordpoints_hooks_get_event_primary_arg_guid_json( WordPoints_Hook_Event_Args $event_args ) {
904
905
	$entity = $event_args->get_primary_arg();
906
907
	if ( ! $entity ) {
908
		return '';
909
	}
910
911
	$the_guid = $entity->get_the_guid();
912
913
	if ( ! $the_guid ) {
914
		return '';
915
	}
916
917
	return wp_json_encode( $the_guid );
918
}
919
920
/**
921
 * Register the global cache groups.
922
 *
923
 * @since 1.0.0
924
 *
925
 * @WordPress\action init 5 Earlier than the default so that the groups will be
926
 *                          registered before any other code runs.
927
 */
928
function wordpoints_hooks_api_add_global_cache_groups() {
929
930
	if ( function_exists( 'wp_cache_add_global_groups' ) ) {
931
932
		wp_cache_add_global_groups(
933
			array(
934
				'wordpoints_hook_periods',
935
				'wordpoints_hook_period_ids_by_reaction',
936
			)
937
		);
938
	}
939
}
940
941
/**
942
 * Escape a MySQL identifier.
943
 *
944
 * Quotes the identifier with backticks and escapes any backticks within it by
945
 * doubling them.
946
 *
947
 * @since 1.0.0
948
 *
949
 * @link https://dev.mysql.com/doc/refman/5.7/en/identifiers.html#idm139700789409120
950
 *
951
 * @param string $identifier The identifier (column, table, alias, etc.).
952
 *
953
 * @return string The escaped identifier. Already quoted, do not place within
954
 *                backticks.
955
 */
956
function wordpoints_escape_mysql_identifier( $identifier ) {
957
	return '`' . str_replace( '`', '``', $identifier ) . '`';
958
}
959
960
// EOF
961