/** * @dataProvider make_absolute_url_testcases */ function test_make_absolute_url($relative_url, $absolute_url, $expected) { if (!is_callable(array('WP_Http', 'make_absolute_url'))) { $this->markTestSkipped("This version of WP_HTTP doesn't support WP_HTTP::make_absolute_url()"); return; } $actual = WP_Http::make_absolute_url($relative_url, $absolute_url); $this->assertEquals($expected, $actual); }
/** * Handles HTTP Redirects and follows them if appropriate. * * @since 3.7.0 * * @static * * @param string $url The URL which was requested. * @param array $args The Arguments which were used to make the request. * @param array $response The Response of the HTTP request. * @return false|object False if no redirect is present, a WP_HTTP or WP_Error result otherwise. */ public static function handle_redirects($url, $args, $response) { // If no redirects are present, or, redirects were not requested, perform no action. if (!isset($response['headers']['location']) || 0 === $args['_redirection']) { return false; } // Only perform redirections on redirection http codes. if ($response['response']['code'] > 399 || $response['response']['code'] < 300) { return false; } // Don't redirect if we've run out of redirects. if ($args['redirection']-- <= 0) { return new WP_Error('http_request_failed', __('Too many redirects.')); } $redirect_location = $response['headers']['location']; // If there were multiple Location headers, use the last header specified. if (is_array($redirect_location)) { $redirect_location = array_pop($redirect_location); } $redirect_location = WP_Http::make_absolute_url($redirect_location, $url); // POST requests should not POST to a redirected location. if ('POST' == $args['method']) { if (in_array($response['response']['code'], array(302, 303))) { $args['method'] = 'GET'; } } // Include valid cookies in the redirect process. if (!empty($response['cookies'])) { foreach ($response['cookies'] as $cookie) { if ($cookie->test($redirect_location)) { $args['cookies'][] = $cookie; } } } return wp_remote_request($redirect_location, $args); }
/** * Callback to add a base url to relative links in passed content. * * @since 2.7.0 * @access private * * @global string $_links_add_base * * @param string $m The matched link. * @return string The processed link. */ function _links_add_base($m) { global $_links_add_base; //1 = attribute name 2 = quotation mark 3 = URL return $m[1] . '=' . $m[2] . (preg_match('#^(\\w{1,20}):#', $m[3], $protocol) && in_array($protocol[1], wp_allowed_protocols()) ? $m[3] : WP_Http::make_absolute_url($m[3], $_links_add_base)) . $m[2]; }
function update_data() { remove_filter('pre_post_title', 'pre_post_title'); $upload_dir = wp_upload_dir(); $filename = $upload_dir['basedir'] . '/data.json'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; $items = array(); $args = array('numberposts' => -1, 'post_type' => 'business'); foreach (get_posts($args) as $post) { $custom_fields = get_post_custom($post->ID); list($address) = $custom_fields['address']; list($latlng) = $custom_fields['latlng']; list($phone) = $custom_fields['phone']; $item = array('label' => my_html_entity_decode(wptexturize($post->post_title)), 'permalink' => get_permalink($post)); $item += array_filter(compact('address', 'latlng', 'phone')); $website = esc_url_raw($custom_fields['website'][0]); if ($website) { $website = strtolower($website); $website = preg_replace('/^https?:\\/\\/(?:www\\.)?([-.\\d_a-z~]+(?:\\/[-.\\/\\d_a-z~]+)?)\\/*/', 'http://$1', $website); $args = array('timeout' => 300, 'redirection' => 0); $response = wp_safe_remote_get($website, $args); $args['redirection'] = 1; $args['reject_unsafe_urls'] = true; for ($i = 0; $i < 5; $i++) { if (is_wp_error($response)) { delete_post_meta($post->ID, 'website_up'); break; } $redirect_response = WP_Http::handle_redirects($website, $args, $response); if (!$redirect_response) { $website = preg_replace('/^(https?:\\/\\/[-.\\d_a-z~]+)\\/+$/i', '$1', $website); //update_post_meta($post->ID, 'website', $website); update_post_meta($post->ID, 'website_up', $website); $body = wp_remote_retrieve_body($response); $doc = new DOMDocument(); @$doc->loadHTML($body); $url_parts = my_parse_url($website); $urls = array(); foreach ($doc->getElementsByTagName('img') as $node) { $relative_url_parts = my_parse_url($node->getAttribute('src')); $relative_url_parts['path'] = str_replace(' ', '%20', $relative_url_parts['path']); $relative_url_parts['query'] = str_replace(' ', '+', $relative_url_parts['query']); if (!$relative_url_parts['scheme']) { $relative_url_parts['scheme'] = $url_parts['scheme']; if (!$relative_url_parts['host']) { $relative_url_parts['host'] = $url_parts['host']; $relative_url_parts['port'] = $url_parts['port']; if (!$relative_url_parts['path']) { $relative_url_parts['path'] = $url_parts['path']; if (!$relative_url_parts['query']) { $relative_url_parts['query'] = $url_parts['query']; } } else { if ($relative_url_parts['path'][0] != '/') { $relative_url_parts['path'] = "/{$relative_url_parts['path']}"; if ($url_parts['path']) { $relative_url_parts['path'] = substr($url_parts['path'], 0, strrpos($url_parts['path'], '/')) . $relative_url_parts['path']; } } } } } do { $relative_url_parts['path'] = preg_replace('/[^\\/]+\\/\\.\\.(?:\\/|$)/', '', $relative_url_parts['path'], 1, $count); } while ($count); $url = "{$relative_url_parts['scheme']}://{$relative_url_parts['host']}"; if ($relative_url_parts['port']) { $url .= ":{$relative_url_parts['port']}"; } $url .= $relative_url_parts['path']; if ($relative_url_parts['query']) { $url .= "?{$relative_url_parts['query']}"; } $urls[$url] = $url; } if ($urls) { $args = array('post_parent' => $post->ID, 'post_type' => 'attachment'); foreach (get_children($args) as $attachment) { wp_delete_attachment($attachment->ID); } $found = false; foreach ($urls as $url) { $tmp_name = download_url($url); list($width, $height) = getimagesize($tmp_name); if ($width > 40 && $height > 40 && $width + $height > 180) { preg_match('/[^\\/]+?(?=\\/?(?:$|\\?))/', $url, $matches); $file_array = array('name' => $matches[0], 'tmp_name' => $tmp_name); $thumbnail_id = media_handle_sideload($file_array, $post->ID); if (!$found) { set_post_thumbnail($post, $thumbnail_id); $found = true; } } } } break; } $website = WP_Http::make_absolute_url(wp_remote_retrieve_header($response, 'location'), $website); $response = $redirect_response; } } $value = wp_get_object_terms($post->ID, 'products', array('fields' => 'names')); if ($value) { $value = array_map('my_html_entity_decode', array_map('wptexturize', $value)); $item['products'] = count($value) == 1 ? $value[0] : $value; } $value = wp_get_object_terms($post->ID, 'farm-practices', array('fields' => 'names')); if ($value) { $value = array_map('my_html_entity_decode', array_map('wptexturize', $value)); $item['farm-practices'] = count($value) == 1 ? $value[0] : $value; } $value = wp_get_object_terms($post->ID, 'business-type', array('fields' => 'names')); if ($value) { $value = array_map('my_html_entity_decode', array_map('wptexturize', $value)); $item['business-type'] = count($value) == 1 ? $value[0] : $value; } $value = wp_get_object_terms($post->ID, 'available-at', array('fields' => 'names')); if ($value) { $value = array_map('my_html_entity_decode', array_map('wptexturize', $value)); $item['available-at'] = count($value) == 1 ? $value[0] : $value; } $thumbnail_id = get_post_thumbnail_id($post->ID); if ($thumbnail_id) { list($item['thumbnail-src'], $item['thumbnail-width'], $item['thumbnail-height']) = wp_get_attachment_image_src($thumbnail_id); } switch (true) { case has_term('Restaurant', 'business-type', $post): $item['icon'] = get_stylesheet_directory_uri() . '/maki/marker-36-1f77b4.png'; break; case has_term('Retailer', 'business-type', $post): $item['icon'] = get_stylesheet_directory_uri() . '/maki/marker-36-2ca02c.png'; break; default: $item['icon'] = get_stylesheet_directory_uri() . '/maki/marker-36-d62728.png'; } $items[] = $item; } $all_products = array(); foreach (get_terms('products') as $term) { $all_products[$term->term_id] = $term; } $all_in_season = get_option('in_season'); foreach ($all_products as $term) { $in_season = $all_in_season[$term->term_id]; if ($term->parent || $in_season) { $item = array('label' => my_html_entity_decode(wptexturize($term->name)), 'type' => 'product'); if ($term->parent) { $item['parent'] = my_html_entity_decode(wptexturize($all_products[$term->parent]->name)); } if ($in_season) { $item['in-season'] = format_in_season($in_season); } $items[] = $item; } } $data = array('items' => $items); file_put_contents($filename, wp_json_encode($data)); }