'storewhy', 'stwy_queued' => $queued ), admin_url( 'admin.php' ) ) ); exit; } add_action( 'admin_post_stwy_rebuild', 'stwy_handle_rebuild' ); /** * Saves settings. */ function stwy_handle_settings() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'storewhy' ) ); } check_admin_referer( 'stwy_settings' ); stwy_update_settings( array( 'track_views' => ! empty( $_POST['track_views'] ) ) ); wp_safe_redirect( add_query_arg( array( 'page' => 'storewhy', 'stwy_saved' => 1 ), admin_url( 'admin.php' ) ) ); exit; } add_action( 'admin_post_stwy_settings', 'stwy_handle_settings' ); /** * @param int $days Days back. * @return array[] Journal rows grouped by local 'Y-m-d'. */ function stwy_events_by_day( $days ) { if ( ! function_exists( 'cljournal_query' ) ) { return array(); } $from = wp_date( 'Y-m-d', time() - ( $days - 1 ) * DAY_IN_SECONDS ) . ' 00:00:00'; $rows = cljournal_query( array( 'after' => get_gmt_from_date( $from ), 'limit' => 500, ) ); $out = array(); foreach ( $rows as $row ) { $out[ get_date_from_gmt( $row['occurred_at'], 'Y-m-d' ) ][] = $row; } return $out; } /** * Renders the page. */ function stwy_render_admin_page() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $days = 30; $store = stwy_store_days( $days ); $events = stwy_events_by_day( $days ); $top = stwy_top_products( $days, 10 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only notice after a nonce-checked redirect. $queued = isset( $_GET['stwy_queued'] ) ? absint( $_GET['stwy_queued'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only notice after a nonce-checked redirect. $saved = ! empty( $_GET['stwy_saved'] ); $settings = stwy_get_settings(); $has_data = (bool) array_filter( $store, static function ( $row ) { return (int) $row['orders'] || (int) $row['views'] || (int) $row['add_to_cart']; } ); ?>