db_manager = $db_manager; $this->a0_options = $a0_options; } public function init() { add_action( 'wp_dashboard_setup', array( $this, 'set_up' ) ); add_action( 'admin_footer', array( $this, 'render' ) ); add_action( 'admin_notices', array( $this, 'show_admin_notice' ) ); add_action( 'admin_init', array( $this, 'notice_ignore' ) ); } public function show_admin_notice() { $screen = get_current_screen(); if ( $screen->id !== 'dashboard' ) { return; } global $current_user ; $user_id = $current_user->ID; if ( ! get_user_meta( $user_id, 'a0_ignore_widgets_explanation' ) ) { echo '

'; printf( __( 'Auth0 tip: You can filter the data by clicking on the charts. Click again to clear the selection. | Hide', 'wp-auth0' ), '?a0_ignore_widgets_explanation=0' ); echo "

"; } } public function notice_ignore() { global $current_user; $user_id = $current_user->ID; if ( isset( $_GET['a0_ignore_widgets_explanation'] ) && '0' == $_GET['a0_ignore_widgets_explanation'] ) { add_user_meta( $user_id, 'a0_ignore_widgets_explanation', 'true', true ); } } protected function get_buckets( $from, $to, $step ) { $buckets = array(); $buckets[] = array( 'from' => 0, 'to' => $from - 1, 'name' => $step == 1 ? ( $from - 1 ) : ( '< ' . ( $from - 1 ) ), ); for ( $a = $from; $a < $to; $a += $step ) { $buckets[] = array( 'from' => $a, 'to' => $a + $step - 1, 'name' => $step == 1 ? $a : ( $a . '-' . ( $a + $step - 1 ) ), ); } $buckets[] = array( 'from' => $a, 'to' => 200, 'name' => $step == 1 ? $a : ( '>= ' . $a ), ); return $buckets; } public function render() { global $current_user; if ( ! in_array( 'administrator', $current_user->roles ) ) { return; } $screen = get_current_screen(); if ( $screen->id !== 'dashboard' ) { return; } $users = $this->db_manager->get_auth0_users(); $this->buckets = $this->get_buckets( $this->a0_options->get( 'chart_age_from' ), $this->a0_options->get( 'chart_age_to' ), $this->a0_options->get( 'chart_age_step' ) ); $usersData = array(); foreach ( $users as $user ) { $userObj = new WP_Auth0_UserProfile( $user->auth0_obj ); $userData = $userObj->get(); $userData['gender'] = empty( $userData['gender'] ) ? self::UNKNOWN_KEY : $userData['gender']; $userData['income'] = empty( $userData['income'] ) ? 0 : $userData['income']; $userData['created_at_day'] = date( 'Y-m-d', strtotime( $userData['created_at'] ) ); if ( ! $userData['age'] ) { $userData['age'] = self::UNKNOWN_KEY; $userData['agebucket'] = self::UNKNOWN_KEY; } else { foreach ( $this->buckets as $bucket ) { if ( $userData['age'] >= $bucket['from'] && $userData['age'] <= $bucket['to'] ) { $userData['agebucket'] = $bucket['name']; } } } $usersData[] = $userData; } ?> roles ) ) { return; } wp_enqueue_style( 'auth0-dashboard-c3-css', WPA0_PLUGIN_LIB_URL . 'c3/c3.min.css' ); wp_enqueue_style( 'auth0-dashboard-css', WPA0_PLUGIN_CSS_URL . 'dashboard.css' ); wp_enqueue_script( 'auth0-dashboard-d3', WPA0_PLUGIN_LIB_URL . 'd3/d3.min.js' ); wp_enqueue_script( 'auth0-dashboard-c3-js', WPA0_PLUGIN_LIB_URL . 'c3/c3.min.js' ); wp_enqueue_script( 'auth0-lodash', WPA0_PLUGIN_LIB_URL . 'lodash.min.js' ); wp_enqueue_script( 'auth0-parallelcoordinates', WPA0_PLUGIN_LIB_URL . 'parallelcoordinates.js' ); wp_enqueue_script( 'auth0-dualdimentionbars', WPA0_PLUGIN_LIB_URL . 'dualdimentionbars.js' ); $widgets = array( new WP_Auth0_Dashboard_Plugins_Age( $this->a0_options ), new WP_Auth0_Dashboard_Plugins_Gender( $this->a0_options ), new WP_Auth0_Dashboard_Plugins_IdP( $this->a0_options ), new WP_Auth0_Dashboard_Plugins_Location(), new WP_Auth0_Dashboard_Plugins_Income(), new WP_Auth0_Dashboard_Plugins_Signups(), ); foreach ( $widgets as $widget ) { wp_add_dashboard_widget( $widget->getId(), $widget->getName(), array( $widget, 'render' ) ); } } }