Skip to content

Commit

Permalink
fix: replace google maps field with open street map field (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink authored Mar 11, 2024
1 parent e4fb6ea commit b983789
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 42 deletions.
19 changes: 13 additions & 6 deletions source/php/AcfFields/json/event-fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,30 @@
"ui": 1
},
{
"center_lat": 53.55064,
"center_lng": 10.00065,
"zoom": 12,
"height": 400,
"return_format": "raw",
"allow_map_layers": 1,
"max_markers": 1,
"layers": [
"OpenStreetMap.Mapnik"
],
"key": "field_65a245c3a4062",
"label": "Location",
"name": "location",
"aria-label": "",
"type": "google_map",
"type": "open_street_map",
"instructions": "",
"required": 1,
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"center_lat": "",
"center_lng": "",
"zoom": "",
"height": ""
"leaflet_map": "{\"lat\":53.55064,\"lng\":10.00065,\"zoom\":12,\"layers\":[\"OpenStreetMap.Mapnik\"],\"markers\":[]}"
},
{
"key": "field_65a4f6af50302",
Expand Down
19 changes: 13 additions & 6 deletions source/php/AcfFields/php/event-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,30 @@
'ui' => 1,
),
4 => array(
'center_lat' => 53.55064,
'center_lng' => 10.00065,
'zoom' => 12,
'height' => 400,
'return_format' => 'raw',
'allow_map_layers' => 1,
'max_markers' => 1,
'layers' => array(
0 => 'OpenStreetMap.Mapnik',
),
'key' => 'field_65a245c3a4062',
'label' => __('Location', 'api-event-manager'),
'name' => 'location',
'aria-label' => '',
'type' => 'google_map',
'type' => 'open_street_map',
'instructions' => '',
'required' => 1,
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'center_lat' => '',
'center_lng' => '',
'zoom' => '',
'height' => '',
'leaflet_map' => '{"lat":53.55064,"lng":10.00065,"zoom":12,"layers":["OpenStreetMap.Mapnik"],"markers":[]}',
),
5 => array(
'key' => 'field_65a4f6af50302',
Expand Down
40 changes: 13 additions & 27 deletions source/php/PostToSchema/EventBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,39 +113,22 @@ public function setIsAccessibleForFree(): EventBuilder

public function setLocation(): EventBuilder
{
$locationMeta = $this->wp->getPostMeta($this->post->ID, 'location', true) ?: null;
$location = $this->wp->getPostMeta($this->post->ID, 'location', true) ?: null;

if (!$locationMeta) {
if (!$location) {
return $this;
}

$place = $this->getPlaceFromAcfMapField($locationMeta);
$place = new \Spatie\SchemaOrg\Place();
$place->address($location['address'] ?? null);
$place->latitude($location['latitude'] ?? null);
$place->longitude($location['longitude'] ?? null);

$this->event->location($place);

return $this;
}

private function getPlaceFromAcfMapField(array $acfMapField)
{
// Address
$address = new \Spatie\SchemaOrg\PostalAddress();
$streetName = $acfMapField['street_name'] ?? '';
$streetNumber = $acfMapField['street_number'] ?? '';
$streetAddress = trim("{$streetName} {$streetNumber}");
$address->streetAddress($streetAddress);
$address->addressLocality($acfMapField['city'] ?? null);
$address->postalCode($acfMapField['post_code'] ?? null);
$address->addressCountry($acfMapField['country_short'] ?? null);

// Location
$location = new \Spatie\SchemaOrg\Place();
$location->address($address);
$location->longitude($acfMapField['lng'] ?? null);
$location->latitude($acfMapField['lat'] ?? null);

return $location->toArray();
}

public function setOrganizer(): EventBuilder
{
$organizationTerms = $this->wp->getPostTerms($this->post->ID, 'organization', []);
Expand All @@ -158,16 +141,19 @@ public function setOrganizer(): EventBuilder
$url = $this->wp->getTermMeta($organizationTerm->term_id, 'url', true) ?: null;
$email = $this->wp->getTermMeta($organizationTerm->term_id, 'email', true) ?: null;
$telephone = $this->wp->getTermMeta($organizationTerm->term_id, 'telephone', true) ?: null;
$address = $this->wp->getTermMeta($organizationTerm->term_id, 'address', true) ?: null;
$location = $this->wp->getTermMeta($organizationTerm->term_id, 'address', true) ?: null;

$organization = new \Spatie\SchemaOrg\Organization();
$organization->name($organizationTerm->name);
$organization->url($url);
$organization->email($email);
$organization->telephone($telephone);

if ($address) {
$place = $this->getPlaceFromAcfMapField($address);
if ($location) {
$place = new \Spatie\SchemaOrg\Place();
$place->address($location['address'] ?? null);
$place->latitude($location['latitude'] ?? null);
$place->longitude($location['longitude'] ?? null);
$organization->location($place);
}

Expand Down
11 changes: 8 additions & 3 deletions tests/phpunit/tests/PostToSchema/EventBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,20 @@ public function testImageIsSet()

public function testLocationIsSet()
{
$location = ['latitude' => 1.2, 'longitude' => 3.4, 'address' => 'TestAddress'];

$post = $this->getMockedPost(['ID' => 123]);
$wpService = Mockery::mock(WPService::class);
$acfService = Mockery::mock(AcfService::class);
$wpService->shouldReceive('getPostMeta')->with($post->ID, 'location', true)->andReturn(['street_name' => 'TestLocation']);
$wpService->shouldReceive('getPostMeta')->with($post->ID, 'location', true)->andReturn($location);
$event = new EventBuilder($post, $wpService, $acfService);

$event->setLocation();

$this->assertEquals('Place', $event->toArray()['location']['@type']);
$this->assertEquals('TestAddress', $event->toArray()['location']['address']);
$this->assertEquals(1.2, $event->toArray()['location']['latitude']);
$this->assertEquals(3.4, $event->toArray()['location']['longitude']);
}

public function testAudienceIsSet()
Expand Down Expand Up @@ -229,7 +234,7 @@ public function testEventGetsOrganizationFromTerm()
'url' => 'https://www.example.com',
'email' => '[email protected]',
'telephone' => '123',
'address' => ['street_name' => 'TestLocation']
'address' => ['address' => 'TestAddress']
];
$wpService
->shouldReceive('getPostTerms')
Expand Down Expand Up @@ -264,7 +269,7 @@ public function testEventGetsOrganizationFromTerm()
$this->assertEquals($termMeta['email'], $schemaArray['organizer']['email']);
$this->assertEquals($termMeta['telephone'], $schemaArray['organizer']['telephone']);
$this->assertEquals('Place', $schemaArray['organizer']['location']['@type']);
$this->assertEquals('TestLocation', $schemaArray['organizer']['location']['address']['streetAddress']);
$this->assertEquals('TestAddress', $schemaArray['organizer']['location']['address']);
}

/**
Expand Down

0 comments on commit b983789

Please sign in to comment.