views
4 years ago
Data.php
4 years ago
Dates.php
4 years ago
Metadata.php
4 years ago
Renderer.php
4 years ago
Metadata.php
149 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo\Report; |
| 11 | |
| 12 | use Piwik\API\Request; |
| 13 | use WpMatomo\Bootstrap; |
| 14 | use WpMatomo\Site; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // if accessed directly |
| 18 | } |
| 19 | |
| 20 | class Metadata { |
| 21 | |
| 22 | public static $cache_all_reports = []; |
| 23 | public static $cache_all_report_pages = []; |
| 24 | |
| 25 | public function get_all_reports() { |
| 26 | if ( ! empty( self::$cache_all_reports ) ) { |
| 27 | return self::$cache_all_reports; |
| 28 | } |
| 29 | |
| 30 | $site = new Site(); |
| 31 | $idsite = $site->get_current_matomo_site_id(); |
| 32 | |
| 33 | if ( $idsite ) { |
| 34 | Bootstrap::do_bootstrap(); |
| 35 | |
| 36 | $all_reports = Request::processRequest( |
| 37 | 'API.getReportMetadata', |
| 38 | [ |
| 39 | 'idSite' => $idsite, |
| 40 | 'filter_limit' => - 1, |
| 41 | ] |
| 42 | ); |
| 43 | foreach ( $all_reports as $single_report ) { |
| 44 | if ( isset( $single_report['uniqueId'] ) ) { |
| 45 | self::$cache_all_reports[ $single_report['uniqueId'] ] = $single_report; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return self::$cache_all_reports; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @internal |
| 55 | * tests only |
| 56 | */ |
| 57 | public static function clear_cache() { |
| 58 | self::$cache_all_reports = []; |
| 59 | self::$cache_all_report_pages = []; |
| 60 | } |
| 61 | |
| 62 | public function find_report_by_unique_id( $unique_id ) { |
| 63 | if ( Renderer::CUSTOM_UNIQUE_ID_VISITS_OVER_TIME === $unique_id ) { |
| 64 | return [ |
| 65 | 'uniqueId' => Renderer::CUSTOM_UNIQUE_ID_VISITS_OVER_TIME, |
| 66 | 'name' => 'Visits over time', |
| 67 | ]; |
| 68 | } |
| 69 | $all_reports = self::get_all_reports(); |
| 70 | |
| 71 | if ( isset( $all_reports[ $unique_id ] ) ) { |
| 72 | return $all_reports[ $unique_id ]; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public function get_all_report_pages() { |
| 77 | if ( ! empty( self::$cache_all_report_pages ) ) { |
| 78 | return self::$cache_all_report_pages; |
| 79 | } |
| 80 | |
| 81 | $site = new Site(); |
| 82 | $idsite = $site->get_current_matomo_site_id(); |
| 83 | |
| 84 | if ( $idsite ) { |
| 85 | Bootstrap::do_bootstrap(); |
| 86 | |
| 87 | self::$cache_all_report_pages = Request::processRequest( |
| 88 | 'API.getReportPagesMetadata', |
| 89 | [ |
| 90 | 'idSite' => $idsite, |
| 91 | 'filter_limit' => - 1, |
| 92 | ] |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | return self::$cache_all_report_pages; |
| 97 | } |
| 98 | |
| 99 | public function find_report_page_params_by_report_metadata( $report_metadata ) { |
| 100 | if ( empty( $report_metadata['module'] ) |
| 101 | || empty( $report_metadata['action'] ) ) { |
| 102 | return []; |
| 103 | } |
| 104 | |
| 105 | $report_pages = self::get_all_report_pages(); |
| 106 | |
| 107 | foreach ( $report_pages as $report_page ) { |
| 108 | if ( ! empty( $report_page['widgets'] ) ) { |
| 109 | foreach ( $report_page['widgets'] as $widget ) { |
| 110 | if ( ! empty( $widget['module'] ) && $widget['module'] === $report_metadata['module'] |
| 111 | && ! empty( $widget['action'] ) && $widget['action'] === $report_metadata['action'] ) { |
| 112 | return [ |
| 113 | 'category' => $report_page['category']['id'], |
| 114 | 'subcategory' => $report_page['subcategory']['id'], |
| 115 | ]; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // we can't resolve all automatically since reportId != widgetId and the used action may differe etc... |
| 122 | // we're hard coding some manually |
| 123 | |
| 124 | if ( 'Actions_get' === $report_metadata['uniqueId'] ) { |
| 125 | return [ |
| 126 | 'category' => 'General_Visitors', |
| 127 | 'subcategory' => 'General_Overview', |
| 128 | ]; |
| 129 | } elseif ( 'Goals_get' === $report_metadata['uniqueId'] ) { |
| 130 | return [ |
| 131 | 'category' => 'Goals_Goals', |
| 132 | 'subcategory' => 'General_Overview', |
| 133 | ]; |
| 134 | } elseif ( 'Goals_get_idGoal--ecommerceOrder' === $report_metadata['uniqueId'] ) { |
| 135 | return [ |
| 136 | 'category' => 'Goals_Ecommerce', |
| 137 | 'subcategory' => 'General_Overview', |
| 138 | ]; |
| 139 | } elseif ( 'Goals_getItemsName' === $report_metadata['uniqueId'] ) { |
| 140 | return [ |
| 141 | 'category' => 'Goals_Ecommerce', |
| 142 | 'subcategory' => 'Goals_Products', |
| 143 | ]; |
| 144 | } |
| 145 | |
| 146 | return []; |
| 147 | } |
| 148 | } |
| 149 |