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

Entity_No_Index_Flag::__construct()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 1
nop 0
dl 0
loc 30
rs 9.44
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
		$no_index_flag = self::YOAST_POST_NO_INDEX_FLAG;
16
17
		add_action( 'wp_insert_post', function ( $post_id, $post, $update ) use ( $no_index_flag ) {
18
19
			$post_type = get_post_type( $post_id );
20
21
			if ( $post_type !== \Wordlift_Entity_Service::TYPE_NAME ) {
22
				// Dont set this flag for any other post types.
23
				return;
24
			}
25
26
			// We need to set this flag only on entity creation.
27
			if ( ! $update ) {
28
				update_post_meta( $post_id, $no_index_flag, 1 );
29
			}
30
31
		}, PHP_INT_MAX, 3 );
32
33
34
		add_action( 'post_updated', function ( $post_id ) use ( $no_index_flag ) {
35
			if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) {
36
				return;
37
			}
38
			// if the post is updated, remove this flag
39
			delete_post_meta( $post_id, $no_index_flag );
40
		}, PHP_INT_MAX );
41
42
	}
43
44
}
45
46