Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add districts with zipcodes, so the REST API can be filtered using a … #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v3.5.0

- Feat: Add districts with zipcodes, so the REST API can be filtered using a zipcode.

## v3.4.4

- Refactor: CMB2 'show on' taxonomy field type from 'select_advanced' to 'taxonomy_multicheck'
Expand Down
33 changes: 33 additions & 0 deletions config/cmb2_metaboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,37 @@
]
]
],
'district_zipcodes' => [
'id' => 'openpub_metadata_district_zipcodes',
'title' => __('Zipcodes', 'openpub-base'),
'object_types' => ['term'],
'taxonomies' => ['openpub-district'],
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => [
'zipcodes' => [
'heading' => [
'type' => 'heading',
'name' => __('Zipcodes', 'openpub-base'),
],
'zipcodes' => [
'id' => 'openpub_zipcodes_group',
'type' => 'group',
'options' => [
'add_button' => __('Add new zipcode', 'openpub-base'),
'remove_button' => __('Remove zipcode', 'openpub-base'),
],
'fields' => [
[
'id' => 'openpub_zipcode',
'name' => __('Zipcode', 'openpub-base'),
'desc' => __('A zipcode that is part of this district. (Only numbers, no letters)', 'openpub-base'),
'type' => 'number',
],
],
],
],
]
]
];
13 changes: 13 additions & 0 deletions config/taxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@
'plural' => __('Show on', 'openpub-base')
]
],
'openpub-district' => [
'object_types' => ['openpub-item'],
'args' => [
'show_in_rest' => true,
'show_admin_column' => true,
'capabilities' => []
],
'names' => [
'singular' => _x('District', 'Taxonomy definition', 'openpub-base'),
'plural' => _x('Districts', 'Taxonomy definition', 'openpub-base'),
'slug' => 'openpub-kern'
]
]
];
Binary file modified languages/openpub-base-nl_NL.mo
Binary file not shown.
30 changes: 30 additions & 0 deletions languages/openpub-base-nl_NL.po
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ msgstr "Vrij notitieveld"
msgid "(the law, authority, local regulations, etc.)"
msgstr "(wetgeving, bevoegd gezag, uitvoerder, lokale regelgeving)"

#: config/metaboxes.php:133 config/metaboxes.php:143
msgid "Zipcodes"
msgstr "Postcodes"

#: config/metaboxes.php:149
msgid "Add new zipcode"
msgstr "Voeg nieuwe postcode toe"

#: config/metaboxes.php:150
msgid "Remove zipcode"
msgstr "Verwijder postcode"

#: config/metaboxes.php:155
msgid "Zipcode"
msgstr "Postcode"

#: config/metaboxes.php:156
msgid "A zipcode that is part of this district. (Only numbers, no letters)"
msgstr "Een postcode die bij deze wijk hoort. (Alleen cijfer, geen letters)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kun je hier nog cijfers van maken


#: config/p2p_connections.php:9
msgid "Connected item(s)"
msgstr "Gekoppelde item(s)"
Expand Down Expand Up @@ -514,6 +534,16 @@ msgctxt "Taxonomy definition"
msgid "Usages"
msgstr "Toepassingen"

#: config/taxonomies.php:73
msgctxt "Taxonomy definition"
msgid "District"
msgstr "Wijk"

#: config/taxonomies.php:73
msgctxt "Taxonomy definition"
msgid "Districts"
msgstr "Wijken"

#: src/Base/Foundation/DependencyChecker.php:62
msgid "The following plugins are required to use the OpenPub:"
msgstr "De volgende plugins zijn vereist om gebruik te maken van de OpenPub:"
Expand Down
30 changes: 30 additions & 0 deletions languages/openpub-base.pot
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ msgstr "
msgid "(the law, authority, local regulations, etc.)"
msgstr ""

#: config/metaboxes.php:133 config/metaboxes.php:143
msgid "Zipcodes"
msgstr ""

#: config/metaboxes.php:149
msgid "Add new zipcode"
msgstr ""

#: config/metaboxes.php:150
msgid "Remove zipcode"
msgstr ""

#: config/metaboxes.php:155
msgid "Zipcode"
msgstr ""

#: config/metaboxes.php:156
msgid "A zipcode that is part of this district. (Only numbers, no letters)"
msgstr ""

#: config/p2p_connections.php:9
msgctxt "P2P titel"
msgid "Connected item(s)"
Expand Down Expand Up @@ -422,6 +442,16 @@ msgctxt "Taxonomy definition"
msgid "Usages"
msgstr ""

#: config/taxonomies.php:73
msgctxt "Taxonomy definition"
msgid "District"
msgstr ""

#: config/taxonomies.php:73
msgctxt "Taxonomy definition"
msgid "Districts"
msgstr ""

#: src/Base/Foundation/DependencyChecker.php:62
msgid "The following plugins are required to use the OpenPub:"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion openpub-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Yard | OpenPub Base
* Plugin URI: https://www.openwebconcept.nl/
* Description: Acts as foundation for other OpenPub related content plugins. This plugin implements actions to allow for other plugins to add and/or change Custom Posttypes, Metaboxes, Taxonomies, en Posts 2 posts relations.
* Version: 3.4.4
* Version: 3.5
* Author: Yard | Digital Agency
* Author URI: https://www.yard.nl/
* License: GPL-3.0
Expand Down
2 changes: 1 addition & 1 deletion src/Base/Foundation/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Plugin
* Version of the plugin.
* Used for setting versions of enqueue scripts and styles.
*/
public const VERSION = '3.4.4';
public const VERSION = '3.5';

/**
* Path to the root of the plugin.
Expand Down
21 changes: 21 additions & 0 deletions src/Base/Metabox/MetaboxServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MetaboxServiceProvider extends ServiceProvider
public function register()
{
$this->plugin->loader->addAction('cmb2_admin_init', $this, 'registerMetaboxes', 10, 0);
$this->plugin->loader->addAction('cmb2_save_term_fields', $this, 'saveGroupUnserialized', 10, 1);
$this->plugin->loader->addAction('admin_notices', new AdminNotice, 'upgradeAdminNotice', 10, 0);

if (class_exists('\WP_CLI')) {
Expand Down Expand Up @@ -109,4 +110,24 @@ protected function getShowOnMetabox(array $configMetaboxes): array
{
return array_merge($configMetaboxes, $this->plugin->config->get('show_on_metabox'));
}

/**
* Save group unserialized so it can be used in the search query.
*
* @param int $termId The current term ID.
*/
public function saveGroupUnserialized(int $termId): void
{
delete_term_meta($termId, self::PREFIX . 'openpub_zipcode');

$zipcodes = get_term_meta($termId, self::PREFIX . 'openpub_zipcodes_group');

if (! $zipcodes || empty($zipcodes[0])) {
return;
}

foreach ( $zipcodes[0] as $zipcode ) {
add_term_meta( $termId, self::PREFIX . 'openpub_zipcode', $zipcode['openpub_zipcode'] );
}
}
}
18 changes: 18 additions & 0 deletions src/Base/Repositories/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,22 @@ public static function addAudienceParameters(string $audiences): array
]
];
}

public static function addZipcodeParameter(string $zipcode): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eindigen met een happy path heeft mijn voorkeur:

	public static function addZipcodeParameter(string $zipcode): array
	{
		global $wpdb;
		$sql = "SELECT term_id FROM {$wpdb->termmeta} WHERE meta_key = '_owc_openpub_zipcode' AND meta_value = %s";
		$results = $wpdb->get_col($wpdb->prepare($sql, (int)$zipcode));

		if (! $results) {
			return [];
		}

		return [
			'tax_query' => [
				[
					'taxonomy' => 'openpub-district',
					'terms' => $results,
					'field' => 'term_id',
					'operator' => 'IN'
				]
			]
		];
	}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dit heb ik aangepast.

{
global $wpdb;
$sql = "SELECT term_id FROM {$wpdb->termmeta} WHERE meta_key = '_owc_openpub_zipcode' AND meta_value = %s";
$results = $wpdb->get_col($wpdb->prepare($sql, (int)$zipcode));

return [
'tax_query' => [
[
'taxonomy' => 'openpub-district',
'terms' => $results,
'field' => 'term_id',
'operator' => 'IN'
]
]
];
}
}
11 changes: 11 additions & 0 deletions src/Base/RestAPI/Controllers/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ protected function itemQueryBuilder(WP_REST_Request $request = null): Item
$items->query(Item::addShowOnParameter($request->get_param('source')));
}

if ($this->getZipcodeParam($request)) {
$items->query(Item::addZipcodeParameter($this->getZipcodeParam($request)));
}

if ($this->getAudienceParam($request)) {
$items->query(Item::addAudienceParameters($this->getAudienceParam($request)));
}
Expand Down Expand Up @@ -211,6 +215,13 @@ protected function getTypeParam(WP_REST_Request $request): string
return is_string($typeParam) ? $typeParam : '';
}

protected function getZipcodeParam(WP_REST_Request $request): string
{
$typeParam = $request->get_param('zipcode');

return ! empty($typeParam) && is_string($typeParam) ? $typeParam : '';
}

protected function validateBoolean(string $value): bool
{
$value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
Expand Down
Loading