set. * @param bool $add_hash Whether a hash will be added as a suffix in the @id. * * @return array Schema ImageObject array. */ public function simple_image_object( $schema_id, $url, $caption = '', $add_hash = false ) { $data = $this->generate_object(); $id_suffix = ( $add_hash ) ? \md5( $url ) : ''; $data['@id'] = $schema_id . $id_suffix; $data['url'] = $url; $data['contentUrl'] = $url; if ( ! empty( $caption ) ) { $data['caption'] = $this->html->smart_strip_tags( $caption ); } return $data; } /** * Retrieves an image's caption if set, or uses the alt tag if that's set. * * @param array $data An ImageObject Schema array. * @param int $attachment_id Attachment ID. * @param string $caption The caption string, if there is one. * * @return array An imageObject with width and height set if available. */ private function add_caption( $data, $attachment_id, $caption = '' ) { if ( $caption !== '' ) { $data['caption'] = $caption; return $data; } $caption = $this->image->get_caption( $attachment_id ); if ( ! empty( $caption ) ) { $data['caption'] = $this->html->smart_strip_tags( $caption ); return $data; } return $data; } /** * Generates our bare bone ImageObject. * * @return array an empty ImageObject */ private function generate_object() { $data = [ '@type' => 'ImageObject', ]; $data = $this->language->add_piece_language( $data ); return $data; } /** * Adds image's width and height. * * @param array $data An ImageObject Schema array. * @param int $attachment_id Attachment ID. * * @return array An imageObject with width and height set if available. */ private function add_image_size( $data, $attachment_id ) { $image_meta = $this->image->get_metadata( $attachment_id ); if ( empty( $image_meta['width'] ) || empty( $image_meta['height'] ) ) { return $data; } $data['width'] = $image_meta['width']; $data['height'] = $image_meta['height']; return $data; } }