Completed
Push — master ( 6d62fd...f8b696 )
by J.D.
03:46
created

hooks.php ➔ wordpoints_hook_conditions_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Hooks API functions.
5
 *
6
 * @package wordpoints
7
 * @since 2.1.0
8
 */
9
10
/**
11
 * Get the hooks app.
12
 *
13
 * @since 2.1.0
14
 *
15
 * @return WordPoints_Hooks The hooks app.
16
 */
17
function wordpoints_hooks() {
18
19
	if ( ! isset( WordPoints_App::$main ) ) {
20
		wordpoints_apps();
21
	}
22
23
	return WordPoints_App::$main->get_sub_app( 'hooks' );
24
}
25
26
/**
27
 * Initialize the hooks API.
28
 *
29
 * @since 2.1.0
30
 *
31
 * @WordPress\action wordpoints_modules_loaded
32
 */
33
function wordpoints_init_hooks() {
34
35
	$hooks = wordpoints_hooks();
36
37
	// Just accessing this causes it to be initialized. We need to do that so
38
	// the actions will be registered and hooked up. The rest of the API can be
39
	// lazy-loaded as it is needed.
40
	$hooks->get_sub_app( 'actions' );
41
}
42
43
/**
44
 * Register hook extension when the extension registry is initialized.
45
 *
46
 * @since 2.1.0
47
 *
48
 * @WordPress\action wordpoints_init_app_registry-hooks-extensions
49
 *
50
 * @param WordPoints_Class_Registry_Persistent $extensions The extension registry.
51
 */
52
function wordpoints_hook_extensions_init( $extensions ) {
53
54
	$extensions->register( 'blocker', 'WordPoints_Hook_Extension_Blocker' );
55
	$extensions->register( 'disable', 'WordPoints_Hook_Extension_Blocker' );
56
	$extensions->register( 'repeat_blocker', 'WordPoints_Hook_Extension_Repeat_Blocker' );
57
	$extensions->register( 'reversals', 'WordPoints_Hook_Extension_Reversals' );
58
	$extensions->register( 'conditions', 'WordPoints_Hook_Extension_Conditions' );
59
	$extensions->register( 'periods', 'WordPoints_Hook_Extension_Periods' );
60
}
61
62
/**
63
 * Register hook conditions when the conditions registry is initialized.
64
 *
65
 * @since 2.1.0
66
 *
67
 * @WordPress\action wordpoints_init_app_registry-hooks-conditions
68
 *
69
 * @param WordPoints_Class_Registry_Children $conditions The conditions registry.
70
 */
71
function wordpoints_hook_conditions_init( $conditions ) {
72
73
	$conditions->register( 'decimal_number', 'equals', 'WordPoints_Hook_Condition_Equals' );
74
	$conditions->register( 'decimal_number', 'greater_than', 'WordPoints_Hook_Condition_Number_Greater_Than' );
75
	$conditions->register( 'decimal_number', 'less_than', 'WordPoints_Hook_Condition_Number_Less_Than' );
76
	$conditions->register( 'entity', 'equals', 'WordPoints_Hook_Condition_Equals' );
77
	$conditions->register( 'entity_array', 'contains', 'WordPoints_Hook_Condition_Entity_Array_Contains' );
78
	$conditions->register( 'integer', 'equals', 'WordPoints_Hook_Condition_Equals' );
79
	$conditions->register( 'integer', 'greater_than', 'WordPoints_Hook_Condition_Number_Greater_Than' );
80
	$conditions->register( 'integer', 'less_than', 'WordPoints_Hook_Condition_Number_Less_Than' );
81
	$conditions->register( 'text', 'contains', 'WordPoints_Hook_Condition_String_Contains' );
82
	$conditions->register( 'text', 'equals', 'WordPoints_Hook_Condition_Equals' );
83
}
84
85
/**
86
 * Register hook actions when the action registry is initialized.
87
 *
88
 * @since 2.1.0
89
 *
90
 * @WordPress\action wordpoints_init_app_registry-hooks-actions
91
 *
92
 * @param WordPoints_Hook_Actions $actions The action registry.
93
 */
94
function wordpoints_hook_actions_init( $actions ) {
95
96
	$actions->register(
97
		'user_register'
98
		, 'WordPoints_Hook_Action'
99
		, array(
100
			'action' => 'user_register',
101
			'data'   => array(
102
				'arg_index' => array( 'user' => 0 ),
103
			),
104
		)
105
	);
106
107
	$actions->register(
108
		'user_delete'
109
		, 'WordPoints_Hook_Action'
110
		, array(
111
			'action' => is_multisite() ? 'wpmu_delete_user' : 'delete_user',
112
			'data'   => array(
113
				'arg_index' => array( 'user' => 0 ),
114
			),
115
		)
116
	);
117
118
	$actions->register(
119
		'user_visit'
120
		, 'WordPoints_Hook_Action'
121
		, array(
122
			'action' => 'wp',
123
		)
124
	);
125
126
	// Register actions for all of the public post types.
127
	$post_types = wordpoints_get_post_types_for_hook_events();
128
129
	/**
130
	 * Filter which post types to register hook actions for.
131
	 *
132
	 * @since 2.1.0
133
	 * @deprecated 2.2.0 Use 'wordpoints_register_hook_actions_for_post_types' instead.
134
	 *
135
	 * @param string[] The post type slugs ("names").
136
	 */
137
	$post_types = apply_filters_deprecated(
138
		'wordpoints_register_hook_actions_for_post_types'
139
		, array( $post_types )
140
		, '2.2.0'
141
		, 'wordpoints_register_hook_events_for_post_types'
142
	);
143
144
	foreach ( $post_types as $slug ) {
145
		wordpoints_register_post_type_hook_actions( $slug );
146
	}
147
148
	// Also register actions for any post types that are late to the party.
149
	add_action( 'registered_post_type', 'wordpoints_register_post_type_hook_actions' );
150
}
151
152
/**
153
 * Register the hook actions for a post type.
154
 *
155
 * @since 2.1.0
156
 *
157
 * @WordPress\action registered_post_type By {@see wordpoints_hook_actions_init()}.
158
 *
159
 * @param string $slug The slug of the post type.
160
 */
161
function wordpoints_register_post_type_hook_actions( $slug ) {
162
163
	$actions = wordpoints_hooks()->get_sub_app( 'actions' );
164
165
	if ( post_type_supports( $slug, 'comments' ) ) {
166
167
		$actions->register(
168
			"comment_approve\\{$slug}"
169
			, 'WordPoints_Hook_Action_Post_Type_Comment'
170
			, array(
171
				'action' => 'transition_comment_status',
172
				'data'   => array(
173
					'arg_index'    => array( "comment\\{$slug}" => 2 ),
174
					'requirements' => array( 0 => 'approved' ),
175
				),
176
			)
177
		);
178
179
		$actions->register(
180
			"comment_new\\{$slug}"
181
			, 'WordPoints_Hook_Action_Comment_New'
182
			, array(
183
				'action' => 'wp_insert_comment',
184
				'data'   => array(
185
					'arg_index' => array( "comment\\{$slug}" => 1 ),
186
				),
187
			)
188
		);
189
190
		$actions->register(
191
			"comment_deapprove\\{$slug}"
192
			, 'WordPoints_Hook_Action_Post_Type_Comment'
193
			, array(
194
				'action' => 'transition_comment_status',
195
				'data'   => array(
196
					'arg_index'    => array( "comment\\{$slug}" => 2 ),
197
					'requirements' => array( 1 => 'approved' ),
198
				),
199
			)
200
		);
201
202
	} // End if ( post type supports comments ).
203
204
	// This works for all post types except attachments.
205
	if ( 'attachment' !== $slug ) {
206
207
		$actions->register(
208
			"post_publish\\{$slug}"
209
			, 'WordPoints_Hook_Action_Post_Type'
210
			, array(
211
				'action' => 'transition_post_status',
212
				'data'   => array(
213
					'arg_index'    => array( "post\\{$slug}" => 2 ),
214
					'requirements' => array(
215
						0 => 'publish',
216
						1 => array( 'comparator' => '!=', 'value' => 'publish' ),
217
					),
218
				),
219
			)
220
		);
221
222
		$actions->register(
223
			"post_depublish\\{$slug}"
224
			, 'WordPoints_Hook_Action_Post_Type'
225
			, array(
226
				'action' => 'transition_post_status',
227
				'data'   => array(
228
					'arg_index'    => array( "post\\{$slug}" => 2 ),
229
					'requirements' => array(
230
						0 => array( 'comparator' => '!=', 'value' => 'publish' ),
231
						1 => 'publish',
232
					),
233
				),
234
			)
235
		);
236
237
		$actions->register(
238
			"post_depublish_delete\\{$slug}"
239
			, 'WordPoints_Hook_Action_Post_Depublish_Delete'
240
			, array(
241
				'action' => 'delete_post',
242
				'data'   => array(
243
					'arg_index' => array( "post\\{$slug}" => 0 ),
244
				),
245
			)
246
		);
247
248
	} else {
249
250
		$actions->register(
251
			'add_attachment'
252
			, 'WordPoints_Hook_Action'
253
			, array(
254
				'action' => 'add_attachment',
255
				'data'   => array(
256
					'arg_index' => array( 'post\attachment' => 0 ),
257
				),
258
			)
259
		);
260
261
	} // End if ( not attachment ) else.
262
263
	$actions->register(
264
		"post_delete\\{$slug}"
265
		, 'WordPoints_Hook_Action_Post_Type'
266
		, array(
267
			'action' => 'delete_post',
268
			'data'   => array(
269
				'arg_index' => array( "post\\{$slug}" => 0 ),
270
			),
271
		)
272
	);
273
274
	/**
275
	 * Fires when registering the hook actions for a post type.
276
	 *
277
	 * @since 2.1.0
278
	 *
279
	 * @param string $slug The slug ("name") of the post type.
280
	 */
281
	do_action( 'wordpoints_register_post_type_hook_actions', $slug );
282
}
283
284
/**
285
 * Register hook events when the event registry is initialized.
286
 *
287
 * @since 2.1.0
288
 *
289
 * @WordPress\action wordpoints_init_app_registry-hooks-events
290
 *
291
 * @param WordPoints_Hook_Events $events The event registry.
292
 */
293
function wordpoints_hook_events_init( $events ) {
294
295
	$events->register(
296
		'user_register'
297
		, 'WordPoints_Hook_Event_User_Register'
298
		, array(
299
			'actions' => array(
300
				'toggle_on'  => 'user_register',
301
				'toggle_off' => 'user_delete',
302
			),
303
			'args' => array(
304
				'user' => 'WordPoints_Hook_Arg',
305
			),
306
		)
307
	);
308
309
	$events->register(
310
		'user_visit'
311
		, 'WordPoints_Hook_Event_User_Visit'
312
		, array(
313
			'actions' => array(
314
				'fire' => 'user_visit',
315
			),
316
			'args' => array(
317
				'current:user' => 'WordPoints_Hook_Arg_Current_User',
318
			),
319
		)
320
	);
321
322
	foreach ( wordpoints_get_post_types_for_hook_events() as $slug ) {
323
		wordpoints_register_post_type_hook_events( $slug );
324
	}
325
326
	// Also register events for any post types that are late to the party.
327
	add_action( 'registered_post_type', 'wordpoints_register_post_type_hook_events' );
328
}
329
330
/**
331
 * Get the slugs of the post types to register events for.
332
 *
333
 * @since 2.2.0
334
 *
335
 * @return string[] The post type slugs to register hook events for.
336
 */
337
function wordpoints_get_post_types_for_hook_events() {
338
339
	/**
340
	 * Filter which post types to register hook events for.
341
	 *
342
	 * @since 2.1.0
343
	 *
344
	 * @param string[] $post_types The post type slugs ("names").
345
	 */
346
	return apply_filters(
347
		'wordpoints_register_hook_events_for_post_types'
348
		, wordpoints_get_post_types_for_auto_integration()
349
	);
350
}
351
352
/**
353
 * Register the hook events for a post type.
354
 *
355
 * @since 2.1.0
356
 *
357
 * @WordPress\action registered_post_type By {@see wordpoints_hook_events_init()}.
358
 *
359
 * @param string $slug The slug of the post type.
360
 */
361
function wordpoints_register_post_type_hook_events( $slug ) {
362
363
	$events = wordpoints_hooks()->get_sub_app( 'events' );
364
365
	if ( 'attachment' === $slug ) {
366
367
		$events->register(
368
			'media_upload'
369
			, 'WordPoints_Hook_Event_Media_Upload'
370
			, array(
371
				'actions' => array(
372
					'toggle_on'  => 'add_attachment',
373
					'toggle_off' => "post_delete\\{$slug}",
374
				),
375
				'args'    => array(
376
					"post\\{$slug}" => 'WordPoints_Hook_Arg',
377
				),
378
			)
379
		);
380
381 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...
382
383
		$events->register(
384
			"post_publish\\{$slug}"
385
			, 'WordPoints_Hook_Event_Post_Publish'
386
			, array(
387
				'actions' => array(
388
					'toggle_on'  => "post_publish\\{$slug}",
389
					'toggle_off' => array(
390
						"post_depublish\\{$slug}",
391
						"post_depublish_delete\\{$slug}",
392
					),
393
				),
394
				'args'    => array(
395
					"post\\{$slug}" => 'WordPoints_Hook_Arg',
396
				),
397
			)
398
		);
399
	}
400
401 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...
402
403
		$events->register(
404
			"comment_leave\\{$slug}"
405
			, 'WordPoints_Hook_Event_Comment_Leave'
406
			, array(
407
				'actions' => array(
408
					'toggle_on'  => array(
409
						"comment_approve\\{$slug}",
410
						"comment_new\\{$slug}",
411
					),
412
					'toggle_off' => "comment_deapprove\\{$slug}",
413
				),
414
				'args' => array(
415
					"comment\\{$slug}" => 'WordPoints_Hook_Arg',
416
				),
417
			)
418
		);
419
	}
420
421
	/**
422
	 * Fires when registering the hook events for a post type.
423
	 *
424
	 * @since 2.1.0
425
	 *
426
	 * @param string $slug The slug ("name") of the post type.
427
	 */
428
	do_action( 'wordpoints_register_post_type_hook_events', $slug );
429
}
430
431
/**
432
 * Get the GUID(s) of the signature arg(s) of an event, serialized as JSON.
433
 *
434
 * If the event does not have any signature args, an empty string will be returned.
435
 *
436
 * @since 2.3.0
437
 *
438
 * @param WordPoints_Hook_Event_Args $event_args The event args.
439
 *
440
 * @return string The signature arg(s)'s GUID(s), JSON encoded.
441
 */
442
function wordpoints_hooks_get_event_signature_arg_guids_json( WordPoints_Hook_Event_Args $event_args ) {
443
444
	$entities = $event_args->get_signature_args();
445
446
	if ( ! $entities ) {
447
		return '';
448
	}
449
450
	$the_guids = array();
451
452
	foreach ( $entities as $arg_slug => $entity ) {
453
454
		$the_guid = $entity->get_the_guid();
455
456
		if ( $the_guid ) {
457
			$the_guids[ $arg_slug ] = $the_guid;
458
		}
459
	}
460
461
	if ( ! $the_guids ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $the_guids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
462
		return '';
463
	}
464
465
	if ( 1 === count( $the_guids ) ) {
466
		$the_guids = reset( $the_guids );
467
	} else {
468
		ksort( $the_guids );
469
	}
470
471
	return wp_json_encode( $the_guids );
472
}
473
474
/**
475
 * Get the GUID of the primary arg of an event, serialized as JSON.
476
 *
477
 * If the event does not have a primary arg, an empty string will be returned.
478
 *
479
 * @since 2.1.0
480
 * @deprecated 2.3.0 Use wordpoints_hooks_get_event_signature_arg_guids_json().
481
 *
482
 * @param WordPoints_Hook_Event_Args $event_args The event args.
483
 *
484
 * @return string The primary arg's GUID, JSON encoded.
485
 */
486
function wordpoints_hooks_get_event_primary_arg_guid_json( WordPoints_Hook_Event_Args $event_args ) {
487
488
	_deprecated_function(
489
		__FUNCTION__
490
		, '2.3.0'
491
		, 'wordpoints_hooks_get_event_signature_arg_guids_json()'
492
	);
493
494
	$entity = $event_args->get_primary_arg();
0 ignored issues
show
Deprecated Code introduced by
The method WordPoints_Hook_Event_Args::get_primary_arg() has been deprecated with message: 2.3.0 Use self::get_signature_args() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
495
496
	if ( ! $entity ) {
497
		return '';
498
	}
499
500
	$the_guid = $entity->get_the_guid();
501
502
	if ( ! $the_guid ) {
503
		return '';
504
	}
505
506
	return wp_json_encode( $the_guid );
507
}
508
509
// EOF
510