Completed
Push — develop ( e2f469...bd88fa )
by David
03:28
created

Wordlift_Thumbnail_Service::deleted_post_meta()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 6.7273
cc 7
eloc 10
nc 5
nop 4
1
<?php
2
3
/**
4
 * This class handle post thumbnails.
5
 *
6
 * @since 3.1.5
7
 */
8
class Wordlift_Thumbnail_Service {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
9
10
	/**
11
	 * The Thumbnail id meta key.
12
	 *
13
	 * @since 3.1.5
14
	 */
15
	const THUMBNAIL_ID_META_KEY = '_thumbnail_id';
16
17
	/**
18
	 * The predicate used in RDF to describe the thumbnail.
19
	 *
20
	 * @since 3.1.5
21
	 */
22
	const THUMBNAIL_RDF_PREDICATE = 'http://schema.org/image';
23
24
	/**
25
	 * The Log service.
26
	 *
27
	 * @since 3.1.5
28
	 * @access private
29
	 * @var \Wordlift_Log_Service The Log service.
30
	 */
31
	private $log_service;
32
33
	/**
34
	 * Create an instance of the Thumbnail service.
35
	 *
36
	 * @since 3.1.5
37
	 */
38
	public function __construct() {
39
40
		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Thumbnail_Service' );
41
42
	}
43
44
	/**
45
	 * Receive post meta events immediately after a post metadata has been deleted.
46
	 *
47
	 * @since 3.1.5
48
	 *
49
	 * @param array $meta_ids An array of deleted metadata entry IDs.
50
	 * @param int $object_id Object ID.
51
	 * @param string $meta_key Meta key.
52
	 * @param mixed $_meta_value Meta value.
53
	 */
54
	public function deleted_post_meta( $meta_ids, $object_id, $meta_key, $_meta_value ) {
55
56
		// Return if it's not the Thumbnail id meta key.
57
		if ( self::THUMBNAIL_ID_META_KEY !== $meta_key ) {
58
			return;
59
		}
60
61
		// Do not perform any action is the post is not published.
62
		if ( 'publish' !== get_post_status( $object_id ) ) {
63
			return;
64
		}
65
66
		// Get the post uri and return if it's null.
67
		if ( null === ( $uri = wl_get_entity_uri( $object_id ) ) ) {
68
			return;
69
		}
70
71
		// Prepare the query and execute it. We don't buffer the query since we're not going to reindex.
72
		$query = sprintf( 'DELETE { <%s> <%s> ?o . } WHERE  { <%1$s> <%2$s> ?o . };', $uri, self::THUMBNAIL_RDF_PREDICATE );
73
		if ( false === rl_execute_sparql_update_query( $query, false ) ) {
74
75
			$this->log_service->error( "An error occurred removing the post thumbnail [ meta ids :: " . ( is_array( $meta_ids ) ? implode( ',', $meta_ids ) : $meta_ids ) . " ][ object id :: $object_id ][ meta key :: $meta_key ][ meta value :: " . ( is_array( $_meta_value ) ? implode( ',', $_meta_value ) : $_meta_value ) . " ][ query :: $query ]" );
76
77
		}
78
79
	}
80
81
	/**
82
	 * Receive post meta events immediately after a post metadata has been added.
83
	 *
84
	 * @since 3.1.5
85
	 *
86
	 * @param int $mid The meta ID after successful update.
87
	 * @param int $object_id Object ID.
88
	 * @param string $meta_key Meta key.
89
	 * @param mixed $_meta_value Meta value.
90
	 */
91
	public function added_post_meta( $mid, $object_id, $meta_key, $_meta_value ) {
92
93
		$this->log_service->trace( "A post meta has been updated [ meta id :: $mid ][ object id :: $object_id ][ meta key :: $meta_key ][ meta value :: " . var_export( $_meta_value, true ) . " ]" );
94
95
		// Return if it's not the Thumbnail id meta key.
96
		if ( self::THUMBNAIL_ID_META_KEY !== $meta_key ) {
97
			return;
98
		}
99
100
		// The meta value must be the numeric id of the attachment. If it isn't, return.
101
		if ( ! is_numeric( $_meta_value ) ) {
102
			return;
103
		}
104
105
		// Do not perform any action is the post is not published.
106
		if ( 'publish' !== get_post_status( $object_id ) ) {
107
			return;
108
		}
109
110
		// Get the post uri and return if it's null.
111
		if ( null === ( $uri = wl_get_entity_uri( $object_id ) ) ) {
112
			return;
113
		}
114
115
		// Get the attachment url and return if not found.
116
		if ( false === ( $attachment_url = wp_get_attachment_url( $_meta_value ) ) ) {
117
			return;
118
		};
119
120
		// Prepare the query and execute it. We don't buffer the query since we're not going to reindex.
121
		$query = sprintf( 'DELETE { <%1$s> <%2$s> ?o . } WHERE  { <%1$s> <%2$s> ?o . }; INSERT DATA { <%1$s> <%2$s> <%3$s> . };', $uri, self::THUMBNAIL_RDF_PREDICATE, $attachment_url );
122
		if ( false === rl_execute_sparql_update_query( $query, false ) ) {
123
124
			$this->log_service->error( "An error occurred removing the post thumbnail [ meta ids :: $mid ][ object id :: $object_id ][ meta key :: $meta_key ][ meta value :: " . var_export( $_meta_value, true ) . " ][ query :: $query ]" );
125
126
		}
127
128
	}
129
130
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
131