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(' %s ',$content_type,$imgdata64); //画像をポスト $req = new HTTP_REQUEST(); $req->setURL($ameblo_url_sv_upload_array[$i]); $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_POST); $req->addRawPostData($rawdata_img); $req->sendRequest(); $res = $req->getResponseBody(); if($res != "") { $xml = simplexml_load_string($res); foreach($xml -> link as $link) { if($link["rel"] == "alternate") { $ameblo_post_image_url = (string)$link["href"]; } // 更新用のURLを取得して、post-metaに保存 if($link["rel"] == "service.edit") { $ameblo_post_image_edit = (string)$link["href"]; } } $ameblo_post_edit_url = get_post_meta($post->ID,'ameblo_post_image_url'.$i, true); if(empty($ameblo_post_edit_url)) { add_post_meta($post->ID, 'ameblo_post_image_url'.$i, $ameblo_post_image_url); add_post_meta($post->ID, 'ameblo_post_image_edit'.$i, $ameblo_post_image_edit); } else { update_post_meta($post->ID, 'ameblo_post_image_url'.$i, $ameblo_post_image_url); update_post_meta($post->ID, 'ameblo_post_image_edit'.$i, $ameblo_post_image_edit); } } else { $ameblo_post_image_url = get_post_meta($post->ID,'ameblo_post_image_url'.$i, true);; } } //記事内容を取得し、加工する //記事タイトル $title = $post->post_title; //本文 $content = get_extended($post->post_content); // 引用タグを追加 $text = '
'; // サムネイルを追加 if($ameblo_post_image_url != "") { $text .= '

'; } 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 .= '
'; $rawdata = sprintf(' %s ', $title, $text); $req = new HTTP_REQUEST(); $ameblo_post_entry_edit = get_post_meta($post->ID,'ameblo_post_entry_edit'.$i, true); if(empty($ameblo_post_entry_edit) || $isSavedToPost) { $req->setURL($ameblo_url_sv_post_array[$i]); $req->setMethod(HTTP_REQUEST_METHOD_POST); } else { $req->setURL($ameblo_post_entry_edit); $req->setMethod(HTTP_REQUEST_METHOD_PUT); } $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->setBody($rawdata); $req->sendRequest(); $res = $req->getResponseBody(); // 登録したエントリーのURLと更新用のURLを取得する if($res != "") { $res = str_replace('', '', $res); $xml = simplexml_load_string($res); foreach($xml -> entry -> link as $link) { if($link["rel"] == "alternate") { $ameblo_post_entry_url = (string)$link["href"]; } if($link["rel"] == "service.edit") { $ameblo_post_entry_edit = (string)$link["href"]; } } $ameblo_post_edit_url = get_post_meta($post->ID,'ameblo_post_entry_url'.$i, true); if(empty($ameblo_post_edit_url)) { add_post_meta($post->ID, 'ameblo_post_entry_url'.$i, $ameblo_post_entry_url); add_post_meta($post->ID, 'ameblo_post_entry_edit'.$i, $ameblo_post_entry_edit); } else { update_post_meta($post->ID, 'ameblo_post_entry_url'.$i, $ameblo_post_entry_url); update_post_meta($post->ID, 'ameblo_post_entry_edit'.$i, $ameblo_post_entry_edit); } } } } } add_action('save_post', 'ameblo_auto_post', 10, 2 ); //管理画面 function ameblo_auto_post_edit_setting(){ if(isset($_POST["submit"])){ for($i = 0;$i < AMEBLO_COUNT;$i ++) { update_option( "ameblo_post_plugin_value_name" . $i ,$_POST["ameblo_name".$i] ); update_option( "ameblo_plugin_value_id" . $i ,$_POST["ameblo_id".$i] ); update_option( "ameblo_plugin_value_pass" . $i ,$_POST["ameblo_pass".$i] ); $user_name_array[$i] = get_option('ameblo_post_plugin_value_name'.$i); $user_id_array[$i] = get_option('ameblo_plugin_value_id'.$i); $user_pass_array[$i] = get_option('ameblo_plugin_value_pass'.$i); } for($i = 0;$i < AMEBLO_COUNT;$i ++) { if($user_id_array[$i] != "") { $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.'"'; $req = new HTTP_REQUEST(); $req->addHeader('X-WSSE', $wsse); $req->setURL(AMEBA_API_URL); $req->sendRequest(); $res = $req->getResponseBody(); //アップロード用のURLを取得 preg_match('/rel="service.post" type="application\/x\.atom\+xml" href="(.*?)"/',$res,$ameblo_post_url_entry_array[$i]); preg_match('/rel="service.upload" type="application\/x\.atom\+xml" href="(.*?)"/',$res,$ameblo_post_url_image_array[$i]); $ameblo_url_sv_post_array[$i] = $ameblo_post_url_entry_array[$i][1]; $ameblo_url_sv_upload_array[$i] = $ameblo_post_url_image_array[$i][1]; } else { $ameblo_url_sv_post_array[$i] = ""; $ameblo_url_sv_upload_array[$i] = ""; } update_option( "ameblo_url_sv_post".$i ,$ameblo_url_sv_post_array[$i] ); update_option( "ameblo_url_sv_upload".$i ,$ameblo_url_sv_upload_array[$i] ); } } for($i = 0;$i < AMEBLO_COUNT;$i ++) { $ameblo_name_array[$i] = get_option('ameblo_post_plugin_value_name'.$i); $ameblo_id_array[$i] = get_option('ameblo_plugin_value_id'.$i); $ameblo_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); } echo '

アメーバプレス投稿設定

使い方マニュアル  高機能プレミアム版

'; for($i = 0;$i < AMEBLO_COUNT;$i ++) { $count = $i + 1; echo '

■アメブロ' . $count . '

'; echo '
アメブロ名
アメブロID
アメブロパスワード
'; } echo '

'; if(isset($_POST["submit"])){ echo '
'; for($i = 0;$i < AMEBLO_COUNT;$i ++) { $count = $i + 1; if($ameblo_url_sv_post_array[$i] != "" && $ameblo_url_sv_upload_array[$i] != "") { echo '

アメブロ' . $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とパスワードをご確認ください

'; } } } echo '
'; } } add_action('admin_menu','ameblo_auto_post_admin_menu'); function ameblo_auto_post_admin_menu(){ add_menu_page( "ameblo settings", "アメブロ設定", "administrator", "ameblo_auto_post_admin_menu", "ameblo_auto_post_edit_setting" ); } // admin_menu アクションフックでカスタムボックスを定義 add_action('admin_menu', 'ameblo_auto_post_add_custom_box'); // データが入力された際 save_post アクションフックを使って何か行う add_action('save_post', 'ameblo_auto_post_save_postdata', 5, 2); // 投稿・固定ページの "advanced" 画面にカスタムセクションを追加 function ameblo_auto_post_add_custom_box() { add_meta_box( 'ameblo_post_sectionid', __( 'アメブロ自動投稿プラグイン', 'ameblo_post_textdomain' ), 'ameblo_auto_post_inner_custom_box', 'post', 'side', 'high' ); } // カスタム投稿・固定ページセクションに内側のフィールドをプリント function ameblo_auto_post_inner_custom_box($post) { // 認証に nonce を使う echo ''; $flg = 0; echo ''; if($flg == 0) { echo '

アメブロの設定が完了していません。

'; } } // 投稿を保存した際、カスタムデータを保存 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; } }