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

Entity_No_Index_Flag   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 28 4
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