DBにテーブルを作ってタイトルと概要文をキャッシュします。
前回まで
スタイルシートを付けてブログカードっぽくしました。
DBの作成とアクセス
テーブルの項目を考える
DBにテーブルを作成します。
記録する項目としては「URL」、「サイト名称」、「タイトル」、「概要文」です。
Pz-LinkCardではこの他に「登録日」や「ソーシャルカウント」などがあります。
項目名称 | 項目名 | 文字数 |
URL | URL | VARCHAR(2048) |
サイト名称 | site_name | VARCHAR(100) |
タイトル | title | VARCHAR(200) |
概要文 | excerpt | VARCHAR(500) |
有効化したときに呼ぶ関数
プラグインを有効化したときにDBのテーブルを作成します。
まずは有効化(activate)したときに呼ばれる関数を作成します。
12 13 14 15 16 |
function __construct() { add_shortcode('blogcard', array($this, 'shortcode' ) ); add_action('wp_enqueue_scripts', array($this, 'enqueue' ) ); register_activation_hook(__FILE__, array($this, 'activate') ); } |
56 57 58 59 60 61 62 63 |
$html = '<DIV class="popo_blogcard"><A href="'.$url.'" target="_blank" class="popo_bc_link"><DIV class="popo_bc_wrap"><DIV class="popo_bc_site_name">'.$site_name.'</DIV><DIV class="popo_bc_content"><DIV class="popo_bc_title">'.$title.'</DIV><DIV class="popo_bc_url">'.$url.'</DIV><DIV class="popo_bc_excerpt">'.$excerpt.'</DIV></DIV></DIV></DIV>'; return $html; } public function activate() { } function enqueue() { wp_enqueue_style('popo-blogcard', plugin_dir_url(__FILE__).'style.css'); } |
DBのテーブルを作成する
dbDelta()という関数を使ってDBのテーブルを定義します。
59 60 61 62 63 64 65 66 67 68 69 70 |
public function activate() { global $wpdb; $wpdb->hide_errors(); require_once (ABSPATH.'wp-admin/includes/upgrade.php'); $sql = "CREATE TABLE ".$wpdb->prefix."popo_blogcard ( url VARCHAR(2048) DEFAULT '', site_name VARCHAR(100) DEFAULT '', title VARCHAR(200) DEFAULT '', excerpt VARCHAR(500) DEFAULT '', ) ".$wpdb->get_charset_collate()." ;"; dbDelta($sql); } |
無効化したときに呼ぶ関数
プラグインを停止したときにDBを削除しようと思います。
停止(deactivate)したときに呼ばれる関数を作成します。
12 13 14 15 16 17 |
function __construct() { add_shortcode('blogcard', array($this, 'shortcode' ) ); add_action('wp_enqueue_scripts', array($this, 'enqueue' ) ); register_activation_hook(__FILE__, array($this, 'activate') ); register_deactivation_hook(__FILE__, array($this, 'deactivate') ); } |
69 70 71 72 73 74 75 76 |
) ".$wpdb->get_charset_collate()." ;"; dbDelta($sql); } public function deactivate() { } function enqueue() { wp_enqueue_style('popo-blogcard', plugin_dir_url(__FILE__).'style.css'); } |
DBのテーブルを削除する
DBのテーブルを削除する記述をします。
72 73 74 75 76 |
public function deactivate() { global $wpdb; $sql = "DROP TABLE ".$wpdb->prefix."popo_blogcard"; $wpdb->query($sql); } |
DBのテーブルに情報を書きだす
cURLで取得した「URL」、「サイト名称」、「タイトル」、「概要文」をDBのテーブルに書き出します。
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
$site_name = $m['host']; } } curl_close($ch ); $result = $wpdb->insert( $wpdb->prefix.'popo_blogcard', array( 'url' => $url, 'site_name' => $site_name, 'title' => $title, 'excerpt' => $excerpt ) ); $html = '<DIV class="popo_blogcard"><A href="'.$url.'" target="_blank" class="popo_bc_link"><DIV class="popo_bc_wrap"><DIV class="popo_bc_site_name">'.$site_name.'</DIV><DIV class="popo_bc_content"><DIV class="popo_bc_title">'.$title.'</DIV><DIV class="popo_bc_url">'.$url.'</DIV><DIV class="popo_bc_excerpt">'.$excerpt.'</DIV></DIV></DIV></DIV>'; return $html; } |
DBのテーブルから情報を読みだす
DBにキャッシュがあったらcURLでアクセスしないでDBから読み出します。
DBにキャッシュが無い場合の処理を字下げします。
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
$url = $opt['url']; global $wpdb; $data = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."popo_blogcard WHERE url=%s", $url ) ); if ($data ) { $site_name = $data->site_name; $title = $data->title; $excerpt = $data->excerpt; } else { $ch = curl_init($url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_TIMEOUT, 8 ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_MAXREDIRS, 8 ); curl_setopt($ch, CURLOPT_AUTOREFERER, true ); curl_setopt($ch, CURLOPT_COOKIESESSION, true ); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false ); $html = curl_exec($ch ); if (curl_errno($ch ) ) { $site_name = ''; $title = $url; $excerpt = ''; } else { $html = mb_convert_encoding($html, 'UTF-8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS' ); if (preg_match('/property="og:title"\s*content="([^"]*)/si', $html, $m ) ) { $title = esc_html($m[1]); } else { if (preg_match('/<\s*title\s*[^>]*>\s*([^<]*)\s*<\s*\/title\s*[^>]*>/si', $html, $m ) ) { $title = esc_html($m[1]); } } if (preg_match('/property="og:description"\s*content="([^"]*)/si', $html, $m ) ) { $excerpt = esc_html($m[1]); } else { if (preg_match('/name="description"\s*content="([^"]*)/si', $html, $m ) ) { $excerpt = esc_html($m[1]); } } if (preg_match('/property="og:site_name"\s*content="([^"]*)/si', $html, $m ) ) { $site_name = esc_html($m[1]); } else { $m = parse_url($url ); $site_name = $m['host']; } } curl_close($ch ); $result = $wpdb->insert( $wpdb->prefix.'popo_blogcard', array( 'url' => $url, 'site_name' => $site_name, 'title' => $title, 'excerpt' => $excerpt ) ); } |
プラグインを有効化する
プラグインを有効化したときにDBのテーブルを作成するので有効化します。
すでに有効化している場合、いったん停止してから有効化をしてください。
見た目には分かりませんがキャッシュから読み出されるようになりました。
今回のコード
今回はここまでです。
プラグイン本体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<?php /* Plugin Name: popo-Blogcard Plugin URI: https://popozure.info/20180808/13142 Description: Blogcard Version: 0.0.1 Author: poporon Author URI: https://popozure.info License: GPL2 */ class popo_blogcard { function __construct() { add_shortcode('blogcard', array($this, 'shortcode' ) ); add_action('wp_enqueue_scripts', array($this, 'enqueue' ) ); register_activation_hook(__FILE__, array($this, 'activate') ); register_deactivation_hook(__FILE__, array($this, 'deactivate') ); } function shortcode($opt , $content = null ) { $url = $opt['url']; global $wpdb; $data = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."popo_blogcard WHERE url=%s", $url ) ); if ($data ) { $site_name = $data->site_name; $title = $data->title; $excerpt = $data->excerpt; } else { $ch = curl_init($url ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_TIMEOUT, 8 ); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); curl_setopt($ch, CURLOPT_MAXREDIRS, 8 ); curl_setopt($ch, CURLOPT_AUTOREFERER, true ); curl_setopt($ch, CURLOPT_COOKIESESSION, true ); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false ); $html = curl_exec($ch ); if (curl_errno($ch ) ) { $site_name = ''; $title = $url; $excerpt = ''; } else { $html = mb_convert_encoding($html, 'UTF-8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS' ); if (preg_match('/property="og:title"\s*content="([^"]*)/si', $html, $m ) ) { $title = esc_html($m[1]); } else { if (preg_match('/<\s*title\s*[^>]*>\s*([^<]*)\s*<\s*\/title\s*[^>]*>/si', $html, $m ) ) { $title = esc_html($m[1]); } } if (preg_match('/property="og:description"\s*content="([^"]*)/si', $html, $m ) ) { $excerpt = esc_html($m[1]); } else { if (preg_match('/name="description"\s*content="([^"]*)/si', $html, $m ) ) { $excerpt = esc_html($m[1]); } } if (preg_match('/property="og:site_name"\s*content="([^"]*)/si', $html, $m ) ) { $site_name = esc_html($m[1]); } else { $m = parse_url($url ); $site_name = $m['host']; } } curl_close($ch ); $result = $wpdb->insert( $wpdb->prefix.'popo_blogcard', array( 'url' => $url, 'site_name' => $site_name, 'title' => $title, 'excerpt' => $excerpt ) ); } $html = '<DIV class="popo_blogcard"><A href="'.$url.'" target="_blank" class="popo_bc_link"><DIV class="popo_bc_wrap"><DIV class="popo_bc_site_name">'.$site_name.'</DIV><DIV class="popo_bc_content"><DIV class="popo_bc_title">'.$title.'</DIV><DIV class="popo_bc_url">'.$url.'</DIV><DIV class="popo_bc_excerpt">'.$excerpt.'</DIV></DIV></DIV></DIV>'; return $html; } public function activate() { global $wpdb; $wpdb->hide_errors(); require_once (ABSPATH.'wp-admin/includes/upgrade.php'); $sql = "CREATE TABLE ".$wpdb->prefix."popo_blogcard ( url VARCHAR(2048) DEFAULT '', site_name VARCHAR(100) DEFAULT '', title VARCHAR(200) DEFAULT '', excerpt VARCHAR(500) DEFAULT '', ) ".$wpdb->get_charset_collate()." ;"; dbDelta($sql); } public function deactivate() { global $wpdb; $sql = "DROP TABLE ".$wpdb->prefix."popo_blogcard"; $wpdb->query($sql); } function enqueue() { wp_enqueue_style('popo-blogcard', plugin_dir_url(__FILE__).'style.css'); } } $popo_blogcard = new popo_blogcard; |
スタイルシート
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
.popo_bc_wrap { margin: 16px; padding: 8px; width: 96%; border: 2px solid #8888ff; background-color: #ffffff; border-radius: 3px; } .popo_bc_link { text-decoration: none; } .popo_bc_site_name { display: block; font-size: 12px; } .popo_bc_title { display: block; font-size: 18px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } .popo_bc_url { display: block; font-size: 12px; color: #00f; text-decoration: underline; } .popo_bc_excerpt { height: 32px; font-size: 12px; color: #444; overflow: hidden; text-overflow: ellipsis; } |
次回は・・・
だいたい終わりが見えてきました。
少し寂しいので次回はサムネイルを追加しようと思います。
では、この辺で。(^-^)o
コメント