Skip to content

Commit

Permalink
Merge branch 'hotfix/1.10.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
abrain committed Nov 5, 2022
2 parents 87b906c + 718e1db commit be896ed
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
*/
class Core
{
const VERSION = '1.10.1';
const DB_VERSION = 71;
const VERSION = '1.10.2';
const DB_VERSION = 72;

/**
* Statische Variable, um die aktuelle (einzige!) Instanz dieser Klasse zu halten
Expand Down
123 changes: 71 additions & 52 deletions src/CustomFieldsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@
use abrain\Einsatzverwaltung\Types\CustomPostType;
use abrain\Einsatzverwaltung\Types\CustomTaxonomy;
use abrain\Einsatzverwaltung\Types\CustomType;
use abrain\Einsatzverwaltung\Types\Report;
use WP_Term;
use function add_action;
use function add_term_meta;
use function array_diff;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_search;
use function array_slice;
use function array_unique;
use function current_action;
use function delete_term_meta;
use function explode;
use function get_term_meta;
use function is_numeric;
use function preg_match;

/**
* Keeps track of the custom fields of our custom types
Expand Down Expand Up @@ -65,7 +71,7 @@ public function add(CustomType $customType, CustomField $customField)
$this->taxonomyFields[$taxonomy] = array();
add_action("{$taxonomy}_add_form_fields", array($this, 'onAddFormFields'));
add_action("{$taxonomy}_edit_form_fields", array($this, 'onEditFormFields'), 10, 2);
add_action("manage_edit-{$taxonomy}_columns", array($this, 'onCustomColumns'));
add_action("manage_edit-{$taxonomy}_columns", array($this, 'onTaxonomyCustomColumns'));
add_filter("manage_{$taxonomy}_custom_column", array($this, 'onTaxonomyColumnContent'), 10, 3);
}

Expand All @@ -75,7 +81,7 @@ public function add(CustomType $customType, CustomField $customField)

if (!array_key_exists($postType, $this->postTypeFields)) {
$this->postTypeFields[$postType] = array();
add_filter("manage_edit-{$postType}_columns", array($this, 'onCustomColumns'));
add_filter("manage_edit-{$postType}_columns", array($this, 'onPostCustomColumns'));
add_action("manage_{$postType}_posts_custom_column", array($this, 'onPostColumnContent'), 10, 2);
}

Expand Down Expand Up @@ -156,54 +162,6 @@ public function onEditFormFields($tag, $taxonomy)
}
}

/**
* Fügt für die zusätzlichen Felder zusätzliche Spalten in der Übersicht ein
*
* @param array $columns
* @return array
*/
public function onCustomColumns($columns): array
{
$screen = get_current_screen();

if (empty($screen)) {
return $columns;
}

if ($screen->post_type === Report::getSlug() && !empty($screen->taxonomy)) {
$taxonomy = $screen->taxonomy;
if (!$this->hasTaxonomy($taxonomy)) {
return $columns;
}

// Add the columns after the description column
$index = array_search('description', array_keys($columns));
$index = is_numeric($index) ? $index + 1 : count($columns);
$before = array_slice($columns, 0, $index, true);
$after = array_slice($columns, $index, null, true);

$columnsToAdd = array();
/** @var CustomField $field */
foreach ($this->taxonomyFields[$taxonomy] as $field) {
$columnsToAdd[$field->key] = $field->label;
}

return array_merge($before, $columnsToAdd, $after);
}

$postType = $screen->post_type;
if (!$this->hasPostType($postType)) {
return $columns;
}

/** @var CustomField $field */
foreach ($this->postTypeFields[$postType] as $field) {
$columns[$field->key] = $field->label;
}

return $columns;
}

/**
* @param string $columnId
* @param int $postId
Expand All @@ -225,6 +183,33 @@ public function onPostColumnContent($columnId, $postId)
echo $customField->getColumnContent($postId);
}

/**
* Adds custom columns to our own post types.
*
* @param string[] $columns
*
* @return string[] An associative array of column headings.
*/
public function onPostCustomColumns($columns): array
{
$currentAction = current_action();
if (preg_match('/^manage_edit-(\w+)_columns$/', $currentAction, $matches) !== 1) {
return $columns;
}

$postType = $matches[1];
if (!$this->hasPostType($postType)) {
return $columns;
}

/** @var CustomField $field */
foreach ($this->postTypeFields[$postType] as $field) {
$columns[$field->key] = $field->label;
}

return $columns;
}

/**
* Filterfunktion für den Inhalt der selbst angelegten Spalten
*
Expand Down Expand Up @@ -260,7 +245,41 @@ public function onTaxonomyColumnContent($string, $columnName, $termId): string
}

/**
* Speichert zusätzliche Infos zu Terms als options ab
* Adds custom columns to our own taxonomies.
*
* @param string[] $columns An associative array of column headings.
*
* @return string[]
*/
public function onTaxonomyCustomColumns($columns): array
{
$currentAction = current_action();
if (preg_match('/^manage_edit-(\w+)_columns$/', $currentAction, $matches) !== 1) {
return $columns;
}

$taxonomy = $matches[1];
if (!$this->hasTaxonomy($taxonomy)) {
return $columns;
}

// Add the columns after the description column
$index = array_search('description', array_keys($columns));
$index = is_numeric($index) ? $index + 1 : count($columns);
$before = array_slice($columns, 0, $index, true);
$after = array_slice($columns, $index, null, true);

$columnsToAdd = array();
/** @var CustomField $field */
foreach ($this->taxonomyFields[$taxonomy] as $field) {
$columnsToAdd[$field->key] = $field->label;
}

return array_merge($before, $columnsToAdd, $after);
}

/**
* Saves custom taxonomy fields to termmeta.
*
* @param int $termId Term ID
* @param int $ttId Term taxonomy ID
Expand Down
2 changes: 1 addition & 1 deletion src/DataAccess/ReportInserter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getInsertArgs(ReportInsertObject $reportImportObject)
$args = array(
'post_type' => Report::getSlug(),
'post_title' => $reportImportObject->getTitle(),
'meta_input' => array(),
'meta_input' => array('einsatz_special' => 0),
'tax_input' => array()
);

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function registerCustomFields(CustomFieldsRepository $customFields)
$customFields->add($this, new NumberInput(
'unit_order',
'Reihenfolge',
'Optionale Angabe, mit der die Anzeigereihenfolge der Einheiten beeinflusst werden kann. Einheiten mit der kleineren Zahl werden zuerst angezeigt, anschließend diejenigen ohne Angabe bzw. dem Wert 0. Haben mehrere Einheiten den gleichen Wert, werden sie in alphabetischer Reihenfolge ausgegeben.'
'Einheiten mit der kleineren Zahl werden zuerst angezeigt, anschließend diejenigen mit dem Wert 0. Bei gleichem Wert werden Einheiten in alphabetischer Reihenfolge ausgegeben.'
));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function registerCustomFields(CustomFieldsRepository $customFields)
$customFields->add($this, new NumberInput(
'vehicleorder',
'Reihenfolge',
'Optionale Angabe, mit der die Anzeigereihenfolge der Fahrzeuge beeinflusst werden kann. Fahrzeuge mit der kleineren Zahl werden zuerst angezeigt, anschließend diejenigen ohne Angabe bzw. dem Wert 0. Haben mehrere Fahrzeuge den gleichen Wert, werden sie in alphabetischer Reihenfolge ausgegeben.'
'Fahrzeuge mit der kleineren Zahl werden zuerst angezeigt, anschließend diejenigen mit dem Wert 0. Bei gleichem Wert werden Fahrzeuge in alphabetischer Reihenfolge ausgegeben.'
));
$customFields->add($this, new Checkbox(
'out_of_service',
Expand Down
29 changes: 29 additions & 0 deletions src/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use WP_Error;
use WP_User;
use function add_option;
use function add_post_meta;
use function add_term_meta;
use function array_key_exists;
use function array_keys;
Expand Down Expand Up @@ -124,6 +125,10 @@ public function doUpdate(int $currentDbVersion, int $targetDbVersion)
if ($currentDbVersion < 71 && $targetDbVersion >= 71) {
$this->upgrade1100();
}

if ($currentDbVersion < 72 && $targetDbVersion >= 72) {
$this->upgrade1102();
}
}

/**
Expand Down Expand Up @@ -640,6 +645,30 @@ public function upgrade1100()
update_option('einsatzvw_db_version', 71);
}

/**
* - Adds missing 'special' postmeta for reports that got created via the API
*
* @since 1.10.2
*/
public function upgrade1102()
{
$postsWithoutPostmeta = get_posts([
'post_type' => 'einsatz',
'post_status' => ['publish', 'private', 'draft'],
'nopaging' => true,
'meta_query' => [
[
'key' => 'einsatz_special',
'compare' => 'NOT EXISTS'
]
]
]);
foreach ($postsWithoutPostmeta as $post) {
add_post_meta($post->ID, 'einsatz_special', 0, true);
}
update_option('einsatzvw_db_version', 72);
}

/**
* Fügt einen Bezeichner für eine Admin Notice der Liste der noch anzuzeigenden Notices hinzu
*
Expand Down
2 changes: 1 addition & 1 deletion src/einsatzverwaltung.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Einsatzverwaltung
Plugin URI: https://einsatzverwaltung.org
Description: Public incident reports for fire departments and other rescue services
Version: 1.10.1
Version: 1.10.2
Author: Andreas Brain
Author URI: https://www.abrain.de
License: GPLv2
Expand Down
6 changes: 5 additions & 1 deletion src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: Feuerwehr, fire department, EMS
Requires at least: 5.1.0
Tested up to: 6.0
Requires PHP: 7.1.0
Stable tag: 1.10.1
Stable tag: 1.10.2
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -52,6 +52,10 @@ Yes, you can find them on [our website](https://einsatzverwaltung.org/faq/).

== Changelog ==

= 1.10.2 =
* Fix: Reports created with the API endpoint could show up as special reports
* Fix: Table was missing columns when creating e.g. units or vehicles

= 1.10.1 =
* Fix: Compatibility issue with PHP 8

Expand Down
9 changes: 6 additions & 3 deletions tests/unit/DataAccess/ReportInserterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function testMinimalData()
'post_status' => 'draft',
'post_title' => 'Some title',
'meta_input' => [
'_einsatz_timeofalerting' => '2021-08-29 17:51:15'
'_einsatz_timeofalerting' => '2021-08-29 17:51:15',
'einsatz_special' => 0,
],
'tax_input' => []
], $insertArgs);
Expand Down Expand Up @@ -113,7 +114,8 @@ public function testCompletePublish()
'post_content' => 'Some random post content',
'meta_input' => [
'einsatz_einsatzort' => 'The location',
'einsatz_einsatzende' => '2021-08-29 21:34:42'
'einsatz_einsatzende' => '2021-08-29 21:34:42',
'einsatz_special' => 0,
],
'tax_input' => [
'einsatzart' => [5123],
Expand Down Expand Up @@ -154,7 +156,8 @@ public function testKeywordCreation()
'post_status' => 'draft',
'post_title' => 'Some title',
'meta_input' => [
'_einsatz_timeofalerting' => '2021-08-29 17:51:15'
'_einsatz_timeofalerting' => '2021-08-29 17:51:15',
'einsatz_special' => 0,
],
'tax_input' => [
'einsatzart' => [9384]
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,23 @@ public function testUpgrade1100AssignsUserRoles()
expect('update_option')->once()->with('einsatzvw_db_version', 71);
(new Update())->upgrade1100();
}

/**
* @throws ExpectationArgsRequired
*/
public function testUpgrade1102Foo()
{
// Return some posts that are missing the 'special' postmeta
$post1 = Mockery::mock('\WP_Post');
$post1->ID = 2893;
$post2 = Mockery::mock('\WP_Post');
$post2->ID = 11280;
expect('get_posts')->once()->andReturn([$post1, $post2]);

expect('add_post_meta')->once()->with($post1->ID, 'einsatz_special', 0, true);
expect('add_post_meta')->once()->with($post2->ID, 'einsatz_special', 0, true);

expect('update_option')->once()->with('einsatzvw_db_version', 72);
(new Update())->upgrade1102();
}
}

0 comments on commit be896ed

Please sign in to comment.