Completed
Pull Request — develop (#1336)
by Naveen
03:34
created

Entity_No_Index_Flag::__construct()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 1
nop 0
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Entity;
4
/**
5
 * @since 3.28.0
6
 * @author Naveen Muthusamy <[email protected]>
7
 */
8
class Entity_No_Index_Flag {
9
10
11
	const YOAST_POST_NO_INDEX_FLAG = '_yoast_wpseo_meta-robots-noindex';
12
13
	public function __construct() {
14
15
		add_action( 'wp_insert_post', function ( $post_id, $post, $update ) {
16
17
			$post_type = get_post_type( $post_id );
18
19
			if ( $post_type !== \Wordlift_Entity_Service::TYPE_NAME ) {
20
				// Dont set this flag for any other post types.
21
				return;
22
			}
23
24
			// We need to set this flag only on entity creation.
25
			if ( ! $update ) {
26
				update_post_meta( $post_id, self::YOAST_POST_NO_INDEX_FLAG, 1 );
27
			}
28
29
		}, PHP_INT_MAX, 3 );
30
31
32
		add_action( 'post_updated', function ( $post_id ) {
33
			if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) {
34
				return;
35
			}
36
			// if the post is updated, remove this flag
37
			delete_post_meta( $post_id, self::YOAST_POST_NO_INDEX_FLAG );
38
		}, PHP_INT_MAX );
39
40
	}
41
42
}
43
44