iCloudメールのimap、smtpの設定。
http://support.apple.com/kb/HT4864
「WordPress.ORGからプラグインのダウンロード数を取得するPHPスクリプト」を書いた時にAPIは無いのかと思ったが、よく考えるとダッシュボードからプラグインを追加する際に、WordPress自身も明らかにWordPress.ORGからデータを取得している。
で、調べてみると「/wp-admin/includes/plugin-install.php」の中にplugins_api()という関数があり、これを使えば各種データを取得できる。
この関数を使えば、多くの情報を正確に取得できるのだが、結局、特殊なページをwp_remote_post()しているだけなので、パフォーマンス面ではメリットはない。
実際に使ってみたが取得する情報量が非常に多いので、パフォーマンスは良くない。
特殊な情報が必要でない限り、wp_remote_post()でプラグインページのHTMLを取得して、正規表現で切り出したほうが自サイトにもWordPress.ORGにも優しいと思う。
ちなみに「plugin-install.php」は、常に読み込まれているわけではないようなので、プラグインなどでplugins_api()を使う場合は、require_once()すること。
WordPressのプラグイン内で外部サイトのページをリクエストする場合は、wp_remote_get()、wp_remote_post()、wp_remote_head()などが便利。
※これらの関数は、wp-includes/http.php内で定義されている。
例えば、前記事の「WordPress.ORGからプラグインのダウンロード数を取得するPHPスクリプト」のコードをwp_remote_get()を使って書きなおすと、次のようになる。
<?php /** Author: redcocker Update: redcocker 2012/2/17 **/ function stat_count() { // Setting $address = 'プラグインページのURL'; $stat_count = -1; $response_data = wp_remote_get($address); if (is_wp_error($response_data) || $response_data['response']['code'] !== 200) { return $stat_count; } $stat_count = get_stat_count($response_data['body']); return $stat_count; } function get_stat_count($data) { preg_match("@<strong>Downloads: </strong>(.*?)<br />@", $data, $matches); $str = str_replace(',','',$matches[1]); settype($str, 'integer'); return number_format($str); } ?>
レスポンスがエラーだったり200以外の場合は、つまりは、想定したURLでページが見つからなかった場合は、-1を返す。
転送されているケースは考慮していないが、移転先もヘッダーから取得できそうな気が。
WordPress.ORGの「Plugin Directory」に登録されているプラグインのダウンロード数を取得する。
WP-Stats-Dashboardからコードを流用。
設定: プラグインの個別ページのURL。
ダウンロード数の取得: stat_count()を実行。
<?php /** Author: dligthart Origin: WP-Stats-Dashboard(WPSDStats.php, WPSDPluginStats.php) Update: redcocker 2012/2/13 **/ function stat_count() { // Setting $address = 'プラグインページのURL'; $xml = fetchDataRemote($address); $stat_count = get_stat_count($xml); return $stat_count; } function fetchDataRemote($url, $agent = 1) { if ('' == $url) return false; $xml = ''; if (function_exists('curl_init')) { $ch = curl_init(); $timeout = 2; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); switch ($agent) { default: case 1: curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13"); break; case 2: curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.8) Gecko/20100722 BTRS86393 Firefox/3.6.8 ( .NET CLR 3.5.30729; .NET4.0C)"); break; case 3: curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); break; } $xml = curl_exec($ch); curl_close($ch); } else { $xml = file_get_contents($url); } return $xml; } function get_stat_count($data) { preg_match("@<strong>Downloads: </strong>(.*?)<br />@", $data, $matches); $str = str_replace(',','',$matches[1]); settype($str, 'integer'); return number_format($str); } ?>
これだけのために余計なトラフィックを生むのが難点。APIがあれば・・・。
単なる一覧だが、ドキュメントとしては本家より充実しているかも。
http://phpdoc.ftwr.co.uk/bbpress-plugin/
上記は、プラグイン版bbPressのページ。
WordPress、普通のbbPress、BuddyPress、GlotPress、BackPressの情報もあり。
Codexによると、フィルターフックの「comment_save_pre」は、
comment_save_pre フィルター関数引数: comment_post_ID、comment_author、comment_author_email、comment_author_url、comment_content、comment_type、user_IDのインデックスを含むコメントデータ配列 コメントを更新・編集する直前に、コメントデータに適用される。
とあるので、難しく考えていたが、何のことはない。
受け取るデータは、コメントの本文のみであり、コメントの本文を引数として渡してあげればOKだった。
以下の例は、preタグ内の記述を実体参照にエスケープする処理。
add_filter('comment_save_pre', 'wp_sh_escape_comment_updated'); function wp_sh_escape_comment_updated($content) { global $wp_sh_setting_opt; $match_num = preg_match_all('|<pre[^>]*?>.*?</pre>|is', $content, $match); if ($match_num != 0 || $match_num != false) { for ($i = 0; $i < $match_num; $i++) { $code = preg_replace('|<pre[^>]*?>(.*?)</pre>|is', '$1', $match[0][$i]); if (strpos($code, "<") !== false || strpos($code, ">") !== false || strpos($code, '"') !== false || strpos($code, "'") !== false || preg_match('/&[^(lt;)|(gt;)|(amp;)|(quot;)|(#039;)]/', $code)) { $replaced_code = htmlspecialchars($code, ENT_QUOTES, 'UTF-8'); } else { $replaced_code = $code; } $content = preg_replace('|'.preg_quote($code, '|').'|', $replaced_code, $content); } } return $content; }