@@ -7,124 +7,124 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Wordlift_Thumbnail_Service { |
| 9 | 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 | - } |
|
| 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 | 129 | |
| 130 | 130 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function __construct() { |
| 39 | 39 | |
| 40 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Thumbnail_Service' ); |
|
| 40 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Thumbnail_Service'); |
|
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
@@ -51,28 +51,28 @@ discard block |
||
| 51 | 51 | * @param string $meta_key Meta key. |
| 52 | 52 | * @param mixed $_meta_value Meta value. |
| 53 | 53 | */ |
| 54 | - public function deleted_post_meta( $meta_ids, $object_id, $meta_key, $_meta_value ) { |
|
| 54 | + public function deleted_post_meta($meta_ids, $object_id, $meta_key, $_meta_value) { |
|
| 55 | 55 | |
| 56 | 56 | // Return if it's not the Thumbnail id meta key. |
| 57 | - if ( self::THUMBNAIL_ID_META_KEY !== $meta_key ) { |
|
| 57 | + if (self::THUMBNAIL_ID_META_KEY !== $meta_key) { |
|
| 58 | 58 | return; |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Do not perform any action is the post is not published. |
| 62 | - if ( 'publish' !== get_post_status( $object_id ) ) { |
|
| 62 | + if ('publish' !== get_post_status($object_id)) { |
|
| 63 | 63 | return; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Get the post uri and return if it's null. |
| 67 | - if ( null === ( $uri = wl_get_entity_uri( $object_id ) ) ) { |
|
| 67 | + if (null === ($uri = wl_get_entity_uri($object_id))) { |
|
| 68 | 68 | return; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 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 ) ) { |
|
| 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 | 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 ]" ); |
|
| 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 | 76 | |
| 77 | 77 | } |
| 78 | 78 | |
@@ -88,40 +88,40 @@ discard block |
||
| 88 | 88 | * @param string $meta_key Meta key. |
| 89 | 89 | * @param mixed $_meta_value Meta value. |
| 90 | 90 | */ |
| 91 | - public function added_post_meta( $mid, $object_id, $meta_key, $_meta_value ) { |
|
| 91 | + public function added_post_meta($mid, $object_id, $meta_key, $_meta_value) { |
|
| 92 | 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 ) . " ]" ); |
|
| 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 | 94 | |
| 95 | 95 | // Return if it's not the Thumbnail id meta key. |
| 96 | - if ( self::THUMBNAIL_ID_META_KEY !== $meta_key ) { |
|
| 96 | + if (self::THUMBNAIL_ID_META_KEY !== $meta_key) { |
|
| 97 | 97 | return; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // The meta value must be the numeric id of the attachment. If it isn't, return. |
| 101 | - if ( ! is_numeric( $_meta_value ) ) { |
|
| 101 | + if ( ! is_numeric($_meta_value)) { |
|
| 102 | 102 | return; |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | // Do not perform any action is the post is not published. |
| 106 | - if ( 'publish' !== get_post_status( $object_id ) ) { |
|
| 106 | + if ('publish' !== get_post_status($object_id)) { |
|
| 107 | 107 | return; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Get the post uri and return if it's null. |
| 111 | - if ( null === ( $uri = wl_get_entity_uri( $object_id ) ) ) { |
|
| 111 | + if (null === ($uri = wl_get_entity_uri($object_id))) { |
|
| 112 | 112 | return; |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | // Get the attachment url and return if not found. |
| 116 | - if ( false === ( $attachment_url = wp_get_attachment_url( $_meta_value ) ) ) { |
|
| 116 | + if (false === ($attachment_url = wp_get_attachment_url($_meta_value))) { |
|
| 117 | 117 | return; |
| 118 | 118 | }; |
| 119 | 119 | |
| 120 | 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 ) ) { |
|
| 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 | 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 ]" ); |
|
| 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 | 125 | |
| 126 | 126 | } |
| 127 | 127 | |