'', 'slug' => 'option_not_available_in_ce', 'type' => 'hook', ], ]; const PROXY_WARNING_HOOK = [ 'label' => '', 'slug' => 'proxy_warning', 'type' => 'hook', ]; const OPTION_DISABLED_BY_PROXY_HOOK = [ 'label' => '', 'slug' => 'option_disabled_by_proxy', 'type' => 'hook', ]; const API_TOKEN_MISSING_HOOK = [ 'label' => '', 'slug' => 'api_token_missing', 'type' => 'hook', ]; const OPTION_DISABLED_BY_MISSING_API_TOKEN_HOOK = [ 'label' => '', 'slug' => 'option_disabled_by_missing_api_token', 'type' => 'hook', ]; const ENABLE_ANALYTICS_DASH_NOTICE = [ 'label' => '', 'slug' => 'enable_analytics_dashboard_notice', 'type' => 'hook', 'hook_type' => 'success', ]; /** * @var array|array[] $fields */ public $fields = []; /** * @var array $wizard_fields */ public $wizard_fields = []; /** * Constructor. * @since 1.3.0 * @access public * @return void * @throws Exception */ public function __construct() { $this->init(); $settings = Helpers::get_settings(); $this->fields = [ 'general' => [ [ 'label' => esc_html__( 'Connect your website with Plausible Analytics', 'plausible-analytics' ), 'slug' => 'connect_to_plausible_analytics', 'type' => 'group', 'desc' => sprintf( wp_kses( // translators: %s: URL to Plausible account settings. __( 'Ensure your domain name matches the one in your Plausible account, then create a Plugin Token (link opens in a new window) and paste it into the \'Plugin Token\' field.', 'plausible-analytics' ), 'post' ), Helpers::get_hosted_domain_url() . '/sites' ), 'fields' => [ [ 'label' => esc_html__( 'Language Domains', 'plausible-analytics' ), 'slug' => 'domain_map', 'type' => 'domain_map', ], ], ], [ 'label' => esc_html__( 'Enhanced measurements', 'plausible-analytics' ), 'slug' => 'enhanced_measurements', 'type' => 'group', 'divide' => true, // translators: %1$s replaced with outbound-links. 'desc' => esc_html__( 'Enable enhanced measurements that you\'d like to track.', 'plausible-analytics' ), 'fields' => [ EnhancedMeasurements::FOUR_O_FOUR => [ 'label' => esc_html__( '404 error pages', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#404-error-pages', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::FOUR_O_FOUR, 'caps' => [ Capabilities::GOALS ], ], EnhancedMeasurements::FILE_DOWNLOADS => [ 'label' => esc_html__( 'File downloads', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#file-downloads', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::FILE_DOWNLOADS, 'caps' => [ Capabilities::GOALS ], ], EnhancedMeasurements::OUTBOUND_LINKS => [ 'label' => esc_html__( 'Outbound links', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#outbound-links', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::OUTBOUND_LINKS, 'caps' => [ Capabilities::GOALS ], ], EnhancedMeasurements::PAGEVIEW_PROPS => [ 'label' => esc_html__( 'Authors and categories', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#authors-and-categories', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::PAGEVIEW_PROPS, 'caps' => [ Capabilities::PROPS ], ], EnhancedMeasurements::ECOMMERCE_REVENUE => [ 'label' => esc_html__( 'Ecommerce revenue', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#track-ecommerce-revenue', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::ECOMMERCE_REVENUE, 'caps' => [ Capabilities::GOALS, Capabilities::FUNNELS, Capabilities::PROPS, Capabilities::REVENUE, ], 'disabled' => ! empty( $settings['self_hosted_domain'] ), 'disabled_tooltip' => self::OPTION_NOT_AVAILABLE_IN_CE_HOOK, ], EnhancedMeasurements::FORM_COMPLETIONS => [ 'label' => esc_html__( 'Form completions', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#form-completions', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::FORM_COMPLETIONS, 'caps' => [ Capabilities::GOALS ], ], EnhancedMeasurements::LOGGED_IN_USER_STATUS => [ 'label' => esc_html__( 'Logged-in user status', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#logged-in-user-status', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::LOGGED_IN_USER_STATUS, 'caps' => [ Capabilities::PROPS ], ], EnhancedMeasurements::SEARCH_QUERIES => [ 'label' => esc_html__( 'Search queries', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#search-queries', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::SEARCH_QUERIES, 'caps' => [ Capabilities::GOALS ], ], EnhancedMeasurements::HASH_BASED_ROUTING => [ 'label' => esc_html__( 'Hash-based routing', 'plausible-analytics' ), 'docs' => 'https://plausible.io/docs/wordpress-integration#hash-based-routing', 'slug' => 'enhanced_measurements', 'type' => 'checkbox', 'value' => EnhancedMeasurements::HASH_BASED_ROUTING, 'caps' => [], ], ], ], [ 'label' => esc_html__( 'Cloaked affiliate links', 'plausible-analytics' ), 'slug' => 'cloaked_affiliate_links', 'type' => 'group', 'docs' => 'https://plausible.io/docs/wordpress-integration#cloaked-affiliate-links', 'desc' => sprintf( // translators: %s: Example URL to affiliate product. __( 'Enter the (partial) URLs you\'d like to track. E.g. enter /recommends/ if you want to track %s.', 'plausible-analytics' ), get_home_url() . '/recommends/affiliate-product/' ), 'fields' => [ [ 'slug' => 'affiliate_links', 'type' => 'clonable_text', 'value' => Helpers::get_settings()['affiliate_links'] ?? [], ], ], ], [ 'label' => esc_html__( 'Query parameters', 'plausible-analytics' ), 'slug' => 'query_params', 'type' => 'group', 'docs' => 'https://plausible.io/docs/wordpress-integration#query-parameters', 'desc' => sprintf( // translators: %s: Example URL with query parameter. __( 'Enter the query parameters you\'d like to track. E.g., enter lang if you want to track %s.', 'plausible-analytics' ), get_home_url() . '?lang=en' ), 'fields' => [ [ 'slug' => 'query_params', 'type' => 'clonable_text', 'value' => Helpers::get_settings()['query_params'] ?? [], ], ], ], [ 'label' => esc_html__( 'Bypass ad blockers', 'plausible-analytics' ), 'slug' => 'bypass_ad_blockers', 'type' => 'group', 'desc' => sprintf( wp_kses( // translators: 1: Proxy endpoint prefix, 2: Proxy endpoint details, 3: URL to learn more about proxy. __( 'Concerned about ad blockers? You can run the Plausible script as a first-party connection from your domain name to count visitors who use ad blockers. The proxy uses WordPress\' API with a randomly generated endpoint, starting with %1$s and %2$s. Learn more »', 'plausible-analytics' ), wp_kses_allowed_html( 'post' ) ), get_site_url( null, rest_get_url_prefix() ), empty( Helpers::get_settings()['proxy_enabled'] ) ? 'a random directory/file for storing the JS file' : 'a JS file, called ' . str_replace( ABSPATH, '', Helpers::get_proxy_resource( 'cache_dir' ) . Helpers::get_filename() . '.js' ), 'https://plausible.io/docs/wordpress-integration#bypass-adblockers-with-the-proxy' ), 'fields' => [ [ 'label' => esc_html__( 'Enable proxy', 'plausible-analytics' ), 'slug' => 'proxy_enabled', 'type' => 'checkbox', 'value' => 'on', 'disabled' => ! empty( Helpers::get_settings()['self_hosted_domain'] ), 'disabled_tooltip' => self::OPTION_NOT_AVAILABLE_IN_CE_HOOK, ], ], ], [ 'label' => esc_html__( 'View your stats in your WordPress dashboard', 'plausible-analytics' ), 'slug' => 'is_shared_link', 'type' => 'group', 'docs' => 'https://plausible.io/docs/wordpress-integration#view-your-stats-inside-wordpress', 'desc' => esc_html__( 'View your site statistics within your WordPress Dashboard.', 'plausible-analytics' ), 'fields' => [ [ 'label' => esc_html__( 'View stats in WordPress', 'plausible-analytics' ), 'slug' => 'enable_analytics_dashboard', 'type' => 'checkbox', 'value' => 'on', 'disabled' => empty( Helpers::get_settings()['api_token'] ) && empty( Helpers::get_settings()['self_hosted_domain'] ), ], ], ], [ 'label' => esc_html__( 'Exclude specific pages from being tracked', 'plausible-analytics' ), 'slug' => 'is_exclude_pages', 'type' => 'group', 'desc' => sprintf( '%1$s %3$s', esc_html__( 'Exclude certain pages from being tracked. You can use an asterisk (*) to match patterns in your page URLs.', 'plausible-analytics' ), esc_url( 'https://plausible.io/docs/wordpress-integration#exclude-specific-pages-from-tracking' ), esc_html__( 'See syntax »', 'plausible-analytics' ) ), 'fields' => [ [ 'label' => esc_html__( 'Excluded pages', 'plausible-analytics' ), 'slug' => 'excluded_pages', 'type' => 'textarea', 'value' => $settings['excluded_pages'], 'placeholder' => esc_html__( 'E.g.', 'plausible-analytics' ) . '/example-page/, *keyword*, /directory*', ], [ 'label' => __( 'Save', 'plausible-analytics' ), 'slug' => 'save-excluded-pages', 'type' => 'button', ], ], ], [ 'label' => esc_html__( 'Track analytics for user roles', 'plausible-analytics' ), 'slug' => 'tracked_user_roles', 'type' => 'group', 'docs' => 'https://plausible.io/docs/wordpress-integration#exclude-admins-and-other-roles', 'desc' => esc_html__( 'By default, visits from logged in users aren\'t tracked. If you want to track visits for certain user roles then please specify them below.', 'plausible-analytics' ), 'fields' => $this->build_user_roles_array( 'tracked_user_roles' ), 'divide' => true, ], [ 'label' => esc_html__( 'Show stats dashboard to additional user roles', 'plausible-analytics' ), 'slug' => 'expand_dashboard_access', 'type' => 'group', 'docs' => 'https://plausible.io/docs/wordpress-integration#view-your-stats-inside-wordpress', 'desc' => esc_html__( 'By default, the stats dashboard is only available to logged in administrators. If you want the dashboard to be available for other logged in users, then please specify them below.', 'plausible-analytics' ), 'fields' => $this->build_user_roles_array( 'expand_dashboard_access', [ 'administrator' => true ] ), 'divide' => true, ], [ 'label' => esc_html__( 'Disable menu in toolbar', 'plausible-analytics' ), 'slug' => 'disable_toolbar_menu', 'type' => 'group', 'docs' => 'https://plausible.io/docs/wordpress-integration#disable-the-toolbar-menu', 'desc' => esc_html__( 'Check this option if you don\'t want the Plausible Analytics menu item to be added to the toolbar at the top of the screen.', 'plausible-analytics' ), 'add_sub_array' => false, 'fields' => [ 'disable_toolbar_menu' => [ 'label' => esc_html__( 'Disable toolbar menu', 'plausible-analytics' ), 'slug' => 'disable_toolbar_menu', 'type' => 'checkbox', 'value' => 'on', ], ], ], ], 'self-hosted' => [ [ 'label' => esc_html__( 'Plausible Community Edition', 'plausible-analytics' ), 'slug' => 'is_self_hosted', 'type' => 'group', 'desc' => sprintf( '%1$s %3$s', wp_kses( __( 'If you\'re using Plausible Community Edition on your own infrastructure, enter the domain name where you installed it to enable the integration with your self-hosted instance. Multisites can use the PLAUSIBLE_SELF_HOSTED_DOMAIN constant to define the URL for all subsites at once.', 'plausible-analytics' ), 'post' ), esc_url( 'https://plausible.io/self-hosted-web-analytics/' ), esc_html__( 'Learn more about Plausible Community Edition.', 'plausible-analytics' ) ), 'fields' => [ [ 'label' => esc_html__( 'Domain name', 'plausible-analytics' ), 'slug' => 'self_hosted_domain', 'type' => 'text', 'value' => defined( 'PLAUSIBLE_SELF_HOSTED_DOMAIN' ) ? PLAUSIBLE_SELF_HOSTED_DOMAIN : $settings['self_hosted_domain'], 'placeholder' => 'e.g. ' . Helpers::get_domain(), 'disabled' => Helpers::proxy_enabled(), ], [ 'label' => __( 'Save', 'plausible-analytics' ), 'slug' => 'save-self-hosted', 'type' => 'button', 'disabled' => Helpers::proxy_enabled(), ], ], ], [ 'label' => esc_html__( 'View stats in your WordPress dashboard', 'plausible-analytics' ), 'slug' => 'self_hosted_shared_link', 'type' => 'group', 'desc' => sprintf( // translators: %s: URL to Plausible shared link documentation. '
  1. ' . __( 'Create a secure and private shared link in your Plausible account.', 'plausible-analytics' ) . '
  2. ' . __( 'Paste the shared link in the text box to view your stats in your WordPress dashboard.', 'plausible-analytics' ) . '
  3. ' . '
', esc_url( 'https://plausible.io/docs/embed-dashboard' ) ), 'fields' => [ [ 'label' => esc_html__( 'Shared link', 'plausible-analytics' ), 'slug' => 'self_hosted_shared_link', 'type' => 'text', 'value' => $settings['self_hosted_shared_link'], 'placeholder' => sprintf( // translators: 1: Plausible hosted domain URL, 2: Site domain name. wp_kses( __( 'E.g. %1$s/share/%2$s?auth=XXXXXXXXXXXX', 'plausible-analytics' ), 'post' ), Helpers::get_hosted_domain_url(), Helpers::get_domain() ), 'disabled' => Helpers::proxy_enabled(), ], [ 'label' => __( 'Save', 'plausible-analytics' ), 'slug' => 'save-self-hosted-shared-link', 'type' => 'button', 'disabled' => Helpers::proxy_enabled(), ], ], ], ], ]; /** * If the proxy is enabled, or the self-hosted domain option has a value, display a warning box. * * @see self::proxy_warning() */ if ( Helpers::proxy_enabled() && ! empty( $settings['self_hosted_domain'] ) ) { $this->fields['general'][4]['fields'][] = self::PROXY_WARNING_HOOK; } /** * If proxy is enabled, disable Self-hosted fields and display a warning. */ if ( Helpers::proxy_enabled() ) { $this->fields['self-hosted'][0]['fields'][] = self::OPTION_DISABLED_BY_PROXY_HOOK; $this->fields['self-hosted'][1]['fields'][] = self::OPTION_DISABLED_BY_PROXY_HOOK; } /** * No Plugin Token is entered. */ if ( empty( Helpers::get_settings()['api_token'][ Helpers::get_current_language_domain_key() ] ) ) { $this->fields['general'][0]['fields'][] = self::API_TOKEN_MISSING_HOOK; $this->fields['general'][5]['fields'][] = self::OPTION_DISABLED_BY_MISSING_API_TOKEN_HOOK; } /** * If View Stats is enabled, display a notice. */ if ( ! empty( Helpers::get_settings()['api_token'][ Helpers::get_current_language_domain_key() ] ) && ! empty( $settings['enable_analytics_dashboard'] ) ) { $this->fields['general'][5]['fields'][] = self::ENABLE_ANALYTICS_DASH_NOTICE; } $this->wizard_fields = [ 'domain_name' => [ 'label' => esc_html__( 'Domain name', 'plausible-analytics' ), 'slug' => 'domain_name[default]', 'type' => 'text', 'value' => Helpers::get_domain(), ], 'api_token' => [ 'label' => sprintf( '%s - %s', esc_html__( 'Plugin Token', 'plausible-analytics' ), __( 'Create Token', 'plausible-analytics' ) ), 'slug' => 'api_token[default]', 'type' => 'text', 'value' => Helpers::get_api_token(), ], 'connect_plausible_analytics' => [ 'label' => empty( Helpers::get_domain() ) || empty( Helpers::get_api_token() ) ? esc_html__( 'Connect', 'plausible-analytics' ) : esc_html__( 'Connected', 'plausible-analytics' ), 'slug' => 'connect_plausible_analytics', 'type' => 'button', 'disabled' => empty( Helpers::get_domain() ) || empty( Helpers::get_api_token() ), ], ]; } /** * Init action hooks. * @return void */ private function init() { /** * WP Core hooks */ add_action( 'admin_menu', [ $this, 'register_menu' ] ); add_action( 'in_admin_header', [ $this, 'add_background_color' ] ); /** * Hooks that run on the settings page. */ new Hooks(); } /** * Load all available user roles as a list (sorted alphabetically) of checkboxes to be processed by the Settings * API. * * @param string $slug * * @return array */ private function build_user_roles_array( $slug, $disable_elements = [] ) { $wp_roles = wp_roles()->roles ?? []; foreach ( $wp_roles as $id => $role ) { $roles_array[ $id ] = [ 'label' => $role['name'] ?? '', 'slug' => $slug, 'type' => 'checkbox', 'value' => $id, ]; if ( in_array( $id, array_keys( $disable_elements ), true ) ) { $roles_array[ $id ]['disabled'] = true; if ( ! empty( $disable_elements[ $id ] ) ) { $roles_array[ $id ]['checked'] = $disable_elements[ $id ]; } } } ksort( $roles_array, SORT_STRING ); return $roles_array; } /** * A little hack to add some classes to the core #wpcontent div. * @return void */ public function add_background_color() { if ( array_key_exists( 'page', $_GET ) && $_GET['page'] == 'plausible_analytics' ) { echo ""; } } /** * Register Menu. * @since 1.0.0 * @access public * @return void */ public function register_menu() { $user = wp_get_current_user(); $user_roles = $user->roles ?? []; $is_allowed = false; /** * By default, only allow administrators to access the Statistics page. */ $capabilities = 'manage_options'; /** * Let's see if current user is allowed to access the Stats page. */ $allowed_roles = Helpers::get_settings()['expand_dashboard_access'] ?? []; foreach ( $user_roles as $user_role ) { if ( is_array( $allowed_roles ) && in_array( $user_role, $allowed_roles, true ) ) { $is_allowed = true; break; } } /** * If current user role is allowed to access, overwrite $capabilities with this user's capabilities. */ if ( $is_allowed ) { if ( isset( $user->caps ) ) { reset( $user->caps ); $capabilities = key( $user->caps ); } } $settings = Helpers::get_settings(); /** * Don't show the Analytics dashboard, if View Stats is disabled. */ if ( ! empty( $settings['enable_analytics_dashboard'] ) || ( ! empty( $settings['self_hosted_domain'] ) && ! empty( $settings['self_hosted_shared_link'] ) ) ) { // Setup `Analytics` page under Dashboard. add_dashboard_page( esc_html__( 'Analytics', 'plausible-analytics' ), esc_html__( 'Analytics', 'plausible-analytics' ), $capabilities, 'plausible_analytics_statistics', [ $this, 'render_analytics_dashboard', ] ); } // Setup `Plausible Analytics` page under Settings. add_options_page( esc_html__( 'Plausible Analytics', 'plausible-analytics' ), esc_html__( 'Plausible Analytics', 'plausible-analytics' ), 'manage_options', 'plausible_analytics', [ $this, 'settings_page', ] ); } /** * Statistics Page via Embed feature. * @since 1.2.0 * @access public * @return void */ public function render_analytics_dashboard() { global $current_user; $settings = Helpers::get_settings(); $language_domain_key = $_GET['domain'] ?? 'default'; $analytics_enabled = $settings['enable_analytics_dashboard']; $shared_link = $settings['shared_link'][ $language_domain_key ] ?? $settings['shared_link']['default'] ?? ''; $self_hosted = ! empty( $settings ['self_hosted_domain'] ); if ( $self_hosted ) { $shared_link = $settings['self_hosted_shared_link']; } $has_access = false; $user_roles_have_access = ! empty( $settings['expand_dashboard_access'] ) ? array_merge( [ 'administrator' ], $settings['expand_dashboard_access'] ) : [ 'administrator' ]; foreach ( $current_user->roles as $role ) { if ( in_array( $role, $user_roles_have_access, true ) ) { $has_access = true; } } // Show error, if not having access. if ( ! $has_access ) : ?>

Shared Link under Self-Hosted Settings.', 'plausible-analytics' ), admin_url( 'options-general.php?page=plausible_analytics&tab=self-hosted' ) ); ?> click here to enable View Stats in WordPress.', 'plausible-analytics' ), admin_url( 'options-general.php?page=plausible_analytics#is_shared_link' ) ); ?>