' . esc_html__( 'Export orders', 'modulary-export' ) . '' ); return $links; } /** * Left side of WordPress's admin footer, on our page only: our own * thank-you line instead of "Thank you for creating with WordPress". * * @param mixed $text Footer text so far. * @return mixed */ public function footer_text( mixed $text ): mixed { if ( ! self::is_current_screen() ) { return $text; } return '' . sprintf( /* translators: %s: "Modulary", linked to the plugin's website. */ esc_html__( 'Thank you for exporting with %s.', 'modulary-export' ), 'Modulary' ) . ''; } /** * Whether the current admin screen is the builder. */ private static function is_current_screen(): bool { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return null !== $screen && 'woocommerce_page_' . self::SLUG === $screen->id; } /** * The builder, opened on a saved template (as if its chip was clicked). * For links in emails and notices; an unknown id opens the builder as usual. * * @param string $id Template id. */ public static function template_url( string $id ): string { return add_query_arg( [ 'page' => self::SLUG, self::TEMPLATE_ARG => rawurlencode( $id ), ], admin_url( 'admin.php' ) ); } /** * The template the builder was linked to, for the bootstrap data. * * Null when the page was opened normally, when the user can't use the * builder, or when a selection of orders is also in the URL (the * selection wins). Otherwise `id` is the template's id when it still * exists, null when it doesn't (`gone`, so the app can say so). * * @return array{id: string|null, gone: bool}|null */ public static function template_to_open(): ?array { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only: picks which saved template the builder shows first. if ( ! isset( $_GET[ self::TEMPLATE_ARG ] ) || isset( $_GET[ Selection::QUERY_ARG ] ) || ! current_user_can( self::CAPABILITY ) ) { return null; } // Template ids are UUIDs (Templates::create()), which sanitize_key() keeps as they are. $id = sanitize_key( wp_unslash( $_GET[ self::TEMPLATE_ARG ] ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended $template = '' !== $id ? Templates::get( $id ) : null; return [ 'id' => null === $template ? null : $template['id'], 'gone' => null === $template, ]; } /** * Mount point for the app. */ public function render(): void { echo '
'; } /** * Enqueue the built admin app on our page only. * * @param string $hook_suffix Current admin page hook. */ public function enqueue( string $hook_suffix ): void { if ( 'woocommerce_page_' . self::SLUG !== $hook_suffix ) { return; } $asset_file = MODULARY_EXPORT_PATH . 'build/index.asset.php'; if ( ! is_file( $asset_file ) ) { add_action( 'admin_notices', static function (): void { echo '

'; esc_html_e( 'Modulary Export assets are not built. Run `pnpm build` in the plugin directory.', 'modulary-export' ); echo '

'; } ); return; } $asset = require $asset_file; wp_enqueue_script( 'modulary-export', MODULARY_EXPORT_URL . 'build/index.js', $asset['dependencies'], $asset['version'], true ); // wp-scripts emits styles imported as `style.scss` to style-index.css. wp_enqueue_style( 'modulary-export', MODULARY_EXPORT_URL . 'build/style-index.css', [ 'wp-components' ], $asset['version'] ); wp_style_add_data( 'modulary-export', 'rtl', 'replace' ); // Translations come from translate.wordpress.org's language packs. wp_set_script_translations( 'modulary-export', 'modulary-export' ); wp_add_inline_script( 'modulary-export', 'window.modularyExport = ' . wp_json_encode( $this->bootstrap_data() ) . ';', 'before' ); /** * The builder's script and styles are enqueued. Add-ons enqueue their * own here, depending on `$handle`, so they load after the builder and * can fill its extension points (`modularyExport.*` JS filters, see the * README). * * @param string $handle Handle of the builder's script and style. */ do_action( 'modulary_export_enqueue_assets', 'modulary-export' ); } /** * Everything the app needs on first paint, so it renders without a request. * * @return array */ private function bootstrap_data(): array { return [ 'version' => MODULARY_EXPORT_VERSION, 'restBase' => 'modulary-export/v1', 'downloadUrl' => admin_url( 'admin-post.php' ), 'nonce' => wp_create_nonce( 'modulary_export_download' ), 'fields' => Fields::all_for_js(), 'presets' => Presets::all(), 'templates' => Templates::all(), 'statuses' => $this->statuses(), 'pro' => [ 'active' => Pro::active(), 'features' => Pro::features(), ], 'locale' => [ 'currency' => get_woocommerce_currency(), 'delimiter' => $this->suggested_delimiter(), // Store price format, for amounts the app writes itself (totals check). 'money' => [ 'decimals' => wc_get_price_decimals(), 'decimalSep' => wc_get_price_decimal_separator(), 'thousandSep' => wc_get_price_thousand_separator(), 'position' => (string) get_option( 'woocommerce_currency_pos', 'left' ), ], ], 'lastExport' => $this->last_export(), // Payment / shipping methods and categories for step 1's "Add filter". 'choices' => FilterOptions::all(), 'selection' => Selection::for_bootstrap(), 'ordersUrl' => Selection::orders_url(), // Opened from a link to a template (Pro's alerts, emails). 'openTemplate' => self::template_to_open(), // Step 3's one-time review request: { exports, active }. 'reviewPrompt' => ReviewPrompt::state( get_current_user_id() ), ]; } /** * The most recent download, re-sanitised, so "Repeat last export" is one click. * * @return array{time: int, rows: int, request: array}|null */ private function last_export(): ?array { $last = get_option( 'modulary_export_last_export' ); if ( ! is_array( $last ) || ! isset( $last['request'] ) || ! is_array( $last['request'] ) ) { return null; } return [ 'time' => (int) ( $last['time'] ?? 0 ), 'rows' => (int) ( $last['rows'] ?? 0 ), 'request' => Request::from_array( $last['request'] )->to_array(), ]; } /** * Order statuses as { key, label } without the "wc-" prefix. * * @return array */ private function statuses(): array { $out = []; foreach ( wc_get_order_statuses() as $key => $label ) { $out[] = [ 'key' => (string) preg_replace( '/^wc-/', '', (string) $key ), 'label' => (string) $label, ]; } return $out; } /** * Locales where Excel expects ";" as the CSV delimiter (decimal comma countries). */ private function suggested_delimiter(): string { $decimal = wc_get_price_decimal_separator(); return ',' === $decimal ? ';' : ','; } }