post_type == 'revision') {
return $post_id;
}
// 公開以外の場合はアメブロに投稿しない
if ($post->post_status != 'publish' && $post->post_status != 'future') {
return $post_id;
}
// 下書き・非公開の場合は削除
if ($post->post_status == 'draft' || $post->post_status == 'private') {
ameblo_delete($post_id, $post);
return $post_id;
}
do_post($post_id, $post);
}
function do_post($post_id, $post, $isSavedToPost=false) {
// 権限のチェック
if ( $post->post_type == 'page' ) {
if ( !current_user_can( 'edit_page', $post_id )) {
return $post_id;
}
} else {
if ( !current_user_can( 'edit_post', $post_id )) {
return $post_id;
}
}
if (empty($post)) {
$post = get_post($post_id);
}
for($i = 0;$i < AMEBLO_COUNT;$i ++) {
$user_id_array[$i] = get_option('ameblo_plugin_value_id'.$i);
$user_pass_array[$i] = get_option('ameblo_plugin_value_pass'.$i);
$ameblo_url_sv_post_array[$i] = get_option('ameblo_url_sv_post'.$i);
$ameblo_url_sv_upload_array[$i] = get_option('ameblo_url_sv_upload'.$i);
$ameblo_post_flag_array[$i] = get_post_meta($post->ID,'ameblo_plugin_value_post'.$i, true);
//アメーバ認証を作成
$created = date('Y-m-d\TH:i:s\Z',strtotime($post->post_date));
$nonce = sha1(md5(time()));
$pass_digest = base64_encode(pack('H*', sha1($nonce.$created.strtolower(md5($user_pass_array[$i])))));
$wsse = 'UsernameToken Username="'.$user_id_array[$i].'", '.
'PasswordDigest="'.$pass_digest.'", '.
'Nonce="'.base64_encode($nonce).'", '.
'Created="'.$created.'"';
if($ameblo_post_flag_array[$i] == "on" && $ameblo_url_sv_post_array[$i] != "" && $ameblo_url_sv_upload_array[$i] != "") {
// 変更の場合は一旦画像を削除する
$ameblo_post_image_edit = get_post_meta($post->ID,'ameblo_post_image_edit'.$i, true);
if(!empty($ameblo_post_image_edit) || $isSavedToPost) {
$req = new HTTP_REQUEST();
$req->setURL($ameblo_post_image_edit);
$req->addHeader('Accept', 'application/x.atom+xml, application/xml, text/xml, */*');
$req->addHeader('Authorization', 'WSSE profile="UsernameToken"');
$req->addHeader('Content-Type', 'application/x.atom+xml');
$req->addHeader('X-WSSE', $wsse);
$req->setMethod(HTTP_REQUEST_METHOD_DELETE);
$req->sendRequest();
$res = $req->getResponseBody();
}
//サムネイルを取得
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
$image = wp_get_attachment_image_src( $post_thumbnail_id, 'post-thumbnail' );
//サムネイルがある場合のみの処理
$ameblo_post_image_url = "";
if ($image) {
list($src, $width, $height) = $image;
$imgfile = esc_attr($src);
$imgdata = file_get_contents($imgfile);
$imgdata64 =base64_encode($imgdata);
//////// choi 2014-09-08 サーバによる不具合(exif)修正。
//////// original
//$content_type = image_type_to_mime_type(exif_imagetype($imgfile));
//////// original end
//////// modefied 2014-09-08
$img_type = getimagesize($imgfile);
$content_type = image_type_to_mime_type($img_type[2]);
//////// modefied end
//////// choi end
$rawdata_img = sprintf('
'; // サムネイルを追加 if($ameblo_post_image_url != "") { $text .= ''; $rawdata = sprintf(''; } if($content["main"] != "") { $text .= nl2br($content["main"]) . "
"; } $content_more = apply_filters('the_content',$content['extended']); // 最後に引用元を追加 if($content_more !=""){ $text .= '
この記事の続きはこちら'; }else { $quote_text = $title; $text_len = mb_strlen($quote_text, "utf-8"); if ($text_len >= 30) { $quote_text = mb_substr($quote_text, 0, 30)."・・・"; } $text .= '
引用元:' . $quote_text . ''; } $text .= '
アメブロ' . $count . 'はアメーバとの連携が確立しました。
'; echo '記事アップURL:'.$ameblo_url_sv_post_array[$i].'
'; echo '画像アップURL:'.$ameblo_url_sv_upload_array[$i].'
'; echo '設定を保存しました。
'; } else { if($ameblo_id_array[$i] != "" && $ameblo_pass_array[$i] != "") { echo 'アメブロ'.$count.'はアメーバとの連携ができませんでした。
IDとパスワードをご確認ください
アメブロの設定が完了していません。
'; } } // 投稿を保存した際、カスタムデータを保存 function ameblo_auto_post_save_postdata( $post_id, $post ) { // データが編集フォームのから適切な認証とともに送られてきたかどうか確認 $noncename = ""; if (!empty($_POST['ameblo_post_noncename'])) { $noncename = $_POST['ameblo_post_noncename']; } if ( !wp_verify_nonce( $noncename, plugin_basename(__FILE__) )) { return $post_id; } // 自動保存ルーチンの場合はスキップ if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) { return $post_id; } // パーミッションチェック if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id )) { return $post_id; } } else { if ( !current_user_can( 'edit_post', $post_id )) { return $post_id; } } // 更新時にリビジョン登録もされるので、その時はスキップ if($post->post_type == 'revision') { return $post_id; } for($i = 0;$i < AMEBLO_COUNT;$i ++) { $ameblo_post_flag_array = get_post_meta($post_id, 'ameblo_plugin_value_post'.$i, true); // DBの内容 if(isset($_POST['ameblo_post_new_field'.$i])) {//画面選択あり if(empty($ameblo_post_flag_array)) {//DBになければ $mydata_array[$i] = $_POST['ameblo_post_new_field'.$i]; // 画面の内容 add_post_meta($post_id, 'ameblo_plugin_value_post'.$i, $mydata_array[$i]); } } else {//画面選択なし if(!empty($ameblo_post_flag_array)) {//DBにあれば delete_post_meta($post_id, 'ameblo_plugin_value_post'.$i); } } } return array(); } function ameblo_delete($post_id, $post) { // 権限のチェック if ( $post->post_type == 'page' ) { if ( !current_user_can( 'edit_page', $post_id )) { return $post_id; } } else { if ( !current_user_can( 'edit_post', $post_id )) { return $post_id; } } for($i = 0;$i < AMEBLO_COUNT;$i ++) { $user_id_array[$i] = get_option('ameblo_plugin_value_id'.$i); $user_pass_array[$i] = get_option('ameblo_plugin_value_pass'.$i); //アメーバ認証を作成 $created = date('Y-m-d\TH:i:s\Z',strtotime($post->post_date)); $nonce = sha1(md5(time())); $pass_digest = base64_encode(pack('H*', sha1($nonce.$created.strtolower(md5($user_pass_array[$i]))))); $wsse = 'UsernameToken Username="'.$user_id_array[$i].'", '. 'PasswordDigest="'.$pass_digest.'", '. 'Nonce="'.base64_encode($nonce).'", '. 'Created="'.$created.'"'; $ameblo_post_entry_edit = get_post_meta($post->ID,'ameblo_post_entry_edit'.$i, true); if(empty($ameblo_post_entry_edit)) { return $post_id; } $req = new HTTP_REQUEST(); $req->setURL($ameblo_post_entry_edit); $req->setMethod(HTTP_REQUEST_METHOD_DELETE); $req->addHeader('Accept', 'application/x.atom+xml, application/xml, text/xml, */*'); $req->addHeader('Authorization', 'WSSE profile="UsernameToken"'); $req->addHeader('Content-Type', 'application/x.atom+xml'); $req->addHeader('X-WSSE', $wsse); $req->sendRequest(); $res = $req->getResponseBody(); } } add_action('trash_post', 'ameblo_delete', 10, 2); add_action('transition_post_status', 'check_status', 10, 3); function check_status($new_status, $old_status, $post) { $post_id = $post->ID; if (($old_status == 'trash' || $old_status == 'private' || $old_status == 'draft') && $new_status == 'publish') { do_post($post_id, $post, true); return; } if ($old_status == 'publish' && ($new_status == 'trash' || $new_status == 'private' || $new_status == 'draft')) { ameblo_delete($post_id, $post); return; } }