@@ -7,127 +7,127 @@ |
||
| 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_or_updated_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 | - // Don't pollute the cache. |
|
| 116 | - wp_cache_delete( $object_id, 'post_meta' ); |
|
| 117 | - |
|
| 118 | - // Get the attachment url and return if not found. |
|
| 119 | - if ( false === ( $attachment_url = wp_get_attachment_url( $_meta_value ) ) ) { |
|
| 120 | - return; |
|
| 121 | - }; |
|
| 122 | - |
|
| 123 | - // Prepare the query and execute it. We don't buffer the query since we're not going to reindex. |
|
| 124 | - $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 ); |
|
| 125 | - if ( false === rl_execute_sparql_update_query( $query, false ) ) { |
|
| 126 | - |
|
| 127 | - $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 ]" ); |
|
| 128 | - |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - } |
|
| 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_or_updated_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 | + // Don't pollute the cache. |
|
| 116 | + wp_cache_delete( $object_id, 'post_meta' ); |
|
| 117 | + |
|
| 118 | + // Get the attachment url and return if not found. |
|
| 119 | + if ( false === ( $attachment_url = wp_get_attachment_url( $_meta_value ) ) ) { |
|
| 120 | + return; |
|
| 121 | + }; |
|
| 122 | + |
|
| 123 | + // Prepare the query and execute it. We don't buffer the query since we're not going to reindex. |
|
| 124 | + $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 ); |
|
| 125 | + if ( false === rl_execute_sparql_update_query( $query, false ) ) { |
|
| 126 | + |
|
| 127 | + $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 ]" ); |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | 133 | } |
@@ -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,43 +88,43 @@ discard block |
||
| 88 | 88 | * @param string $meta_key Meta key. |
| 89 | 89 | * @param mixed $_meta_value Meta value. |
| 90 | 90 | */ |
| 91 | - public function added_or_updated_post_meta( $mid, $object_id, $meta_key, $_meta_value ) { |
|
| 91 | + public function added_or_updated_post_meta($mid, $object_id, $meta_key, $_meta_value) { |
|
| 92 | 92 | |
| 93 | 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 | // Don't pollute the cache. |
| 116 | - wp_cache_delete( $object_id, 'post_meta' ); |
|
| 116 | + wp_cache_delete($object_id, 'post_meta'); |
|
| 117 | 117 | |
| 118 | 118 | // Get the attachment url and return if not found. |
| 119 | - if ( false === ( $attachment_url = wp_get_attachment_url( $_meta_value ) ) ) { |
|
| 119 | + if (false === ($attachment_url = wp_get_attachment_url($_meta_value))) { |
|
| 120 | 120 | return; |
| 121 | 121 | }; |
| 122 | 122 | |
| 123 | 123 | // Prepare the query and execute it. We don't buffer the query since we're not going to reindex. |
| 124 | - $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 ); |
|
| 125 | - if ( false === rl_execute_sparql_update_query( $query, false ) ) { |
|
| 124 | + $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); |
|
| 125 | + if (false === rl_execute_sparql_update_query($query, false)) { |
|
| 126 | 126 | |
| 127 | - $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 ]" ); |
|
| 127 | + $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 ]"); |
|
| 128 | 128 | |
| 129 | 129 | } |
| 130 | 130 | |
@@ -7,9 +7,9 @@ |
||
| 7 | 7 | * @param mixed $response The response to send to the client as JSON. |
| 8 | 8 | */ |
| 9 | 9 | function wl_core_send_json( $response ) { |
| 10 | - if ( ob_get_contents() ) { |
|
| 11 | - ob_clean(); |
|
| 12 | - } |
|
| 10 | + if ( ob_get_contents() ) { |
|
| 11 | + ob_clean(); |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | - wp_send_json( $response ); |
|
| 14 | + wp_send_json( $response ); |
|
| 15 | 15 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | * |
| 7 | 7 | * @param mixed $response The response to send to the client as JSON. |
| 8 | 8 | */ |
| 9 | -function wl_core_send_json( $response ) { |
|
| 10 | - if ( ob_get_contents() ) { |
|
| 9 | +function wl_core_send_json($response) { |
|
| 10 | + if (ob_get_contents()) { |
|
| 11 | 11 | ob_clean(); |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | - wp_send_json( $response ); |
|
| 14 | + wp_send_json($response); |
|
| 15 | 15 | } |
@@ -17,34 +17,34 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | function wl_schema_get_value( $post_id, $property_name ) { |
| 19 | 19 | |
| 20 | - // Property name must be defined. |
|
| 21 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 22 | - return null; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - // store eventual schema name in different variable |
|
| 26 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 27 | - |
|
| 28 | - // Establish entity id. |
|
| 29 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 30 | - $post_id = get_the_ID(); |
|
| 31 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 32 | - return null; |
|
| 33 | - } |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - // Get custom fields. |
|
| 37 | - $term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 38 | - // Search for the required meta value (by constant name or schema name) |
|
| 39 | - foreach ( $term_mapping as $wl_constant => $property_info ) { |
|
| 40 | - $found_constant = ( $wl_constant == $property_name ); |
|
| 41 | - $found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name ); |
|
| 42 | - if ( $found_constant || $found_predicate ) { |
|
| 43 | - return get_post_meta( $post_id, $wl_constant ); |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - return null; |
|
| 20 | + // Property name must be defined. |
|
| 21 | + if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 22 | + return null; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + // store eventual schema name in different variable |
|
| 26 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 27 | + |
|
| 28 | + // Establish entity id. |
|
| 29 | + if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 30 | + $post_id = get_the_ID(); |
|
| 31 | + if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 32 | + return null; |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + // Get custom fields. |
|
| 37 | + $term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 38 | + // Search for the required meta value (by constant name or schema name) |
|
| 39 | + foreach ( $term_mapping as $wl_constant => $property_info ) { |
|
| 40 | + $found_constant = ( $wl_constant == $property_name ); |
|
| 41 | + $found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name ); |
|
| 42 | + if ( $found_constant || $found_predicate ) { |
|
| 43 | + return get_post_meta( $post_id, $wl_constant ); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + return null; |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | /** |
@@ -58,38 +58,38 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | function wl_schema_set_value( $post_id, $property_name, $property_value ) { |
| 60 | 60 | |
| 61 | - // Some checks on the parameters |
|
| 62 | - if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) { |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 61 | + // Some checks on the parameters |
|
| 62 | + if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) { |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - // Build full schema uri if necessary |
|
| 67 | - $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 66 | + // Build full schema uri if necessary |
|
| 67 | + $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 68 | 68 | |
| 69 | - // Get accepted properties |
|
| 70 | - $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 69 | + // Get accepted properties |
|
| 70 | + $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 71 | 71 | |
| 72 | - // Find the name of the custom-field managing the schema property |
|
| 73 | - foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 74 | - if ( $field['predicate'] == $property_name ) { |
|
| 72 | + // Find the name of the custom-field managing the schema property |
|
| 73 | + foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 74 | + if ( $field['predicate'] == $property_name ) { |
|
| 75 | 75 | |
| 76 | - // Deal with single values |
|
| 77 | - if ( ! is_array( $property_value ) ) { |
|
| 78 | - $property_value = array( $property_value ); |
|
| 79 | - } |
|
| 76 | + // Deal with single values |
|
| 77 | + if ( ! is_array( $property_value ) ) { |
|
| 78 | + $property_value = array( $property_value ); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - // Delete present meta |
|
| 82 | - delete_post_meta( $post_id, $wl_constant ); |
|
| 81 | + // Delete present meta |
|
| 82 | + delete_post_meta( $post_id, $wl_constant ); |
|
| 83 | 83 | |
| 84 | - foreach ( $property_value as $value ) { |
|
| 85 | - add_post_meta( $post_id, $wl_constant, $value ); |
|
| 86 | - } |
|
| 84 | + foreach ( $property_value as $value ) { |
|
| 85 | + add_post_meta( $post_id, $wl_constant, $value ); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - return true; |
|
| 89 | - } |
|
| 90 | - } |
|
| 88 | + return true; |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - return false; |
|
| 92 | + return false; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -102,18 +102,18 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | function wl_schema_get_types( $post_id ) { |
| 104 | 104 | |
| 105 | - // Some checks on the parameters |
|
| 106 | - if ( ! is_numeric( $post_id ) ) { |
|
| 107 | - return null; |
|
| 108 | - } |
|
| 105 | + // Some checks on the parameters |
|
| 106 | + if ( ! is_numeric( $post_id ) ) { |
|
| 107 | + return null; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - $type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 110 | + $type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 111 | 111 | |
| 112 | - if ( isset( $type['uri'] ) ) { |
|
| 113 | - return array( $type['uri'] ); |
|
| 114 | - } |
|
| 112 | + if ( isset( $type['uri'] ) ) { |
|
| 113 | + return array( $type['uri'] ); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - return null; |
|
| 116 | + return null; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -126,21 +126,21 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | function wl_schema_set_types( $post_id, $type_names ) { |
| 128 | 128 | |
| 129 | - // Some checks on the parameters |
|
| 130 | - if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) { |
|
| 131 | - return null; |
|
| 132 | - } |
|
| 129 | + // Some checks on the parameters |
|
| 130 | + if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) { |
|
| 131 | + return null; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - // TODO: support more than one type. |
|
| 135 | - if ( is_array( $type_names ) ) { |
|
| 136 | - $type_names = $type_names[0]; |
|
| 137 | - } |
|
| 134 | + // TODO: support more than one type. |
|
| 135 | + if ( is_array( $type_names ) ) { |
|
| 136 | + $type_names = $type_names[0]; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - // Get the schema URI (e.g. http://schema.org/Thing) |
|
| 140 | - $type_names = wl_build_full_schema_uri_from_schema_slug( $type_names ); |
|
| 139 | + // Get the schema URI (e.g. http://schema.org/Thing) |
|
| 140 | + $type_names = wl_build_full_schema_uri_from_schema_slug( $type_names ); |
|
| 141 | 141 | |
| 142 | - // Actually sets the taxonomy type |
|
| 143 | - wl_set_entity_main_type( $post_id, $type_names ); |
|
| 142 | + // Actually sets the taxonomy type |
|
| 143 | + wl_set_entity_main_type( $post_id, $type_names ); |
|
| 144 | 144 | |
| 145 | 145 | } |
| 146 | 146 | |
@@ -153,11 +153,11 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | function wl_build_full_schema_uri_from_schema_slug( $schema_name ) { |
| 155 | 155 | |
| 156 | - $schema_root_address = 'http://schema.org/'; |
|
| 156 | + $schema_root_address = 'http://schema.org/'; |
|
| 157 | 157 | |
| 158 | - if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary |
|
| 159 | - $schema_name = $schema_root_address . $schema_name; |
|
| 160 | - } |
|
| 158 | + if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary |
|
| 159 | + $schema_name = $schema_root_address . $schema_name; |
|
| 160 | + } |
|
| 161 | 161 | |
| 162 | - return $schema_name; |
|
| 162 | + return $schema_name; |
|
| 163 | 163 | } |
@@ -15,32 +15,32 @@ discard block |
||
| 15 | 15 | * |
| 16 | 16 | * @return array|null An array of values or NULL in case of no values (or error). |
| 17 | 17 | */ |
| 18 | -function wl_schema_get_value( $post_id, $property_name ) { |
|
| 18 | +function wl_schema_get_value($post_id, $property_name) { |
|
| 19 | 19 | |
| 20 | 20 | // Property name must be defined. |
| 21 | - if ( ! isset( $property_name ) || is_null( $property_name ) ) { |
|
| 21 | + if ( ! isset($property_name) || is_null($property_name)) { |
|
| 22 | 22 | return null; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | // store eventual schema name in different variable |
| 26 | - $property_schema_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 26 | + $property_schema_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 27 | 27 | |
| 28 | 28 | // Establish entity id. |
| 29 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 29 | + if (is_null($post_id) || ! is_numeric($post_id)) { |
|
| 30 | 30 | $post_id = get_the_ID(); |
| 31 | - if ( is_null( $post_id ) || ! is_numeric( $post_id ) ) { |
|
| 31 | + if (is_null($post_id) || ! is_numeric($post_id)) { |
|
| 32 | 32 | return null; |
| 33 | 33 | } |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | // Get custom fields. |
| 37 | - $term_mapping = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 37 | + $term_mapping = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 38 | 38 | // Search for the required meta value (by constant name or schema name) |
| 39 | - foreach ( $term_mapping as $wl_constant => $property_info ) { |
|
| 40 | - $found_constant = ( $wl_constant == $property_name ); |
|
| 41 | - $found_predicate = ( isset( $property_info['predicate'] ) && $property_info['predicate'] == $property_schema_name ); |
|
| 42 | - if ( $found_constant || $found_predicate ) { |
|
| 43 | - return get_post_meta( $post_id, $wl_constant ); |
|
| 39 | + foreach ($term_mapping as $wl_constant => $property_info) { |
|
| 40 | + $found_constant = ($wl_constant == $property_name); |
|
| 41 | + $found_predicate = (isset($property_info['predicate']) && $property_info['predicate'] == $property_schema_name); |
|
| 42 | + if ($found_constant || $found_predicate) { |
|
| 43 | + return get_post_meta($post_id, $wl_constant); |
|
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | |
@@ -56,33 +56,33 @@ discard block |
||
| 56 | 56 | * |
| 57 | 57 | * @return boolean The method returns true if everything went ok, an error string otherwise. |
| 58 | 58 | */ |
| 59 | -function wl_schema_set_value( $post_id, $property_name, $property_value ) { |
|
| 59 | +function wl_schema_set_value($post_id, $property_name, $property_value) { |
|
| 60 | 60 | |
| 61 | 61 | // Some checks on the parameters |
| 62 | - if ( ! is_numeric( $post_id ) || is_null( $property_name ) || empty( $property_value ) || is_null( $property_value ) ) { |
|
| 62 | + if ( ! is_numeric($post_id) || is_null($property_name) || empty($property_value) || is_null($property_value)) { |
|
| 63 | 63 | return false; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Build full schema uri if necessary |
| 67 | - $property_name = wl_build_full_schema_uri_from_schema_slug( $property_name ); |
|
| 67 | + $property_name = wl_build_full_schema_uri_from_schema_slug($property_name); |
|
| 68 | 68 | |
| 69 | 69 | // Get accepted properties |
| 70 | - $accepted_fields = wl_entity_taxonomy_get_custom_fields( $post_id ); |
|
| 70 | + $accepted_fields = wl_entity_taxonomy_get_custom_fields($post_id); |
|
| 71 | 71 | |
| 72 | 72 | // Find the name of the custom-field managing the schema property |
| 73 | - foreach ( $accepted_fields as $wl_constant => $field ) { |
|
| 74 | - if ( $field['predicate'] == $property_name ) { |
|
| 73 | + foreach ($accepted_fields as $wl_constant => $field) { |
|
| 74 | + if ($field['predicate'] == $property_name) { |
|
| 75 | 75 | |
| 76 | 76 | // Deal with single values |
| 77 | - if ( ! is_array( $property_value ) ) { |
|
| 78 | - $property_value = array( $property_value ); |
|
| 77 | + if ( ! is_array($property_value)) { |
|
| 78 | + $property_value = array($property_value); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | // Delete present meta |
| 82 | - delete_post_meta( $post_id, $wl_constant ); |
|
| 82 | + delete_post_meta($post_id, $wl_constant); |
|
| 83 | 83 | |
| 84 | - foreach ( $property_value as $value ) { |
|
| 85 | - add_post_meta( $post_id, $wl_constant, $value ); |
|
| 84 | + foreach ($property_value as $value) { |
|
| 85 | + add_post_meta($post_id, $wl_constant, $value); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | return true; |
@@ -100,17 +100,17 @@ discard block |
||
| 100 | 100 | * @return array Array of type(s) (e.g. Type, for the http://schema.org/Type) |
| 101 | 101 | * or NULL in case of no values (or error). |
| 102 | 102 | */ |
| 103 | -function wl_schema_get_types( $post_id ) { |
|
| 103 | +function wl_schema_get_types($post_id) { |
|
| 104 | 104 | |
| 105 | 105 | // Some checks on the parameters |
| 106 | - if ( ! is_numeric( $post_id ) ) { |
|
| 106 | + if ( ! is_numeric($post_id)) { |
|
| 107 | 107 | return null; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - $type = Wordlift_Entity_Type_Service::get_instance()->get( $post_id ); |
|
| 110 | + $type = Wordlift_Entity_Type_Service::get_instance()->get($post_id); |
|
| 111 | 111 | |
| 112 | - if ( isset( $type['uri'] ) ) { |
|
| 113 | - return array( $type['uri'] ); |
|
| 112 | + if (isset($type['uri'])) { |
|
| 113 | + return array($type['uri']); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | return null; |
@@ -124,23 +124,23 @@ discard block |
||
| 124 | 124 | * |
| 125 | 125 | * @return boolean True if everything went ok, an error string otherwise. |
| 126 | 126 | */ |
| 127 | -function wl_schema_set_types( $post_id, $type_names ) { |
|
| 127 | +function wl_schema_set_types($post_id, $type_names) { |
|
| 128 | 128 | |
| 129 | 129 | // Some checks on the parameters |
| 130 | - if ( ! is_numeric( $post_id ) || empty( $type_names ) || is_null( $type_names ) ) { |
|
| 130 | + if ( ! is_numeric($post_id) || empty($type_names) || is_null($type_names)) { |
|
| 131 | 131 | return null; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | // TODO: support more than one type. |
| 135 | - if ( is_array( $type_names ) ) { |
|
| 135 | + if (is_array($type_names)) { |
|
| 136 | 136 | $type_names = $type_names[0]; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | // Get the schema URI (e.g. http://schema.org/Thing) |
| 140 | - $type_names = wl_build_full_schema_uri_from_schema_slug( $type_names ); |
|
| 140 | + $type_names = wl_build_full_schema_uri_from_schema_slug($type_names); |
|
| 141 | 141 | |
| 142 | 142 | // Actually sets the taxonomy type |
| 143 | - wl_set_entity_main_type( $post_id, $type_names ); |
|
| 143 | + wl_set_entity_main_type($post_id, $type_names); |
|
| 144 | 144 | |
| 145 | 145 | } |
| 146 | 146 | |
@@ -151,12 +151,12 @@ discard block |
||
| 151 | 151 | * |
| 152 | 152 | * @return string The full schema uri (es. 'latitude' returns 'http://schema.org/latitude') |
| 153 | 153 | */ |
| 154 | -function wl_build_full_schema_uri_from_schema_slug( $schema_name ) { |
|
| 154 | +function wl_build_full_schema_uri_from_schema_slug($schema_name) { |
|
| 155 | 155 | |
| 156 | 156 | $schema_root_address = 'http://schema.org/'; |
| 157 | 157 | |
| 158 | - if ( strpos( $schema_name, $schema_root_address ) === false ) { // === necessary |
|
| 159 | - $schema_name = $schema_root_address . $schema_name; |
|
| 158 | + if (strpos($schema_name, $schema_root_address) === false) { // === necessary |
|
| 159 | + $schema_name = $schema_root_address.$schema_name; |
|
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | return $schema_name; |
@@ -14,374 +14,374 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Wordlift_Rating_Service { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Entity rating max. |
|
| 19 | - * |
|
| 20 | - * @since 3.3.0 |
|
| 21 | - */ |
|
| 22 | - const RATING_MAX = 7; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Entity rating score meta key. |
|
| 26 | - * |
|
| 27 | - * @since 3.3.0 |
|
| 28 | - */ |
|
| 29 | - const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Entity rating warnings meta key. |
|
| 33 | - * |
|
| 34 | - * @since 3.3.0 |
|
| 35 | - */ |
|
| 36 | - const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Entity warning has related post identifier. |
|
| 40 | - * |
|
| 41 | - * @since 3.3.0 |
|
| 42 | - */ |
|
| 43 | - const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Entity warning has content post identifier. |
|
| 47 | - * |
|
| 48 | - * @since 3.3.0 |
|
| 49 | - */ |
|
| 50 | - const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Entity warning has related entities identifier. |
|
| 54 | - * |
|
| 55 | - * @since 3.3.0 |
|
| 56 | - */ |
|
| 57 | - const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Entity warning is published identifier. |
|
| 61 | - * |
|
| 62 | - * @since 3.3.0 |
|
| 63 | - */ |
|
| 64 | - const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Entity warning has thumbnail identifier. |
|
| 68 | - * |
|
| 69 | - * @since 3.3.0 |
|
| 70 | - */ |
|
| 71 | - const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Entity warning has same as identifier. |
|
| 75 | - * |
|
| 76 | - * @since 3.3.0 |
|
| 77 | - */ |
|
| 78 | - const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Entity warning has completed metadata identifier. |
|
| 82 | - * |
|
| 83 | - * @since 3.3.0 |
|
| 84 | - */ |
|
| 85 | - const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 89 | - * |
|
| 90 | - * @since 3.10.0 |
|
| 91 | - * @access private |
|
| 92 | - * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 93 | - */ |
|
| 94 | - private $entity_service; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 98 | - * |
|
| 99 | - * @since 3.10.0 |
|
| 100 | - * @access private |
|
| 101 | - * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 102 | - */ |
|
| 103 | - private $entity_type_service; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The Notice service. |
|
| 107 | - * |
|
| 108 | - * @since 3.3.0 |
|
| 109 | - * @access private |
|
| 110 | - * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 111 | - */ |
|
| 112 | - private $notice_service; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * A {@link Wordlift_Log_Service} instance. |
|
| 116 | - * |
|
| 117 | - * @since 3.10.0 |
|
| 118 | - * @access private |
|
| 119 | - * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 120 | - */ |
|
| 121 | - private $log; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Create a {@link Wordlift_Rating_Service} instance. |
|
| 125 | - * |
|
| 126 | - * @since 3.10.0 |
|
| 127 | - * |
|
| 128 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 129 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 130 | - * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
|
| 131 | - */ |
|
| 132 | - public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 133 | - |
|
| 134 | - $this->entity_service = $entity_service; |
|
| 135 | - $this->entity_type_service = $entity_type_service; |
|
| 136 | - $this->notice_service = $notice_service; |
|
| 137 | - |
|
| 138 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 139 | - |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Set rating for a given entity. |
|
| 144 | - * |
|
| 145 | - * @since 3.3.0 |
|
| 146 | - * |
|
| 147 | - * @param int $post_id The entity post id. |
|
| 148 | - * |
|
| 149 | - * @return array An array representing the rating obj. |
|
| 150 | - */ |
|
| 151 | - public function set_rating_for( $post_id ) { |
|
| 152 | - |
|
| 153 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 154 | - return; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // Calculate rating for the given post. |
|
| 158 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 159 | - |
|
| 160 | - // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 161 | - // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 162 | - // |
|
| 163 | - // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 164 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 165 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 166 | - |
|
| 167 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 168 | - |
|
| 169 | - // Finally returns the rating |
|
| 170 | - return $rating; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Get or calculate rating for a given entity |
|
| 176 | - * |
|
| 177 | - * @since 3.3.0 |
|
| 178 | - * |
|
| 179 | - * @param int $post_id The entity post id. |
|
| 180 | - * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 181 | - * |
|
| 182 | - * @return array An array representing the rating obj. |
|
| 183 | - */ |
|
| 184 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 185 | - |
|
| 186 | - // If forced reload is required or rating is missing. |
|
| 187 | - if ( $force_reload ) { |
|
| 188 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 189 | - |
|
| 190 | - return $this->set_rating_for( $post_id ); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 194 | - |
|
| 195 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 196 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 197 | - |
|
| 198 | - return $this->set_rating_for( $post_id ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 202 | - |
|
| 203 | - // Finally return score and warnings |
|
| 204 | - return array( |
|
| 205 | - 'raw_score' => $current_raw_score, |
|
| 206 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 207 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 208 | - 'warnings' => $current_warnings, |
|
| 209 | - ); |
|
| 210 | - |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Calculate rating for a given entity. |
|
| 215 | - * |
|
| 216 | - * Rating depends from following criteria: |
|
| 217 | - * |
|
| 218 | - * 1. Is the current entity related to at least 1 post? |
|
| 219 | - * 2. Is the current entity content post not empty? |
|
| 220 | - * 3. Is the current entity related to at least 1 entity? |
|
| 221 | - * 4. Is the entity published? |
|
| 222 | - * 5. There is a a thumbnail associated to the entity? |
|
| 223 | - * 6. Has the entity a sameas defined? |
|
| 224 | - * 7. Are all schema.org required metadata compiled? |
|
| 225 | - * |
|
| 226 | - * Each positive check means +1 in terms of rating score. |
|
| 227 | - * |
|
| 228 | - * @since 3.3.0 |
|
| 229 | - * |
|
| 230 | - * @param int $post_id The entity post id. |
|
| 231 | - * |
|
| 232 | - * @return array An array representing the rating obj. |
|
| 233 | - */ |
|
| 234 | - private function calculate_rating_for( $post_id ) { |
|
| 235 | - |
|
| 236 | - // If it's not an entity, return. |
|
| 237 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 238 | - return array(); |
|
| 239 | - } |
|
| 240 | - // Retrieve the post object. |
|
| 241 | - $post = get_post( $post_id ); |
|
| 242 | - |
|
| 243 | - // Rating value. |
|
| 244 | - $score = 0; |
|
| 245 | - |
|
| 246 | - // Store warning messages. |
|
| 247 | - $warnings = array(); |
|
| 248 | - |
|
| 249 | - // Is the current entity related to at least 1 post? |
|
| 250 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 251 | - $score ++ : |
|
| 252 | - array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
| 253 | - |
|
| 254 | - // Is the post content not empty? |
|
| 255 | - ( ! empty( $post->post_content ) ) ? |
|
| 256 | - $score ++ : |
|
| 257 | - array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
| 258 | - |
|
| 259 | - // Is the current entity related to at least 1 entity? |
|
| 260 | - // Was the current entity already disambiguated? |
|
| 261 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 262 | - $score ++ : |
|
| 263 | - array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
| 264 | - |
|
| 265 | - // Is the entity published? |
|
| 266 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 267 | - $score ++ : |
|
| 268 | - array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
| 269 | - |
|
| 270 | - // Has a thumbnail? |
|
| 271 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 272 | - $score ++ : |
|
| 273 | - array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
| 274 | - |
|
| 275 | - // Get all post meta keys for the current post |
|
| 276 | - global $wpdb; |
|
| 277 | - |
|
| 278 | - $query = $wpdb->prepare( |
|
| 279 | - "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 280 | - ); |
|
| 281 | - |
|
| 282 | - // Check intersection between available meta keys and expected ones |
|
| 283 | - // arrays to detect missing values. |
|
| 284 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 285 | - |
|
| 286 | - // If each expected key is contained in available keys array ... |
|
| 287 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 288 | - $score ++ : |
|
| 289 | - array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
| 290 | - |
|
| 291 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 292 | - |
|
| 293 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 294 | - array() : |
|
| 295 | - array_keys( $schema['custom_fields'] ); |
|
| 296 | - |
|
| 297 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 298 | - // If each expected key is contained in available keys array ... |
|
| 299 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 300 | - $score ++ : |
|
| 301 | - array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
| 302 | - |
|
| 303 | - // Finally return score and warnings |
|
| 304 | - return array( |
|
| 305 | - 'raw_score' => $score, |
|
| 306 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 307 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 308 | - 'warnings' => $warnings, |
|
| 309 | - ); |
|
| 310 | - |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * Get as rating as input and convert in a traffic-light rating |
|
| 315 | - * |
|
| 316 | - * @since 3.3.0 |
|
| 317 | - * |
|
| 318 | - * @param int $score The rating score for a given entity. |
|
| 319 | - * |
|
| 320 | - * @return string The input HTML code. |
|
| 321 | - */ |
|
| 322 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 323 | - // RATING_MAX : $score = 3 : x |
|
| 324 | - // See http://php.net/manual/en/function.round.php |
|
| 325 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 326 | - |
|
| 327 | - // If rating is 0, return 1, otherwise return rating |
|
| 328 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 329 | - |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Get as rating as input and convert in a traffic-light rating |
|
| 334 | - * |
|
| 335 | - * @since 3.3.0 |
|
| 336 | - * |
|
| 337 | - * @param int $score The rating score for a given entity. |
|
| 338 | - * |
|
| 339 | - * @return string The input HTML code. |
|
| 340 | - */ |
|
| 341 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 342 | - |
|
| 343 | - // RATING_MAX : $score = 100 : x |
|
| 344 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Add admin notices for the current entity depending on the current rating. |
|
| 350 | - * |
|
| 351 | - * @since 3.3.0 |
|
| 352 | - */ |
|
| 353 | - public function in_admin_header() { |
|
| 354 | - |
|
| 355 | - // Return safely if get_current_screen() is not defined (yet) |
|
| 356 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 357 | - return; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - $screen = get_current_screen(); |
|
| 361 | - // If there is any valid screen nothing to do |
|
| 362 | - if ( null === $screen ) { |
|
| 363 | - return; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - // If you're not in the entity post edit page, return. |
|
| 367 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 368 | - return; |
|
| 369 | - } |
|
| 370 | - // Retrieve the current global post |
|
| 371 | - global $post; |
|
| 372 | - // If it's not an entity, return. |
|
| 373 | - if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 374 | - return; |
|
| 375 | - } |
|
| 376 | - // Retrieve an updated rating for the current entity |
|
| 377 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 378 | - |
|
| 379 | - // If there is at least 1 warning |
|
| 380 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 381 | - // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 382 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - } |
|
| 17 | + /** |
|
| 18 | + * Entity rating max. |
|
| 19 | + * |
|
| 20 | + * @since 3.3.0 |
|
| 21 | + */ |
|
| 22 | + const RATING_MAX = 7; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Entity rating score meta key. |
|
| 26 | + * |
|
| 27 | + * @since 3.3.0 |
|
| 28 | + */ |
|
| 29 | + const RATING_RAW_SCORE_META_KEY = '_wl_entity_rating_raw_score'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Entity rating warnings meta key. |
|
| 33 | + * |
|
| 34 | + * @since 3.3.0 |
|
| 35 | + */ |
|
| 36 | + const RATING_WARNINGS_META_KEY = '_wl_entity_rating_warnings'; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Entity warning has related post identifier. |
|
| 40 | + * |
|
| 41 | + * @since 3.3.0 |
|
| 42 | + */ |
|
| 43 | + const RATING_WARNING_HAS_RELATED_POSTS = 'There are no related posts for the current entity.'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Entity warning has content post identifier. |
|
| 47 | + * |
|
| 48 | + * @since 3.3.0 |
|
| 49 | + */ |
|
| 50 | + const RATING_WARNING_HAS_CONTENT_POST = 'This entity has not description.'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Entity warning has related entities identifier. |
|
| 54 | + * |
|
| 55 | + * @since 3.3.0 |
|
| 56 | + */ |
|
| 57 | + const RATING_WARNING_HAS_RELATED_ENTITIES = 'There are no related entities for the current entity.'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Entity warning is published identifier. |
|
| 61 | + * |
|
| 62 | + * @since 3.3.0 |
|
| 63 | + */ |
|
| 64 | + const RATING_WARNING_IS_PUBLISHED = 'This entity is not published. It will not appear within analysis results.'; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Entity warning has thumbnail identifier. |
|
| 68 | + * |
|
| 69 | + * @since 3.3.0 |
|
| 70 | + */ |
|
| 71 | + const RATING_WARNING_HAS_THUMBNAIL = 'This entity has no featured image yet.'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Entity warning has same as identifier. |
|
| 75 | + * |
|
| 76 | + * @since 3.3.0 |
|
| 77 | + */ |
|
| 78 | + const RATING_WARNING_HAS_SAME_AS = 'There are no sameAs configured for this entity.'; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Entity warning has completed metadata identifier. |
|
| 82 | + * |
|
| 83 | + * @since 3.3.0 |
|
| 84 | + */ |
|
| 85 | + const RATING_WARNING_HAS_COMPLETED_METADATA = 'Schema.org metadata for this entity are not completed.'; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 89 | + * |
|
| 90 | + * @since 3.10.0 |
|
| 91 | + * @access private |
|
| 92 | + * @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 93 | + */ |
|
| 94 | + private $entity_service; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 98 | + * |
|
| 99 | + * @since 3.10.0 |
|
| 100 | + * @access private |
|
| 101 | + * @var Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 102 | + */ |
|
| 103 | + private $entity_type_service; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The Notice service. |
|
| 107 | + * |
|
| 108 | + * @since 3.3.0 |
|
| 109 | + * @access private |
|
| 110 | + * @var \Wordlift_Notice_Service $notice_service The Notice service. |
|
| 111 | + */ |
|
| 112 | + private $notice_service; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * A {@link Wordlift_Log_Service} instance. |
|
| 116 | + * |
|
| 117 | + * @since 3.10.0 |
|
| 118 | + * @access private |
|
| 119 | + * @var Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 120 | + */ |
|
| 121 | + private $log; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Create a {@link Wordlift_Rating_Service} instance. |
|
| 125 | + * |
|
| 126 | + * @since 3.10.0 |
|
| 127 | + * |
|
| 128 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 129 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 130 | + * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
|
| 131 | + */ |
|
| 132 | + public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 133 | + |
|
| 134 | + $this->entity_service = $entity_service; |
|
| 135 | + $this->entity_type_service = $entity_type_service; |
|
| 136 | + $this->notice_service = $notice_service; |
|
| 137 | + |
|
| 138 | + $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 139 | + |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Set rating for a given entity. |
|
| 144 | + * |
|
| 145 | + * @since 3.3.0 |
|
| 146 | + * |
|
| 147 | + * @param int $post_id The entity post id. |
|
| 148 | + * |
|
| 149 | + * @return array An array representing the rating obj. |
|
| 150 | + */ |
|
| 151 | + public function set_rating_for( $post_id ) { |
|
| 152 | + |
|
| 153 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 154 | + return; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // Calculate rating for the given post. |
|
| 158 | + $rating = $this->calculate_rating_for( $post_id ); |
|
| 159 | + |
|
| 160 | + // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
|
| 161 | + // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
|
| 162 | + // |
|
| 163 | + // See https://codex.wordpress.org/Function_Reference/update_post_meta |
|
| 164 | + update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 165 | + update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 166 | + |
|
| 167 | + $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 168 | + |
|
| 169 | + // Finally returns the rating |
|
| 170 | + return $rating; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Get or calculate rating for a given entity |
|
| 176 | + * |
|
| 177 | + * @since 3.3.0 |
|
| 178 | + * |
|
| 179 | + * @param int $post_id The entity post id. |
|
| 180 | + * @param $force_reload $warnings_needed If true, detailed warnings collection is provided with the rating obj. |
|
| 181 | + * |
|
| 182 | + * @return array An array representing the rating obj. |
|
| 183 | + */ |
|
| 184 | + public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 185 | + |
|
| 186 | + // If forced reload is required or rating is missing. |
|
| 187 | + if ( $force_reload ) { |
|
| 188 | + $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 189 | + |
|
| 190 | + return $this->set_rating_for( $post_id ); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 194 | + |
|
| 195 | + if ( ! is_numeric( $current_raw_score ) ) { |
|
| 196 | + $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 197 | + |
|
| 198 | + return $this->set_rating_for( $post_id ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 202 | + |
|
| 203 | + // Finally return score and warnings |
|
| 204 | + return array( |
|
| 205 | + 'raw_score' => $current_raw_score, |
|
| 206 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 207 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 208 | + 'warnings' => $current_warnings, |
|
| 209 | + ); |
|
| 210 | + |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Calculate rating for a given entity. |
|
| 215 | + * |
|
| 216 | + * Rating depends from following criteria: |
|
| 217 | + * |
|
| 218 | + * 1. Is the current entity related to at least 1 post? |
|
| 219 | + * 2. Is the current entity content post not empty? |
|
| 220 | + * 3. Is the current entity related to at least 1 entity? |
|
| 221 | + * 4. Is the entity published? |
|
| 222 | + * 5. There is a a thumbnail associated to the entity? |
|
| 223 | + * 6. Has the entity a sameas defined? |
|
| 224 | + * 7. Are all schema.org required metadata compiled? |
|
| 225 | + * |
|
| 226 | + * Each positive check means +1 in terms of rating score. |
|
| 227 | + * |
|
| 228 | + * @since 3.3.0 |
|
| 229 | + * |
|
| 230 | + * @param int $post_id The entity post id. |
|
| 231 | + * |
|
| 232 | + * @return array An array representing the rating obj. |
|
| 233 | + */ |
|
| 234 | + private function calculate_rating_for( $post_id ) { |
|
| 235 | + |
|
| 236 | + // If it's not an entity, return. |
|
| 237 | + if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 238 | + return array(); |
|
| 239 | + } |
|
| 240 | + // Retrieve the post object. |
|
| 241 | + $post = get_post( $post_id ); |
|
| 242 | + |
|
| 243 | + // Rating value. |
|
| 244 | + $score = 0; |
|
| 245 | + |
|
| 246 | + // Store warning messages. |
|
| 247 | + $warnings = array(); |
|
| 248 | + |
|
| 249 | + // Is the current entity related to at least 1 post? |
|
| 250 | + ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 251 | + $score ++ : |
|
| 252 | + array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
| 253 | + |
|
| 254 | + // Is the post content not empty? |
|
| 255 | + ( ! empty( $post->post_content ) ) ? |
|
| 256 | + $score ++ : |
|
| 257 | + array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
| 258 | + |
|
| 259 | + // Is the current entity related to at least 1 entity? |
|
| 260 | + // Was the current entity already disambiguated? |
|
| 261 | + ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 262 | + $score ++ : |
|
| 263 | + array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
| 264 | + |
|
| 265 | + // Is the entity published? |
|
| 266 | + ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 267 | + $score ++ : |
|
| 268 | + array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
| 269 | + |
|
| 270 | + // Has a thumbnail? |
|
| 271 | + ( has_post_thumbnail( $post->ID ) ) ? |
|
| 272 | + $score ++ : |
|
| 273 | + array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
| 274 | + |
|
| 275 | + // Get all post meta keys for the current post |
|
| 276 | + global $wpdb; |
|
| 277 | + |
|
| 278 | + $query = $wpdb->prepare( |
|
| 279 | + "SELECT DISTINCT (meta_key) FROM $wpdb->postmeta WHERE post_id = %d", $post->ID |
|
| 280 | + ); |
|
| 281 | + |
|
| 282 | + // Check intersection between available meta keys and expected ones |
|
| 283 | + // arrays to detect missing values. |
|
| 284 | + $available_meta_keys = $wpdb->get_col( $query ); |
|
| 285 | + |
|
| 286 | + // If each expected key is contained in available keys array ... |
|
| 287 | + ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 288 | + $score ++ : |
|
| 289 | + array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
| 290 | + |
|
| 291 | + $schema = $this->entity_type_service->get( $post_id ); |
|
| 292 | + |
|
| 293 | + $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 294 | + array() : |
|
| 295 | + array_keys( $schema['custom_fields'] ); |
|
| 296 | + |
|
| 297 | + $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 298 | + // If each expected key is contained in available keys array ... |
|
| 299 | + ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 300 | + $score ++ : |
|
| 301 | + array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
| 302 | + |
|
| 303 | + // Finally return score and warnings |
|
| 304 | + return array( |
|
| 305 | + 'raw_score' => $score, |
|
| 306 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 307 | + 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 308 | + 'warnings' => $warnings, |
|
| 309 | + ); |
|
| 310 | + |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * Get as rating as input and convert in a traffic-light rating |
|
| 315 | + * |
|
| 316 | + * @since 3.3.0 |
|
| 317 | + * |
|
| 318 | + * @param int $score The rating score for a given entity. |
|
| 319 | + * |
|
| 320 | + * @return string The input HTML code. |
|
| 321 | + */ |
|
| 322 | + private function convert_raw_score_to_traffic_light( $score ) { |
|
| 323 | + // RATING_MAX : $score = 3 : x |
|
| 324 | + // See http://php.net/manual/en/function.round.php |
|
| 325 | + $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 326 | + |
|
| 327 | + // If rating is 0, return 1, otherwise return rating |
|
| 328 | + return ( 0 == $rating ) ? 1 : $rating; |
|
| 329 | + |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Get as rating as input and convert in a traffic-light rating |
|
| 334 | + * |
|
| 335 | + * @since 3.3.0 |
|
| 336 | + * |
|
| 337 | + * @param int $score The rating score for a given entity. |
|
| 338 | + * |
|
| 339 | + * @return string The input HTML code. |
|
| 340 | + */ |
|
| 341 | + public function convert_raw_score_to_percentage( $score ) { |
|
| 342 | + |
|
| 343 | + // RATING_MAX : $score = 100 : x |
|
| 344 | + return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Add admin notices for the current entity depending on the current rating. |
|
| 350 | + * |
|
| 351 | + * @since 3.3.0 |
|
| 352 | + */ |
|
| 353 | + public function in_admin_header() { |
|
| 354 | + |
|
| 355 | + // Return safely if get_current_screen() is not defined (yet) |
|
| 356 | + if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 357 | + return; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + $screen = get_current_screen(); |
|
| 361 | + // If there is any valid screen nothing to do |
|
| 362 | + if ( null === $screen ) { |
|
| 363 | + return; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + // If you're not in the entity post edit page, return. |
|
| 367 | + if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 368 | + return; |
|
| 369 | + } |
|
| 370 | + // Retrieve the current global post |
|
| 371 | + global $post; |
|
| 372 | + // If it's not an entity, return. |
|
| 373 | + if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 374 | + return; |
|
| 375 | + } |
|
| 376 | + // Retrieve an updated rating for the current entity |
|
| 377 | + $rating = $this->get_rating_for( $post->ID, true ); |
|
| 378 | + |
|
| 379 | + // If there is at least 1 warning |
|
| 380 | + if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 381 | + // TODO - Pass Wordlift_Notice_Service trough the service constructor |
|
| 382 | + $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | 387 | } |
@@ -129,13 +129,13 @@ discard block |
||
| 129 | 129 | * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
| 130 | 130 | * @param \Wordlift_Notice_Service $notice_service A {@link Wordlift_Notice_Service} instance. |
| 131 | 131 | */ |
| 132 | - public function __construct( $entity_service, $entity_type_service, $notice_service ) { |
|
| 132 | + public function __construct($entity_service, $entity_type_service, $notice_service) { |
|
| 133 | 133 | |
| 134 | 134 | $this->entity_service = $entity_service; |
| 135 | 135 | $this->entity_type_service = $entity_type_service; |
| 136 | 136 | $this->notice_service = $notice_service; |
| 137 | 137 | |
| 138 | - $this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rating_Service' ); |
|
| 138 | + $this->log = Wordlift_Log_Service::get_logger('Wordlift_Rating_Service'); |
|
| 139 | 139 | |
| 140 | 140 | } |
| 141 | 141 | |
@@ -148,23 +148,23 @@ discard block |
||
| 148 | 148 | * |
| 149 | 149 | * @return array An array representing the rating obj. |
| 150 | 150 | */ |
| 151 | - public function set_rating_for( $post_id ) { |
|
| 151 | + public function set_rating_for($post_id) { |
|
| 152 | 152 | |
| 153 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 153 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 154 | 154 | return; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | // Calculate rating for the given post. |
| 158 | - $rating = $this->calculate_rating_for( $post_id ); |
|
| 158 | + $rating = $this->calculate_rating_for($post_id); |
|
| 159 | 159 | |
| 160 | 160 | // Store the rating on db as post meta. Please notice that RATING_RAW_SCORE_META_KEY |
| 161 | 161 | // is saved on a different meta to allow score sorting. Both meta are managed as unique. |
| 162 | 162 | // |
| 163 | 163 | // See https://codex.wordpress.org/Function_Reference/update_post_meta |
| 164 | - update_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score'] ); |
|
| 165 | - update_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings'] ); |
|
| 164 | + update_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, $rating['raw_score']); |
|
| 165 | + update_post_meta($post_id, self::RATING_WARNINGS_META_KEY, $rating['warnings']); |
|
| 166 | 166 | |
| 167 | - $this->log->trace( sprintf( "Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'] ) ); |
|
| 167 | + $this->log->trace(sprintf("Rating set for [ post_id :: $post_id ] [ rating :: %s ]", $rating['raw_score'])); |
|
| 168 | 168 | |
| 169 | 169 | // Finally returns the rating |
| 170 | 170 | return $rating; |
@@ -181,30 +181,30 @@ discard block |
||
| 181 | 181 | * |
| 182 | 182 | * @return array An array representing the rating obj. |
| 183 | 183 | */ |
| 184 | - public function get_rating_for( $post_id, $force_reload = false ) { |
|
| 184 | + public function get_rating_for($post_id, $force_reload = false) { |
|
| 185 | 185 | |
| 186 | 186 | // If forced reload is required or rating is missing. |
| 187 | - if ( $force_reload ) { |
|
| 188 | - $this->log->trace( "Force rating reload [ post_id :: $post_id ]" ); |
|
| 187 | + if ($force_reload) { |
|
| 188 | + $this->log->trace("Force rating reload [ post_id :: $post_id ]"); |
|
| 189 | 189 | |
| 190 | - return $this->set_rating_for( $post_id ); |
|
| 190 | + return $this->set_rating_for($post_id); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | - $current_raw_score = get_post_meta( $post_id, self::RATING_RAW_SCORE_META_KEY, true ); |
|
| 193 | + $current_raw_score = get_post_meta($post_id, self::RATING_RAW_SCORE_META_KEY, true); |
|
| 194 | 194 | |
| 195 | - if ( ! is_numeric( $current_raw_score ) ) { |
|
| 196 | - $this->log->trace( "Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]" ); |
|
| 195 | + if ( ! is_numeric($current_raw_score)) { |
|
| 196 | + $this->log->trace("Rating missing for [ post_id :: $post_id ] [ current_raw_score :: $current_raw_score ]"); |
|
| 197 | 197 | |
| 198 | - return $this->set_rating_for( $post_id ); |
|
| 198 | + return $this->set_rating_for($post_id); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - $current_warnings = get_post_meta( $post_id, self::RATING_WARNINGS_META_KEY, true ); |
|
| 201 | + $current_warnings = get_post_meta($post_id, self::RATING_WARNINGS_META_KEY, true); |
|
| 202 | 202 | |
| 203 | 203 | // Finally return score and warnings |
| 204 | 204 | return array( |
| 205 | 205 | 'raw_score' => $current_raw_score, |
| 206 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $current_raw_score ), |
|
| 207 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $current_raw_score ), |
|
| 206 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($current_raw_score), |
|
| 207 | + 'percentage_score' => $this->convert_raw_score_to_percentage($current_raw_score), |
|
| 208 | 208 | 'warnings' => $current_warnings, |
| 209 | 209 | ); |
| 210 | 210 | |
@@ -231,14 +231,14 @@ discard block |
||
| 231 | 231 | * |
| 232 | 232 | * @return array An array representing the rating obj. |
| 233 | 233 | */ |
| 234 | - private function calculate_rating_for( $post_id ) { |
|
| 234 | + private function calculate_rating_for($post_id) { |
|
| 235 | 235 | |
| 236 | 236 | // If it's not an entity, return. |
| 237 | - if ( ! $this->entity_service->is_entity( $post_id ) ) { |
|
| 237 | + if ( ! $this->entity_service->is_entity($post_id)) { |
|
| 238 | 238 | return array(); |
| 239 | 239 | } |
| 240 | 240 | // Retrieve the post object. |
| 241 | - $post = get_post( $post_id ); |
|
| 241 | + $post = get_post($post_id); |
|
| 242 | 242 | |
| 243 | 243 | // Rating value. |
| 244 | 244 | $score = 0; |
@@ -247,30 +247,25 @@ discard block |
||
| 247 | 247 | $warnings = array(); |
| 248 | 248 | |
| 249 | 249 | // Is the current entity related to at least 1 post? |
| 250 | - ( 0 < count( wl_core_get_related_post_ids( $post->ID ) ) ) ? |
|
| 251 | - $score ++ : |
|
| 252 | - array_push( $warnings, __( 'There are no related posts for the current entity.', 'wordlift' ) ); |
|
| 250 | + (0 < count(wl_core_get_related_post_ids($post->ID))) ? |
|
| 251 | + $score++ : array_push($warnings, __('There are no related posts for the current entity.', 'wordlift')); |
|
| 253 | 252 | |
| 254 | 253 | // Is the post content not empty? |
| 255 | - ( ! empty( $post->post_content ) ) ? |
|
| 256 | - $score ++ : |
|
| 257 | - array_push( $warnings, __( 'This entity has not description.', 'wordlift' ) ); |
|
| 254 | + ( ! empty($post->post_content)) ? |
|
| 255 | + $score++ : array_push($warnings, __('This entity has not description.', 'wordlift')); |
|
| 258 | 256 | |
| 259 | 257 | // Is the current entity related to at least 1 entity? |
| 260 | 258 | // Was the current entity already disambiguated? |
| 261 | - ( 0 < count( wl_core_get_related_entity_ids( $post->ID ) ) ) ? |
|
| 262 | - $score ++ : |
|
| 263 | - array_push( $warnings, __( 'There are no related entities for the current entity.', 'wordlift' ) ); |
|
| 259 | + (0 < count(wl_core_get_related_entity_ids($post->ID))) ? |
|
| 260 | + $score++ : array_push($warnings, __('There are no related entities for the current entity.', 'wordlift')); |
|
| 264 | 261 | |
| 265 | 262 | // Is the entity published? |
| 266 | - ( 'publish' === get_post_status( $post->ID ) ) ? |
|
| 267 | - $score ++ : |
|
| 268 | - array_push( $warnings, __( 'This entity is not published. It will not appear within analysis results.', 'wordlift' ) ); |
|
| 263 | + ('publish' === get_post_status($post->ID)) ? |
|
| 264 | + $score++ : array_push($warnings, __('This entity is not published. It will not appear within analysis results.', 'wordlift')); |
|
| 269 | 265 | |
| 270 | 266 | // Has a thumbnail? |
| 271 | - ( has_post_thumbnail( $post->ID ) ) ? |
|
| 272 | - $score ++ : |
|
| 273 | - array_push( $warnings, __( 'This entity has no featured image yet.', 'wordlift' ) ); |
|
| 267 | + (has_post_thumbnail($post->ID)) ? |
|
| 268 | + $score++ : array_push($warnings, __('This entity has no featured image yet.', 'wordlift')); |
|
| 274 | 269 | |
| 275 | 270 | // Get all post meta keys for the current post |
| 276 | 271 | global $wpdb; |
@@ -281,30 +276,27 @@ discard block |
||
| 281 | 276 | |
| 282 | 277 | // Check intersection between available meta keys and expected ones |
| 283 | 278 | // arrays to detect missing values. |
| 284 | - $available_meta_keys = $wpdb->get_col( $query ); |
|
| 279 | + $available_meta_keys = $wpdb->get_col($query); |
|
| 285 | 280 | |
| 286 | 281 | // If each expected key is contained in available keys array ... |
| 287 | - ( in_array( Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys ) ) ? |
|
| 288 | - $score ++ : |
|
| 289 | - array_push( $warnings, __( 'There are no sameAs configured for this entity.', 'wordlift' ) ); |
|
| 282 | + (in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys)) ? |
|
| 283 | + $score++ : array_push($warnings, __('There are no sameAs configured for this entity.', 'wordlift')); |
|
| 290 | 284 | |
| 291 | - $schema = $this->entity_type_service->get( $post_id ); |
|
| 285 | + $schema = $this->entity_type_service->get($post_id); |
|
| 292 | 286 | |
| 293 | - $expected_meta_keys = ( null === $schema['custom_fields'] ) ? |
|
| 294 | - array() : |
|
| 295 | - array_keys( $schema['custom_fields'] ); |
|
| 287 | + $expected_meta_keys = (null === $schema['custom_fields']) ? |
|
| 288 | + array() : array_keys($schema['custom_fields']); |
|
| 296 | 289 | |
| 297 | - $intersection = array_intersect( $expected_meta_keys, $available_meta_keys ); |
|
| 290 | + $intersection = array_intersect($expected_meta_keys, $available_meta_keys); |
|
| 298 | 291 | // If each expected key is contained in available keys array ... |
| 299 | - ( count( $intersection ) === count( $expected_meta_keys ) ) ? |
|
| 300 | - $score ++ : |
|
| 301 | - array_push( $warnings, __( 'Schema.org metadata for this entity are not completed.', 'wordlift' ) ); |
|
| 292 | + (count($intersection) === count($expected_meta_keys)) ? |
|
| 293 | + $score++ : array_push($warnings, __('Schema.org metadata for this entity are not completed.', 'wordlift')); |
|
| 302 | 294 | |
| 303 | 295 | // Finally return score and warnings |
| 304 | 296 | return array( |
| 305 | 297 | 'raw_score' => $score, |
| 306 | - 'traffic_light_score' => $this->convert_raw_score_to_traffic_light( $score ), |
|
| 307 | - 'percentage_score' => $this->convert_raw_score_to_percentage( $score ), |
|
| 298 | + 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), |
|
| 299 | + 'percentage_score' => $this->convert_raw_score_to_percentage($score), |
|
| 308 | 300 | 'warnings' => $warnings, |
| 309 | 301 | ); |
| 310 | 302 | |
@@ -319,13 +311,13 @@ discard block |
||
| 319 | 311 | * |
| 320 | 312 | * @return string The input HTML code. |
| 321 | 313 | */ |
| 322 | - private function convert_raw_score_to_traffic_light( $score ) { |
|
| 314 | + private function convert_raw_score_to_traffic_light($score) { |
|
| 323 | 315 | // RATING_MAX : $score = 3 : x |
| 324 | 316 | // See http://php.net/manual/en/function.round.php |
| 325 | - $rating = round( ( $score * 3 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 317 | + $rating = round(($score * 3) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 326 | 318 | |
| 327 | 319 | // If rating is 0, return 1, otherwise return rating |
| 328 | - return ( 0 == $rating ) ? 1 : $rating; |
|
| 320 | + return (0 == $rating) ? 1 : $rating; |
|
| 329 | 321 | |
| 330 | 322 | } |
| 331 | 323 | |
@@ -338,10 +330,10 @@ discard block |
||
| 338 | 330 | * |
| 339 | 331 | * @return string The input HTML code. |
| 340 | 332 | */ |
| 341 | - public function convert_raw_score_to_percentage( $score ) { |
|
| 333 | + public function convert_raw_score_to_percentage($score) { |
|
| 342 | 334 | |
| 343 | 335 | // RATING_MAX : $score = 100 : x |
| 344 | - return round( ( $score * 100 ) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP ); |
|
| 336 | + return round(($score * 100) / self::RATING_MAX, 0, PHP_ROUND_HALF_UP); |
|
| 345 | 337 | } |
| 346 | 338 | |
| 347 | 339 | |
@@ -353,33 +345,33 @@ discard block |
||
| 353 | 345 | public function in_admin_header() { |
| 354 | 346 | |
| 355 | 347 | // Return safely if get_current_screen() is not defined (yet) |
| 356 | - if ( false === function_exists( 'get_current_screen' ) ) { |
|
| 348 | + if (false === function_exists('get_current_screen')) { |
|
| 357 | 349 | return; |
| 358 | 350 | } |
| 359 | 351 | |
| 360 | 352 | $screen = get_current_screen(); |
| 361 | 353 | // If there is any valid screen nothing to do |
| 362 | - if ( null === $screen ) { |
|
| 354 | + if (null === $screen) { |
|
| 363 | 355 | return; |
| 364 | 356 | } |
| 365 | 357 | |
| 366 | 358 | // If you're not in the entity post edit page, return. |
| 367 | - if ( Wordlift_Entity_Service::TYPE_NAME !== $screen->id ) { |
|
| 359 | + if (Wordlift_Entity_Service::TYPE_NAME !== $screen->id) { |
|
| 368 | 360 | return; |
| 369 | 361 | } |
| 370 | 362 | // Retrieve the current global post |
| 371 | 363 | global $post; |
| 372 | 364 | // If it's not an entity, return. |
| 373 | - if ( ! $this->entity_service->is_entity( $post->ID ) ) { |
|
| 365 | + if ( ! $this->entity_service->is_entity($post->ID)) { |
|
| 374 | 366 | return; |
| 375 | 367 | } |
| 376 | 368 | // Retrieve an updated rating for the current entity |
| 377 | - $rating = $this->get_rating_for( $post->ID, true ); |
|
| 369 | + $rating = $this->get_rating_for($post->ID, true); |
|
| 378 | 370 | |
| 379 | 371 | // If there is at least 1 warning |
| 380 | - if ( isset( $rating['warnings'] ) && 0 < count( $rating['warnings'] ) ) { |
|
| 372 | + if (isset($rating['warnings']) && 0 < count($rating['warnings'])) { |
|
| 381 | 373 | // TODO - Pass Wordlift_Notice_Service trough the service constructor |
| 382 | - $this->notice_service->add_suggestion( $rating['warnings'] ); |
|
| 374 | + $this->notice_service->add_suggestion($rating['warnings']); |
|
| 383 | 375 | } |
| 384 | 376 | |
| 385 | 377 | } |
@@ -18,63 +18,63 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_Author_Storage extends Wordlift_Post_Property_Storage { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 27 | - */ |
|
| 28 | - private $entity_service; |
|
| 21 | + /** |
|
| 22 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 27 | + */ |
|
| 28 | + private $entity_service; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * The {@link Wordlift_User_Service} instance. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * @access private |
|
| 35 | - * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 36 | - */ |
|
| 37 | - private $user_service; |
|
| 30 | + /** |
|
| 31 | + * The {@link Wordlift_User_Service} instance. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * @access private |
|
| 35 | + * @var \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 36 | + */ |
|
| 37 | + private $user_service; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Create a {@link Wordlift_Post_Author_Storage} instance. |
|
| 41 | - * |
|
| 42 | - * @since 3.15.0 |
|
| 43 | - * |
|
| 44 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 45 | - * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 46 | - */ |
|
| 47 | - public function __construct( $entity_service, $user_service ) { |
|
| 48 | - parent::__construct( Wordlift_Post_Property_Storage::AUTHOR ); |
|
| 39 | + /** |
|
| 40 | + * Create a {@link Wordlift_Post_Author_Storage} instance. |
|
| 41 | + * |
|
| 42 | + * @since 3.15.0 |
|
| 43 | + * |
|
| 44 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 45 | + * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
|
| 46 | + */ |
|
| 47 | + public function __construct( $entity_service, $user_service ) { |
|
| 48 | + parent::__construct( Wordlift_Post_Property_Storage::AUTHOR ); |
|
| 49 | 49 | |
| 50 | - $this->entity_service = $entity_service; |
|
| 51 | - $this->user_service = $user_service; |
|
| 50 | + $this->entity_service = $entity_service; |
|
| 51 | + $this->user_service = $user_service; |
|
| 52 | 52 | |
| 53 | - } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get the property value. |
|
| 57 | - * |
|
| 58 | - * @since 3.15.0 |
|
| 59 | - * |
|
| 60 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 61 | - * |
|
| 62 | - * @return array|string|null A single string, or an array of values or null |
|
| 63 | - * if the property isn't recognized. |
|
| 64 | - */ |
|
| 65 | - public function get( $post_id ) { |
|
| 66 | - $author_id = parent::get( $post_id ); |
|
| 55 | + /** |
|
| 56 | + * Get the property value. |
|
| 57 | + * |
|
| 58 | + * @since 3.15.0 |
|
| 59 | + * |
|
| 60 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 61 | + * |
|
| 62 | + * @return array|string|null A single string, or an array of values or null |
|
| 63 | + * if the property isn't recognized. |
|
| 64 | + */ |
|
| 65 | + public function get( $post_id ) { |
|
| 66 | + $author_id = parent::get( $post_id ); |
|
| 67 | 67 | |
| 68 | - // Get the entity bound to this user. |
|
| 69 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 68 | + // Get the entity bound to this user. |
|
| 69 | + $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 70 | 70 | |
| 71 | - // If there's no entity bound return a simple author structure. |
|
| 72 | - if ( empty( $entity_id ) ) { |
|
| 73 | - return $this->user_service->get_uri( $author_id ); |
|
| 74 | - } |
|
| 71 | + // If there's no entity bound return a simple author structure. |
|
| 72 | + if ( empty( $entity_id ) ) { |
|
| 73 | + return $this->user_service->get_uri( $author_id ); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // Return the entity URI. |
|
| 77 | - return $this->entity_service->get_uri( $entity_id ); |
|
| 78 | - } |
|
| 76 | + // Return the entity URI. |
|
| 77 | + return $this->entity_service->get_uri( $entity_id ); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | 80 | } |
@@ -44,8 +44,8 @@ discard block |
||
| 44 | 44 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 45 | 45 | * @param \Wordlift_User_Service $user_service The {@link Wordlift_User_Service} instance. |
| 46 | 46 | */ |
| 47 | - public function __construct( $entity_service, $user_service ) { |
|
| 48 | - parent::__construct( Wordlift_Post_Property_Storage::AUTHOR ); |
|
| 47 | + public function __construct($entity_service, $user_service) { |
|
| 48 | + parent::__construct(Wordlift_Post_Property_Storage::AUTHOR); |
|
| 49 | 49 | |
| 50 | 50 | $this->entity_service = $entity_service; |
| 51 | 51 | $this->user_service = $user_service; |
@@ -62,19 +62,19 @@ discard block |
||
| 62 | 62 | * @return array|string|null A single string, or an array of values or null |
| 63 | 63 | * if the property isn't recognized. |
| 64 | 64 | */ |
| 65 | - public function get( $post_id ) { |
|
| 66 | - $author_id = parent::get( $post_id ); |
|
| 65 | + public function get($post_id) { |
|
| 66 | + $author_id = parent::get($post_id); |
|
| 67 | 67 | |
| 68 | 68 | // Get the entity bound to this user. |
| 69 | - $entity_id = $this->user_service->get_entity( $author_id ); |
|
| 69 | + $entity_id = $this->user_service->get_entity($author_id); |
|
| 70 | 70 | |
| 71 | 71 | // If there's no entity bound return a simple author structure. |
| 72 | - if ( empty( $entity_id ) ) { |
|
| 73 | - return $this->user_service->get_uri( $author_id ); |
|
| 72 | + if (empty($entity_id)) { |
|
| 73 | + return $this->user_service->get_uri($author_id); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // Return the entity URI. |
| 77 | - return $this->entity_service->get_uri( $entity_id ); |
|
| 77 | + return $this->entity_service->get_uri($entity_id); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | } |
@@ -18,48 +18,48 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_Taxonomy_Storage extends Wordlift_Storage { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The taxonomy name. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var string $taxonomy The taxonomy name. |
|
| 27 | - */ |
|
| 28 | - private $taxonomy; |
|
| 21 | + /** |
|
| 22 | + * The taxonomy name. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var string $taxonomy The taxonomy name. |
|
| 27 | + */ |
|
| 28 | + private $taxonomy; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Create a {@link Wordlift_Post_Taxonomy_Storage} with the specified |
|
| 32 | - * taxonomy name. |
|
| 33 | - * |
|
| 34 | - * @since 3.15.0 |
|
| 35 | - * |
|
| 36 | - * @param string $taxonomy The taxonomy name. |
|
| 37 | - */ |
|
| 38 | - public function __construct( $taxonomy ) { |
|
| 30 | + /** |
|
| 31 | + * Create a {@link Wordlift_Post_Taxonomy_Storage} with the specified |
|
| 32 | + * taxonomy name. |
|
| 33 | + * |
|
| 34 | + * @since 3.15.0 |
|
| 35 | + * |
|
| 36 | + * @param string $taxonomy The taxonomy name. |
|
| 37 | + */ |
|
| 38 | + public function __construct( $taxonomy ) { |
|
| 39 | 39 | |
| 40 | - $this->taxonomy = $taxonomy; |
|
| 41 | - } |
|
| 40 | + $this->taxonomy = $taxonomy; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get the taxonomy's terms associated with the specified {@link WP_Post}. |
|
| 45 | - * |
|
| 46 | - * @since 3.15.0 |
|
| 47 | - * |
|
| 48 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 49 | - * |
|
| 50 | - * @return array|WP_Error An array of terms or {@link WP_Error} in case of error. |
|
| 51 | - */ |
|
| 52 | - public function get( $post_id ) { |
|
| 43 | + /** |
|
| 44 | + * Get the taxonomy's terms associated with the specified {@link WP_Post}. |
|
| 45 | + * |
|
| 46 | + * @since 3.15.0 |
|
| 47 | + * |
|
| 48 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 49 | + * |
|
| 50 | + * @return array|WP_Error An array of terms or {@link WP_Error} in case of error. |
|
| 51 | + */ |
|
| 52 | + public function get( $post_id ) { |
|
| 53 | 53 | |
| 54 | - return wp_get_post_terms( $post_id, $this->taxonomy, array( |
|
| 55 | - 'hide_empty' => false, |
|
| 56 | - // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 57 | - // An issue has been opened with the AAM plugin author as well. |
|
| 58 | - // |
|
| 59 | - // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 60 | - // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 61 | - 'fields' => 'all', |
|
| 62 | - ) ); |
|
| 63 | - } |
|
| 54 | + return wp_get_post_terms( $post_id, $this->taxonomy, array( |
|
| 55 | + 'hide_empty' => false, |
|
| 56 | + // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
|
| 57 | + // An issue has been opened with the AAM plugin author as well. |
|
| 58 | + // |
|
| 59 | + // see https://github.com/insideout10/wordlift-plugin/issues/334 |
|
| 60 | + // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
|
| 61 | + 'fields' => 'all', |
|
| 62 | + ) ); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | 65 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @param string $taxonomy The taxonomy name. |
| 37 | 37 | */ |
| 38 | - public function __construct( $taxonomy ) { |
|
| 38 | + public function __construct($taxonomy) { |
|
| 39 | 39 | |
| 40 | 40 | $this->taxonomy = $taxonomy; |
| 41 | 41 | } |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | * |
| 50 | 50 | * @return array|WP_Error An array of terms or {@link WP_Error} in case of error. |
| 51 | 51 | */ |
| 52 | - public function get( $post_id ) { |
|
| 52 | + public function get($post_id) { |
|
| 53 | 53 | |
| 54 | - return wp_get_post_terms( $post_id, $this->taxonomy, array( |
|
| 54 | + return wp_get_post_terms($post_id, $this->taxonomy, array( |
|
| 55 | 55 | 'hide_empty' => false, |
| 56 | 56 | // Because of #334 (and the AAM plugin) we changed fields from 'id=>slug' to 'all'. |
| 57 | 57 | // An issue has been opened with the AAM plugin author as well. |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | // see https://github.com/insideout10/wordlift-plugin/issues/334 |
| 60 | 60 | // see https://wordpress.org/support/topic/idslug-not-working-anymore?replies=1#post-8806863 |
| 61 | 61 | 'fields' => 'all', |
| 62 | - ) ); |
|
| 62 | + )); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | } |
@@ -18,41 +18,41 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Wordlift_Post_Meta_Storage extends Wordlift_Storage { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * The meta key to load data from. |
|
| 23 | - * |
|
| 24 | - * @since 3.15.0 |
|
| 25 | - * @access private |
|
| 26 | - * @var string $meta_key The meta key to load data from. |
|
| 27 | - */ |
|
| 28 | - private $meta_key; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Create a {@link Wordlift_Post_Meta_Storage} instance, by providing the |
|
| 32 | - * meta key the storage should read from. |
|
| 33 | - * |
|
| 34 | - * @since 3.15.0 |
|
| 35 | - * |
|
| 36 | - * @param string $meta_key The post meta key. |
|
| 37 | - */ |
|
| 38 | - public function __construct( $meta_key ) { |
|
| 39 | - |
|
| 40 | - $this->meta_key = $meta_key; |
|
| 41 | - |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Get the value for the specified meta key. |
|
| 46 | - * |
|
| 47 | - * @since 3.15.0 |
|
| 48 | - * |
|
| 49 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 50 | - * |
|
| 51 | - * @return array An array of values (or an empty array if nothing is set). |
|
| 52 | - */ |
|
| 53 | - public function get( $post_id ) { |
|
| 54 | - |
|
| 55 | - return get_post_meta( $post_id, $this->meta_key ); |
|
| 56 | - } |
|
| 21 | + /** |
|
| 22 | + * The meta key to load data from. |
|
| 23 | + * |
|
| 24 | + * @since 3.15.0 |
|
| 25 | + * @access private |
|
| 26 | + * @var string $meta_key The meta key to load data from. |
|
| 27 | + */ |
|
| 28 | + private $meta_key; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Create a {@link Wordlift_Post_Meta_Storage} instance, by providing the |
|
| 32 | + * meta key the storage should read from. |
|
| 33 | + * |
|
| 34 | + * @since 3.15.0 |
|
| 35 | + * |
|
| 36 | + * @param string $meta_key The post meta key. |
|
| 37 | + */ |
|
| 38 | + public function __construct( $meta_key ) { |
|
| 39 | + |
|
| 40 | + $this->meta_key = $meta_key; |
|
| 41 | + |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Get the value for the specified meta key. |
|
| 46 | + * |
|
| 47 | + * @since 3.15.0 |
|
| 48 | + * |
|
| 49 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 50 | + * |
|
| 51 | + * @return array An array of values (or an empty array if nothing is set). |
|
| 52 | + */ |
|
| 53 | + public function get( $post_id ) { |
|
| 54 | + |
|
| 55 | + return get_post_meta( $post_id, $this->meta_key ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | 58 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @param string $meta_key The post meta key. |
| 37 | 37 | */ |
| 38 | - public function __construct( $meta_key ) { |
|
| 38 | + public function __construct($meta_key) { |
|
| 39 | 39 | |
| 40 | 40 | $this->meta_key = $meta_key; |
| 41 | 41 | |
@@ -50,9 +50,9 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @return array An array of values (or an empty array if nothing is set). |
| 52 | 52 | */ |
| 53 | - public function get( $post_id ) { |
|
| 53 | + public function get($post_id) { |
|
| 54 | 54 | |
| 55 | - return get_post_meta( $post_id, $this->meta_key ); |
|
| 55 | + return get_post_meta($post_id, $this->meta_key); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
| 36 | 36 | */ |
| 37 | - public function __construct( $entity_service ) { |
|
| 37 | + public function __construct($entity_service) { |
|
| 38 | 38 | |
| 39 | 39 | $this->entity_service = $entity_service; |
| 40 | 40 | |
@@ -50,18 +50,18 @@ discard block |
||
| 50 | 50 | * @return array|string|null A single string, or an array of values or null |
| 51 | 51 | * if the property isn't recognized. |
| 52 | 52 | */ |
| 53 | - public function get( $post_id ) { |
|
| 53 | + public function get($post_id) { |
|
| 54 | 54 | |
| 55 | 55 | // get related entities. |
| 56 | - $related = wl_core_get_related_entity_ids( $post_id ); |
|
| 56 | + $related = wl_core_get_related_entity_ids($post_id); |
|
| 57 | 57 | |
| 58 | 58 | // A reference to the entity service. |
| 59 | 59 | $entity_service = $this->entity_service; |
| 60 | 60 | |
| 61 | 61 | // Map the related posts' ids to URIs. |
| 62 | - return array_map( function ( $item ) use ( $entity_service ) { |
|
| 63 | - return $entity_service->get_uri( $item ); |
|
| 64 | - }, $related ); |
|
| 62 | + return array_map(function($item) use ($entity_service) { |
|
| 63 | + return $entity_service->get_uri($item); |
|
| 64 | + }, $related); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | } |
@@ -21,53 +21,53 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Wordlift_Post_Related_Storage extends Wordlift_Storage { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The {@link Wordlift_Entity_Service} instance. |
|
| 26 | - * |
|
| 27 | - * @since 3.15.0 |
|
| 28 | - * @access private |
|
| 29 | - * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 30 | - */ |
|
| 31 | - private $entity_service; |
|
| 24 | + /** |
|
| 25 | + * The {@link Wordlift_Entity_Service} instance. |
|
| 26 | + * |
|
| 27 | + * @since 3.15.0 |
|
| 28 | + * @access private |
|
| 29 | + * @var \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 30 | + */ |
|
| 31 | + private $entity_service; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Create a {@link Wordlift_Post_Related_Storage} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.15.0 |
|
| 37 | - * |
|
| 38 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 39 | - */ |
|
| 40 | - public function __construct( $entity_service ) { |
|
| 33 | + /** |
|
| 34 | + * Create a {@link Wordlift_Post_Related_Storage} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.15.0 |
|
| 37 | + * |
|
| 38 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 39 | + */ |
|
| 40 | + public function __construct( $entity_service ) { |
|
| 41 | 41 | |
| 42 | - $this->entity_service = $entity_service; |
|
| 42 | + $this->entity_service = $entity_service; |
|
| 43 | 43 | |
| 44 | - } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Get the property value. |
|
| 48 | - * |
|
| 49 | - * There is no filter for entities or posts, the returned data here can |
|
| 50 | - * be used for `relations` and `references` according to the client. |
|
| 51 | - * |
|
| 52 | - * @since 3.15.0 |
|
| 53 | - * |
|
| 54 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 55 | - * |
|
| 56 | - * @return array|string|null A single string, or an array of values or null |
|
| 57 | - * if the property isn't recognized. |
|
| 58 | - */ |
|
| 59 | - public function get( $post_id ) { |
|
| 46 | + /** |
|
| 47 | + * Get the property value. |
|
| 48 | + * |
|
| 49 | + * There is no filter for entities or posts, the returned data here can |
|
| 50 | + * be used for `relations` and `references` according to the client. |
|
| 51 | + * |
|
| 52 | + * @since 3.15.0 |
|
| 53 | + * |
|
| 54 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 55 | + * |
|
| 56 | + * @return array|string|null A single string, or an array of values or null |
|
| 57 | + * if the property isn't recognized. |
|
| 58 | + */ |
|
| 59 | + public function get( $post_id ) { |
|
| 60 | 60 | |
| 61 | - // get related entities. |
|
| 62 | - $related = wl_core_get_related_entity_ids( $post_id ); |
|
| 61 | + // get related entities. |
|
| 62 | + $related = wl_core_get_related_entity_ids( $post_id ); |
|
| 63 | 63 | |
| 64 | - // A reference to the entity service. |
|
| 65 | - $entity_service = $this->entity_service; |
|
| 64 | + // A reference to the entity service. |
|
| 65 | + $entity_service = $this->entity_service; |
|
| 66 | 66 | |
| 67 | - // Map the related posts' ids to URIs. |
|
| 68 | - return array_map( function ( $item ) use ( $entity_service ) { |
|
| 69 | - return $entity_service->get_uri( $item ); |
|
| 70 | - }, $related ); |
|
| 71 | - } |
|
| 67 | + // Map the related posts' ids to URIs. |
|
| 68 | + return array_map( function ( $item ) use ( $entity_service ) { |
|
| 69 | + return $entity_service->get_uri( $item ); |
|
| 70 | + }, $related ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |
@@ -16,35 +16,35 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Wordlift_Storage { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Get the values for the property of the {@link WP_Post}. |
|
| 21 | - * |
|
| 22 | - * @since 3.15.0 |
|
| 23 | - * |
|
| 24 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 25 | - * |
|
| 26 | - * @return array |
|
| 27 | - */ |
|
| 28 | - abstract public function get( $post_id ); |
|
| 19 | + /** |
|
| 20 | + * Get the values for the property of the {@link WP_Post}. |
|
| 21 | + * |
|
| 22 | + * @since 3.15.0 |
|
| 23 | + * |
|
| 24 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 25 | + * |
|
| 26 | + * @return array |
|
| 27 | + */ |
|
| 28 | + abstract public function get( $post_id ); |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Get the first value for a property. |
|
| 32 | - * |
|
| 33 | - * @since 3.15.0 |
|
| 34 | - * |
|
| 35 | - * @param int $post_id The {@link WP_Post}'s id. |
|
| 36 | - * |
|
| 37 | - * @return string The first property value or an empty string. |
|
| 38 | - */ |
|
| 39 | - public function get_first( $post_id ) { |
|
| 30 | + /** |
|
| 31 | + * Get the first value for a property. |
|
| 32 | + * |
|
| 33 | + * @since 3.15.0 |
|
| 34 | + * |
|
| 35 | + * @param int $post_id The {@link WP_Post}'s id. |
|
| 36 | + * |
|
| 37 | + * @return string The first property value or an empty string. |
|
| 38 | + */ |
|
| 39 | + public function get_first( $post_id ) { |
|
| 40 | 40 | |
| 41 | - $values = $this->get( $post_id ); |
|
| 41 | + $values = $this->get( $post_id ); |
|
| 42 | 42 | |
| 43 | - if ( empty( $values ) ) { |
|
| 44 | - return ''; |
|
| 45 | - } |
|
| 43 | + if ( empty( $values ) ) { |
|
| 44 | + return ''; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - return $values[0]; |
|
| 48 | - } |
|
| 47 | + return $values[0]; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | * |
| 26 | 26 | * @return array |
| 27 | 27 | */ |
| 28 | - abstract public function get( $post_id ); |
|
| 28 | + abstract public function get($post_id); |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Get the first value for a property. |
@@ -36,11 +36,11 @@ discard block |
||
| 36 | 36 | * |
| 37 | 37 | * @return string The first property value or an empty string. |
| 38 | 38 | */ |
| 39 | - public function get_first( $post_id ) { |
|
| 39 | + public function get_first($post_id) { |
|
| 40 | 40 | |
| 41 | - $values = $this->get( $post_id ); |
|
| 41 | + $values = $this->get($post_id); |
|
| 42 | 42 | |
| 43 | - if ( empty( $values ) ) { |
|
| 43 | + if (empty($values)) { |
|
| 44 | 44 | return ''; |
| 45 | 45 | } |
| 46 | 46 | |