self::SLUG, 'title' => static function () { return __( 'CatalogRelay import', 'catalogrelay' ); }, 'capability' => 'manage_woocommerce', 'render' => array( self::class, 'render' ), ), ); } /** * Hooks (admin requests only). * * @return void */ public static function hook(): void { add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue' ) ); EntryPointRegistry::add( array( 'type' => 'admin_post', 'name' => self::DOWNLOAD, 'capability' => 'manage_woocommerce', 'nonce_action' => self::DOWNLOAD, 'callback' => array( self::class, 'download' ), ) ); } /** * Whether the current screen is this plugin's page. * * @return bool */ public static function isCurrent(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only: selects whether scripts are loaded. return isset( $_GET['page'] ) && self::SLUG === sanitize_key( wp_unslash( $_GET['page'] ) ); } /** * Page markup. * * @return void */ public static function render(): void { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'catalogrelay' ), '', array( 'response' => 403 ) ); } echo '

' . esc_html__( 'CatalogRelay Safe Importer', 'catalogrelay' ) . '


'; if ( ! Plugin::wooReady() ) { // The menu item comes from the shared menu; without WooCommerce the app and its REST routes are not loaded. echo '

' . esc_html__( 'CatalogRelay Safe Importer needs WooCommerce 10.0 or newer to be installed and active.', 'catalogrelay' ) . '

'; return; } if ( ! is_file( plugin_dir_path( Plugin::file() ) . 'build/index.js' ) ) { echo '

' . esc_html__( 'The admin app has not been built. Run "npm install && npm run build" in the plugin directory.', 'catalogrelay' ) . '

'; return; } echo '

' . esc_html__( 'Loading…', 'catalogrelay' ) . '

'; echo ''; } /** * Enqueue the built app. * * @return void */ public static function enqueue(): void { if ( ! self::isCurrent() || ! current_user_can( 'manage_woocommerce' ) ) { return; } $dir = plugin_dir_path( Plugin::file() ); $url = plugin_dir_url( Plugin::file() ); if ( ! is_file( $dir . 'build/index.js' ) ) { return; } $asset = is_file( $dir . 'build/index.asset.php' ) ? require $dir . 'build/index.asset.php' : array( 'dependencies' => array( 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n' ), 'version' => Plugin::VERSION, ); wp_enqueue_script( 'catalogrelay-app', $url . 'build/index.js', $asset['dependencies'], $asset['version'], true ); if ( is_file( $dir . 'build/style-index.css' ) ) { wp_enqueue_style( 'catalogrelay-app', $url . 'build/style-index.css', array( 'wp-components' ), $asset['version'] ); wp_style_add_data( 'catalogrelay-app', 'rtl', 'replace' ); // build/style-index-rtl.css for right-to-left languages. } wp_set_script_translations( 'catalogrelay-app', 'catalogrelay', $dir . 'languages' ); $data = array( 'restPath' => 'catalogrelay/v1', 'downloadUrl' => admin_url( 'admin-post.php' ), 'downloadAction' => self::DOWNLOAD, 'downloadNonce' => wp_create_nonce( self::DOWNLOAD ), 'decimal' => wc_get_price_decimal_separator(), 'thousand' => wc_get_price_thousand_separator(), 'locale' => str_replace( '_', '-', get_user_locale() ), 'timeZone' => wp_timezone_string(), 'maxUpload' => wp_max_upload_size(), 'incoming' => null !== IncomingDir::dir(), 'snapshotDays' => (int) \Rotosix\CatalogRelay\Vendor\Core\Settings\SettingsStore::get( Container::requireCurrent(), 'retention.snapshot_days', 7 ), 'settingsUrl' => admin_url( 'admin.php?page=' . Container::requireCurrent()->slug ), 'version' => Plugin::VERSION, ); wp_add_inline_script( 'catalogrelay-app', 'window.catalogrelayData = ' . wp_json_encode( $data ) . ';', 'before' ); } /** * CSV downloads: failed (rows), issues, diff, conflicts, sample. * * @return void */ public static function download(): void { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- the nonce is verified by EntryPointRegistry before this callback runs. $kind = isset( $_REQUEST['kind'] ) ? sanitize_key( wp_unslash( $_REQUEST['kind'] ) ) : ''; $id = isset( $_REQUEST['job_id'] ) ? absint( wp_unslash( $_REQUEST['job_id'] ) ) : 0; // phpcs:enable $d = Container::requireCurrent(); nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); if ( 'sample' === $kind ) { header( 'Content-Disposition: attachment; filename="catalogrelay-sample.csv"' ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen SampleCsv::write( $out, wc_get_price_decimal_separator() ); fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose exit; } $job = JobStore::find( $d, $id ); if ( ! $job || 0 !== strpos( $job->type, 'catalogrelay.' ) ) { wp_die( esc_html__( 'The job does not exist.', 'catalogrelay' ), '', array( 'response' => 404 ) ); } $p = JobParams::of( $job ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( 'catalogrelay-' . $kind . '-' . $id . '.csv' ) . '"' ); switch ( $kind ) { case 'failed': $root = $p->retryOf() ? $p->retryOf() : $job->id; if ( ! ReportWriter::sendSaved( $root, $p, $out ) ) { ReportWriter::failedRows( $root, $p, $out ); } break; case 'issues': ReportWriter::issues( $job->id, $out ); break; case 'diff': ReportWriter::diff( $job->id, $out ); break; case 'conflicts': ReportWriter::conflicts( $job->id, $out ); break; default: fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose wp_die( esc_html__( 'Unknown download.', 'catalogrelay' ), '', array( 'response' => 400 ) ); } fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose exit; } }