/** * Theme initialization. */ function b3_setup_theme() { // Setup theme: $b3 = new B3_Theme('b3', '0.1.0'); $b3->wp_api_check(); $b3->setup(); $b3->set_permalinks(new B3_Permalinks($b3)); $b3->set_scripts(new B3_Scripts($b3)); }
/** * Inject client application scripts. * * This action is called on `wp_enqueue_scripts` and injects required client * application settings from the backend, such as: * * - `root`: Theme root URI. * - `url`: RESTful WP API endpoint prefix. * - `name`: Site name. * - `nonce`: Nonce string. * * @todo Find a way to enqueue JS data without having to register a script URI. */ public function setup() { $site_url = parse_url(site_url()); $routes = array(); if (class_exists('B3_RoutesHelper')) { $routes_helper = new B3_RoutesHelper(); $routes = $routes_helper->get_routes(); } if (current_theme_supports('live-updates')) { wp_enqueue_script('heartbeat'); } $this->require_scripts(); $settings = array('name' => get_bloginfo('name'), 'api' => home_url(json_get_url_prefix()), 'nonce' => wp_create_nonce('wp_json'), 'api_url' => home_url(json_get_url_prefix()), 'site_path' => (string) isset($site_url['path']) ? $site_url['path'] : '', 'root_url' => get_stylesheet_directory_uri(), 'site_url' => site_url(), 'routes' => $routes, 'scripts' => $this->scripts); wp_register_script($this->theme->get_slug() . '-settings', 'settings.js', null, $this->theme->get_version()); wp_localize_script($this->theme->get_slug() . '-settings', 'WP_API_SETTINGS', $settings); wp_enqueue_script($this->theme->get_slug() . '-settings'); }