/** * Pings back the links found in a post. * * @since 0.0.1 * * @global string $hq_version * * @param string $content Post content to check for links. * @param int $post_ID Post ID. */ function pingback($content, $post_ID) { global $hq_version; include_once ABSPATH . HQINC . '/class-IXR.php'; include_once ABSPATH . HQINC . '/class-hq-http-ixr-client.php'; // original code by Mort (http://mort.mine.nu:8080) $post_links = array(); $pung = get_pung($post_ID); // Step 1 // Parsing the post, external links (if any) are stored in the $post_links array $post_links_temp = hq_extract_urls($content); // Step 2. // Walking thru the links array // first we get rid of links pointing to sites, not to specific files // Example: // http://dummy-weblog.org // http://dummy-weblog.org/ // http://dummy-weblog.org/post.php // We don't wanna ping first and second types, even if they have a valid <link/> foreach ((array) $post_links_temp as $link_test) { if (!in_array($link_test, $pung) && url_to_postid($link_test) != $post_ID && !is_local_attachment($link_test)) { // Also, let's never ping local attachments. if ($test = @parse_url($link_test)) { if (isset($test['query'])) { $post_links[] = $link_test; } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') { $post_links[] = $link_test; } } } } $post_links = array_unique($post_links); /** * Fires just before pinging back links found in a post. * * @since 0.0.1 * * @param array &$post_links An array of post links to be checked, passed by reference. * @param array &$pung Whether a link has already been pinged, passed by reference. * @param int $post_ID The post ID. */ do_action_ref_array('pre_ping', array(&$post_links, &$pung, $post_ID)); foreach ((array) $post_links as $pagelinkedto) { $pingback_server_url = discover_pingback_server_uri($pagelinkedto); if ($pingback_server_url) { @set_time_limit(60); // Now, the RPC call $pagelinkedfrom = get_permalink($post_ID); // using a timeout of 3 seconds should be enough to cover slow servers $client = new HQ_HTTP_IXR_Client($pingback_server_url); $client->timeout = 3; /** * Filter the user agent sent when pinging-back a URL. * * @since 0.0.1 * * @param string $concat_useragent The user agent concatenated with ' -- HiveQueen/' * and the HiveQueen version. * @param string $useragent The useragent. * @param string $pingback_server_url The server URL being linked to. * @param string $pagelinkedto URL of page linked to. * @param string $pagelinkedfrom URL of page linked from. */ $client->useragent = apply_filters('pingback_useragent', $client->useragent . ' -- HiveQueen/' . $hq_version, $client->useragent, $pingback_server_url, $pagelinkedto, $pagelinkedfrom); // when set to true, this outputs debug messages by itself $client->debug = false; if ($client->query('pingback.ping', $pagelinkedfrom, $pagelinkedto) || isset($client->error->code) && 48 == $client->error->code) { // Already registered add_ping($post_ID, $pagelinkedto); } } } }
/** * Check content for video and audio links to add as enclosures. * * Will not add enclosures that have already been added and will * remove enclosures that are no longer in the post. This is called as * pingbacks and trackbacks. * * @since 0.0.1 * * @global hqdb $hqdb * * @param string $content Post Content. * @param int $post_ID Post ID. */ function do_enclose($content, $post_ID) { global $hqdb; //TODO: Tidy this ghetto code up and make the debug code optional include_once ABSPATH . HQINC . '/class-IXR.php'; $post_links = array(); $pung = get_enclosed($post_ID); $post_links_temp = hq_extract_urls($content); foreach ($pung as $link_test) { if (!in_array($link_test, $post_links_temp)) { // link no longer in post $mids = $hqdb->get_col($hqdb->prepare("SELECT meta_id FROM {$hqdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $hqdb->esc_like($link_test) . '%')); foreach ($mids as $mid) { delete_metadata_by_mid('post', $mid); } } } foreach ((array) $post_links_temp as $link_test) { if (!in_array($link_test, $pung)) { // If we haven't pung it already $test = @parse_url($link_test); if (false === $test) { continue; } if (isset($test['query'])) { $post_links[] = $link_test; } elseif (isset($test['path']) && $test['path'] != '/' && $test['path'] != '') { $post_links[] = $link_test; } } } foreach ((array) $post_links as $url) { if ($url != '' && !$hqdb->get_var($hqdb->prepare("SELECT post_id FROM {$hqdb->postmeta} WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE %s", $post_ID, $hqdb->esc_like($url) . '%'))) { if ($headers = hq_get_http_headers($url)) { $len = isset($headers['content-length']) ? (int) $headers['content-length'] : 0; $type = isset($headers['content-type']) ? $headers['content-type'] : ''; $allowed_types = array('video', 'audio'); // Check to see if we can figure out the mime type from // the extension $url_parts = @parse_url($url); if (false !== $url_parts) { $extension = pathinfo($url_parts['path'], PATHINFO_EXTENSION); if (!empty($extension)) { foreach (hq_get_mime_types() as $exts => $mime) { if (preg_match('!^(' . $exts . ')$!i', $extension)) { $type = $mime; break; } } } } if (in_array(substr($type, 0, strpos($type, "/")), $allowed_types)) { add_post_meta($post_ID, 'enclosure', "{$url}\n{$len}\n{$mime}\n"); } } } } }