load_assets(); $data = $this->get_data(); if ( empty( $data ) ) { return null; } // Based on the $type and $metric we'll compose the template path and class name for the block output. $template_file = MONSTERINSIGHTS_PLUGIN_DIR . 'includes/gutenberg/site-insights/templates/' . $type . '/class-' . $type . '-' . $metric . '.php'; if (!file_exists($template_file)) { return false; } require_once MONSTERINSIGHTS_PLUGIN_DIR . '/includes/gutenberg/site-insights/templates/class-site-insights-metric-template.php'; require_once MONSTERINSIGHTS_PLUGIN_DIR . '/includes/gutenberg/site-insights/templates/class-site-insights-duoscorecard-template.php'; require_once $template_file; $class_name = 'MonsterInsights_SiteInsights_Template_' . ucfirst($type) . '_' . ucfirst($metric); if ( !class_exists($class_name) ) { return false; } $template = new $class_name($attributes, $data); $output = $template->output(); $block_attributes = get_block_wrapper_attributes(array( 'class' => "monsterinsights-{$type}-block", )); return sprintf( '
%2$s
', $block_attributes, $output ); } /** * Returns report data for a specific date range. * * @return array|bool */ private function get_data() { // We do not have a current auth. $site_auth = MonsterInsights()->auth->get_viewname(); $ms_auth = is_multisite() && MonsterInsights()->auth->get_network_viewname(); if ( ! $site_auth && ! $ms_auth ) { return false; } require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reports/abstract-report.php'; require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/reports/site-insights.php'; $report = new MonsterInsights_Report_Site_Insights(); $data = $report->get_data(array( 'start' => date( 'Y-m-d', strtotime( '-31 days' ) ), 'end' => date( 'Y-m-d', strtotime( '-1 days' ) ) )); if ( !isset( $data['success'] ) || !$data['success'] || empty($data['data'])) { return false; } return $data['data']; } }