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

Minimalstatistiken #1440

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CommonsBooking\Settings\Settings;
use CommonsBooking\Repository\BookingCodes;
use CommonsBooking\View\Dashboard;
use CommonsBooking\View\Statistics;
use CommonsBooking\Wordpress\CustomPostType\CustomPostType;
use CommonsBooking\Wordpress\CustomPostType\Item;
use CommonsBooking\Wordpress\CustomPostType\Location;
Expand Down Expand Up @@ -597,6 +598,7 @@ public static function registerScriptsAndStyles() {

public function registerShortcodes() {
add_shortcode('cb_search', array(new SearchShortcode(), 'execute'));
add_shortcode( 'cb_statistics', array( Statistics::class, 'shortcode' ) );
}

/**
Expand Down
78 changes: 78 additions & 0 deletions src/View/Statistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace CommonsBooking\View;

class Statistics extends View {

public static function __callStatic( $name, $arguments ) {
if ( strpos( $name, 'sum_' ) === 0 ) {
$metaValue = str_replace('sum_', '', $name);
return self::sumMeta($arguments[0], $metaValue);
}
elseif ( strpos( $name, 'count_' ) === 0 ) {
$metaValue = str_replace('count_', '', $name);
return self::countMeta($arguments[0], $metaValue);
}
elseif ( strpos( $name, 'avg_' ) === 0 ) {
$metaValue = str_replace('avg_', '', $name);
return self::avgMeta($arguments[0], $metaValue);
}
}

public static function shortcode( $args ) {
$args = shortcode_atts( [
'do' => '',
'type' => '',
], $args, 'cb_statistics' );

$fn = $args['do'];
$type = $args['type'];

if ( ! $fn || ! $type ) {
return '';
}

switch ($type) {
case 'booking':
$posts = \CommonsBooking\Repository\Booking::get( [], [], null, true, null, ['confirmed', 'unconfirmed', 'canceled', 'publish', 'inherit'] );
break;
case 'item':
$posts = \CommonsBooking\Repository\Item::get([],true);
break;
case 'location':
$posts = \CommonsBooking\Repository\Location::get([],true);
break;
default:
return '';
}

return self::$fn( $posts );

}

public static function count( $posts ): int {
return count( $posts );
}

private static function getProperty( $post, $property ) {
if ( method_exists( $post, $property ) ) {
return $post->{$property}();
} else {
return $post->getMeta( $property );
}
}

public static function sumMeta( $posts, $metaValue ): int {
return array_sum( array_map( fn( $post ) => self::getProperty( $post, $metaValue ), $posts ) );
}

public static function countMeta( $posts, $metaValue ): int {
return count( array_filter( $posts, fn( $post ) => self::getProperty( $post, $metaValue ) ) );
}

public static function avgMeta( $posts, $metaValue ): int {
$sum = self::sumMeta($posts, $metaValue);
$count = self::countMeta($posts, $metaValue);
return $sum / $count;
}
}