@@ -16,85 +16,85 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | class Wordlift_Attachment_Service { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * The singleton instance. |
|
| 21 | - * |
|
| 22 | - * @since 3.20.0 |
|
| 23 | - * @access private |
|
| 24 | - * @var \Wordlift_Attachment_Service The singleton instance. |
|
| 25 | - */ |
|
| 26 | - private static $instance; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Create a {@link Wordlift_Attachment_Service} instance. |
|
| 30 | - * |
|
| 31 | - * @since 3.20.0 |
|
| 32 | - */ |
|
| 33 | - public function __construct() { |
|
| 34 | - |
|
| 35 | - self::$instance = $this; |
|
| 36 | - |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Get the singleton instance. |
|
| 41 | - * |
|
| 42 | - * @return \Wordlift_Attachment_Service The singleton instance. |
|
| 43 | - * @since 3.20.0 |
|
| 44 | - * |
|
| 45 | - */ |
|
| 46 | - public static function get_instance() { |
|
| 47 | - |
|
| 48 | - return self::$instance; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Get an attachment ID given a URL. |
|
| 53 | - * |
|
| 54 | - * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
| 55 | - * |
|
| 56 | - * @param string $url The attachment URL. |
|
| 57 | - * |
|
| 58 | - * @return int|false Attachment ID on success, false on failure |
|
| 59 | - * @since 3.10.0 |
|
| 60 | - * |
|
| 61 | - */ |
|
| 62 | - public function get_attachment_id( $url ) { |
|
| 63 | - |
|
| 64 | - // Get the upload directory data, we need the base URL to check whether |
|
| 65 | - // the URL we received is within WP. |
|
| 66 | - $dir = wp_upload_dir(); |
|
| 67 | - |
|
| 68 | - // Get the filename, the extension is kept. |
|
| 69 | - if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
| 70 | - return false; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - $filename = $matches[1] . $matches[2]; |
|
| 74 | - |
|
| 75 | - // The following query is CPU-intensive, we need to review it. |
|
| 76 | - // |
|
| 77 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 78 | - // |
|
| 79 | - // Query for attachments with the specified filename. |
|
| 80 | - $query = new WP_Query( array( |
|
| 81 | - 'post_type' => 'attachment', |
|
| 82 | - 'post_status' => 'inherit', |
|
| 83 | - 'fields' => 'ids', |
|
| 84 | - 'meta_query' => array( |
|
| 85 | - array( |
|
| 86 | - 'value' => $filename, |
|
| 87 | - 'compare' => '=', |
|
| 88 | - 'key' => '_wp_attached_file', |
|
| 89 | - ), |
|
| 90 | - ), |
|
| 91 | - 'posts_per_page' => 1, |
|
| 92 | - 'ignore_sticky_posts' => true, |
|
| 93 | - ) ); |
|
| 94 | - |
|
| 95 | - // If there are no posts, return. |
|
| 96 | - if ( $query->have_posts() ) { |
|
| 97 | - return $query->posts[0]; |
|
| 19 | + /** |
|
| 20 | + * The singleton instance. |
|
| 21 | + * |
|
| 22 | + * @since 3.20.0 |
|
| 23 | + * @access private |
|
| 24 | + * @var \Wordlift_Attachment_Service The singleton instance. |
|
| 25 | + */ |
|
| 26 | + private static $instance; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Create a {@link Wordlift_Attachment_Service} instance. |
|
| 30 | + * |
|
| 31 | + * @since 3.20.0 |
|
| 32 | + */ |
|
| 33 | + public function __construct() { |
|
| 34 | + |
|
| 35 | + self::$instance = $this; |
|
| 36 | + |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Get the singleton instance. |
|
| 41 | + * |
|
| 42 | + * @return \Wordlift_Attachment_Service The singleton instance. |
|
| 43 | + * @since 3.20.0 |
|
| 44 | + * |
|
| 45 | + */ |
|
| 46 | + public static function get_instance() { |
|
| 47 | + |
|
| 48 | + return self::$instance; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Get an attachment ID given a URL. |
|
| 53 | + * |
|
| 54 | + * Inspired from https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
|
| 55 | + * |
|
| 56 | + * @param string $url The attachment URL. |
|
| 57 | + * |
|
| 58 | + * @return int|false Attachment ID on success, false on failure |
|
| 59 | + * @since 3.10.0 |
|
| 60 | + * |
|
| 61 | + */ |
|
| 62 | + public function get_attachment_id( $url ) { |
|
| 63 | + |
|
| 64 | + // Get the upload directory data, we need the base URL to check whether |
|
| 65 | + // the URL we received is within WP. |
|
| 66 | + $dir = wp_upload_dir(); |
|
| 67 | + |
|
| 68 | + // Get the filename, the extension is kept. |
|
| 69 | + if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
| 70 | + return false; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + $filename = $matches[1] . $matches[2]; |
|
| 74 | + |
|
| 75 | + // The following query is CPU-intensive, we need to review it. |
|
| 76 | + // |
|
| 77 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 78 | + // |
|
| 79 | + // Query for attachments with the specified filename. |
|
| 80 | + $query = new WP_Query( array( |
|
| 81 | + 'post_type' => 'attachment', |
|
| 82 | + 'post_status' => 'inherit', |
|
| 83 | + 'fields' => 'ids', |
|
| 84 | + 'meta_query' => array( |
|
| 85 | + array( |
|
| 86 | + 'value' => $filename, |
|
| 87 | + 'compare' => '=', |
|
| 88 | + 'key' => '_wp_attached_file', |
|
| 89 | + ), |
|
| 90 | + ), |
|
| 91 | + 'posts_per_page' => 1, |
|
| 92 | + 'ignore_sticky_posts' => true, |
|
| 93 | + ) ); |
|
| 94 | + |
|
| 95 | + // If there are no posts, return. |
|
| 96 | + if ( $query->have_posts() ) { |
|
| 97 | + return $query->posts[0]; |
|
| 98 | 98 | // foreach ( $query->posts as $attachment_id ) { |
| 99 | 99 | // |
| 100 | 100 | // // Get the attachment metadata, we need the filename. |
@@ -109,95 +109,95 @@ discard block |
||
| 109 | 109 | // return $attachment_id; |
| 110 | 110 | // } |
| 111 | 111 | // } |
| 112 | - } |
|
| 113 | - |
|
| 114 | - // If we got here, we couldn't find any attachment. |
|
| 115 | - return false; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get images embedded in the post content. |
|
| 120 | - * |
|
| 121 | - * @param string $content The post content. |
|
| 122 | - * |
|
| 123 | - * @return array An array of attachment ids. |
|
| 124 | - * @since 3.10.0 |
|
| 125 | - * |
|
| 126 | - */ |
|
| 127 | - public function get_image_embeds( $content ) { |
|
| 128 | - |
|
| 129 | - // Go over all the images included in the post content, check if they are |
|
| 130 | - // in the DB, and if so include them. |
|
| 131 | - $images = array(); |
|
| 132 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 133 | - return array(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Map the image URLs to attachment ids. |
|
| 137 | - $that = $this; |
|
| 138 | - $ids = array_map( function ( $url ) use ( $that ) { |
|
| 139 | - return $that->get_attachment_id( $url ); |
|
| 140 | - }, $images[1] ); |
|
| 141 | - |
|
| 142 | - // Filter out not found ids (i.e. id is false). |
|
| 143 | - return array_filter( $ids, function ( $item ) { |
|
| 144 | - return false !== $item; |
|
| 145 | - } ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Get images linked via the `gallery` shortcode. |
|
| 150 | - * |
|
| 151 | - * @param \WP_Post $post A {@link WP_Post} instance. |
|
| 152 | - * |
|
| 153 | - * @return array An array of attachment ids. |
|
| 154 | - * @since 3.10.0 |
|
| 155 | - * |
|
| 156 | - */ |
|
| 157 | - public function get_gallery( $post ) { |
|
| 158 | - |
|
| 159 | - // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
| 160 | - // checked at the moment. |
|
| 161 | - |
|
| 162 | - // Prepare the return value. |
|
| 163 | - $ids = array(); |
|
| 164 | - |
|
| 165 | - // As the above for images in galleries. |
|
| 166 | - // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
| 167 | - $pattern = get_shortcode_regex(); |
|
| 168 | - |
|
| 169 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 170 | - && array_key_exists( 2, $matches ) |
|
| 171 | - && in_array( 'gallery', $matches[2] ) |
|
| 172 | - ) { |
|
| 173 | - |
|
| 174 | - $keys = array_keys( $matches[2], 'gallery' ); |
|
| 175 | - |
|
| 176 | - foreach ( $keys as $key ) { |
|
| 177 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 178 | - |
|
| 179 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 180 | - // gallery images insert explicitly by their ids. |
|
| 181 | - |
|
| 182 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 183 | - // Since we do not check for actual image existence |
|
| 184 | - // when generating the json content, check it now. |
|
| 185 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 186 | - $ids[ $attachment_id ] = true; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - } else { |
|
| 190 | - // gallery shortcode with no ids uses all the images |
|
| 191 | - // attached to the post. |
|
| 192 | - $images = get_attached_media( 'image', $post->ID ); |
|
| 193 | - foreach ( $images as $attachment ) { |
|
| 194 | - $ids[ $attachment->ID ] = true; |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - return array_keys( $ids ); |
|
| 201 | - } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + // If we got here, we couldn't find any attachment. |
|
| 115 | + return false; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get images embedded in the post content. |
|
| 120 | + * |
|
| 121 | + * @param string $content The post content. |
|
| 122 | + * |
|
| 123 | + * @return array An array of attachment ids. |
|
| 124 | + * @since 3.10.0 |
|
| 125 | + * |
|
| 126 | + */ |
|
| 127 | + public function get_image_embeds( $content ) { |
|
| 128 | + |
|
| 129 | + // Go over all the images included in the post content, check if they are |
|
| 130 | + // in the DB, and if so include them. |
|
| 131 | + $images = array(); |
|
| 132 | + if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 133 | + return array(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Map the image URLs to attachment ids. |
|
| 137 | + $that = $this; |
|
| 138 | + $ids = array_map( function ( $url ) use ( $that ) { |
|
| 139 | + return $that->get_attachment_id( $url ); |
|
| 140 | + }, $images[1] ); |
|
| 141 | + |
|
| 142 | + // Filter out not found ids (i.e. id is false). |
|
| 143 | + return array_filter( $ids, function ( $item ) { |
|
| 144 | + return false !== $item; |
|
| 145 | + } ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Get images linked via the `gallery` shortcode. |
|
| 150 | + * |
|
| 151 | + * @param \WP_Post $post A {@link WP_Post} instance. |
|
| 152 | + * |
|
| 153 | + * @return array An array of attachment ids. |
|
| 154 | + * @since 3.10.0 |
|
| 155 | + * |
|
| 156 | + */ |
|
| 157 | + public function get_gallery( $post ) { |
|
| 158 | + |
|
| 159 | + // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
|
| 160 | + // checked at the moment. |
|
| 161 | + |
|
| 162 | + // Prepare the return value. |
|
| 163 | + $ids = array(); |
|
| 164 | + |
|
| 165 | + // As the above for images in galleries. |
|
| 166 | + // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
|
| 167 | + $pattern = get_shortcode_regex(); |
|
| 168 | + |
|
| 169 | + if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 170 | + && array_key_exists( 2, $matches ) |
|
| 171 | + && in_array( 'gallery', $matches[2] ) |
|
| 172 | + ) { |
|
| 173 | + |
|
| 174 | + $keys = array_keys( $matches[2], 'gallery' ); |
|
| 175 | + |
|
| 176 | + foreach ( $keys as $key ) { |
|
| 177 | + $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 178 | + |
|
| 179 | + if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 180 | + // gallery images insert explicitly by their ids. |
|
| 181 | + |
|
| 182 | + foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 183 | + // Since we do not check for actual image existence |
|
| 184 | + // when generating the json content, check it now. |
|
| 185 | + if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 186 | + $ids[ $attachment_id ] = true; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + } else { |
|
| 190 | + // gallery shortcode with no ids uses all the images |
|
| 191 | + // attached to the post. |
|
| 192 | + $images = get_attached_media( 'image', $post->ID ); |
|
| 193 | + foreach ( $images as $attachment ) { |
|
| 194 | + $ids[ $attachment->ID ] = true; |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + return array_keys( $ids ); |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | 203 | } |
@@ -59,25 +59,25 @@ discard block |
||
| 59 | 59 | * @since 3.10.0 |
| 60 | 60 | * |
| 61 | 61 | */ |
| 62 | - public function get_attachment_id( $url ) { |
|
| 62 | + public function get_attachment_id($url) { |
|
| 63 | 63 | |
| 64 | 64 | // Get the upload directory data, we need the base URL to check whether |
| 65 | 65 | // the URL we received is within WP. |
| 66 | 66 | $dir = wp_upload_dir(); |
| 67 | 67 | |
| 68 | 68 | // Get the filename, the extension is kept. |
| 69 | - if ( 1 !== preg_match( "@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches ) ) { |
|
| 69 | + if (1 !== preg_match("@^{$dir['baseurl']}/(.+?)(?:-\d+x\d+)?(\.\w+)$@", $url, $matches)) { |
|
| 70 | 70 | return false; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - $filename = $matches[1] . $matches[2]; |
|
| 73 | + $filename = $matches[1].$matches[2]; |
|
| 74 | 74 | |
| 75 | 75 | // The following query is CPU-intensive, we need to review it. |
| 76 | 76 | // |
| 77 | 77 | // See https://github.com/insideout10/wordlift-plugin/issues/689. |
| 78 | 78 | // |
| 79 | 79 | // Query for attachments with the specified filename. |
| 80 | - $query = new WP_Query( array( |
|
| 80 | + $query = new WP_Query(array( |
|
| 81 | 81 | 'post_type' => 'attachment', |
| 82 | 82 | 'post_status' => 'inherit', |
| 83 | 83 | 'fields' => 'ids', |
@@ -90,10 +90,10 @@ discard block |
||
| 90 | 90 | ), |
| 91 | 91 | 'posts_per_page' => 1, |
| 92 | 92 | 'ignore_sticky_posts' => true, |
| 93 | - ) ); |
|
| 93 | + )); |
|
| 94 | 94 | |
| 95 | 95 | // If there are no posts, return. |
| 96 | - if ( $query->have_posts() ) { |
|
| 96 | + if ($query->have_posts()) { |
|
| 97 | 97 | return $query->posts[0]; |
| 98 | 98 | // foreach ( $query->posts as $attachment_id ) { |
| 99 | 99 | // |
@@ -124,23 +124,23 @@ discard block |
||
| 124 | 124 | * @since 3.10.0 |
| 125 | 125 | * |
| 126 | 126 | */ |
| 127 | - public function get_image_embeds( $content ) { |
|
| 127 | + public function get_image_embeds($content) { |
|
| 128 | 128 | |
| 129 | 129 | // Go over all the images included in the post content, check if they are |
| 130 | 130 | // in the DB, and if so include them. |
| 131 | 131 | $images = array(); |
| 132 | - if ( false === preg_match_all( '#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images ) ) { |
|
| 132 | + if (false === preg_match_all('#<img [^>]*src="([^\\">]*)"[^>]*>#', $content, $images)) { |
|
| 133 | 133 | return array(); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // Map the image URLs to attachment ids. |
| 137 | 137 | $that = $this; |
| 138 | - $ids = array_map( function ( $url ) use ( $that ) { |
|
| 139 | - return $that->get_attachment_id( $url ); |
|
| 140 | - }, $images[1] ); |
|
| 138 | + $ids = array_map(function($url) use ($that) { |
|
| 139 | + return $that->get_attachment_id($url); |
|
| 140 | + }, $images[1]); |
|
| 141 | 141 | |
| 142 | 142 | // Filter out not found ids (i.e. id is false). |
| 143 | - return array_filter( $ids, function ( $item ) { |
|
| 143 | + return array_filter($ids, function($item) { |
|
| 144 | 144 | return false !== $item; |
| 145 | 145 | } ); |
| 146 | 146 | } |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | * @since 3.10.0 |
| 155 | 155 | * |
| 156 | 156 | */ |
| 157 | - public function get_gallery( $post ) { |
|
| 157 | + public function get_gallery($post) { |
|
| 158 | 158 | |
| 159 | 159 | // @todo: the `gallery` shortcode has an `exclude` attribute which isn't |
| 160 | 160 | // checked at the moment. |
@@ -166,38 +166,38 @@ discard block |
||
| 166 | 166 | // Code inspired by http://wordpress.stackexchange.com/questions/80408/how-to-get-page-post-gallery-attachment-images-in-order-they-are-set-in-backend |
| 167 | 167 | $pattern = get_shortcode_regex(); |
| 168 | 168 | |
| 169 | - if ( preg_match_all( '/' . $pattern . '/s', $post->post_content, $matches ) |
|
| 170 | - && array_key_exists( 2, $matches ) |
|
| 171 | - && in_array( 'gallery', $matches[2] ) |
|
| 169 | + if (preg_match_all('/'.$pattern.'/s', $post->post_content, $matches) |
|
| 170 | + && array_key_exists(2, $matches) |
|
| 171 | + && in_array('gallery', $matches[2]) |
|
| 172 | 172 | ) { |
| 173 | 173 | |
| 174 | - $keys = array_keys( $matches[2], 'gallery' ); |
|
| 174 | + $keys = array_keys($matches[2], 'gallery'); |
|
| 175 | 175 | |
| 176 | - foreach ( $keys as $key ) { |
|
| 177 | - $atts = shortcode_parse_atts( $matches[3][ $key ] ); |
|
| 176 | + foreach ($keys as $key) { |
|
| 177 | + $atts = shortcode_parse_atts($matches[3][$key]); |
|
| 178 | 178 | |
| 179 | - if ( is_array( $atts ) && array_key_exists( 'ids', $atts ) ) { |
|
| 179 | + if (is_array($atts) && array_key_exists('ids', $atts)) { |
|
| 180 | 180 | // gallery images insert explicitly by their ids. |
| 181 | 181 | |
| 182 | - foreach ( explode( ',', $atts['ids'] ) as $attachment_id ) { |
|
| 182 | + foreach (explode(',', $atts['ids']) as $attachment_id) { |
|
| 183 | 183 | // Since we do not check for actual image existence |
| 184 | 184 | // when generating the json content, check it now. |
| 185 | - if ( wp_get_attachment_image_src( $attachment_id, 'full' ) ) { |
|
| 186 | - $ids[ $attachment_id ] = true; |
|
| 185 | + if (wp_get_attachment_image_src($attachment_id, 'full')) { |
|
| 186 | + $ids[$attachment_id] = true; |
|
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | } else { |
| 190 | 190 | // gallery shortcode with no ids uses all the images |
| 191 | 191 | // attached to the post. |
| 192 | - $images = get_attached_media( 'image', $post->ID ); |
|
| 193 | - foreach ( $images as $attachment ) { |
|
| 194 | - $ids[ $attachment->ID ] = true; |
|
| 192 | + $images = get_attached_media('image', $post->ID); |
|
| 193 | + foreach ($images as $attachment) { |
|
| 194 | + $ids[$attachment->ID] = true; |
|
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - return array_keys( $ids ); |
|
| 200 | + return array_keys($ids); |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | } |
@@ -25,345 +25,345 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | abstract class Wordlift_Abstract_Post_To_Jsonld_Converter implements Wordlift_Post_Converter { |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * The JSON-LD context. |
|
| 30 | - * |
|
| 31 | - * @since 3.10.0 |
|
| 32 | - */ |
|
| 33 | - const CONTEXT = 'http://schema.org'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 37 | - * |
|
| 38 | - * @since 3.10.0 |
|
| 39 | - * @access protected |
|
| 40 | - * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 41 | - */ |
|
| 42 | - protected $entity_type_service; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 46 | - * |
|
| 47 | - * @since 3.10.0 |
|
| 48 | - * @access protected |
|
| 49 | - * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance. |
|
| 50 | - */ |
|
| 51 | - protected $entity_service; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * A {@link Wordlift_User_Service} instance. |
|
| 55 | - * |
|
| 56 | - * @since 3.10.0 |
|
| 57 | - * @access private |
|
| 58 | - * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 59 | - */ |
|
| 60 | - protected $user_service; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * A {@link Wordlift_Attachment_Service} instance. |
|
| 64 | - * |
|
| 65 | - * @since 3.10.0 |
|
| 66 | - * @access private |
|
| 67 | - * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 68 | - */ |
|
| 69 | - protected $attachment_service; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 73 | - * |
|
| 74 | - * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 75 | - * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 76 | - * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 77 | - * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 78 | - * |
|
| 79 | - * @since 3.10.0 |
|
| 80 | - * |
|
| 81 | - */ |
|
| 82 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) { |
|
| 83 | - |
|
| 84 | - $this->entity_type_service = $entity_type_service; |
|
| 85 | - $this->entity_service = $entity_service; |
|
| 86 | - $this->user_service = $user_service; |
|
| 87 | - $this->attachment_service = $attachment_service; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 92 | - * found while processing the post is set in the $references array. |
|
| 93 | - * |
|
| 94 | - * @param int $post_id The post id. |
|
| 95 | - * @param array $references An array of entity references. |
|
| 96 | - * @param array $references_infos |
|
| 97 | - * |
|
| 98 | - * @return array A JSON-LD array. |
|
| 99 | - * @since 3.10.0 |
|
| 100 | - */ |
|
| 101 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 102 | - |
|
| 103 | - // Get the post instance. |
|
| 104 | - $post = get_post( $post_id ); |
|
| 105 | - if ( null === $post ) { |
|
| 106 | - // Post not found. |
|
| 107 | - return null; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - // Get the post URI @id. |
|
| 111 | - $id = $this->entity_service->get_uri( $post->ID ); |
|
| 112 | - if ( is_null( $id ) ) { |
|
| 113 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /* |
|
| 28 | + /** |
|
| 29 | + * The JSON-LD context. |
|
| 30 | + * |
|
| 31 | + * @since 3.10.0 |
|
| 32 | + */ |
|
| 33 | + const CONTEXT = 'http://schema.org'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * A {@link Wordlift_Entity_Type_Service} instance. |
|
| 37 | + * |
|
| 38 | + * @since 3.10.0 |
|
| 39 | + * @access protected |
|
| 40 | + * @var \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 41 | + */ |
|
| 42 | + protected $entity_type_service; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 46 | + * |
|
| 47 | + * @since 3.10.0 |
|
| 48 | + * @access protected |
|
| 49 | + * @var \Wordlift_Entity_Service $entity_type_service A {@link Wordlift_Entity_Service} instance. |
|
| 50 | + */ |
|
| 51 | + protected $entity_service; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * A {@link Wordlift_User_Service} instance. |
|
| 55 | + * |
|
| 56 | + * @since 3.10.0 |
|
| 57 | + * @access private |
|
| 58 | + * @var \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 59 | + */ |
|
| 60 | + protected $user_service; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * A {@link Wordlift_Attachment_Service} instance. |
|
| 64 | + * |
|
| 65 | + * @since 3.10.0 |
|
| 66 | + * @access private |
|
| 67 | + * @var \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 68 | + */ |
|
| 69 | + protected $attachment_service; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Wordlift_Post_To_Jsonld_Converter constructor. |
|
| 73 | + * |
|
| 74 | + * @param \Wordlift_Entity_Type_Service $entity_type_service A {@link Wordlift_Entity_Type_Service} instance. |
|
| 75 | + * @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 76 | + * @param \Wordlift_User_Service $user_service A {@link Wordlift_User_Service} instance. |
|
| 77 | + * @param \Wordlift_Attachment_Service $attachment_service A {@link Wordlift_Attachment_Service} instance. |
|
| 78 | + * |
|
| 79 | + * @since 3.10.0 |
|
| 80 | + * |
|
| 81 | + */ |
|
| 82 | + public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) { |
|
| 83 | + |
|
| 84 | + $this->entity_type_service = $entity_type_service; |
|
| 85 | + $this->entity_service = $entity_service; |
|
| 86 | + $this->user_service = $user_service; |
|
| 87 | + $this->attachment_service = $attachment_service; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Convert the provided {@link WP_Post} to a JSON-LD array. Any entity reference |
|
| 92 | + * found while processing the post is set in the $references array. |
|
| 93 | + * |
|
| 94 | + * @param int $post_id The post id. |
|
| 95 | + * @param array $references An array of entity references. |
|
| 96 | + * @param array $references_infos |
|
| 97 | + * |
|
| 98 | + * @return array A JSON-LD array. |
|
| 99 | + * @since 3.10.0 |
|
| 100 | + */ |
|
| 101 | + public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 102 | + |
|
| 103 | + // Get the post instance. |
|
| 104 | + $post = get_post( $post_id ); |
|
| 105 | + if ( null === $post ) { |
|
| 106 | + // Post not found. |
|
| 107 | + return null; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + // Get the post URI @id. |
|
| 111 | + $id = $this->entity_service->get_uri( $post->ID ); |
|
| 112 | + if ( is_null( $id ) ) { |
|
| 113 | + $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /* |
|
| 117 | 117 | * The `types` variable holds one or more entity types. The `type` variable isn't used anymore. |
| 118 | 118 | * |
| 119 | 119 | * @since 3.20.0 We support more than one entity type. |
| 120 | 120 | * |
| 121 | 121 | * @see https://github.com/insideout10/wordlift-plugin/issues/835 |
| 122 | 122 | */ |
| 123 | - // // Get the entity @type. We consider `post` BlogPostings. |
|
| 124 | - // $type = $this->entity_type_service->get( $post_id ); |
|
| 125 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 126 | - $type = self::make_one( $types ); |
|
| 127 | - |
|
| 128 | - // Prepare the response. |
|
| 129 | - $jsonld = array( |
|
| 130 | - '@context' => self::CONTEXT, |
|
| 131 | - '@id' => $id, |
|
| 132 | - '@type' => $type, |
|
| 133 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 134 | - ); |
|
| 135 | - |
|
| 136 | - // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 137 | - /* |
|
| 123 | + // // Get the entity @type. We consider `post` BlogPostings. |
|
| 124 | + // $type = $this->entity_type_service->get( $post_id ); |
|
| 125 | + $types = $this->entity_type_service->get_names( $post_id ); |
|
| 126 | + $type = self::make_one( $types ); |
|
| 127 | + |
|
| 128 | + // Prepare the response. |
|
| 129 | + $jsonld = array( |
|
| 130 | + '@context' => self::CONTEXT, |
|
| 131 | + '@id' => $id, |
|
| 132 | + '@type' => $type, |
|
| 133 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 134 | + ); |
|
| 135 | + |
|
| 136 | + // Set the `mainEntityOfPage` property if the post has some contents. |
|
| 137 | + /* |
|
| 138 | 138 | * Apply the `wl_post_content` filter, in case 3rd parties want to change the post content, e.g. |
| 139 | 139 | * because the content is written elsewhere. |
| 140 | 140 | * |
| 141 | 141 | * @since 3.20.0 |
| 142 | 142 | */ |
| 143 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 144 | - if ( ! empty( $post_content ) ) { |
|
| 145 | - // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 146 | - // main entity for the specified URL. It might be however that the |
|
| 147 | - // post/page is actually about another specific entity. How WL deals |
|
| 148 | - // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 149 | - // |
|
| 150 | - // See http://schema.org/mainEntityOfPage |
|
| 151 | - // |
|
| 152 | - // No need to specify `'@type' => 'WebPage'. |
|
| 153 | - // |
|
| 154 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 155 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 159 | - * |
|
| 160 | - * @since 3.27.7 |
|
| 161 | - */ |
|
| 162 | - if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ) ) ) { |
|
| 163 | - $jsonld['mainEntityOfPage'] = array( |
|
| 164 | - '@type' => 'WebPage', |
|
| 165 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ) |
|
| 166 | - ); |
|
| 167 | - } |
|
| 168 | - }; |
|
| 169 | - |
|
| 170 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 171 | - |
|
| 172 | - // Get the entities referenced by this post and set it to the `references` |
|
| 173 | - // array so that the caller can do further processing, such as printing out |
|
| 174 | - // more of those references. |
|
| 175 | - $references_without_locations = Object_Relation_Service::get_instance() |
|
| 176 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 177 | - |
|
| 178 | - /* |
|
| 143 | + $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 144 | + if ( ! empty( $post_content ) ) { |
|
| 145 | + // We're setting the `mainEntityOfPage` to signal which one is the |
|
| 146 | + // main entity for the specified URL. It might be however that the |
|
| 147 | + // post/page is actually about another specific entity. How WL deals |
|
| 148 | + // with that hasn't been defined yet (see https://github.com/insideout10/wordlift-plugin/issues/451). |
|
| 149 | + // |
|
| 150 | + // See http://schema.org/mainEntityOfPage |
|
| 151 | + // |
|
| 152 | + // No need to specify `'@type' => 'WebPage'. |
|
| 153 | + // |
|
| 154 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 155 | + $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
|
| 159 | + * |
|
| 160 | + * @since 3.27.7 |
|
| 161 | + */ |
|
| 162 | + if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ) ) ) { |
|
| 163 | + $jsonld['mainEntityOfPage'] = array( |
|
| 164 | + '@type' => 'WebPage', |
|
| 165 | + 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ) |
|
| 166 | + ); |
|
| 167 | + } |
|
| 168 | + }; |
|
| 169 | + |
|
| 170 | + $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 171 | + |
|
| 172 | + // Get the entities referenced by this post and set it to the `references` |
|
| 173 | + // array so that the caller can do further processing, such as printing out |
|
| 174 | + // more of those references. |
|
| 175 | + $references_without_locations = Object_Relation_Service::get_instance() |
|
| 176 | + ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 177 | + |
|
| 178 | + /* |
|
| 179 | 179 | * Add the locations to the references. |
| 180 | 180 | * |
| 181 | 181 | * @since 3.19.5 |
| 182 | 182 | * |
| 183 | 183 | * @see https://github.com/insideout10/wordlift-plugin/issues/858. |
| 184 | 184 | */ |
| 185 | - // A reference to use in closure. |
|
| 186 | - $entity_type_service = $this->entity_type_service; |
|
| 187 | - $locations = array_reduce( $references_without_locations, function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 188 | - /** |
|
| 189 | - * @var $reference Reference |
|
| 190 | - */ |
|
| 191 | - // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 192 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 193 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 194 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 195 | - |
|
| 196 | - return $carry; |
|
| 197 | - } |
|
| 198 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 199 | - $post_location_references = array_map( function ( $post_id ) { |
|
| 200 | - return new Post_Reference( $post_id ); |
|
| 201 | - }, $post_location_ids ); |
|
| 202 | - |
|
| 203 | - return array_merge( $carry, $post_location_references); |
|
| 204 | - }, array() ); |
|
| 205 | - |
|
| 206 | - // Merge the references with the referenced locations if any. |
|
| 207 | - $references = array_merge( $references_without_locations, $locations ); |
|
| 208 | - |
|
| 209 | - return $jsonld; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 214 | - * part since it is set with the '@context'. |
|
| 215 | - * |
|
| 216 | - * @param string $value The property value. |
|
| 217 | - * |
|
| 218 | - * @return string The property value without the context. |
|
| 219 | - * @since 3.10.0 |
|
| 220 | - * |
|
| 221 | - */ |
|
| 222 | - public function relative_to_context( $value ) { |
|
| 223 | - |
|
| 224 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Set the images, by looking for embedded images, for images loaded via the |
|
| 229 | - * gallery and for the featured image. |
|
| 230 | - * |
|
| 231 | - * Uses the cache service to store the results of this function for a day. |
|
| 232 | - * |
|
| 233 | - * @param $attachment_service Wordlift_Attachment_Service |
|
| 234 | - * @param WP_Post $post The target {@link WP_Post}. |
|
| 235 | - * @param array $jsonld The JSON-LD array. |
|
| 236 | - * |
|
| 237 | - * @since 3.10.0 |
|
| 238 | - */ |
|
| 239 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 240 | - |
|
| 241 | - // Prepare the attachment ids array. |
|
| 242 | - $ids = array(); |
|
| 243 | - |
|
| 244 | - // Set the thumbnail id as first attachment id, if any. |
|
| 245 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 246 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 247 | - $ids[] = $thumbnail_id; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - // For the time being the following is being removed since the query |
|
| 251 | - // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 252 | - // |
|
| 253 | - // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 254 | - // |
|
| 255 | - // Get the embeds, removing existing ids. |
|
| 256 | - if ( apply_filters( 'wl_feature__enable__image-embeds', true ) ) { |
|
| 257 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 258 | - } else { |
|
| 259 | - $embeds = array(); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - // Get the gallery, removing existing ids. |
|
| 263 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 264 | - |
|
| 265 | - // Map the attachment ids to images' data structured for schema.org use. |
|
| 266 | - $images_with_sizes = array_filter( |
|
| 267 | - array_reduce( array_merge( $ids, $embeds, $gallery ), |
|
| 268 | - function ( $carry, $item ) { |
|
| 269 | - /* |
|
| 185 | + // A reference to use in closure. |
|
| 186 | + $entity_type_service = $this->entity_type_service; |
|
| 187 | + $locations = array_reduce( $references_without_locations, function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 188 | + /** |
|
| 189 | + * @var $reference Reference |
|
| 190 | + */ |
|
| 191 | + // @see https://schema.org/location for the schema.org types using the `location` property. |
|
| 192 | + if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 193 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 194 | + && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 195 | + |
|
| 196 | + return $carry; |
|
| 197 | + } |
|
| 198 | + $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 199 | + $post_location_references = array_map( function ( $post_id ) { |
|
| 200 | + return new Post_Reference( $post_id ); |
|
| 201 | + }, $post_location_ids ); |
|
| 202 | + |
|
| 203 | + return array_merge( $carry, $post_location_references); |
|
| 204 | + }, array() ); |
|
| 205 | + |
|
| 206 | + // Merge the references with the referenced locations if any. |
|
| 207 | + $references = array_merge( $references_without_locations, $locations ); |
|
| 208 | + |
|
| 209 | + return $jsonld; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * If the provided value starts with the schema.org context, we remove the schema.org |
|
| 214 | + * part since it is set with the '@context'. |
|
| 215 | + * |
|
| 216 | + * @param string $value The property value. |
|
| 217 | + * |
|
| 218 | + * @return string The property value without the context. |
|
| 219 | + * @since 3.10.0 |
|
| 220 | + * |
|
| 221 | + */ |
|
| 222 | + public function relative_to_context( $value ) { |
|
| 223 | + |
|
| 224 | + return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Set the images, by looking for embedded images, for images loaded via the |
|
| 229 | + * gallery and for the featured image. |
|
| 230 | + * |
|
| 231 | + * Uses the cache service to store the results of this function for a day. |
|
| 232 | + * |
|
| 233 | + * @param $attachment_service Wordlift_Attachment_Service |
|
| 234 | + * @param WP_Post $post The target {@link WP_Post}. |
|
| 235 | + * @param array $jsonld The JSON-LD array. |
|
| 236 | + * |
|
| 237 | + * @since 3.10.0 |
|
| 238 | + */ |
|
| 239 | + public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 240 | + |
|
| 241 | + // Prepare the attachment ids array. |
|
| 242 | + $ids = array(); |
|
| 243 | + |
|
| 244 | + // Set the thumbnail id as first attachment id, if any. |
|
| 245 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 246 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 247 | + $ids[] = $thumbnail_id; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + // For the time being the following is being removed since the query |
|
| 251 | + // initiated by `get_image_embeds` is consuming lots of CPU. |
|
| 252 | + // |
|
| 253 | + // See https://github.com/insideout10/wordlift-plugin/issues/689. |
|
| 254 | + // |
|
| 255 | + // Get the embeds, removing existing ids. |
|
| 256 | + if ( apply_filters( 'wl_feature__enable__image-embeds', true ) ) { |
|
| 257 | + $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 258 | + } else { |
|
| 259 | + $embeds = array(); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + // Get the gallery, removing existing ids. |
|
| 263 | + $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 264 | + |
|
| 265 | + // Map the attachment ids to images' data structured for schema.org use. |
|
| 266 | + $images_with_sizes = array_filter( |
|
| 267 | + array_reduce( array_merge( $ids, $embeds, $gallery ), |
|
| 268 | + function ( $carry, $item ) { |
|
| 269 | + /* |
|
| 270 | 270 | * @todo: we're not sure that we're getting attachment data here, we |
| 271 | 271 | * should filter `false`s. |
| 272 | 272 | */ |
| 273 | 273 | |
| 274 | - $sources = array_merge( |
|
| 275 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 276 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 277 | - ); |
|
| 278 | - |
|
| 279 | - $sources_with_image = array_filter( $sources, function ( $source ) { |
|
| 280 | - return ! empty( $source[0] ); |
|
| 281 | - } ); |
|
| 282 | - |
|
| 283 | - // Get the attachment data. |
|
| 284 | - // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 285 | - |
|
| 286 | - // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 287 | - |
|
| 288 | - // Bail if image is not found. |
|
| 289 | - // In some cases, you can delete the image from the database |
|
| 290 | - // or from uploads dir, but the image id still exists as featured image |
|
| 291 | - // or in [gallery] shortcode. |
|
| 292 | - // if ( empty( $attachment[0] ) ) { |
|
| 293 | - if ( empty( $sources_with_image ) ) { |
|
| 294 | - return $carry; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - // Merge the arrays. |
|
| 298 | - return array_merge( |
|
| 299 | - $carry, |
|
| 300 | - $sources_with_image |
|
| 301 | - ); |
|
| 302 | - } |
|
| 303 | - // Initial array. |
|
| 304 | - , array() ) |
|
| 305 | - ); |
|
| 306 | - |
|
| 307 | - // Refactor data as per schema.org specifications. |
|
| 308 | - $images = array_map( function ( $attachment ) { |
|
| 309 | - return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 310 | - array( |
|
| 311 | - '@type' => 'ImageObject', |
|
| 312 | - 'url' => $attachment[0], |
|
| 313 | - ), $attachment |
|
| 314 | - ); |
|
| 315 | - }, $images_with_sizes ); |
|
| 316 | - |
|
| 317 | - // Add images if present. |
|
| 318 | - if ( 0 < count( $images ) ) { |
|
| 319 | - $jsonld['image'] = $images; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * If the provided array of values contains only one value, then one single |
|
| 326 | - * value is returned, otherwise the original array is returned. |
|
| 327 | - * |
|
| 328 | - * @param array $value An array of values. |
|
| 329 | - * |
|
| 330 | - * @return mixed|array A single value or the original array. |
|
| 331 | - * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 332 | - * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 333 | - * @since 3.8.0 |
|
| 334 | - * @access private |
|
| 335 | - * |
|
| 336 | - */ |
|
| 337 | - protected static function make_one( $value ) { |
|
| 338 | - |
|
| 339 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Process the provided array by adding the width / height if the values |
|
| 344 | - * are available and are greater than 0. |
|
| 345 | - * |
|
| 346 | - * @param array $image The `ImageObject` array. |
|
| 347 | - * @param array $attachment The attachment array. |
|
| 348 | - * |
|
| 349 | - * @return array The enriched `ImageObject` array. |
|
| 350 | - * @since 3.14.0 |
|
| 351 | - * |
|
| 352 | - */ |
|
| 353 | - public static function set_image_size( $image, $attachment ) { |
|
| 354 | - |
|
| 355 | - // If you specify a "width" or "height" value you should leave out |
|
| 356 | - // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 357 | - // |
|
| 358 | - // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 359 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 360 | - $image['width'] = $attachment[1]; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 364 | - $image['height'] = $attachment[2]; |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - return $image; |
|
| 368 | - } |
|
| 274 | + $sources = array_merge( |
|
| 275 | + Wordlift_Image_Service::get_sources( $item ), |
|
| 276 | + array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 277 | + ); |
|
| 278 | + |
|
| 279 | + $sources_with_image = array_filter( $sources, function ( $source ) { |
|
| 280 | + return ! empty( $source[0] ); |
|
| 281 | + } ); |
|
| 282 | + |
|
| 283 | + // Get the attachment data. |
|
| 284 | + // $attachment = wp_get_attachment_image_src( $item, 'full' ); |
|
| 285 | + |
|
| 286 | + // var_dump( array( "sources-$item" => Wordlift_Image_Service::get_sources( $item ) ) ); |
|
| 287 | + |
|
| 288 | + // Bail if image is not found. |
|
| 289 | + // In some cases, you can delete the image from the database |
|
| 290 | + // or from uploads dir, but the image id still exists as featured image |
|
| 291 | + // or in [gallery] shortcode. |
|
| 292 | + // if ( empty( $attachment[0] ) ) { |
|
| 293 | + if ( empty( $sources_with_image ) ) { |
|
| 294 | + return $carry; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + // Merge the arrays. |
|
| 298 | + return array_merge( |
|
| 299 | + $carry, |
|
| 300 | + $sources_with_image |
|
| 301 | + ); |
|
| 302 | + } |
|
| 303 | + // Initial array. |
|
| 304 | + , array() ) |
|
| 305 | + ); |
|
| 306 | + |
|
| 307 | + // Refactor data as per schema.org specifications. |
|
| 308 | + $images = array_map( function ( $attachment ) { |
|
| 309 | + return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
|
| 310 | + array( |
|
| 311 | + '@type' => 'ImageObject', |
|
| 312 | + 'url' => $attachment[0], |
|
| 313 | + ), $attachment |
|
| 314 | + ); |
|
| 315 | + }, $images_with_sizes ); |
|
| 316 | + |
|
| 317 | + // Add images if present. |
|
| 318 | + if ( 0 < count( $images ) ) { |
|
| 319 | + $jsonld['image'] = $images; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * If the provided array of values contains only one value, then one single |
|
| 326 | + * value is returned, otherwise the original array is returned. |
|
| 327 | + * |
|
| 328 | + * @param array $value An array of values. |
|
| 329 | + * |
|
| 330 | + * @return mixed|array A single value or the original array. |
|
| 331 | + * @since 3.20.0 The function has been moved from {@link Wordlift_Entity_Post_To_Jsonld_Converter} to |
|
| 332 | + * {@link Wordlift_Abstract_Post_To_Jsonld_Converter}. |
|
| 333 | + * @since 3.8.0 |
|
| 334 | + * @access private |
|
| 335 | + * |
|
| 336 | + */ |
|
| 337 | + protected static function make_one( $value ) { |
|
| 338 | + |
|
| 339 | + return 1 === count( $value ) ? $value[0] : $value; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Process the provided array by adding the width / height if the values |
|
| 344 | + * are available and are greater than 0. |
|
| 345 | + * |
|
| 346 | + * @param array $image The `ImageObject` array. |
|
| 347 | + * @param array $attachment The attachment array. |
|
| 348 | + * |
|
| 349 | + * @return array The enriched `ImageObject` array. |
|
| 350 | + * @since 3.14.0 |
|
| 351 | + * |
|
| 352 | + */ |
|
| 353 | + public static function set_image_size( $image, $attachment ) { |
|
| 354 | + |
|
| 355 | + // If you specify a "width" or "height" value you should leave out |
|
| 356 | + // 'px'. For example: "width":"4608px" should be "width":"4608". |
|
| 357 | + // |
|
| 358 | + // See https://github.com/insideout10/wordlift-plugin/issues/451. |
|
| 359 | + if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 360 | + $image['width'] = $attachment[1]; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 364 | + $image['height'] = $attachment[2]; |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + return $image; |
|
| 368 | + } |
|
| 369 | 369 | } |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | * @since 3.10.0 |
| 80 | 80 | * |
| 81 | 81 | */ |
| 82 | - public function __construct( $entity_type_service, $entity_service, $user_service, $attachment_service ) { |
|
| 82 | + public function __construct($entity_type_service, $entity_service, $user_service, $attachment_service) { |
|
| 83 | 83 | |
| 84 | 84 | $this->entity_type_service = $entity_type_service; |
| 85 | 85 | $this->entity_service = $entity_service; |
@@ -98,19 +98,19 @@ discard block |
||
| 98 | 98 | * @return array A JSON-LD array. |
| 99 | 99 | * @since 3.10.0 |
| 100 | 100 | */ |
| 101 | - public function convert( $post_id, &$references = array(), &$references_infos = array() ) { |
|
| 101 | + public function convert($post_id, &$references = array(), &$references_infos = array()) { |
|
| 102 | 102 | |
| 103 | 103 | // Get the post instance. |
| 104 | - $post = get_post( $post_id ); |
|
| 105 | - if ( null === $post ) { |
|
| 104 | + $post = get_post($post_id); |
|
| 105 | + if (null === $post) { |
|
| 106 | 106 | // Post not found. |
| 107 | 107 | return null; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | // Get the post URI @id. |
| 111 | - $id = $this->entity_service->get_uri( $post->ID ); |
|
| 112 | - if ( is_null( $id ) ) { |
|
| 113 | - $id = 'get_uri returned null, dataset is ' . wl_configuration_get_redlink_dataset_uri(); |
|
| 111 | + $id = $this->entity_service->get_uri($post->ID); |
|
| 112 | + if (is_null($id)) { |
|
| 113 | + $id = 'get_uri returned null, dataset is '.wl_configuration_get_redlink_dataset_uri(); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | /* |
@@ -122,15 +122,15 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | // // Get the entity @type. We consider `post` BlogPostings. |
| 124 | 124 | // $type = $this->entity_type_service->get( $post_id ); |
| 125 | - $types = $this->entity_type_service->get_names( $post_id ); |
|
| 126 | - $type = self::make_one( $types ); |
|
| 125 | + $types = $this->entity_type_service->get_names($post_id); |
|
| 126 | + $type = self::make_one($types); |
|
| 127 | 127 | |
| 128 | 128 | // Prepare the response. |
| 129 | 129 | $jsonld = array( |
| 130 | 130 | '@context' => self::CONTEXT, |
| 131 | 131 | '@id' => $id, |
| 132 | 132 | '@type' => $type, |
| 133 | - 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt( $post ), |
|
| 133 | + 'description' => Wordlift_Post_Excerpt_Helper::get_text_excerpt($post), |
|
| 134 | 134 | ); |
| 135 | 135 | |
| 136 | 136 | // Set the `mainEntityOfPage` property if the post has some contents. |
@@ -140,8 +140,8 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @since 3.20.0 |
| 142 | 142 | */ |
| 143 | - $post_content = apply_filters( 'wl_post_content', $post->post_content, $post ); |
|
| 144 | - if ( ! empty( $post_content ) ) { |
|
| 143 | + $post_content = apply_filters('wl_post_content', $post->post_content, $post); |
|
| 144 | + if ( ! empty($post_content)) { |
|
| 145 | 145 | // We're setting the `mainEntityOfPage` to signal which one is the |
| 146 | 146 | // main entity for the specified URL. It might be however that the |
| 147 | 147 | // post/page is actually about another specific entity. How WL deals |
@@ -152,28 +152,28 @@ discard block |
||
| 152 | 152 | // No need to specify `'@type' => 'WebPage'. |
| 153 | 153 | // |
| 154 | 154 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 155 | - $jsonld['mainEntityOfPage'] = get_the_permalink( $post->ID ); |
|
| 155 | + $jsonld['mainEntityOfPage'] = get_the_permalink($post->ID); |
|
| 156 | 156 | |
| 157 | 157 | /** |
| 158 | 158 | * @see https://github.com/insideout10/wordlift-plugin/issues/1207 |
| 159 | 159 | * |
| 160 | 160 | * @since 3.27.7 |
| 161 | 161 | */ |
| 162 | - if ( in_array( $type, array( 'Occupation', 'OccupationAggregationByEmployer' ) ) ) { |
|
| 162 | + if (in_array($type, array('Occupation', 'OccupationAggregationByEmployer'))) { |
|
| 163 | 163 | $jsonld['mainEntityOfPage'] = array( |
| 164 | 164 | '@type' => 'WebPage', |
| 165 | - 'lastReviewed' => get_post_time( 'Y-m-d\TH:i:sP', true, $post, false ) |
|
| 165 | + 'lastReviewed' => get_post_time('Y-m-d\TH:i:sP', true, $post, false) |
|
| 166 | 166 | ); |
| 167 | 167 | } |
| 168 | 168 | }; |
| 169 | 169 | |
| 170 | - $this->set_images( $this->attachment_service, $post, $jsonld ); |
|
| 170 | + $this->set_images($this->attachment_service, $post, $jsonld); |
|
| 171 | 171 | |
| 172 | 172 | // Get the entities referenced by this post and set it to the `references` |
| 173 | 173 | // array so that the caller can do further processing, such as printing out |
| 174 | 174 | // more of those references. |
| 175 | 175 | $references_without_locations = Object_Relation_Service::get_instance() |
| 176 | - ->get_references( $post_id, Object_Type_Enum::POST ); |
|
| 176 | + ->get_references($post_id, Object_Type_Enum::POST); |
|
| 177 | 177 | |
| 178 | 178 | /* |
| 179 | 179 | * Add the locations to the references. |
@@ -184,27 +184,27 @@ discard block |
||
| 184 | 184 | */ |
| 185 | 185 | // A reference to use in closure. |
| 186 | 186 | $entity_type_service = $this->entity_type_service; |
| 187 | - $locations = array_reduce( $references_without_locations, function ( $carry, $reference ) use ( $entity_type_service ) { |
|
| 187 | + $locations = array_reduce($references_without_locations, function($carry, $reference) use ($entity_type_service) { |
|
| 188 | 188 | /** |
| 189 | 189 | * @var $reference Reference |
| 190 | 190 | */ |
| 191 | 191 | // @see https://schema.org/location for the schema.org types using the `location` property. |
| 192 | - if ( ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Action' ) |
|
| 193 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Event' ) |
|
| 194 | - && ! $entity_type_service->has_entity_type( $reference->get_id(), 'http://schema.org/Organization' ) ) { |
|
| 192 | + if ( ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Action') |
|
| 193 | + && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Event') |
|
| 194 | + && ! $entity_type_service->has_entity_type($reference->get_id(), 'http://schema.org/Organization')) { |
|
| 195 | 195 | |
| 196 | 196 | return $carry; |
| 197 | 197 | } |
| 198 | - $post_location_ids = get_post_meta( $reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION ); |
|
| 199 | - $post_location_references = array_map( function ( $post_id ) { |
|
| 200 | - return new Post_Reference( $post_id ); |
|
| 201 | - }, $post_location_ids ); |
|
| 198 | + $post_location_ids = get_post_meta($reference->get_id(), Wordlift_Schema_Service::FIELD_LOCATION); |
|
| 199 | + $post_location_references = array_map(function($post_id) { |
|
| 200 | + return new Post_Reference($post_id); |
|
| 201 | + }, $post_location_ids); |
|
| 202 | 202 | |
| 203 | - return array_merge( $carry, $post_location_references); |
|
| 204 | - }, array() ); |
|
| 203 | + return array_merge($carry, $post_location_references); |
|
| 204 | + }, array()); |
|
| 205 | 205 | |
| 206 | 206 | // Merge the references with the referenced locations if any. |
| 207 | - $references = array_merge( $references_without_locations, $locations ); |
|
| 207 | + $references = array_merge($references_without_locations, $locations); |
|
| 208 | 208 | |
| 209 | 209 | return $jsonld; |
| 210 | 210 | } |
@@ -219,9 +219,9 @@ discard block |
||
| 219 | 219 | * @since 3.10.0 |
| 220 | 220 | * |
| 221 | 221 | */ |
| 222 | - public function relative_to_context( $value ) { |
|
| 222 | + public function relative_to_context($value) { |
|
| 223 | 223 | |
| 224 | - return 0 === strpos( $value, self::CONTEXT . '/' ) ? substr( $value, strlen( self::CONTEXT ) + 1 ) : $value; |
|
| 224 | + return 0 === strpos($value, self::CONTEXT.'/') ? substr($value, strlen(self::CONTEXT) + 1) : $value; |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -236,14 +236,14 @@ discard block |
||
| 236 | 236 | * |
| 237 | 237 | * @since 3.10.0 |
| 238 | 238 | */ |
| 239 | - public static function set_images( $attachment_service, $post, &$jsonld ) { |
|
| 239 | + public static function set_images($attachment_service, $post, &$jsonld) { |
|
| 240 | 240 | |
| 241 | 241 | // Prepare the attachment ids array. |
| 242 | 242 | $ids = array(); |
| 243 | 243 | |
| 244 | 244 | // Set the thumbnail id as first attachment id, if any. |
| 245 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 246 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 245 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 246 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
| 247 | 247 | $ids[] = $thumbnail_id; |
| 248 | 248 | } |
| 249 | 249 | |
@@ -253,31 +253,31 @@ discard block |
||
| 253 | 253 | // See https://github.com/insideout10/wordlift-plugin/issues/689. |
| 254 | 254 | // |
| 255 | 255 | // Get the embeds, removing existing ids. |
| 256 | - if ( apply_filters( 'wl_feature__enable__image-embeds', true ) ) { |
|
| 257 | - $embeds = array_diff( $attachment_service->get_image_embeds( $post->post_content ), $ids ); |
|
| 256 | + if (apply_filters('wl_feature__enable__image-embeds', true)) { |
|
| 257 | + $embeds = array_diff($attachment_service->get_image_embeds($post->post_content), $ids); |
|
| 258 | 258 | } else { |
| 259 | 259 | $embeds = array(); |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | 262 | // Get the gallery, removing existing ids. |
| 263 | - $gallery = array_diff( $attachment_service->get_gallery( $post ), $ids, $embeds ); |
|
| 263 | + $gallery = array_diff($attachment_service->get_gallery($post), $ids, $embeds); |
|
| 264 | 264 | |
| 265 | 265 | // Map the attachment ids to images' data structured for schema.org use. |
| 266 | 266 | $images_with_sizes = array_filter( |
| 267 | - array_reduce( array_merge( $ids, $embeds, $gallery ), |
|
| 268 | - function ( $carry, $item ) { |
|
| 267 | + array_reduce(array_merge($ids, $embeds, $gallery), |
|
| 268 | + function($carry, $item) { |
|
| 269 | 269 | /* |
| 270 | 270 | * @todo: we're not sure that we're getting attachment data here, we |
| 271 | 271 | * should filter `false`s. |
| 272 | 272 | */ |
| 273 | 273 | |
| 274 | 274 | $sources = array_merge( |
| 275 | - Wordlift_Image_Service::get_sources( $item ), |
|
| 276 | - array( wp_get_attachment_image_src( $item, 'full' ) ) |
|
| 275 | + Wordlift_Image_Service::get_sources($item), |
|
| 276 | + array(wp_get_attachment_image_src($item, 'full')) |
|
| 277 | 277 | ); |
| 278 | 278 | |
| 279 | - $sources_with_image = array_filter( $sources, function ( $source ) { |
|
| 280 | - return ! empty( $source[0] ); |
|
| 279 | + $sources_with_image = array_filter($sources, function($source) { |
|
| 280 | + return ! empty($source[0]); |
|
| 281 | 281 | } ); |
| 282 | 282 | |
| 283 | 283 | // Get the attachment data. |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | // or from uploads dir, but the image id still exists as featured image |
| 291 | 291 | // or in [gallery] shortcode. |
| 292 | 292 | // if ( empty( $attachment[0] ) ) { |
| 293 | - if ( empty( $sources_with_image ) ) { |
|
| 293 | + if (empty($sources_with_image)) { |
|
| 294 | 294 | return $carry; |
| 295 | 295 | } |
| 296 | 296 | |
@@ -301,21 +301,21 @@ discard block |
||
| 301 | 301 | ); |
| 302 | 302 | } |
| 303 | 303 | // Initial array. |
| 304 | - , array() ) |
|
| 304 | + , array()) |
|
| 305 | 305 | ); |
| 306 | 306 | |
| 307 | 307 | // Refactor data as per schema.org specifications. |
| 308 | - $images = array_map( function ( $attachment ) { |
|
| 308 | + $images = array_map(function($attachment) { |
|
| 309 | 309 | return Wordlift_Abstract_Post_To_Jsonld_Converter::set_image_size( |
| 310 | 310 | array( |
| 311 | 311 | '@type' => 'ImageObject', |
| 312 | 312 | 'url' => $attachment[0], |
| 313 | 313 | ), $attachment |
| 314 | 314 | ); |
| 315 | - }, $images_with_sizes ); |
|
| 315 | + }, $images_with_sizes); |
|
| 316 | 316 | |
| 317 | 317 | // Add images if present. |
| 318 | - if ( 0 < count( $images ) ) { |
|
| 318 | + if (0 < count($images)) { |
|
| 319 | 319 | $jsonld['image'] = $images; |
| 320 | 320 | } |
| 321 | 321 | |
@@ -334,9 +334,9 @@ discard block |
||
| 334 | 334 | * @access private |
| 335 | 335 | * |
| 336 | 336 | */ |
| 337 | - protected static function make_one( $value ) { |
|
| 337 | + protected static function make_one($value) { |
|
| 338 | 338 | |
| 339 | - return 1 === count( $value ) ? $value[0] : $value; |
|
| 339 | + return 1 === count($value) ? $value[0] : $value; |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | /** |
@@ -350,17 +350,17 @@ discard block |
||
| 350 | 350 | * @since 3.14.0 |
| 351 | 351 | * |
| 352 | 352 | */ |
| 353 | - public static function set_image_size( $image, $attachment ) { |
|
| 353 | + public static function set_image_size($image, $attachment) { |
|
| 354 | 354 | |
| 355 | 355 | // If you specify a "width" or "height" value you should leave out |
| 356 | 356 | // 'px'. For example: "width":"4608px" should be "width":"4608". |
| 357 | 357 | // |
| 358 | 358 | // See https://github.com/insideout10/wordlift-plugin/issues/451. |
| 359 | - if ( isset( $attachment[1] ) && is_numeric( $attachment[1] ) && 0 < $attachment[1] ) { |
|
| 359 | + if (isset($attachment[1]) && is_numeric($attachment[1]) && 0 < $attachment[1]) { |
|
| 360 | 360 | $image['width'] = $attachment[1]; |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - if ( isset( $attachment[2] ) && is_numeric( $attachment[2] ) && 0 < $attachment[2] ) { |
|
| 363 | + if (isset($attachment[2]) && is_numeric($attachment[2]) && 0 < $attachment[2]) { |
|
| 364 | 364 | $image['height'] = $attachment[2]; |
| 365 | 365 | } |
| 366 | 366 | |