Plugin Directory

Changeset 1929831


Ignore:
Timestamp:
08/24/2018 04:52:13 PM (8 years ago)
Author:
minosystem
Message:

新記事ツクレイアウトに対応

Location:
article-importer/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • article-importer/trunk/article-importer.php

    r1921914 r1929831  
    7676                $roopCount++;
    7777
    78                 if(count($csvData) != 8) {
    79                     echo sprintf("<p><strong>スキップしました:%s行目</strong></p>" , $roopCount);
    80                     continue;
     78                if(count($csvData) == 8) {
     79                    $Title = $csvData[0];
     80                    $MovieUrl = $csvData[1];
     81                    $ThumbnailUrl = $csvData[2];
     82                    $Category = $csvData[3];
     83                    $Tags = $csvData[4];
     84                    $Remarks = $csvData[5];
     85                    $ArticleDate = $csvData[6];
     86                    $TemplateInfo = $csvData[7];
     87
     88                    $Tags = mb_convert_kana($Tags, 's');
     89                    $allTags = explode( " ", $Tags );
     90                   
     91                    $post_categories = term_exists($Category, 'category');
     92                    if ($post_categories === 0 || $post_categories === null) {
     93                        $post_categories = wp_insert_term( $Category, 'category' );
     94                    }
     95
     96                    $articleImagePath = "";
     97                   
     98                    if($ThumbnailUrl != "") {
     99                        $baseThumbnailName = substr($ThumbnailUrl, strrpos($ThumbnailUrl , "/" ) + 1);
     100                       
     101                        list($file_name, $file_type) = explode("." , $baseThumbnailName);
     102                        $postname = date("YmdHis") . $this->makeRandStr(32);
     103                        $picName = $postname . "." . $file_type;
     104
     105                        $wp_upload_dir = wp_upload_dir();
     106                       
     107                        $options['ssl']['verify_peer']=false;
     108                        $options['ssl']['verify_peer_name']=false;
     109                        $data = file_get_contents($ThumbnailUrl, false, stream_context_create($options));
     110                       
     111                        $imagepath = $wp_upload_dir['path'] . "/" . $picName;
     112                        file_put_contents($imagepath, $data);
     113                        $meta = getimagesize($imagepath);
     114                        $articleImagePath = $wp_upload_dir["url"] . "/" . $picName;
     115
     116                        // ファイルメタ情報登録
     117                        $attachment = array(
     118                          'post_mime_type' => image_type_to_mime_type($meta[2]),
     119                          'post_title' => preg_replace('/\.[^.]+$/', '', $imagepath),
     120                          'post_content' => '',
     121                          'post_status' => 'inherit'
     122                        );
     123                    }
     124                   
     125                    $TemplateInfo = str_replace("{{{タイトル}}}", $Title, $TemplateInfo);
     126                    $TemplateInfo = str_replace("{{{動画URL}}}", $MovieUrl, $TemplateInfo);
     127                    $TemplateInfo = str_replace("{{{サムネイル画像URL}}}", $articleImagePath, $TemplateInfo);
     128                    $TemplateInfo = str_replace("{{{カテゴリ}}}", $Category, $TemplateInfo);
     129                    $TemplateInfo = str_replace("{{{タグ}}}", $Tags, $TemplateInfo);
     130                    $TemplateInfo = str_replace("{{{備考}}}", $Remarks, $TemplateInfo);
     131                   
     132                    $post = array(
     133                      'post_status'           => 'publish',
     134                      'post_type'             => 'post',
     135                      'post_author'           => $user_id,
     136                      'ping_status'           => get_option( 'default_ping_status' ),
     137                      'post_parent'           => 0,
     138                      'menu_order'            => 0,
     139                      'to_ping'               => '',
     140                      'pinged'                => '',
     141                      'post_password'         => '',
     142                      'guid'                  => '',
     143                      'post_content_filtered' => '',
     144                      'post_excerpt'          => '',
     145                      'post_date'          => date("Y-m-d H:i:s", strtotime($ArticleDate)),
     146                      'post_content'          => $TemplateInfo,
     147                      'post_title'            => $Title,
     148                      'post_name'             => $postname,
     149                      'tags_input'             => $allTags,
     150                    );
     151                   
     152                    $post_ID = wp_insert_post( $post, $wp_error );
     153                   
     154                    wp_set_post_categories( $post_ID, $post_categories, false );
     155
     156                    if($ThumbnailUrl != "") {
     157                        $attach_id  = wp_insert_attachment( $attachment, $imagepath, $post_ID);
     158
     159                        if($attach_id) {
     160                            require_once(ABSPATH . "wp-admin" . '/includes/image.php'); // これが必要
     161                            $attach_data = wp_generate_attachment_metadata( $attach_id, $imagepath );
     162                            wp_update_attachment_metadata( $attach_id,  $attach_data );
     163                            $thumbnail = add_post_meta( $post_ID, "_thumbnail_id", $attach_id, false );
     164                        }
     165                    }
    81166                }
    82                 $Title = $csvData[0];
    83                 $MovieUrl = $csvData[1];
    84                 $ThumbnailUrl = $csvData[2];
    85                 $Category = $csvData[3];
    86                 $Tags = $csvData[4];
    87                 $Remarks = $csvData[5];
    88                 $ArticleDate = $csvData[6];
    89                 $TemplateInfo = $csvData[7];
    90 
    91                 $Tags = mb_convert_kana($Tags, 's');
    92                 $allTags = explode( " ", $Tags );
    93                
    94                 $post_categories = term_exists($Category, 'category');
    95                 if ($post_categories === 0 || $post_categories === null) {
    96                     $post_categories = wp_insert_term( $Category, 'category' );
    97                 }
    98 
    99                 $articleImagePath = "";
    100                
    101                 if($ThumbnailUrl != "") {
    102                     $baseThumbnailName = substr($ThumbnailUrl, strrpos($ThumbnailUrl , "/" ) + 1);
    103                    
    104                     list($file_name, $file_type) = explode("." , $baseThumbnailName);
    105                     $postname = date("YmdHis") . $this->makeRandStr(32);
    106                     $picName = $postname . "." . $file_type;
    107 
    108                     $wp_upload_dir = wp_upload_dir();
    109                    
    110                     $options['ssl']['verify_peer']=false;
    111                     $options['ssl']['verify_peer_name']=false;
    112                     $data = file_get_contents($ThumbnailUrl, false, stream_context_create($options));
    113                    
    114                     $imagepath = $wp_upload_dir['path'] . "/" . $picName;
    115                     file_put_contents($imagepath, $data);
    116                     $meta = getimagesize($imagepath);
    117                     $articleImagePath = $wp_upload_dir["url"] . "/" . $picName;
    118 
    119                     // ファイルメタ情報登録
    120                     $attachment = array(
    121                       'post_mime_type' => image_type_to_mime_type($meta[2]),
    122                       'post_title' => preg_replace('/\.[^.]+$/', '', $imagepath),
    123                       'post_content' => '',
    124                       'post_status' => 'inherit'
     167
     168                if(count($csvData) == 7) {
     169                    $Title = $csvData[0];
     170                    $Tags = $csvData[1];
     171                    $Category = $csvData[2];
     172                    $ArticleDate = $csvData[3];
     173                    $ThumbnailUrls = $csvData[4];
     174                    $Remarks = $csvData[5];
     175                    $Publicfg = $csvData[6];
     176
     177                    $Tags = mb_convert_kana($Tags, 's');
     178                    $allTags = explode( " ", $Tags );
     179                   
     180                    $post_categories = term_exists($Category, 'category');
     181                    if ($post_categories === 0 || $post_categories === null) {
     182                        $post_categories = wp_insert_term( $Category, 'category' );
     183                    }
     184
     185                    $firstPic = "";
     186                    $loopcount = 0;
     187
     188                    if($ThumbnailUrls != "") {
     189                        $tmpArray = explode( ",", $ThumbnailUrls );
     190                        foreach ($tmpArray as $ThumbnailUrl) {
     191
     192                            if($ThumbnailUrl == "") {
     193                                contonue;
     194                            }
     195
     196                            $baseThumbnailName = substr($ThumbnailUrl, strrpos($ThumbnailUrl , "/" ) + 1);
     197                           
     198                            list($file_name, $file_type) = explode("." , $baseThumbnailName);
     199                            $postname = date("YmdHis") . $this->makeRandStr(32);
     200                            $picName = $postname . "." . $file_type;
     201
     202                            $wp_upload_dir = wp_upload_dir();
     203                           
     204                            $options['ssl']['verify_peer']=false;
     205                            $options['ssl']['verify_peer_name']=false;
     206                            $data = file_get_contents($ThumbnailUrl, false, stream_context_create($options));
     207                           
     208                            $imagepath = $wp_upload_dir['path'] . "/" . $picName;
     209                            file_put_contents($imagepath, $data);
     210                            $meta = getimagesize($imagepath);
     211                            $articleImagePath = $wp_upload_dir["url"] . "/" . $picName;
     212
     213                            if($loopcount == 0) {
     214                                $firstPic = $articleImagePath;
     215                            }
     216
     217                            // ファイルメタ情報登録
     218                            $attachment = array(
     219                              'post_mime_type' => image_type_to_mime_type($meta[2]),
     220                              'post_title' => preg_replace('/\.[^.]+$/', '', $imagepath),
     221                              'post_content' => '',
     222                              'post_status' => 'inherit'
     223                            );
     224                           
     225                            $Remarks = str_replace($ThumbnailUrl, $articleImagePath, $Remarks);
     226
     227                            $loopcount++;
     228                        }
     229                    }
     230
     231                    $publicval = "";
     232
     233                    if($Publicfg == "2") {
     234                        $publicval = "publish";
     235                    } else {
     236                        $publicval = "private";
     237                    }
     238                   
     239                    $post = array(
     240                      'post_status'           => $publicval,
     241                      'post_type'             => 'post',
     242                      'post_author'           => $user_id,
     243                      'ping_status'           => get_option( 'default_ping_status' ),
     244                      'post_parent'           => 0,
     245                      'menu_order'            => 0,
     246                      'to_ping'               => '',
     247                      'pinged'                => '',
     248                      'post_password'         => '',
     249                      'guid'                  => '',
     250                      'post_content_filtered' => '',
     251                      'post_excerpt'          => '',
     252                      'post_date'          => date("Y-m-d H:i:s", strtotime($ArticleDate)),
     253                      'post_content'          => $Remarks,
     254                      'post_title'            => $Title,
     255                      'post_name'             => $postname,
     256                      'tags_input'             => $allTags,
    125257                    );
    126                 }
    127                
    128                 $TemplateInfo = str_replace("{{{タイトル}}}", $Title, $TemplateInfo);
    129                 $TemplateInfo = str_replace("{{{動画URL}}}", $MovieUrl, $TemplateInfo);
    130                 $TemplateInfo = str_replace("{{{サムネイル画像URL}}}", $articleImagePath, $TemplateInfo);
    131                 $TemplateInfo = str_replace("{{{カテゴリ}}}", $Category, $TemplateInfo);
    132                 $TemplateInfo = str_replace("{{{タグ}}}", $Tags, $TemplateInfo);
    133                 $TemplateInfo = str_replace("{{{備考}}}", $Remarks, $TemplateInfo);
    134                
    135                 $post = array(
    136                   'post_status'           => 'publish',
    137                   'post_type'             => 'post',
    138                   'post_author'           => $user_id,
    139                   'ping_status'           => get_option( 'default_ping_status' ),
    140                   'post_parent'           => 0,
    141                   'menu_order'            => 0,
    142                   'to_ping'               => '',
    143                   'pinged'                => '',
    144                   'post_password'         => '',
    145                   'guid'                  => '',
    146                   'post_content_filtered' => '',
    147                   'post_excerpt'          => '',
    148                   'post_date'          => date("Y-m-d H:i:s", strtotime($ArticleDate)),
    149                   'post_content'          => $TemplateInfo,
    150                   'post_title'            => $Title,
    151                   'post_name'             => $postname,
    152                   'tags_input'             => $allTags,
    153                 );
    154                
    155                 $post_ID = wp_insert_post( $post, $wp_error );
    156                
    157                 wp_set_post_categories( $post_ID, $post_categories, false );
    158 
    159                 if($ThumbnailUrl != "") {
    160                     $attach_id  = wp_insert_attachment( $attachment, $imagepath, $post_ID);
    161 
    162                     if($attach_id) {
    163                         require_once(ABSPATH . "wp-admin" . '/includes/image.php'); // これが必要
    164                         $attach_data = wp_generate_attachment_metadata( $attach_id, $imagepath );
    165                         wp_update_attachment_metadata( $attach_id,  $attach_data );
    166                         $thumbnail = add_post_meta( $post_ID, "_thumbnail_id", $attach_id, false );
     258                   
     259                    $post_ID = wp_insert_post( $post, $wp_error );
     260                   
     261                    wp_set_post_categories( $post_ID, $post_categories, false );
     262
     263                    if($firstPic != "") {
     264                        $attach_id  = wp_insert_attachment( $attachment, $firstPic, $post_ID);
     265
     266                        if($attach_id) {
     267                            require_once(ABSPATH . "wp-admin" . '/includes/image.php'); // これが必要
     268                            $attach_data = wp_generate_attachment_metadata( $attach_id, $firstPic );
     269                            wp_update_attachment_metadata( $attach_id,  $attach_data );
     270                            $thumbnail = add_post_meta( $post_ID, "_thumbnail_id", $attach_id, false );
     271                        }
    167272                    }
    168273                }
  • article-importer/trunk/readme.txt

    r1921914 r1929831  
    55Requires at least: 4.9
    66Tested up to: 4.9
    7 Stable tag: 1.2
     7Stable tag: 2.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2929
    3030= どのようなレイアウトのデータが対応していますか? =
    31 以下のカンマ区切りのデータとなります。
     31対応レイアウトが2種類あります。全てカンマ区切りのデータとなります。
     32■旧記事ツク
    3233タイトル, 動画URL, サムネイル画像URL, カテゴリ, タグ, 備考, 公開日, テンプレート構文
     34■新記事ツク
     35タイトル, タグ, カテゴリ, 公開日, サムネイル画像URL, 本文, 公開ステータス
    3336
    3437== Screenshots ==
     
    3740
    3841== Changelog ==
     42
     43= 2.0 =
     44新レイアウトのCSVデータに対応
    3945
    4046= 1.2 =
Note: See TracChangeset for help on using the changeset viewer.