diff --git a/CHANGELOG.md b/CHANGELOG.md index 412c184..1c3d62c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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' diff --git a/config/cmb2_metaboxes.php b/config/cmb2_metaboxes.php index 4873cf9..54acb58 100644 --- a/config/cmb2_metaboxes.php +++ b/config/cmb2_metaboxes.php @@ -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', + ], + ], + ], + ], + ] + ] ]; diff --git a/config/taxonomies.php b/config/taxonomies.php index 2c086e2..dc36695 100644 --- a/config/taxonomies.php +++ b/config/taxonomies.php @@ -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' + ] + ] ]; diff --git a/languages/openpub-base-nl_NL.mo b/languages/openpub-base-nl_NL.mo index 17fe351..eb922ec 100644 Binary files a/languages/openpub-base-nl_NL.mo and b/languages/openpub-base-nl_NL.mo differ diff --git a/languages/openpub-base-nl_NL.po b/languages/openpub-base-nl_NL.po index 14bc66b..ad86a6b 100644 --- a/languages/openpub-base-nl_NL.po +++ b/languages/openpub-base-nl_NL.po @@ -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)" + #: config/p2p_connections.php:9 msgid "Connected item(s)" msgstr "Gekoppelde item(s)" @@ -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:" diff --git a/languages/openpub-base.pot b/languages/openpub-base.pot index 07186d5..c410fd6 100644 --- a/languages/openpub-base.pot +++ b/languages/openpub-base.pot @@ -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)" @@ -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 "" diff --git a/openpub-base.php b/openpub-base.php index 5ca75c1..3338239 100644 --- a/openpub-base.php +++ b/openpub-base.php @@ -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 diff --git a/src/Base/Foundation/Plugin.php b/src/Base/Foundation/Plugin.php index 8cca70d..f0cee31 100644 --- a/src/Base/Foundation/Plugin.php +++ b/src/Base/Foundation/Plugin.php @@ -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. diff --git a/src/Base/Metabox/MetaboxServiceProvider.php b/src/Base/Metabox/MetaboxServiceProvider.php index e28af25..6ee9b57 100644 --- a/src/Base/Metabox/MetaboxServiceProvider.php +++ b/src/Base/Metabox/MetaboxServiceProvider.php @@ -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')) { @@ -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'] ); + } + } } diff --git a/src/Base/Repositories/Item.php b/src/Base/Repositories/Item.php index cfcf5b6..63baf8f 100644 --- a/src/Base/Repositories/Item.php +++ b/src/Base/Repositories/Item.php @@ -134,4 +134,22 @@ public static function addAudienceParameters(string $audiences): array ] ]; } + + 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)); + + return [ + 'tax_query' => [ + [ + 'taxonomy' => 'openpub-district', + 'terms' => $results, + 'field' => 'term_id', + 'operator' => 'IN' + ] + ] + ]; + } } diff --git a/src/Base/RestAPI/Controllers/ItemController.php b/src/Base/RestAPI/Controllers/ItemController.php index aad0828..392e9fc 100644 --- a/src/Base/RestAPI/Controllers/ItemController.php +++ b/src/Base/RestAPI/Controllers/ItemController.php @@ -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))); } @@ -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);