Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
burhandodhy committed Jan 30, 2025
1 parent 6fc5fa3 commit 44abf61
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
2 changes: 1 addition & 1 deletion assets/js/geo-location/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { registerPlugin } from '@wordpress/plugins';
/**
* Internal dependencies.
*/
import LatLongPanel from './plugins/meta-fields';
import LatLongPanel from './panel';

registerPlugin('ep-lat-long', {
render: LatLongPanel,
Expand Down
41 changes: 41 additions & 0 deletions assets/js/geo-location/panel/AutoCompleteField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState, useEffect, useRef } from "@wordpress/element";

Check failure on line 1 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `"@wordpress/element"` with `'@wordpress/element'`
import { __ } from "@wordpress/i18n";

Check failure on line 2 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `"@wordpress/i18n"` with `'@wordpress/i18n'`
import { TextControl } from "@wordpress/components";

Check failure on line 3 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `"@wordpress/components"` with `'@wordpress/components'`

export default ({ onPlaceSelected, value }) => {
const inputRef = useRef(null);

const [location, setLocation] = useState(value);


Check failure on line 10 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `⏎↹console.log(·location·` with `↹console.log(location`
console.log( location );

Check warning on line 11 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Unexpected console statement
useEffect(() => {
if (inputRef.current && window.google) {
const autocomplete = new window.google.maps.places.Autocomplete(

Check failure on line 14 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `⏎↹↹↹↹inputRef.current,⏎↹↹↹↹` with `inputRef.current,·`
inputRef.current,
{
types: ["geocode"], // You can restrict the types of predictions

Check failure on line 17 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `↹↹↹↹↹types:·["geocode"` with `↹↹↹↹types:·['geocode'`
},

Check failure on line 18 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `↹},⏎↹↹↹` with `}`
);

autocomplete.addListener("place_changed", () => {

Check failure on line 21 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Replace `"place_changed"` with `'place_changed'`
const place = autocomplete.getPlace();
setLocation(place.formatted_address);
onPlaceSelected(place);
});
}
}, []);

Check failure on line 27 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

React Hook useEffect has a missing dependency: 'onPlaceSelected'. Either include it or remove the dependency array. If 'onPlaceSelected' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<>

Check failure on line 30 in assets/js/geo-location/panel/AutoCompleteField.js

View workflow job for this annotation

GitHub Actions / ES Lint

Fragments should contain more than one child - otherwise, there’s no need for a Fragment at all
<TextControl
ref={inputRef}
type="text"
label={__("Address", "elasticpress-labs")}
placeholder={__("Enter an address", "elasticpress-labs")}
value={location}
onChange={(value) => setLocation(value)}
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import { PluginDocumentSettingPanel } from '@wordpress/editor';
import { TextControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState, useEffect, useRef } from '@wordpress/element';

import AutoCompleteField from './AutoCompleteField';

export default () => {
const { editPost } = useDispatch('core/editor');

const {
ep_latitude = false,
ep_longitude = false,
ep_location = false,
...meta
} = useSelect((select) => select('core/editor').getEditedPostAttribute('meta') || {});

Expand All @@ -20,20 +24,34 @@ export default () => {
editPost({ meta: { ...meta, ep_longitude: longitude } });
};

const onPlaceSelected = (place) => {
editPost({ meta: { ...meta, ep_location: place.formatted_address } });

if (place.geometry && place.geometry.location) {
const latitude = place.geometry.location.lat();
const longitude = place.geometry.location.lng();

onUpdateLatitude(latitude);
onUpdateLongitude(longitude);
}
};

return (
<PluginDocumentSettingPanel
name="ep-lat-long-panel"
title={__('ElasticPress Geo Location', 'elasticpress')}
title={__("ElasticPress Geo Location", "elasticpress-labs")}
className="ep-lat-long-panel"
>
<AutoCompleteField value={ep_location} onPlaceSelected={onPlaceSelected} />

<TextControl
label={__('Latitude', 'elasticpress')}
label={__("Latitude", "elasticpress-labs")}
value={ep_latitude}
onChange={onUpdateLatitude}
type="number"
/>
<TextControl
label={__('Longitude', 'elasticpress')}
label={__("Longitude", "elasticpress-labs")}
value={ep_longitude}
onChange={onUpdateLongitude}
type="number"
Expand Down
51 changes: 51 additions & 0 deletions includes/classes/Feature/GeoLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public function __construct() {
* Setup all feature hooks
*/
public function setup() {
$settings = $this->get_settings();

if ( empty( $settings['active'] ) ) {
return;
}

add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
add_action( 'init', [ $this, 'register_meta' ] );

Expand All @@ -59,6 +65,40 @@ public function admin_scripts(): void {
);

wp_set_script_translations( 'ep_geo_location_script', 'elasticpress-labs' );

$settings = $this->get_settings();
if ( ! empty( $settings['google_maps_api_key'] ) ) {
$google_places_api_url = add_query_arg(
[
'key' => $settings['google_maps_api_key'],
'libraries' => 'places',
],
'https://maps.googleapis.com/maps/api/js'
);

wp_enqueue_script(
'google-places-api',
$google_places_api_url,
[],
null,

Check warning on line 83 in includes/classes/Feature/GeoLocation.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Resource version not set in call to wp_enqueue_script(). This means new versions of the script may not always be loaded due to browser caching.
true
);
}
}

/**
* Set the `settings_schema` attribute
*/
public function set_settings_schema() {
$this->settings_schema = [
[
'default' => '',
'help' => __( 'Add your Google Maps API key.', 'elasticpress-labs' ),
'key' => 'google_maps_api_key',
'label' => __( 'Google Maps API Key', 'elasticpress-labs' ),
'type' => 'text',
],
];
}

/**
Expand Down Expand Up @@ -86,6 +126,17 @@ public function register_meta(): void {
'show_in_rest' => true,
]
);

register_post_meta(
'',
'ep_location',
[
'type' => 'string',
'description' => 'Location',
'single' => true,
'show_in_rest' => true,
]
);
}

/**
Expand Down

0 comments on commit 44abf61

Please sign in to comment.