views
6 years ago
Data.php
6 years ago
Dates.php
6 years ago
Metadata.php
6 years ago
Renderer.php
6 years ago
Metadata.php
146 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 | public static $CACHE_ALL_REPORTS = array(); |
| 22 | public static $CACHE_ALL_REPORT_PAGES = array(); |
| 23 | |
| 24 | public function get_all_reports() { |
| 25 | if ( ! empty( self::$CACHE_ALL_REPORTS ) ) { |
| 26 | return self::$CACHE_ALL_REPORTS; |
| 27 | } |
| 28 | |
| 29 | $site = new Site(); |
| 30 | $idsite = $site->get_current_matomo_site_id(); |
| 31 | |
| 32 | if ( $idsite ) { |
| 33 | Bootstrap::do_bootstrap(); |
| 34 | |
| 35 | $all_reports = Request::processRequest( |
| 36 | 'API.getReportMetadata', |
| 37 | array( |
| 38 | 'idSite' => $idsite, |
| 39 | 'filter_limit' => - 1, |
| 40 | ) |
| 41 | ); |
| 42 | foreach ( $all_reports as $single_report ) { |
| 43 | if ( isset( $single_report['uniqueId'] ) ) { |
| 44 | self::$CACHE_ALL_REPORTS[ $single_report['uniqueId'] ] = $single_report; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return self::$CACHE_ALL_REPORTS; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @internal |
| 54 | * tests only |
| 55 | */ |
| 56 | public static function clear_cache() { |
| 57 | self::$CACHE_ALL_REPORTS = array(); |
| 58 | self::$CACHE_ALL_REPORT_PAGES = array(); |
| 59 | } |
| 60 | |
| 61 | public function find_report_by_unique_id( $unique_id ) { |
| 62 | if ($unique_id === Renderer::CUSTOM_UNIQUE_ID_VISITS_OVER_TIME) { |
| 63 | return array('uniqueId' => Renderer::CUSTOM_UNIQUE_ID_VISITS_OVER_TIME, 'name' => 'Visits over time'); |
| 64 | } |
| 65 | $all_reports = self::get_all_reports(); |
| 66 | |
| 67 | if ( isset( $all_reports[ $unique_id ] ) ) { |
| 68 | return $all_reports[ $unique_id ]; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public function get_all_report_pages() { |
| 73 | if ( ! empty( self::$CACHE_ALL_REPORT_PAGES ) ) { |
| 74 | return self::$CACHE_ALL_REPORT_PAGES; |
| 75 | } |
| 76 | |
| 77 | $site = new Site(); |
| 78 | $idsite = $site->get_current_matomo_site_id(); |
| 79 | |
| 80 | if ( $idsite ) { |
| 81 | Bootstrap::do_bootstrap(); |
| 82 | |
| 83 | self::$CACHE_ALL_REPORT_PAGES = Request::processRequest( |
| 84 | 'API.getReportPagesMetadata', |
| 85 | array( |
| 86 | 'idSite' => $idsite, |
| 87 | 'filter_limit' => - 1, |
| 88 | ) |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | return self::$CACHE_ALL_REPORT_PAGES; |
| 93 | } |
| 94 | |
| 95 | public function find_report_page_params_by_report_metadata( $report_metadata ) { |
| 96 | if ( empty( $report_metadata['module'] ) |
| 97 | || empty( $report_metadata['action'] ) ) { |
| 98 | return array(); |
| 99 | } |
| 100 | |
| 101 | $report_pages = self::get_all_report_pages(); |
| 102 | |
| 103 | foreach ( $report_pages as $report_page ) { |
| 104 | if ( ! empty( $report_page['widgets'] ) ) { |
| 105 | foreach ( $report_page['widgets'] as $widget ) { |
| 106 | if ( ! empty( $widget['module'] ) && $widget['module'] === $report_metadata['module'] |
| 107 | && ! empty( $widget['action'] ) && $widget['action'] === $report_metadata['action'] ) { |
| 108 | return array( |
| 109 | 'category' => $report_page['category']['id'], |
| 110 | 'subcategory' => $report_page['subcategory']['id'], |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // we can't resolve all automatically since reportId != widgetId and the used action may differe etc... |
| 118 | // we're hard coding some manually |
| 119 | |
| 120 | if ( 'Actions_get' === $report_metadata['uniqueId'] ) { |
| 121 | return array( |
| 122 | 'category' => 'General_Visitors', |
| 123 | 'subcategory' => 'General_Overview', |
| 124 | ); |
| 125 | } elseif ( 'Goals_get' === $report_metadata['uniqueId'] ) { |
| 126 | return array( |
| 127 | 'category' => 'Goals_Goals', |
| 128 | 'subcategory' => 'General_Overview', |
| 129 | ); |
| 130 | } elseif ( 'Goals_get_idGoal--ecommerceOrder' === $report_metadata['uniqueId'] ) { |
| 131 | return array( |
| 132 | 'category' => 'Goals_Ecommerce', |
| 133 | 'subcategory' => 'General_Overview', |
| 134 | ); |
| 135 | } elseif ( 'Goals_getItemsName' === $report_metadata['uniqueId'] ) { |
| 136 | return array( |
| 137 | 'category' => 'Goals_Ecommerce', |
| 138 | 'subcategory' => 'Goals_Products', |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | return array(); |
| 143 | } |
| 144 | |
| 145 | } |
| 146 |