self::SLUG ), admin_url( 'admin.php' ) );
array_unshift( $links, '' . esc_html__( 'Settings', '360-orbit-nocookie-analytics' ) . '' );
return $links;
}
public static function register_menu() {
// Genau ein Eintrag im gemeinsamen "360 WP Orbit"-Untermenue -- Einstellungen
// usw. sind eigene Tabs auf dieser einen Seite, keine eigenen Menueeintraege.
self::$hook = add_submenu_page(
Wp360_NoCookie_Analytics_Orbit_Menu::SLUG,
'NoCookie Analytics',
'NoCookie Analytics',
self::CAPABILITY,
self::SLUG,
array( __CLASS__, 'render_page' )
);
add_action( 'load-' . self::$hook, array( __CLASS__, 'add_help_tabs' ) );
}
/**
* Einheitliches Hilfe-Panel (README "Suite-übergreifend"): WordPress'
* eigene "Hilfe"-Lasche oben rechts mit den drei wichtigsten Fragen,
* statt sie ungenutzt zu lassen. Reine Texte, keine Logik.
*/
public static function add_help_tabs() {
$screen = get_current_screen();
if ( ! $screen ) {
return;
}
$screen->add_help_tab(
array(
'id' => 'wp360-nca-help-basics',
'title' => __( 'What does NoCookie Analytics do?', '360-orbit-nocookie-analytics' ),
'content' => '
' . __( 'It recognizes visitors via a pseudonymous hash made from IP address, user agent and a salt — no raw IP, no cookie, no LocalStorage. Only the hash, the visited path, the post ID and the timestamp are stored.', '360-orbit-nocookie-analytics' ) . '
',
)
);
$screen->add_help_tab(
array(
'id' => 'wp360-nca-help-numbers',
'title' => __( 'Why do the numbers differ from Google Analytics?', '360-orbit-nocookie-analytics' ),
'content' => '
' . __( 'Bots are excluded by user-agent pattern, logged-in editors/admins are not counted, and the beacon reports directly to a REST endpoint — this bypasses page caches, but also misses views served purely from cache without any JavaScript running in the browser.', '360-orbit-nocookie-analytics' ) . '
',
)
);
$retention_help = __( 'Freely configurable in the Settings tab (0 = never delete), default around 13 months; older hits are cleaned up daily by cron. A salt reset additionally invalidates all previous visitor hashes, without deleting the stored hits themselves.', '360-orbit-nocookie-analytics' );
$screen->add_help_tab(
array(
'id' => 'wp360-nca-help-retention',
'title' => __( 'How long is the data kept?', '360-orbit-nocookie-analytics' ),
'content' => '
' . esc_html( $retention_help ) . '
',
)
);
}
/**
* Deaktivierungs-Rückfrage (README "Suite-übergreifend"): kein Formular,
* nur ein kurzer Hinweis vor dem eigentlichen Deaktivieren in der
* Plugin-Liste, was dabei aufhoert und was erhalten bleibt.
*/
public static function enqueue_deactivation_notice_script( $hook ) {
if ( 'plugins.php' !== $hook || ! defined( 'WP360_NCA_FILE' ) ) {
return;
}
$basename = plugin_basename( WP360_NCA_FILE );
$message = __( 'Deactivate NoCookie Analytics? Tracking new visitors/views stops immediately. Already stored statistics stay saved and come back if you reactivate the plugin.', '360-orbit-nocookie-analytics' );
// Kein eigenes Script-Tag im Markup (WordPress.org-Richtlinie): ein Handle
// ohne Datei, an das der Code per wp_add_inline_script() haengt.
wp_register_script( 'wp360-nca-deactivate', false, array(), WP360_NCA_VERSION, true );
wp_enqueue_script( 'wp360-nca-deactivate' );
wp_add_inline_script(
'wp360-nca-deactivate',
sprintf(
'( function () {
var row = document.querySelector( %s );
var link = row ? row.querySelector( \'a[href*="action=deactivate"]\' ) : null;
if ( ! link ) {
return;
}
link.addEventListener( \'click\', function ( event ) {
if ( ! window.confirm( %s ) ) {
event.preventDefault();
}
} );
} )();',
wp_json_encode( 'tr[data-plugin="' . $basename . '"]' ),
wp_json_encode( $message )
)
);
}
/**
* Gesundheitscheck (README "Suite-übergreifend"): traegt zur
* gemeinsamen "360 WP Orbit"-Uebersicht bei -- rein lesend, nichts
* wird hier ausgeloest. Bewusst am gemeinsamen, nicht praefigierten
* Filter 'wp360_orbit_health' registriert (siehe class-orbit-menu.php).
*/
public static function health_lines( array $lines, $slug ) {
if ( '360-orbit-nocookie-analytics' !== $slug ) {
return $lines;
}
if ( class_exists( 'Wp360_NoCookie_Analytics_Database' ) ) {
$count = Wp360_NoCookie_Analytics_Database::count_hits();
$lines[] = $count > 0
/* translators: %s: number of stored hits, already formatted */
? sprintf( __( '%s stored hits', '360-orbit-nocookie-analytics' ), number_format_i18n( $count ) )
: __( 'No hits recorded yet.', '360-orbit-nocookie-analytics' );
}
return $lines;
}
/**
* Alle Tabs dieser Variante (Slug => Beschriftung). Ein einziger
* Pro-Tab statt gesperrter Attrappen-Tabs (WordPress.org-Richtlinie 11:
* Upgrade-Hinweise sparsam und nur auf der eigenen Einstellungsseite).
*
* @return array
*/
private static function tabs() {
$tabs = array(
'stats' => __( 'Statistics', '360-orbit-nocookie-analytics' ),
'settings' => __( 'Settings', '360-orbit-nocookie-analytics' ),
);
$tabs['pro'] = __( 'NoCookie Analytics Pro', '360-orbit-nocookie-analytics' );
return $tabs;
}
private static function current_tab() {
$valid = array_keys( self::tabs() );
if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$tab = sanitize_key( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( in_array( $tab, $valid, true ) ) {
return $tab;
}
}
return 'stats';
}
private static function render_tabs( $current_tab ) {
$tabs = self::tabs();
?>
$label ) { ?>
rest_url( WP360_NCA_REST_NAMESPACE . '/stats' ),
'restNonce' => wp_create_nonce( 'wp_rest' ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'liveNonce' => wp_create_nonce( 'wp360_nca_nonce' ),
'strings' => array(
'loading' => __( 'Loading …', '360-orbit-nocookie-analytics' ),
'loadError' => __( 'Could not load data.', '360-orbit-nocookie-analytics' ),
'noData' => __( 'No data in this period.', '360-orbit-nocookie-analytics' ),
'visitors' => __( 'Visitors', '360-orbit-nocookie-analytics' ),
'views' => __( 'Views', '360-orbit-nocookie-analytics' ),
),
)
);
}
$settings_tabs = array( 'settings' );
if ( in_array( self::current_tab(), $settings_tabs, true ) ) {
wp_enqueue_script( 'wp360-nca-settings', WP360_NCA_URL . 'assets/admin.js', array( 'jquery' ), WP360_NCA_VERSION, true );
wp_localize_script(
'wp360-nca-settings',
'wp360NcaSettings',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wp360_nca_nonce' ),
'strings' => array(
'saved' => __( 'Saved.', '360-orbit-nocookie-analytics' ),
'saveError' => __( 'Error while saving.', '360-orbit-nocookie-analytics' ),
'resetSaltConfirm' => __( 'Really reset the salt? This invalidates all visitor hashes recorded so far.', '360-orbit-nocookie-analytics' ),
'saltReset' => __( 'Salt has been reset.', '360-orbit-nocookie-analytics' ),
'saltResetError' => __( 'Error while resetting.', '360-orbit-nocookie-analytics' ),
),
)
);
}
}
public static function register_stats_route() {
register_rest_route(
WP360_NCA_REST_NAMESPACE,
'/stats',
array(
'methods' => 'GET',
'callback' => array( __CLASS__, 'get_stats' ),
'permission_callback' => static fn () => current_user_can( self::CAPABILITY ),
'args' => array(
'end' => array(
'type' => 'string',
'required' => false,
),
'start' => array(
'type' => 'string',
'required' => false,
),
),
)
);
}
public static function get_stats( WP_REST_Request $request ) {
[$start, $end] = self::resolve_range( (string) $request->get_param( 'start' ), (string) $request->get_param( 'end' ) );
$start_sql = $start->format( 'Y-m-d 00:00:00' );
$end_sql = $end->modify( '+1 day' )->format( 'Y-m-d 00:00:00' );
$response = array(
'start' => $start->format( 'Y-m-d' ),
'end' => $end->format( 'Y-m-d' ),
'series' => Wp360_NoCookie_Analytics_Stats::daily_series( $start_sql, $end_sql ),
'totals' => Wp360_NoCookie_Analytics_Stats::totals( $start_sql, $end_sql ),
'top_pages' => Wp360_NoCookie_Analytics_Stats::top_pages( $start_sql, $end_sql ),
);
return new WP_REST_Response( $response );
}
/**
* Der Zeitraum der Statistik: von "start" bis "end" (beide als Y-m-d). Fehlt das Ende, gilt heute,
* fehlt der Anfang, die sieben Tage bis zum Ende. Vertauschte Angaben werden gedreht; ein Zeitraum
* ist hoechstens MAX_RANGE_DAYS lang (die Aufbewahrung ist ohnehin kuerzer).
*
* @param string $start Anfang oder leer.
* @param string $end Ende oder leer.
* @return DateTimeImmutable[] Anfang und Ende, jeweils 00:00 Uhr.
*/
private static function resolve_range( $start, $end ) {
$tz = wp_timezone();
$end_date = self::parse_date( $end, $tz );
if ( $end_date === null ) {
$end_date = new DateTimeImmutable( 'now', $tz );
}
$end_date = $end_date->setTime( 0, 0 );
$start_date = self::parse_date( $start, $tz );
$start_date = $start_date === null ? $end_date->modify( '-6 days' ) : $start_date->setTime( 0, 0 );
if ( $start_date > $end_date ) {
list( $start_date, $end_date ) = array( $end_date, $start_date );
}
$earliest = $end_date->modify( '-' . ( self::MAX_RANGE_DAYS - 1 ) . ' days' );
if ( $start_date < $earliest ) {
$start_date = $earliest;
}
return array( $start_date, $end_date );
}
private static function parse_date( $value, DateTimeZone $tz ) {
if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}$/', $value ) ) {
return null;
}
$date = DateTimeImmutable::createFromFormat( 'Y-m-d', $value, $tz );
return $date instanceof DateTimeImmutable ? $date : null;
}
public static function render_page() {
if ( ! current_user_can( self::CAPABILITY ) ) {
return;
}
$edition = 'Free';
$current_tab = self::current_tab();
?>
360 Orbit ()
-- das steckt hier im Flex-Branding-Block, die Meldung erschiene also neben der Ueberschrift statt darunter. ?>
*/
printf( esc_html__( 'CSV export of the time series is part of %s.', '360-orbit-nocookie-analytics' ), $product ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $product ist oben escaped.
?>