function load_scripts() { wp_enqueue_script('admin-app', JS_APP_URL . 'build/js/admin-app-scripts.js', array('jquery'), PLUGIN_VERSION, false); wp_enqueue_script('react-components', JS_APP_URL . 'build/js/all-components.js', array('admin-app'), PLUGIN_VERSION, true); wp_enqueue_style('admin-app-styles', JS_APP_URL . 'build/css/styles.css', array(), PLUGIN_VERSION, 'all'); wp_localize_script('admin-app', 'admin_app_local', array('api_url' => get_rest_url(), 'template_directory' => JS_APP_URL . 'templates', 'nonce' => wp_create_nonce('wp_rest'))); }
/** * Plugin Name: WP-API Client JS */ function json_api_client_js() { $scripts = wp_scripts(); $src = plugins_url('build/js/wp-api.js', __FILE__); if (isset($scripts->registered['wp-api'])) { $scripts->registered['wp-api']->src = $src; } else { wp_register_script('wp-api', $src, array('jquery', 'underscore', 'backbone'), '1.0', true); } /** * @var \WP_REST_Server $wp_rest_server */ global $wp_rest_server; if (empty($wp_rest_server)) { /** This filter is documented in wp-includes/rest-api.php */ $wp_rest_server_class = apply_filters('wp_rest_server_class', 'WP_REST_Server'); $wp_rest_server = new $wp_rest_server_class(); /** This filter is documented in wp-includes/rest-api.php */ do_action('rest_api_init', $wp_rest_server); } $schema_request = new WP_REST_Request('GET', '/wp/v2'); $schema_response = $wp_rest_server->dispatch($schema_request); $schema = null; if (!$schema_response->is_error()) { $schema = $schema_response->get_data(); } $settings = array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest'), 'versionString' => 'wp/v2/', 'schema' => $schema); wp_localize_script('wp-api', 'wpApiSettings', $settings); }
/** * Registers REST API JavaScript helpers. * * @since 4.4.0 * * @see wp_register_scripts() */ function rest_register_scripts() { // Use minified scripts if SCRIPT_DEBUG is not on. $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; wp_register_script('wp-api', plugins_url('wp-api' . $suffix . '.js', __FILE__), array('jquery', 'backbone', 'underscore'), '1.2', true); $settings = array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest'), 'versionString' => 'wp/v2/'); wp_localize_script('wp-api', 'wpApiSettings', $settings); }
/** * Register assets */ public final function register_scripts() { // JS Cookie wp_register_script('js-cookie', _2ch_plugin_dir_url('/dist/js/js.cookie.js'), array(), '2.1.0', true); // Google reCAPTCHA wp_register_script('recaptcha', 'https://www.google.com/recaptcha/api.js?render=explicit&onload=nichanCallback', array(), null, false); // Form helper wp_register_script('2ch-form', _2ch_plugin_dir_url('/dist/js/2ch.js'), array('jquery-form', 'js-cookie', 'recaptcha'), PLUGIN_2CH_VERSION, true); wp_localize_script('2ch-form', 'NichanVars', array('root' => get_rest_url(), 'nonce' => wp_create_nonce('wp_rest'), 'callback' => false, 'message' => __('Comment has been posted and waiting form moderation.', '2ch'))); }
/** * Send a Link header for the API. */ function rest_output_link_header() { if (headers_sent()) { return; } $api_root = get_rest_url(); if (empty($api_root)) { return; } header('Link: <' . esc_url_raw($api_root) . '>; rel="https://github.com/WP-API/WP-API"', false); }
/** * Enqueue scripts and styles - only called when builder is active. * @return void */ function enqueue() { if (class_exists('FLBuilderModel') && FLBuilderModel::is_builder_active()) { // Enqueue Stylesheets wp_enqueue_style('fl-template-browser', plugins_url('/css/template-browser.css', dirname(__FILE__)), array('open-sans')); // Enqueue Scripts wp_enqueue_script('fl-template-browser', plugins_url('/js/min/template-browser-min.js', dirname(__FILE__)), array('backbone', 'wp-util')); $data = array('collections' => BRJ_TemplateCollection::get_collections(), 'libraries' => array_values(BRJ_TemplateLibrary::get_libraries()), 'root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest')); wp_localize_script('fl-template-browser', 'BRJ_StoreInitialData', $data); } }
/** * Plugin Name: WP-API Client JS */ function json_api_client_js() { /** * Check if WP API functionality exists. Not using is_plugin_active in prepartion for */ if (!function_exists('rest_get_url_prefix')) { return; } wp_enqueue_script('wp-api', plugins_url('wp-api.js', __FILE__), array('jquery', 'underscore', 'backbone'), '1.0', true); $settings = array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest')); wp_localize_script('wp-api', 'WP_API_Settings', $settings); }
/** * Get menu locations. * * @since 1.2.0 * @param $request * @return array All registered menus locations */ public function get_items($request) { $rest_url = get_rest_url() . $this->namespace . '/' . $this->base; $locations = get_nav_menu_locations(); $registered_menus = get_registered_nav_menus(); $rest_menus = array(); if ($locations && $registered_menus) { foreach ($registered_menus as $slug => $label) { $rest_menus[] = array($slug => array('ID' => $locations[$slug], 'label' => $label, 'meta' => array('links' => array('collection' => $rest_url, 'self' => $rest_url . '/' . $slug)), 'tree' => $this->get_menu_tree($slug))); } } return $rest_menus; }
function fed_scripts() { wp_enqueue_style('wp-stripe-fed-styles', WP_STRIPE_URL . '/build/front-end/css/wp-stripe-fed-styles.css', array(), WP_STRIPE_VERSION, 'all'); wp_enqueue_script('wp-stripe-fed-scripts', WP_STRIPE_URL . '/build/front-end/js/stripe-wp-fed-scripts.js', array('jquery'), WP_STRIPE_VERSION, false); $local_object = array('api_url' => get_rest_url(), 'template_directory' => WP_STRIPE_URL . 'templates', 'nonce' => wp_create_nonce('wp_rest')); if (get_option('stripe_wp_confirmation_type', false) == 'page' && get_option('stripe_wp_confirmation_page', false)) { $local_object['confirmation'] = array('type' => 'page', 'page' => get_permalink(get_option('stripe_wp_confirmation_page', false))); } if (get_option('stripe_wp_confirmation_type', false) == 'message' && get_option('stripe_wp_confirmation_message', false)) { $local_object['confirmation'] = array('type' => 'message', 'message' => get_permalink(get_option('stripe_wp_confirmation_message', false))); } wp_localize_script('wp-stripe-fed-scripts', 'stripe_wp_local', $local_object); }
/** * Test that list endpoint returns expected format */ public function test_get_items() { $ev = Utils::create_test_event(); // Don't test internal events with this test $internal_events = array('a8c_cron_control_force_publish_missed_schedules', 'a8c_cron_control_confirm_scheduled_posts', 'a8c_cron_control_clean_legacy_data', 'a8c_cron_control_purge_completed_events'); foreach ($internal_events as $internal_event) { wp_clear_scheduled_hook($internal_event); } $request = new \WP_REST_Request('POST', '/' . \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_LIST); $request->set_body(wp_json_encode(array('secret' => \WP_CRON_CONTROL_SECRET))); $request->set_header('content-type', 'application/json'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertResponseStatus(200, $response); $this->assertArrayHasKey('events', $data); $this->assertArrayHasKey('endpoint', $data); $this->assertResponseData(array('events' => array(array('timestamp' => $ev['timestamp'], 'action' => md5($ev['action']), 'instance' => md5(maybe_serialize($ev['args'])))), 'endpoint' => get_rest_url(null, \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_RUN)), $response); }
/** * Print the JavaScript settings. */ public function print_settings() { ?> <script type="text/javascript"> window.wp = window.wp || {}; window.wp.react = window.wp.react || {}; window.wp.react.settings = { emoji_url: '<?php echo REACT_URL . '/static/emoji.json'; ?> ', endpoint: '<?php echo get_rest_url(null, $this->api->namespace . '/' . $this->api->rest_base); ?> ' } </script> <?php }
/** * List events pending for the current period */ public function get_events() { $events = get_option('cron'); // That was easy if (!is_array($events) || empty($events)) { return array('events' => null); } // Simplify array format for further processing $events = collapse_events_array($events); // Select only those events to run in the next sixty seconds // Will include missed events as well $current_events = $internal_events = array(); $current_window = strtotime(sprintf('+%d seconds', JOB_QUEUE_WINDOW_IN_SECONDS)); foreach ($events as $event) { // Skip events whose time hasn't come if ($event['timestamp'] > $current_window) { continue; } // Skip events that don't have any callbacks hooked to their actions, unless their execution is requested if (!$this->action_has_callback_or_should_run_anyway($event)) { continue; } // Necessary data to identify an individual event // `$event['action']` is hashed to avoid information disclosure // Core hashes `$event['instance']` for us $event_data_public = array('timestamp' => $event['timestamp'], 'action' => md5($event['action']), 'instance' => $event['instance']); // Queue internal events separately to avoid them being blocked if (is_internal_event($event['action'])) { $internal_events[] = $event_data_public; } else { $current_events[] = $event_data_public; } } // Limit batch size to avoid resource exhaustion if (count($current_events) > JOB_QUEUE_SIZE) { $current_events = $this->reduce_queue($current_events); } // Combine with Internal Events and return necessary data to process the event queue return array('events' => array_merge($current_events, $internal_events), 'endpoint' => get_rest_url(null, REST_API::API_NAMESPACE . '/' . REST_API::ENDPOINT_RUN)); }
function wp_enqueue_scripts() { global $post; $rest_server = rest_get_server(); if ($this->has_fee()) { wp_enqueue_style('wp-core-ui', $this->url('/css/wp-core-ui.css'), false, self::VERSION, 'screen'); wp_enqueue_style('wp-core-ui-colors', $this->url('/css/wp-core-ui-colors.css'), false, self::VERSION, 'screen'); wp_enqueue_style('wp-auth-check'); wp_enqueue_script('wp-auth-check'); wp_enqueue_script('fee-tinymce', $this->url('/vendor/tinymce.js'), array(), self::TINYMCE_VERSION, true); wp_enqueue_script('fee-tinymce-image', $this->url('/js/tinymce.image.js'), array('fee-tinymce'), self::VERSION, true); wp_enqueue_script('fee-tinymce-theme', $this->url('/js/tinymce.theme.js'), array('fee-tinymce'), self::VERSION, true); foreach (array('lists', 'paste', 'wordpress', 'wplink', 'wptextpattern', 'wpview') as $plugin) { wp_enqueue_script('fee-' . $plugin, $this->url('/vendor/' . $plugin . '.js'), array('fee-tinymce'), self::VERSION, true); } $tinymce_plugins = array('wordpress', 'feeImage', 'wptextpattern', 'wplink', 'wpview', 'paste', 'lists'); $tinymce_toolbar = array('bold', 'italic', 'strikethrough', 'link'); $tinymce = array('selector' => '.fee-content', 'plugins' => implode(' ', array_unique(apply_filters('fee_tinymce_plugins', $tinymce_plugins))), 'toolbar' => apply_filters('fee_tinymce_toolbar', $tinymce_toolbar), 'theme' => 'fee', 'inline' => true, 'relative_urls' => false, 'convert_urls' => false, 'browser_spellcheck' => true, 'placeholder' => apply_filters('fee_content_placeholder', __('Just write…')), 'wpeditimage_html5_captions' => current_theme_supports('html5', 'caption'), 'end_container_on_empty_block' => true); $request = new WP_REST_Request('GET', '/wp/v2/' . ($post->post_type === 'page' ? 'pages' : 'posts') . '/' . $post->ID); $request->set_query_params(array('context' => 'edit')); $result = $rest_server->dispatch($request); wp_enqueue_script('fee', $this->url('/js/fee.js'), array('fee-tinymce', 'wp-util', 'heartbeat', 'editor', 'wp-api', 'media-views'), self::VERSION, true); wp_localize_script('fee', 'feeData', array('tinymce' => apply_filters('fee_tinymce_config', $tinymce), 'post' => $result->get_data(), 'lock' => !wp_check_post_lock($post->ID) ? implode(':', wp_set_post_lock($post->ID)) : false, 'titlePlaceholder' => apply_filters('enter_title_here', __('Enter title here'), $post), 'editURL' => get_edit_post_link())); $request = new WP_REST_Request('GET', '/wp/v2'); $result = $rest_server->dispatch($request); wp_localize_script('wp-api', 'wpApiSettings', array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest'), 'versionString' => 'wp/v2/', 'schema' => $result->get_data(), 'cacheSchema' => true)); wp_enqueue_media(array('post' => $post)); wp_deregister_script('mce-view'); wp_enqueue_script('mce-view', $this->url('/vendor/mce-view.js'), array('shortcode', 'jquery', 'media-views', 'media-audiovideo'), self::VERSION, true); wp_enqueue_script('mce-view-register', $this->url('/js/mce-view-register.js'), array('mce-view', 'fee'), self::VERSION, true); wp_localize_script('mce-view-register', 'mce_view_register', array('post_id' => $post->ID)); wp_enqueue_style('tinymce-core', $this->url('/css/tinymce.core.css'), false, self::VERSION, 'screen'); wp_enqueue_style('tinymce-view', $this->url('/css/tinymce.view.css'), false, self::VERSION, 'screen'); wp_enqueue_style('fee', $this->url('/css/fee.css'), false, self::VERSION, 'screen'); wp_enqueue_style('dashicons'); } if (current_user_can('edit_posts')) { if (is_singular()) { require_once ABSPATH . '/wp-admin/includes/post.php'; $user_id = wp_check_post_lock($post->ID); $user = get_userdata($user_id); } wp_enqueue_script('fee-adminbar', $this->url('/js/fee-adminbar.js'), array('wp-util', 'wp-api'), self::VERSION, true); wp_localize_script('fee-adminbar', 'fee_adminbar', array('lock' => is_singular() && $user_id ? $user->display_name : false, 'supportedPostTypes' => $this->get_supported_post_types(), 'postNew' => admin_url('post-new.php'), 'nonce' => wp_create_nonce('fee-new'))); } }
/** * Boot script for Customizer preview. * * @action wp_head */ public function boot_preview_script() { global $wp_customize; wp_print_scripts(array('customize-rest-resources-preview-manager')); $dirty_setting_values = array(); foreach (array_keys($wp_customize->unsanitized_post_values()) as $setting_id) { if (!preg_match('#^rest_resource\\[#', $setting_id)) { continue; } $setting = $wp_customize->get_setting($setting_id); if ($setting) { $dirty_setting_values[$setting_id] = $setting->value(); } } $args = array('previewedTheme' => $wp_customize->get_stylesheet(), 'previewNonce' => wp_create_nonce('preview-customize_' . $wp_customize->get_stylesheet()), 'restApiRoot' => get_rest_url(), 'initialDirtySettingValues' => $dirty_setting_values); ?> <script> /* global CustomizeRestResources */ CustomizeRestResources.manager = new CustomizeRestResources.RestResourcesPreviewManager( <?php echo wp_json_encode($args); ?> ); </script> <?php }
/** * Enqueue scripts and styles. */ function anadama_scripts() { wp_enqueue_style('anadama-style', get_stylesheet_uri()); wp_enqueue_script('anadama-react', get_template_directory_uri() . '/js/app.js', array('jquery'), ANADAMA_VERSION, true); wp_localize_script('anadama-react', 'AnadamaSettings', array('nonce' => wp_create_nonce('wp_rest'), 'user' => get_current_user_id(), 'title' => get_bloginfo('name', 'display'), 'URL' => array('root' => esc_url_raw(get_rest_url(null, '/wp/v2')), 'menuRoot' => esc_url_raw(get_rest_url(null, '/wp-api-menus/v2')), 'base' => esc_url_raw(home_url())))); }
/** * Enqueue scripts and styles. */ function foxhound_scripts() { wp_enqueue_style('foxhound-style', get_template_directory_uri() . '/build/style.css'); wp_enqueue_script(FOXHOUND_APP, get_template_directory_uri() . '/build/app.js', array('jquery'), FOXHOUND_VERSION, true); if (class_exists('Jetpack_Tiled_Gallery')) { Jetpack_Tiled_Gallery::default_scripts_and_styles(); } $url = trailingslashit(home_url()); $path = trailingslashit(parse_url($url, PHP_URL_PATH)); $front_page_slug = false; $blog_page_slug = false; if ('posts' !== get_option('show_on_front')) { $front_page_id = get_option('page_on_front'); $front_page = get_post($front_page_id); if ($front_page->post_name) { $front_page_slug = $front_page->post_name; } $blog_page_id = get_option('page_for_posts'); $blog_page = get_post($blog_page_id); if ($blog_page->post_name) { $blog_page_slug = $blog_page->post_name; } } wp_localize_script(FOXHOUND_APP, 'SiteSettings', array('endpoint' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest'))); $user_id = get_current_user_id(); $user = get_userdata($user_id); wp_localize_script(FOXHOUND_APP, 'FoxhoundSettings', array('user' => get_current_user_id(), 'userDisplay' => $user ? $user->display_name : '', 'frontPage' => array('page' => $front_page_slug, 'blog' => $blog_page_slug), 'URL' => array('base' => esc_url_raw($url), 'path' => $path), 'meta' => array('title' => get_bloginfo('name', 'display'), 'description' => get_bloginfo('description', 'display')))); }
/** * generates the host-meta base array (and activate filter) * * @param array $host_meta * @return array */ public static function generate_default_content($host_meta) { $host_meta = array(); // add subject $host_meta['subject'] = site_url(); // add feeds $host_meta['links'] = array(array('rel' => 'alternate', 'href' => get_bloginfo('atom_url'), 'type' => 'application/atom+xml'), array('rel' => 'alternate', 'href' => get_bloginfo('rss2_url'), 'type' => 'application/rss+xml'), array('rel' => 'alternate', 'href' => get_bloginfo('rdf_url'), 'type' => 'application/rdf+xml')); // RSD discovery link $host_meta['links'][] = array('rel' => 'EditURI', 'href' => esc_url(site_url('xmlrpc.php?rsd', 'rpc')), 'type' => 'application/rsd+xml'); // add WordPress API if (function_exists('get_rest_url')) { $host_meta['links'][] = array('rel' => 'https://api.w.org/', 'href' => esc_url(get_rest_url())); } return $host_meta; }
/** * Get URL to a REST endpoint. * * @param string $path Optional. REST route. Default empty. * @param string $scheme Optional. Sanitization scheme. Default 'json'. * @return string Full URL to the endpoint. */ function rest_url($path = '', $scheme = 'json') { return get_rest_url(null, $path, $scheme); }
public function lss_widget_output($atts, $content = "") { $s = isset($_POST['LSSW_s']) ? $_POST['LSSW_s'] : NULL; $query = isset($_POST['LSSW_field_query']) ? $_POST['LSSW_field_query'] : NULL; wp_enqueue_script($this->plugin_name); wp_localize_script($this->plugin_name, 'LSSW_data', array('base' => get_rest_url(null, '/lsswidget/v1/'), 'dashboard' => Lss_Tools_Option::get_option('url'), 'search' => 'search/', 'signup' => 'signup/', 'keyword' => Lss_Tools_Option::get_option('keyword') ?: true, 'contact' => Lss_Tools_Option::get_option('contact') ?: true)); ob_start(); include plugin_dir_path(__FILE__) . 'partials/lss-tools-public-display.php'; return ob_get_clean(); }
/** * Localizes the bootstrap, enqueues the script and styles for the help page */ public function page() { $this->help_sections = array(); $this->add_fieldset('health', _x('Health', 'This section displays the overall health of Connect for WooCommerce and the things it depends on', 'connectforwoocommerce'), $this->get_health_items()); $this->add_fieldset('services', __('Services', 'connectforwoocommerce'), $this->get_services_items()); $this->add_fieldset('debug', __('Debug', 'connectforwoocommerce'), $this->get_debug_items()); $this->add_fieldset('support', __('Support', 'connectforwoocommerce'), $this->get_support_items()); $root_view = 'wc-connect-admin-help'; $admin_array = array('storeOptions' => $this->service_settings_store->get_store_options(), 'formSchema' => $this->get_form_schema(), 'formLayout' => $this->get_form_layout(), 'formData' => $this->get_form_data(), 'predefinedPackages' => array(), 'callbackURL' => get_rest_url(null, "/wc/v1/connect/self-help"), 'nonce' => wp_create_nonce('wp_rest'), 'rootView' => $root_view); wp_localize_script('wc_connect_admin', 'wcConnectData', $admin_array); wp_enqueue_script('wc_connect_admin'); wp_enqueue_style('wc_connect_admin'); ?> <h2> <?php _e('Connect for WooCommerce Status', 'woocommerce'); ?> </h2> <div class="wc-connect-admin-container" id="<?php echo esc_attr($root_view); ?> "></div> <?php }
/** * This function is added to the wc_connect_service_admin_options action by this class * (see attach_hooks) and then that action is fired by WC_Connect_Shipping_Method::admin_options * to get the service instance form layout and settings bundled inside wcConnectData * as the form container is emitted into the body's HTML */ public function localize_and_enqueue_service_script($id, $instance = false) { if (!function_exists('get_rest_url')) { return; } $settings_store = $this->get_service_settings_store(); $schemas_store = $this->get_service_schemas_store(); $service_schema = $schemas_store->get_service_schema_by_id_or_instance_id($instance ? $instance : $id); if (!$service_schema) { return; } $path = $instance ? "/wc/v1/connect/services/{$id}/{$instance}" : "/wc/v1/connect/services/{$id}"; $admin_array = array('storeOptions' => $settings_store->get_store_options(), 'formSchema' => $service_schema->service_settings, 'formLayout' => $service_schema->form_layout, 'formData' => $settings_store->get_service_settings($id, $instance), 'predefinedPackages' => $settings_store->get_predefined_packages_for_service($id), 'callbackURL' => get_rest_url(null, $path), 'nonce' => wp_create_nonce('wp_rest'), 'rootView' => 'wc-connect-service-settings'); wp_localize_script('wc_connect_admin', 'wcConnectData', $admin_array); wp_enqueue_script('wc_connect_admin'); wp_enqueue_style('wc_connect_admin'); }
public function enqueue_scripts() { wp_enqueue_script('easy-customer-invoices-admin', App::get_url('assets/admin.min.js'), array('jquery', 'wp-util'), App::get_info('version'), true); wp_localize_script('easy-customer-invoices-admin', 'wpeci_settings', array('api_root' => get_rest_url(), 'api_nonce' => wp_create_nonce('wp_rest'), 'ajax_nonce' => wp_create_nonce('eci_ajax'), 'currency' => Util::get_base_currency())); }
/** * @ticket 34299 */ public function test_rest_url_scheme() { $_SERVER['SERVER_NAME'] = parse_url(home_url(), PHP_URL_HOST); $_siteurl = get_option('siteurl'); set_current_screen('edit.php'); $this->assertTrue(is_admin()); // Test an HTTP URL unset($_SERVER['HTTPS']); $url = get_rest_url(); $this->assertSame('http', parse_url($url, PHP_URL_SCHEME)); // Test an HTTPS URL $_SERVER['HTTPS'] = 'on'; $url = get_rest_url(); $this->assertSame('https', parse_url($url, PHP_URL_SCHEME)); // Switch to an admin request on a different domain name $_SERVER['SERVER_NAME'] = 'admin.example.org'; update_option('siteurl', 'http://admin.example.org'); $this->assertNotEquals($_SERVER['SERVER_NAME'], parse_url(home_url(), PHP_URL_HOST)); // // Test an HTTP URL unset($_SERVER['HTTPS']); $url = get_rest_url(); $this->assertSame('http', parse_url($url, PHP_URL_SCHEME)); // // Test an HTTPS URL $_SERVER['HTTPS'] = 'on'; $url = get_rest_url(); $this->assertSame('http', parse_url($url, PHP_URL_SCHEME)); // Reset update_option('siteurl', $_siteurl); set_current_screen('front'); }
public function meta_box($post) { $order = wc_get_order($post); $debug_page_uri = esc_url(add_query_arg(array('page' => 'wc-status', 'tab' => 'connect'), admin_url('admin.php'))); $store_options = $this->settings_store->get_store_options(); $root_view = 'wc-connect-create-shipping-label'; $admin_array = array('purchaseURL' => get_rest_url(null, '/wc/v1/connect/label/purchase'), 'addressNormalizationURL' => get_rest_url(null, '/wc/v1/connect/normalize-address'), 'getRatesURL' => get_rest_url(null, '/wc/v1/connect/shipping-rates'), 'labelStatusURL' => get_rest_url(null, '/wc/v1/connect/label/' . $order->id . '-%d'), 'labelRefundURL' => get_rest_url(null, '/wc/v1/connect/label/' . $order->id . '-%d/refund'), 'labelsPreviewURL' => get_rest_url(null, '/wc/v1/connect/labels/preview'), 'labelsPrintURL' => get_rest_url(null, '/wc/v1/connect/labels/print'), 'paperSize' => $this->get_paper_size(), 'nonce' => wp_create_nonce('wp_rest'), 'rootView' => $root_view); $labels_data = get_post_meta($order->id, 'wc_connect_labels', true); if ($labels_data) { $admin_array['labelsData'] = json_decode($labels_data, true, WOOCOMMERCE_CONNECT_MAX_JSON_DECODE_DEPTH); } else { $store_options['countriesData'] = $this->get_states_map(); $admin_array['formData'] = $this->get_form_data($order); } $admin_array['storeOptions'] = $store_options; wp_localize_script('wc_connect_admin', 'wcConnectData', $admin_array); wp_enqueue_script('wc_connect_admin'); wp_enqueue_style('wc_connect_admin'); ?> <div class="wc-connect-admin-container" id="<?php echo esc_attr($root_view); ?> "> <span class="form-troubles" style="opacity: 0"> <?php printf(__('Shipping labels not loading? Visit the <a href="%s">status page</a> for troubleshooting steps.', 'connectforwoocommerce'), $debug_page_uri); ?> </span> </div> <?php }
/** * Get menu locations. * * @since 1.2.0 * @param $request * @return array All registered menus locations */ public static function get_menu_locations($request) { $locations = get_nav_menu_locations(); $registered_menus = get_registered_nav_menus(); $rest_url = get_rest_url() . self::get_api_namespace() . '/menu-locations/'; $rest_menus = array(); if ($locations && $registered_menus) { foreach ($registered_menus as $slug => $label) { // Sanity check if (!isset($locations[$slug])) { continue; } $rest_menus[$slug]['ID'] = $locations[$slug]; $rest_menus[$slug]['label'] = $label; $rest_menus[$slug]['meta']['links']['collection'] = $rest_url; $rest_menus[$slug]['meta']['links']['self'] = $rest_url . $slug; } } return $rest_menus; }
/** * Registers REST API JavaScript helpers. * * @since 4.4.0 * * @see wp_register_scripts() */ function rest_register_scripts() { wp_register_script('wp-api', plugins_url('wp-api.js', __FILE__), array('jquery', 'backbone', 'underscore'), '1.1', true); $settings = array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest')); wp_localize_script('wp-api', 'WP_API_Settings', $settings); }
/** * Handles serving an API request. * * Matches the current server URI to a route and runs the first matching * callback then outputs a JSON representation of the returned value. * * @since 4.4.0 * @access public * * @see WP_REST_Server::dispatch() * * @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used. * Default null. * @return false|null Null if not served and a HEAD request, false otherwise. */ public function serve_request($path = null) { $content_type = isset($_GET['_jsonp']) ? 'application/javascript' : 'application/json'; $this->send_header('Content-Type', $content_type . '; charset=' . get_option('blog_charset')); $this->send_header('X-Robots-Tag', 'noindex'); $api_root = get_rest_url(); if (!empty($api_root)) { $this->send_header('Link', '<' . esc_url_raw($api_root) . '>; rel="https://api.w.org/"'); } /* * Mitigate possible JSONP Flash attacks. * * https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ */ $this->send_header('X-Content-Type-Options', 'nosniff'); $this->send_header('Access-Control-Expose-Headers', 'X-WP-Total, X-WP-TotalPages'); $this->send_header('Access-Control-Allow-Headers', 'Authorization'); /** * Send nocache headers on authenticated requests. * * @since 4.4.0 * * @param bool $rest_send_nocache_headers Whether to send no-cache headers. */ $send_no_cache_headers = apply_filters('rest_send_nocache_headers', is_user_logged_in()); if ($send_no_cache_headers) { foreach (wp_get_nocache_headers() as $header => $header_value) { $this->send_header($header, $header_value); } } /** * Filters whether the REST API is enabled. * * @since 4.4.0 * * @param bool $rest_enabled Whether the REST API is enabled. Default true. */ $enabled = apply_filters('rest_enabled', true); /** * Filters whether jsonp is enabled. * * @since 4.4.0 * * @param bool $jsonp_enabled Whether jsonp is enabled. Default true. */ $jsonp_enabled = apply_filters('rest_jsonp_enabled', true); $jsonp_callback = null; if (!$enabled) { echo $this->json_error('rest_disabled', __('The REST API is disabled on this site.'), 404); return false; } if (isset($_GET['_jsonp'])) { if (!$jsonp_enabled) { echo $this->json_error('rest_callback_disabled', __('JSONP support is disabled on this site.'), 400); return false; } $jsonp_callback = $_GET['_jsonp']; if (!wp_check_jsonp_callback($jsonp_callback)) { echo $this->json_error('rest_callback_invalid', __('The JSONP callback function is invalid.'), 400); return false; } } if (empty($path)) { if (isset($_SERVER['PATH_INFO'])) { $path = $_SERVER['PATH_INFO']; } else { $path = '/'; } } $request = new WP_REST_Request($_SERVER['REQUEST_METHOD'], $path); $request->set_query_params(wp_unslash($_GET)); $request->set_body_params(wp_unslash($_POST)); $request->set_file_params($_FILES); $request->set_headers($this->get_headers(wp_unslash($_SERVER))); $request->set_body($this->get_raw_data()); /* * HTTP method override for clients that can't use PUT/PATCH/DELETE. First, we check * $_GET['_method']. If that is not set, we check for the HTTP_X_HTTP_METHOD_OVERRIDE * header. */ if (isset($_GET['_method'])) { $request->set_method($_GET['_method']); } elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { $request->set_method($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']); } $result = $this->check_authentication(); if (!is_wp_error($result)) { $result = $this->dispatch($request); } // Normalize to either WP_Error or WP_REST_Response... $result = rest_ensure_response($result); // ...then convert WP_Error across. if (is_wp_error($result)) { $result = $this->error_to_response($result); } /** * Filters the API response. * * Allows modification of the response before returning. * * @since 4.4.0 * @since 4.5.0 Applied to embedded responses. * * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Server $this Server instance. * @param WP_REST_Request $request Request used to generate the response. */ $result = apply_filters('rest_post_dispatch', rest_ensure_response($result), $this, $request); // Wrap the response in an envelope if asked for. if (isset($_GET['_envelope'])) { $result = $this->envelope_response($result, isset($_GET['_embed'])); } // Send extra data from response objects. $headers = $result->get_headers(); $this->send_headers($headers); $code = $result->get_status(); $this->set_status($code); /** * Filters whether the request has already been served. * * Allow sending the request manually - by returning true, the API result * will not be sent to the client. * * @since 4.4.0 * * @param bool $served Whether the request has already been served. * Default false. * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $this Server instance. */ $served = apply_filters('rest_pre_serve_request', false, $result, $request, $this); if (!$served) { if ('HEAD' === $request->get_method()) { return null; } // Embed links inside the request. $result = $this->response_to_data($result, isset($_GET['_embed'])); $result = wp_json_encode($result); $json_error_message = $this->get_json_last_error(); if ($json_error_message) { $json_error_obj = new WP_Error('rest_encode_error', $json_error_message, array('status' => 500)); $result = $this->error_to_response($json_error_obj); $result = wp_json_encode($result->data[0]); } if ($jsonp_callback) { // Prepend '/**/' to mitigate possible JSONP Flash attacks // https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/ echo '/**/' . $jsonp_callback . '(' . $result . ')'; } else { echo $result; } } return null; }
/** * The get_rest_url function should return a URL consistently terminated with a "/", * whether the blog is configured with pretty permalink support or not. */ public function test_rest_url_generation() { // In pretty permalinks case, we expect a path of wp-json/ with no query. update_option('permalink_structure', '/%year%/%monthnum%/%day%/%postname%/'); $this->assertEquals('http://' . WP_TESTS_DOMAIN . '/wp-json/', get_rest_url()); update_option('permalink_structure', ''); // In non-pretty case, we get a query string to invoke the rest router. $this->assertEquals('http://' . WP_TESTS_DOMAIN . '/?rest_route=/', get_rest_url()); }
public function test_link_header_on_requests() { $api_root = get_rest_url(); $request = new WP_REST_Request('GET', '/', array()); $result = $this->server->serve_request('/'); $headers = $this->server->sent_headers; $this->assertEquals('<' . esc_url_raw($api_root) . '>; rel="https://api.w.org/"', $headers['Link']); }
departure_date : jQuery('#Mydate').val(), fullname : jQuery('#fullname').val(), email : jQuery('#email').val(), phone : jQuery('#phone').val(), address : jQuery('#address').val(), payment_status : 'Pending' }; var token = '<?php echo base64_encode('admin' . ':' . 'cIhX xib0 Irhh ls6N'); ?> '; var req = { url: '<?php echo esc_url_raw(get_rest_url()); ?> ' + 'wp/v2/' + 'book', type: 'POST', data: { title: '', status: 'publish', fields: data }, headers: { <?php if (is_user_logged_in()) { ?> 'X-WP-Nonce' : '<?php echo wp_create_nonce('wp_rest'); ?>