Completed
Pull Request — develop (#1233)
by
unknown
02:52
created

Sync_Post_Hooks::do_delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Dataset;
4
5
use Wordlift\Object_Type_Enum;
6
7
class Sync_Post_Hooks extends Abstract_Sync_Hooks {
8
	/**
9
	 * @var \Wordlift_Log_Service
10
	 */
11
	private $log;
12
13
	/**
14
	 * @var Sync_Service
15
	 */
16
	private $sync_service;
17
18
	/**
19
	 * @var Sync_Object_Adapter_Factory
20
	 */
21
	private $sync_object_factory;
22
23
	/**
24
	 * Sync_Post_Hooks constructor.
25
	 *
26
	 * @param Sync_Service $sync_service
27
	 * @param Sync_Object_Adapter_Factory $sync_object_factory
28
	 */
29 View Code Duplication
	function __construct( $sync_service, $sync_object_factory ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
30
		parent::__construct();
31
32
		$this->log = \Wordlift_Log_Service::get_logger( get_class() );
33
34
		$this->sync_service        = $sync_service;
35
		$this->sync_object_factory = $sync_object_factory;
36
37
		$this->register_hooks();
38
	}
39
40
	private function register_hooks() {
41
		/**
42
		 * Register hooks for post and meta.
43
		 */
44
		add_action( 'save_post', array( $this, 'save_post' ) );
45
		add_action( 'added_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
46
		add_action( 'updated_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
47
		add_action( 'deleted_post_meta', array( $this, 'changed_post_meta' ), 10, 4 );
48
		add_action( 'delete_post', array( $this, 'delete_post' ) );
49
50
	}
51
52
	public function save_post( $post_id ) {
53
54
		if ( ! in_array( get_post_type( $post_id ), \Wordlift_Entity_Service::valid_entity_post_types() ) ) {
55
			return;
56
		}
57
58
		$this->sync( $post_id );
59
60
	}
61
62
	public function changed_post_meta( $meta_id, $post_id, $meta_key, $_meta_value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $_meta_value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
64
		if ( in_array( $meta_key,
65
				apply_filters( 'wl_dataset__sync_post_hooks__ignored_meta_keys',
66
					apply_filters( 'wl_dataset__sync_hooks__ignored_meta_keys',
67
						array(
68
							'_pingme',
69
							'_encloseme',
70
							'entity_url',
71
						) ) ) )
72
		     || ! in_array( get_post_type( $post_id ), \Wordlift_Entity_Service::valid_entity_post_types() )
73
		) {
74
			return;
75
		}
76
77
		$this->sync( $post_id );
78
79
	}
80
81
	private function sync( $post_id ) {
82
		$this->enqueue( array( 'do_sync', $post_id ) );
83
	}
84
85
	public function do_sync( $post_id ) {
86
		try {
87
			$post = get_post( $post_id );
88
			if ( !isset( $post ) ) {
89
				return;
90
			}
91
			$this->sync_service->sync_many( array(
92
				$this->sync_object_factory->create( Object_Type_Enum::POST, $post_id ),
93
				$this->sync_object_factory->create( Object_Type_Enum::USER, $post->post_author )
94
			) );
95
		} catch ( \Exception $e ) {
96
			$this->log->error( "An error occurred while trying to sync post $post_id: " . $e->getMessage(), $e );
97
		}
98
99
	}
100
101
	public function delete_post( $post_id ) {
102
		$this->enqueue( array( 'do_delete', $post_id ) );
103
	}
104
105
	public function do_delete( $post_id ) {
106
		try {
107
			$this->sync_service->delete_one( Object_Type_Enum::POST, $post_id );
108
		} catch ( \Exception $e ) {
109
			$this->log->error( "An error occurred while trying to delete post $post_id: " . $e->getMessage(), $e );
110
		}
111
	}
112
113
}
114