WP_REST_Server::READABLE, 'callback' => array( $this, 'countries' ), // Public because guests load the checkout country list; static reference data. 'permission_callback' => '__return_true', ), ) ); register_rest_route( self::API_NAMESPACE, '/catalog/currencies', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'currencies' ), // Public because it is static reference data, like the country list. 'permission_callback' => '__return_true', ), ) ); register_rest_route( self::API_NAMESPACE, '/catalog/pages', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'pages' ), // A page list leaks site structure, so it is gated like its pickers. 'permission_callback' => array( $this, 'settingsPermission' ), ), ) ); } /** * Return countries in name order. * * @since 1.0.0 * * @return WP_REST_Response The country list. */ public function countries(): WP_REST_Response { $out = array(); foreach ( Countries::all() as $code => $name ) { $out[] = array( 'code' => (string) $code, 'name' => (string) $name, ); } return new WP_REST_Response( $out, 200 ); } /** * Return currencies in label order. * * @since 1.0.0 * * @return WP_REST_Response The currency list. */ public function currencies(): WP_REST_Response { $out = array(); foreach ( Currencies::all() as $code => $entry ) { $out[] = array( 'code' => (string) $code, 'label' => (string) $entry['label'], 'symbol' => (string) $entry['symbol'], 'decimals' => (int) $entry['decimals'], ); } return new WP_REST_Response( $out, 200 ); } /** * Return published pages for settings pickers. * * @since 1.0.0 * * @return WP_REST_Response The page list. */ public function pages(): WP_REST_Response { $pages = get_pages(); if ( ! is_array( $pages ) ) { $pages = array(); } $out = array(); foreach ( $pages as $page ) { $title = trim( (string) $page->post_title ); $out[] = array( 'id' => (int) $page->ID, 'title' => '' !== $title ? $title : __( '(no title)', 'releasecart' ), ); } return new WP_REST_Response( $out, 200 ); } }