74 lines
2.2 KiB
PHP
74 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
//use CodeIgniter\API\ResponseTrait;
|
|
|
|
class WrenchBlog extends BaseController
|
|
{
|
|
|
|
//use ResponseTrait;
|
|
|
|
protected $db;
|
|
public $con_name = 'wrench_blog';
|
|
|
|
private function apiData() {
|
|
$this->db = \Config\Database::connect($this->con_name);
|
|
$data = array();
|
|
|
|
try {
|
|
$mysql = "SELECT id, post_title, post_content,post_date,comment_count FROM wp_posts WHERE post_type='post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 9";
|
|
$mysql = "SELECT p1.id AS id, p1.*, wm2.meta_value FROM wp_posts p1 LEFT JOIN wp_postmeta wm1
|
|
ON (wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = '_thumbnail_id' )
|
|
LEFT JOIN
|
|
wp_postmeta wm2
|
|
ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file' AND wm2.meta_value IS NOT NULL )
|
|
WHERE
|
|
p1.post_status='publish'
|
|
AND p1.post_type='post'
|
|
ORDER BY p1.post_date DESC LIMIT 2000";
|
|
|
|
$query = $this->db->query($mysql);
|
|
|
|
$data['payload']['blogdata'] = $query->getResult('array');
|
|
$totalCount = count( $data['payload']['blogdata'] );
|
|
|
|
$randomIndex = rand(1, $totalCount);
|
|
|
|
$data['payload']['featured'] = $data['payload']['blogdata'][$randomIndex ];
|
|
$data['payload']['image_url'] = 'https://blog.float.sg/wp-content/uploads/';
|
|
$data['payload']['blog_url'] = 'https://blog.float.sg/';
|
|
$data['payload']['total'] = $totalCount;
|
|
} catch (Exception $ex) {
|
|
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function blogData($endpoint,$raw_array)
|
|
{
|
|
/*
|
|
$rawData = $this->apiData();
|
|
// $res1= $rawData[0]['payload'];
|
|
$res1= $rawData; //[0]['payload'];
|
|
$this->saveCache('blogdata',$res1['payload']);
|
|
*/
|
|
|
|
$res1 = $this->getCache($endpoint);
|
|
if (count($res1)==0){
|
|
$rawData = $this->apiData();
|
|
$res1= $rawData['payload']; //[0]['payload'];
|
|
$this->saveCache($endpoint,$res1);
|
|
}
|
|
//$resJson = $this->response->setJson($res1);
|
|
return $res1;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
} |