Example #1
0
 /**
  * Save the data as a post
  *
  * @param array $data Save data
  * @return array|boolean
  */
 public function savePost($data = array())
 {
     if (empty($data)) {
         return false;
     }
     // タイトル生成
     preg_match('/^.*?(。|\\n|$)/', $data['message'], $match);
     $title = $match[0];
     $content = $data['message'];
     if ($data['link_text'] == true) {
         $content .= '<p class="facebook-post-url"><a href="' . $data['permalink'] . '">' . __('Facebook', FacebookPortal::TEXT_DOMAIN) . '</a></p>';
     }
     if ($data['auto_link'] == true) {
         $content = $this->Helper->autoLinkUrls($content, array('escape' => false));
     }
     $_data = array('post_status' => 'publish', 'post_author' => $data['post_author'], 'post_content' => $content, 'post_title' => $title, 'post_category' => json_decode($data['post_category']), 'post_date' => FacebookPortal::date('Y-m-d H:i:s', $data['timestamp']));
     if ($post_id = wp_insert_post($_data)) {
         add_post_meta($post_id, 'facebook_post_id', $data['id']);
         add_post_meta($post_id, 'facebook_page_id', $data['facebook_page_id']);
         $_data['ID'] = $post_id;
         return $_data;
     }
     return false;
 }
Example #2
0
        if (!$date['post_exist']) {
            ?>
                        <input type="checkbox" name="ids[]" value="<?php 
            echo $date['id'];
            ?>
">
                    <?php 
        }
        ?>
                    </th>
                    <td><?php 
        echo $date['message'];
        ?>
</td>
                    <td class="date column-date"><?php 
        echo FacebookPortal::date('Y-m-d H:i:s', $date['timestamp']);
        ?>
</td>
                    <td class="type column-type"><?php 
        echo $date['type'];
        ?>
</td>
                </tr>
<?php 
    }
    ?>
            </tbody>
        </table>

<?php 
    submit_button(__('Save the data checked.', FacebookPortal::TEXT_DOMAIN), 'primary');
Example #3
0
 /**
  * 記事更新
  * 
  * @return boolean
  */
 public function main()
 {
     // ページ情報を取得
     $fields = 'id,facebook_page_id,post_author,post_category,image_type,image_size,link_text,auto_link,post_updated';
     if ($results = $this->Model->getAll($fields)) {
         global $wpdb;
         $facebook_post_ids = $wpdb->get_col("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'facebook_post_id'");
         // Facebook ページの数だけ繰り返し
         foreach ($results as $result) {
             // 記事取得
             $since = $result['post_updated'] ? $result['post_updated'] : null;
             if (!($response = $this->Facebook->getFacebookFeed($result['facebook_page_id'], $since))) {
                 continue;
             } else {
                 $created_time = array();
                 foreach ($response as $feed) {
                     if (in_array($feed['id'], $facebook_post_ids)) {
                         continue;
                     }
                     // 投稿日をタイムスタンプに変換
                     $feed['timestamp'] = FacebookPortal::strtotime($feed['created_time']);
                     // デフォルト設定を代入
                     $feed['post_author'] = $result['post_author'];
                     $feed['post_category'] = $result['post_category'];
                     $feed['link_text'] = $result['link_text'];
                     $feed['auto_link'] = $result['auto_link'];
                     $feed['facebook_page_id'] = $result['facebook_page_id'];
                     // 投稿保存
                     if ($_data = $this->Common->savePost($feed)) {
                         // 更新確認用タイムスタンプ
                         $created_time[] = $feed['timestamp'];
                         if ($feed['type'] === 'photo' && isset($feed['object_id'])) {
                             // 添付画像の取り扱い設定がなければ処理をスキップ
                             if (empty($result['image_type'])) {
                                 continue;
                             }
                             // 添付画像取得保存
                             if ($photoUrls = $this->Facebook->getPhotoUrl($feed['object_id'])) {
                                 $attachment_ids = array();
                                 foreach ($photoUrls as $photoUrl) {
                                     if ($attachment_id = $this->Common->savePhoto($photoUrl, $_data['ID'], $_data['post_author'])) {
                                         $attachment_ids[] = $attachment_id;
                                     }
                                 }
                                 if (!empty($attachment_ids)) {
                                     // アイキャッチ画像
                                     if ($result['image_type'] == 'attachment') {
                                         $attachment_id = reset($attachment_ids);
                                         set_post_thumbnail($_data['ID'], $attachment_id);
                                     }
                                     // 記事に挿入
                                     if ($result['image_type'] == 'insert') {
                                         $attachment_id = reset($attachment_ids);
                                         $image_tag = wp_get_attachment_image($attachment_id, $result['image_size']);
                                         $content = $_data['post_content'] . '<p>' . $image_tag . '</p>';
                                         $params = $_data;
                                         $params['post_content'] = $content;
                                         wp_insert_post($params);
                                     }
                                     // ギャラリーショートコード
                                     if ($result['image_type'] == 'gallery') {
                                         //$ids = implode(',', $attachment_ids);
                                         //$option = (!empty($result['gallery_options'])) ? ' ' . stripslashes($result['gallery_options']) : '';
                                         //$content = $_data['post_content'] . '<p>[gallery ids="' . $ids . '"' . $option . ']</p>';
                                         //$params = $_data;
                                         //$params['post_content'] = $content;
                                         //wp_insert_post($params);
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if (count($created_time) > 0) {
                     arsort($created_time);
                     $post_updated = array_shift($created_time);
                     $fields = array('post_updated' => $post_updated);
                     $conditions = array('id' => $result['id']);
                     $wpdb->update($wpdb->prefix . FacebookPortal::DB_TABLE, $fields, $conditions);
                 }
             }
         }
     }
 }
Example #4
0
        ?>
" title="<?php 
        echo sprintf(__('View &#8220;%s&#8221;', FacebookPortal::TEXT_DOMAIN), $date['name']);
        ?>
" target="_blank"><?php 
        _e('View');
        ?>
</a></span>
                    </div>
                </td>
                <td class="page_id column-page_id"><?php 
        echo $date['facebook_page_id'];
        ?>
</td>
                <td class="date column-date"><?php 
        echo $date['post_updated'] ? FacebookPortal::date('Y-m-d H:i:s', $date['post_updated']) : '';
        ?>
</td>
                <td class="column-url"><a href="<?php 
        echo $date['page_url'];
        ?>
" title="<?php 
        echo sprintf(__('View &#8220;%s&#8221;', FacebookPortal::TEXT_DOMAIN), $date['name']);
        ?>
" target="_blank"><?php 
        echo $date['page_url'];
        ?>
</a></td>
            </tr>
<?php 
    }