settings = $settings; $this->basename = $basename; } /** * Register hooks. * * @return void */ public function boot(): void { add_filter( 'plugin_action_links_' . $this->basename, array( $this, 'action_links' ) ); add_action( 'admin_notices', array( $this, 'setup_notice' ) ); add_action( 'admin_init', array( $this, 'maybe_dismiss' ) ); } /** * Add the Settings link to the plugin row. * * @param mixed $links Existing links. * @return array */ public function action_links( $links ): array { $links = is_array( $links ) ? $links : array(); $settings_link = sprintf( '%s', esc_url( admin_url( 'admin.php?page=' . SettingsController::PAGE ) ), esc_html__( 'Settings', 'adpix-analytics' ) ); array_unshift( $links, $settings_link ); return $links; } /** * Tell the site owner the plugin is not measuring anything yet. * * @return void */ public function setup_notice(): void { if ( ! current_user_can( SettingsController::CAPABILITY ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; // Shown where someone is already thinking about plugins or settings, not on top of every page // of the dashboard. if ( null === $screen || ! in_array( $screen->id, array( 'plugins', 'dashboard' ), true ) ) { return; } $settings = $this->settings->load(); if ( $settings->is_connected() || $settings->can_inject_tag() ) { return; } if ( get_user_meta( get_current_user_id(), self::DISMISSED, true ) ) { return; } $url = wp_nonce_url( add_query_arg( 'adpix-analytics-dismiss', '1', admin_url( 'plugins.php' ) ), 'adpix_analytics_dismiss' ); printf( '

%1$s %3$s   %5$s

', esc_html__( 'AdPix Analytics is active but not connected yet, so nothing is being measured.', 'adpix-analytics' ), esc_url( admin_url( 'admin.php?page=' . SettingsController::PAGE ) ), esc_html__( 'Connect it now', 'adpix-analytics' ), esc_url( $url ), esc_html__( 'Dismiss', 'adpix-analytics' ) ); } /** * Remember a dismissal. * * @return void */ public function maybe_dismiss(): void { if ( empty( $_GET['adpix-analytics-dismiss'] ) ) { return; } if ( ! current_user_can( SettingsController::CAPABILITY ) ) { return; } $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['_wpnonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'adpix_analytics_dismiss' ) ) { return; } update_user_meta( get_current_user_id(), self::DISMISSED, 1 ); wp_safe_redirect( admin_url( 'plugins.php' ) ); exit; } }